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