Skip to content

Commit

Permalink
reconciled attribute should be seperated from paid
Browse files Browse the repository at this point in the history
  • Loading branch information
sevannerse committed Aug 27, 2021
1 parent d413e04 commit 7274c2d
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions app/Models/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Document extends Model

protected $table = 'documents';

protected $appends = ['attachment', 'amount_without_tax', 'discount', 'paid', 'received_at', 'status_label', 'sent_at'];
protected $appends = ['attachment', 'amount_without_tax', 'discount', 'paid', 'received_at', 'status_label', 'sent_at', 'reconciled'];

protected $dates = ['deleted_at', 'issued_at', 'due_at'];

Expand Down Expand Up @@ -267,7 +267,6 @@ public function getPaidAttribute()
}

$paid = 0;
$reconciled = $reconciled_amount = 0;

$code = $this->currency_code;
$rate = $this->currency_rate;
Expand All @@ -282,6 +281,36 @@ public function getPaidAttribute()
}

$paid += $amount;
}
}

return round($paid, $precision);
}

/**
* Get the reconcilation status.
*
* @return integer
*/
public function getReconciledAttribute()
{
if (empty($this->amount)) {
return 0;
}

$reconciled = $reconciled_amount = 0;

$code = $this->currency_code;
$rate = $this->currency_rate;
$precision = config('money.' . $code . '.precision');

if ($this->transactions->count()) {
foreach ($this->transactions as $transaction) {
$amount = $transaction->amount;

if ($code != $transaction->currency_code) {
$amount = $this->convertBetween($amount, $transaction->currency_code, $transaction->currency_rate, $code, $rate);
}

if ($transaction->reconciled) {
$reconciled_amount = +$amount;
Expand All @@ -293,9 +322,7 @@ public function getPaidAttribute()
$reconciled = 1;
}

$this->setAttribute('reconciled', $reconciled);

return round($paid, $precision);
return $reconciled;
}

/**
Expand Down

0 comments on commit 7274c2d

Please sign in to comment.