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 .= "
" . var_export($var, true) . "
"; } 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(<<modify('-31 days'); $date = $date->modify('-1 days'); $clientEmployeeCount = fetch(<<= ? 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); } ?>
# اسم الموظف عدد العملاء متوسط عدد العملاء عدد النقاط الخدمات نقاط الرجوع نقاط العملاء الجدد الأموال للرجوع الأموال للجدد المستحقات
الإجمالي تدليك الأقدام تدليك جسم تدليك كامل أخرى النقاط (x15) حافز عدد العملاء الإجمالي بدون حافز نسبة الرجوع الإجمالي مع حافز نسبة الرجوع
العدد النسبة العدد النسبة العدد النسبة العدد النسبة