diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 7f6abc1773..8072c36a49 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -429,7 +429,7 @@ class InvoiceController extends BaseController event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - return $this->itemResponse($invoice); + return $this->itemResponse($invoice->fresh()); } /** diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 32adfc0cf2..8c8b7151e1 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -487,7 +487,6 @@ class InvoiceService ->updateInvoiceBalance($adjustment * -1, 'Adjustment for removing gateway fee'); $this->invoice->client->service()->updateBalance($adjustment * -1); - // $this->invoice->client->service()->calculateBalance(); } diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 74ab599d04..ea68196cbb 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -46,8 +46,9 @@ class MarkSent extends AbstractService ->ledger() ->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} marked as sent."); - $this->invoice->client->service()->calculateBalance(); - + // $this->invoice->client->service()->calculateBalance(); + $this->invoice->client->service()->updateBalance($adjustment); + /* Perform additional actions on invoice */ $this->invoice ->service() diff --git a/tests/Unit/InvoiceMarkPaidTest.php b/tests/Unit/InvoiceMarkPaidTest.php new file mode 100644 index 0000000000..f7d64b4067 --- /dev/null +++ b/tests/Unit/InvoiceMarkPaidTest.php @@ -0,0 +1,164 @@ +makeTestData(); + } + + public function testInvoiceMarkPaidFromDraft() + { + + + $c = \App\Models\Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + ]); + + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + $item->cost = 10; + $item->tax_name1 = ''; + $item->tax_rate1 = 0; + $item->type_id = '1'; + $item->tax_id = '1'; + $line_items[] = $item; + + + $i = Invoice::factory()->create([ + 'discount' => 0, + 'tax_name1' => '', + 'tax_name2' => '', + 'tax_name3' => '', + 'tax_rate1' => 0, + 'tax_rate2' => 0, + 'tax_rate3' => 0, + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $c->id, + 'line_items' => $line_items, + 'status_id' => 1, + 'uses_inclusive_taxes' => false, + 'is_amount_discount' => false + ]); + + $i->calc()->getInvoice(); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/invoices/{$i->hashed_id}?paid=true", []); + + $response->assertStatus(200); + + + $this->assertEquals(0, $response->json('data.balance')); + $this->assertEquals(10, $response->json('data.paid_to_date')); + $this->assertEquals(4, $response->json('data.status_id')); + + $i = $i->fresh(); + + $this->assertEquals(0, $i->balance); + $this->assertEquals(10, $i->paid_to_date); + $this->assertEquals(4, $i->status_id); + + + } + + + public function testInvoiceMarkPaidFromDraftBulk() + { + + + $c = \App\Models\Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + ]); + + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + $item->cost = 10; + $item->tax_name1 = ''; + $item->tax_rate1 = 0; + $item->type_id = '1'; + $item->tax_id = '1'; + $line_items[] = $item; + + + $i = Invoice::factory()->create([ + 'discount' => 0, + 'tax_name1' => '', + 'tax_name2' => '', + 'tax_name3' => '', + 'tax_rate1' => 0, + 'tax_rate2' => 0, + 'tax_rate3' => 0, + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $c->id, + 'line_items' => $line_items, + 'status_id' => 1, + 'uses_inclusive_taxes' => false, + 'is_amount_discount' => false + ]); + + $i->calc()->getInvoice(); + + $data = [ + 'action' => 'mark_paid', + 'ids' => [$i->hashed_id] + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson("/api/v1/invoices/bulk", $data); + + $response->assertStatus(200); + + $this->assertEquals(0, $response->json('data.0.balance')); + $this->assertEquals(10, $response->json('data.0.paid_to_date')); + $this->assertEquals(4, $response->json('data.0.status_id')); + + $i = $i->fresh(); + + $this->assertEquals(0, $i->balance); + $this->assertEquals(10, $i->paid_to_date); + $this->assertEquals(4, $i->status_id); + + + } + +} + \ No newline at end of file