1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Ninja/Intents/CreateInvoiceIntent.php
2016-08-13 22:19:37 +03:00

45 lines
1.3 KiB
PHP

<?php namespace App\Ninja\Intents;
use Exception;
use App\Models\EntityModel;
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);
}
}