= 5.3.3 case JSON_ERROR_UTF8: $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.'; break; // PHP >= 5.5.0 case JSON_ERROR_RECURSION: $error = 'One or more recursive references in the value to be encoded.'; break; // PHP >= 5.5.0 case JSON_ERROR_INF_OR_NAN: $error = 'One or more NAN or INF values in the value to be encoded.'; break; case JSON_ERROR_UNSUPPORTED_TYPE: $error = 'A value of a type that cannot be encoded was given.'; break; default: $error = 'Unknown JSON error occured.'; break; } if ($error !== '') { // throw the Exception or exit // or whatever :) return false; } // everything is OK return true; } function data_decode($data) { $alph = [ "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы", "Ь", "Э", "Ю", "Я", "а", "б", "в", "г", "д", "е", "ё", "ж", "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы", "ь", "э", "ю", "я", ]; foreach ($alph as $key => $letter) { $haystack = mb_convert_encoding($data, "CP1251", "UTF-8"); $needle = $letter; $pos = strripos($haystack, $needle); if ($pos === false) { $after_conv = false; } else { $after_conv = true; break; } } if (!$after_conv) { foreach ($alph as $key => $letter) { $haystack = $data; $needle = $letter; $pos = strripos($haystack, $needle); if ($pos === false) { $before_conv = false; } else { $before_conv = true; break; } } } if ($after_conv) { $retval = mb_convert_encoding($data, "CP1251", "UTF-8"); } elseif ($before_conv) { $retval = $data; } else { $retval = $data; } return $retval; } $reportType = $input['type']; //merge, slice, move if (isset($reportType)) { //merge if ($reportType == 'merge') { $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 (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 = 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 = 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 [ 'status' => 'success', 'shift_id' => $input['shift_id'], 'count' => count($merged_order_items), 'data' => $merged_order_items, ]; } //slice if ($reportType == 'slice') { $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 (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 = 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 = 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 (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 [ 'status' => 'success', 'shift_id' => $input['shift_id'], 'count' => $action_count, 'data' => $sliced_order_data, ]; } if ($reportType == 'move') { $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' => data_decode($data_from['value']), 'move_from_staff' => $staff_name_from, 'move_from_time' => $time_from, 'move_to' => data_decode($item['value']), 'move_to_staff' => $staff_name_to, 'move_to_time' => $time_to ); } } } return [ 'status' => 'success', 'count' => count($moved_order_data), 'data' => $moved_order_data, ]; } if ($reportType == 'staff') { 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; } $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 ); } return [ 'status' => 'success', 'staffs' => $staff_data, ]; } 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'); } $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 ); } return [ 'status' => 'success', 'staffs' => $staff_data, ]; } } if ($reportType == 'deleted') { $total_count = $total_sum = 0; $deleted = []; if (isset($input['shift_id'])) { $shift_id = $input['shift_id']; $deleted_from_orders = ExchangeActions::select('order_code') ->where('action_type', 5) ->where('shift_id', $shift_id) ->groupBy('order_code') ->get(); foreach ($deleted_from_orders as $deleted_from_order) { $items = []; $order_id = $deleted_from_order['order_code']; $deleted_items = ExchangeDeleted::where('shift_id', $shift_id) ->where('order_code', $order_id) ->get(); foreach ($deleted_items as $deleted_item) { $item_info = ExchangeActions::where('shift_id', $shift_id) ->where('order_code', $deleted_item['order_code']) ->where('action_type', 5) ->where('order_position', $deleted_item['item_id']) ->first(); $time = $item_info['time']; $who = Staff::getName($item_info['who']); $reason = Reasons::getName($item_info['reason']); $approved = Staff::getName($item_info['more']); $item_count = $deleted_item['count']; $item_price = $deleted_item['sale_price']; $item_name = Dishes::getName($deleted_item['menu_code']); $total_count += $item_count; $total_sum += $item_count * $item_price; $items[] = array( 'dish_name' => $item_name, 'who' => $who, 'approved' => $approved, 'reason' => $reason, 'time' => $time, 'count' => $item_count, 'price' => $item_price, 'sum' => floatval($item_price * $item_count) ); } $deleted[] = array( 'order_code' => $order_id, 'shift_id' => $shift_id, 'items' => $items ); } } 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'])); $deletedShifts = ExchangeActions::select('shift_id') ->where('action_type', 5) ->where('time', '>', $start_date) ->where('time', '<', $end_date) ->groupBy('shift_id') ->get(); foreach ($deletedShifts as $deletedShift) { $shift_id = intval($deletedShift['shift_id']); $deleted_from_orders = ExchangeActions::select('order_code') ->where('action_type', 5) ->where('shift_id', $shift_id) ->groupBy('order_code') ->get(); foreach ($deleted_from_orders as $deleted_from_order) { $items = []; $order_id = $deleted_from_order['order_code']; $deleted_items = ExchangeDeleted::where('shift_id', $shift_id) ->where('order_code', $order_id) ->get(); foreach ($deleted_items as $deleted_item) { $item_info = ExchangeActions::where('shift_id', $shift_id) ->where('order_code', $deleted_item['order_code']) ->where('action_type', 5) ->where('order_position', $deleted_item['item_id']) ->first(); $time = $item_info['time']; $who = Staff::getName($item_info['who']); $reason = Reasons::getName($item_info['reason']); $approved = Staff::getName($item_info['more']); $item_count = $deleted_item['count']; $item_price = $deleted_item['sale_price']; $item_name = Dishes::getName($deleted_item['menu_code']); $total_count += $item_count; $total_sum += $item_count * $item_price; $items[] = array( 'dish_name' => $item_name, 'who' => $who, 'approved' => $approved, 'reason' => $reason, 'time' => $time, 'count' => $item_count, 'price' => $item_price, 'sum' => floatval($item_price * $item_count) ); } $deleted[] = array( 'order_code' => $order_id, 'shift_id' => $shift_id, 'items' => $items ); } } $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', 'total_count' => strval($total_count), 'total_sum' => $total_sum, 'deleted' => $deleted ]; } if ($input['type'] == 'abc') { 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_shifts = ExchangeOrders::where('opened', '>=', $start_date) ->where('closed', '=<', $end_date) ->where('is_deleted', 0) ->where('is_returned', 0) ->where('is_closed', 1) ->groupBy('shift_id') ->get(); $orders = ExchangeOrders::where('opened', '>=', $start_date) ->where('closed', '=<', $end_date) ->where('is_deleted', 0) ->where('is_returned', 0) ->where('is_closed', 1) ->get(); $revenue = 0; foreach ($orders_shifts as $orders_shift) { $shift_revenue = ExchangeShifts::where('shift_id', $orders_shift['shift_id']) ->first(); $revenue += $shift_revenue['revenue']; } foreach ($orders as $order) { $order_items = ExchangeItems::where('order_code', $order['code']) ->where('shift_id', $order['shift_id']) ->get(); } } } } else { return [ 'status' => 'success', 'error_message' => 'Проверьте введенные данные', ]; } } }