Fixes for allowing a deleted invoice to be marked as sent

This commit is contained in:
David Bomba 2022-02-06 13:46:19 +11:00
parent 8acf738197
commit 5897a4e749
3 changed files with 61 additions and 3 deletions

View File

@ -33,8 +33,8 @@ class MarkSent extends AbstractService
public function run() public function run()
{ {
/* Return immediately if status is not draft */ /* Return immediately if status is not draft or invoice has been deleted */
if ($this->invoice && $this->invoice->fresh()->status_id != Invoice::STATUS_DRAFT) { if ($this->invoice && ($this->invoice->fresh()->status_id != Invoice::STATUS_DRAFT || $this->invoice->is_deleted)) {
return $this->invoice; return $this->invoice;
} }

View File

@ -11,7 +11,7 @@ return [
* Threshold level for the N+1 query detection. If a relation query will be * 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. * 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. * Here you can whitelist model relations.

View File

@ -10,9 +10,11 @@
*/ */
namespace Tests\Feature; namespace Tests\Feature;
use App\Helpers\Invoice\InvoiceSum;
use App\Models\Client; use App\Models\Client;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\Invoice; use App\Models\Invoice;
use App\Repositories\InvoiceRepository;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -43,6 +45,62 @@ class InvoiceTest extends TestCase
$this->makeTestData(); $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() public function testInvoiceList()
{ {
Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) { Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {