From 17758a24ccab62a326bc6a7118f59956a631268a Mon Sep 17 00:00:00 2001 From: denisdulici Date: Fri, 27 Apr 2018 22:16:35 +0300 Subject: [PATCH] update dates of new records #315 --- app/Console/Commands/RecurringCheck.php | 29 ++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/app/Console/Commands/RecurringCheck.php b/app/Console/Commands/RecurringCheck.php index bc10544f0..5e7b893d3 100644 --- a/app/Console/Commands/RecurringCheck.php +++ b/app/Console/Commands/RecurringCheck.php @@ -53,7 +53,7 @@ class RecurringCheck extends Command */ public function handle() { - $today = Date::today(); + $this->today = Date::today(); // Get all companies $companies = Company::all(); @@ -76,7 +76,7 @@ class RecurringCheck extends Command $current = Date::parse($schedule->current()->getStart()); // Check if should recur today - if ($today->ne($current)) { + if ($this->today->ne($current)) { continue; } @@ -97,7 +97,12 @@ class RecurringCheck extends Command break; case 'Payment': case 'Revenue': - $model->duplicate(); + // Create new record + $clone = $model->duplicate(); + + // Update date + $clone->paid_at = $this->today->format('Y-m-d'); + $clone->save(); break; } } @@ -109,8 +114,17 @@ class RecurringCheck extends Command protected function recurInvoice($company, $model) { + // Create new record $clone = $model->duplicate(); + // Days between invoiced and due date + $diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->invoiced_at)); + + // Update dates + $clone->invoiced_at = $this->today->format('Y-m-d'); + $clone->due_at = $this->today->addDays($diff_days)->format('Y-m-d'); + $clone->save(); + // Add invoice history InvoiceHistory::create([ 'company_id' => session('company_id'), @@ -140,8 +154,17 @@ class RecurringCheck extends Command protected function recurBill($company, $model) { + // Create new record $clone = $model->duplicate(); + // Days between invoiced and due date + $diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->invoiced_at)); + + // Update dates + $clone->billed_at = $this->today->format('Y-m-d'); + $clone->due_at = $this->today->addDays($diff_days)->format('Y-m-d'); + $clone->save(); + // Add bill history BillHistory::create([ 'company_id' => session('company_id'),