389 lines
12 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;
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";
function dd(...$vars) {
$result = "";
foreach ($vars as $var) {
$result .= "<pre>" . var_export($var, true) . "</pre>";
}
echo $result;
die;
}
function fetch(string $query, array $params = []): array {
global $db;
$stmt = mysqli_prepare($db, $query);
$error = mysqli_error($db);
if (!empty($error)) {
dd($error);
}
if (!empty($params)) {
$paramTypes = '';
foreach ($params as $param) {
if (is_int($param)) {
$paramTypes .= 'i';
} else if (is_float($param)) {
$paramTypes .= 'd';
} else {
$paramTypes .= 's';
}
}
$stmt->bind_param($paramTypes, ...$params);
}
$stmt->execute();
$error = mysqli_error($db);
if (!empty($error)) {
dd($error);
}
$result = $stmt->get_result();
return $result->fetch_all(MYSQLI_ASSOC);
}
const MASSAGE_FEET = 'تدليك أقدام';
const MASSAGE_BODY = 'تدليك جسم';
const MASSAGE_FULLBODY = 'تدليك كامل';
const MASSAGE_OTHER = 'أخرى';
function idToName(?int $id): string {
$result = null;
switch ($id) {
case 1:
case 30:
case 179:
case 183:
case 198:
$result = MASSAGE_FEET;
break;
case 2:
case 31:
case 180:
case 184:
case 197:
$result = MASSAGE_BODY;
break;
case 3:
case 32:
case 181:
case 185:
case 196:
$result = MASSAGE_FULLBODY;
break;
default:
$result = MASSAGE_OTHER;
}
return $result;
}
$test = fetch(<<<SQL
SELECT
book_details.day_date,
book.id AS book_id,
book.tretment_id AS client_id,
emplyee.id AS employee_id,
emplyee.name AS employee_name,
book_details.status_id AS status_id,
services.id AS service_id,
services.point AS points
FROM book_details
LEFT JOIN book ON book_details.book_id = book.id
LEFT JOIN services ON book_details.services_id = services.id
LEFT JOIN emplyee ON book_details.emplyee_id = emplyee.id
JOIN (
SELECT book_id, MAX(id) AS max_id
FROM book_details
GROUP BY book_id
) book_details_max
ON
book_details.book_id = book_details_max.book_id AND
book_details.id = book_details_max.max_id
WHERE
DATE_FORMAT(book_details.day_date, '%Y-%m') = DATE_FORMAT(?, '%Y-%m')
AND emplyee.occupation_id = 2
AND book_details.status_id IN (6,8,12,13,14,15)
SQL, [
$first
]);
$results = [];
foreach ($test as $points) {
$id = $points['employee_id'];
$results[$id]['employee_name'] = $points['employee_name'];
$date = new \DateTime($points['day_date']);
$minus31Days = (clone $date)->modify('-31 days');
$date = $date->modify('-1 days');
$clientEmployeeCount = fetch(<<<SQL
SELECT
COUNT(*) as client_count
FROM book_details
LEFT JOIN book ON book_details.book_id = book.id
JOIN (
SELECT book_id, MAX(id) AS max_id
FROM book_details
GROUP BY book_id
) book_details_max
ON
book_details.book_id = book_details_max.book_id AND
book_details.id = book_details_max.max_id
WHERE
book_details.day_date >= ? AND
book_details.day_date <= ? AND
book_details.status_id IN (6,8,12,13,14,15) AND
book_details.emplyee_id = ? AND
book.tretment_id = ?
SQL, [
$minus31Days->format('Y-m-d'),
$date->format('Y-m-d'),
$id,
$points['client_id'],
])[0]['client_count'];
$clientEmployeeCount += 1;
$results[$id]['clients'][$points['client_id']]['count'] += $clientEmployeeCount;
$results[$id]['clients'][$points['client_id']]['new'] ??= 0.0;
$results[$id]['clients'][$points['client_id']]['old'] ??= 0.0;
if ($clientEmployeeCount === 1) {
$results[$id]['clients'][$points['client_id']]['new'] += $points['points'];
} else if ($clientEmployeeCount === 2) {
$results[$id]['clients'][$points['client_id']]['old'] += $points['points'];
} else if ($clientEmployeeCount === 3) {
$results[$id]['clients'][$points['client_id']]['old_for_points'] += $points['points'];
$results[$id]['clients'][$points['client_id']]['old_25'] += $points['points'];
} else {
$results[$id]['clients'][$points['client_id']]['old'] += $points['points'];
}
$results[$id]['unique_clients'][$points['day_date']][$points['client_id']] += 1;
$results[$id]['points'] += $points['points'];
$results[$id]['booking_count'] += 1;
$results[$id]['service_counts'][MASSAGE_FEET] ??= 0;
$results[$id]['service_counts'][MASSAGE_BODY] ??= 0;
$results[$id]['service_counts'][MASSAGE_FULLBODY] ??= 0;
$results[$id]['service_counts'][MASSAGE_OTHER] ??= 0;
$serviceName = idToName($points['service_id']);
$results[$id]['service_counts'][$serviceName] += 1;
}
foreach ($results as $id => &$result) {
$result['client_count'] = 0;
foreach ($result['unique_clients'] as $day => $clients) {
$result['client_count'] += count($clients);
}
foreach ($result['clients'] as $clientId => $data) {
$result['new_client_points'] += $data['new'];
$result['new_client_money'] += $data['new'] * 15;
$result['returning_client_points'] += $data['old'] + $data['old_for_points'];
$result['returning_client_money'] += $data['old'] * 20;
$result['returning_client_money'] += $data['old_25'] * 25;
}
$count = $result['client_count'];
$mean = $count / 26.0;
$mean = floor($mean);
$result['client_count_money'] = 0.0;
for ($i = $mean; $i >= 5; --$i) {
if ($i <= 10 && $i >= 7) {
$result['client_count_money'] += 1000;
} else if ($i <= 6 && $i >= 5) {
$result['client_count_money'] += 500;
}
}
$result['points_money'] = $result['points'] * 15.0;
$result['total_without_returning_money'] += $result['client_count_money'];
$result['total_without_returning_money'] += $result['points_money'];
$result['total_with_returning_money'] += $result['client_count_money'];
$result['total_with_returning_money'] += $result['new_client_money'];
$result['total_with_returning_money'] += $result['returning_client_money'];
unset($result['clients']);
unset($result['unique_clients']);
unset($result['returning_clients']);
}
usort($results, function ($a, $b) {
return ($a['client_count'] <=> $b['client_count']) * -1;
});
foreach ($results as &$result) {
$total = $result['booking_count'];
$count = $result['service_counts'][MASSAGE_FEET];
$result['service_percentages'][MASSAGE_FEET] = '' . round(($count / $total) * 100, 2);
$count = $result['service_counts'][MASSAGE_BODY];
$result['service_percentages'][MASSAGE_BODY] = '' . round(($count / $total) * 100, 2);
$count = $result['service_counts'][MASSAGE_FULLBODY];
$result['service_percentages'][MASSAGE_FULLBODY] = '' . round(($count / $total) * 100, 2);
$count = $result['service_counts'][MASSAGE_OTHER];
$result['service_percentages'][MASSAGE_OTHER] = '' . round(($count / $total) * 100, 2);
}
?>
<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="3">#</th>
<th scope="col" rowspan="3">اسم الموظف</th>
<th scope="col" rowspan="3">عدد العملاء</th>
<th scope="col" rowspan="3">متوسط عدد العملاء</th>
<th scope="col" rowspan="3">عدد النقاط</th>
<th scope="col" rowspan="1" colspan="9">الخدمات</th>
<th scope="col" rowspan="3">نقاط الرجوع</th>
<th scope="col" rowspan="3">نقاط العملاء الجدد</th>
<th scope="col" rowspan="3">الأموال للرجوع</th>
<th scope="col" rowspan="3">الأموال للجدد</th>
<th scope="col" rowspan="1" colspan="4">المستحقات</th>
</tr>
<tr>
<th scope="col" rowspan="2">الإجمالي</th>
<th scope="col" colspan="2">تدليك الأقدام</th>
<th scope="col" colspan="2">تدليك جسم</th>
<th scope="col" colspan="2">تدليك كامل</th>
<th scope="col" colspan="2">أخرى</th>
<th scope="col" rowspan="2">النقاط (x15)</th>
<th scope="col" rowspan="2">حافز عدد العملاء</th>
<th scope="col" rowspan="2">الإجمالي بدون حافز نسبة الرجوع</th>
<th scope="col" rowspan="2">الإجمالي مع حافز نسبة الرجوع</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>
</tr>
</thead>
<tbody>
<?php
$count = count($results);
?>
<?php for ($i = 1; $i <= $count; ++$i): ?>
<?php $employee = $results[$i - 1]; ?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $employee['employee_name'] ?></td>
<td><?php echo $employee['client_count'] ?></td>
<td><?php echo round($employee['client_count'] / 26.0, 2) ?></td>
<td><?php echo $employee['points'] ?></td>
<td><?php echo $employee['booking_count'] ?></td>
<td><?php echo $employee['service_counts'][MASSAGE_FEET] ?></td>
<td><?php echo $employee['service_percentages'][MASSAGE_FEET] ?></td>
<td><?php echo $employee['service_counts'][MASSAGE_BODY] ?></td>
<td><?php echo $employee['service_percentages'][MASSAGE_BODY] ?></td>
<td><?php echo $employee['service_counts'][MASSAGE_FULLBODY] ?></td>
<td><?php echo $employee['service_percentages'][MASSAGE_FULLBODY] ?></td>
<td><?php echo $employee['service_counts'][MASSAGE_OTHER] ?></td>
<td><?php echo $employee['service_percentages'][MASSAGE_OTHER] ?></td>
<td><?php echo $employee['returning_client_points'] ?></td>
<td><?php echo $employee['new_client_points'] ?></td>
<td><?php echo $employee['returning_client_money'] ?></td>
<td><?php echo $employee['new_client_money'] ?></td>
<td><?php echo $employee['points_money'] ?></td>
<td><?php echo $employee['client_count_money'] ?></td>
<td><?php echo $employee['total_without_returning_money'] ?></td>
<td><?php echo $employee['total_with_returning_money'] ?></td>
</tr>
<?php endfor; ?>
</tbody>
</pre>