270 lines
8.1 KiB
PHP
Executable File

<?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;
$branchId = $_GET['b'] ?? 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
order_id as order_id,
TRIM(customer_name) as customer_name,
MAX(water) as water,
order_type as drink,
order_branch as branch_id,
branch.branch_name as branch_name,
order_status as status,
date as time
FROM o_drinks_orders
INNER JOIN branch ON branch.id = order_branch
WHERE
date BETWEEN ? AND ? AND
order_status = 'Done' AND
order_branch = ?
GROUP BY
UNIX_TIMESTAMP(date) DIV 1800,
drink,
customer_name
SQL;
$modeQuery = <<<SQL
SELECT
order_id as order_id,
TRIM(customer_name) as customer_name,
MAX(water) as water,
order_type as drink,
order_branch as branch_id,
branch.branch_name as branch_name,
order_status as status,
date as time
FROM o_drinks_orders
INNER JOIN branch ON branch.id = order_branch
WHERE
date BETWEEN ? AND ? AND
order_status = 'Done' AND
order_branch = ?
GROUP BY
UNIX_TIMESTAMP(date) DIV 1800,
drink,
customer_name
SQL;
$stmt = mysqli_prepare($db, $query);
$error = mysqli_error($db);
if (!empty($error)) {
var_dump($error);
die;
}
$stmt->bind_param('ssi', $first, $second, $branchId);
$stmt->execute();
$queryResult = $stmt->get_result();
$queryResult = $queryResult->fetch_all(MYSQLI_ASSOC);
if (!empty($error)) {
var_dump($queryResult, $error);
die;
} else {
// $data = [
// "length" => count($queryResult),
// "data" => $queryResult,
// ];
// echo "<pre>" . var_export($data, true) . "</pre>";
// die;
}
const TIME_FORMAT = 'Y-m-d H:i:s';
$lastTableDate = null;
$dayDrinks = [];
$dayCount = 0;
$dayWater = 0;
$drinks = [];
$water = 0;
function printDayTotals(\DateTime $currentTableDate) {
global $lastTableDate;
global $dayDrinks;
global $dayCount;
global $dayWater;
if ($dayCount > 0) {
$print = <<<HTML
<tr class="day-totals">
<td colspan="2">عدد الطلبات:<p class="bold">$dayCount</p></td>
<td colspan="5">اجمالي طلبات الموية:<p class="bold">$dayWater</p></td>
<tr>
HTML;
foreach ($dayDrinks as $name => $count) {
$print .= <<<HTML
<tr class="day-totals">
<td colspan="7" class="bold">$name: $count</td>
<tr>
HTML;
}
} else {
$print = "";
}
$dayCount = 0;
$dayWater = 0;
$dayDrinks = [];
$lastTableDate = $currentTableDate;
echo $print;
}
function printTotals() {
global $queryResult;
global $drinks;
global $water;
$count = count($queryResult);
$print = <<<HTML
<tr class="totals">
<td colspan="2">اجمالي الطلبات في الفرع:<p class="bold">$count</p></td>
<td colspan="5">اجمالي طلبات الموية:<p class="bold">$water</p></td>
<tr>
HTML;
foreach ($drinks as $name => $count) {
$print .= <<<HTML
<tr class="totals">
<td colspan="7" class="bold">$name: $count</td>
<tr>
HTML;
}
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;
}
.day-totals {
background-color: #0FF;
color: #000;
}
.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>
<pre>
<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>
</tr>
</thead>
<?php foreach ($queryResult as $i => &$res): ?>
<?php
if (!$lastTableDate) {
$lastTableDate = \DateTime::createFromFormat(TIME_FORMAT, $res['time']);
}
$currentTableDate = \DateTime::createFromFormat(TIME_FORMAT, $res['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);
}
$dayDrinks[$res['drink']] += 1;
$drinks[$res['drink']] += 1;
if (!empty($res['water'])) {
$dayWater += 1;
$water += 1;
}
$dayCount += 1;
?>
<tr>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $i + 1 ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['order_id'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['time'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['branch_name'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['customer_name'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['drink'] ?></td>
<td rowspan="<?php echo $detailsCount ?>"><?php echo $res['water'] ?></td>
</tr>
<?php endforeach; ?>
<?php
if (isset($currentTableDate)) {
printDayTotals($currentTableDate);
}
printTotals();
?>
</tbody>
</table>
</pre>