From 24104509b3e85d64141fe087f0e66b16589d7ffc Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 17 Sep 2020 09:26:23 +1000 Subject: [PATCH] Fixes for recurring invoice schema - auto_bill_enabled --- app/Factory/RecurringInvoiceToInvoiceFactory.php | 8 ++++++-- app/Jobs/RecurringInvoice/SendRecurring.php | 9 ++++----- app/Services/Invoice/AutoBillInvoice.php | 2 +- ..._18_140557_add_is_public_to_documents_table.php | 14 ++++++++------ 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/Factory/RecurringInvoiceToInvoiceFactory.php b/app/Factory/RecurringInvoiceToInvoiceFactory.php index 572f2727d3..cafa7ee385 100644 --- a/app/Factory/RecurringInvoiceToInvoiceFactory.php +++ b/app/Factory/RecurringInvoiceToInvoiceFactory.php @@ -31,13 +31,15 @@ class RecurringInvoiceToInvoiceFactory $invoice->public_notes = $recurring_invoice->public_notes; $invoice->private_notes = $recurring_invoice->private_notes; $invoice->date = date_create()->format($client->date_format()); - $invoice->due_date = $recurring_invoice->due_date; //todo calculate based on terms + $invoice->due_date = $recurring_invoice->calculateDueDate($recurring_invoice->next_send_date); $invoice->is_deleted = $recurring_invoice->is_deleted; $invoice->line_items = $recurring_invoice->line_items; $invoice->tax_name1 = $recurring_invoice->tax_name1; $invoice->tax_rate1 = $recurring_invoice->tax_rate1; $invoice->tax_name2 = $recurring_invoice->tax_name2; $invoice->tax_rate2 = $recurring_invoice->tax_rate2; + $invoice->tax_name3 = $recurring_invoice->tax_name3; + $invoice->tax_rate3 = $recurring_invoice->tax_rate3; $invoice->custom_value1 = $recurring_invoice->custom_value1; $invoice->custom_value2 = $recurring_invoice->custom_value2; $invoice->custom_value3 = $recurring_invoice->custom_value3; @@ -45,10 +47,12 @@ class RecurringInvoiceToInvoiceFactory $invoice->amount = $recurring_invoice->amount; $invoice->balance = $recurring_invoice->balance; $invoice->user_id = $recurring_invoice->user_id; + $invoice->assigned_user_id = $recurring_invoice->assigned_user_id; $invoice->company_id = $recurring_invoice->company_id; $invoice->recurring_id = $recurring_invoice->id; $invoice->client_id = $client->id; - + $invoice->auto_bill_enabled = $recurring_invoice->auto_bill_enabled; + return $invoice; } } diff --git a/app/Jobs/RecurringInvoice/SendRecurring.php b/app/Jobs/RecurringInvoice/SendRecurring.php index 82e645f24a..bec81ae599 100644 --- a/app/Jobs/RecurringInvoice/SendRecurring.php +++ b/app/Jobs/RecurringInvoice/SendRecurring.php @@ -62,7 +62,7 @@ class SendRecurring implements ShouldQueue ->createInvitations() ->save(); - $invoice->invitations->each(function ($invitation) use ($invoice) { + $invoice->invitations->each(function ($invitation) use ($invoice) { $email_builder = (new InvoiceEmail())->build($invitation); @@ -72,6 +72,9 @@ class SendRecurring implements ShouldQueue }); + if($invoice->client->getSetting('auto_bill_date') == 'on_send_date' && $this->recurring_invoice->auto_bill_enabled) + $invoice->service()->autoBill()->save(); + /* Set next date here to prevent a recurring loop forming */ $this->recurring_invoice->next_send_date = $this->recurring_invoice->nextSendDate()->format('Y-m-d'); $this->recurring_invoice->remaining_cycles = $this->recurring_invoice->remainingCycles(); @@ -86,10 +89,6 @@ class SendRecurring implements ShouldQueue if ($invoice->invitations->count() > 0) event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars())); - // Fire Payment if auto-bill is enabled - if ($this->recurring_invoice->auto_bill) - $invoice->service()->autoBill()->save(); - } } diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 8ce07c3c8b..3dad55c4e8 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -46,7 +46,7 @@ class AutoBillInvoice extends AbstractService $this->invoice = $this->invoice->service()->markSent()->save(); if ($this->invoice->balance > 0) { - $gateway_token = $this->getGateway($this->invoice->balance); + $gateway_token = $this->getGateway($this->invoice->balance); //todo what if it is only a partial amount? } else { return $this->invoice->service()->markPaid()->save(); } 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 1be7ff6c48..119434755b 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 @@ -60,20 +60,22 @@ class AddIsPublicToDocumentsTable extends Migration $table->boolean('custom_surcharge_tax2')->default(false); $table->boolean('custom_surcharge_tax3')->default(false); $table->boolean('custom_surcharge_tax4')->default(false); + $table->integer('remaining_cycles')->nullable()->change(); + $table->dropColumn('start_date'); + $table->string('due_date_days')->nullable(); + $table->date('partial_due_date')->nullable(); $table->decimal('exchange_rate', 13, 6)->default(1); }); + Schema::table('invoices', function ($table) { + $table->boolean('auto_bill_enabled')->default(0); + }); + Schema::table('companies', function ($table) { $table->enum('default_auto_bill', ['off', 'always', 'optin', 'optout'])->default('off'); }); - Schema::table('recurring_invoices', function (Blueprint $table) { - $table->integer('remaining_cycles')->nullable()->change(); - $table->dropColumn('start_date'); - $table->string('due_date_days')->nullable(); - $table->date('partial_due_date')->nullable(); - }); } /**