1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Ninja/Intents/InvoiceIntent.php

106 lines
3.4 KiB
PHP
Raw Normal View History

2016-08-09 22:06:24 +02:00
<?php namespace App\Ninja\Intents;
use Auth;
use Exception;
use App\Models\Invoice;
class InvoiceIntent extends BaseIntent
{
2016-08-13 21:19:37 +02:00
protected $fieldMap = [
'deposit' => 'partial',
'due' => 'due_date',
];
2016-08-10 14:57:34 +02:00
2016-08-09 22:06:24 +02:00
public function __construct($state, $data)
{
$this->invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
parent::__construct($state, $data);
}
2016-08-13 21:19:37 +02:00
protected function stateInvoice()
2016-08-09 22:06:24 +02:00
{
2016-08-13 21:19:37 +02:00
$invoiceId = $this->stateEntity(ENTITY_INVOICE);
2016-08-09 22:06:24 +02:00
2017-01-30 17:05:31 +01:00
if (! $invoiceId) {
2016-08-09 22:06:24 +02:00
throw new Exception(trans('texts.intent_not_supported'));
}
$invoice = Invoice::scope($invoiceId)->first();
2017-01-30 17:05:31 +01:00
if (! $invoice) {
2016-08-09 22:06:24 +02:00
throw new Exception(trans('texts.intent_not_supported'));
}
2017-01-30 17:05:31 +01:00
if (! Auth::user()->can('view', $invoice)) {
2016-08-09 22:06:24 +02:00
throw new Exception(trans('texts.not_allowed'));
}
return $invoice;
}
2016-08-13 21:19:37 +02:00
protected function requestInvoiceItems()
2016-08-09 22:06:24 +02:00
{
$productRepo = app('App\Ninja\Repositories\ProductRepository');
$invoiceItems = [];
2016-08-14 12:33:22 +02:00
$offset = 0;
2016-08-09 22:06:24 +02:00
2017-01-30 17:05:31 +01:00
if (! isset($this->data->compositeEntities) || ! count($this->data->compositeEntities)) {
2016-08-09 22:06:24 +02:00
return [];
}
foreach ($this->data->compositeEntities as $entity) {
if ($entity->parentType == 'InvoiceItem') {
$product = false;
$qty = 1;
foreach ($entity->children as $child) {
if ($child->type == 'Product') {
2016-08-14 12:33:22 +02:00
// check additional words in product name
// https://social.msdn.microsoft.com/Forums/azure/en-US/a508e039-0f76-4280-8156-4a017bcfc6dd/none-of-your-composite-entities-contain-all-of-the-highlighted-entities?forum=LUIS
$query = $this->data->query;
$startIndex = strpos($query, $child->value, $offset);
$endIndex = strlen($query);
$offset = $startIndex + 1;
foreach ($this->data->entities as $indexChild) {
if ($indexChild->startIndex > $startIndex) {
$endIndex = min($endIndex, $indexChild->startIndex);
}
}
$productName = substr($query, $startIndex, ($endIndex - $startIndex));
$product = $productRepo->findPhonetically($productName);
2016-08-09 22:06:24 +02:00
} else {
$qty = $child->value;
}
}
2016-08-13 21:19:37 +02:00
if ($product) {
$item['qty'] = $qty;
$item['product_key'] = $product->product_key;
$item['cost'] = $product->cost;
$item['notes'] = $product->notes;
2016-08-10 14:57:34 +02:00
2016-08-13 21:19:37 +02:00
if ($taxRate = $product->default_tax_rate) {
$item['tax_name1'] = $taxRate->name;
$item['tax_rate1'] = $taxRate->rate;
}
$invoiceItems[] = $item;
}
2016-08-09 22:06:24 +02:00
}
}
2016-08-13 21:19:37 +02:00
/*
if ( ! count($invoiceItems)) {
foreach ($this->data->entities as $param) {
if ($param->type == 'Product') {
$product = $productRepo->findPhonetically($param->entity);
}
}
}
*/
2016-08-09 22:06:24 +02:00
return $invoiceItems;
}
}