Files
admin-php-module/commands/POSTRestoreShift.php
miroman-afk 5c3a8a464b v.2.28
2023-05-04 10:41:36 +03:00

275 lines
13 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\ExchangeDeleted;
use App\Component\Models\ExchangeItems;
use App\Component\Models\ExchangeOrders;
use App\Component\Models\ExchangeShifts;
use App\Component\Models\ShiftOnlineActions;
use App\Component\Models\ShiftOnlineDeleted;
use App\Component\Models\ShiftOnlineItems;
use App\Component\Models\ShiftOnlineOrders;
use App\Component\Models\Shifts;
use App\Component\Models\Terminal;
use App\Component\Models\TerminalUpdate;
use App\Console\Commands\HRCCommand;
use App\Console\Commands\HRCCommandInterface;
use Illuminate\Support\Facades\Log;
class POSTRestoreShift extends HRCCommand implements HRCCommandInterface
{
protected $signature = 'postrestoreshift';
public function command($input, $output = null)
{
function dirDel($dir)
{
$d = opendir($dir);
while (($entry = readdir($d)) !== false) {
if ($entry != "." && $entry != "..") {
if (is_dir($dir . "/" . $entry)) {
dirDel($dir . "/" . $entry);
} else {
unlink($dir . "/" . $entry);
}
}
}
closedir($d);
rmdir($dir);
}
$shift_id = $input['shift_id'];
$cache_dir = __DIR__ . "\\..\\..\\..\\Cache\\";
$nowTime = time();
$update = true;
function decode_text($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;
}
$terminal = Terminal::where('is_active', 1)
->where('soft', '1')
->first();
$exchange_shifts = ExchangeShifts::where('id', '=', $shift_id)
->first();
$exchange_orders = ExchangeOrders::where('shift_id', '=', $shift_id)
->get();
$exchange_items = ExchangeItems::where('shift_id', '=', $shift_id)
->get();
$exchange_actions = ExchangeActions::where('shift_id', '=', $shift_id)
->get();
$exchange_deleted = ExchangeDeleted::where('shift_id', '=', $shift_id)
->get();
$terminal_online = TerminalUpdate::where('method', 'online')
->where('terminal_id', $terminal['id'])
->first();
$baseTime = date_create($terminal_online['next_at']);
$baseTime = date_format($baseTime, 'U');
$old_date = date_create($terminal_online['next_at']);
date_modify($old_date, '+30 minutes');
$new_date = date_format($old_date, 'Y-m-d H:i:s');
$newTime = $nowTime + ($terminal_online['period'] * 60);
if ($newTime >= $baseTime) {
$new_terminal_online = TerminalUpdate::find($terminal_online['id']);
$new_terminal_online->next_at = $new_date;
$new_terminal_online->save();
}
if ($update) {
$online_shift = Shifts::first();
if ($online_shift['z_number'] != $shift_id) {
//clear shifts
Shifts::truncate();
$shift = new Shifts;
$shift->opened = $exchange_shifts['opened'];
$shift->closed = $exchange_shifts['closed'];
$shift->who_open = $exchange_shifts['who_open'];
$shift->who_close = $exchange_shifts['who_close'];
$shift->z_number = $exchange_shifts['id'];
$shift->save();
//clear shift_online_orders
ShiftOnlineOrders::truncate();
foreach ($exchange_orders as $key => $exchange_order) {
$shift_online_order = new ShiftOnlineOrders;
$shift_online_order->cash = $exchange_order["cash"];
$shift_online_order->check_number = $exchange_order["check_number"];
$shift_online_order->clearing = $exchange_order["clearing"];
$shift_online_order->client_code = $exchange_order["client_code"];
$shift_online_order->client_count = $exchange_order["clients_count"];
if ($exchange_order["closed"] == '0000-00-00 00:00:00') {
$exchange_order["closed"] = '1970-01-01 00:00:00';
}
$shift_online_order->closed = $exchange_order["closed"];
$shift_online_order->code = $exchange_order["code"];
$shift_online_order->credit = $exchange_order["credit"];
$shift_online_order->full_sum = $exchange_order["full_sum"];
$shift_online_order->guid = $exchange_order["guid"];
$shift_online_order->is_block = $exchange_order["is_block"];
$shift_online_order->is_closed = $exchange_order["is_closed"];
$shift_online_order->is_deleted = $exchange_order["is_deleted"];
$shift_online_order->is_edit = $exchange_order["is_edit"];
$shift_online_order->is_printed = $exchange_order["is_printed"];
$shift_online_order->is_returned = $exchange_order["is_returned"];
$shift_online_order->is_self = $exchange_order["is_self"];
$shift_online_order->is_waited = $exchange_order["is_waited"];
if ($exchange_order["manual_discount"] == '') {
$exchange_order["manual_discount"] = 0;
} else {
$exchange_order["manual_discount"] = $exchange_order["manual_discount"] + 0;
}
$shift_online_order->manual_discount = $exchange_order["manual_discount"];
$shift_online_order->opened = $exchange_order["opened"];
$shift_online_order->order_sum = $exchange_order["order_sum"];
$shift_online_order->params_code = $exchange_order["params_id"];
$shift_online_order->place_name = mb_convert_encoding(decode_text($exchange_order["place_name"]), 'UTF-8', 'UTF-8');
$shift_online_order->presale = $exchange_order["presale"];
$shift_online_order->sale_sum = $exchange_order["sale_sum"];
$shift_online_order->self = $exchange_order["self"];
$shift_online_order->table_name = $exchange_order["table_name"];
$shift_online_order->table_place = $exchange_order["table_place"];
$shift_online_order->terminal_id = $exchange_order["terminal_id"];
$shift_online_order->version_type = $exchange_order["version_type"];
$shift_online_order->who_close = $exchange_order["who_close"];
$shift_online_order->who_open = $exchange_order["who_open"];
$shift_online_order->save();
}
ShiftOnlineItems::truncate();
foreach ($exchange_items as $key => $exchange_item) {
$shift_online_item = new ShiftOnlineItems;
$shift_online_item->code = $exchange_item["code"];
$shift_online_item->cof = $exchange_item["cof"];
$shift_online_item->count = $exchange_item["count"];
$shift_online_item->count_real = $exchange_item["count_real"];
$shift_online_item->count_return = $exchange_item["count_return"];
$shift_online_item->dish_code = $exchange_item["dishes_code"];
$shift_online_item->menu_code = $exchange_item["menu_code"];
if ($exchange_item["menu_code"] == 0 && $exchange_item["menu_code"] == 0 && $exchange_item["units_id"] == 0) {
$shift_online_item->modificator_code = 1;
} else {
$shift_online_item->modificator_code = 0;
}
$shift_online_item->order_code = $exchange_item["order_code"];
$shift_online_item->parent_id = $exchange_item["parent_id"];
$shift_online_item->real_price = $exchange_item["real_price"];
$shift_online_item->sale_price = $exchange_item["sale_price"];
$shift_online_item->special_price = $exchange_item["special_price"];
$shift_online_item->terminal_id = $exchange_item["terminal_id"];
$shift_online_item->units_code = $exchange_item["units_id"];
$shift_online_item->save();
}
ShiftOnlineActions::truncate();
foreach ($exchange_actions as $key => $exchange_action) {
$shift_online_action = new ShiftOnlineActions;
$shift_online_action->type_action = $exchange_action["action_type"];
$shift_online_action->more = $exchange_action["more"];
$shift_online_action->order_code = $exchange_action["order_code"];
$shift_online_action->order_position = $exchange_action["order_position"];
$shift_online_action->reason = $exchange_action["reason"];
$shift_online_action->terminal_id = $exchange_action["terminal_id"];
$shift_online_action->time = $exchange_action["time"];
$shift_online_action->value = $exchange_action["value"];
$shift_online_action->who = $exchange_action["who"];
$shift_online_action->workcode = $exchange_action["work_code"];
$shift_online_action->workgroup = $exchange_action["work_group"];
$shift_online_action->save();
}
ShiftOnlineDeleted::truncate();
foreach ($exchange_deleted as $key => $exchange_deleted_item) {
$shift_online_deleted_item = new ShiftOnlineDeleted;
$shift_online_deleted_item->code = $exchange_deleted_item["code"];
$shift_online_deleted_item->count = $exchange_deleted_item["count"];
$shift_online_deleted_item->dishes_code = $exchange_deleted_item["dishes_code"];
$shift_online_deleted_item->item_id = $exchange_deleted_item["item_id"];
$shift_online_deleted_item->menu_code = $exchange_deleted_item["menu_code"];
$shift_online_deleted_item->order_code = $exchange_deleted_item["order_code"];
$shift_online_deleted_item->real_price = $exchange_deleted_item["real_price"];
$shift_online_deleted_item->sale_price = $exchange_deleted_item["sale_price"];
$shift_online_deleted_item->terminal_id = $exchange_deleted_item["terminal_id"];
$shift_online_deleted_item->units_id = $exchange_deleted_item["units_id"];
$shift_online_deleted_item->save();
}
$exc_time = time();
$diff_time = $exc_time - $nowTime;
$terminal_online = TerminalUpdate::where('method', 'online')->where('terminal_id', $exchange_shifts['terminal_id'])->first();
dirDel($cache_dir);
return [
'status' => 'success',
'message' => 'Данные автоматически синхронизируются с POS-системой ' . $terminal_online['next_at'],
];
} else {
return [
'status' => 'error',
'more' => 'Смена уже восстановлена',
'stop_at' => 'Stop at check z_number',
];
}
} else {
return [
'status' => 'error',
'more' => 'Смена уже восстановлена',
'stop_at' => 'Stop at check time',
];
}
}
}