Files
admin-php-module/commands/GETDataReport.php
miroman-afk 229ab8667e v.2.34
Обновлены округления
Добавлены миграции
2023-08-15 12:19:34 +03:00

2467 lines
139 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Commands;
use App\Component\Models\Base;
use App\Component\Models\Cache;
use App\Component\Models\Client;
use App\Component\Models\ClientsActions;
use App\Component\Models\Dishes;
use App\Component\Models\ExchangeActions;
use App\Component\Models\ExchangeDeleted;
use App\Component\Models\ExchangeItems;
use App\Component\Models\ExchangeOrders;
use App\Component\Models\ExchangeShifts;
use App\Component\Models\Filesystem;
use App\Component\Models\Printer;
use App\Component\Models\Reasons;
use App\Component\Models\Report;
use App\Component\Models\Settings;
use App\Component\Models\Staff;
use App\Component\Models\Terminal;
use App\Component\Models\Units;
use App\Console\Commands\HRCCommand;
use App\Console\Commands\HRCCommandInterface;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class GETDataReport extends HRCCommand implements HRCCommandInterface
{
protected $signature = 'getdatareport';
public function command($input, $output = null)
{
$reportType = $input['type'];
if (isset($reportType)) {
if ($reportType == 'merge') {
$code = Filesystem::GetCode($input['token']);
$cache = Cache::get($code, 'shift', $input);
if ($cache) {
return $cache;
}
$merged_order_items = [];
$orders = ExchangeActions::select('order_code')
->where('shift_id', $input['shift_id'])
->groupBy('order_code')
->get();
foreach ($orders as $key => $order) {
$actions = ExchangeActions::where('order_code', $order['order_code'])
->where('shift_id', $input['shift_id'])
->orderBy('time', 'asc')
->get();
foreach ($actions as $key => $item) {
if ($item['action_type'] == 31) {
$merge_order = intval($item['value']);
$item_actions = ExchangeActions::where('order_code', $merge_order)
->where('shift_id', intval($input['shift_id']))
->where('action_type', 32)
->get();
foreach ($item_actions as $key => $item_action) {
if ($item_action['more']) {
$merge_trigger = 1;
$merge_items_before = $merge_items_after = $merge_items = $merge_order_item_before = $merge_order_item_after = $merged_items = [];
if (Base::validate_json($item_action['more'])) {
$merge_order_items = json_decode(utf8_encode($item_action['more']), true, JSON_INVALID_UTF8_SUBSTITUTE);
} else {
$merge_order_items = json_decode(utf8_encode(base64_decode($item_action['more'], TRUE)), true, JSON_INVALID_UTF8_SUBSTITUTE);
}
$merge_order_items_before = $merge_order_items['before']['items'];
$merge_order_items_after = $merge_order_items['after']['items'];
foreach ($merge_order_items_before as $key => $merge_order_item_before) {
$merge_items_before[] = array(
'id' => $key + 1,
'code' => $merge_order_item_before['id'],
'name' => $merge_order_item_before['name'],
'count' => $merge_order_item_before['count'],
'sale_price' => $merge_order_item_before['sale_price']
);
}
foreach ($merge_order_items_after as $key => $merge_order_item_after) {
$merge_items_after[] = array(
'id' => $key + 1,
'code' => $merge_order_item_after['id'],
'name' => $merge_order_item_after['name'],
'count' => $merge_order_item_after['count'],
'sale_price' => $merge_order_item_after['sale_price']
);
}
foreach ($merge_order_items_after as $key => $merge_order_item_after) {
if (!in_array(array(
'id' => $key + 1,
'code' => $merge_order_item_after['id'],
'name' => $merge_order_item_after['name'],
'count' => $merge_order_item_after['count'],
'sale_price' => $merge_order_item_after['sale_price']), $merge_items_before, true)
) {
$merged_items[] = array(
'code' => $merge_order_item_after['id'],
'name' => $merge_order_item_after['name'],
'count' => $merge_order_item_after['count'],
'sale_price' => $merge_order_item_after['sale_price']
);
}
}
foreach ($merged_items as $key => $merged_item) {
$merge_items[] = array(
'id' => $key + 1,
'code' => $merged_item['code'],
'name' => $merged_item['name'],
'count' => $merged_item['count'],
'sale_price' => $merged_item['sale_price']
);
}
$staff_name = Staff::getName($item['who']);
$start_order_place = ExchangeActions::where('order_code', $item['order_code'])
->where('action_type', 45)
->where('shift_id', $input['shift_id'])
->first();
$start_order_place = Base::data_decode($start_order_place['value']);
$end_order_place = ExchangeActions::where('order_code', $merge_order)
->where('action_type', 45)
->where('shift_id', $input['shift_id'])
->first();
$end_order_place = Base::data_decode($end_order_place['value']);
$merged_order_items[] = array(
'start_order' => $item['order_code'],
'end_order' => $merge_order,
'time' => date('d.m.Y H:i:s', strtotime($item['time'])),
'staff' => $staff_name,
'start_order_place' => $start_order_place,
'end_order_place' => $end_order_place,
'merge_order_items' => $merge_order_items,
'items' => $merge_items
);
}
}
}
}
}
$return = array(
'status' => 'success',
'shift_id' => $input['shift_id'],
'count' => count($merged_order_items),
'data' => $merged_order_items,
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
if ($reportType == 'slice') {
$code = Filesystem::GetCode($input['token']);
$cache = Cache::get($code, 'shift', $input);
if ($cache) {
return $cache;
}
$sliced_order_data = [];
$orders = ExchangeActions::select('order_code')
->where('shift_id', $input['shift_id'])
->groupBy('order_code')
->get();
$action_count = ExchangeActions::where('shift_id', $input['shift_id'])
->where('action_type', 35)
->count();
if ($action_count > 0) {
foreach ($orders as $key => $order) {
$actions = ExchangeActions::where('order_code', $order['order_code'])
->where('shift_id', $input['shift_id'])
->where('action_type', 35)
->orderBy('time', 'asc')
->get();
foreach ($actions as $key => $action) {
$value = $action['value'];
$codeOrder = $action['order_code'];
if (Base::validate_json($action['more'])) {
$moved_items = json_decode(utf8_encode($action['more']), true, JSON_INVALID_UTF8_SUBSTITUTE);
} else {
$moved_items = json_decode(utf8_encode(base64_decode($action['more'], TRUE)), true, JSON_INVALID_UTF8_SUBSTITUTE);
}
$from_place = ExchangeActions::where('order_code', $codeOrder)
->where('action_type', 45)
->where('shift_id', $input['shift_id'])
->first();
$from_place = Base::data_decode($from_place['value']);
$to_place = ExchangeActions::where('order_code', $value)
->where('action_type', 45)
->where('shift_id', $input['shift_id'])
->first();
$to_place = Base::data_decode($to_place['value']);
$to_action = ExchangeActions::where('value', $codeOrder)
->where('order_code', $value)
->where('action_type', 34)
->where('shift_id', $input['shift_id'])
->first();
if (Base::validate_json($to_action['more'])) {
$start_data = json_decode(utf8_encode($to_action['more']), true, JSON_INVALID_UTF8_SUBSTITUTE);
} else {
$start_data = json_decode(utf8_encode(base64_decode($to_action['more'], TRUE)), true, JSON_INVALID_UTF8_SUBSTITUTE);
}
$staff_name = Staff::getName($action['who']);
$time = date('d.m.Y H:i:s', strtotime($action['time']));
$sliced_order_data[] = array(
'from_order' => $codeOrder,
'from_place' => $from_place,
'staff' => $staff_name,
'time' => $time,
'start_data' => $start_data,
'to_order' => $value,
'to_place' => $to_place,
'moved_items' => $moved_items
);
}
}
}
$return = array(
'status' => 'success',
'shift_id' => $input['shift_id'],
'count' => $action_count,
'data' => $sliced_order_data,
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
if ($reportType == 'move') {
$code = Filesystem::GetCode($input['token']);
$cache = Cache::get($code, 'shift', $input);
if ($cache) {
return $cache;
}
$orders = ExchangeActions::select('order_code')
->where('shift_id', $input['shift_id'])
->groupBy('order_code')
->get();
foreach ($orders as $key => $order) {
$actions = ExchangeActions::where('order_code', $order['order_code'])
->where('shift_id', $input['shift_id'])
->orderBy('time', 'asc')
->get();
foreach ($actions as $key => $item) {
$moved_order_data = [];
if ($item['action_type'] == 39) {
$staff_name_to = Staff::getName($item['who']);
$time_to = date('d.m.Y H:i:s', strtotime($item['time']));
$data_from = ExchangeActions::where('order_code', $item['order_code'])
->where('shift_id', $input['shift_id'])
->where('action_type', 45)
->first();
$staff_name_from = Staff::getName($data_from['who']);
$time_from = date('d.m.Y H:i:s', strtotime($data_from['time']));
$moved_order_data[] = array(
'order' => $item['order_code'],
'move_from' => Base::data_decode($data_from['value']),
'move_from_staff' => $staff_name_from,
'move_from_time' => $time_from,
'move_to' => Base::data_decode($item['value']),
'move_to_staff' => $staff_name_to,
'move_to_time' => $time_to
);
}
}
}
$return = array(
'status' => 'success',
'count' => count($moved_order_data),
'data' => $moved_order_data,
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
if ($reportType == 'staff') {
$work_code = Filesystem::GetCode($input['token']);
$cache = Cache::get($work_code, 'shift', $input);
if ($cache) {
return $cache;
}
if (isset($input['shift_id'])) {
$staff = $staff_data = [];
$countOpened = $countClosed = $sumOpened = $sumClosed = $inputCount = $inputSum = $outputCount = $outputSum = $annulCount = $annulSum = $returnCount = $returnSum = $deletedCount = $deletedSum = 0;
$openedOrders = ExchangeOrders::select('who_open')
->where('shift_id', $input['shift_id'])
->where('who_open', '>', 0)
->groupBy('who_open')
->get();
$closedOrders = ExchangeOrders::select('who_close')
->where('shift_id', $input['shift_id'])
->where('who_close', '>', 0)
->groupBy('who_close')
->get();
foreach ($openedOrders as $key => $openedOrder) {
array_push($staff, $openedOrder['who_open']);
}
foreach ($closedOrders as $key => $closedOrder) {
array_push($staff, $closedOrder['who_close']);
}
$staff = array_unique($staff);
foreach ($staff as $key => $value) {
$staffId = intval($value);
$name = Staff::getName($staffId);
$code = $staffId;
$countOpened = ExchangeOrders::where('who_open', $staffId)
->where('shift_id', $input['shift_id'])
->where('is_deleted', 0)
->where('is_returned', 0)
->where('order_sum', '>', 0)
->count();
$countClosed = ExchangeOrders::where('who_close', $staffId)
->where('shift_id', $input['shift_id'])
->where('is_deleted', 0)
->where('is_returned', 0)
->where('order_sum', '>', 0)
->count();
$sumOpened = ExchangeOrders::where('who_open', $staffId)
->where('shift_id', $input['shift_id'])
->where('is_deleted', 0)
->where('is_returned', 0)
->where('order_sum', '>', 0)
->sum('order_sum');
$sumClosed = ExchangeOrders::where('who_close', $staffId)
->where('shift_id', $input['shift_id'])
->where('is_deleted', 0)
->where('is_returned', 0)
->where('order_sum', '>', 0)
->sum('order_sum');
$inputCount = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 49)
->count();
$inputSum = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 49)
->sum('value');
$outputCount = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 50)
->count();
$outputSum = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 50)
->sum('value');
$annulCount = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 43)
->count();
$annulOrders = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 43)
->get();
foreach ($annulOrders as $key => $annulOrder) {
$annulOrderSum = ExchangeOrders::where('code', $annulOrder['order_code'])
->where('shift_id', $input['shift_id'])
->first();
$annulSum = $annulSum + $annulOrderSum['order_sum'];
}
$returnCount = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 19)
->count();
$returnOrders = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 19)
->get();
foreach ($returnOrders as $key => $returnOrder) {
$returnOrderSum = ExchangeOrders::where('code', $returnOrder['order_code'])
->where('shift_id', $input['shift_id'])
->first();
$returnSum = $returnSum + abs($returnOrderSum['order_sum']);
}
$deletedCount = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 8)
->where('reason', '>', 0)
->count();
$deletedOrders = ExchangeActions::where('who', $staffId)
->where('shift_id', $input['shift_id'])
->where('action_type', 8)
->where('reason', '>', 0)
->get();
foreach ($deletedOrders as $key => $deletedOrder) {
$deletedOrdersSum = ExchangeDeleted::where('order_code', $deletedOrder['order_code'])
->where('shift_id', $input['shift_id'])
->sum('sale_price');
$deletedSum = $deletedSum + $deletedOrdersSum;
}
if ($deletedSum === 0) {
$deletedCount = 0;
}
$staff_data[] = array(
'name' => $name,
'code' => $code,
'opened_count' => intval($countOpened),
'closed_count' => intval($countClosed),
'opened_sum' => floatval($sumOpened),
'closed_sum' => floatval($sumClosed),
'input_count' => $inputCount,
'output_count' => $outputCount,
'input_sum' => $inputSum,
'output_sum' => $outputSum,
'annul_count' => $annulCount,
'annul_sum' => $annulSum,
'return_count' => $returnCount,
'return_sum' => round($returnSum, 2),
'deleted_count' => $deletedCount,
'deleted_sum' => $deletedSum
);
}
}
if (isset($input['start_date']) && isset($input['end_date'])) {
$staff = $staff_data = $shifts = [];
$countOpened = $countClosed = $sumOpened = $sumClosed = $inputCount = $inputSum = $outputCount = $outputSum = $annulCount = $annulSum = $returnCount = $returnSum = $deletedCount = $deletedSum = 0;
$end_date = date('Y-m-d H:i:s', strtotime($input['end_date']));
$start_date = date('Y-m-d H:i:s', strtotime($input['start_date']));
$shiftIds = ExchangeShifts::where('opened', '>', $start_date)
->where('opened', '<', $end_date)
->get();
foreach ($shiftIds as $key => $shiftId) {
array_push($shifts, $shiftId['id']);
}
$openedOrders = ExchangeOrders::select('who_open')
->whereIn('shift_id', $shifts)
->where('who_open', '>', 0)
->groupBy('who_open')
->get();
$closedOrders = ExchangeOrders::select('who_close')
->whereIn('shift_id', $shifts)
->where('who_close', '>', 0)
->groupBy('who_close')
->get();
foreach ($openedOrders as $key => $openedOrder) {
array_push($staff, $openedOrder['who_open']);
}
foreach ($closedOrders as $key => $closedOrder) {
array_push($staff, $closedOrder['who_close']);
}
$staff = array_unique($staff);
foreach ($staff as $key => $value) {
$staffId = intval($value);
$name = Staff::getName($staffId);
$code = $staffId;
$countOpened = ExchangeOrders::where('who_open', $staffId)
->whereIn('shift_id', $shifts)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('order_sum', '>', 0)
->count();
$countClosed = ExchangeOrders::where('who_close', $staffId)
->whereIn('shift_id', $shifts)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('order_sum', '>', 0)
->count();
$sumOpened = ExchangeOrders::where('who_open', $staffId)
->whereIn('shift_id', $shifts)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('order_sum', '>', 0)
->sum('order_sum');
$sumClosed = ExchangeOrders::where('who_close', $staffId)
->whereIn('shift_id', $shifts)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('order_sum', '>', 0)
->sum('order_sum');
$inputCount = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 49)
->count();
$inputSum = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 49)
->sum('value');
$outputCount = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 50)
->count();
$outputSum = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 50)
->sum('value');
$annulCount = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 43)
->count();
$annulOrders = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 43)
->get();
foreach ($annulOrders as $key => $annulOrder) {
$annulSum = $annulSum + ExchangeOrders::where('code', $annulOrder['order_code'])
->whereIn('shift_id', $shifts)
->sum('order_sum');
}
$returnCount = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 19)
->count();
$returnOrders = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 19)
->get();
foreach ($returnOrders as $key => $returnOrder) {
$returnSum = $returnSum + abs(ExchangeOrders::where('code', $returnOrder['order_code'])
->whereIn('shift_id', $shifts)
->sum('order_sum'));
}
$deletedCount = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 8)
->where('reason', '>', 0)
->count();
$deletedOrders = ExchangeActions::where('who', $staffId)
->whereIn('shift_id', $shifts)
->where('action_type', 8)
->where('reason', '>', 0)
->get();
foreach ($deletedOrders as $key => $deletedOrder) {
$deletedSum = $deletedSum + ExchangeDeleted::where('order_code', $deletedOrder['order_code'])
->whereIn('shift_id', $shifts)
->sum('sale_price');
}
if ($deletedSum === 0) {
$deletedCount = 0;
}
$staff_data[] = array(
'name' => $name,
'code' => $code,
'opened_count' => intval($countOpened),
'closed_count' => intval($countClosed),
'opened_sum' => floatval($sumOpened),
'closed_sum' => floatval($sumClosed),
'input_count' => $inputCount,
'output_count' => $outputCount,
'input_sum' => $inputSum,
'output_sum' => $outputSum,
'annul_count' => $annulCount,
'annul_sum' => $annulSum,
'return_count' => $returnCount,
'return_sum' => round($returnSum, 2),
'deleted_count' => $deletedCount,
'deleted_sum' => $deletedSum
);
}
if (Report::where('start_date', $start_date)->where('end_date', $end_date)->where('report_type', 'staff')->count() < 1) {
$report = new Report;
$report->user_id = intval($input['admin_user_id']);
$report->name = 'Отчет по персоналу';
$report->shift_id = 0;
$report->start_date = $start_date;
$report->end_date = $end_date;
$report->report_type = 'staff';
$report->save();
}
}
$return = array(
'status' => 'success',
'staffs' => $staff_data,
);
if (!$cache) {
$cache = Cache::store(strval($work_code), 'shift', $input, $return);
}
return $cache;
}
if ($reportType == 'deleted') {
$code = Filesystem::GetCode($input['token']);
$cache = Cache::get($code, 'shift', $input);
if ($cache) {
return $cache;
}
if (isset($input['shift_id'])) {
$shift_id = $input['shift_id'];
$actionsResult = ExchangeDeleted::select('order_code', 'menu_code', 'count', 'sale_price', 'item_id')
->with('deletedActions', function ($q) use ($shift_id) {
$q->where('shift_id', '=', $shift_id);
})
->where('shift_id', $shift_id)
->get();
$orders = ExchangeDeleted::select('order_code')
->where('shift_id', $shift_id)
->get();
$ordersCount = ExchangeDeleted::select('order_code')
->where('shift_id', $shift_id)
->count();
if ($ordersCount === 0) {
return [
'status' => 'success',
'message' => 'Удаления отсутствуют',
'totalCount' => 0
];
}
$totalCount = 0;
$totalSum = 0;
$items = [];
foreach ($orders as $order) {
$result[$order['order_code']]['items'] = [];
}
foreach ($actionsResult as $actionResult) {
foreach ($actionResult->deletedActions as $item) {
$count = round(Base::toFloat($item['value']), 3);
$who = Staff::getName(intval($item['who']));
$time = $item['time'];
$reason = Reasons::getName(intval($item['reason']));
$dish = Dishes::getName($actionResult['menu_code']);
$price = floatval($actionResult['sale_price']);
$total = round($count * $price, 2);
$totalCount += $count;
$totalSum += $total;
$result[$actionResult['order_code']]['items'][] = array(
'who' => $who,
'time' => $time,
'reason' => $reason,
'dish' => $dish,
'count' => $count,
'price' => $price,
'total' => $total
);
}
}
foreach ($result as $k => $repResult) {
$itemCount = $itemTotal = 0;
foreach ($repResult['items'] as $item) {
$itemCount += $item['count'];
$itemTotal += $item['total'];
}
$items[] = array(
'order_id' => $k,
'orderTotalCount' => $itemCount,
'orderTotalSum' => $itemTotal,
'items' => $repResult['items']
);
}
$return = array(
'status' => 'success',
'totalCount' => round($totalCount, 3),
'totalSum' => round($totalSum, 2),
'orders' => $items
);
}
if (isset($input['start_date']) && isset($input['end_date'])) {
$end_date = date('Y-m-d H:i:s', strtotime($input['end_date'] . ' 23:59:59'));
$start_date = date('Y-m-d H:i:s', strtotime($input['start_date'] . ' 00:00:01'));
$shifts = ExchangeShifts::select('id')
->where('opened', '>', $start_date)
->where('closed', '<', $end_date)
->get();
$shifts_count = ExchangeShifts::select('id')
->where('opened', '>', $start_date)
->where('closed', '<', $end_date)
->count();
$shift_orders = [];
$totalCount = $totalSum = 0;
if ($shifts_count < 1) {
if (Report::where('start_date', $start_date)->where('end_date', $end_date)->where('report_type', 'deleted')->count() < 1) {
$report = new Report;
$report->user_id = intval($input['admin_user_id']);
$report->name = 'Отчет по удалениям';
$report->shift_id = 0;
$report->start_date = $start_date;
$report->end_date = $end_date;
$report->report_type = 'deleted';
$report->save();
}
return [
'status' => 'success',
'message' => 'Удаления отсутствуют',
'totalCount' => 0
];
}
foreach ($shifts as $shift) {
$shift_id = $shift['id'];
$actionsResult = ExchangeDeleted::select('order_code', 'menu_code', 'count', 'sale_price', 'item_id', 'shift_id')
->with('deletedActions', function ($q) use ($shift_id) {
$q->where('shift_id', $shift_id);
})
->where('shift_id', $shift_id)
->get();
$orders = ExchangeDeleted::select('order_code', 'shift_id')
->where('shift_id', $shift_id)
->get();
$ordersCount = ExchangeDeleted::select('order_code', 'shift_id')
->where('shift_id', $shift_id)
->count();
if ($ordersCount < 1) {
continue;
}
$totalCount = 0;
$totalSum = 0;
$totalCountInShift = 0;
$totalSumInShift = 0;
foreach ($orders as $order) {
$result[$shift_id][$order['order_code']]['items'] = [];
}
$orders = [];
foreach ($actionsResult as $actionResult) {
foreach ($actionResult->deletedActions as $item) {
$count = round(Base::toFloat($item['value']), 3);
$who = Staff::getName(intval($item['who']));
$time = $item['time'];
$reason = Reasons::getName(intval($item['reason']));
$dish = Dishes::getName($actionResult['menu_code']);
$price = floatval($actionResult['sale_price']);
$total = round($count * $price, 2);
$totalCount += $count;
$totalSum += $total;
$totalCountInShift += $count;
$totalSumInShift += $total;
$result[$shift_id][$actionResult['order_code']]['items'][] = array(
'who' => $who,
'time' => $time,
'reason' => $reason,
'dish' => $dish,
'count' => $count,
'price' => $price,
'total' => $total
);
}
}
foreach ($result[$shift_id] as $m => $res) {
$itemCount = $itemSum = 0;
foreach ($res['items'] as $res_item) {
$itemCount += $res_item['count'];
$itemSum += $res_item['total'];
}
$orders[] = array(
'order_id' => $m,
'totalCount' => $itemCount,
'totalSum' => $itemSum,
'items' => $res['items']
);
}
$shift_orders[] = array(
'shift_id' => $shift_id,
'totalCount' => round($totalCountInShift, 3),
'totalSum' => round($totalSumInShift, 2),
'orders' => $orders
);
}
$totalSum = $totalCount = 0;
foreach ($shift_orders as $shift_order) {
$totalSum += $shift_order['totalSum'];
$totalCount += $shift_order['totalCount'];
}
if (Report::where('start_date', $start_date)->where('end_date', $end_date)->where('report_type', 'deleted')->count() < 1) {
$report = new Report;
$report->user_id = intval($input['admin_user_id']);
$report->name = 'Отчет по удалениям';
$report->shift_id = 0;
$report->start_date = $start_date;
$report->end_date = $end_date;
$report->report_type = 'deleted';
$report->save();
}
$return = array(
'status' => 'success',
'totalCount' => round($totalCount, 3),
'totalSum' => round($totalSum, 2),
'orders' => $shift_orders
);
}
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
if ($reportType == 'discounts') {
$code = Filesystem::GetCode($input['token']);
$cache = Cache::get($code, 'shift', $input);
if ($cache) {
return $cache;
}
if (isset($input['shift_id'])) {
$orders_info = [];
$orders = ExchangeOrders::select('shift_id', 'code', 'opened', 'closed', 'full_sum', 'order_sum', 'sale_sum')
->where('shift_id', $input['shift_id'])
->where('sale_sum', '>', 0)
->where('is_closed', 1)
->where('is_deleted', 0)
->where('is_returned', 0)
->get();
if (count($orders) < 1) {
return [
'status' => 'success',
'message' => 'Заказы со скидками отсутствуют',
'shift_id' => $input['shift_id'],
'orders_count' => count($orders),
'orders_info' => $orders_info
];
} else {
$orders_full_sum = $orders_sale_sum = $orders_order_sum = 0;
foreach ($orders as $k => $order) {
$item = ExchangeItems::whereColumn('real_price', '>', 'sale_price')
->where('shift_id', $input['shift_id'])
->where('order_code', $order['code'])
->first();
if ($item['sale_price'] < 0.01 && $item['real_price'] > 0) {
$order_discount = 100;
} else {
$order_discount = round((100 - (100 * $item['sale_price'] / $item['real_price'])));
}
$orders_full_sum += $order['full_sum'];
$orders_sale_sum += $order['sale_sum'];
$orders_order_sum += $order['order_sum'];
$orders_info[$order_discount]['orders'][] = $order;
if (!isset($orders_info[$order_discount]['info'])) {
$orders_info[$order_discount]['info'] = array(
'full_sum' => round($order['full_sum'], 2),
'sale_sum' => round($order['sale_sum'], 2),
'order_sum' => round($order['order_sum'], 2),
'orders_count' => 1
);
} else {
$orders_info[$order_discount]['info']['full_sum'] = round(round($orders_info[$order_discount]['info']['full_sum'], 2) + round($order['full_sum'], 2), 2);
$orders_info[$order_discount]['info']['sale_sum'] = round(round($orders_info[$order_discount]['info']['sale_sum'], 2) + round($order['sale_sum'], 2), 2);
$orders_info[$order_discount]['info']['order_sum'] = round(round($orders_info[$order_discount]['info']['order_sum'], 2) + round($order['order_sum'], 2), 2);
$orders_info[$order_discount]['info']['orders_count']++;
}
}
$return = array(
'status' => 'success',
'message' => 'Заказы со скидками. Смена №' . $input['shift_id'],
'shift_id' => $input['shift_id'],
'orders_count' => count($orders),
'orders_full_sum' => round($orders_full_sum, 2),
'orders_sale_sum' => round($orders_sale_sum, 2),
'orders_order_sum' => round($orders_order_sum, 2),
'orders_info' => $orders_info,
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
}
if (isset($input['start_date']) && isset($input['end_date'])) {
$end_date = date('Y-m-d H:i:s', strtotime($input['end_date']));
$start_date = date('Y-m-d H:i:s', strtotime($input['start_date']));
$orders_info = [];
$orders = ExchangeOrders::select('shift_id', 'code', 'opened', 'closed', 'full_sum', 'order_sum', 'sale_sum')
->where('opened', '>', $start_date)
->where('closed', '<', $end_date)
->where('sale_sum', '>', 0)
->where('is_closed', 1)
->where('is_deleted', 0)
->where('is_returned', 0)
->get();
if (count($orders) < 1) {
return [
'status' => 'success',
'message' => 'Заказы со скидками отсутствуют',
'start_date' => $start_date,
'end_date' => $end_date,
'orders_count' => count($orders),
'orders_info' => $orders_info
];
} else {
$orders_full_sum = $orders_sale_sum = $orders_order_sum = 0;
foreach ($orders as $k => $order) {
$item = ExchangeItems::whereColumn('real_price', '>', 'sale_price')
->where('shift_id', $order['shift_id'])
->where('order_code', $order['code'])
->first();
if ($item['sale_price'] < 0.01 && $item['real_price'] > 0) {
$order_discount = 100;
} else {
$order_discount = round((100 - (100 * $item['sale_price'] / $item['real_price'])));
}
$orders_full_sum += $order['full_sum'];
$orders_sale_sum += $order['sale_sum'];
$orders_order_sum += $order['order_sum'];
$orders_info[$order_discount]['orders'][] = $order;
if (!isset($orders_info[$order_discount]['info'])) {
$orders_info[$order_discount]['info'] = array(
'full_sum' => round($order['full_sum'], 2),
'sale_sum' => round($order['sale_sum'], 2),
'order_sum' => round($order['order_sum'], 2),
'orders_count' => 1
);
} else {
$orders_info[$order_discount]['info']['full_sum'] = round(round($orders_info[$order_discount]['info']['full_sum'], 2) + round($order['full_sum'], 2), 2);
$orders_info[$order_discount]['info']['sale_sum'] = round(round($orders_info[$order_discount]['info']['sale_sum'], 2) + round($order['sale_sum'], 2), 2);
$orders_info[$order_discount]['info']['order_sum'] = round(round($orders_info[$order_discount]['info']['order_sum'], 2) + round($order['order_sum'], 2), 2);
$orders_info[$order_discount]['info']['orders_count']++;
}
}
if (Report::where('start_date', $start_date)->where('end_date', $end_date)->where('report_type', 'discounts')->count() < 1) {
$report = new Report;
$report->user_id = intval($input['admin_user_id']);
$report->name = 'Отчет по скидкам';
$report->shift_id = 0;
$report->start_date = $start_date;
$report->end_date = $end_date;
$report->report_type = 'discounts';
$report->save();
}
$return = array(
'status' => 'success',
'message' => 'Заказы со скидками',
'orders_count' => count($orders),
'orders_full_sum' => round($orders_full_sum, 2),
'orders_sale_sum' => round($orders_sale_sum, 2),
'orders_order_sum' => round($orders_order_sum, 2),
'start_date' => $start_date,
'end_date' => $end_date,
'orders_info' => $orders_info
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
}
}
if ($reportType == 'realisation') {
$code = Filesystem::GetCode($input['token']);
$cache = Cache::get($code, 'shift', $input);
if ($cache) {
return $cache;
}
if (isset($input['shift_id'])) {
$shift_id = $input['shift_id'];
$printers = Printer::get();
foreach ($printers as $printer) {
$printer_total_count[$printer['code']] = $ret_printer_total_count[$printer['code']] = 0;
$printer_total_sum[$printer['code']] = $ret_printer_total_sum[$printer['code']] = 0;
$report[$printer['code']]['items'] = $ret_report[$printer['code']]['items'] = [];
}
$total_sum = $total_count = 0;
$report = $result = $ret_result = $ret_report_items = [];
$items = ExchangeItems::select('dishes_code')
->selectRaw('SUM(count - count_return) as count')
->selectRaw('SUM((count - count_return) * sale_price) as total')
->selectRaw('SUM(count_return) as count_return')
->selectRaw('SUM(count_return * sale_price) as count_return_total')
->where('shift_id', $shift_id)
->where('menu_code', '>', 0)
->where('dishes_code', '>', 0)
->where('real_price', '>=', 0)
->where('sale_price', '>=', 0)
->groupBy('dishes_code')
->get();
foreach ($items as $k => $item) {
$printer_code = Dishes::GetPrinterCode($item['dishes_code']);
$printer_name = Printer::getName($printer_code);
$item_name = Dishes::getName($item['dishes_code']);
if ($item['count_return'] > 0) {
$ret_printer_total_count[$printer_code] += $item['count_return'];
$ret_printer_total_sum[$printer_code] += $item['count_return_total'];
$ret_report_items[$printer_code][] = array(
'printer_code' => $printer_code,
'code' => $item['dishes_code'],
'name' => $item_name,
'count' => round($item['count_return'], 3),
'sum' => round($item['count_return_total'], 2)
);
$ret_report[$printer_code] = array(
'code' => $printer_code,
'name' => $printer_name,
'count' => round($ret_printer_total_count[$printer_code], 3),
'sum' => round($ret_printer_total_sum[$printer_code], 2)
);
$printer_total_count[$printer_code] += $item['count'];
$printer_total_sum[$printer_code] += $item['total'];
if ($item['count'] > 0) {
$report_items[$printer_code][] = array(
'printer_code' => $printer_code,
'code' => $item['dishes_code'],
'name' => $item_name,
'count' => round($item['count'], 3),
'sum' => round($item['total'], 2)
);
}
$report[$printer_code] = array(
'code' => $printer_code,
'name' => $printer_name,
'count' => round($printer_total_count[$printer_code], 3),
'sum' => round($printer_total_sum[$printer_code], 2)
);
} else {
$printer_total_count[$printer_code] += $item['count'];
$printer_total_sum[$printer_code] += $item['total'];
$report_items[$printer_code][] = array(
'printer_code' => $printer_code,
'code' => $item['dishes_code'],
'name' => $item_name,
'count' => round($item['count'], 3),
'sum' => round($item['total'], 2)
);
$report[$printer_code] = array(
'code' => $printer_code,
'name' => $printer_name,
'count' => round($printer_total_count[$printer_code], 3),
'sum' => round($printer_total_sum[$printer_code], 2)
);
}
$total_sum += $item['total'];
$total_count += $item['count'];
}
foreach ($report as $n => $rep_item) {
foreach ($report_items as $k => $report_item) {
if ($n == $k) {
$report[$n]['items'] = $report_items[$k];
}
}
}
foreach ($report as $item) {
array_push($result, $item);
}
foreach ($ret_report as $n => $ret_rep_item) {
foreach ($ret_report_items as $k => $ret_report_item) {
if ($n == $k) {
$ret_report[$n]['items'] = $ret_report_items[$k];
}
}
}
foreach ($ret_report as $ret_item) {
if (count($ret_item['items']) > 0) {
array_push($ret_result, $ret_item);
}
}
$return = array(
'status' => 'success',
'message' => 'Отчет по реализации',
'printers' => $result,
'ret_prints' => $ret_result,
'total_count' => round($total_count, 3),
'total_sum' => round($total_sum, 2)
);
}
if (isset($input['start_date']) && isset($input['end_date'])) {
$end_date = date('Y-m-d H:i:s', strtotime($input['end_date'] . ' 23:59:59'));
$start_date = date('Y-m-d H:i:s', strtotime($input['start_date'] . ' 00:00:01'));
$shifts = ExchangeShifts::select('id')
->where('opened', '>', $start_date)
->where('closed', '<', $end_date)
->get();
$shift_ids = [];
foreach ($shifts as $shift) {
array_push($shift_ids, $shift['id']);
}
$printers = Printer::get();
foreach ($printers as $printer) {
$printer_total_count[$printer['code']] = $ret_printer_total_count[$printer['code']] = 0;
$printer_total_sum[$printer['code']] = $ret_printer_total_sum[$printer['code']] = 0;
$report[$printer['code']]['items'] = $ret_report[$printer['code']]['items'] = [];
}
$total_sum = $total_count = 0;
$report = $result = $ret_result = $ret_report_items = [];
$items = ExchangeItems::select('dishes_code')
->selectRaw('SUM(count - count_return) as count')
->selectRaw('SUM((count - count_return) * sale_price) as total')
->selectRaw('SUM(count_return) as count_return')
->selectRaw('SUM(count_return * sale_price) as count_return_total')
->whereIn('shift_id', $shift_ids)
->where('menu_code', '>', 0)
->where('dishes_code', '>', 0)
->where('real_price', '>=', 0)
->where('sale_price', '>=', 0)
->groupBy('dishes_code')
->get();
foreach ($items as $k => $item) {
$printer_code = Dishes::GetPrinterCode($item['dishes_code']);
$printer_name = Printer::getName($printer_code);
$item_name = Dishes::getName($item['dishes_code']);
if ($item['count_return'] > 0) {
$ret_printer_total_count[$printer_code] += $item['count_return'];
$ret_printer_total_sum[$printer_code] += $item['count_return_total'];
$ret_report_items[$printer_code][] = array(
'printer_code' => $printer_code,
'code' => $item['dishes_code'],
'name' => $item_name,
'count' => round($item['count_return'], 3),
'sum' => round($item['count_return_total'], 2)
);
$ret_report[$printer_code] = array(
'code' => $printer_code,
'name' => $printer_name,
'count' => round($ret_printer_total_count[$printer_code], 3),
'sum' => round($ret_printer_total_sum[$printer_code], 2)
);
$printer_total_count[$printer_code] += $item['count'];
$printer_total_sum[$printer_code] += $item['total'];
if ($item['count'] > 0) {
$report_items[$printer_code][] = array(
'printer_code' => $printer_code,
'code' => $item['dishes_code'],
'name' => $item_name,
'count' => round($item['count'], 3),
'sum' => round($item['total'], 2)
);
}
$report[$printer_code] = array(
'code' => $printer_code,
'name' => $printer_name,
'count' => round($printer_total_count[$printer_code], 3),
'sum' => round($printer_total_sum[$printer_code], 2)
);
} else {
$printer_total_count[$printer_code] += $item['count'];
$printer_total_sum[$printer_code] += $item['total'];
$report_items[$printer_code][] = array(
'printer_code' => $printer_code,
'code' => $item['dishes_code'],
'name' => $item_name,
'count' => round($item['count'], 3),
'sum' => round($item['total'], 2)
);
$report[$printer_code] = array(
'code' => $printer_code,
'name' => $printer_name,
'count' => round($printer_total_count[$printer_code], 3),
'sum' => round($printer_total_sum[$printer_code], 2)
);
}
$total_sum += $item['total'];
$total_count += $item['count'];
}
foreach ($report as $n => $rep_item) {
foreach ($report_items as $k => $report_item) {
if ($n == $k) {
$report[$n]['items'] = $report_items[$k];
}
}
}
foreach ($report as $item) {
array_push($result, $item);
}
foreach ($ret_report as $n => $ret_rep_item) {
foreach ($ret_report_items as $k => $ret_report_item) {
if ($n == $k) {
$ret_report[$n]['items'] = $ret_report_items[$k];
}
}
}
foreach ($ret_report as $ret_item) {
if (count($ret_item['items']) > 0) {
array_push($ret_result, $ret_item);
}
}
if (Report::where('start_date', $start_date)->where('end_date', $end_date)->where('report_type', 'realisation')->count() < 1) {
$report = new Report;
$report->user_id = intval($input['admin_user_id']);
$report->name = 'Отчет по реализации';
$report->shift_id = 0;
$report->start_date = $start_date;
$report->end_date = $end_date;
$report->report_type = 'realisation';
$report->save();
}
$return = array(
'status' => 'success',
'message' => 'Отчет по реализации',
'printers' => $result,
'ret_prints' => $ret_result,
'total_count' => round($total_count, 3),
'total_sum' => round($total_sum, 2)
);
}
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
if ($reportType == 'statistic') {
$code = Filesystem::GetCode($input['token']);
$cache = Cache::get($code, 'shift', $input);
if ($cache) {
return $cache;
}
if (isset($input['shift_id'])) {
$shift_id = $input['shift_id'];
$shift_time = ExchangeShifts::where('id', $shift_id)->first();
$shift_start_time = $shift_time['opened'];
$shift_end_time = $shift_time['closed'];
//Checkout
$checkout_sum = 0;
$checkouts = ExchangeActions::where('shift_id', $shift_id)
->where('action_type', 49)
->get();
foreach ($checkouts as $checkout) {
$checkout_sum += Base::tofloat($checkout['value']);
}
//Cashout
$cashout_sum = 0;
$cashouts = ExchangeActions::where('shift_id', $shift_id)
->where('action_type', 50)
->get();
foreach ($cashouts as $cashout) {
$cashout_sum += Base::tofloat($cashout['value']);
}
//Orders
$orders_count = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->count();
$orders_sum_ws = ExchangeOrders::where('who_open', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('full_sum');
$sales_sum = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('shift_id', $shift_id)
->sum('sale_sum');
$orders_sum_full = ExchangeOrders::where('who_open', '>', 0)
->where('shift_id', $shift_id)
->where('is_deleted', 0)
->sum('order_sum');
$order_waited_count = ExchangeOrders::where('who_open', '>', 0)
->where('who_close', 0)
->where('order_sum', '>=', 0)
->where('shift_id', $shift_id)
->count();
$order_waited_sum = ExchangeOrders::where('who_open', '>', 0)
->where('who_close', 0)
->where('order_sum', '>=', 0)
->where('shift_id', $shift_id)
->sum('order_sum');
//Returns
$returned_items_count = ExchangeItems::where('count_return', '>', 0)
->where('shift_id', $shift_id)
->count();
$returned_orders = ExchangeOrders::select('code')
->where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_returned', 1)
->where('shift_id', $shift_id)
->get();
$returned_items_sum = 0;
foreach ($returned_orders as $returned_order) {
$returned_item = ExchangeItems::selectRaw('SUM(count_return * sale_price) as return_sum')
->where('order_code', $returned_order['code'])
->where('shift_id', $shift_id)
->groupBy('order_code')
->first();
$returned_items_sum += floatval($returned_item['return_sum']);
}
//Cancellations
$cancellations_count = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_deleted', 1)
->where('check_number', '>', 0)
->where('shift_id', $shift_id)
->count();
$cancellations_sum = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_deleted', 1)
->where('check_number', '>', 0)
->where('shift_id', $shift_id)
->sum('order_sum');
//Presales
$presales_income_count = ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->whereIn('action_type', [11, 12, 13, 21])
->count();
$presales_outcome_count = ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->whereIn('action_type', [14, 15, 16])
->count();
$presales_income_cash_sum = ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 11)
->sum('action_value');
$presales_income_credit_sum = ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 12)
->sum('action_value');
$presales_income_clearing_sum = ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 13)
->sum('action_value');
$presales_income_discount_sum = ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 21)
->sum('action_value');
$presales_outcome_cash_sum = ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 14)
->sum('action_value');
$presales_outcome_credit_sum = ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 15)
->sum('action_value');
$presales_outcome_clearing_sum = ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 16)
->sum('action_value');
//Clients
$clients_count = $orders_count;
$clients_sum = ExchangeOrders::where('who_open', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('order_sum');
$one_client_count = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('clients_count');
$average_sum = round($clients_sum / $clients_count, 2);
$average_sum_one_guest = round($clients_sum / $one_client_count, 2);
//Pay methods
$pay_cash_sum = ExchangeOrders::where('who_open', '>', 0)
//->where('order_sum', '>', 0)
//->where('cash', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('cash');
$pay_credit_sum = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('credit', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('credit');
$pay_clearing_sum = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('clearing', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('clearing');
$pay_self_sum = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('self', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('self');
$pay_presale_sum = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('presale', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('presale');
$pay_online_orders = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('check_number', 0)
->where('cash', 0)
->where('credit', 0)
->where('clearing', 0)
->where('presale', 0)
->where('self', 0)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('shift_id', $shift_id)
->get();
$pay_online_sum = 0;
foreach ($pay_online_orders as $pay_online_order) {
$count_online_open = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $pay_online_order['code'])
->where('action_type', 45)
->where('more', '<>', '0')
->count();
$count_online_close = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $pay_online_order['code'])
->where('action_type', 6)
->count();
if ($count_online_open > 0 && $count_online_close > 0) {
$pay_online_sum += $pay_online_order['order_sum'];
}
}
$pay_another_sum = 0;
$pays_another = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
//->where('check_number', '>', 0)
->where('cash', 0)
->where('credit', 0)
->where('clearing', 0)
->where('presale', 0)
->where('self', 0)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('shift_id', $shift_id)
->get();
foreach ($pays_another as $pay_another) {
$exch_another = ExchangeActions::where('order_code', $pay_another['code'])
->where('action_type', 51)
->where('shift_id', $shift_id)
->count();
if ($exch_another > 0) {
$pay_another_sum += $pay_another['order_sum'];
}
}
$pay_echeck_sum = $pay_delivery_sum = 0;
$settings = Settings::where('code', 10)
->where('value', '1')
->count();
if ($settings > 0) {
$pay_echeck_orders = ExchangeOrders::where('who_open', '>', 0)
->where('is_closed', 1)
->where('check_number', 0)
->where('cash', 0)
->where('credit', 0)
->where('clearing', 0)
->where('presale', 0)
->where('self', 0)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('table_name', '<>', '0')
->where('shift_id', $shift_id)
->get();
foreach ($pay_echeck_orders as $pay_echeck_order) {
$count_echeck_open = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $pay_echeck_order['code'])
->where('action_type', 6)
->count();
if ($count_echeck_open == 0 && $pay_echeck_order['is_closed'] == 1) {
$pay_echeck_sum += $pay_echeck_order['order_sum'];
}
}
$pay_delivery_sum = ExchangeOrders::where('who_open', '>', 0)
->where('is_closed', 1)
->where('check_number', 0)
->where('cash', 0)
->where('credit', 0)
->where('clearing', 0)
->where('presale', 0)
->where('self', 0)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('table_name', '0')
->where('shift_id', $shift_id)
->sum('order_sum');
}
$return = array(
'status' => 'success',
'fiscal' => [
'income_sum' => $checkout_sum,
'outcome_sum' => $cashout_sum
],
'realisation' => [
'orders_count' => $orders_count,
'orders_sum_with_sales' => $orders_sum_full,
'orders_sum_without_sales' => $orders_sum_ws,
'sale_sum' => $sales_sum,
'waited_orders_count' => $order_waited_count,
'waited_orders_sum' => $order_waited_sum,
],
'returns' => [
'returned_orders_count' => $returned_items_count,
'returned_items_sum' => $returned_items_sum,
],
'cancellations' => [
'cancellations_count' => $cancellations_count,
'cancellations_sum' => Base::tofloat($cancellations_sum),
],
'statistic' => [
'clients_count' => $clients_count,
'clients_average_sum' => $average_sum,
'clients_average_sum_one' => $average_sum_one_guest,
],
'presales' => [
'presales_income_count' => $presales_income_count,
'presales_outcome_count' => $presales_outcome_count,
'presales_income_cash_sum' => $presales_income_cash_sum,
'presales_income_credit_sum' => $presales_income_credit_sum,
'presales_income_clearing_sum' => $presales_income_clearing_sum,
'presales_income_discount_sum' => $presales_income_discount_sum,
'presales_outcome_cash_sum' => $presales_outcome_cash_sum,
'presales_outcome_credit_sum' => $presales_outcome_credit_sum,
'presales_outcome_clearing_sum' => $presales_outcome_clearing_sum
],
'payments' => [
'pay_cash' => $pay_cash_sum,
'pay_credit' => $pay_credit_sum,
'pay_clearing' => $pay_clearing_sum,
'pay_self' => $pay_self_sum,
'pay_presale' => $pay_presale_sum,
'pay_online' => $pay_online_sum,
'pay_another' => $pay_another_sum,
'pay_echeck' => $pay_echeck_sum,
'pay_delivery' => $pay_delivery_sum
]
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
if (isset($input['start_date']) && isset($input['end_date'])) {
$end_date = date('Y-m-d H:i:s', strtotime($input['end_date'] . ' 23:59:59'));
$start_date = date('Y-m-d H:i:s', strtotime($input['start_date'] . ' 00:00:01'));
$shifts = ExchangeShifts::select('id')
->where('opened', '>', $start_date)
->where('closed', '<', $end_date)
->get();
$checkout_sum = $clients_sum = $one_client_count = 0;
$orders_count = $order_waited_count = $order_waited_sum = 0;
$sales_sum = $orders_sum_ws = $orders_sum_full = 0;
$cashout_sum = 0;
$returned_items_sum = $returned_items_count = 0;
$cancellations_count = $cancellations_sum = 0;
$presales_income_count = $presales_outcome_count = 0;
$presales_income_cash_sum = $presales_income_credit_sum = $presales_income_clearing_sum = $presales_income_discount_sum = 0;
$presales_outcome_cash_sum = $presales_outcome_credit_sum = $presales_outcome_clearing_sum = 0;
$pay_cash_sum = $pay_clearing_sum = 0;
$pay_credit_sum = $pay_self_sum = 0;
$pay_presale_sum = $pay_another_sum = 0;
$pay_echeck_sum = 0;
$pay_delivery_sum = 0;
$pay_online_sum = 0;
foreach ($shifts as $shift) {
$shift_id = $shift['id'];
$shift_time = ExchangeShifts::where('id', $shift_id)->first();
$shift_start_time = $shift_time['opened'];
$shift_end_time = $shift_time['closed'];
//Checkout
$checkouts = ExchangeActions::where('shift_id', $shift_id)
->where('action_type', 49)
->get();
foreach ($checkouts as $checkout) {
$checkout_sum += Base::tofloat($checkout['value']);
}
//Cashout
$cashouts = ExchangeActions::where('shift_id', $shift_id)
->where('action_type', 50)
->get();
foreach ($cashouts as $cashout) {
$cashout_sum += Base::tofloat($cashout['value']);
}
//Orders
$orders_count += ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->count();
$orders_sum_ws += ExchangeOrders::where('who_open', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('full_sum');
$sales_sum += ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('shift_id', $shift_id)
->sum('sale_sum');
$orders_sum_full += ExchangeOrders::where('who_open', '>', 0)
->where('shift_id', $shift_id)
->where('is_deleted', 0)
->sum('order_sum');
$order_waited_count += ExchangeOrders::where('who_open', '>', 0)
->where('who_close', 0)
->where('order_sum', '>=', 0)
->where('shift_id', $shift_id)
->count();
$order_waited_sum += ExchangeOrders::where('who_open', '>', 0)
->where('who_close', 0)
->where('order_sum', '>=', 0)
->where('shift_id', $shift_id)
->sum('order_sum');
//Returns
$returned_items_count += ExchangeItems::where('count_return', '>', 0)
->where('shift_id', $shift_id)
->count();
$returned_orders = ExchangeOrders::select('code')
->where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_returned', 1)
->where('shift_id', $shift_id)
->get();
foreach ($returned_orders as $returned_order) {
$returned_item = ExchangeItems::selectRaw('SUM(count_return * sale_price) as return_sum')
->where('order_code', $returned_order['code'])
->where('shift_id', $shift_id)
->groupBy('order_code')
->first();
$returned_items_sum += floatval($returned_item['return_sum']);
}
//Cancellations
$cancellations_count += ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_deleted', 1)
->where('check_number', '>', 0)
->where('shift_id', $shift_id)
->count();
$cancellations_sum += ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_deleted', 1)
->where('check_number', '>', 0)
->where('shift_id', $shift_id)
->sum('order_sum');
//Presales
$presales_income_count += ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->whereIn('action_type', [11, 12, 13, 21])
->count();
$presales_outcome_count += ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->whereIn('action_type', [14, 15, 16])
->count();
$presales_income_cash_sum += ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 11)
->sum('action_value');
$presales_income_credit_sum += ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 12)
->sum('action_value');
$presales_income_clearing_sum += ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 13)
->sum('action_value');
$presales_income_discount_sum += ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 21)
->sum('action_value');
$presales_outcome_cash_sum += ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 14)
->sum('action_value');
$presales_outcome_credit_sum += ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 15)
->sum('action_value');
$presales_outcome_clearing_sum += ClientsActions::where('created', '>', $shift_start_time)
->where('created', '<', $shift_end_time)
->where('action_type', 16)
->sum('action_value');
//Clients
$clients_count = $orders_count;
$clients_sum += ExchangeOrders::where('who_open', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('order_sum');
$one_client_count += ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>=', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('clients_count');
$average_sum = round($clients_sum / $clients_count, 2);
$average_sum_one_guest = round($clients_sum / $one_client_count, 2);
//Pay methods
$pay_cash_sum += ExchangeOrders::where('who_open', '>', 0)
//->where('order_sum', '>', 0)
//->where('cash', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('cash');
$pay_credit_sum += ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('credit', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('credit');
$pay_clearing_sum += ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('clearing', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('clearing');
$pay_self_sum += ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('self', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('self');
$pay_presale_sum += ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('presale', '>', 0)
->where('is_deleted', 0)
->where('shift_id', $shift_id)
->sum('presale');
$pay_online_orders = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
->where('check_number', 0)
->where('cash', 0)
->where('credit', 0)
->where('clearing', 0)
->where('presale', 0)
->where('self', 0)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('shift_id', $shift_id)
->get();
foreach ($pay_online_orders as $pay_online_order) {
$count_online_open = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $pay_online_order['code'])
->where('action_type', 45)
->where('more', '<>', '0')
->count();
$count_online_close = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $pay_online_order['code'])
->where('action_type', 6)
->count();
if ($count_online_open > 0 && $count_online_close > 0) {
$pay_online_sum += $pay_online_order['order_sum'];
}
}
$pays_another = ExchangeOrders::where('who_open', '>', 0)
->where('order_sum', '>', 0)
//->where('check_number', '>', 0)
->where('cash', 0)
->where('credit', 0)
->where('clearing', 0)
->where('presale', 0)
->where('self', 0)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('shift_id', $shift_id)
->get();
foreach ($pays_another as $pay_another) {
$exch_another = ExchangeActions::where('order_code', $pay_another['code'])
->where('action_type', 51)
->where('shift_id', $shift_id)
->count();
if ($exch_another > 0) {
$pay_another_sum += $pay_another['order_sum'];
}
}
$settings = Settings::where('code', 10)
->where('value', '1')
->count();
if ($settings > 0) {
$pay_echeck_orders = ExchangeOrders::where('who_open', '>', 0)
->where('is_closed', 1)
->where('check_number', 0)
->where('cash', 0)
->where('credit', 0)
->where('clearing', 0)
->where('presale', 0)
->where('self', 0)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('table_name', '<>', '0')
->where('shift_id', $shift_id)
->get();
foreach ($pay_echeck_orders as $pay_echeck_order) {
$count_echeck_open = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $pay_echeck_order['code'])
->where('action_type', 6)
->count();
if ($count_echeck_open == 0 && $pay_echeck_order['is_closed'] == 1) {
$pay_echeck_sum += $pay_echeck_order['order_sum'];
}
}
$pay_delivery_sum += ExchangeOrders::where('who_open', '>', 0)
->where('is_closed', 1)
->where('check_number', 0)
->where('cash', 0)
->where('credit', 0)
->where('clearing', 0)
->where('presale', 0)
->where('self', 0)
->where('is_deleted', 0)
->where('is_returned', 0)
->where('table_name', '0')
->where('shift_id', $shift_id)
->sum('order_sum');
}
}
if (Report::where('start_date', $start_date)->where('end_date', $end_date)->where('report_type', 'statistics')->count() < 1) {
$report = new Report;
$report->user_id = intval($input['admin_user_id']);
$report->name = 'Отчет по статистике';
$report->shift_id = 0;
$report->start_date = $start_date;
$report->end_date = $end_date;
$report->report_type = 'statistics';
$report->save();
}
$return = array(
'status' => 'success',
'fiscal' => [
'income_sum' => $checkout_sum,
'outcome_sum' => $cashout_sum
],
'realisation' => [
'orders_count' => $orders_count,
'orders_sum_with_sales' => $orders_sum_full,
'orders_sum_without_sales' => $orders_sum_ws,
'sale_sum' => $sales_sum,
'waited_orders_count' => $order_waited_count,
'waited_orders_sum' => $order_waited_sum,
],
'returns' => [
'returned_orders_count' => $returned_items_count,
'returned_items_sum' => $returned_items_sum,
],
'cancellations' => [
'cancellations_count' => $cancellations_count,
'cancellations_sum' => Base::tofloat($cancellations_sum),
],
'statistic' => [
'clients_count' => $clients_count,
'clients_average_sum' => $average_sum,
'clients_average_sum_one' => $average_sum_one_guest,
],
'presales' => [
'presales_income_count' => $presales_income_count,
'presales_outcome_count' => $presales_outcome_count,
'presales_income_cash_sum' => $presales_income_cash_sum,
'presales_income_credit_sum' => $presales_income_credit_sum,
'presales_income_clearing_sum' => $presales_income_clearing_sum,
'presales_income_discount_sum' => $presales_income_discount_sum,
'presales_outcome_cash_sum' => $presales_outcome_cash_sum,
'presales_outcome_credit_sum' => $presales_outcome_credit_sum,
'presales_outcome_clearing_sum' => $presales_outcome_clearing_sum
],
'payments' => [
'pay_cash' => $pay_cash_sum,
'pay_credit' => $pay_credit_sum,
'pay_clearing' => $pay_clearing_sum,
'pay_self' => $pay_self_sum,
'pay_presale' => $pay_presale_sum,
'pay_online' => $pay_online_sum,
'pay_another' => $pay_another_sum,
'pay_echeck' => $pay_echeck_sum,
'pay_delivery' => $pay_delivery_sum
]
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
}
if ($reportType == 'payment') {
$code = Filesystem::GetCode($input['token']);
$cache = Cache::get($code, 'shift', $input);
if ($cache) {
return $cache;
}
if (isset($input['shift_id'])) {
$shift_id = $input['shift_id'];
$shift_time = ExchangeShifts::where('id', $shift_id)->first();
$shift_start_time = $shift_time['opened'];
$shift_end_time = $shift_time['closed'];
$printers = Printer::get();
foreach ($printers as $printer) {
$report[$printer['code']]['cash'] = $report[$printer['code']]['credit'] = 0;
$report[$printer['code']]['presale'] = $report[$printer['code']]['self'] = 0;
$report[$printer['code']]['clearing'] = $report[$printer['code']]['emoney'] = 0;
$report[$printer['code']]['online'] = 0;
}
$report[0]['cash'] = $report[0]['credit'] = 0;
$report[0]['presale'] = $report[0]['self'] = 0;
$report[0]['clearing'] = $report[0]['emoney'] = 0;
$report[0]['online'] = 0;
$orders = ExchangeOrders::where('shift_id', $shift_id)
->where('is_deleted', 0)
->where('is_closed', 1)
->where('order_sum', '>=', 0)
->get();
foreach ($orders as $order) {
$order_emoney = 0;
$order_online = 0;
$sum_payments = $order['cash'] + $order['credit'] + $order['presale'] + $order['self'] + $order['clearing'] + $order_emoney + $order_online;
if ($sum_payments == 0) {
$count_online_open = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $order['code'])
->where('action_type', 45)
->where('more', '<>', '0')
->count();
$count_online_close = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $order['code'])
->where('action_type', 6)
->count();
if ($count_online_open > 0 && $count_online_close > 0) {
$order_online = $order['order_sum'];
}
$count_another = ExchangeActions::where('order_code', $order['code'])
->where('action_type', 51)
->where('shift_id', $shift_id)
->count();
if ($count_another > 0) {
$order_emoney = $order['order_sum'];
}
}
$sum_payments = $order['cash'] + $order['credit'] + $order['presale'] + $order['self'] + $order['clearing'] + $order_emoney + $order_online;
if ($sum_payments == 0) {
continue;
}
$cash_cof = $order['cash'] / $sum_payments;
$credit_cof = $order['credit'] / $sum_payments;
$presale_cof = $order['presale'] / $sum_payments;
$self_cof = $order['self'] / $sum_payments;
$clearing_cof = $order['clearing'] / $sum_payments;
$online_cof = $order_online / $sum_payments;
$emoney_cof = $order_emoney / $sum_payments;
$items = ExchangeItems::where('shift_id', $shift_id)
->where('order_code', $order['code'])
->get();
foreach ($items as $item) {
$sum = round(($item['count'] - $item['count_return']) * $item['sale_price'], 2);
$printer_code = Dishes::GetPrinterCode($item['dishes_code']);
$report[$printer_code]['cash'] += round($sum * $cash_cof, 2);
$report[$printer_code]['credit'] += round($sum * $credit_cof, 2);
$report[$printer_code]['presale'] += round($sum * $presale_cof, 2);
$report[$printer_code]['self'] += round($sum * $self_cof, 2);
$report[$printer_code]['clearing'] += round($sum * $clearing_cof, 2);
$report[$printer_code]['emoney'] += round($sum * $emoney_cof, 2);
$report[$printer_code]['online'] += round($sum * $online_cof, 2);
}
}
foreach ($report as $key => $item) {
$item['cash'] = round($item['cash'], 2);
$item['credit'] = round($item['credit'], 2);
$item['presale'] = round($item['presale'], 2);
$item['self'] = round($item['self'], 2);
$item['clearing'] = round($item['clearing'], 2);
$item['emoney'] = round($item['emoney'], 2);
$item['online'] = round($item['online'], 2);
$out[] = array(
'printer_name' => Printer::getName($key),
'full_sum' => round($item['cash'] + $item['credit'] + $item['presale'] + $item['self'] + $item['clearing'] + $item['emoney'] + $item['online'], 2),
'payments' => $item
);
}
$return = array(
'status' => 'success',
'items' => $out
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
if (isset($input['start_date']) && isset($input['end_date'])) {
$end_date = date('Y-m-d H:i:s', strtotime($input['end_date'] . ' 23:59:59'));
$start_date = date('Y-m-d H:i:s', strtotime($input['start_date'] . ' 00:00:01'));
$printers = Printer::get();
foreach ($printers as $printer) {
$report[$printer['code']]['cash'] = $report[$printer['code']]['credit'] = 0;
$report[$printer['code']]['presale'] = $report[$printer['code']]['self'] = 0;
$report[$printer['code']]['clearing'] = $report[$printer['code']]['emoney'] = 0;
$report[$printer['code']]['online'] = 0;
}
$report[0]['cash'] = $report[0]['credit'] = 0;
$report[0]['presale'] = $report[0]['self'] = 0;
$report[0]['clearing'] = $report[0]['emoney'] = 0;
$report[0]['online'] = 0;
$shifts = ExchangeShifts::select('id')
->where('opened', '>', $start_date)
->where('closed', '<', $end_date)
->get();
foreach ($shifts as $shift) {
$shift_id = $shift['id'];
$orders = ExchangeOrders::where('shift_id', $shift_id)
->where('is_deleted', 0)
->where('is_closed', 1)
->where('order_sum', '>=', 0)
->get();
foreach ($orders as $order) {
$order_emoney = 0;
$order_online = 0;
$sum_payments = $order['cash'] + $order['credit'] + $order['presale'] + $order['self'] + $order['clearing'] + $order_emoney + $order_online;
if ($sum_payments == 0) {
$count_online_open = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $order['code'])
->where('action_type', 45)
->where('more', '<>', '0')
->count();
$count_online_close = ExchangeActions::where('shift_id', $shift_id)
->where('order_code', $order['code'])
->where('action_type', 6)
->count();
if ($count_online_open > 0 && $count_online_close > 0) {
$order_online = $order['order_sum'];
}
$count_another = ExchangeActions::where('order_code', $order['code'])
->where('action_type', 51)
->where('shift_id', $shift_id)
->count();
if ($count_another > 0) {
$order_emoney = $order['order_sum'];
}
}
$sum_payments = $order['cash'] + $order['credit'] + $order['presale'] + $order['self'] + $order['clearing'] + $order_emoney + $order_online;
if ($sum_payments == 0) {
continue;
}
$cash_cof = $order['cash'] / $sum_payments;
$credit_cof = $order['credit'] / $sum_payments;
$presale_cof = $order['presale'] / $sum_payments;
$self_cof = $order['self'] / $sum_payments;
$clearing_cof = $order['clearing'] / $sum_payments;
$online_cof = $order_online / $sum_payments;
$emoney_cof = $order_emoney / $sum_payments;
$items = ExchangeItems::where('shift_id', $shift_id)
->where('order_code', $order['code'])
->get();
foreach ($items as $item) {
$sum = round(($item['count'] - $item['count_return']) * $item['sale_price'], 2);
$printer_code = Dishes::GetPrinterCode($item['dishes_code']);
$report[$printer_code]['cash'] += round($sum * $cash_cof, 2);
$report[$printer_code]['credit'] += round($sum * $credit_cof, 2);
$report[$printer_code]['presale'] += round($sum * $presale_cof, 2);
$report[$printer_code]['self'] += round($sum * $self_cof, 2);
$report[$printer_code]['clearing'] += round($sum * $clearing_cof, 2);
$report[$printer_code]['emoney'] += round($sum * $emoney_cof, 2);
$report[$printer_code]['online'] += round($sum * $online_cof, 2);
}
}
}
foreach ($report as $key => $item) {
$item['cash'] = round($item['cash'], 2);
$item['credit'] = round($item['credit'], 2);
$item['presale'] = round($item['presale'], 2);
$item['self'] = round($item['self'], 2);
$item['clearing'] = round($item['clearing'], 2);
$item['emoney'] = round($item['emoney'], 2);
$item['online'] = round($item['online'], 2);
$out[] = array(
'printer_name' => Printer::getName($key),
'full_sum' => round($item['cash'] + $item['credit'] + $item['presale'] + $item['self'] + $item['clearing'] + $item['emoney'] + $item['online'], 2),
'payments' => $item
);
}
$return = array(
'status' => 'success',
'items' => $out
);
if (Report::where('start_date', $start_date)->where('end_date', $end_date)->where('report_type', 'payment')->count() < 1) {
$report = new Report;
$report->user_id = intval($input['admin_user_id']);
$report->name = 'Отчет по отделам и видам оплат';
$report->shift_id = 0;
$report->start_date = $start_date;
$report->end_date = $end_date;
$report->report_type = 'payment';
$report->save();
}
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
}
if ($reportType == 'report_construct') {
$code = Filesystem::GetCode($input['token']);
$cache = Cache::get($code, 'shift', $input);
if ($cache) {
return $cache;
}
if (isset($input['shift_id'])) {
$shift_id = $input['shift_id'];
$shift_data = ExchangeShifts::where('id', $shift_id)->first();
$close_actions = ExchangeActions::select('order_code')
->where('shift_id', $shift_id)
->groupBy('order_code')
->get();
$close_actions_count = ExchangeActions::select('order_code')
->where('shift_id', $shift_id)
->groupBy('order_code')
->count();
if ($close_actions_count < 1) {
return [
'status' => 'success',
'error_message' => 'Заказы отсутствуют',
];
}
$order_ids = [];
foreach ($close_actions as $close_action) {
$order_status = ExchangeOrders::getOrderStatus($close_action['order_code'], $shift_id);
if ($order_status != 'Удален' && $order_status != 'Возвращен') {
$order_ids[] = $close_action['order_code'];
}
}
$items = ExchangeItems::selectRaw('code, menu_code, dishes_code, order_code, units_id, (count - count_return) as count, cof, real_price, sale_price, special_price, shift_id')
->where('shift_id', $shift_id)
->whereIn('order_code', $order_ids)
->get();
$collection = new Collection();
foreach ($items as $item) {
$order_cash_cof = $order_credit_cof = $order_presale_cof = $order_clearing_cof = 0;
$order = ExchangeOrders::where('code', $item['order_code'])
->where('shift_id', $shift_id)
->first();
$returned_order_order_sum = $returned_order_cash = $returned_order_credit = $returned_order_presale = $returned_order_clearing = 0;
if ($order['is_returned'] > 0) {
$returned_order_id = ExchangeOrders::getReturnedOrderId($item['order_code'], $shift_id);
$returned_order = ExchangeOrders::where('code', $returned_order_id)
->where('shift_id', $shift_id)
->first();
$returned_order_order_sum = abs($returned_order['order_sum']);
$returned_order_cash = abs($returned_order['cash']);
$returned_order_credit = abs($returned_order['credit']);
$returned_order_presale = abs($returned_order['presale']);
$returned_order_clearing = abs($returned_order['clearing']);
}
$order_order_sum = $order['order_sum'] - $returned_order_order_sum;
$order_cash = $order['cash'] - $returned_order_cash;
$order_credit = $order['credit'] - $returned_order_credit;
$order_presale = $order['presale'] - $returned_order_presale;
$order_clearing = $order['clearing'] - $returned_order_clearing;
if ($order['client_code'] != '') {
$order_client_name = Client::getName($order['client_code']);
} else {
$order_client_name = '';
}
if ($order_order_sum > 0) {
$order_cash_cof = $order_cash / $order_order_sum;
$order_credit_cof = $order_credit / $order_order_sum;
$order_presale_cof = $order_presale / $order_order_sum;
$order_clearing_cof = $order_clearing / $order_order_sum;
}
$create_actions = ExchangeActions::where('action_type', 2)
->where('shift_id', $shift_id)
->where('order_position', $item['code'])
->get();
$create_count = 0;
foreach ($create_actions as $create_action) {
$create_count += Base::toFloat($create_action['value']);
if ($create_count <= $item['count'] || count($create_actions) == 1) {
$realPrice = $item['real_price'] * $item['count'] * $item['cof'];
$salePrice = $item['sale_price'] * $item['count'];
if ($realPrice > 0) {
$discount = $salePrice / $realPrice;
$discount = (1 - $discount) * 100;
} else {
$discount = 0;
}
$item_menu_code = $item['menu_code'];
$item_dishes_code = $item['dishes_code'];
$item_order_code = $item['order_code'];
$item_units_id = $item['units_id'];
if (count($create_actions) == 1) {
$item_count = $item['count'];
} else {
$item_count = Base::toFloat($create_action['value']);
}
$item_cof = $item['cof'];
$item_real_price = $item['real_price'];
$item_sale_price = $item['sale_price'];
$item_special_price = $item['special_price'];
$item_cash = $item_count * $item_sale_price * $order_cash_cof;
$item_credit = $item_count * $item_sale_price * $order_credit_cof;
$item_presale = $item_count * $item_sale_price * $order_presale_cof;
$item_clearing = $item_count * $item_sale_price * $order_clearing_cof;
$item_discount_sum = $item_real_price - $item_sale_price;
$item_staff = Staff::getName($create_action['who']);
$item_time = $create_action['time'];
$item_printer = Printer::getName(Dishes::GetPrinterCode($item_menu_code));
$item_work_place = Terminal::getNameByWorkGroup($create_action['work_group'], $create_action['work_code']);
$items_array = array(
'shift_id' => $shift_id,
'work_place' => $item_work_place,
'place_name' => Base::data_decode($order['place_name']),
'table_name' => $order['table_name'],
'printer_name' => $item_printer,
'client_name' => $order_client_name,
'staff_name' => $item_staff,
'order_code' => $item_order_code,
'item_time' => $item_time,
'item_menu_code' => $item_menu_code,
'item_dishes_code' => $item_dishes_code,
'item_name' => Dishes::getName($item_menu_code),
'item_units_id' => $item_units_id,
'item_units_name' => Units::getName($item_units_id),
'item_cof' => floatval($item_cof),
'item_count' => $item_count,
'item_real_price' => floatval($item_count * $item_real_price),
'item_sale_price' => floatval($item_count * $item_sale_price),
'item_special_price' => floatval($item_count * $item_special_price),
'item_cash' => $item_cash,
'item_credit' => $item_credit,
'item_presale' => $item_presale,
'item_clearing' => $item_clearing,
'item_discount_sum' => round($item_discount_sum, 2),
'item_discount_value' => round($discount, 2),
'order_opened' => $order['opened'],
'order_closed' => $order['closed'],
'order_check_number' => $order['check_number'],
'shift_opened' => $shift_data['opened'],
'shift_closed' => $shift_data['closed'],
);
$collection->push((object)$items_array);
} else {
break;
}
}
}
$return = array(
'status' => 'success',
'data' => $collection
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
if (isset($input['start_date']) && isset($input['end_date'])) {
$end_date = date('Y-m-d H:i:s', strtotime($input['end_date'] . ' 23:59:59'));
$start_date = date('Y-m-d H:i:s', strtotime($input['start_date'] . ' 00:00:01'));
$shifts = ExchangeShifts::select('id', 'opened', 'closed')
->where('opened', '>', $start_date)
->where('closed', '<', $end_date)
->get();
$collection = new Collection();
foreach ($shifts as $shift) {
$shift_id = $shift['id'];
$close_actions = ExchangeActions::select('order_code')
->where('shift_id', $shift_id)
->groupBy('order_code')
->get();
$close_actions_count = ExchangeActions::select('order_code')
->where('shift_id', $shift_id)
->groupBy('order_code')
->count();
if ($close_actions_count < 1) {
continue;
}
$order_ids = [];
foreach ($close_actions as $close_action) {
$order_status = ExchangeOrders::getOrderStatus($close_action['order_code'], $shift_id);
if ($order_status != 'Удален' && $order_status != 'Возвращен') {
$order_ids[] = $close_action['order_code'];
}
}
$items = ExchangeItems::selectRaw('code, menu_code, dishes_code, order_code, units_id, (count - count_return) as count, cof, real_price, sale_price, special_price, shift_id')
->where('shift_id', $shift_id)
->whereIn('order_code', $order_ids)
->get();
foreach ($items as $item) {
$order_cash_cof = $order_credit_cof = $order_presale_cof = $order_clearing_cof = 0;
$order = ExchangeOrders::where('code', $item['order_code'])
->where('shift_id', $shift_id)
->first();
$returned_order_order_sum = $returned_order_cash = $returned_order_credit = $returned_order_presale = $returned_order_clearing = 0;
if ($order['is_returned'] > 0) {
$returned_order_id = ExchangeOrders::getReturnedOrderId($item['order_code'], $shift_id);
$returned_order = ExchangeOrders::where('code', $returned_order_id)
->where('shift_id', $shift_id)
->first();
$returned_order_order_sum = abs($returned_order['order_sum']);
$returned_order_cash = abs($returned_order['cash']);
$returned_order_credit = abs($returned_order['credit']);
$returned_order_presale = abs($returned_order['presale']);
$returned_order_clearing = abs($returned_order['clearing']);
}
$order_order_sum = $order['order_sum'] - $returned_order_order_sum;
$order_cash = $order['cash'] - $returned_order_cash;
$order_credit = $order['credit'] - $returned_order_credit;
$order_presale = $order['presale'] - $returned_order_presale;
$order_clearing = $order['clearing'] - $returned_order_clearing;
if ($order['client_code'] != '') {
$order_client_name = Client::getName($order['client_code']);
} else {
$order_client_name = '';
}
if ($order_order_sum > 0) {
$order_cash_cof = $order_cash / $order_order_sum;
$order_credit_cof = $order_credit / $order_order_sum;
$order_presale_cof = $order_presale / $order_order_sum;
$order_clearing_cof = $order_clearing / $order_order_sum;
}
$create_actions = ExchangeActions::where('action_type', 2)
->where('shift_id', $shift_id)
->where('order_position', $item['code'])
->get();
$create_count = 0;
foreach ($create_actions as $create_action) {
$create_count += Base::toFloat($create_action['value']);
if ($create_count <= $item['count']) {
$realPrice = $item['real_price'] * $item['count'] * $item['cof'];
$salePrice = $item['sale_price'] * $item['count'];
if ($realPrice > 0) {
$discount = $salePrice / $realPrice;
$discount = (1 - $discount) * 100;
} else {
$discount = 0;
}
$item_menu_code = $item['menu_code'];
$item_dishes_code = $item['dishes_code'];
$item_order_code = $item['order_code'];
$item_units_id = $item['units_id'];
$item_count = Base::toFloat($create_action['value']);
$item_cof = $item['cof'];
$item_real_price = $item['real_price'];
$item_sale_price = $item['sale_price'];
$item_special_price = $item['special_price'];
$item_cash = $item_count * $item_sale_price * $order_cash_cof;
$item_credit = $item_count * $item_sale_price * $order_credit_cof;
$item_presale = $item_count * $item_sale_price * $order_presale_cof;
$item_clearing = $item_count * $item_sale_price * $order_clearing_cof;
$item_discount_sum = $item_real_price - $item_sale_price;
$item_staff = Staff::getName($create_action['who']);
$item_time = $create_action['time'];
$item_printer = Printer::getName(Dishes::GetPrinterCode($item_menu_code));
$item_work_place = Terminal::getNameByWorkGroup(intval($create_action['work_group']), intval($create_action['work_code']));
$items_array = array(
'shift_id' => $shift_id,
'work_place' => $item_work_place,
'place_name' => Base::data_decode($order['place_name']),
'table_name' => $order['table_name'],
'printer_name' => $item_printer,
'client_name' => $order_client_name,
'staff_name' => $item_staff,
'order_code' => $item_order_code,
'item_time' => $item_time,
'item_menu_code' => $item_menu_code,
'item_dishes_code' => $item_dishes_code,
'item_name' => Dishes::getName($item_menu_code),
'item_units_id' => $item_units_id,
'item_units_name' => Units::getName($item_units_id),
'item_cof' => floatval($item_cof),
'item_count' => $item_count,
'item_real_price' => floatval($item_real_price),
'item_sale_price' => floatval($item_sale_price),
'item_special_price' => floatval($item_special_price),
'item_cash' => $item_cash,
'item_credit' => $item_credit,
'item_presale' => $item_presale,
'item_clearing' => $item_clearing,
'item_discount_sum' => round($item_discount_sum, 2),
'item_discount_value' => round($discount, 2),
'order_opened' => $order['opened'],
'order_closed' => $order['closed'],
'order_check_number' => $order['check_number'],
'shift_opened' => $shift['opened'],
'shift_closed' => $shift['closed'],
);
$collection->push((object)$items_array);
} else {
break;
}
}
}
}
if (Report::where('start_date', $start_date)->where('end_date', $end_date)->where('report_type', 'reportRealisationConstruct')->count() < 1) {
$report = new Report;
$report->user_id = intval($input['admin_user_id']);
$report->name = 'Расширенный отчет по реализации';
$report->shift_id = 0;
$report->start_date = $start_date;
$report->end_date = $end_date;
$report->report_type = 'reportRealisationConstruct';
$report->save();
}
$return = array(
'status' => 'success',
'data' => $collection
);
if (!$cache) {
$cache = Cache::store(strval($code), 'shift', $input, $return);
}
return $cache;
}
}
if ($reportType == 'presales') {
$code = Filesystem::GetCode($input['token']);
if (isset($input['shift_id'])) {
$plugin = 'shift';
}
if (isset($input['start_date']) && isset($input['end_date'])) {
$plugin = 'report';
}
$cache = Cache::get($code, $plugin, $input);
if ($cache) {
return $cache;
}
if (isset($input['shift_id'])) {
$collection = new Collection();
}
if (isset($input['start_date']) && isset($input['end_date'])) {
$end_date = date('Y-m-d H:i:s', strtotime($input['end_date'] . ' 23:59:59'));
$start_date = date('Y-m-d H:i:s', strtotime($input['start_date'] . ' 00:00:01'));
$shifts = ExchangeShifts::select('id', 'opened', 'closed')
->where('opened', '>', $start_date)
->where('closed', '<', $end_date)
->get();
$collection = new Collection();
foreach ($shifts as $shift) {
}
}
$return = array(
'status' => 'success',
'data' => $collection
);
if (!$cache) {
$cache = Cache::store(strval($code), $plugin, $input, $return);
}
return $cache;
}
} else {
return [
'status' => 'success',
'error_message' => 'Проверьте введенные данные',
];
}
}
}