diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 5c5185c6de01..204d997a1312 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -132,7 +132,7 @@ class AutoBillInvoice extends AbstractService info("adjusting credit balance {$current_credit->balance} by this amount ". $credit['amount']); $current_credit->service() - ->updateBalance($credit['amount']*-1) + ->adjustBalance($credit['amount']*-1) ->updatePaidToDate($credit['amount']) ->setCalculatedStatus() ->save(); diff --git a/tests/Feature/EntityPaidToDateTest.php b/tests/Feature/EntityPaidToDateTest.php new file mode 100644 index 000000000000..bf4613faf608 --- /dev/null +++ b/tests/Feature/EntityPaidToDateTest.php @@ -0,0 +1,141 @@ +faker = \Faker\Factory::create(); + + Model::reguard(); + + $this->makeTestData(); + $this->withoutExceptionHandling(); + + $this->withoutMiddleware( + ThrottleRequests::class + ); + } + + public function testPaidToDateWithMarkPaidAction() + { + + //create new client + + $data = [ + 'name' => 'A Nice Client', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/clients', $data); + + $response->assertStatus(200); + + $arr = $response->json(); + + $client_hash_id = $arr['data']['id']; + $client = Client::find($this->decodePrimaryKey($client_hash_id)); + + $this->assertEquals($client->balance, 0); + $this->assertEquals($client->paid_to_date, 0); + //create new invoice. + + $line_items = []; + + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + $item->cost = 10; + + $line_items[] = (array)$item; + + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + $item->cost = 10; + + $line_items[] = (array)$item; + + $invoice = [ + 'status_id' => 1, + 'number' => '', + 'discount' => 0, + 'is_amount_discount' => 1, + 'po_number' => '3434343', + 'public_notes' => 'notes', + 'is_deleted' => 0, + 'custom_value1' => 0, + 'custom_value2' => 0, + 'custom_value3' => 0, + 'custom_value4' => 0, + 'client_id' => $client_hash_id, + 'line_items' => (array)$line_items, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/invoices/', $invoice) + ->assertStatus(200); + + $arr = $response->json(); + + $invoice_one_hashed_id = $arr['data']['id']; + + $invoice = Invoice::find($this->decodePrimaryKey($invoice_one_hashed_id)); + + $this->assertEquals($invoice->balance, 0); + $this->assertEquals($invoice->paid_to_date, 0); + + $invoice->service()->markSent()->save(); + + $this->assertEquals($invoice->balance, 20); + + $invoice->service()->markPaid()->save(); + + $this->assertEquals($invoice->paid_to_date, 20); + } +}