From 7263e87db25b33df7de42e5a5bc6bde69bc556d4 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Mon, 16 Jan 2017 13:59:46 +0200 Subject: [PATCH] Enable setting default payment terms --- app/Models/Account.php | 8 ++++++++ app/Ninja/Presenters/AccountPresenter.php | 11 +++++++++++ app/Ninja/Repositories/InvoiceRepository.php | 6 +++++- .../2017_01_01_214241_add_inclusive_taxes.php | 2 +- resources/views/accounts/details.blade.php | 5 +++++ resources/views/invoices/edit.blade.php | 1 + 6 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/Models/Account.php b/app/Models/Account.php index 88d9308e01..e6633be9f7 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -89,6 +89,7 @@ class Account extends Eloquent 'email_design_id', 'enable_email_markup', 'domain_id', + 'payment_terms', ]; /** @@ -1724,6 +1725,13 @@ class Account extends Eloquent return $this->enable_email_markup; } + + public function defaultDueDate() + { + $numDays = $this->payment_terms == -1 ? 0 : $this->payment_terms; + + return Carbon::now($this->getTimezone())->addDays($numDays)->format('Y-m-d'); + } } Account::updated(function ($account) diff --git a/app/Ninja/Presenters/AccountPresenter.php b/app/Ninja/Presenters/AccountPresenter.php index 00d496461b..5bd94dbd90 100644 --- a/app/Ninja/Presenters/AccountPresenter.php +++ b/app/Ninja/Presenters/AccountPresenter.php @@ -52,6 +52,17 @@ class AccountPresenter extends Presenter return $this->entity->size ? $this->entity->size->name : ''; } + public function dueDatePlaceholder() + { + if ($this->entity->payment_terms == 0) { + return ' '; + } + + $date = $this->entity->defaultDueDate(); + + return $date ? Utils::fromSqlDate($date) : ' '; + } + private function createRBit($type, $source, $properties) { $data = new stdClass(); diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index f5a591d046..015c7dc630 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -4,6 +4,7 @@ use App\Models\Account; use DB; use Utils; use Auth; +use Carbon; use App\Models\Invoice; use App\Models\InvoiceItem; use App\Models\Invitation; @@ -305,6 +306,9 @@ 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(); + } } else { $invoice = Invoice::scope($publicId)->firstOrFail(); if (Utils::isNinjaDev()) { @@ -380,7 +384,7 @@ class InvoiceRepository extends BaseRepository $invoice->due_date = $data['due_date']; } } else { - if (isset($data['due_date']) || isset($data['due_date_sql'])) { + if (!empty($data['due_date']) || !empty($data['due_date_sql'])) { $invoice->due_date = isset($data['due_date_sql']) ? $data['due_date_sql'] : Utils::toSqlDate($data['due_date']); } $invoice->frequency_id = 0; diff --git a/database/migrations/2017_01_01_214241_add_inclusive_taxes.php b/database/migrations/2017_01_01_214241_add_inclusive_taxes.php index 96f28dbcb9..f5697a6d19 100644 --- a/database/migrations/2017_01_01_214241_add_inclusive_taxes.php +++ b/database/migrations/2017_01_01_214241_add_inclusive_taxes.php @@ -34,7 +34,7 @@ class AddInclusiveTaxes extends Migration $table->integer('client_number_counter')->default(0)->nullable(); $table->text('client_number_pattern')->nullable(); $table->tinyInteger('domain_id')->default(1)->nullable()->unsigned(); - $table->tinyInteger('payment_terms')->nullable()->unsigned(); + $table->tinyInteger('payment_terms')->nullable(); }); Schema::table('activities', function ($table) diff --git a/resources/views/accounts/details.blade.php b/resources/views/accounts/details.blade.php index cf83bf2936..21b83738b2 100644 --- a/resources/views/accounts/details.blade.php +++ b/resources/views/accounts/details.blade.php @@ -59,6 +59,11 @@ @endif + {!! Former::select('payment_terms') + ->addOption('','') + ->fromQuery(Cache::get('paymentTerms'), 'name', 'num_days') + ->help(trans('texts.payment_terms_help')) !!} + {!! Former::select('size_id') ->addOption('','') ->fromQuery($sizes, 'name', 'id') !!} diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index 56a51276cd..39825a8e0a 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -149,6 +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()) ->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()')