Updates for cancelled invoice presentation

This commit is contained in:
David Bomba 2024-04-29 09:17:37 +10:00
parent aa649ac625
commit 086e82649b
3 changed files with 39 additions and 9 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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 = '<br><br>';
@ -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
*