'date:Y-m-d', // 'due_date' => 'date:Y-m-d', // 'partial_due_date' => 'date:Y-m-d', 'line_items' => 'object', 'backup' => 'object', 'updated_at' => 'timestamp', 'created_at' => 'timestamp', 'deleted_at' => 'timestamp', ]; protected $with = []; 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_REVERSED = 6; const STATUS_OVERDUE = -1; //status < 4 || < 3 && !is_deleted && !trashed() && due_date < now() const STATUS_UNPAID = -2; //status < 4 || < 3 && !is_deleted && !trashed() public function getEntityType() { return self::class; } public function getDateAttribute($value) { return $this->dateMutator($value); } public function getDueDateAttribute($value) { return $this->dateMutator($value); } public function getPartialDueDateAttribute($value) { return $this->dateMutator($value); } 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 contacts() // { // return $this->hasManyThrough(ClientContact::class, Client::class); // } public function documents() { return $this->morphMany(Document::class, 'documentable'); } public function payments() { return $this->morphToMany(Payment::class, 'paymentable')->withPivot('amount', 'refunded')->withTimestamps()->withTrashed(); } public function company_ledger() { return $this->morphMany(CompanyLedger::class, 'company_ledgerable'); } public function activities() { return $this->hasMany(Activity::class); } public function history() { return $this->hasManyThrough(Backup::class, Activity::class); } public function credits() { return $this->hasMany(Credit::class); } // 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 ledger() { return new LedgerService($this); } /* ---------------- */ /* Settings getters */ /* ---------------- */ public function getStatusAttribute() { if ($this->status_id == self::STATUS_SENT && $this->due_date > Carbon::now()) { return self::STATUS_UNPAID; } elseif ($this->status_id == self::STATUS_PARTIAL && $this->partial_due_date > Carbon::now()) { return self::STATUS_UNPAID; } elseif ($this->status_id == self::STATUS_SENT && $this->due_date < Carbon::now()) { return self::STATUS_OVERDUE; } elseif ($this->status_id == self::STATUS_PARTIAL && $this->partial_due_date < Carbon::now()) { return self::STATUS_OVERDUE; } else { return $this->status_id; } } public function isPayable(): bool { if ($this->status_id == self::STATUS_DRAFT && $this->is_deleted == false) { return true; } elseif ($this->status_id == self::STATUS_SENT && $this->is_deleted == false) { return true; } elseif ($this->status_id == self::STATUS_PARTIAL && $this->is_deleted == false) { return true; } elseif ($this->status_id == self::STATUS_SENT && $this->is_deleted == false) { return true; } elseif ($this->status_id == self::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 self::STATUS_DRAFT: return '
'.ctrans('texts.draft').'
'; break; case self::STATUS_SENT: return '
'.ctrans('texts.sent').'
'; break; case self::STATUS_PARTIAL: return '
'.ctrans('texts.partial').'
'; break; case self::STATUS_PAID: return '
'.ctrans('texts.paid').'
'; break; case self::STATUS_CANCELLED: return '
'.ctrans('texts.cancelled').'
'; break; case self::STATUS_OVERDUE: return '
'.ctrans('texts.overdue').'
'; break; case self::STATUS_UNPAID: return '
'.ctrans('texts.unpaid').'
'; break; case self::STATUS_REVERSED: return '
'.ctrans('texts.reversed').'
'; break; default: // code... break; } } public static function stringStatus(int $status) { switch ($status) { case self::STATUS_DRAFT: return ctrans('texts.draft'); break; case self::STATUS_SENT: return ctrans('texts.sent'); break; case self::STATUS_PARTIAL: return ctrans('texts.partial'); break; case self::STATUS_PAID: return ctrans('texts.paid'); break; case self::STATUS_CANCELLED: return ctrans('texts.cancelled'); break; case self::STATUS_OVERDUE: return ctrans('texts.overdue'); break; case self::STATUS_UNPAID: return ctrans('texts.unpaid'); break; case self::STATUS_REVERSED: return ctrans('texts.reversed'); break; default: // code... break; } } /** * Access the invoice calculator object. * * @return object The invoice calculator object getters */ public function calc() { $invoice_calc = null; if ($this->uses_inclusive_taxes) { $invoice_calc = new InvoiceSumInclusive($this); } else { $invoice_calc = new InvoiceSum($this); } return $invoice_calc->build(); } public function pdf_file_path($invitation = null) { if (! $invitation) { $invitation = $this->invitations->first(); } $storage_path = Storage::url($this->client->invoice_filepath().$this->number.'.pdf'); if (! Storage::exists($this->client->invoice_filepath().$this->number.'.pdf')) { event(new InvoiceWasUpdated($this, $this->company, Ninja::eventVars())); CreateInvoicePdf::dispatchNow($invitation); } return $storage_path; } /** * Updates Invites to SENT. */ public function markInvitationsSent() { $this->invitations->each(function ($invitation) { if (! isset($invitation->sent_date)) { $invitation->sent_date = Carbon::now(); $invitation->save(); } }); } /** * Filtering logic to determine * whether an invoice is locked * based on the current status of the invoice. * @return bool [description] */ public function isLocked() :bool { $locked_status = $this->client->getSetting('lock_invoices'); switch ($locked_status) { case 'off': return false; break; case 'when_sent': return $this->status_id == self::STATUS_DRAFT; break; case 'when_paid': return $this->status_id == self::STATUS_PAID || $this->status_id == self::STATUS_PARTIAL; break; default: return false; break; } } /* Graveyard */ // /** // * Determines if invoice overdue. // * // * @param float $balance The balance // * @param date. $due_date The due date // * // * @return boolean True if overdue, False otherwise. // */ // public static function isOverdue($balance, $due_date) // { // if (! $this->formatValue($balance,2) > 0 || ! $due_date) { // return false; // } // // // it isn't considered overdue until the end of the day // return strtotime($this->createClientDate(date(), $this->client->timezone()->name)) > (strtotime($due_date) + (60 * 60 * 24)); // } /** * @param bool $save * * Has this been dragged from V1? */ // public function updatePaidStatus($paid = false, $save = true) : bool // { // $status_id = false; // if ($paid && $this->balance == 0) { // $status_id = self::STATUS_PAID; // } elseif ($paid && $this->balance > 0 && $this->balance < $this->amount) { // $status_id = self::STATUS_PARTIAL; // } elseif ($this->hasPartial() && $this->balance > 0) { // $status_id = ($this->balance == $this->amount ? self::STATUS_SENT : self::STATUS_PARTIAL); // } // if ($status_id && $status_id != $this->status_id) { // $this->status_id = $status_id; // if ($save) { // $this->save(); // } // } // } public function getBalanceDueAttribute() { return $this->balance; } public function getTotalAttribute() { return $this->calc()->getTotal(); } }