}
return $reports;
}
-}
\ No newline at end of file
+
+
+ /**
+ * @throws \yii\base\Exception
+ * @throws Exception
+ */
+ public function showDays($data) {
+ set_time_limit(600);
+ $days = [];
+ if (!empty($data->month)) {
+ $startDate = date("Y-m-01", strtotime($data->month));
+ $endDate = date("Y-m-t", strtotime($data->month));
+
+ $currentDate = $startDate;
+ while ($currentDate <= $endDate) {
+ $days[] = $currentDate;
+ $currentDate = date("Y-m-d", strtotime($currentDate . " +1 day"));
+ }
+ } else {
+ if (!empty($data->date)) {
+ foreach ($data->date as $day) {
+ $days[] = date("Y-m-d", strtotime($day));
+ }
+ }
+ }
+ $reports = [];
+
+ $cityStoreNames = ArrayHelper::map(CityStore::find()->where(['visible' => '1'])->all(), 'id', 'name');
+
+ $eitStores = ExportImportTable::find()->where(['export_val' => $data->stores])
+ ->select(['entity_id', 'export_val'])
+ ->where(['export_id' => 1, 'entity' => 'city_store'])
+ ->indexBy(['entity_id'])
+ ->asArray()->all();
+
+ $store_guids = [];
+ foreach ($data->stores as $store_id) {
+ $store_guids[$store_id] = $eitStores[$store_id]['export_val'];
+ }
+
+ $cond = ['or'];
+ foreach ($days as $ind => $day) {
+ $cond[]= ['between', 'date',
+ date("Y-m-d 00:00:00", strtotime($day)),
+ date("Y-m-d 23:59:59", strtotime($day))];
+ }
+ $employeesTotal = Sales::find()->select(["COUNT(*) as cnt", "admin_id"]) // , "DATA_FORMAT(date, '%Y-%m-%d') as day"
+ ->where(['store_id' => $data->stores])
+ ->andWhere($cond)
+ ->groupBy(['admin_id'])->asArray()->all();
+ $employeeCountTotal = count($employeesTotal);
+
+ foreach ($days as $ind => $day) {
+
+
+ $employeeCount = [];
+ foreach ($data->stores as $store_id) {
+ $employees = Sales::find()->select(["COUNT(*) as cnt", "admin_id"])
+ ->where(['between', 'date',
+ date("Y-m-d 00:00:00", strtotime($day)),
+ date("Y-m-d 23:59:59", strtotime($day))])
+ ->andWhere(['store_id' => $store_id])
+ ->groupBy(['admin_id'])->asArray()->all();
+ $employeeCount[$store_id] = count($employees);
+ }
+
+ $storeVisitorsQuantityTotal = [];
+ $storeSaleQuantityTotal = [];
+ $storeSaleTotalTotal = [];
+ $storeSaleReturnQuantityTotal = [];
+ $storeSaleReturnTotalTotal = [];
+ $storeSaleBonusCountTotal = [];
+ $storeSaleNewBonusCountTotal = [];
+ $storeSaleRepeatBonusCountTotal = [];
+ $totalWriteOffsPerDateTotal = [];
+ $totalWriteOffsPerMonthTotal = [];
+ $totalPayrollDaysTotal = [];
+ $totalPayrollMonthTotal = [];
+ $totalMatrixPerDayTotal = [];
+ $totalWrapPerDayTotal = [];
+ $totalServicePerDayTotal = [];
+ $totalPottedPerDayTotal = [];
+
+ $adminPayrollDaysMonth = AdminPayrollDays::find()->select(["FLOOR(SUM(day_payroll)) as total", 'store_id'])
+ ->where([
+ 'between', 'date',
+ date("Y-m-01 00:00:00", strtotime($day)),
+ date("Y-m-d 23:59:59", strtotime($day))])
+ ->andWhere(['store_id' => $data->stores])
+ ->groupBy(['store_id'])
+ ->indexBy('store_id')
+ ->asArray()->all();
+
+ $writeOffsMonth = WriteOffs::find()->select(['sum(summ) as total', 'store_id'])
+ ->where([
+ 'between', 'date',
+ date("Y-m-01 00:00:00", strtotime($day)),
+ date("Y-m-d 23:59:59", strtotime($day))])
+ ->andWhere(['type' => WriteOffsErp::WRITE_OFFS_TYPE_BRAK])
+ ->andWhere(['store_id' => array_values($store_guids)])
+ ->groupBy(['store_id'])
+ ->indexBy('store_id')
+ ->asArray()->all();
+
+ $salesMonth = Sales::find()->alias('s')->select([
+ "sum(CASE WHEN operation='Продажа' THEN s.summ ELSE (CASE WHEN operation='Возврат' THEN -s.summ ELSE 0 END) END) as total",
+ 'store_id_1c',
+ ])
+ ->where([
+ 'between', 'date',
+ date("Y-m-01 00:00:00", strtotime($day)),
+ date("Y-m-d 23:59:59", strtotime($day))])
+ ->andWhere(['order_id' => ['', '0']])
+ ->andWhere(['s.store_id' => $data->stores])
+ ->groupBy(['store_id_1c'])
+ ->indexBy('store_id_1c')
+ ->asArray()->all();
+
+
+
+ $adminPayrollDays = AdminPayrollDays::find()->select(["FLOOR(SUM(day_payroll)) as total", 'store_id'])
+ ->where(['date' => $day])
+ ->andWhere(['store_id' => $data->stores])
+ ->groupBy(['store_id'])
+ ->indexBy('store_id')
+ ->asArray()->all();
+
+ $date_start = $data->shift_type == 2 ?
+ date("Y-m-d 20:00:00", strtotime($day)) :
+ ($data->shift_type == 1 ?
+ date("Y-m-d 08:00:00", strtotime($day)) :
+ date("Y-m-d 00:00:00", strtotime($day)));
+ $date_end = $data->shift_type == 1 ?
+ date("Y-m-d 20:00:00", strtotime($day)) :
+ ($data->shift_type == 2 ?
+ date("Y-m-d 08:00:00", strtotime("+1 day", strtotime($day))) :
+ date("Y-m-d 00:00:00", strtotime("+1 day", strtotime($day))));
+
+// return [$adminPayrollDaysMonth, $adminPayrollDays];
+ $storeVisitorsQuery = StoreVisitors::find()
+ ->select([
+ 'counter' => new \yii\db\Expression("SUM(counter)"),
+ 'store_id'
+ ]);
+
+ if ($data->shift_type == 1) {
+ $storeVisitorsQuery->andWhere(['>=', 'date_hour', 8])->andWhere(['<=', 'date_hour', 20])
+ ->andWhere(['date' => date("Y-m-d", strtotime($day))]);
+ }
+ if ($data->shift_type == 2) {
+ $storeVisitorsQuery->andWhere([
+ 'or',
+ [
+ 'and',
+ ['>=', 'date_hour', 20],
+ ['<=', 'date_hour', 23],
+ ['date' => date("Y-m-d", strtotime($day))]
+ ],
+ [
+ 'and',
+ ['>=', 'date_hour', 0],
+ ['<=', 'date_hour', 7],
+ ['date' => date("Y-m-d", strtotime("+1 day", strtotime($day)))]
+ ],
+ ]);
+ }
+ if ($data->shift_type == 0) {
+ $storeVisitorsQuery->andWhere([
+ 'or',
+ [
+ 'and',
+ ['>=', 'date_hour', 8],
+ ['<=', 'date_hour', 23],
+ ['date' => date("Y-m-d", strtotime($day))]
+ ],
+ [
+ 'and',
+ ['>=', 'date_hour', 0],
+ ['<=', 'date_hour', 7],
+ ['date' => date("Y-m-d", strtotime("+1 day", strtotime($day)))]
+ ],
+ ]);
+ }
+
+ $storeVisitors = $storeVisitorsQuery->andWhere(['store_id' => $data->stores])
+ ->groupBy(['store_id'])
+ ->indexBy('store_id')->asArray()->all();
+// return [$storeVisitors];
+
+ $salesPhones = Sales::find()->select(["DISTINCT(phone)"])
+ ->where(['between', 'date', $date_start, $date_end])
+ ->andWhere(['IS NOT', 'phone', new Expression('NULL')])
+ ->andWhere(['store_id' => $data->stores])
+ ->asArray()->all();
+
+// return [$salesPhones];
+
+ $sales = Sales::find()->alias('s')->select(["COUNT(*) as cnt",
+ "sum(CASE WHEN operation='Продажа' THEN s.summ ELSE (CASE WHEN operation='Возврат' THEN -s.summ ELSE 0 END) END) as total",
+ "sum(CASE WHEN (s.phone != 0) AND (u.telegram_is_subscribed = 1) THEN 1 ELSE 0 END) as bonusUserCount",
+ "sum(CASE WHEN (s.phone != 0) AND (u.telegram_is_subscribed = 1) AND ((u.date_first_sale IS NOT NULL) AND (TO_CHAR(u.date_first_sale, 'YYYY-MM-DD') = TO_CHAR(s.date, 'YYYY-MM-DD'))) THEN 1 ELSE 0 END) as newBonusUserCount",
+ "sum(CASE WHEN (s.phone != 0) AND (u.telegram_is_subscribed = 1) AND ((u.date_first_sale IS NULL) OR (TO_CHAR(u.date_first_sale, 'YYYY-MM-DD') < TO_CHAR(s.date, 'YYYY-MM-DD'))) THEN 1 ELSE 0 END) as repeatBonusUserCount",
+ 'store_id_1c',
+ ])
+ ->leftJoin('users u', 'u.phone = s.phone::varchar(20) AND u.phone IN (\''
+ . implode('\',\'', ArrayHelper::getColumn($salesPhones, 'phone')) .'\')')
+ ->where(['between', 's.date', $date_start, $date_end])
+ ->andWhere(['order_id' => ['', '0']])
+ ->andWhere(['s.store_id' => $data->stores])
+ ->groupBy(['store_id_1c'])
+ ->indexBy('store_id_1c')
+ ->asArray()->all();
+
+// return [$sales];
+
+ $salesReturn = Sales::find()->alias('s')->select(["COUNT(*) as cnt", "sum(s.summ) as total", "store_id_1c"])
+ ->where(['operation' => Sales::OPERATION_RETURN])
+ ->andWhere(['order_id' => ['', '0']])
+ ->andWhere(['between', 's.date', $date_start, $date_end])
+ ->andWhere(['s.store_id' => $data->stores])
+ ->groupBy(['store_id_1c'])
+ ->indexBy('store_id_1c')
+ ->asArray()->all();
+
+// return [$salesReturn];
+
+ $writeOffs = WriteOffs::find()->select(['sum(summ) as total', 'store_id'])
+ ->where(['between', 'date', $date_start, $date_end])
+ ->andWhere(['type' => WriteOffsErp::WRITE_OFFS_TYPE_BRAK])
+ ->andWhere(['store_id' => array_values($store_guids)])
+ ->groupBy(['store_id'])
+ ->indexBy('store_id')
+ ->asArray()->all();
+
+// return [$writeOffsMonth, $writeOffs];
+
+ $specificSales = [];
+ foreach (['matrix', 'wrap', 'services', 'potted'] as $productTip) {
+ $productsClass = ProductsClass::find()->select(['category_id', 'tip'])
+ ->where(['tip' => $productTip])
+ ->indexBy('category_id')
+ ->asArray()->all();
+
+ $products1c = Products1c::find()->select(['id', 'parent_id', 'name'])
+ ->where(['parent_id' => array_keys($productsClass), 'tip' => 'products'])
+ ->indexBy(['id'])
+ ->asArray()->all();
+
+ $salesMatrix = Sales::find()->alias('s')->select([
+ "sum(CASE WHEN operation='Продажа' THEN p.summ ELSE (CASE WHEN operation='Возврат' THEN -p.summ ELSE 0 END) END) as total",
+ 'store_id'
+ ]) ->leftJoin('sales_products p', 'p.check_id = s.id')
+ ->where(['between', 's.date', $date_start, $date_end])
+ ->andWhere(['order_id' => ['', '0']])
+ ->andWhere(['p.product_id' => array_keys($products1c)])
+ ->andWhere(['store_id' => $data->stores])
+ ->groupBy(['store_id'])
+ ->indexBy('store_id')
+ ->asArray()->all();
+
+ $specificSales[$productTip] = $salesMatrix;
+ }
+
+// return [$specificSales];
+
+ $storeVisitorsQuantity = [];
+ $storeSaleQuantity = [];
+ $storeSaleTotal = [];
+ $storeSaleReturnQuantity = [];
+ $storeSaleReturnTotal = [];
+ $storeSaleBonusCount = [];
+ $storeSaleNewBonusCount = [];
+ $storeSaleRepeatBonusCount = [];
+ foreach ($data->stores as $store_id) {
+ $storeVisitorsQuantity[$store_id] = (int)($storeVisitors[$store_id]['counter'] ?? 0);
+ $storeSaleQuantity[$store_id] = (int)($sales[$eitStores[$store_id]['export_val']]['cnt'] ?? 0);
+ $storeSaleTotal[$store_id] = (int)($sales[$eitStores[$store_id]['export_val']]['total'] ?? 0);
+ $storeSaleReturnQuantity[$store_id] = (int)($salesReturn[$eitStores[$store_id]['export_val']]['cnt'] ?? 0);
+ $storeSaleReturnTotal[$store_id] = (int)($salesReturn[$eitStores[$store_id]['export_val']]['total'] ?? 0);
+ $storeSaleBonusCount[$store_id] = (int)($sales[$eitStores[$store_id]['export_val']]['bonusUserCount'] ?? 0);
+ $storeSaleNewBonusCount[$store_id] = (int)($sales[$eitStores[$store_id]['export_val']]['newBonusUserCount'] ?? 0);
+ $storeSaleRepeatBonusCount[$store_id] = (int)($sales[$eitStores[$store_id]['export_val']]['repeatBonusUserCount'] ?? 0);
+
+ $storeVisitorsQuantityTotal[$store_id] = ($storeVisitorsQuantityTotal[$store_id] ?? 0) + $storeVisitorsQuantity[$store_id];
+ $storeSaleQuantityTotal[$store_id] = ($storeSaleQuantityTotal[$store_id] ?? 0) + $storeSaleQuantity[$store_id];
+ $storeSaleTotalTotal[$store_id] = ($storeSaleTotalTotal[$store_id] ?? 0) + $storeSaleTotal[$store_id];
+ $storeSaleReturnQuantityTotal[$store_id] = ($storeSaleReturnQuantityTotal[$store_id] ?? 0) + $storeSaleReturnQuantity[$store_id];
+ $storeSaleReturnTotalTotal[$store_id] = ($storeSaleReturnTotalTotal[$store_id] ?? 0) + $storeSaleReturnTotal[$store_id];
+ $storeSaleBonusCountTotal[$store_id] = ($storeSaleBonusCountTotal[$store_id] ?? 0) + $storeSaleBonusCount[$store_id];
+ $storeSaleNewBonusCountTotal[$store_id] = ($storeSaleNewBonusCountTotal[$store_id] ?? 0) + $storeSaleNewBonusCount[$store_id];
+ $storeSaleRepeatBonusCountTotal[$store_id] = ($storeSaleRepeatBonusCountTotal[$store_id] ?? 0) + $storeSaleRepeatBonusCount[$store_id];
+
+ $totalWriteOffsPerDateTotal[$store_id] = ($totalWriteOffsPerDateTotal[$store_id] ?? 0) + (int)($writeOffs[$store_guids[$store_id]]["total"] ?? 0);
+ $totalPayrollDaysTotal[$store_id] = ($totalPayrollDaysTotal[$store_id] ?? 0) + (int)($adminPayrollDays[$store_id]['total'] ?? 0);
+
+ $totalSpecificPerDay = [];
+ foreach (['matrix', 'wrap', 'services', 'potted'] as $spec) {
+ $totalSpecificPerDay[$store_id][$spec] = 0;
+ foreach ($specificSales[$spec][$store_id] ?? [] as $specificSale) {
+ $totalSpecificPerDay[$store_id][$spec] += (float)($specificSale ?? 0);
+ }
+ }
+ $totalMatrixPerDay[$store_id] = $totalSpecificPerDay[$store_id]['matrix'];
+ $totalWrapPerDay[$store_id] = $totalSpecificPerDay[$store_id]['wrap'];
+ $totalServicePerDay[$store_id] = $totalSpecificPerDay[$store_id]['services'];
+ $totalPottedPerDay[$store_id] = $totalSpecificPerDay[$store_id]['potted'];
+
+ $totalMatrixPerDayTotal[$store_id] = ($totalMatrixPerDayTotal[$store_id] ?? 0) + $totalMatrixPerDay[$store_id];
+ $totalWrapPerDayTotal[$store_id] = ($totalWrapPerDayTotal[$store_id] ?? 0) + $totalWrapPerDay[$store_id];
+ $totalServicePerDayTotal[$store_id] = ($totalServicePerDayTotal[$store_id] ?? 0) + $totalServicePerDay[$store_id];
+ $totalPottedPerDayTotal[$store_id] = ($totalPottedPerDayTotal[$store_id] ?? 0) + $totalPottedPerDay[$store_id];
+ }
+
+
+
+ $stores = [];
+ $total = [];
+
+ foreach ($data->stores as $store_id) {
+ $store = [
+ "sale_month_total" => ($salesMonth[$store_guids[$store_id]]["total"] ?? 0),
+ "sale_total" => $storeSaleTotalTotal[$store_id] ?? 0,
+ "sale_quantity" => $storeSaleQuantityTotal[$store_id] ?? 0,
+ "sale_avg" => $storeSaleQuantityTotal[$store_id] ?? 0 > 0 ? floor($storeSaleTotalTotal[$store_id] / $storeSaleQuantityTotal[$store_id]) : 0,
+ "total_write_offs_per_date" => $totalWriteOffsPerDateTotal[$store_id] ?? 0,
+ "total_write_offs_per_date_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor($totalWriteOffsPerDateTotal[$store_id] / $storeSaleTotalTotal[$store_id] * 100) : 0,
+ "total_write_offs_per_month" => (int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0),//$totalWriteOffsPerMonthTotal[$store_id] ?? 0,
+// "total_write_offs_per_month_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor((int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0) / $storeSaleTotalTotal[$store_id] * 100) : 0,
+ "total_write_offs_per_month_percent" => ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) > 0 ? floor((int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0) / ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) * 100) : 0,
+ "total_payroll_days" => $totalPayrollDaysTotal[$store_id] ?? 0,
+ "total_payroll_days_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor($totalPayrollDaysTotal[$store_id] / $storeSaleTotalTotal[$store_id] * 100) : 0,
+ "total_payroll_month" => (int)($adminPayrollDaysMonth[$store_id]['total'] ?? 0),//$totalPayrollMonthTotal[$store_id] ?? 0,
+// "total_payroll_month_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor(($adminPayrollDaysMonth[$store_id]['total'] ?? 0) / $storeSaleTotalTotal[$store_id] * 100) : 0,
+ "total_payroll_month_percent" => ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) > 0 ? floor(($adminPayrollDaysMonth[$store_id]['total'] ?? 0) / ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) * 100) : 0,
+ "employee_sale_avg" => $employeeCount[$store_id] ?? 0 > 0 ? floor($storeSaleTotalTotal[$store_id] / $employeeCount[$store_id]) : 0,
+ "visitors_quantity" => $storeVisitorsQuantityTotal[$store_id] ?? 0,
+ "conversion" => $storeSaleQuantityTotal[$store_id] ?? 0 > 0 ? floor($storeVisitorsQuantityTotal[$store_id] / $storeSaleQuantityTotal[$store_id] * 100) : 0,
+ "bonus_user_count" => $storeSaleBonusCountTotal[$store_id] ?? 0,
+ "bonus_user_per_sale_percent" => $storeSaleQuantityTotal[$store_id] ?? 0 > 0 ? floor($storeSaleBonusCountTotal[$store_id] / $storeSaleQuantityTotal[$store_id] * 100) : 0,
+ "bonus_new_user_count" => $storeSaleNewBonusCountTotal[$store_id] ?? 0,
+ "bonus_repeat_user_count" => $storeSaleRepeatBonusCountTotal[$store_id] ?? 0,
+ "sale_return_quantity" => $storeSaleReturnQuantityTotal[$store_id] ?? 0,
+ "sale_return_total" => $storeSaleReturnTotalTotal[$store_id] ?? 0,
+ "total_matrix_per_day" => $totalMatrixPerDayTotal[$store_id] ?? 0,
+ "total_matrix_per_day_percent" => ($storeSaleTotalTotal[$store_id] ?? 0) > 0 ? floor(($totalMatrixPerDayTotal[$store_id] ?? 0) / $storeSaleTotalTotal[$store_id] * 100) : 0,
+ "total_wrap_per_day" => $totalWrapPerDayTotal[$store_id] ?? 0,
+ "total_services_per_day" => $totalServicePerDayTotal[$store_id] ?? 0,
+ "total_potted_per_day" => $totalPottedPerDayTotal[$store_id] ?? 0,
+ ];
+ $stores []= ['id' => $store_id, 'guid' => $eitStores[$store_id]['export_val'],
+ 'name' => $cityStoreNames[$store_id], 'data' => $store];
+
+ $total["sale_month_total"] = ($total["sale_month_total"] ?? 0) + ($salesMonth[$store_guids[$store_id]]["total"] ?? 0);
+ $total["sale_total"] = ($total["sale_total"] ?? 0) + ($storeSaleTotalTotal[$store_id] ?? 0);
+ $total["sale_quantity"] = ($total["sale_quantity"] ?? 0) + ($storeSaleQuantityTotal[$store_id] ?? 0);
+ $total["sale_avg"] = 0;
+ $total["total_write_offs_per_date"] = ($total["total_write_offs_per_date"] ?? 0) + ($totalWriteOffsPerDateTotal[$store_id] ?? 0);
+ $total["total_write_offs_per_date_percent"] = 0;
+ $total["total_write_offs_per_month"] = ($total["total_write_offs_per_month"] ?? 0) + (int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0); //($totalWriteOffsPerMonthTotal[$store_id] ?? 0);
+ $total["total_write_offs_per_month_percent"] = 0;
+ $total["total_payroll_days"] = ($total["total_payroll_days"] ?? 0) + ($totalPayrollDaysTotal[$store_id] ?? 0);
+ $total["total_payroll_days_percent"] = 0;
+ $total["total_payroll_month"] = ($total["total_payroll_month"] ?? 0) + (int)($adminPayrollDaysMonth[$store_id]['total'] ?? 0);//($totalPayrollMonthTotal[$store_id] ?? 0);
+ $total["total_payroll_month_percent"] = 0;
+ $total["employee_sale_avg"] = 0;
+ $total["visitors_quantity"] = ($total["visitors_quantity"] ?? 0) + ($storeVisitorsQuantityTotal[$store_id] ?? 0);
+ $total["conversion"] = 0;
+ $total["bonus_user_count"] = ($total["bonus_user_count"] ?? 0) + ($storeSaleBonusCountTotal[$store_id] ?? 0);
+ $total["bonus_user_per_sale_percent"] = 0;
+ $total["bonus_new_user_count"] = ($total["bonus_new_user_count"] ?? 0) + ($storeSaleNewBonusCountTotal[$store_id] ?? 0);
+ $total["bonus_repeat_user_count"] = ($total["bonus_repeat_user_count"] ?? 0) + ($storeSaleRepeatBonusCountTotal[$store_id] ?? 0);
+ $total["sale_return_quantity"] = ($total["sale_return_quantity"] ?? 0) + ($storeSaleReturnQuantityTotal[$store_id] ?? 0);
+ $total["sale_return_total"] = ($total["sale_return_total"] ?? 0) + ($storeSaleReturnTotalTotal[$store_id] ?? 0);
+ $total["total_matrix_per_day"] = ($total["total_matrix_per_day"] ?? 0) + ($totalMatrixPerDayTotal[$store_id] ?? 0);
+ $total["total_matrix_per_day_percent"] = $total["sale_total"] > 0 ? floor($total["total_matrix_per_day"] / $total["sale_total"] * 100) : 0;
+ $total["total_wrap_per_day"] = ($total["total_wrap_per_day"] ?? 0) + ($totalWrapPerDayTotal[$store_id] ?? 0);
+ $total["total_services_per_day"] = ($total["total_services_per_day"] ?? 0) + ($totalServicePerDayTotal[$store_id] ?? 0);
+ $total["total_potted_per_day"] = ($total["total_potted_per_day"] ?? 0) + ($totalPottedPerDayTotal[$store_id] ?? 0);
+ }
+ $total["sale_avg"] = $total["sale_quantity"] > 0 ? floor($total["sale_total"] / $total["sale_quantity"]) : 0;
+ $total["total_write_offs_per_date_percent"] = $total["sale_total"] > 0 ? floor($total["total_write_offs_per_date"] / $total["sale_total"] * 100) : 0;
+ $total["total_write_offs_per_month_percent"] = $total["sale_month_total"] > 0 ? floor($total["total_write_offs_per_month"] / $total["sale_month_total"] * 100) : 0;
+ $total["total_payroll_days_percent"] = $total["sale_total"] > 0 ? floor($total["total_payroll_days"] / $total["sale_total"] * 100) : 0;
+ $total["total_payroll_month_percent"] = $total["sale_month_total"] > 0 ? floor($total["total_payroll_month"] / $total["sale_month_total"] * 100) : 0;
+ $total["employee_sale_avg"] = $employeeCountTotal > 0 ? floor($total["sale_total"] / $employeeCountTotal) : 0;
+ $total["conversion"] = $total["visitors_quantity"] > 0 ? floor($total["sale_quantity"] / $total["visitors_quantity"] * 100) : 0;;
+ $total["bonus_user_per_sale_percent"] = $total["sale_quantity"] > 0 ? floor($total["bonus_user_count"] / $total["sale_quantity"] * 100) : 0;
+
+ $report = [
+ "date" => $day,
+ "stores" => $stores,
+ "total" => $total,
+ ];
+ $reports []= $report;
+ }
+ return $reports;
+ }
+}