fixes for edge case when deleting a payment on a deleted invoice

This commit is contained in:
David Bomba 2021-09-06 11:04:00 +10:00
parent 69c5891723
commit b06d761c8e
2 changed files with 74 additions and 17 deletions

View File

@ -84,25 +84,40 @@ class DeletePayment
nlog("net deletable amount - refunded = {$net_deletable}"); nlog("net deletable amount - refunded = {$net_deletable}");
$paymentable_invoice->service() if(!$paymentable_invoice->is_deleted)
->updateBalance($net_deletable) {
->updatePaidToDate($net_deletable * -1) $paymentable_invoice->service()
->save(); ->updateBalance($net_deletable)
->updatePaidToDate($net_deletable * -1)
->save();
$paymentable_invoice->ledger() $paymentable_invoice->ledger()
->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}") ->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}")
->save(); ->save();
$paymentable_invoice->client $paymentable_invoice->client
->service() ->service()
->updateBalance($net_deletable) ->updateBalance($net_deletable)
->updatePaidToDate($net_deletable * -1) ->updatePaidToDate($net_deletable * -1)
->save(); ->save();
if ($paymentable_invoice->balance == $paymentable_invoice->amount) { if ($paymentable_invoice->balance == $paymentable_invoice->amount) {
$paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save(); $paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save();
} else { } else {
$paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save(); $paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save();
}
}
else {
//If the invoice is deleted we only update the meta data on the invoice
//and reduce the clients paid to date
$paymentable_invoice->updatePaidToDate($net_deletable * -1)
->save();
$paymentable_invoice->client
->service()
->updatePaidToDate($net_deletable * -1)
->save();
} }
}); });

View File

@ -117,6 +117,7 @@ class MultiPaymentDeleteTest extends TestCase
$invoice = $invoice->calc()->getInvoice(); $invoice = $invoice->calc()->getInvoice();
$this->assertEquals(0, $client->balance); $this->assertEquals(0, $client->balance);
$this->assertEquals(0, $client->paid_to_date);
$this->assertEquals(0, $invoice->balance); $this->assertEquals(0, $invoice->balance);
//mark sent //mark sent
@ -126,6 +127,7 @@ class MultiPaymentDeleteTest extends TestCase
$invoice->client->fresh(); $invoice->client->fresh();
$this->assertEquals(325, $invoice->balance); $this->assertEquals(325, $invoice->balance);
$this->assertEquals(0, $invoice->client->fresh()->paid_to_date);
$this->assertEquals(325, $invoice->client->balance); $this->assertEquals(325, $invoice->client->balance);
//payment 163 //payment 163
@ -154,6 +156,7 @@ class MultiPaymentDeleteTest extends TestCase
//payment 162 //payment 162
$this->assertEquals(162, $invoice->fresh()->balance); $this->assertEquals(162, $invoice->fresh()->balance);
$this->assertEquals(162, $invoice->client->fresh()->balance); $this->assertEquals(162, $invoice->client->fresh()->balance);
$this->assertEquals(163, $invoice->client->fresh()->paid_to_date);
$data = [ $data = [
@ -179,6 +182,7 @@ class MultiPaymentDeleteTest extends TestCase
$this->assertEquals(0, $invoice->fresh()->balance); $this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance); $this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(325, $invoice->client->fresh()->paid_to_date);
//refund payment 2 by 63 dollars //refund payment 2 by 63 dollars
@ -203,6 +207,7 @@ class MultiPaymentDeleteTest extends TestCase
$this->assertEquals(63, $invoice->fresh()->balance); $this->assertEquals(63, $invoice->fresh()->balance);
$this->assertEquals(63, $invoice->client->fresh()->balance); $this->assertEquals(63, $invoice->client->fresh()->balance);
$this->assertEquals(262, $invoice->client->fresh()->paid_to_date);
//delete payment 2 //delete payment 2
@ -218,6 +223,7 @@ class MultiPaymentDeleteTest extends TestCase
$this->assertEquals(162, $invoice->fresh()->balance); $this->assertEquals(162, $invoice->fresh()->balance);
$this->assertEquals(162, $invoice->client->fresh()->balance); $this->assertEquals(162, $invoice->client->fresh()->balance);
$this->assertEquals(163, $invoice->client->fresh()->paid_to_date);
// Pay 162 again and create payment #3 // Pay 162 again and create payment #3
@ -248,6 +254,7 @@ class MultiPaymentDeleteTest extends TestCase
$this->assertEquals(0, $invoice->fresh()->balance); $this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance); $this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(325, $invoice->client->fresh()->paid_to_date);
//refund payment 3 by 63 //refund payment 3 by 63
@ -271,6 +278,7 @@ class MultiPaymentDeleteTest extends TestCase
$this->assertEquals(63, $invoice->fresh()->balance); $this->assertEquals(63, $invoice->fresh()->balance);
$this->assertEquals(63, $invoice->client->fresh()->balance); $this->assertEquals(63, $invoice->client->fresh()->balance);
$this->assertEquals(262, $invoice->client->fresh()->paid_to_date);
//payment 4 for 63 //payment 4 for 63
$data = [ $data = [
@ -297,6 +305,7 @@ class MultiPaymentDeleteTest extends TestCase
$this->assertEquals(0, $invoice->fresh()->balance); $this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance); $this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(325, $invoice->client->fresh()->paid_to_date);
// delete payment 3 // delete payment 3
// //
@ -313,6 +322,7 @@ class MultiPaymentDeleteTest extends TestCase
$this->assertEquals(63, $invoice->fresh()->balance); $this->assertEquals(63, $invoice->fresh()->balance);
$this->assertEquals(63, $invoice->client->fresh()->balance); $this->assertEquals(63, $invoice->client->fresh()->balance);
$this->assertEquals(262, $invoice->client->fresh()->paid_to_date);
//set discount of 63 to invoice //set discount of 63 to invoice
@ -329,6 +339,38 @@ class MultiPaymentDeleteTest extends TestCase
$this->assertEquals(0, $invoice->fresh()->balance); $this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance); $this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(262, $invoice->client->fresh()->paid_to_date);
//now delete the invoice
$data = [
'ids' => [$invoice->hashed_id],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/invoices/bulk?action=delete', $data);
$this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(262, $invoice->client->fresh()->paid_to_date);
//Delete payment 4 which is for $162
$data = [
'ids' => [$this->encodePrimaryKey($payment_1->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/bulk?action=delete', $data);
$this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->paid_to_date);
} }
} }