solve recurring invoice starts_at condition

This commit is contained in:
harshjagad20
2021-12-06 16:13:20 +05:30
parent b543f6d389
commit 8026c205cc

View File

@ -20,6 +20,10 @@ class RecurringInvoice extends Model
'id', 'id',
]; ];
protected $dates = [
'starts_at'
];
public const NONE = 'NONE'; public const NONE = 'NONE';
public const COUNT = 'COUNT'; public const COUNT = 'COUNT';
public const DATE = 'DATE'; public const DATE = 'DATE';
@ -259,34 +263,36 @@ class RecurringInvoice extends Model
public function generateInvoice() public function generateInvoice()
{ {
if (Carbon::now()->format('Y-m-d H:i:s') < $this->starts_at) { if (Carbon::now()->lessThan($this->starts_at)) {
if ($this->limit_by == 'DATE') { return;
$startDate = Carbon::today()->format('Y-m-d'); }
$endDate = $this->limit_date; if ($this->limit_by == 'DATE') {
$startDate = Carbon::today()->format('Y-m-d');
if ($endDate >= $startDate) { $endDate = $this->limit_date;
$this->createInvoice();
$this->updateNextInvoiceDate(); if ($endDate >= $startDate) {
} 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->createInvoice();
$this->updateNextInvoiceDate(); $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();
} }
} }