Files
admin-php-module/commands/GETDataReport.php
miroman-afk ebd1180d89 v.2.15
Добавлен журнал заказов
Добавлены отчеты: по переносам, по объединениям, по разбиениям
2022-06-13 13:16:50 +03:00

218 lines
9.4 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\ExchangeActions;
use App\Component\Models\Staff;
use App\Console\Commands\HRCCommand;
use App\Console\Commands\HRCCommandInterface;
class GETDataReport extends HRCCommand implements HRCCommandInterface {
protected $signature = 'getdatareport';
public function command($input, $output = null) {
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;
}
function staffName($data) {
$staff_name = Staff::where('code', $data)->where('is_history', 0)->first();
if ($staff_name) {
$staff_name = $staff_name['name'];
} else {
$staff_name = Staff::where('code', $data)->where('is_history', 1)->first();
if ($staff_name) {
$staff_name = $staff_name['name'];
} else {
$staff_name = 'Связанный персонал не найден';
}
}
return $staff_name;
}
$reportType = $input['type'];
$shiftNumber = $input['shift_id'];
//merge, slice, move
if (isset($reportType) && isset($shiftNumber)) {
//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 = $item['value'];
$item_actions = ExchangeActions::where('order_code', $item['value'] + 0)
->where('shift_id', $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 = [];
$merge_order_items = json_decode(utf8_encode($item_action['more']), 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 = staffName($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 + 0)->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 + 0, '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'];
$moved_items = json_decode(utf8_encode($action['more']), 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();
$start_data = json_decode(utf8_encode($to_action['more']), true, JSON_INVALID_UTF8_SUBSTITUTE);
$staff_name = staffName($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) {
if ($item['action_type'] == 39) {
$staff_name_to = staffName($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 = staffName($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,
];
}
} else {
return [
'status' => 'success',
'error_message' => 'Проверьте введенные данные',
];
}
}
}