diff --git a/commands/POSTClient.php b/commands/POSTClient.php index c2e149d..363f88b 100644 --- a/commands/POSTClient.php +++ b/commands/POSTClient.php @@ -22,36 +22,61 @@ class POSTClient extends HRCCommand implements HRCCommandInterface { $client->name = urldecode($input['name']); $client->unloaded = 0; - $clientGroup = ClientsGroup::where('id', $input['group_id'])->first(); - $client->group_id = urldecode($clientGroup->code); + $clientGroup = ClientsGroup::find($input['group_id']); + $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']); + 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 { - $phoneData = urldecode($input['phone']); - $clientPhone->phone = '+375 (' . substr($phoneData, 0, 2) . ') ' . substr($phoneData, 2, 3) . '-' . substr($phoneData, 5, 2) . '-' . substr($phoneData, 7, 2); + $clientPhone->phone = '+375 ( ) - -'; } - } else { - $clientPhone->phone = ''; + $clientPhone->save(); } $clientEmail = ClientsEmail::where('client_guid', $client->user_code)->first(); - $clientEmail->email = urldecode($input['email']); + 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(); - $clientAddress->address = urldecode($input['address']); + 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(); - $clientBarcode->code_id = urldecode($input['barcode']); + 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(); - $clientPhone->save(); - $clientEmail->save(); - $clientAddress->save(); - $clientBarcode->save(); + return [ 'status' => 'success', 'client' => $client, @@ -64,16 +89,31 @@ class POSTClient extends HRCCommand implements HRCCommandInterface { } if ($input['task'] == 'delete') { - $client = Client::where('id', '=', $input['id'])->first(); + $client = Client::find($input['id']); $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(); - $clientPhone->delete(); - $clientEmail->delete(); - $clientAddress->delete(); - $clientBarcode->delete(); - $client->delete(); + $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' => 'Клиент удален',