diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index ee91bc8afab1..97079eb42416 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -194,13 +194,17 @@ class Invoice extends EntityModel implements BalanceAffecting public function updatePaidStatus($save = true) { - if ($this->isPaid() && $this->balance > 0) { - $this->invoice_status_id = ($this->balance == $this->amount ? INVOICE_STATUS_SENT : INVOICE_STATUS_PARTIAL); - if ($save) { - $this->save(); - } - } elseif ($this->invoice_status_id && $this->amount > 0 && $this->balance == 0 && $this->invoice_status_id != INVOICE_STATUS_PAID) { - $this->invoice_status_id = INVOICE_STATUS_PAID; + $statusId = false; + if ($this->amount > 0 && $this->balance == 0) { + $statusId = INVOICE_STATUS_PAID; + } elseif ($this->balance > 0 && $this->balance < $this->amount) { + $statusId = INVOICE_STATUS_PARTIAL; + } elseif ($this->isPartial() && $this->balance > 0) { + $statusId = ($this->balance == $this->amount ? INVOICE_STATUS_SENT : INVOICE_STATUS_PARTIAL); + } + + if ($statusId && $statusId != $this->invoice_status_id) { + $this->invoice_status_id = $statusId; if ($save) { $this->save(); } @@ -263,6 +267,11 @@ class Invoice extends EntityModel implements BalanceAffecting return $this->invoice_status_id >= INVOICE_STATUS_VIEWED; } + public function isPartial() + { + return $this->invoice_status_id >= INVOICE_STATUS_PARTIAL; + } + public function isPaid() { return $this->invoice_status_id >= INVOICE_STATUS_PAID;