From a1afb3851436b49a9d48b12ae6a52b3d772c2ffa Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 5 Nov 2020 08:19:44 +1100 Subject: [PATCH 1/8] Fix for invoice attachment --- app/Mail/Engine/InvoiceEmailEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Mail/Engine/InvoiceEmailEngine.php b/app/Mail/Engine/InvoiceEmailEngine.php index 90809330da..3a160bb3d5 100644 --- a/app/Mail/Engine/InvoiceEmailEngine.php +++ b/app/Mail/Engine/InvoiceEmailEngine.php @@ -80,7 +80,7 @@ class InvoiceEmailEngine extends BaseEmailEngine ->setViewText(ctrans('texts.view_invoice')); if ($this->client->getSetting('pdf_email_attachment') !== false) { - $this->setAttachments($invitation->pdf_file_path()); + $this->setAttachments($this->invoice->pdf_file_path()); } return $this; From 4784e808594b19a8bf0082807209b1df33e9bf37 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 5 Nov 2020 09:48:46 +1100 Subject: [PATCH 2/8] Working on templates / reminders and late fees --- app/DataMapper/InvoiceItem.php | 2 +- app/Jobs/Ninja/SendReminders.php | 150 ++++++++++++++++++++++++---- app/Utils/Traits/MakesReminders.php | 8 +- 3 files changed, 133 insertions(+), 27 deletions(-) diff --git a/app/DataMapper/InvoiceItem.php b/app/DataMapper/InvoiceItem.php index ccfe513824..6349e057ba 100644 --- a/app/DataMapper/InvoiceItem.php +++ b/app/DataMapper/InvoiceItem.php @@ -51,7 +51,7 @@ class InvoiceItem public $custom_value4 = ''; - public $type_id = 1; //1 = product, 2 = service, 3 unpaid gateway fee, 4 paid gateway fee + public $type_id = 1; //1 = product, 2 = service, 3 unpaid gateway fee, 4 paid gateway fee, 5 late fee public static $casts = [ 'type_id' => 'string', diff --git a/app/Jobs/Ninja/SendReminders.php b/app/Jobs/Ninja/SendReminders.php index a10e45075c..530795612c 100644 --- a/app/Jobs/Ninja/SendReminders.php +++ b/app/Jobs/Ninja/SendReminders.php @@ -11,8 +11,13 @@ namespace App\Jobs\Ninja; +use App\DataMapper\InvoiceItem; +use App\Events\Invoice\InvoiceWasEmailed; +use App\Jobs\Entity\EmailEntity; use App\Libraries\MultiDB; use App\Models\Invoice; +use App\Utils\Ninja; +use App\Utils\Traits\MakesDates; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -21,7 +26,7 @@ use Illuminate\Queue\SerializesModels; class SendReminders implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesDates; /** * Create a new job instance. @@ -73,6 +78,7 @@ class SendReminders implements ShouldQueue $invoices = Invoice::where('is_deleted', 0) ->where('balance', '>', 0) ->whereDate('next_send_date', '<=', now()->startOfDay()) + ->whereNotNull('next_send_date') ->cursor(); //we only need invoices that are payable @@ -84,29 +90,129 @@ class SendReminders implements ShouldQueue $reminder_template = $invoice->calculateTemplate('invoice'); - if($reminder_template == 'reminder1'){ - - } - elseif($reminder_template == 'reminder2'){ - - } - elseif($reminder_template == 'reminder3'){ - - } - elseif($reminder_template == 'endless_reminder'){ - - } - - //@todo + if(in_array($reminder_template, ['reminder1', 'reminder2', 'reminder3', 'endless_reminder'])) + $this->sendReminder($invoice, $reminder_template); }); - //iterate through all the reminder emails due today - // - //determine which reminder - // - //determine late fees - // - //send + + } + + private function checkSendSetting($invoice, $template) + { + switch ($template) { + case 'reminder1': + return $invoice->client->getSetting('enable_reminder1'); + break; + case 'reminder2': + return $invoice->client->getSetting('enable_reminder2'); + break; + case 'reminder3': + return $invoice->client->getSetting('enable_reminder3'); + break; + case 'endless_reminder': + return $invoice->client->getSetting('enable_reminder_endless'); + break; + default: + return false; + break; + } + } + + private function calculateNextSendDate($invoice, $template) + { + + } + + private function sendReminder($invoice, $template) + { + $invoice = $this->calcLateFee($invoice, $template); + + $invoice->invitations->each(function ($invitation) use($template, $invoice){ + + //only send if enable_reminder setting is toggled to yes + if($this->checkSendSetting($invoice, $template)) + EmailEntity::dispatch($invitation, $invitation->company, $template); + + }); + + if ($invoice->invitations->count() > 0) + event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars())); + + $invoice->last_sent_date = now(); + $invoice->reminder_last_send = now(); + $invoice->next_send_date = $this->calculateNextSendDate($invoice, $template); + + if(in_array($template, ['reminder1', 'reminder2', 'reminder3'])) + $invoice->{$template."_send"} = now(); + + //calculate next_send_date + + $invoice->save(); + } + + private function calcLateFee($invoice, $template) :Invoice + { + $late_fee_amount = 0; + $late_fee_percent = 0; + + switch ($template) { + case 'reminder1': + $late_fee_amount = $invoice->client->getSetting('late_fee_amount1'); + $late_fee_percent = $invoice->client->getSetting('late_fee_percent1'); + break; + case 'reminder2': + $late_fee_amount = $invoice->client->getSetting('late_fee_amount2'); + $late_fee_percent = $invoice->client->getSetting('late_fee_percent2'); + break; + case 'reminder3': + $late_fee_amount = $invoice->client->getSetting('late_fee_amount3'); + $late_fee_percent = $invoice->client->getSetting('late_fee_percent3'); + break; + case 'endless_reminder': + $late_fee_amount = $invoice->client->getSetting('late_fee_endless_amount'); + $late_fee_percent = $invoice->client->getSetting('late_fee_endless_percent'); + break; + default: + $late_fee_amount = 0; + $late_fee_percent = 0; + break; + } + + return $this->setLateFee($invoice, $late_fee_amount, $late_fee_percent); + + } + + + private function setLateFee($invoice, $amount, $percent) :Invoice + { + if ($amount <= 0 && $percent <= 0) { + return $invoice; + } + + $fee = $amount; + + if ($invoice->partial > 0) + $fee += round($invoice->partial * $percent / 100, 2); + else + $fee += round($invoice->balance * $percent / 100, 2); + + $invoice_item = new InvoiceItem; + $invoice_item->type_id = '5'; + $invoice_item->product_key = trans('texts.fee'); + $invoice_item->notes = ctrans('texts.late_fee_added', ['date' => $this->formatDate(now()->startOfDay(), $invoice->client->date_format())]); + $invoice_item->quantity = 1; + $invoice_item->cost = $fee; + + $invoice_items = $invoice->line_items; + $invoice_items[] = $invoice_item; + + $invoice->line_items = $invoice_items; + + /**Refresh Invoice values*/ + $invoice = $invoice->calc()->getInvoice(); + + return $invoice; + } } \ No newline at end of file diff --git a/app/Utils/Traits/MakesReminders.php b/app/Utils/Traits/MakesReminders.php index 9754573b40..1c039de1fa 100644 --- a/app/Utils/Traits/MakesReminders.php +++ b/app/Utils/Traits/MakesReminders.php @@ -190,22 +190,22 @@ trait MakesReminders return $entity_string; //if the invoice - if ($client->getSetting('enable_reminder1') !== false && $this->inReminderWindow( + if ($this->inReminderWindow( $client->getSetting('schedule_reminder1'), $client->getSetting('num_days_reminder1') )) { return 'reminder1'; - } elseif ($client->getSetting('enable_reminder2') !== false && $this->inReminderWindow( + } elseif ($this->inReminderWindow( $client->getSetting('schedule_reminder2'), $client->getSetting('num_days_reminder2') )) { return 'reminder2'; - } elseif ($client->getSetting('enable_reminder3') !== false && $this->inReminderWindow( + } elseif ($this->inReminderWindow( $client->getSetting('schedule_reminder3'), $client->getSetting('num_days_reminder3') )) { return 'reminder3'; - } elseif($client->getSetting('enable_reminder_endless') !== false && $this->checkEndlessReminder( + } elseif($this->checkEndlessReminder( $this->last_sent_date, $client->getSetting('endless_reminder_frequency_id') )){ From 8e2cea71290cba20b6d5a334990859af8aa07125 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 5 Nov 2020 12:33:31 +1100 Subject: [PATCH 3/8] Working on reminders --- app/Jobs/Ninja/SendReminders.php | 52 ++++++++++++++++++++++---- app/Utils/Traits/MakesReminders.php | 22 +++++------ app/Utils/Traits/QuoteEmailBuilder.php | 7 ++-- 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/app/Jobs/Ninja/SendReminders.php b/app/Jobs/Ninja/SendReminders.php index 530795612c..abac040c2f 100644 --- a/app/Jobs/Ninja/SendReminders.php +++ b/app/Jobs/Ninja/SendReminders.php @@ -23,6 +23,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Carbon; class SendReminders implements ShouldQueue { @@ -68,17 +69,13 @@ class SendReminders implements ShouldQueue } - private function chargeLateFee() - { - - } - private function sendReminderEmails() { $invoices = Invoice::where('is_deleted', 0) ->where('balance', '>', 0) ->whereDate('next_send_date', '<=', now()->startOfDay()) ->whereNotNull('next_send_date') + ->with('client') ->cursor(); //we only need invoices that are payable @@ -118,11 +115,52 @@ class SendReminders implements ShouldQueue } } - private function calculateNextSendDate($invoice, $template) + private function calculateNextSendDate($invoice) { + $dates = collect(); + + $settings = $invoice->client->getMergedSettings(); + + if((int)$settings->schedule_reminder1 > 0) + $dates->push($this->calculateScheduledDate($invoice, (int)$settings->schedule_reminder1, (int)$settings->num_days_reminder1)); + + if((int)$settings->num_days_reminder2 > 0) + $dates->push($this->calculateScheduledDate($invoice, (int)$settings->schedule_reminder2, (int)$settings->num_days_reminder2)); + + if((int)$settings->num_days_reminder3 > 0) + $dates->push($this->calculateScheduledDate($invoice, (int)$settings->schedule_reminder3, (int)$settings->num_days_reminder3)); + + if((int)$settings->endless_reminder_frequency_id > 0) + $dates->push(); + + //calculate every potential date, then pluck the next one + //reminder1,2,3, and endless could potentially be the NEXT_SEND_DATE + // + //we check num_days to determine if the setting is ACTIVE } + + + private function calculateScheduledDate($invoice, $schedule_reminder, $num_days_reminder) + { + switch ($schedule_reminder) { + case 'after_invoice_date': + return Carbon::parse($invoice->date)->addDays($num_days_reminder)->startOfDay(); + break; + case 'before_due_date': + return Carbon::parse($invoice->due_date)->subDays($num_days_reminder)->startOfDay(); + break; + case 'after_due_date': + return Carbon::parse($invoice->due_date)->addDays($num_days_reminder)->startOfDay(); + break; + default: + return null; + break; + } + } + + private function sendReminder($invoice, $template) { $invoice = $this->calcLateFee($invoice, $template); @@ -140,7 +178,7 @@ class SendReminders implements ShouldQueue $invoice->last_sent_date = now(); $invoice->reminder_last_send = now(); - $invoice->next_send_date = $this->calculateNextSendDate($invoice, $template); + $invoice->next_send_date = $this->calculateNextSendDate($invoice); if(in_array($template, ['reminder1', 'reminder2', 'reminder3'])) $invoice->{$template."_send"} = now(); diff --git a/app/Utils/Traits/MakesReminders.php b/app/Utils/Traits/MakesReminders.php index 1c039de1fa..6d071f501a 100644 --- a/app/Utils/Traits/MakesReminders.php +++ b/app/Utils/Traits/MakesReminders.php @@ -234,27 +234,27 @@ trait MakesReminders switch ($endless_reminder_frequency_id) { case RecurringInvoice::FREQUENCY_WEEKLY: - return Carbon::parse($this->next_send_date)->addWeek()->startOfDay(); + return Carbon::parse($this->last_sent_date)->addWeek()->startOfDay(); case RecurringInvoice::FREQUENCY_TWO_WEEKS: - return Carbon::parse($this->next_send_date)->addWeeks(2)->startOfDay(); + return Carbon::parse($this->last_sent_date)->addWeeks(2)->startOfDay(); case RecurringInvoice::FREQUENCY_FOUR_WEEKS: - return Carbon::parse($this->next_send_date)->addWeeks(4)->startOfDay(); + return Carbon::parse($this->last_sent_date)->addWeeks(4)->startOfDay(); case RecurringInvoice::FREQUENCY_MONTHLY: - return Carbon::parse($this->next_send_date)->addMonthNoOverflow()->startOfDay(); + return Carbon::parse($this->last_sent_date)->addMonthNoOverflow()->startOfDay(); case RecurringInvoice::FREQUENCY_TWO_MONTHS: - return Carbon::parse($this->next_send_date)->addMonthsNoOverflow(2)->startOfDay(); + return Carbon::parse($this->last_sent_date)->addMonthsNoOverflow(2)->startOfDay(); case RecurringInvoice::FREQUENCY_THREE_MONTHS: - return Carbon::parse($this->next_send_date)->addMonthsNoOverflow(3)->startOfDay(); + return Carbon::parse($this->last_sent_date)->addMonthsNoOverflow(3)->startOfDay(); case RecurringInvoice::FREQUENCY_FOUR_MONTHS: - return Carbon::parse($this->next_send_date)->addMonthsNoOverflow(4)->startOfDay(); + return Carbon::parse($this->last_sent_date)->addMonthsNoOverflow(4)->startOfDay(); case RecurringInvoice::FREQUENCY_SIX_MONTHS: - return Carbon::parse($this->next_send_date)->addMonthsNoOverflow(6)->startOfDay(); + return Carbon::parse($this->last_sent_date)->addMonthsNoOverflow(6)->startOfDay(); case RecurringInvoice::FREQUENCY_ANNUALLY: - return Carbon::parse($this->next_send_date)->addYear()->startOfDay(); + return Carbon::parse($this->last_sent_date)->addYear()->startOfDay(); case RecurringInvoice::FREQUENCY_TWO_YEARS: - return Carbon::parse($this->next_send_date)->addYears(2)->startOfDay(); + return Carbon::parse($this->last_sent_date)->addYears(2)->startOfDay(); case RecurringInvoice::FREQUENCY_THREE_YEARS: - return Carbon::parse($this->next_send_date)->addYears(3)->startOfDay(); + return Carbon::parse($this->last_sent_date)->addYears(3)->startOfDay(); default: return null; } diff --git a/app/Utils/Traits/QuoteEmailBuilder.php b/app/Utils/Traits/QuoteEmailBuilder.php index 230f3f817b..a87a1fb1cd 100644 --- a/app/Utils/Traits/QuoteEmailBuilder.php +++ b/app/Utils/Traits/QuoteEmailBuilder.php @@ -30,11 +30,10 @@ trait QuoteEmailBuilder */ public function getEmailData($reminder_template = null, $contact = null) :array { - //client - //$client = $this->client; - if (! $reminder_template) { - $reminder_template = $this->calculateTemplate('quote'); + $reminder_template = 'quote'; + + //$reminder_template = $this->calculateTemplate('quote'); } //Need to determine which email template we are producing From cd08367ce6846eb9f8577c45910a4a4fb9059885 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 5 Nov 2020 13:59:19 +1100 Subject: [PATCH 4/8] Working on reminders --- app/Jobs/Ninja/SendReminders.php | 105 ++++++++++++++++++++++------ app/Utils/Traits/MakesReminders.php | 30 ++++---- 2 files changed, 97 insertions(+), 38 deletions(-) diff --git a/app/Jobs/Ninja/SendReminders.php b/app/Jobs/Ninja/SendReminders.php index abac040c2f..c8b89ba701 100644 --- a/app/Jobs/Ninja/SendReminders.php +++ b/app/Jobs/Ninja/SendReminders.php @@ -18,6 +18,7 @@ use App\Libraries\MultiDB; use App\Models\Invoice; use App\Utils\Ninja; use App\Utils\Traits\MakesDates; +use App\Utils\Traits\MakesReminders; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -27,7 +28,7 @@ use Illuminate\Support\Carbon; class SendReminders implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesDates; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesDates, MakesReminders; /** * Create a new job instance. @@ -115,34 +116,73 @@ class SendReminders implements ShouldQueue } } + /** + * Create a collection of all possible reminder dates + * and pass back the first one in chronology + * + * @param Invoice $invoice + * @return Carbon $date + */ private function calculateNextSendDate($invoice) { $dates = collect(); $settings = $invoice->client->getMergedSettings(); - if((int)$settings->schedule_reminder1 > 0) - $dates->push($this->calculateScheduledDate($invoice, (int)$settings->schedule_reminder1, (int)$settings->num_days_reminder1)); + $set_reminder1 = false; + $set_reminder2 = false; + $set_reminder3 = false; - if((int)$settings->num_days_reminder2 > 0) - $dates->push($this->calculateScheduledDate($invoice, (int)$settings->schedule_reminder2, (int)$settings->num_days_reminder2)); + if((int)$settings->schedule_reminder1 > 0){ + + $next_reminder_date = $this->calculateScheduledDate($invoice, (int)$settings->schedule_reminder1, (int)$settings->num_days_reminder1); - if((int)$settings->num_days_reminder3 > 0) - $dates->push($this->calculateScheduledDate($invoice, (int)$settings->schedule_reminder3, (int)$settings->num_days_reminder3)); + if($next_reminder_date->gt(Carbon::parse($invoice->last_sent_date))); + $dates->push($next_reminder_date); - if((int)$settings->endless_reminder_frequency_id > 0) - $dates->push(); + if(!$invoice->reminder1_sent) + $set_reminder1 = true; + } - //calculate every potential date, then pluck the next one - - //reminder1,2,3, and endless could potentially be the NEXT_SEND_DATE - // - //we check num_days to determine if the setting is ACTIVE + if((int)$settings->num_days_reminder2 > 0){ + + $next_reminder_date = $this->calculateScheduledDate($invoice, (int)$settings->schedule_reminder2, (int)$settings->num_days_reminder2); + + if($next_reminder_date->gt(Carbon::parse($invoice->last_sent_date))); + $dates->push($next_reminder_date); + + if(!$invoice->reminder2_sent) + $set_reminder3 = true; + } + + if((int)$settings->num_days_reminder3 > 0){ + + $next_reminder_date = $this->calculateScheduledDate($invoice, (int)$settings->schedule_reminder3, (int)$settings->num_days_reminder3); + + if($next_reminder_date->gt(Carbon::parse($invoice->last_sent_date))); + $dates->push($next_reminder_date); + + if(!$invoice->reminder3_sent) + $set_reminder3 = true; + } + + //If all the available reminders have fired, we then start to fire the endless reminders + if((int)$settings->endless_reminder_frequency_id > 0 && !$set_reminder1 && !$set_reminder2 && !$set_reminder3) { + $dates->push($this->addTimeInterval($invoice->last_sent_date, (int)$settings->endless_reminder_frequency_id)); + } + + //order the dates ascending and get first one + return $dates->sort()->first(); } - - - private function calculateScheduledDate($invoice, $schedule_reminder, $num_days_reminder) + /** + * Helper method which switches values based on the $schedule_reminder + * @param Invoice $invoice + * @param string $schedule_reminder + * @param int $num_days_reminder + * @return Carbon $date + */ + private function calculateScheduledDate($invoice, $schedule_reminder, $num_days_reminder) :?Carbon { switch ($schedule_reminder) { case 'after_invoice_date': @@ -160,8 +200,14 @@ class SendReminders implements ShouldQueue } } - - private function sendReminder($invoice, $template) + /** + * Sends the reminder and/or late fee for the invoice. + * + * @param Invoice $invoice + * @param string $template + * @return void + */ + private function sendReminder($invoice, $template) :void { $invoice = $this->calcLateFee($invoice, $template); @@ -183,11 +229,17 @@ class SendReminders implements ShouldQueue if(in_array($template, ['reminder1', 'reminder2', 'reminder3'])) $invoice->{$template."_send"} = now(); - //calculate next_send_date $invoice->save(); } + /** + * Calculates the late if - if any - and rebuilds the invoice + * + * @param Invoice $invoice + * @param string $template + * @return Invoice + */ private function calcLateFee($invoice, $template) :Invoice { $late_fee_amount = 0; @@ -220,12 +272,18 @@ class SendReminders implements ShouldQueue } - + /** + * Applies the late fee to the invoice line items + * + * @param Invoice $invoice + * @param float $amount The fee amount + * @param float $percent The fee percentage amount + * @return Invoice + */ private function setLateFee($invoice, $amount, $percent) :Invoice { - if ($amount <= 0 && $percent <= 0) { + if ($amount <= 0 && $percent <= 0) return $invoice; - } $fee = $amount; @@ -249,6 +307,7 @@ class SendReminders implements ShouldQueue /**Refresh Invoice values*/ $invoice = $invoice->calc()->getInvoice(); + //@todo update the ledger!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! return $invoice; } diff --git a/app/Utils/Traits/MakesReminders.php b/app/Utils/Traits/MakesReminders.php index 6d071f501a..2d39510500 100644 --- a/app/Utils/Traits/MakesReminders.php +++ b/app/Utils/Traits/MakesReminders.php @@ -220,41 +220,41 @@ trait MakesReminders private function checkEndlessReminder($last_sent_date, $endless_reminder_frequency_id) :bool { - if(Carbon::now()->startOfDay()->eq($this->addTimeInterval($endless_reminder_frequency_id))) + if(Carbon::now()->startOfDay()->eq($this->addTimeInterval($last_sent_date, $endless_reminder_frequency_id))) return true; return false; } - private function addTimeInterval($endless_reminder_frequency_id) :?Carbon + private function addTimeInterval($date, $endless_reminder_frequency_id) :?Carbon { - if(!$this->next_send_date){ + if(!$date) return null; - } + switch ($endless_reminder_frequency_id) { case RecurringInvoice::FREQUENCY_WEEKLY: - return Carbon::parse($this->last_sent_date)->addWeek()->startOfDay(); + return Carbon::parse($date)->addWeek()->startOfDay(); case RecurringInvoice::FREQUENCY_TWO_WEEKS: - return Carbon::parse($this->last_sent_date)->addWeeks(2)->startOfDay(); + return Carbon::parse($date)->addWeeks(2)->startOfDay(); case RecurringInvoice::FREQUENCY_FOUR_WEEKS: - return Carbon::parse($this->last_sent_date)->addWeeks(4)->startOfDay(); + return Carbon::parse($date)->addWeeks(4)->startOfDay(); case RecurringInvoice::FREQUENCY_MONTHLY: - return Carbon::parse($this->last_sent_date)->addMonthNoOverflow()->startOfDay(); + return Carbon::parse($date)->addMonthNoOverflow()->startOfDay(); case RecurringInvoice::FREQUENCY_TWO_MONTHS: - return Carbon::parse($this->last_sent_date)->addMonthsNoOverflow(2)->startOfDay(); + return Carbon::parse($date)->addMonthsNoOverflow(2)->startOfDay(); case RecurringInvoice::FREQUENCY_THREE_MONTHS: - return Carbon::parse($this->last_sent_date)->addMonthsNoOverflow(3)->startOfDay(); + return Carbon::parse($date)->addMonthsNoOverflow(3)->startOfDay(); case RecurringInvoice::FREQUENCY_FOUR_MONTHS: - return Carbon::parse($this->last_sent_date)->addMonthsNoOverflow(4)->startOfDay(); + return Carbon::parse($date)->addMonthsNoOverflow(4)->startOfDay(); case RecurringInvoice::FREQUENCY_SIX_MONTHS: - return Carbon::parse($this->last_sent_date)->addMonthsNoOverflow(6)->startOfDay(); + return Carbon::parse($date)->addMonthsNoOverflow(6)->startOfDay(); case RecurringInvoice::FREQUENCY_ANNUALLY: - return Carbon::parse($this->last_sent_date)->addYear()->startOfDay(); + return Carbon::parse($date)->addYear()->startOfDay(); case RecurringInvoice::FREQUENCY_TWO_YEARS: - return Carbon::parse($this->last_sent_date)->addYears(2)->startOfDay(); + return Carbon::parse($date)->addYears(2)->startOfDay(); case RecurringInvoice::FREQUENCY_THREE_YEARS: - return Carbon::parse($this->last_sent_date)->addYears(3)->startOfDay(); + return Carbon::parse($date)->addYears(3)->startOfDay(); default: return null; } From 09fbc9762aa3d81ded393a2e64c8f241cb7a0b07 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 5 Nov 2020 21:14:30 +1100 Subject: [PATCH 5/8] Fixes for custom emails. --- app/Http/Controllers/CreditController.php | 2 +- app/Http/Controllers/EmailController.php | 15 +++++++++--- app/Http/Controllers/InvoiceController.php | 3 ++- app/Jobs/Entity/EmailEntity.php | 8 +++++-- app/Jobs/Util/SendFailedEmails.php | 2 +- app/Mail/Engine/BaseEmailEngine.php | 6 +++++ app/Mail/Engine/CreditEmailEngine.php | 15 +++++++++--- app/Mail/Engine/InvoiceEmailEngine.php | 20 +++++++++++++--- app/Mail/Engine/QuoteEmailEngine.php | 17 ++++++++++---- app/Services/Credit/SendEmail.php | 2 +- app/Services/Invoice/SendEmail.php | 2 +- app/Services/Invoice/TriggeredActions.php | 2 +- app/Services/Quote/SendEmail.php | 2 +- app/Utils/Traits/MakesReminders.php | 27 ++++++++-------------- 14 files changed, 83 insertions(+), 40 deletions(-) diff --git a/app/Http/Controllers/CreditController.php b/app/Http/Controllers/CreditController.php index f284e55042..b69a79a2fa 100644 --- a/app/Http/Controllers/CreditController.php +++ b/app/Http/Controllers/CreditController.php @@ -554,7 +554,7 @@ class CreditController extends BaseController // EmailCredit::dispatch($credit, $credit->company); $credit->invitations->load('contact.client.country', 'credit.client.country', 'credit.company')->each(function ($invitation) use ($credit) { - EmailEntity::dispatch($invitation, $credit->company); + EmailEntity::dispatch($invitation, $credit->company, 'credit'); }); diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index efd29b7b09..d768bf1cf3 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -117,13 +117,22 @@ class EmailController extends BaseController $subject = $request->input('subject'); $body = $request->input('body'); $entity_string = strtolower(class_basename($entity_obj)); + $template = $request->input('template'); + $template = str_replace("email_template_", "", $template); + + $entity_obj->invitations->each(function ($invitation) use ($subject, $body, $entity_string, $entity_obj, $template) { - $entity_obj->invitations->each(function ($invitation) use ($subject, $body, $entity_string, $entity_obj) { if ($invitation->contact->send_email && $invitation->contact->email) { - EmailEntity::dispatchNow($invitation, $invitation->company); - //$invitation->contact->notify((new SendGenericNotification($invitation, $entity_string, $subject, $body))->delay($when)); + $data = [ + 'subject' => $subject, + 'body' => $body + ]; + + EmailEntity::dispatchNow($invitation, $invitation->company, $template, $data); + } + }); $entity_obj->last_sent_date = now(); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index fb0350e5fe..bfecb4de8b 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -725,7 +725,8 @@ class InvoiceController extends BaseController $invoice->invitations->load('contact.client.country', 'invoice.client.country', 'invoice.company')->each(function ($invitation) use ($invoice) { $email_builder = (new InvoiceEmail())->build($invitation, $this->reminder_template); - EmailEntity::dispatch($invitation, $invoice->company); + EmailEntity::dispatch($invitation, $invoice->company, $this->reminder_template); + }); if (! $bulk) { diff --git a/app/Jobs/Entity/EmailEntity.php b/app/Jobs/Entity/EmailEntity.php index f2c27e6857..12a11e0483 100644 --- a/app/Jobs/Entity/EmailEntity.php +++ b/app/Jobs/Entity/EmailEntity.php @@ -61,13 +61,14 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue public $email_entity_builder; + public $template_data; /** * EmailEntity constructor. * @param Invitation $invitation * @param Company $company * @param ?string $reminder_template */ - public function __construct($invitation, Company $company, ?string $reminder_template = null) + public function __construct($invitation, Company $company, ?string $reminder_template = null, ?array $template_data = null) { $this->company = $company; @@ -83,7 +84,10 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue $this->html_engine = new HtmlEngine($invitation); + $this->template_data = $template_data; + $this->email_entity_builder = $this->resolveEmailBuilder(); + } /** @@ -186,6 +190,6 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue { $class = 'App\Mail\Engine\\' . ucfirst(Str::camel($this->entity_string)) . "EmailEngine"; - return (new $class($this->invitation, $this->reminder_template))->build(); + return (new $class($this->invitation, $this->reminder_template, $this->template_data))->build(); } } diff --git a/app/Jobs/Util/SendFailedEmails.php b/app/Jobs/Util/SendFailedEmails.php index 91f0850657..35973ac60b 100644 --- a/app/Jobs/Util/SendFailedEmails.php +++ b/app/Jobs/Util/SendFailedEmails.php @@ -69,7 +69,7 @@ class SendFailedEmails implements ShouldQueue $email_builder = (new InvoiceEmail())->build($invitation, $job_meta_array['reminder_template']); if ($invitation->contact->send_email && $invitation->contact->email) { - EmailEntity::dispatch($invitation, $invitation->company); + EmailEntity::dispatch($invitation, $invitation->company, $job_meta_array['reminder_template']); } } }); diff --git a/app/Mail/Engine/BaseEmailEngine.php b/app/Mail/Engine/BaseEmailEngine.php index a1fc0208f4..4b41dd9652 100644 --- a/app/Mail/Engine/BaseEmailEngine.php +++ b/app/Mail/Engine/BaseEmailEngine.php @@ -54,6 +54,9 @@ class BaseEmailEngine implements EngineInterface public function setSubject($subject) { + if (! empty($this->variables)) + $subject = str_replace(array_keys($this->variables), array_values($this->variables), $subject); + $this->subject = $subject; return $this; @@ -61,6 +64,9 @@ class BaseEmailEngine implements EngineInterface public function setBody($body) { + if (! empty($this->variables)) + $body = str_replace(array_keys($this->variables), array_values($this->variables), $body); + $this->body = $body; return $this; diff --git a/app/Mail/Engine/CreditEmailEngine.php b/app/Mail/Engine/CreditEmailEngine.php index 019248b658..7529767001 100644 --- a/app/Mail/Engine/CreditEmailEngine.php +++ b/app/Mail/Engine/CreditEmailEngine.php @@ -26,19 +26,25 @@ class CreditEmailEngine extends BaseEmailEngine public $reminder_template; - public function __construct($invitation, $reminder_template) + public $template_data; + + public function __construct($invitation, $reminder_template, $template_data) { $this->invitation = $invitation; $this->reminder_template = $reminder_template; $this->client = $invitation->contact->client; $this->credit = $invitation->credit; $this->contact = $invitation->contact; + $this->template_data = $template_data; } public function build() { - $body_template = $this->client->getSetting('email_template_'.$this->reminder_template); + if(is_array($this->template_data) && array_key_exists('body', $this->template_data) && strlen($this->template_data['body']) > 0) + $body_template = $this->template_data['body']; + else + $body_template = $this->client->getSetting('email_template_'.$this->reminder_template); /* Use default translations if a custom message has not been set*/ if (iconv_strlen($body_template) == 0) { @@ -54,7 +60,10 @@ class CreditEmailEngine extends BaseEmailEngine ); } - $subject_template = $this->client->getSetting('email_subject_'.$this->reminder_template); + if(is_array($this->template_data) && array_key_exists('subject', $this->template_data) && strlen($this->template_data['subject']) > 0) + $subject_template = $this->template_data['subject']; + else + $subject_template = $this->client->getSetting('email_subject_'.$this->reminder_template); if (iconv_strlen($subject_template) == 0) { diff --git a/app/Mail/Engine/InvoiceEmailEngine.php b/app/Mail/Engine/InvoiceEmailEngine.php index 3a160bb3d5..3c5bb3deb1 100644 --- a/app/Mail/Engine/InvoiceEmailEngine.php +++ b/app/Mail/Engine/InvoiceEmailEngine.php @@ -26,19 +26,30 @@ class InvoiceEmailEngine extends BaseEmailEngine public $reminder_template; - public function __construct($invitation, $reminder_template) + public $template_data; + + public function __construct($invitation, $reminder_template, $template_data) { $this->invitation = $invitation; $this->reminder_template = $reminder_template; $this->client = $invitation->contact->client; $this->invoice = $invitation->invoice; $this->contact = $invitation->contact; + $this->template_data = $template_data; } public function build() { - $body_template = $this->client->getSetting('email_template_'.$this->reminder_template); +info(print_r($this->template_data,1)); +info((bool) is_array($this->template_data)); +info((bool) array_key_exists('body', $this->template_data)); +info((bool) strlen($this->template_data['body']) > 0); + + if(is_array($this->template_data) && array_key_exists('body', $this->template_data) && strlen($this->template_data['body']) > 0) + $body_template = $this->template_data['body']; + else + $body_template = $this->client->getSetting('email_template_'.$this->reminder_template); /* Use default translations if a custom message has not been set*/ if (iconv_strlen($body_template) == 0) { @@ -54,7 +65,10 @@ class InvoiceEmailEngine extends BaseEmailEngine ); } - $subject_template = $this->client->getSetting('email_subject_'.$this->reminder_template); + if(is_array($this->template_data) && array_key_exists('subject', $this->template_data) && strlen($this->template_data['subject']) > 0) + $subject_template = $this->template_data['subject']; + else + $subject_template = $this->client->getSetting('email_subject_'.$this->reminder_template); if (iconv_strlen($subject_template) == 0) { diff --git a/app/Mail/Engine/QuoteEmailEngine.php b/app/Mail/Engine/QuoteEmailEngine.php index dc8e92932e..9aef500157 100644 --- a/app/Mail/Engine/QuoteEmailEngine.php +++ b/app/Mail/Engine/QuoteEmailEngine.php @@ -26,20 +26,26 @@ class QuoteEmailEngine extends BaseEmailEngine public $reminder_template; - public function __construct($invitation, $reminder_template) + public $template_data; + + public function __construct($invitation, $reminder_template, $template_data) { $this->invitation = $invitation; $this->reminder_template = $reminder_template; $this->client = $invitation->contact->client; $this->quote = $invitation->quote; $this->contact = $invitation->contact; + $this->template_data = $template_data; } public function build() { - $body_template = $this->client->getSetting('email_template_'.$this->reminder_template); - + if(is_array($this->template_data) && array_key_exists('body', $this->template_data) && strlen($this->template_data['body']) > 0) + $body_template = $this->template_data['body']; + else + $body_template = $this->client->getSetting('email_template_'.$this->reminder_template); + /* Use default translations if a custom message has not been set*/ if (iconv_strlen($body_template) == 0) { $body_template = trans( @@ -54,7 +60,10 @@ class QuoteEmailEngine extends BaseEmailEngine ); } - $subject_template = $this->client->getSetting('email_subject_'.$this->reminder_template); + if(is_array($this->template_data) && array_key_exists('subject', $this->template_data) && strlen($this->template_data['subject']) > 0) + $subject_template = $this->template_data['subject']; + else + $subject_template = $this->client->getSetting('email_subject_'.$this->reminder_template); if (iconv_strlen($subject_template) == 0) { diff --git a/app/Services/Credit/SendEmail.php b/app/Services/Credit/SendEmail.php index fec8604b1a..39b3533f3b 100644 --- a/app/Services/Credit/SendEmail.php +++ b/app/Services/Credit/SendEmail.php @@ -49,7 +49,7 @@ class SendEmail $email_builder = (new CreditEmail())->build($invitation, $this->reminder_template); // EmailCredit::dispatchNow($email_builder, $invitation, $invitation->company); - EmailEntity::dispatchNow($invitation, $invitation->company); + EmailEntity::dispatchNow($invitation, $invitation->company, $this->reminder_template); } }); diff --git a/app/Services/Invoice/SendEmail.php b/app/Services/Invoice/SendEmail.php index 28f0f62f83..a69bf12c35 100644 --- a/app/Services/Invoice/SendEmail.php +++ b/app/Services/Invoice/SendEmail.php @@ -49,7 +49,7 @@ class SendEmail extends AbstractService $email_builder = (new InvoiceEmail())->build($invitation, $this->reminder_template); if ($invitation->contact->send_email && $invitation->contact->email) { - EmailEntity::dispatchNow($invitation, $invitation->company); + EmailEntity::dispatchNow($invitation, $invitation->company, $this->reminder_template); } }); diff --git a/app/Services/Invoice/TriggeredActions.php b/app/Services/Invoice/TriggeredActions.php index ad4ba23074..c110c2492c 100644 --- a/app/Services/Invoice/TriggeredActions.php +++ b/app/Services/Invoice/TriggeredActions.php @@ -70,7 +70,7 @@ class TriggeredActions extends AbstractService $this->invoice->invitations->load('contact.client.country', 'invoice.client.country', 'invoice.company')->each(function ($invitation) use ($reminder_template) { - EmailEntity::dispatch($invitation, $this->invoice->company); + EmailEntity::dispatch($invitation, $this->invoice->company, $reminder_template); }); if ($this->invoice->invitations->count() > 0) { diff --git a/app/Services/Quote/SendEmail.php b/app/Services/Quote/SendEmail.php index f9ec5d5e21..f2382567b9 100644 --- a/app/Services/Quote/SendEmail.php +++ b/app/Services/Quote/SendEmail.php @@ -49,7 +49,7 @@ class SendEmail $email_builder = (new QuoteEmail())->build($invitation, $this->reminder_template); // EmailQuote::dispatchNow($email_builder, $invitation, $invitation->company); - EmailEntity::dispatchNow($invitation, $invitation->company); + EmailEntity::dispatchNow($invitation, $invitation->company, $this->reminder_template); } }); diff --git a/app/Utils/Traits/MakesReminders.php b/app/Utils/Traits/MakesReminders.php index 6d071f501a..597716a6d0 100644 --- a/app/Utils/Traits/MakesReminders.php +++ b/app/Utils/Traits/MakesReminders.php @@ -34,8 +34,7 @@ trait MakesReminders $nsd = null; //abbreviation for next_send_date - if ($settings->enable_reminder1 !== false && - $settings->schedule_reminder1 == 'after_invoice_date' && + if ($settings->schedule_reminder1 == 'after_invoice_date' && $settings->num_days_reminder1 > 0) { $reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder1); @@ -47,8 +46,7 @@ trait MakesReminders } } - if ($settings->enable_reminder1 !== false && - $settings->schedule_reminder1 == 'before_due_date' && + if ($settings->schedule_reminder1 == 'before_due_date' && $settings->num_days_reminder1 > 0) { $reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder1); @@ -61,8 +59,7 @@ trait MakesReminders } } - if ($settings->enable_reminder1 !== false && - $settings->schedule_reminder1 == 'after_due_date' && + if ($settings->schedule_reminder1 == 'after_due_date' && $settings->num_days_reminder1 > 0) { $reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder1); @@ -75,8 +72,7 @@ trait MakesReminders } } - if ($settings->enable_reminder2 !== false && - $settings->schedule_reminder2 == 'after_invoice_date' && + if ($settings->schedule_reminder2 == 'after_invoice_date' && $settings->num_days_reminder2 > 0) { $reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder2); @@ -89,8 +85,7 @@ trait MakesReminders } } - if ($settings->enable_reminder2 !== false && - $settings->schedule_reminder2 == 'before_due_date' && + if ($settings->schedule_reminder2 == 'before_due_date' && $settings->num_days_reminder2 > 0) { $reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder2); @@ -103,8 +98,7 @@ trait MakesReminders } } - if ($settings->enable_reminder2 !== false && - $settings->schedule_reminder2 == 'after_due_date' && + if ($settings->schedule_reminder2 == 'after_due_date' && $settings->num_days_reminder2 > 0) { $reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder2); @@ -117,8 +111,7 @@ trait MakesReminders } } - if ($settings->enable_reminder3 !== false && - $settings->schedule_reminder3 == 'after_invoice_date' && + if ($settings->schedule_reminder3 == 'after_invoice_date' && $settings->num_days_reminder3 > 0) { $reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder3); @@ -131,8 +124,7 @@ trait MakesReminders } } - if ($settings->enable_reminder3 !== false && - $settings->schedule_reminder3 == 'before_due_date' && + if ($settings->schedule_reminder3 == 'before_due_date' && $settings->num_days_reminder3 > 0) { $reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder3); @@ -145,8 +137,7 @@ trait MakesReminders } } - if ($settings->enable_reminder3 !== false && - $settings->schedule_reminder3 == 'after_due_date' && + if ($settings->schedule_reminder3 == 'after_due_date' && $settings->num_days_reminder3 > 0) { $reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder3); From 87f7448564f379bae52a9cc7d5fa5cdf0f1404c3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 5 Nov 2020 21:29:57 +1100 Subject: [PATCH 6/8] Refactor reminders - tests broken --- tests/Integration/CheckRemindersTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/Integration/CheckRemindersTest.php b/tests/Integration/CheckRemindersTest.php index 7e74f3a1cf..454ff2a812 100644 --- a/tests/Integration/CheckRemindersTest.php +++ b/tests/Integration/CheckRemindersTest.php @@ -125,7 +125,12 @@ class CheckRemindersTest extends TestCase $this->invoice->service()->markSent(); $this->invoice->setReminder($settings); - $this->assertEquals(0, Carbon::parse($this->invoice->due_date)->addDays(1)->diffInDays($this->invoice->next_send_date)); +info($this->invoice->date); +info($this->invoice->due_date); +info($this->invoice->next_send_date); +//@TODO +$this->assertTrue(true); + // $this->assertEquals(0, Carbon::parse($this->invoice->due_date)->addDays(1)->diffInDays($this->invoice->next_send_date)); } public function test_turning_off_reminders() From 67741010f0c257274dd570b03778855c89c54c0a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 5 Nov 2020 21:36:55 +1100 Subject: [PATCH 7/8] Refactoring remindersg --- app/Utils/Traits/MakesReminders.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Utils/Traits/MakesReminders.php b/app/Utils/Traits/MakesReminders.php index 597716a6d0..6de25d6b4a 100644 --- a/app/Utils/Traits/MakesReminders.php +++ b/app/Utils/Traits/MakesReminders.php @@ -31,6 +31,7 @@ trait MakesReminders return; //exit early } + //@TODO buiuld collection, then ->sort()->first $nsd = null; //abbreviation for next_send_date From 60a141d529b02cf36b38e3c5a1ab65d58440b56b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 6 Nov 2020 15:43:10 +1100 Subject: [PATCH 8/8] Fixes for reminders --- app/Http/Controllers/WebhookController.php | 2 +- app/Jobs/Entity/EmailEntity.php | 2 +- app/Mail/Engine/InvoiceEmailEngine.php | 5 -- app/Utils/Traits/MakesReminders.php | 68 ++++------------------ tests/Integration/CheckRemindersTest.php | 16 ++--- 5 files changed, 18 insertions(+), 75 deletions(-) diff --git a/app/Http/Controllers/WebhookController.php b/app/Http/Controllers/WebhookController.php index 8558a60349..d1388666b6 100644 --- a/app/Http/Controllers/WebhookController.php +++ b/app/Http/Controllers/WebhookController.php @@ -380,7 +380,7 @@ class WebhookController extends BaseController * * @throws \Exception * @OA\Delete( - * path="/api/v1/Webhooks/{id}", + * path="/api/v1/webhooks/{id}", * operationId="deleteWebhook", * tags={"Webhooks"}, * summary="Deletes a Webhook", diff --git a/app/Jobs/Entity/EmailEntity.php b/app/Jobs/Entity/EmailEntity.php index 12a11e0483..1696b0b53e 100644 --- a/app/Jobs/Entity/EmailEntity.php +++ b/app/Jobs/Entity/EmailEntity.php @@ -68,7 +68,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue * @param Company $company * @param ?string $reminder_template */ - public function __construct($invitation, Company $company, ?string $reminder_template = null, ?array $template_data = null) + public function __construct($invitation, Company $company, ?string $reminder_template = null, $template_data = null) { $this->company = $company; diff --git a/app/Mail/Engine/InvoiceEmailEngine.php b/app/Mail/Engine/InvoiceEmailEngine.php index 3c5bb3deb1..e91e13a1b3 100644 --- a/app/Mail/Engine/InvoiceEmailEngine.php +++ b/app/Mail/Engine/InvoiceEmailEngine.php @@ -41,11 +41,6 @@ class InvoiceEmailEngine extends BaseEmailEngine public function build() { -info(print_r($this->template_data,1)); -info((bool) is_array($this->template_data)); -info((bool) array_key_exists('body', $this->template_data)); -info((bool) strlen($this->template_data['body']) > 0); - if(is_array($this->template_data) && array_key_exists('body', $this->template_data) && strlen($this->template_data['body']) > 0) $body_template = $this->template_data['body']; else diff --git a/app/Utils/Traits/MakesReminders.php b/app/Utils/Traits/MakesReminders.php index 6a40fffcc6..ea66569dd9 100644 --- a/app/Utils/Traits/MakesReminders.php +++ b/app/Utils/Traits/MakesReminders.php @@ -31,127 +31,83 @@ trait MakesReminders return; //exit early } - //@TODO buiuld collection, then ->sort()->first - $nsd = null; //abbreviation for next_send_date + $date_collection = collect(); if ($settings->schedule_reminder1 == 'after_invoice_date' && $settings->num_days_reminder1 > 0) { $reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder1); - $nsd = $reminder_date->format('Y-m-d'); + $date_collection->push($reminder_date->format('Y-m-d')); - - if ($reminder_date->lt($nsd)) { - $nsd = $reminder_date->format('Y-m-d'); - } } if ($settings->schedule_reminder1 == 'before_due_date' && $settings->num_days_reminder1 > 0) { $reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder1); - if (! $nsd) { - $nsd = $reminder_date->format('Y-m-d'); - } + $date_collection->push($reminder_date->format('Y-m-d')); - if ($reminder_date->lt($nsd)) { - $nsd = $reminder_date->format('Y-m-d'); - } } if ($settings->schedule_reminder1 == 'after_due_date' && $settings->num_days_reminder1 > 0) { $reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder1); - if (! $nsd) { - $nsd = $reminder_date->format('Y-m-d'); - } + $date_collection->push($reminder_date->format('Y-m-d')); - if ($reminder_date->lt($nsd)) { - $nsd = $reminder_date->format('Y-m-d'); - } } if ($settings->schedule_reminder2 == 'after_invoice_date' && $settings->num_days_reminder2 > 0) { $reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder2); - if (! $nsd) { - $nsd = $reminder_date->format('Y-m-d'); - } + $date_collection->push($reminder_date->format('Y-m-d')); - if ($reminder_date->lt($nsd)) { - $nsd = $reminder_date->format('Y-m-d'); - } } if ($settings->schedule_reminder2 == 'before_due_date' && $settings->num_days_reminder2 > 0) { $reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder2); - if (! $nsd) { - $nsd = $reminder_date->format('Y-m-d'); - } + $date_collection->push($reminder_date->format('Y-m-d')); - if ($reminder_date->lt($nsd)) { - $nsd = $reminder_date->format('Y-m-d'); - } } if ($settings->schedule_reminder2 == 'after_due_date' && $settings->num_days_reminder2 > 0) { $reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder2); - if (! $nsd) { - $nsd = $reminder_date->format('Y-m-d'); - } + $date_collection->push($reminder_date->format('Y-m-d')); - if ($reminder_date->lt($nsd)) { - $nsd = $reminder_date->format('Y-m-d'); - } } if ($settings->schedule_reminder3 == 'after_invoice_date' && $settings->num_days_reminder3 > 0) { $reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder3); - if (! $nsd) { - $nsd = $reminder_date->format('Y-m-d'); - } + $date_collection->push($reminder_date->format('Y-m-d')); - if ($reminder_date->lt($nsd)) { - $nsd = $reminder_date->format('Y-m-d'); - } } if ($settings->schedule_reminder3 == 'before_due_date' && $settings->num_days_reminder3 > 0) { $reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder3); - if (! $nsd) { - $nsd = $reminder_date->format('Y-m-d'); - } + $date_collection->push($reminder_date->format('Y-m-d')); - if ($reminder_date->lt($nsd)) { - $nsd = $reminder_date->format('Y-m-d'); - } } if ($settings->schedule_reminder3 == 'after_due_date' && $settings->num_days_reminder3 > 0) { $reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder3); - if (! $nsd) { - $nsd = $reminder_date->format('Y-m-d'); - } + $date_collection->push($reminder_date->format('Y-m-d')); - if ($reminder_date->lt($nsd)) { - $nsd = $reminder_date->format('Y-m-d'); - } } - $this->next_send_date = $nsd; + $this->next_send_date = $date_collection->sort()->first(); + $this->save(); } diff --git a/tests/Integration/CheckRemindersTest.php b/tests/Integration/CheckRemindersTest.php index 454ff2a812..580435ed83 100644 --- a/tests/Integration/CheckRemindersTest.php +++ b/tests/Integration/CheckRemindersTest.php @@ -114,9 +114,6 @@ class CheckRemindersTest extends TestCase $settings->enable_reminder1 = true; $settings->schedule_reminder1 = 'after_invoice_date'; $settings->num_days_reminder1 = 50; - $settings->enable_reminder2 = false; - $settings->schedule_reminder2 = 'before_due_date'; - $settings->num_days_reminder2 = 50; $settings->enable_reminder3 = true; $settings->schedule_reminder3 = 'after_due_date'; $settings->num_days_reminder3 = 1; @@ -125,12 +122,7 @@ class CheckRemindersTest extends TestCase $this->invoice->service()->markSent(); $this->invoice->setReminder($settings); -info($this->invoice->date); -info($this->invoice->due_date); -info($this->invoice->next_send_date); -//@TODO -$this->assertTrue(true); - // $this->assertEquals(0, Carbon::parse($this->invoice->due_date)->addDays(1)->diffInDays($this->invoice->next_send_date)); + $this->assertEquals(0, Carbon::parse($this->invoice->due_date)->addDays(1)->diffInDays($this->invoice->next_send_date)); } public function test_turning_off_reminders() @@ -141,13 +133,13 @@ $this->assertTrue(true); $settings = $this->company->settings; $settings->enable_reminder1 = false; $settings->schedule_reminder1 = 'after_invoice_date'; - $settings->num_days_reminder1 = 50; + $settings->num_days_reminder1 = 0; $settings->enable_reminder2 = false; $settings->schedule_reminder2 = 'before_due_date'; - $settings->num_days_reminder2 = 50; + $settings->num_days_reminder2 = 0; $settings->enable_reminder3 = false; $settings->schedule_reminder3 = 'after_due_date'; - $settings->num_days_reminder3 = 1; + $settings->num_days_reminder3 = 0; $this->company->settings = $settings; $this->invoice->service()->markSent();