From 2ead34fef025b19946242f2c05c44179660abd12 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Mon, 28 Dec 2015 12:15:56 +0200 Subject: [PATCH] Enabled setting reminder before/after due date or invoice date --- app/Console/Commands/SendReminders.php | 2 +- app/Http/Controllers/AccountController.php | 5 ++- app/Http/routes.php | 8 +++- app/Models/Account.php | 26 +++++++++++ app/Models/Invoice.php | 18 -------- app/Ninja/Repositories/InvoiceRepository.php | 13 +++--- ...015_12_27_154513_add_reminder_settings.php | 44 +++++++++++++++++++ public/css/built.css | 4 ++ public/css/style.css | 4 ++ resources/lang/en/texts.php | 6 +++ resources/views/accounts/template.blade.php | 42 +++++++++++++++--- .../templates_and_reminders.blade.php | 16 ++++--- resources/views/invoices/edit.blade.php | 2 + 13 files changed, 149 insertions(+), 41 deletions(-) create mode 100644 database/migrations/2015_12_27_154513_add_reminder_settings.php diff --git a/app/Console/Commands/SendReminders.php b/app/Console/Commands/SendReminders.php index a7e6cc4d01..3243ac016e 100644 --- a/app/Console/Commands/SendReminders.php +++ b/app/Console/Commands/SendReminders.php @@ -44,7 +44,7 @@ class SendReminders extends Command $this->info($account->name . ': ' . count($invoices).' invoices found'); foreach ($invoices as $invoice) { - if ($reminder = $invoice->getReminder()) { + if ($reminder = $account->getInvoiceReminder($invoice)) { $this->info('Send to ' . $invoice->id); $this->mailer->sendInvoice($invoice, $reminder); } diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index d3e0d50f0e..bb2c82d8ff 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -450,8 +450,9 @@ class AccountController extends BaseController $account->$enableField = Input::get($enableField) ? true : false; if ($account->$enableField) { - $numDaysField = "num_days_{$type}"; - $account->$numDaysField = Input::get($numDaysField); + $account->{"num_days_{$type}"} = Input::get("num_days_{$type}"); + $account->{"field_{$type}"} = Input::get("field_{$type}"); + $account->{"direction_{$type}"} = Input::get("field_{$type}") == REMINDER_FIELD_INVOICE_DATE ? REMINDER_DIRECTION_AFTER : Input::get("direction_{$type}"); } } diff --git a/app/Http/routes.php b/app/Http/routes.php index 3a6aef5205..46545c06b6 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -144,7 +144,7 @@ Route::group(['middleware' => 'auth'], function() { Route::post('tasks/bulk', 'TaskController@bulk'); Route::get('api/recurring_invoices/{client_id?}', array('as'=>'api.recurring_invoices', 'uses'=>'InvoiceController@getRecurringDatatable')); - + Route::get('invoices/invoice_history/{invoice_id}', 'InvoiceController@invoiceHistory'); Route::get('quotes/quote_history/{invoice_id}', 'InvoiceController@invoiceHistory'); @@ -490,6 +490,12 @@ if (!defined('CONTACT_EMAIL')) { define('REMINDER2', 'reminder2'); define('REMINDER3', 'reminder3'); + define('REMINDER_DIRECTION_AFTER', 1); + define('REMINDER_DIRECTION_BEFORE', 2); + + define('REMINDER_FIELD_DUE_DATE', 1); + define('REMINDER_FIELD_INVOICE_DATE', 2); + define('SOCIAL_GOOGLE', 'Google'); define('SOCIAL_FACEBOOK', 'Facebook'); define('SOCIAL_GITHUB', 'GitHub'); diff --git a/app/Models/Account.php b/app/Models/Account.php index d908a892c5..abb4f2e7d1 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -768,6 +768,32 @@ class Account extends Eloquent } } + public function getReminderDate($reminder) + { + if ( ! $this->{"enable_reminder{$reminder}"}) { + return false; + } + + $numDays = $this->{"num_days_reminder{$reminder}"}; + $plusMinus = $this->{"direction_reminder{$reminder}"} == REMINDER_DIRECTION_AFTER ? '-' : '+'; + + return date('Y-m-d', strtotime("$plusMinus $numDays days")); + } + + public function getInvoiceReminder($invoice) + { + for ($i=1; $i<=3; $i++) { + if ($date = $this->getReminderDate($i)) { + $field = $this->{"field_reminder{$i}"} == REMINDER_FIELD_DUE_DATE ? 'due_date' : 'invoice_date'; + if ($this->$field == $date) { + return "reminder{$i}"; + } + } + } + + return false; + } + public function showTokenCheckbox() { if (!$this->isGatewayConfigured(GATEWAY_STRIPE)) { diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 3c95bcbddb..6b4c9a0b60 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -595,24 +595,6 @@ class Invoice extends EntityModel implements BalanceAffecting return false; } - public function getReminder() - { - for ($i=1; $i<=3; $i++) { - $field = "enable_reminder{$i}"; - if (!$this->account->$field) { - continue; - } - $field = "num_days_reminder{$i}"; - $date = date('Y-m-d', strtotime("- {$this->account->$field} days")); - - if ($this->due_date == $date) { - return "reminder{$i}"; - } - } - - return false; - } - public function getPDFString() { if (!env('PHANTOMJS_CLOUD_KEY')) { diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 08a0438d65..11e0b31746 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -639,16 +639,15 @@ class InvoiceRepository extends BaseRepository public function findNeedingReminding($account) { $dates = []; - for ($i = 1; $i <= 3; $i++) { - $field = "enable_reminder{$i}"; - if (!$account->$field) { - continue; + + for ($i=1; $i<=3; $i++) { + if ($date = $account->getReminderDate($i)) { + $field = $account->{"field_reminder{$i}"} == REMINDER_FIELD_DUE_DATE ? 'due_date' : 'invoice_date'; + $dates[] = "$field = '$date'"; } - $field = "num_days_reminder{$i}"; - $dates[] = "due_date = '".date('Y-m-d', strtotime("- {$account->$field} days"))."'"; } - $sql = implode(' OR ', $dates); + $sql = implode(' OR ', $dates); $invoices = Invoice::whereAccountId($account->id) ->where('balance', '>', 0) ->whereRaw('('.$sql.')') diff --git a/database/migrations/2015_12_27_154513_add_reminder_settings.php b/database/migrations/2015_12_27_154513_add_reminder_settings.php new file mode 100644 index 0000000000..0a94d49c78 --- /dev/null +++ b/database/migrations/2015_12_27_154513_add_reminder_settings.php @@ -0,0 +1,44 @@ +smallInteger('direction_reminder1')->default(1); + $table->smallInteger('direction_reminder2')->default(1); + $table->smallInteger('direction_reminder3')->default(1); + + $table->smallInteger('field_reminder1')->default(1); + $table->smallInteger('field_reminder2')->default(1); + $table->smallInteger('field_reminder3')->default(1); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('accounts', function ($table) { + $table->dropColumn('direction_reminder1'); + $table->dropColumn('direction_reminder2'); + $table->dropColumn('direction_reminder3'); + + $table->dropColumn('field_reminder1'); + $table->dropColumn('field_reminder2'); + $table->dropColumn('field_reminder3'); + }); + } + +} diff --git a/public/css/built.css b/public/css/built.css index 94e81164bd..fc019d3c79 100644 --- a/public/css/built.css +++ b/public/css/built.css @@ -3368,6 +3368,10 @@ ul.user-accounts a:hover div.remove { width: 350px; } +.smaller { + font-size: .9em; +} + /* Show selected section in settings nav */ .list-group-item.selected:before { position: absolute; diff --git a/public/css/style.css b/public/css/style.css index 345b37e5c7..d140f23f96 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -1017,6 +1017,10 @@ ul.user-accounts a:hover div.remove { width: 350px; } +.smaller { + font-size: .9em; +} + /* Show selected section in settings nav */ .list-group-item.selected:before { position: absolute; diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index aa6067c841..9eb66ca26d 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -984,4 +984,10 @@ return array( 'next_invoice_number' => 'The next invoice number is :number.', 'next_quote_number' => 'The next quote number is :number.', + 'days_before' => 'days before', + 'days_after' => 'days after', + 'field_due_date' => 'due date', + 'field_invoice_date' => 'invoice date', + 'schedule' => 'Schedule', + ); diff --git a/resources/views/accounts/template.blade.php b/resources/views/accounts/template.blade.php index 4938296e5b..0d4294af5b 100644 --- a/resources/views/accounts/template.blade.php +++ b/resources/views/accounts/template.blade.php @@ -1,13 +1,34 @@
@if (isset($isReminder) && $isReminder) -
+ + {!! Former::populateField('enable_' . $field, intval($account->{'enable_' . $field})) !!} + +
{!! Former::checkbox('enable_' . $field) ->text(trans('texts.enable'))->label('') !!} - {!! Former::input('num_days_' . $field) - ->label(trans('texts.num_days_reminder')) - ->addClass('enable-' . $field) !!} + + {!! Former::plaintext('schedule') + ->value( + Former::input('num_days_' . $field) + ->addClass('enable-' . $field) + ->style('float:left;width:20%') + ->raw() . + Former::select('direction_' . $field) + ->addOption(trans('texts.days_before'), REMINDER_DIRECTION_BEFORE) + ->addOption(trans('texts.days_after'), REMINDER_DIRECTION_AFTER) + ->addClass('enable-' . $field) + ->style('float:left;width:40%') + ->raw() . + '' . + Former::select('field_' . $field) + ->addOption(trans('texts.field_due_date'), REMINDER_FIELD_DUE_DATE) + ->addOption(trans('texts.field_invoice_date'), REMINDER_FIELD_INVOICE_DATE) + ->addClass('enable-' . $field) + ->style('float:left;width:40%') + ->raw() + ) !!}
@endif @@ -70,10 +91,17 @@ NINJA.formIsChanged = true; }); editors['{{ $field }}'] = editor; + + $('#field_{{ $field }}').change(function() { + setDirectionShown('{{ $field }}'); + }) + setDirectionShown('{{ $field }}'); + + $('.email-subject .input-group-addon').click(function() { + $('#templateHelpModal').modal('show'); + }); }); - $('.email-subject .input-group-addon').click(function() { - $('#templateHelpModal').modal('show'); - }); + \ No newline at end of file diff --git a/resources/views/accounts/templates_and_reminders.blade.php b/resources/views/accounts/templates_and_reminders.blade.php index 01a7b0099b..a709890b86 100644 --- a/resources/views/accounts/templates_and_reminders.blade.php +++ b/resources/views/accounts/templates_and_reminders.blade.php @@ -33,11 +33,6 @@ @endforeach @endforeach - {!! Former::populateField('enable_reminder1', intval($account->enable_reminder1)) !!} - {!! Former::populateField('enable_reminder2', intval($account->enable_reminder2)) !!} - {!! Former::populateField('enable_reminder3', intval($account->enable_reminder3)) !!} - -

{!! trans('texts.email_templates') !!}

@@ -188,6 +183,17 @@ $('.enable-reminder' + id).attr('disabled', !checked) } + function setDirectionShown(field) { + var val = $('#field_' + field).val(); + if (val == {{ REMINDER_FIELD_INVOICE_DATE }}) { + $('#days_after_' + field).show(); + $('#direction_' + field).hide(); + } else { + $('#days_after_' + field).hide(); + $('#direction_' + field).show(); + } + } + function processVariables(str) { if (!str) { return ''; diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index 725b8c0247..af2de672b0 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -131,6 +131,7 @@ @if ($invoice->recurring_invoice) {!! trans('texts.created_by_invoice', ['invoice' => link_to('/invoices/'.$invoice->recurring_invoice->public_id, trans('texts.recurring_invoice'))]) !!} @elseif ($invoice->id) + @if (isset($lastSent) && $lastSent) {!! trans('texts.last_sent_on', ['date' => link_to('/invoices/'.$lastSent->public_id, $invoice->last_sent_date, ['id' => 'lastSent'])]) !!}
@endif @@ -138,6 +139,7 @@ {!! trans('texts.next_send_on', ['date' => ''.$account->formatDate($invoice->getNextSendDate()). '']) !!} @endif +
@endif