Files
admin-php-module/commands/POSTClient.php
miroman-afk fb46c8e739 v.2.27
Fixed reports
2023-05-02 15:21:54 +03:00

306 lines
14 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\Client;
use App\Component\Models\ClientsAddress;
use App\Component\Models\ClientsBarcode;
use App\Component\Models\ClientsEmail;
use App\Component\Models\ClientsGroup;
use App\Component\Models\ClientsPhone;
use App\Console\Commands\HRCCommand;
use App\Console\Commands\HRCCommandInterface;
use Illuminate\Support\Facades\Log;
class POSTClient extends HRCCommand implements HRCCommandInterface
{
protected $signature = 'postclient';
public function command($input, $output = null)
{
if (isset($input['task'])) {
if ($input['task'] == 'update') {
log::debug($input);
if (isset($input['id'])) {
$id = $input['id'];
} else if (isset($input['client_guid'])) {
$fClient = Client::where('user_code', $input['client_guid'])->first();
if (!isset($fClient)) {
return [
'status' => 'success',
'error_message' => 'Клиент не найден',
];
}
$id = $fClient['id'];
} else {
return [
'status' => 'success',
'error_message' => 'Проверьте введенные данные',
];
}
if (isset($input['group_id']) && !isset($input['group_guid'])) {
$group_id = $input['group_id'];
} else if (isset($input['group_guid']) && !isset($input['group_id'])) {
$fGroup = ClientsGroup::where('code', $input['group_guid'])->first();
if (!isset($fGroup)) {
return [
'status' => 'success',
'error_message' => 'Группа не найдена',
];
}
$group_id = $fGroup['id'];
} else if (isset($input['group_id']) && isset($input['group_guid'])) {
$fGroup = ClientsGroup::where('code', $input['group_guid'])->first();
if (!isset($fGroup)) {
$fGroup = ClientsGroup::where('id', isset($input['group_id']))->first();
if (!isset($fGroup)) {
return [
'status' => 'success',
'error_message' => 'Группа не найдена',
];
}
}
$group_id = $fGroup['id'];
} else {
return [
'status' => 'success',
'error_message' => 'Проверьте введенные данные',
];
}
$client = Client::find($id);
$client->name = urldecode($input['name']);
$client->unloaded = 0;
$clientGroup = ClientsGroup::where('id', $group_id)->first();
$client->group_id = $clientGroup['code'];
if (isset($input['phone'])) {
$phone = $input['phone'];
Log::debug($phone);
$clientPhone = ClientsPhone::where('client_guid', $client->user_code)->first();
Log::debug($clientPhone);
if (isset($clientPhone)) {
$clientPhone = ClientsPhone::find($clientPhone['id']);
if ($phone !== '' && $phone !== '+375 ( ) - -') {
if (substr($phone, 0, 1) == '+') {
$clientPhone->phone = '+375 (' . substr($phone, 4, 2) . ') ' . substr($phone, 6, 3) . '-' . substr($phone, 9, 2) . '-' . substr($phone, 11, 2);
log::debug('UPD Substr true: ' . $phone);
} else {
log::debug('UPD Substr false: ' . $phone);
$clientPhone->phone = '+375 (' . substr($phone, 0, 2) . ') ' . substr($phone, 2, 3) . '-' . substr($phone, 5, 2) . '-' . substr($phone, 7, 2);
}
} else {
$clientPhone->phone = '+375 ( ) - -';
}
} else {
$clientPhone = new ClientsPhone;
if ($phone !== '' && $phone !== '+375 ( ) - -') {
if (substr($phone, 0, 1) == '+') {
$clientPhone->phone = '+375 (' . substr($phone, 4, 2) . ') ' . substr($phone, 6, 3) . '-' . substr($phone, 9, 2) . '-' . substr($phone, 11, 2);
log::debug('CR Substr true: ' . $phone);
} else {
log::debug('CR Substr false: ' . $phone);
$clientPhone->phone = '+375 (' . substr($phone, 0, 2) . ') ' . substr($phone, 2, 3) . '-' . substr($phone, 5, 2) . '-' . substr($phone, 7, 2);
}
} else {
$clientPhone->phone = '+375 ( ) - -';
}
}
$clientPhone->client_guid = $client->user_code;
$clientPhone->save();
}
if (isset($input['email'])) {
$clientEmail = ClientsEmail::where('client_guid', $client->user_code)->first();
if ($clientEmail) {
$clientEmail = ClientsEmail::find($clientEmail['id']);
if (isset($input['email']) && $input['email'] !== '') {
$clientEmail->email = urldecode($input['email']);
} else {
$clientEmail->email = '';
}
} else {
$clientEmail = new ClientsEmail;
$clientEmail->email = urldecode($input['email']);
}
$clientEmail->client_guid = $client->user_code;
$clientEmail->save();
} else {
$clientEmail = '';
}
if (isset($input['address'])) {
$clientAddress = ClientsAddress::where('client_guid', $client->user_code)->first();
if ($clientAddress) {
$clientAddress = ClientsAddress::find($clientAddress['id']);
if ($input['address'] !== '') {
$clientAddress->address = urldecode($input['address']);
} else {
$clientAddress->address = '';
}
} else {
$clientAddress = new ClientsAddress;
$clientAddress->address = urldecode($input['address']);
}
$clientAddress->client_guid = $client->user_code;
$clientAddress->save();
}
$clientBarcodeStart = 0;
$clientBarcodeEnd = 0;
if (isset($input['barcode'])) {
if ($input['barcode'] !== '') {
$client->barcode_type = 2;
$client->barcode_start = $clientBarcodeStart = urldecode($input['barcode']);
$client->barcode_end = $clientBarcodeEnd = urldecode($input['barcode']);
} else {
$client->barcode_type = 1;
$client->barcode_start = $clientBarcodeStart;
$client->barcode_end = $clientBarcodeEnd;
}
}
if (isset($input['is_special_price']) && $input['is_special_price'] == 'true') {
$specialPrice = 1;
} else {
$specialPrice = 0;
}
if (isset($input['is_employee']) && $input['is_employee'] == 'true') {
$employee = 1;
} else {
$employee = 0;
}
$client->is_special_price = $specialPrice;
$client->is_employee = $employee;
$client->save();
return [
'status' => 'success',
'client' => $client,
'phone' => $clientPhone,
'email' => $clientEmail,
'address' => $clientAddress,
'barcode' => $clientBarcodeStart,
'message' => 'Клиент обновлен',
];
}
if ($input['task'] == 'delete') {
$client = Client::find($input['id']);
$clientGroup = ClientsGroup::where('code', $client->group_id)->first();
$clientPhone = ClientsPhone::where('client_guid', $client->user_code)->first();
$clientPhone = ClientsPhone::find($clientPhone['id']);
$clientEmail = ClientsEmail::where('client_guid', $client->user_code)->first();
$clientEmail = ClientsEmail::find($clientEmail['id']);
$clientAddress = ClientsAddress::where('client_guid', $client->user_code)->first();
$clientAddress = ClientsAddress::find($clientAddress['id']);
$clientBarcode = ClientsBarcode::where('client_guid', $client->user_code)->first();
$clientBarcode = ClientsBarcode::find($clientBarcode['id']);
if ($clientPhone) {
$clientPhone->delete();
}
if ($clientEmail) {
$clientEmail->delete();
}
if ($clientAddress) {
$clientAddress->delete();
}
if ($clientBarcode) {
$clientBarcode->delete();
}
if ($client) {
$client->delete();
}
return [
'status' => 'success',
'message' => 'Клиент удален',
'currentGroup' => $clientGroup['id'],
];
}
if ($input['task'] == 'lock') {
if (isset($input['id'])) {
$id = $input['id'];
} else if (isset($input['client_guid'])) {
$fClient = Client::where('user_code', $input['client_guid'])->first();
if (!isset($fClient)) {
return [
'status' => 'success',
'error_message' => 'Клиент не найден',
];
}
$id = $fClient['id'];
} else {
return [
'status' => 'success',
'error_message' => 'Проверьте введенные данные',
];
}
$client = Client::find($id);
$clientGroup = ClientsGroup::where('code', $client->group_id)->first();
$client->is_block = 1;
$client->save();
return [
'status' => 'success',
'message' => 'Клиент заблокирован',
'currentGroup' => $clientGroup['id'],
];
}
if ($input['task'] == 'unlock') {
if (isset($input['id'])) {
$id = $input['id'];
} else if (isset($input['client_guid'])) {
$fClient = Client::where('user_code', $input['client_guid'])->first();
if (!isset($fClient)) {
return [
'status' => 'success',
'error_message' => 'Клиент не найден',
];
}
$id = $fClient['id'];
} else {
return [
'status' => 'success',
'error_message' => 'Проверьте введенные данные',
];
}
$client = Client::find($id);
$clientGroup = ClientsGroup::where('code', $client->group_id)->first();
$client->is_block = 0;
$client->save();
return [
'status' => 'success',
'message' => 'Клиент разблокирован',
'currentGroup' => $clientGroup['id'],
];
}
if ($input['task'] == 'search' && isset($input['name'])) {
$client = Client::where('name', 'like', '%' . urldecode($input['name']) . '%')->get();
if (count($client) > 0) {
$message = 'Клиенты найдены!';
} else {
$message = 'Клиенты с именем ' . urldecode($input['name']) . ' не найдены!';
}
return [
'status' => 'success',
'message' => $message,
'clients' => $client
];
}
} else {
return [
'status' => 'success',
'error_message' => 'Проверьте введенные данные',
];
}
}
}