From 4d72a312d63b19ccfd42c030e9955e055c8269bd Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 10 Sep 2024 14:29:01 +1000 Subject: [PATCH] Fixes for statements --- app/Services/Payment/RefundPayment.php | 1 + app/Services/Pdf/PdfBuilder.php | 31 ++++++++++++++++++++++ app/Services/PdfMaker/Design.php | 36 +++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index a353c50791f2..4d406754aa73 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -103,6 +103,7 @@ class RefundPayment //block prevents the edge case where a partial refund was attempted. $this->refund_data['invoices'] = $this->payment->invoices->map(function ($invoice) { return [ + 'date' => now()->addSeconds($invoice->client->timezone_offset())->format('Y-m-d'), 'invoice_id' => $invoice->id, 'amount' => $invoice->pivot->amount, ]; diff --git a/app/Services/Pdf/PdfBuilder.php b/app/Services/Pdf/PdfBuilder.php index 093b8b15774d..d1edef413238 100644 --- a/app/Services/Pdf/PdfBuilder.php +++ b/app/Services/Pdf/PdfBuilder.php @@ -356,6 +356,37 @@ class PdfBuilder $tbody[] = $element; $this->payment_amount_total += $payment->pivot->amount; + + if($payment->pivot->refunded > 0){ + + $refund_date = $payment->date; + + if($payment->refund_meta && is_array($payment->refund_meta)){ + + $refund_array = collect($payment->refund_meta)->first(function ($meta) use($invoice){ + foreach($meta['invoices'] as $refunded_invoice){ + + if ($refunded_invoice['invoice_id'] == $invoice->id) { + return true; + } + + } + }); + + $refund_date = $refund_array['date']; + } + + $element = ['element' => 'tr', 'elements' => []]; + $element['elements'][] = ['element' => 'td', 'content' => $invoice->number]; + $element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($refund_date, $this->service->config->date_format, $this->service->config->locale) ?: ' ']; + $element['elements'][] = ['element' => 'td', 'content' => ctrans('texts.refund')]; + $element['elements'][] = ['element' => 'td', 'content' => $this->service->config->formatMoney($payment->pivot->refunded) ?: ' ']; + + $tbody[] = $element; + + $this->payment_amount_total -= $payment->pivot->refunded; + + } } } diff --git a/app/Services/PdfMaker/Design.php b/app/Services/PdfMaker/Design.php index c9b5bfa94ca5..54d11c0a9ac9 100644 --- a/app/Services/PdfMaker/Design.php +++ b/app/Services/PdfMaker/Design.php @@ -561,7 +561,41 @@ class Design extends BaseDesign $tbody[] = $element; $this->payment_amount_total += $payment->pivot->amount; - } + + if ($payment->pivot->refunded > 0) { + + $refund_date = $payment->date; + + if($payment->refund_meta && is_array($payment->refund_meta)){ + + $refund_array = collect($payment->refund_meta)->first(function ($meta) use($invoice){ + foreach($meta['invoices'] as $refunded_invoice){ + + if ($refunded_invoice['invoice_id'] == $invoice->id) { + return true; + } + + } + }); + + $refund_date = $refund_array['date']; + } + + $element = ['element' => 'tr', 'elements' => []]; + $element['elements'][] = ['element' => 'td', 'content' => $invoice->number]; + $element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($refund_date, $this->client->date_format(), $this->client->locale()) ?: ' ']; + $element['elements'][] = ['element' => 'td', 'content' => ctrans('texts.refund')]; + $element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($payment->pivot->refunded, $this->client) ?: ' ']; + + $tbody[] = $element; + + $this->payment_amount_total -= $payment->pivot->refunded; + + } + + } + + } return [