2017-01-30 21:40:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Ninja\Intents;
|
2016-08-04 20:01:30 +03:00
|
|
|
|
2016-08-07 22:42:32 +03:00
|
|
|
use App\Models\EntityModel;
|
2017-01-30 21:40:43 +02:00
|
|
|
use Exception;
|
2016-08-07 22:42:32 +03:00
|
|
|
|
2016-08-09 23:06:24 +03:00
|
|
|
class CreateInvoiceIntent extends InvoiceIntent
|
2016-08-04 20:01:30 +03:00
|
|
|
{
|
|
|
|
|
public function process()
|
|
|
|
|
{
|
2016-08-13 22:19:37 +03:00
|
|
|
$client = $this->requestClient();
|
|
|
|
|
$invoiceItems = $this->requestInvoiceItems();
|
2016-08-04 20:01:30 +03:00
|
|
|
|
2017-01-30 18:05:31 +02:00
|
|
|
if (! $client) {
|
2016-08-10 15:57:34 +03:00
|
|
|
throw new Exception(trans('texts.client_not_found'));
|
2016-08-06 20:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
2016-08-13 22:19:37 +03:00
|
|
|
$data = array_merge($this->requestFields(), [
|
2016-08-07 22:42:32 +03:00
|
|
|
'client_id' => $client->id,
|
|
|
|
|
'invoice_items' => $invoiceItems,
|
2016-08-10 15:57:34 +03:00
|
|
|
]);
|
2016-08-04 20:01:30 +03:00
|
|
|
|
2016-08-13 22:19:37 +03:00
|
|
|
//var_dump($data);
|
2016-08-10 15:57:34 +03:00
|
|
|
|
2016-08-07 22:42:32 +03:00
|
|
|
$valid = EntityModel::validate($data, ENTITY_INVOICE);
|
2016-08-06 20:54:56 +03:00
|
|
|
|
2016-08-07 22:42:32 +03:00
|
|
|
if ($valid !== true) {
|
2016-08-09 23:06:24 +03:00
|
|
|
throw new Exception($valid);
|
2016-08-06 20:54:56 +03:00
|
|
|
}
|
2016-08-04 20:01:30 +03:00
|
|
|
|
2016-08-13 22:19:37 +03:00
|
|
|
$invoiceService = app('App\Services\InvoiceService');
|
|
|
|
|
$invoice = $invoiceService->save($data);
|
2016-08-10 15:57:34 +03:00
|
|
|
|
2017-01-30 18:05:31 +02:00
|
|
|
$invoiceItemIds = array_map(function ($item) {
|
2016-08-08 17:45:37 +03:00
|
|
|
return $item['public_id'];
|
|
|
|
|
}, $invoice->invoice_items->toArray());
|
2016-08-06 20:54:56 +03:00
|
|
|
|
2016-08-13 22:19:37 +03:00
|
|
|
$this->setStateEntityType(ENTITY_INVOICE);
|
|
|
|
|
$this->setStateEntities(ENTITY_CLIENT, $client->public_id);
|
|
|
|
|
$this->setStateEntities(ENTITY_INVOICE, $invoice->public_id);
|
|
|
|
|
$this->setStateEntities(ENTITY_INVOICE_ITEM, $invoiceItemIds);
|
2016-08-06 20:54:56 +03:00
|
|
|
|
2016-08-10 17:04:17 +03:00
|
|
|
return $this->createResponse(SKYPE_CARD_RECEIPT, $invoice->present()->skypeBot);
|
2016-08-04 20:01:30 +03:00
|
|
|
}
|
|
|
|
|
}
|