diff --git a/app/Ninja/Intents/BaseIntent.php b/app/Ninja/Intents/BaseIntent.php index 0e6e136ced64..7693627eb85f 100644 --- a/app/Ninja/Intents/BaseIntent.php +++ b/app/Ninja/Intents/BaseIntent.php @@ -84,6 +84,17 @@ class BaseIntent return false; } + protected function hasField($field, $value = false) + { + $fieldValue = $this->getField($field); + + if ($value) { + return $fieldValue && $fieldValue == $value; + } else { + return $fieldValue ? true : false; + } + } + public function process() { throw new Exception(trans('texts.intent_not_supported')); @@ -154,6 +165,20 @@ class BaseIntent return $client; } + protected function requestInvoice() + { + $invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository'); + $invoice = false; + + foreach ($this->data->entities as $param) { + if ($param->type == 'builtin.number') { + return $invoiceRepo->findPhonetically($param->entity); + } + } + + return false; + } + protected function requestFields() { $data = []; diff --git a/app/Ninja/Intents/WebApp/CreatePaymentIntent.php b/app/Ninja/Intents/WebApp/CreatePaymentIntent.php index 7f62428c1274..63cb631de1b1 100644 --- a/app/Ninja/Intents/WebApp/CreatePaymentIntent.php +++ b/app/Ninja/Intents/WebApp/CreatePaymentIntent.php @@ -8,12 +8,18 @@ class CreatePaymentIntent extends BaseIntent { public function process() { - $client = $this->requestClient(); - $clientPublicId = $client ? $client->public_id : null; + $clientPublicId = 0; + $invoicePublicId = 0; + + if ($invoice = $this->requestInvoice()) { + $invoicePublicId = $invoice->public_id; + } elseif ($client = $this->requestClient()) { + $clientPublicId = $client->public_id; + } //$invoiceItems = $this->requestInvoiceItems(); - $url = '/payments/create/' . $clientPublicId . '?'; + $url = sprintf('/payments/create/%s/%s', $clientPublicId, $invoicePublicId); //$url .= $this->requestFieldsAsString(Invoice::$requestFields); return redirect($url); diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 3daa8b3d89b4..6c33d8c8c88d 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -1121,4 +1121,26 @@ class InvoiceRepository extends BaseRepository $this->save($data, $invoice); $invoice->load('invoice_items'); } + + public function findPhonetically($invoiceNumber) + { + $map = []; + $max = SIMILAR_MIN_THRESHOLD; + $invoiceId = 0; + + $invoices = Invoice::scope()->get(['id', 'invoice_number', 'public_id']); + + foreach ($invoices as $invoice) { + $map[$invoice->id] = $invoice; + $similar = similar_text($invoiceNumber, $invoice->invoice_number, $percent); + var_dump($similar); + if ($percent > $max) { + $invoiceId = $invoice->id; + $max = $percent; + } + } + + return ($invoiceId && isset($map[$invoiceId])) ? $map[$invoiceId] : null; + } + } diff --git a/resources/views/partials/speech_recognition.blade.php b/resources/views/partials/speech_recognition.blade.php index 9b94ffcdb479..cfb63f7b3413 100644 --- a/resources/views/partials/speech_recognition.blade.php +++ b/resources/views/partials/speech_recognition.blade.php @@ -172,11 +172,11 @@ } function onMicrophoneClick() { - //$('#search').val('create new invoice for edgar a po number of 1234'); + $('#search').val('new payment for invoice 1279'); //$('#search').val('invoice for edgar'); //$('#search').val('create a task for edgar'); - //$('#search-form').submit(); - //return; + $('#search-form').submit(); + return; $('.fa-microphone').hide(); $('#search').val("{{ trans('texts.listening') }}");