'object', 'updated_at' => 'timestamp', 'created_at' => 'timestamp', 'deleted_at' => 'timestamp', ]; protected $with = [ 'company', 'client', ]; protected $appends = [ 'hashed_id', 'status' ]; const STATUS_DRAFT = 1; const STATUS_SENT = 2; const STATUS_PARTIAL = 3; const STATUS_PAID = 4; const STATUS_CANCELLED = 5; const STATUS_OVERDUE = -1; const STATUS_UNPAID = -2; const STATUS_REVERSED = -3; public function company() { return $this->belongsTo(Company::class); } public function user() { return $this->belongsTo(User::class)->withTrashed(); } public function assigned_user() { return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed(); } public function invitations() { return $this->hasMany(InvoiceInvitation::class); } public function client() { return $this->belongsTo(Client::class)->withTrashed(); } public function documents() { return $this->morphMany(Document::class, 'documentable'); } public function payments() { return $this->morphToMany(Payment::class, 'paymentable')->withPivot('amount','refunded')->withTimestamps();; } public function company_ledger() { return $this->morphMany(CompanyLedger::class, 'company_ledgerable'); } public function credits() { return $this->belongsToMany(Credit::class)->using(Paymentable::class)->withPivot('amount','refunded')->withTimestamps();; } /** * Service entry points */ public function service() :InvoiceService { return new InvoiceService($this); } public function markPaid() :InvoiceService { return $this->service()->markPaid(); } public function applyNumber() :InvoiceService { return $this->service()->applyNumber(); } public function applyPayment($payment, $payment_amount) :InvoiceService { return $this->service()->applyPayment($payment, $payment_amount); } public function updateBalance($balance_adjustment) :InvoiceService { return $this->service()->updateBalance($balance_adjustment); } public function setDueDate() :InvoiceService { return $this->service->setDueDate(); } public function setStatus($status) :InvoiceService { return $this->service()->setStatus($status); } public function clearPartial() :InvoiceService { return $this->service()->clearPartial(); } public function updatePartial($amount) :InvoiceService { return $this->service()->updatePartial($amount); } public function markSent() :InvoiceService { return $this->service()->markSent(); } public function markViewed() :InvoiceService { return $this->service()->markViewed(); } /* ---------------- */ /* Settings getters */ /* ---------------- */ public function getStatusAttribute() { if ($this->status_id == Invoice::STATUS_SENT && $this->due_date > Carbon::now()) { return Invoice::STATUS_UNPAID; } elseif ($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date > Carbon::now()) { return Invoice::STATUS_UNPAID; } elseif ($this->status_id == Invoice::STATUS_SENT && $this->due_date < Carbon::now()) { return Invoice::STATUS_OVERDUE; } elseif ($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date < Carbon::now()) { return Invoice::STATUS_OVERDUE; } else { return $this->status_id; } } /** * If True, prevents an invoice from being * modified once it has been marked as sent * * @return boolean isLocked */ public function isLocked() : bool { return $this->client->getSetting('lock_sent_invoices'); } public function isPayable() : bool { if ($this->status_id == Invoice::STATUS_SENT && $this->is_deleted == false) { return true; } elseif ($this->status_id == Invoice::STATUS_PARTIAL && $this->is_deleted == false) { return true; } elseif ($this->status_id == Invoice::STATUS_SENT && $this->is_deleted == false) { return true; } elseif ($this->status_id == Invoice::STATUS_DRAFT && $this->is_deleted == false) { return true; } else { return false; } } public function isRefundable() : bool { if($this->is_deleted) return false; if(($this->amount - $this->balance) == 0) return false; return true; } /** * @return bool */ public function isPartial() : bool { return $this->status_id >= self::STATUS_PARTIAL; } /** * @return bool */ public function hasPartial() : bool { return ($this->partial && $this->partial > 0) === true; } public static function badgeForStatus(int $status) { switch ($status) { case Invoice::STATUS_DRAFT: return '