Files
admin-php-module/commands/POSTClient.php
miroman-afk f964aca7a9 v2.5
Update response in "delete client"
2022-01-13 01:43:50 +03:00

131 lines
4.1 KiB
PHP

<?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;
class POSTClient extends HRCCommand implements HRCCommandInterface {
protected $signature = 'postclient';
public function command($input, $output = null) {
if (isset($input['task']) && isset($input['id'])) {
if ($input['task'] == 'update') {
$client = Client::find($input['id']);
$client->name = urldecode($input['name']);
$client->unloaded = 0;
$clientGroup = ClientsGroup::find($input['group_id']);
$client->group_id = urldecode($clientGroup['code']);
$clientPhone = ClientsPhone::where('client_guid', $client->user_code)->first();
if ($clientPhone) {
$clientPhone = ClientsPhone::find($clientPhone['id']);
if ($input['phone'] !== '') {
if (substr($input['phone'], 0, 1) == '+') {
$phoneData = urldecode($input['phone']);
} else {
$phoneData = urldecode($input['phone']);
$clientPhone->phone = '+375 (' . substr($phoneData, 0, 2) . ') ' . substr($phoneData, 2, 3) . '-' . substr($phoneData, 5, 2) . '-' . substr($phoneData, 7, 2);
}
} else {
$clientPhone->phone = '+375 ( ) - -';
}
$clientPhone->save();
}
$clientEmail = ClientsEmail::where('client_guid', $client->user_code)->first();
if ($clientEmail) {
$clientEmail = ClientsEmail::find($clientEmail['id']);
if ($input['email'] !== '') {
$clientEmail->email = urldecode($input['email']);
} else {
$clientEmail->email = '';
}
$clientEmail->save();
}
$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 = '';
}
$clientAddress->save();
}
$clientBarcode = ClientsBarcode::where('client_guid', $client->user_code)->first();
if ($clientBarcode) {
$clientBarcode = ClientsBarcode::find($clientBarcode['id']);
if ($input['barcode'] !== '') {
$clientBarcode->code_id = urldecode($input['barcode']);
} else {
$clientBarcode->code_id = '';
}
$clientBarcode->save();
}
$client->save();
$clientGroup->save();
return [
'status' => 'success',
'client' => $client,
'phone' => $clientPhone,
'email' => $clientEmail,
'address' => $clientAddress,
'barcode' => $clientBarcode,
'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'],
];
}
} else {
return [
'status' => 'success',
'error_message' => 'Проверьте введенные данные',
];
}
}
}