diff --git a/app/Models/Account.php b/app/Models/Account.php index 9742d3cbb5..cbfbe647b6 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -1726,11 +1726,26 @@ class Account extends Eloquent return $this->enable_email_markup; } - public function defaultDueDate() + public function defaultDaysDue($client = false) { - $numDays = $this->payment_terms == -1 ? 0 : $this->payment_terms; + if ($client && $client->payment_terms != 0) { + return $client->defaultDaysDue(); + } - return Carbon::now($this->getTimezone())->addDays($numDays)->format('Y-m-d'); + return $this->payment_terms == -1 ? 0 : $this->payment_terms; + } + + public function defaultDueDate($client = false) + { + if ($client && $client->payment_terms != 0) { + $numDays = $client->defaultDaysDue(); + } elseif ($this->payment_terms != 0) { + $numDays = $this->defaultDaysDue(); + } else { + return null; + } + + return Carbon::now()->addDays($numDays)->format('Y-m-d'); } public function hasMultipleAccounts() diff --git a/app/Models/Client.php b/app/Models/Client.php index d88f5986c0..04435245ea 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -531,6 +531,11 @@ class Client extends EntityModel public function hasAutoBillConfigurableInvoices(){ return $this->invoices()->whereIn('auto_bill', [AUTO_BILL_OPT_IN, AUTO_BILL_OPT_OUT])->count() > 0; } + + public function defaultDaysDue() + { + return $this->payment_terms == -1 ? 0 : $this->payment_terms; + } } Client::creating(function ($client) { diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 2d161f0e01..b05f66b19a 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -1031,13 +1031,12 @@ class Invoice extends EntityModel implements BalanceAffecting if($dueDate) { return date('Y-m-d', $dueDate);// SQL format } - } - else if ($this->client->payment_terms != 0) { + } else if ($this->client->payment_terms != 0) { // No custom due date set for this invoice; use the client's payment terms - $days = $this->client->payment_terms; - if ($days == -1) { - $days = 0; - } + $days = $this->client->defaultDaysDue(); + return date('Y-m-d', strtotime('+'.$days.' day', $now)); + } else if ($this->account->payment_terms != 0) { + $days = $this->account->defaultDaysDue(); return date('Y-m-d', strtotime('+'.$days.' day', $now)); } } diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 000a2970e5..dbcf79503c 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -5,6 +5,7 @@ use DB; use Utils; use Auth; use Carbon; +use App\Models\Client; use App\Models\Invoice; use App\Models\InvoiceItem; use App\Models\Invitation; @@ -308,8 +309,11 @@ class InvoiceRepository extends BaseRepository if (isset($data['has_expenses']) && filter_var($data['has_expenses'], FILTER_VALIDATE_BOOLEAN)) { $invoice->has_expenses = true; } - if ($account->payment_terms != 0) { - $invoice->due_date = $account->defaultDueDate(); + + // set the default due date + if ($entityType == ENTITY_INVOICE) { + $client = Client::scope()->whereId($data['client_id'])->first(); + $invoice->due_date = $account->defaultDueDate($client); } } else { $invoice = Invoice::scope($publicId)->firstOrFail(); @@ -667,6 +671,7 @@ class InvoiceRepository extends BaseRepository } $clone->invoice_number = $invoiceNumber ?: $account->getNextNumber($clone); $clone->invoice_date = Utils::today(false)->format('Y-m-d'); + $clone->due_date = $account->defaultDueDate($invoice->client); foreach ([ 'client_id', diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index 6958eff5a2..3a41a4652f 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -149,7 +149,7 @@ {!! Former::text('invoice_date')->data_bind("datePicker: invoice_date, valueUpdate: 'afterkeydown'")->label(trans("texts.{$entityType}_date")) ->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT))->appendIcon('calendar')->addGroupClass('invoice_date') !!} {!! Former::text('due_date')->data_bind("datePicker: due_date, valueUpdate: 'afterkeydown'")->label(trans("texts.{$entityType}_due_date")) - ->placeholder($invoice->exists ? ' ' : $account->present()->dueDatePlaceholder()) + ->placeholder($invoice->exists || $invoice->isQuote() ? ' ' : $account->present()->dueDatePlaceholder()) ->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT))->appendIcon('calendar')->addGroupClass('due_date') !!} {!! Former::text('partial')->data_bind("value: partial, valueUpdate: 'afterkeydown'")->onkeyup('onPartialChange()')