1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Ninja/Intents/UpdateInvoiceIntent.php

57 lines
1.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Intents;
2016-08-09 22:06:24 +02:00
use App\Models\EntityModel;
use App\Models\Invoice;
2017-01-30 20:40:43 +01:00
use Exception;
2016-08-09 22:06:24 +02:00
class UpdateInvoiceIntent extends InvoiceIntent
{
public function process()
{
2016-08-13 21:19:37 +02:00
$invoice = $this->stateInvoice();
$invoiceItems = $this->requestInvoiceItems();
2016-08-09 22:06:24 +02:00
2016-08-13 21:19:37 +02:00
$data = array_merge($this->requestFields(), [
2016-08-10 14:57:34 +02:00
'public_id' => $invoice->public_id,
'invoice_items' => array_merge($invoice->invoice_items->toArray(), $invoiceItems),
]);
2016-08-09 22:06:24 +02:00
2016-08-10 14:57:34 +02:00
// map the cost and qty fields to the invoice items
if (isset($data['cost']) || isset($data['quantity'])) {
foreach ($data['invoice_items'] as $key => $item) {
// if it's new or we recently created it
if (empty($item['public_id']) || in_array($item['public_id'], $this->entities(ENTITY_INVOICE_ITEM))) {
$data['invoice_items'][$key]['cost'] = isset($data['cost']) ? $data['cost'] : $item['cost'];
$data['invoice_items'][$key]['qty'] = isset($data['quantity']) ? $data['quantity'] : $item['qty'];
}
}
}
2016-08-09 22:06:24 +02:00
2016-08-13 21:19:37 +02:00
//var_dump($data);
2016-08-10 14:57:34 +02:00
$valid = EntityModel::validate($data, ENTITY_INVOICE, $invoice);
if ($valid !== true) {
throw new Exception($valid);
}
$invoice = $this->invoiceRepo->save($data, $invoice);
$invoiceItems = array_slice($invoice->invoice_items->toArray(), count($invoiceItems) * -1);
2017-01-30 17:05:31 +01:00
$invoiceItemIds = array_map(function ($item) {
2016-08-10 14:57:34 +02:00
return $item['public_id'];
}, $invoiceItems);
2016-08-13 21:19:37 +02:00
$this->setStateEntities(ENTITY_INVOICE_ITEM, $invoiceItemIds);
2016-08-10 14:57:34 +02:00
$response = $invoice
->load('invoice_items')
->present()
->skypeBot;
2016-08-10 16:04:17 +02:00
return $this->createResponse(SKYPE_CARD_RECEIPT, $response);
2016-08-09 22:06:24 +02:00
}
}