where('is_active', '=', 1)->first(); $dirname = __DIR__ . "\\..\\..\\..\\Exchange\\" . $terminal['key'] . "\\"; $filename = "newyear.json"; if (!is_dir($dirname)) { mkdir($dirname, 0777); } if (!file_exists($dirname . $filename)) { $info = ExchangeItems::where('menu_code', '>', 0) ->where('real_price', '>', 1) ->where('created_at', '>=', '2021-01-01 00:00:00') ->where('created_at', '<', '2021-12-31 23:59:59') ->get() ->unique('menu_code'); $count = ExchangeItems::where('menu_code', '>', 0)->where('created_at', '>=', '2021-01-01 00:00:00')->where('created_at', '<', '2021-12-31 23:59:59')->count(); if ($count > 0) { foreach ($info as $key => $value) { $out[] = $value; } foreach ($out as $key => $item) { $dishInfo = Dishes::where('code', '=', $item['menu_code']) ->where('legacy_code', '=', $item['dishes_code']) // ->where('is_history', '=', 0) ->first(); $onlineDishInfo = ExchangeItems::where('menu_code', '=', $item['menu_code']) ->where('dishes_code', '=', $item['dishes_code']) ->where('created_at', '>=', '2021-01-01 00:00:00') ->where('created_at', '<', '2021-12-31 23:59:59') ->first(); $dishName = $dishInfo['name']; $dishCount = ExchangeItems::where('menu_code', '=', $item['menu_code']) ->where('created_at', '>=', '2021-01-01 00:00:00') ->where('created_at', '<', '2021-12-31 23:59:59') ->sum('count'); $dishSum = $onlineDishInfo['real_price'] * $dishCount; if ($dishSum > 0) { $dishTotalCost = round(($onlineDishInfo['special_price'] * $dishCount), 2); $dishPercent = round((100 - ((($dishSum - $dishTotalCost) * 100) / $dishSum)), 2); $dishProfit = $dishSum - $dishTotalCost; } else { $dishTotalCost = 0; $dishPercent = 0; $dishProfit = 0; } $dishes[] = array('name' => $dishName, 'count' => intval($dishCount), 'sum' => intval($dishSum), 'totalCost' => intval($dishTotalCost), 'percentProffit' => $dishPercent, 'proffit' => $dishProfit); } for ($i = 0; $i < count($dishes); $i++) { $sortkey[$i] = $dishes[$i]['count']; } arsort($sortkey); foreach ($sortkey as $key => $key) { $sorted[] = $dishes[$key]; } $sorted = array_slice($sorted, 0, 3); //End top dishes counter //Start top day //SELECT COUNT(`code`) AS `orders_counter`, `shift_id` FROM `exchange_orders` WHERE `is_closed` > 0 AND `opened` > '2021-01-01 00:00:00' GROUP BY `shift_id` ORDER BY `orders_counter` desc $shifts = ExchangeOrders::where('is_closed', '>', 0) ->where('opened', '>', '2021-01-01 00:00:00') ->get() ->unique('shift_id'); foreach ($shifts as $key => $shift) { $shift_id = $shift['shift_id']; $shift_order_count = ExchangeOrders::where('shift_id', '=', $shift_id)->count(); $shift_order_sum = ExchangeOrders::where('shift_id', '=', $shift_id)->sum('order_sum'); $shift_date = ExchangeOrders::where('shift_id', '=', $shift_id)->first(); $tr_m = ['Нулябрь' /* для сдвига индекса на +1*/, 'Января', 'Февраля', 'Марта', 'Апреля', 'Мая', 'Июня', 'Июля', 'Августа', 'Сентября', 'Октября', 'Ноября', 'Декабря']; $exp_date = getdate(strtotime($shift_date['opened'])); $full_datedate = sprintf( '%d %s %d', $exp_date['mday'], $tr_m[$exp_date['mon']], $exp_date['year'] ); $shift_counter[] = array('shift_id' => $shift_id, 'count' => $shift_order_count, 'date' => $full_datedate, 'sum' => $shift_order_sum); } for ($i = 0; $i < count($shift_counter); $i++) { $shift_counter_sortkey[$i] = $shift_counter[$i]['count']; } arsort($shift_counter_sortkey); foreach ($shift_counter_sortkey as $key => $key) { $shift_counter_sorted[] = $shift_counter[$key]; } $shift_counter_sorted = array_slice($shift_counter_sorted, 0, 1); //End top day //Return data $data = [ 'status' => 'success', 'count' => $count, 'dishes' => $sorted, 'top_shift' => $shift_counter_sorted, 'in_file' => 1, ]; $handle = fopen($dirname . $filename, 'w+'); fputs($handle, json_encode($data)); fclose($handle); return $data; } else { return [ 'status' => 'success', 'count' => 0, 'dishes' => [], ]; } } else { $data = json_decode(file_get_contents($dirname . $filename), true); return $data; } } }