2025-03-24 19:02:58 +03:00

335 lines
13 KiB
PHP

<?php
require_once('/home/aqdamypanel/www/cp/fixed/config/go_con.php');
ini_set('memory_limit', '2048M');
set_time_limit(300);
require '/home/aqdamypanel/vendor/autoload.php';
$date = $_GET['d'] ?? null;
if (!$date) {
$date = new \DateTime();
} else {
$date = \DateTime::createFromFormat("Y-m", $date);
}
$year = $date->format("Y");
$month = $date->format("m");
$first = "{$year}-{$month}-01 03:00:00";
$second = new \DateTime($first);
$second->modify("first day of next month");
$year = $second->format("Y");
$month = $second->format("m");
$second = "{$year}-{$month}-01 03:00:00";
$query = <<<SQL
SELECT
invoices.id as invoice_id,
invoices.number_style as invoice_number,
tretment.name as client_name,
invoices.create_time as invoice_creation_time,
invoices_details.pay as amount_paid,
invoices_add.add as amount_added,
invoices_discount.discount as discount,
db_tables.name as discount_reason,
invoices_details_has_how_pay.notes as notes,
how_pay.name as payment_type,
COUNT(invoices_has_book.book_id) as booking_count
FROM invoices
INNER JOIN invoices_has_book ON invoices_has_book.invoices_id = invoices.id
INNER JOIN invoices_details ON invoices_details.invoices_id = invoices.id
LEFT JOIN invoices_add ON invoices_add.invoices_details_id = invoices_details.id
LEFT JOIN invoices_discount ON invoices_discount.invoices_details_id = invoices_details.id
LEFT JOIN invoices_discount_details ON invoices_discount_details.invoices_discount_id = invoices_discount.id
LEFT JOIN invoices_discount_details_has_db_tables ON invoices_discount_details_has_db_tables.invoices_discount_details_id = invoices_discount_details.id
LEFT JOIN db_tables ON db_tables.id = invoices_discount_details_has_db_tables.db_tables_id
INNER JOIN invoices_details_has_how_pay ON invoices_details_has_how_pay.invoices_details_id = invoices_details.id
INNER JOIN how_pay ON how_pay.id = invoices_details_has_how_pay.how_pay_id
INNER JOIN book ON book.id = invoices_has_book.book_id
INNER JOIN tretment ON tretment.id = book.tretment_id
WHERE
invoices.create_time BETWEEN '$first' AND '$second' AND
invoices.activation_id = 2
GROUP BY invoices.id
SQL;
$queryResult = mysqli_query($db, $query);
$error = mysqli_error($db);
$queryResult = mysqli_fetch_all($queryResult, MYSQLI_ASSOC);
if (!empty($error)) {
var_dump($queryResult, $error);
die;
} else {
// echo "<pre>" . var_export($queryResult, true) . "</pre>";
// die;
}
$detailsQuery = <<<'SQL'
SELECT
book.id as booking_id,
book.create_time as booking_registration_date,
dd.day_date as booking_date,
dd.day_time as booking_time,
branch.branch_name as employee_branch,
services.name as service_name,
emplyee.name as employee_name,
driver.name as driver_name,
services.price as service_official_price,
status.name as status_name
FROM invoices_details
INNER JOIN invoices_has_book ON invoices_has_book.invoices_id = invoices_details.invoices_id
INNER JOIN book ON book.id = invoices_has_book.book_id
INNER JOIN book_details dd ON dd.book_id = book.id
INNER JOIN book_has_driver_for_points ON book_has_driver_for_points.book_id = book.id
INNER JOIN emplyee as driver ON driver.id = book_has_driver_for_points.emplyee_id
INNER JOIN services ON services.id = dd.services_id
INNER JOIN branch ON branch.id = dd.branch_id
INNER JOIN emplyee ON emplyee.id = dd.emplyee_id
INNER JOIN status ON status.id = dd.status_id
INNER JOIN invoices_details_has_how_pay ON invoices_details_has_how_pay.invoices_details_id = invoices_details.id
INNER JOIN how_pay ON how_pay.id = invoices_details_has_how_pay.how_pay_id
WHERE
dd.id IN (SELECT MAX(book_details.id)
FROM book_details
WHERE book_id = dd.book_id
GROUP BY book_id) AND
dd.status_id IN (10, 12, 13, 16, 19, 22, 26) AND
invoices_details.invoices_id = ?
SQL;
$finalResult = [];
foreach ($queryResult as &$data) {
$invoiceId = $data['invoice_id'];
$stmt = mysqli_prepare($db, $detailsQuery);
$error = mysqli_error($db);
if (!empty($error)) {
var_dump($error);
die;
}
$stmt->bind_param('i', $invoiceId);
$stmt->execute();
$result = $stmt->get_result();
$result = $result->fetch_all(MYSQLI_ASSOC);
if (empty($result)) {
continue;
} else {
$data['booking_details'] = $result;
$finalResult[] = $data;
}
}
$lastTableDate = null;
$dayCount = 0;
$servicePriceTotal = 0.0;
$servicePriceTotalTaxed = 0.0;
$paidTotal = 0.0;
$discountTotal = 0.0;
$deficit = 0.0;
function printDayTotals(\DateTime $currentTableDate) {
global $servicePriceTotal;
global $servicePriceTotalTaxed;
global $dayCount;
global $paidTotal;
global $discountTotal;
global $lastTableDate;
global $deficit;
$total = round($servicePriceTotal, 2);
$totalTaxed = round($servicePriceTotalTaxed, 2);
$deficit = round($totalTaxed - $paidTotal - $discountTotal, 2);
if ($servicePriceTotal > 0) {
if ($deficit <= 0) {
$deficit = abs($deficit);
$print = <<<HTML
<tr class="totals">
<td colspan="2">عدد الفواتير: <p class="bold">$dayCount</p></td>
<td colspan="4">اجمالي الفواتير: <p class="bold">$paidTotal</p></td>
<td colspan="3">اجمالي الخصومات: <p class="bold">$discountTotal</p></td>
<td colspan="4">مجموع سعر الخدمات بدون الضريبة: <p class="bold">$total</p></td>
<td colspan="4">مجموع سعر الخدمات مع الضريبة: <p class="bold">$totalTaxed</p></td>
<td colspan="4">مبالغ إضافية دفعها العملاء: <p class="bold">$deficit</p></td>
</tr>
HTML;
} else {
$print = <<<HTML
<tr class="totals">
<td colspan="2">عدد الفواتير: <p class="bold">$dayCount</p></td>
<td colspan="4">اجمالي الفواتير: <p class="bold">$paidTotal</p></td>
<td colspan="3">اجمالي الخصومات: <p class="bold">$discountTotal</p></td>
<td colspan="4">مجموع سعر الخدمات بدون الضريبة: <p class="bold">$total</p></td>
<td colspan="4">مجموع سعر الخدمات مع الضريبة: <p class="bold">$totalTaxed</p></td>
<td colspan="4">خصم أموال لم يسجله النظام: <p class="bold">$deficit</p></td>
</tr>
HTML;
}
} else {
$print = "";
}
$dayCount = 0;
$servicePriceTotal = 0.0;
$servicePriceTotalTaxed = 0.0;
$paidTotal = 0.0;
$discountTotal = 0.0;
$lastTableDate = $currentTableDate;
echo $print;
}
?>
<style>
* {
font-family: 'Segoe UI';
}
th,
td {
border: 1px solid rgb(160 160 160);
padding: 8px 10px;
}
th[scope='col'] {
background-color: #505050;
color: #fff;
}
th[scope='row'] {
background-color: #d6ecd4;
}
td {
text-align: center;
}
tr:nth-of-type(even):not(.totals) {
background-color: #eee;
}
.totals {
background-color: #0F0;
color: #000;
}
.bold {
font-weight: bold;
}
.sticky {
position: sticky;
position: -webkit-sticky;
top: 0;
}
table {
border-collapse: collapse;
border: 2px solid rgb(140 140 140);
font-family: sans-serif;
font-size: 0.8rem;
letter-spacing: 1px;
}
</style>
<table dir="rtl">
<thead class="sticky">
<tr>
<th scope="col" rowspan="2">#</th>
<th scope="col" rowspan="2">معرف الفاتورة</th>
<th scope="col" rowspan="2">رقم الفاتورة</th>
<th scope="col" rowspan="2">اسم العميل</th>
<th scope="col" rowspan="2">تاريخ ووقت إنشاء الفاتورة</th>
<th scope="col" rowspan="2">اجمالي مبلغ الفاتورة بعد الخصم والضريبة</th>
<th scope="col" rowspan="2">الخصم</th>
<!-- <th scope="col" rowspan="2">سبب الخصم</th> -->
<th scope="col" rowspan="2">طريقة الدفع</th>
<th scope="col" rowspan="2">ملاحظات الفاتورة</th>
<th scope="col" colspan="11">تفاصيل الزيارة</th>
</tr>
<tr>
<th scope="col">رقم الحجز</th>
<th scope="col">تاريخ إنشاء الحجز</th>
<th scope="col">تاريخ الزيارة</th>
<th scope="col">وقت الزيارة</th>
<th scope="col">فرع خروج الفني</th>
<th scope="col">اسم السائق</th>
<th scope="col">اسم الخدمة</th>
<th scope="col">اسم الفني</th>
<th scope="col">سعر الخدمة الأساسي قبل الضريبة وقبل الخصم</th>
<th scope="col">سعر الخدمة بعد الضريبة وقبل الخصم</th>
<th scope="col">الحالة</th>
</tr>
</thead>
<tbody>
<?php foreach ($finalResult as $i => &$res): ?>
<?php
if (!$lastTableDate) {
$lastTableDate = \DateTime::createFromFormat('Y-m-d H:i:s', $res['invoice_creation_time']);
}
$currentTableDate = \DateTime::createFromFormat('Y-m-d H:i:s', $res['invoice_creation_time']);
$last = $lastTableDate->format("Y-m-d");
$current = $currentTableDate->format("Y-m-d");
$currentHour = $currentTableDate->format('H');
if ($currentTableDate->format('H') < 3) {
$currentTableDate->modify('-1 day');
$current = $currentTableDate->format('Y-m-d');
}
if ($last != $current) {
printDayTotals($currentTableDate);
}
$detailsCount = $res['booking_count'];
$paidTotal += round($res['amount_paid'], 2);
$discountTotal += round($res['discount'] * 1.15, 2);
?>
<?php $dayCount += 1; ?>
<tr>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $i + 1 ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['invoice_id'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['invoice_number'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['client_name'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['invoice_creation_time'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['amount_paid'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo round($res['discount'] * 1.15, 2) ?? 0.0 ?></td>
<!-- <td rowspan="<?php echo $detailsCount ?>">
<?php
// if ($res['discount_reason']) {
// if ($res['discount_reason'] == 'visits_add_or_desc_reason') {
// echo 'خصم من مسؤول الزيارات غير معروف سبب الخصم';
// } else {
// echo $res['discount_reason'];
// }
// } else if ($res['discount'] && $res['discount'] > 0) {
// echo 'غير معروف';
// } else if ($res['payment_type'] == 'رصيد اموال') {
// echo 'غير معروف سبب الخصم وغير معروف مبلغ الخصم';
// } else {
// echo 'بدون';
// }
?>
</td> -->
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['payment_type'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['notes'] ?></td>
<?php foreach ($res['booking_details'] as &$details): ?>
<?php $servicePriceTotal += $details['service_official_price']; ?>
<?php $servicePriceTotalTaxed += round($details['service_official_price'] * 1.15, 2); ?>
<td><?php echo $details['booking_id'] ?></td>
<td><?php echo $details['booking_registration_date'] ?></td>
<td><?php echo $details['booking_date'] ?></td>
<td><?php echo $details['booking_time'] ?></td>
<td><?php echo $details['employee_branch'] ?></td>
<td><?php echo $details['driver_name'] ?></td>
<td><?php echo $details['service_name'] ?></td>
<td><?php echo $details['employee_name'] ?></td>
<td><?php echo $details['service_official_price'] ?></td>
<td><?php echo round($details['service_official_price'] * 1.15, 2) ?></td>
<td><?php echo $details['status_name'] ?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
<?php
printDayTotals($currentTableDate);
?>
</tbody>
</table>