Rework payment tables, enable payment editing

This commit is contained in:
Deon George
2021-07-23 17:25:26 +10:00
parent fa62a47680
commit d463239b17
19 changed files with 352 additions and 116 deletions

View File

@@ -106,7 +106,7 @@ class Invoice extends Model implements IDs
*/
public function getDueAttribute(): float
{
return sprintf('%4.'.$this->currency()->rounding.'f',$this->total-$this->paid);
return sprintf('%3.2f',$this->getTotalAttribute()-$this->getPaidAttribute());
}
/**
@@ -162,28 +162,27 @@ class Invoice extends Model implements IDs
*/
public function getPaidAttribute(): float
{
if (! $this->_paid)
$this->_paid = $this->currency()->round(
$this->paymentitems
->filter(function($item) { return ! $item->payment->pending_status; })
->sum('alloc_amt'));
return $this->_paid;
return $this->paymentitems
->filter(function($item) { return ! $item->payment->pending_status; })
->sum('alloc_amt');
}
/**
* Get the date that the invoice was paid in full.
* We assume the last payment received pays it in full.
* We assume the last payment received pays it in full, if its fully paid.
*
* @return Carbon|null
*/
public function getPaidDateAttribute(): ?Carbon
{
if ($this->getDueAttribute())
return NULL;
$o = $this->payments
->filter(function($item) { return ! $item->pending_status; })
->last();
return $o ? $o->date_payment : NULL;
return $o ? $o->payment_date : NULL;
}
/**
@@ -193,10 +192,9 @@ class Invoice extends Model implements IDs
*/
public function getPaidPendingAttribute(): float
{
return $this->currency()->round(
$this->paymentitems
->filter(function($item) { return $item->payment->pending_status; })
->sum('alloc_amt'));
return $this->paymentitems
->filter(function($item) { return $item->payment->pending_status; })
->sum('alloc_amt');
}
/**
@@ -238,7 +236,7 @@ class Invoice extends Model implements IDs
*/
public function getTotalSubAttribute(): float
{
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total-$this->tax_total);
return $this->items->where('active',TRUE)->sum('sub_total');
}
/**
@@ -259,29 +257,17 @@ class Invoice extends Model implements IDs
*/
public function getTotalTaxAttribute(): float
{
if (! $this->_total_tax)
foreach ($this->items as $o) {
if ($o->active)
$this->_total_tax += $this->currency()->round($o->tax);
}
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total_tax);
return $this->items->where('active',TRUE)->sum('tax');
}
/**
* Invoice total due
*
* @return string
* @return float
*/
public function getTotalAttribute(): float
{
if (! $this->_total)
foreach ($this->items as $o) {
if ($o->active)
$this->_total += $this->currency()->round($o->total);
}
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total);
return $this->getTotalSubAttribute()+$this->getTotalTaxAttribute();
}
public function currency()