From aa69a1723243eb3a60874d2fd75f1857d32f3ffd Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Fri, 6 Nov 2015 10:21:45 +0200 Subject: [PATCH] Check for duplicate invoice number when approving a quote --- app/Models/Account.php | 9 +++++++++ app/Ninja/Repositories/InvoiceRepository.php | 16 ++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/Models/Account.php b/app/Models/Account.php index 16cdd8673f13..2784e40ff119 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -132,6 +132,15 @@ class Account extends Eloquent return !$this->language_id || $this->language_id == DEFAULT_LANGUAGE; } + public function hasInvoicePrefix() + { + if ( ! $this->invoice_number_prefix && ! $this->quote_number_prefix) { + return false; + } + + return $this->invoice_number_prefix != $this->quote_number_prefix; + } + public function getDisplayName() { if ($this->name) { diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 24e808c3f922..44ce5d4c9a4c 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -348,19 +348,19 @@ class InvoiceRepository extends BaseRepository $clone = Invoice::createNew($invoice); $clone->balance = $invoice->amount; - // if the invoice prefix is diff than quote prefix, use the same number for the invoice - if (($account->invoice_number_prefix || $account->quote_number_prefix) - && $account->invoice_number_prefix != $account->quote_number_prefix - && $account->share_counter) { - + // if the invoice prefix is diff than quote prefix, use the same number for the invoice (if it's available) + $invoiceNumber = false; + if ($account->hasInvoicePrefix() && $account->share_counter) { $invoiceNumber = $invoice->invoice_number; if ($account->quote_number_prefix && strpos($invoiceNumber, $account->quote_number_prefix) === 0) { $invoiceNumber = substr($invoiceNumber, strlen($account->quote_number_prefix)); } - $clone->invoice_number = $account->invoice_number_prefix.$invoiceNumber; - } else { - $clone->invoice_number = $account->getNextInvoiceNumber($invoice); + $invoiceNumber = $account->invoice_number_prefix . $invoiceNumber; + if (Invoice::scope()->withTrashed()->whereInvoiceNumber($invoiceNumber)->first()) { + $invoiceNumber = false; + } } + $clone->invoice_number = $invoiceNumber ?: $account->getNextInvoiceNumber($clone); foreach ([ 'client_id',