diff --git a/app/Factory/CloneQuoteToInvoiceFactory.php b/app/Factory/CloneQuoteToInvoiceFactory.php index b7da525f4893..03d057987a99 100644 --- a/app/Factory/CloneQuoteToInvoiceFactory.php +++ b/app/Factory/CloneQuoteToInvoiceFactory.php @@ -30,7 +30,8 @@ class CloneQuoteToInvoiceFactory unset($quote_array['invitations']); //preserve terms if they exist on Quotes - if(array_key_exists('terms', $quote_array) && strlen($quote_array['terms']) < 2) + //if(array_key_exists('terms', $quote_array) && strlen($quote_array['terms']) < 2) + if(!$quote->company->use_quote_terms_on_conversion) unset($quote_array['terms']); // unset($quote_array['public_notes']); @@ -38,7 +39,6 @@ class CloneQuoteToInvoiceFactory unset($quote_array['design_id']); unset($quote_array['user']); - foreach ($quote_array as $key => $value) { $invoice->{$key} = $value; } diff --git a/app/Jobs/Cron/RecurringInvoicesCron.php b/app/Jobs/Cron/RecurringInvoicesCron.php index 19ffaca7487c..194d17e11d7e 100644 --- a/app/Jobs/Cron/RecurringInvoicesCron.php +++ b/app/Jobs/Cron/RecurringInvoicesCron.php @@ -67,11 +67,23 @@ class RecurringInvoicesCron nlog("Trying to send {$recurring_invoice->number}"); + /* Special check if we should generate another invoice is the previous one is yet to be paid */ + if($recurring_invoice->company->stop_on_unpaid_recurring && $recurring_invoice->invoices()->whereIn('status_id', [2,3])->where('is_deleted', 0)->where('balance', '>', 0)->exists()) { + + nlog("Existing invoice exists, skipping"); + return; + + } + try{ + SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db); + } catch(\Exception $e){ + nlog("Unable to sending recurring invoice {$recurring_invoice->id} ". $e->getMessage()); + } }); @@ -103,6 +115,13 @@ class RecurringInvoicesCron nlog("Trying to send {$recurring_invoice->number}"); + if($recurring_invoice->company->stop_on_unpaid_recurring) { + + if($recurring_invoice->invoices()->whereIn('status_id', [2,3])->where('is_deleted', 0)->where('balance', '>', 0)->exists()) + return; + + } + try{ SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db); } diff --git a/app/Models/Company.php b/app/Models/Company.php index 7ff29a5b3c9a..40300ae8972b 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -100,6 +100,8 @@ class Company extends BaseModel 'client_registration_fields', 'convert_rate_to_client', 'markdown_email_enabled', + 'stop_on_unpaid_recurring', + 'use_quote_terms_on_conversion', ]; protected $hidden = [ diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index 82fc9af82fa3..3bc05c684e1d 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -168,6 +168,7 @@ class CompanyTransformer extends EntityTransformer 'client_registration_fields' => (array) $company->client_registration_fields, 'convert_rate_to_client' => (bool) $company->convert_rate_to_client, 'markdown_email_enabled' => (bool) $company->markdown_email_enabled, + 'stop_on_unpaid_recurring' => (bool) $company->stop_on_unpaid_recurring, ]; } diff --git a/database/migrations/2022_05_16_224917_add_auto_bill_tries_to_invoices_table.php b/database/migrations/2022_05_16_224917_add_auto_bill_tries_to_invoices_table.php index befc660c0cbf..5d57b04687e9 100644 --- a/database/migrations/2022_05_16_224917_add_auto_bill_tries_to_invoices_table.php +++ b/database/migrations/2022_05_16_224917_add_auto_bill_tries_to_invoices_table.php @@ -16,6 +16,11 @@ class AddAutoBillTriesToInvoicesTable extends Migration Schema::table('invoices', function (Blueprint $table) { $table->smallInteger('auto_bill_tries')->default(0); }); + + Schema::table('companies', function (Blueprint $table) { + $table->boolean('stop_on_unpaid_recurring')->default(0); + $table->boolean('use_quote_terms_on_conversion')->default(0); + }); } /** @@ -25,8 +30,6 @@ class AddAutoBillTriesToInvoicesTable extends Migration */ public function down() { - Schema::table('invoices', function (Blueprint $table) { - $table->dropColumn('auto_bill_tries'); - }); + } }