diff --git a/app/Console/Commands/SendReminders.php b/app/Console/Commands/SendReminders.php index a7e6cc4d019a..3243ac016eaf 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 d3e0d50f0e48..bb2c82d8ff33 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 3a6aef5205d7..46545c06b6b8 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 d908a892c581..abb4f2e7d169 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 3c95bcbddba0..6b4c9a0b60a0 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 08a0438d653f..11e0b31746d3 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 000000000000..0a94d49c788d --- /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 94e81164bd99..fc019d3c790b 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 345b37e5c761..d140f23f9661 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 aa6067c841cc..9eb66ca26d8d 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 4938296e5b85..0d4294af5b6f 100644 --- a/resources/views/accounts/template.blade.php +++ b/resources/views/accounts/template.blade.php @@ -1,13 +1,34 @@