From 2b877d3414fabfa34699241e8b05da539c385f01 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 12 Sep 2020 20:33:12 +1000 Subject: [PATCH] Working on recurringDates Array --- app/Models/RecurringInvoice.php | 44 ++++++++++++++++++- ...40557_add_is_public_to_documents_table.php | 2 + 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index 88b6842ffa..394725f8bb 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -219,7 +219,11 @@ class RecurringInvoice extends BaseModel { if ($this->remaining_cycles == 0) { return 0; - } else { + } + else if($this->remaining_cycles == -1) { + return -1; + } + else { return $this->remaining_cycles - 1; } } @@ -298,5 +302,43 @@ class RecurringInvoice extends BaseModel public function recurringDates() { //todo send back a list of the next send dates and due dates + + /* Return early if nothing to send back! */ + if( $this->status_id == self::STATUS_COMPLETED || + $this->status_id == self::STATUS_DRAFT || + $this->status_id == self::STATUS_CANCELLED || + $this->remaining_cycles == 0) { + + return []; + } + + /* Endless - lets send 10 back*/ + $iterations = $this->remaining_cycles; + + if($this->remaining_cycles == -1) + $iterations = 10; + + $next_send_date = $this->next_send_date; + $due_date_days = $this->due_date_days; + + if(!$due_date_days) + $due_date_days = 1; + + $data = []; + + for($x=0; $x<$iterations; $x++) + { + + $data[] = [ + 'next_send_date' => $next_send_date->format('Y-m-d'), + 'due_date' => $next_send_date->addDays($due_date_days)->format('Y-m-d') + ]; + + } + + + return $data; } + + } diff --git a/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php b/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php index c60cdb56f2..ffb0bb8241 100644 --- a/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php +++ b/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php @@ -69,6 +69,8 @@ class AddIsPublicToDocumentsTable extends Migration Schema::table('recurring_invoices', function (Blueprint $table) { $table->integer('remaining_cycles')->nullable()->change(); + $table->dropColumn('start_date'); + $table->integer('due_date_days')->nullable(); }); }