From 70a575386c68c8160c9e5e95e6d9776ae9c4b0c9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 7 Oct 2020 10:16:57 +1100 Subject: [PATCH] Fixes for designs in migration: --- app/Helpers/Invoice/InvoiceItemSum.php | 8 ++++++-- .../Invoice/UniqueInvoiceNumberRule.php | 3 +++ app/Models/Gateway.php | 1 + app/Services/Invoice/ApplyNumber.php | 4 ++-- app/Utils/Traits/GeneratesCounter.php | 10 ++++++++-- database/factories/CompanyFactory.php | 1 + tests/Feature/InvoiceEmailTest.php | 8 ++++---- tests/MockAccountData.php | 12 ++++++------ tests/Unit/GeneratesCounterTest.php | 16 ++++++++-------- 9 files changed, 39 insertions(+), 24 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index eaecb3338c..ee66fddc67 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -230,7 +230,10 @@ class InvoiceItemSum continue; } - $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total)); + //$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total)); + $amount = ( $this->sub_total > 0 ) ? $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total)) : 0; + + $item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount); $item_tax += $item_tax_rate1_total; @@ -260,7 +263,8 @@ class InvoiceItemSum } /** - * Sets default values for the line_items. + * Sets default casts for the values in the line_items. + * * @return $this */ private function cleanLineItem() diff --git a/app/Http/ValidationRules/Invoice/UniqueInvoiceNumberRule.php b/app/Http/ValidationRules/Invoice/UniqueInvoiceNumberRule.php index 15523687bc..f8156b7a97 100644 --- a/app/Http/ValidationRules/Invoice/UniqueInvoiceNumberRule.php +++ b/app/Http/ValidationRules/Invoice/UniqueInvoiceNumberRule.php @@ -55,6 +55,9 @@ class UniqueInvoiceNumberRule implements Rule */ private function checkIfInvoiceNumberUnique() : bool { + if(empty($this->input['number'])) + return true; + $invoice = Invoice::where('client_id', $this->input['client_id']) ->where('number', $this->input['number']) ->withTrashed() diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index 801d4d5eec..448c522e3a 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -37,6 +37,7 @@ class Gateway extends StaticModel /** * @return mixed + * @deprecated 5.0.17 No longer needs as we are removing omnipay dependence */ public function getFields() { diff --git a/app/Services/Invoice/ApplyNumber.php b/app/Services/Invoice/ApplyNumber.php index ccd30dbdce..72edd57a5d 100644 --- a/app/Services/Invoice/ApplyNumber.php +++ b/app/Services/Invoice/ApplyNumber.php @@ -44,11 +44,11 @@ class ApplyNumber extends AbstractService switch ($this->client->getSetting('counter_number_applied')) { case 'when_saved': - $this->invoice->number = $this->getNextInvoiceNumber($this->client); + $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice); break; case 'when_sent': if ($this->invoice->status_id == Invoice::STATUS_SENT) { - $this->invoice->number = $this->getNextInvoiceNumber($this->client); + $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice); } break; diff --git a/app/Utils/Traits/GeneratesCounter.php b/app/Utils/Traits/GeneratesCounter.php index a94a231cfc..bd84bb285a 100644 --- a/app/Utils/Traits/GeneratesCounter.php +++ b/app/Utils/Traits/GeneratesCounter.php @@ -37,7 +37,7 @@ trait GeneratesCounter * * @return string The next invoice number. */ - public function getNextInvoiceNumber(Client $client) :string + public function getNextInvoiceNumber(Client $client, ?Invoice $invoice) :string { //Reset counters if enabled $this->resetCounters($client); @@ -67,6 +67,9 @@ trait GeneratesCounter $invoice_number = $this->checkEntityNumber(Invoice::class, $client, $counter, $padding, $pattern); + if($invoice && $invoice->recurring_id) + $invoice_number = $this->prefixCounter($invoice_number, $client->getSetting('recurring_number_prefix')); + $this->incrementCounter($counter_entity, 'invoice_number_counter'); return $invoice_number; @@ -140,6 +143,9 @@ trait GeneratesCounter $quote_number = $this->checkEntityNumber(Quote::class, $client, $counter, $padding, $pattern); + // if($this->recurring_id) + // $quote_number = $this->prefixCounter($quote_number, $client->getSetting('recurring_number_prefix')); + $this->incrementCounter($counter_entity, $used_counter); return $quote_number; @@ -168,7 +174,7 @@ trait GeneratesCounter $pattern = ''; $padding = $client->getSetting('counter_padding'); $invoice_number = $this->checkEntityNumber(RecurringInvoice::class, $client, $counter, $padding, $pattern); - $invoice_number = $this->prefixCounter($invoice_number, $client->getSetting('recurring_number_prefix')); + //$invoice_number = $this->prefixCounter($invoice_number, $client->getSetting('recurring_number_prefix')); //increment the correct invoice_number Counter (company vs client) if ($is_client_counter) { diff --git a/database/factories/CompanyFactory.php b/database/factories/CompanyFactory.php index b01724b8ac..bd6239854e 100644 --- a/database/factories/CompanyFactory.php +++ b/database/factories/CompanyFactory.php @@ -39,6 +39,7 @@ class CompanyFactory extends Factory 'db' => config('database.default'), 'settings' => CompanySettings::defaults(), 'is_large' => false, + 'enabled_modules' => config('ninja.enabled_modules'), 'custom_fields' => (object) [ //'invoice1' => 'Custom Date|date', // 'invoice2' => '2|switch', diff --git a/tests/Feature/InvoiceEmailTest.php b/tests/Feature/InvoiceEmailTest.php index 58a2d1d013..1e4887a75d 100644 --- a/tests/Feature/InvoiceEmailTest.php +++ b/tests/Feature/InvoiceEmailTest.php @@ -54,7 +54,7 @@ class InvoiceEmailTest extends TestCase { $this->invoice->date = now(); $this->invoice->due_date = now()->addDays(7); - $this->invoice->number = $this->getNextInvoiceNumber($this->client); + $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice); $this->invoice->client_id = $this->client->id; @@ -90,7 +90,7 @@ class InvoiceEmailTest extends TestCase $this->invoice->date = now(); $this->invoice->due_date = now()->addDays(7); - $this->invoice->number = $this->getNextInvoiceNumber($this->client); + $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice); $this->invoice->client_id = $this->client->id; $this->invoice->setRelation('client', $this->client); @@ -115,7 +115,7 @@ class InvoiceEmailTest extends TestCase $this->invoice->date = now(); $this->invoice->due_date = now()->addDays(7); - $this->invoice->number = $this->getNextInvoiceNumber($this->client); + $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice); $this->invoice->client_id = $this->client->id; @@ -145,7 +145,7 @@ class InvoiceEmailTest extends TestCase $this->invoice->date = now(); $this->invoice->due_date = now()->addDays(7); - $this->invoice->number = $this->getNextInvoiceNumber($this->client); + $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice); $this->invoice->client_id = $this->client->id; $this->invoice->setRelation('client', $this->client); diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 36dcf0e9fd..d4a3bca6e2 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -356,7 +356,7 @@ trait MockAccountData $recurring_invoice->next_send_date = Carbon::now(); $recurring_invoice->save(); - $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client); + $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client, $this->invoice); $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); @@ -366,7 +366,7 @@ trait MockAccountData $recurring_invoice->next_send_date = Carbon::now(); $recurring_invoice->save(); - $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client); + $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client, $this->invoice); $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); @@ -376,7 +376,7 @@ trait MockAccountData $recurring_invoice->next_send_date = Carbon::now(); $recurring_invoice->save(); - $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client); + $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client, $this->invoice); $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); @@ -386,7 +386,7 @@ trait MockAccountData $recurring_invoice->next_send_date = Carbon::now(); $recurring_invoice->save(); - $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client); + $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client, $this->invoice); $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); @@ -396,7 +396,7 @@ trait MockAccountData $recurring_invoice->next_send_date = Carbon::now(); $recurring_invoice->save(); - $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client); + $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client, $this->invoice); $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); @@ -406,7 +406,7 @@ trait MockAccountData $recurring_invoice->next_send_date = Carbon::now()->addDays(10); $recurring_invoice->save(); - $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client); + $recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client, $this->invoice); $recurring_invoice->save(); $gs = new GroupSetting; diff --git a/tests/Unit/GeneratesCounterTest.php b/tests/Unit/GeneratesCounterTest.php index da4d204b7a..25980d1205 100644 --- a/tests/Unit/GeneratesCounterTest.php +++ b/tests/Unit/GeneratesCounterTest.php @@ -79,11 +79,11 @@ class GeneratesCounterTest extends TestCase public function testInvoiceNumberValue() { - $invoice_number = $this->getNextInvoiceNumber($this->client); + $invoice_number = $this->getNextInvoiceNumber($this->client, $this->invoice); $this->assertEquals($invoice_number, '0008'); - $invoice_number = $this->getNextInvoiceNumber($this->client); + $invoice_number = $this->getNextInvoiceNumber($this->client, $this->invoice); $this->assertEquals($invoice_number, '0009'); } @@ -112,8 +112,8 @@ class GeneratesCounterTest extends TestCase $this->client->save(); $this->client->fresh(); - $invoice_number = $this->getNextInvoiceNumber($this->client); - $invoice_number2 = $this->getNextInvoiceNumber($this->client); + $invoice_number = $this->getNextInvoiceNumber($this->client, $this->invoice); + $invoice_number2 = $this->getNextInvoiceNumber($this->client, $this->invoice); $this->assertEquals($invoice_number, date('Y').'-0001'); $this->assertEquals($invoice_number2, date('Y').'-0002'); @@ -203,7 +203,7 @@ class GeneratesCounterTest extends TestCase $cliz = ClientFactory::create($this->company->id, $this->user->id); $cliz->settings = ClientSettings::defaults(); $cliz->save(); - $invoice_number = $this->getNextInvoiceNumber($cliz); + $invoice_number = $this->getNextInvoiceNumber($cliz, $this->invoice); $this->assertEquals($cliz->getSetting('counter_padding'), 5); $this->assertEquals($invoice_number, '00007'); @@ -218,7 +218,7 @@ class GeneratesCounterTest extends TestCase $cliz->settings = ClientSettings::defaults(); $cliz->save(); - $invoice_number = $this->getNextInvoiceNumber($cliz); + $invoice_number = $this->getNextInvoiceNumber($cliz, $this->invoice); $this->assertEquals($cliz->getSetting('counter_padding'), 10); $this->assertEquals(strlen($invoice_number), 10); @@ -235,11 +235,11 @@ class GeneratesCounterTest extends TestCase $cliz->settings = ClientSettings::defaults(); $cliz->save(); - $invoice_number = $this->getNextInvoiceNumber($cliz); + $invoice_number = $this->getNextInvoiceNumber($cliz, $this->invoice); $this->assertEquals($invoice_number, '0008'); - $invoice_number = $this->getNextInvoiceNumber($cliz); + $invoice_number = $this->getNextInvoiceNumber($cliz, $this->invoice); $this->assertEquals($invoice_number, '0009'); }