From 8026c205cc33c50798c2d4d0d0a6a988f2b6a44d Mon Sep 17 00:00:00 2001 From: harshjagad20 Date: Mon, 6 Dec 2021 16:13:20 +0530 Subject: [PATCH] solve recurring invoice starts_at condition --- app/Models/RecurringInvoice.php | 48 ++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index 5e936804..e6e04cb9 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -20,6 +20,10 @@ class RecurringInvoice extends Model 'id', ]; + protected $dates = [ + 'starts_at' + ]; + public const NONE = 'NONE'; public const COUNT = 'COUNT'; public const DATE = 'DATE'; @@ -259,34 +263,36 @@ class RecurringInvoice extends Model public function generateInvoice() { - if (Carbon::now()->format('Y-m-d H:i:s') < $this->starts_at) { - if ($this->limit_by == 'DATE') { - $startDate = Carbon::today()->format('Y-m-d'); + if (Carbon::now()->lessThan($this->starts_at)) { + return; + } - $endDate = $this->limit_date; + if ($this->limit_by == 'DATE') { + $startDate = Carbon::today()->format('Y-m-d'); - if ($endDate >= $startDate) { - $this->createInvoice(); + $endDate = $this->limit_date; - $this->updateNextInvoiceDate(); - } else { - $this->markStatusAsCompleted(); - } - } elseif ($this->limit_by == 'COUNT') { - $invoiceCount = Invoice::where('recurring_invoice_id', $this->id)->count(); - - if ($invoiceCount < $this->limit_count) { - $this->createInvoice(); - - $this->updateNextInvoiceDate(); - } else { - $this->markStatusAsCompleted(); - } - } else { + if ($endDate >= $startDate) { $this->createInvoice(); $this->updateNextInvoiceDate(); + } else { + $this->markStatusAsCompleted(); } + } elseif ($this->limit_by == 'COUNT') { + $invoiceCount = Invoice::where('recurring_invoice_id', $this->id)->count(); + + if ($invoiceCount < $this->limit_count) { + $this->createInvoice(); + + $this->updateNextInvoiceDate(); + } else { + $this->markStatusAsCompleted(); + } + } else { + $this->createInvoice(); + + $this->updateNextInvoiceDate(); } }