diff --git a/app/Models/Account.php b/app/Models/Account.php index 16cdd8673f..2784e40ff1 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 24e808c3f9..44ce5d4c9a 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',