diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index 128b704fd..063cbe46d 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -353,7 +353,7 @@ class RecurringInvoice extends BaseModel */ public function recurringDates() { - info($this->next_send_date); + /* Return early if nothing to send back! */ if( $this->status_id == self::STATUS_COMPLETED || $this->status_id == self::STATUS_DRAFT || @@ -404,4 +404,40 @@ class RecurringInvoice extends BaseModel } + private function calculateDueDate($terms, $date) + { + + switch ($terms) { + case 'client_terms': + return $this->calculateDateFromTerms($date); + break; + case 'first_of_month': + return $this->calculateFirstDayOfMonth($date); + break; + case 'last_of_month': + break; + default: + return $this->setDayOfMonth($date, $terms); + break; + } + } + + /** + * Calculates a date based on the client payment terms. + * + * @param Carbon $date A given date + * @return NULL|Carbon The date + */ + public function calculateDateFromTerms($date) + { + + $client_payment_terms = $this->client->getSetting('payment_terms'); + + if($client_payment_terms == '')//no due date! return null; + return null; + + return $date->copy()->addDays($client_payment_terms); //add the number of days in the payment terms to the date + } + + } diff --git a/app/Transformers/RecurringInvoiceTransformer.php b/app/Transformers/RecurringInvoiceTransformer.php index 1458595b8..e8af2751e 100644 --- a/app/Transformers/RecurringInvoiceTransformer.php +++ b/app/Transformers/RecurringInvoiceTransformer.php @@ -131,7 +131,7 @@ class RecurringInvoiceTransformer extends EntityTransformer 'custom_surcharge_tax3' => (bool) $invoice->custom_surcharge_tax3, 'custom_surcharge_tax4' => (bool) $invoice->custom_surcharge_tax4, 'line_items' => $invoice->line_items ?: (array) [], - 'entity_type' => 'recurring_invoice', + 'entity_type' => 'recurringInvoice', 'frequency_id' => (string) $invoice->frequency_id, 'remaining_cycles' => (int) $invoice->remaining_cycles, 'recurring_dates' => (array) $invoice->recurringDates(), diff --git a/app/Utils/Traits/Recurring/HasRecurrence.php b/app/Utils/Traits/Recurring/HasRecurrence.php new file mode 100644 index 000000000..30f7d331a --- /dev/null +++ b/app/Utils/Traits/Recurring/HasRecurrence.php @@ -0,0 +1,64 @@ +copy()->startOfMonth()->addMonth(); + } + + /** + * Calculates the last day of the month. + * + * If it is the last day of the month - we add a month on. + * + * @param Carbon $date The start date + * @return Carbon The last day of month + */ + public function calculateLastDayOfMonth($date) + { + if($date->isLastOfMonth()) + return $date->copy()->endOfMonth()->addMonthNoOverflow(); + + return $date->copy()->endOfMonth(); + } + + /** + * Sets the day of the month, if in the past we ADD a month + * + * @param Carbon $date The start date + * @param String|Int $day_of_month The day of the month + */ + public function setDateOfMonth($date, $day_of_month) + { + + $set_date = $date->copy()->setUnitNoOverflow('day', $day_of_month, 'month'); + + if($set_date->isPast()) + return $set_date->addMonthNoOverflow(); + + return $set_date; + } + +} \ No newline at end of file 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 e7e86ec69..8713851f8 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 @@ -70,7 +70,7 @@ 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(); + $table->string('due_date_days')->nullable(); $table->date('partial_due_date')->nullable(); }); } diff --git a/tests/Feature/CancelInvoiceTest.php b/tests/Feature/CancelInvoiceTest.php index 89667fb75..654f165a8 100644 --- a/tests/Feature/CancelInvoiceTest.php +++ b/tests/Feature/CancelInvoiceTest.php @@ -1,5 +1,13 @@ status_id = RecurringInvoice::STATUS_PENDING; $recurring_invoice->frequency_id = RecurringInvoice::FREQUENCY_MONTHLY; $recurring_invoice->remaining_cycles = 5; - $recurring_invoice->due_date_days = 5; + $recurring_invoice->due_date_days = '5'; $recurring_invoice->next_send_date = now(); $recurring_invoice->save(); diff --git a/tests/Unit/RecurringDueDatesTest.php b/tests/Unit/RecurringDueDatesTest.php new file mode 100644 index 000000000..6196f360b --- /dev/null +++ b/tests/Unit/RecurringDueDatesTest.php @@ -0,0 +1,42 @@ +calculateFirstDayOfMonth($date); + + $this->assertEquals('2020-03-01', $due_date->format('Y-m-d')); + + } +} diff --git a/tests/Unit/SentryTest.php b/tests/Unit/SentryTest.php index 9b8fa2ef3..42abb51ce 100644 --- a/tests/Unit/SentryTest.php +++ b/tests/Unit/SentryTest.php @@ -1,5 +1,13 @@