Files
admin-php-module/commands/POSTClient.php
2021-06-04 13:46:11 +03:00

89 lines
2.9 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::where('id', $input['group_id'])->first();
$client->group_id = urldecode($clientGroup->code);
$clientPhone = ClientsPhone::where('client_guid', $client->user_code)->first();
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 = '';
}
$clientEmail = ClientsEmail::where('client_guid', $client->user_code)->first();
$clientEmail->email = urldecode($input['email']);
$clientAddress = ClientsAddress::where('client_guid', $client->user_code)->first();
$clientAddress->address = urldecode($input['address']);
$clientBarcode = ClientsBarcode::where('client_guid', $client->user_code)->first();
$clientBarcode->code_id = urldecode($input['barcode']);
$client->save();
$clientGroup->save();
$clientPhone->save();
$clientEmail->save();
$clientAddress->save();
$clientBarcode->save();
return [
'status' => 'success',
'client' => $client,
'phone' => $clientPhone,
'email' => $clientEmail,
'address' => $clientAddress,
'barcode' => $clientBarcode,
'message' => 'Клиент обновлен',
];
}
if ($input['task'] == 'delete') {
$client = Client::where('id', '=', $input['id'])->first();
$clientPhone = ClientsPhone::where('client_guid', $client->user_code)->first();
$clientEmail = ClientsEmail::where('client_guid', $client->user_code)->first();
$clientAddress = ClientsAddress::where('client_guid', $client->user_code)->first();
$clientBarcode = ClientsBarcode::where('client_guid', $client->user_code)->first();
$clientPhone->delete();
$clientEmail->delete();
$clientAddress->delete();
$clientBarcode->delete();
$client->delete();
return [
'status' => 'success',
'message' => 'Клиент удален',
];
}
} else {
return [
'status' => 'success',
'error_message' => 'Проверьте введенные данные',
];
}
}
}