Files
admin-php-module/commands/CreateClient.php
2021-05-31 16:11:03 +03:00

73 lines
2.2 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 CreateClient extends HRCCommand implements HRCCommandInterface {
protected $signature = 'postcreateclient';
public function command($input, $output = null) {
$group = ClientsGroup::where('id', '=', $input['group_id'])->first();
$total = Client::where('name', '=', $input['name'])->count();
if ($input['is_special_price'] == 'true') {
$specialPrice = 1;
} else {
$specialPrice = 0;
}
$client = new Client;
if ($total >= 1) {
$total = $total + 1;
$client->name = '#' . $total . ' ' . urldecode($input['name']);
} else {
$client->name = urldecode($input['name']);
}
$client->user_code = strtoupper(md5(time()));
$client->group_id = $group['code'];
$client->is_special_price = $specialPrice;
$clientEmail = new ClientsEmail;
$clientEmail->email = urldecode($input['email']);
$clientEmail->client_guid = $client->user_code;
$clientAddress = new ClientsAddress;
$clientAddress->address = urldecode($input['address']);
$clientAddress->client_guid = $client->user_code;
$clientPhone = new ClientsPhone;
if ($input['phone'] !== '') {
$phoneData = urldecode($input['phone']);
$phone = '+375 (' . substr($phoneData, 0, 2) . ') ' . substr($phoneData, 2, 3) . '-' . substr($phoneData, 5, 2) . '-' . substr($phoneData, 7, 2);
} else {
$phone = '';
}
$clientPhone->phone = $phone;
$clientPhone->client_guid = $client->user_code;
$clientBarcode = new ClientsBarcode;
$clientBarcode->code_id = urldecode($input['barcode']);
$clientBarcode->name = '';
$clientBarcode->client_guid = $client->user_code;
$clientBarcode->value = urldecode($input['barcode']);
$clientBarcode->block = 0;
$clientBarcode->symptom_block = 0;
$clientBarcode->save();
$clientEmail->save();
$clientAddress->save();
$client->save();
$clientPhone->save();
return [
'status' => 'success',
'message' => 'Клиент создан',
];
}
}