1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Ninja/Intents/CreateInvoiceIntent.php
2019-01-30 22:25:37 +11:00

47 lines
1.3 KiB
PHP

<?php
namespace App\Ninja\Intents;
use App\Models\EntityModel;
use Exception;
class CreateInvoiceIntent extends InvoiceIntent
{
public function process()
{
$client = $this->requestClient();
$invoiceItems = $this->requestInvoiceItems();
if (! $client) {
throw new Exception(trans('texts.client_not_found'));
}
$data = array_merge($this->requestFields(), [
'client_id' => $client->id,
'invoice_items' => $invoiceItems,
]);
//var_dump($data);
$valid = EntityModel::validate($data, ENTITY_INVOICE);
if ($valid !== true) {
throw new Exception($valid);
}
$invoiceService = app('App\Services\InvoiceService');
$invoice = $invoiceService->save($data);
$invoiceItemIds = array_map(function ($item) {
return $item['public_id'];
}, $invoice->invoice_items->toArray());
$this->setStateEntityType(ENTITY_INVOICE);
$this->setStateEntities(ENTITY_CLIENT, $client->public_id);
$this->setStateEntities(ENTITY_INVOICE, $invoice->public_id);
$this->setStateEntities(ENTITY_INVOICE_ITEM, $invoiceItemIds);
return $this->createResponse(SKYPE_CARD_RECEIPT, $invoice->present()->skypeBot);
}
}