From 5897a4e7497c76c6756638b9cec31e50d6b64537 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 6 Feb 2022 13:46:19 +1100 Subject: [PATCH] Fixes for allowing a deleted invoice to be marked as sent --- app/Services/Invoice/MarkSent.php | 4 +-- config/querydetector.php | 2 +- tests/Feature/InvoiceTest.php | 58 +++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 40a851f6dd09..5c7c53200665 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -33,8 +33,8 @@ class MarkSent extends AbstractService public function run() { - /* Return immediately if status is not draft */ - if ($this->invoice && $this->invoice->fresh()->status_id != Invoice::STATUS_DRAFT) { + /* Return immediately if status is not draft or invoice has been deleted */ + if ($this->invoice && ($this->invoice->fresh()->status_id != Invoice::STATUS_DRAFT || $this->invoice->is_deleted)) { return $this->invoice; } diff --git a/config/querydetector.php b/config/querydetector.php index 5f8207ef22e8..72b3fc33ada1 100644 --- a/config/querydetector.php +++ b/config/querydetector.php @@ -11,7 +11,7 @@ return [ * Threshold level for the N+1 query detection. If a relation query will be * executed more then this amount, the detector will notify you about it. */ - 'threshold' => (int) env('QUERY_DETECTOR_THRESHOLD', 1), + 'threshold' => (int) env('QUERY_DETECTOR_THRESHOLD', 3), /* * Here you can whitelist model relations. diff --git a/tests/Feature/InvoiceTest.php b/tests/Feature/InvoiceTest.php index 87d37188f2f1..d57f588ac8e6 100644 --- a/tests/Feature/InvoiceTest.php +++ b/tests/Feature/InvoiceTest.php @@ -10,9 +10,11 @@ */ namespace Tests\Feature; +use App\Helpers\Invoice\InvoiceSum; use App\Models\Client; use App\Models\ClientContact; use App\Models\Invoice; +use App\Repositories\InvoiceRepository; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -43,6 +45,62 @@ class InvoiceTest extends TestCase $this->makeTestData(); } + public function testMarkingDeletedInvoiceAsSent() + { + + Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) { + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $c->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $c->id, + 'company_id' => $this->company->id, + ]); + }); + + $client = Client::all()->first(); + + $invoice = Invoice::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]); + $invoice->status_id = Invoice::STATUS_DRAFT; + + $invoice->line_items = $this->buildLineItems(); + $invoice->uses_inclusive_taxes = false; + $invoice->tax_rate1 = 0; + $invoice->tax_rate2 = 0; + $invoice->tax_rate3 = 0; + $invoice->discount = 0; + + $invoice->save(); + + $invoice_calc = new InvoiceSum($invoice); + $invoice_calc->build(); + + $invoice = $invoice_calc->getInvoice(); + $invoice->save(); + + $this->assertEquals(Invoice::STATUS_DRAFT, $invoice->status_id); + $this->assertEquals(10, $invoice->amount); + $this->assertEquals(0, $invoice->balance); + + $invoice_repository = new InvoiceRepository(); + $invoice = $invoice_repository->delete($invoice); + + + $this->assertEquals(10, $invoice->amount); + $this->assertEquals(0, $invoice->balance); + $this->assertTrue($invoice->is_deleted); + + $invoice->service()->markSent()->save(); + + $this->assertEquals(0, $invoice->balance); + + } + public function testInvoiceList() { Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {