diff --git a/app/Console/Commands/SendReminders.php b/app/Console/Commands/SendReminders.php index 69d3bbc945..51e5a7b940 100644 --- a/app/Console/Commands/SendReminders.php +++ b/app/Console/Commands/SendReminders.php @@ -126,6 +126,7 @@ class SendReminders extends Command continue; } + // standard reminders $invoices = $this->invoiceRepo->findNeedingReminding($account); $this->info($account->name . ': ' . count($invoices) . ' invoices found'); @@ -135,6 +136,15 @@ class SendReminders extends Command $this->mailer->sendInvoice($invoice, $reminder); } } + + // endless reminders + $invoices = $this->invoiceRepo->findNeedingEndlessReminding($account); + $this->info($account->name . ': ' . count($invoices) . ' endless invoices found'); + + foreach ($invoices as $invoice) { + $this->info('Send email: ' . $invoice->id); + $this->mailer->sendInvoice($invoice, 'reminder4'); + } } } diff --git a/app/Listeners/InvoiceListener.php b/app/Listeners/InvoiceListener.php index f69bbcda9e..5f06c4d702 100644 --- a/app/Listeners/InvoiceListener.php +++ b/app/Listeners/InvoiceListener.php @@ -6,6 +6,7 @@ use Illuminate\Queue\Events\JobExceptionOccurred; use App\Events\InvoiceInvitationWasViewed; use App\Events\InvoiceWasCreated; use App\Events\InvoiceWasUpdated; +use App\Events\InvoiceWasEmailed; use App\Events\PaymentFailed; use App\Events\PaymentWasCreated; use App\Events\PaymentWasDeleted; @@ -60,6 +61,16 @@ class InvoiceListener $invitation->markViewed(); } + /** + * @param InvoiceWasEmailed $event + */ + public function emailedInvoice(InvoiceWasEmailed $event) + { + $invoice = $event->invoice; + $invoice->last_sent_date = date('Y-m-d'); + $invoice->save(); + } + /** * @param PaymentWasCreated $event */ diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index a48a27530f..fe45ace9f3 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -697,7 +697,7 @@ class AccountRepository public function findWithReminders() { - return Account::whereRaw('enable_reminder1 = 1 OR enable_reminder2 = 1 OR enable_reminder3 = 1')->get(); + return Account::whereRaw('enable_reminder1 = 1 OR enable_reminder2 = 1 OR enable_reminder3 = 1 OR enable_reminder4 = 1')->get(); } public function findWithFees() diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index be8eec1ebe..85e23ea7ed 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -1204,6 +1204,40 @@ class InvoiceRepository extends BaseRepository return $invoices; } + public function findNeedingEndlessReminding(Account $account) + { + $frequencyId = $account->account_email_settings->frequency_id_reminder4; + $frequency = Utils::getFromCache($frequencyId, 'frequencies'); + + $lastSentDate = date_create(); + $lastSentDate->sub(date_interval_create_from_date_string($frequency->date_interval)); + + $invoices = Invoice::invoiceType(INVOICE_TYPE_STANDARD) + ->with('client', 'invoice_items') + ->whereHas('client', function ($query) { + $query->whereSendReminders(true); + }) + ->whereAccountId($account->id) + ->where('balance', '>', 0) + ->where('is_recurring', '=', false) + ->whereIsPublic(true) + ->where('last_sent_date', '<', $lastSentDate); + + for ($i=1; $i<=3; $i++) { + if (!$account->{"enable_reminder{$i}"}) { + continue; + } + $field = $account->{"field_reminder{$i}"} == REMINDER_FIELD_DUE_DATE ? 'due_date' : 'invoice_date'; + $date = date_create(); + if ($account->{"direction_reminder{$i}"} == REMINDER_DIRECTION_AFTER) { + $date->sub(date_interval_create_from_date_string($account->{"num_days_reminder{$i}"} . ' days')); + } + $invoices->where($field, '<', $date); + } + + return $invoices->get(); + } + public function clearGatewayFee($invoice) { $account = $invoice->account; diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 2b266bbbad..35cf2f23f7 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -56,6 +56,7 @@ class EventServiceProvider extends ServiceProvider ], 'App\Events\InvoiceWasEmailed' => [ 'App\Listeners\NotificationListener@emailedInvoice', + 'App\Listeners\InvoiceListener@emailedInvoice', ], 'App\Events\InvoiceInvitationWasEmailed' => [ 'App\Listeners\ActivityListener@emailedInvoice', diff --git a/database/migrations/2017_12_13_074024_add_remember_2fa_token.php b/database/migrations/2017_12_13_074024_add_remember_2fa_token.php index 4bd0006e04..46fee1c73d 100644 --- a/database/migrations/2017_12_13_074024_add_remember_2fa_token.php +++ b/database/migrations/2017_12_13_074024_add_remember_2fa_token.php @@ -69,6 +69,20 @@ class AddRemember2faToken extends Migration $table->unsignedInteger('frequency_id_reminder4')->nullable(); }); + Schema::table('frequencies', function ($table) { + $table->string('date_interval')->nullable(); + }); + + DB::statement("update invoices, ( + select max(created_at) created_at, invoice_id + from activities + where activity_type_id = 6 + group by invoice_id + ) as activities + set invoices.last_sent_date = activities.created_at + where invoices.id = activities.invoice_id + and invoices.is_recurring = 0 + and invoices.invoice_type_id = 1"); } /** @@ -117,5 +131,10 @@ class AddRemember2faToken extends Migration $table->dropColumn('email_template_reminder4'); $table->dropColumn('frequency_id_reminder4'); }); + + Schema::table('frequencies', function ($table) { + $table->dropColumn('date_interval'); + }); + } } diff --git a/database/seeds/FrequencySeeder.php b/database/seeds/FrequencySeeder.php index 23d80adba9..4935c57af3 100644 --- a/database/seeds/FrequencySeeder.php +++ b/database/seeds/FrequencySeeder.php @@ -9,21 +9,22 @@ class FrequencySeeder extends Seeder Eloquent::unguard(); $frequencies = [ - ['name' => 'Weekly'], - ['name' => 'Two weeks'], - ['name' => 'Four weeks'], - ['name' => 'Monthly'], - ['name' => 'Two months'], - ['name' => 'Three months'], - ['name' => 'Four months'], - ['name' => 'Six months'], - ['name' => 'Annually'], + ['name' => 'Weekly', 'date_interval' => '1 week'], + ['name' => 'Two weeks', 'date_interval' => '2 weeks'], + ['name' => 'Four weeks', 'date_interval' => '4 weeks'], + ['name' => 'Monthly', 'date_interval' => '1 month'], + ['name' => 'Two months', 'date_interval' => '2 months'], + ['name' => 'Three months', 'date_interval' => '3 months'], + ['name' => 'Four months', 'date_interval' => '4 months'], + ['name' => 'Six months', 'date_interval' => '6 months'], + ['name' => 'Annually', 'date_interval' => '1 year'], ]; foreach ($frequencies as $frequency) { $record = Frequency::whereName($frequency['name'])->first(); if ($record) { - //$record->save(); + $record->date_interval = $frequency['date_interval']; + $record->save(); } else { Frequency::create($frequency); }