invoiceninja/app/Ninja/Intents/CreateInvoiceIntent.php

47 lines
1.3 KiB
PHP
Raw Normal View History

2016-08-04 20:01:30 +03:00
<?php namespace App\Ninja\Intents;
2016-08-07 22:42:32 +03:00
use App\Models\EntityModel;
2016-08-04 20:01:30 +03:00
class CreateInvoiceIntent extends BaseIntent
{
public function process()
{
$invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
2016-08-06 20:54:56 +03:00
$client = $this->parseClient();
$invoiceItems = $this->parseInvoiceItems();
2016-08-04 20:01:30 +03:00
2016-08-07 22:42:32 +03:00
if ( ! $client) {
2016-08-06 20:54:56 +03:00
return view('bots.skype.message', [
'message' => trans('texts.client_not_found')
])->render();
}
2016-08-07 22:42:32 +03:00
$data = [
'client_id' => $client->id,
'invoice_items' => $invoiceItems,
];
2016-08-04 20:01:30 +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) {
return view('bots.skype.message', [
'message' => $valid
])->render();
2016-08-06 20:54:56 +03:00
}
2016-08-04 20:01:30 +03:00
2016-08-07 22:42:32 +03:00
$invoice = $invoiceRepo->save($data);
2016-08-08 17:45:37 +03:00
$invoiceItemIds = array_map(function($item) {
return $item['public_id'];
}, $invoice->invoice_items->toArray());
2016-08-06 20:54:56 +03:00
2016-08-08 17:45:37 +03:00
$this->setState(ENTITY_CLIENT, [$client->public_id]);
$this->setState(ENTITY_INVOICE, [$invoice->public_id]);
$this->setState(ENTITY_INVOICE_ITEM, $invoiceItemIds);
2016-08-06 20:54:56 +03:00
2016-08-07 22:42:32 +03:00
return view('bots.skype.invoice', [
'invoice' => $invoice
])->render();
2016-08-04 20:01:30 +03:00
}
}