From 3abd0e0b17be629ecfd8b2d29a0b9528c563891d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 1 Jun 2020 14:18:33 +1000 Subject: [PATCH] Fixes for tests --- app/Services/Payment/RefundPayment.php | 41 ++++++++++++++++++++++++++ tests/Feature/RefundTest.php | 2 -- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index ba17898919c5..822eaadbfb5c 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -46,6 +46,7 @@ class RefundPayment ->buildCreditLineItems() //generate the credit note items ->updateCreditables() //return the credits first ->updatePaymentables() //update the paymentable items + ->adjustInvoices() ->createActivity() // create the refund activity ->processGatewayRefund() //process the gateway refund if needed ->save(); @@ -242,6 +243,46 @@ class RefundPayment return $this; } + + private function adjustInvoices() + { + $adjustment_amount = 0; + + if(isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) + { + foreach ($this->refund_data['invoices'] as $refunded_invoice) { + $invoice = Invoice::find($refunded_invoice['invoice_id']); + + $invoice->service()->updateBalance($refunded_invoice['amount'])->save(); + + if ($invoice->amount == $invoice->balance) { + $invoice->service()->setStatus(Invoice::STATUS_SENT); + } else { + $invoice->service()->setStatus(Invoice::STATUS_PARTIAL); + } + + $client = $invoice->client; + + $adjustment_amount += $refunded_invoice['amount']; + $client->balance += $refunded_invoice['amount']; + + $client->save(); + + //todo adjust ledger balance here? or after and reference the credit and its total + } + + $ledger_string = ''; //todo + + $this->credit_note->ledger()->updateCreditBalance($adjustment_amount, $ledger_string); + + $this->payment->client->paid_to_date -= $this->refund_data['amount']; + $this->payment->client->save(); + + } + + return $this; + } + private function save() { $this->payment->save(); diff --git a/tests/Feature/RefundTest.php b/tests/Feature/RefundTest.php index 1df44379e248..898ac90a82ab 100644 --- a/tests/Feature/RefundTest.php +++ b/tests/Feature/RefundTest.php @@ -125,8 +125,6 @@ class RefundTest extends TestCase $response->assertStatus(200); -info($arr); - $this->assertEquals(50, $arr['data']['refunded']); $this->assertEquals(Payment::STATUS_REFUNDED, $arr['data']['status_id']); }