diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index b05a7b9c8ea6..75a6139d92a0 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -245,12 +245,11 @@ class InvoiceSum */ private function setCalculatedAttributes(): self { - /* If amount != balance then some money has been paid on the invoice, need to subtract this difference from the total to set the new balance */ - - if ($this->invoice->status_id != Invoice::STATUS_DRAFT) { + if($this->invoice->status_id == Invoice::STATUS_CANCELLED){ + $this->invoice->balance = 0; + } + elseif ($this->invoice->status_id != Invoice::STATUS_DRAFT) { if ($this->invoice->amount != $this->invoice->balance) { - // $paid_to_date = $this->invoice->amount - $this->invoice->balance; - $this->invoice->balance = Number::roundValue($this->getTotal(), $this->precision) - $this->invoice->paid_to_date; //21-02-2024 cannot use the calculated $paid_to_date here as it could send the balance backward. } else { $this->invoice->balance = Number::roundValue($this->getTotal(), $this->precision); diff --git a/app/Helpers/Invoice/InvoiceSumInclusive.php b/app/Helpers/Invoice/InvoiceSumInclusive.php index 93990217a2f5..aa2dcdcfa95e 100644 --- a/app/Helpers/Invoice/InvoiceSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceSumInclusive.php @@ -279,10 +279,11 @@ class InvoiceSumInclusive private function setCalculatedAttributes() { /* If amount != balance then some money has been paid on the invoice, need to subtract this difference from the total to set the new balance */ - if ($this->invoice->status_id != Invoice::STATUS_DRAFT) { + if($this->invoice->status_id == Invoice::STATUS_CANCELLED){ + $this->invoice->balance = 0; + } + elseif ($this->invoice->status_id != Invoice::STATUS_DRAFT) { if ($this->invoice->amount != $this->invoice->balance) { - // $paid_to_date = $this->invoice->amount - $this->invoice->balance; - $this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision) - $this->invoice->paid_to_date; } else { $this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision); diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 226f82490222..c3946d619309 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -720,7 +720,7 @@ class HtmlEngine $data['$payment.date'] = ['value' => '', 'label' => ctrans('texts.payment_date')]; $data['$payment.number'] = ['value' => '', 'label' => ctrans('texts.payment_number')]; $data['$payment.transaction_reference'] = ['value' => '', 'label' => ctrans('texts.transaction_reference')]; - + $data['$payment.refunded'] = ['value' => '', 'label' => ctrans('texts.refund')]; if ($this->entity_string == 'invoice' && $this->entity->net_payments()->exists()) { $payment_list = '

'; @@ -742,6 +742,7 @@ class HtmlEngine $data['$payment.date'] = ['value' => $this->formatDate($payment->date, $this->client->date_format()), 'label' => ctrans('texts.payment_date')]; $data['$payment.number'] = ['value' => $payment->number, 'label' => ctrans('texts.payment_number')]; $data['$payment.transaction_reference'] = ['value' => $payment->transaction_reference, 'label' => ctrans('texts.transaction_reference')]; + $data['$payment.refunded'] = ['value' => $this->getPaymentMeta($payment), 'label' => ctrans('texts.refund')]; } @@ -755,6 +756,35 @@ class HtmlEngine return $data; } + private function getPaymentMeta(\App\Models\Payment $payment) { + + if(!is_array($payment->refund_meta)) + return ''; + + return + collect($payment->refund_meta) + ->map(function ($refund) use ($payment) { + + $date = \Carbon\Carbon::parse($refund['date'] ?? $payment->date)->addSeconds($payment->client->timezone_offset()); + $date = $this->translateDate($date, $payment->client->date_format(), $payment->client->locale()); + $entity = ctrans('texts.invoice'); + + $map = []; + + foreach($refund['invoices'] as $refunded_invoice) { + $invoice = \App\Models\Invoice::withTrashed()->find($refunded_invoice['invoice_id']); + $amount = Number::formatMoney($refunded_invoice['amount'], $payment->client); + $notes = ctrans('texts.status_partially_refunded_amount', ['amount' => $amount]); + + array_push($map, "{$date} {$entity} #{$invoice->number} {$notes}\n"); + + } + + return $map; + + })->flatten()->implode("\n"); + + } /** * Returns a localized string for tax compliance purposes *