'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 getStatusAttribute()
    {
        if($this->status_id == Invoice::STATUS_SENT && $this->due_date > Carbon::now())
            return Invoice::STATUS_UNPAID;
        else if($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date > Carbon::now())
            return Invoice::STATUS_UNPAID;
        else if($this->status_id == Invoice::STATUS_SENT && $this->due_date < Carbon::now())
            return Invoice::STATUS_OVERDUE;
        else if($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date < Carbon::now())
            return Invoice::STATUS_OVERDUE;
        else
            return $this->status_id;
    }
    
    public function company()
    {
        return $this->belongsTo(Company::class);
    }
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    public function assigned_user()
    {
        return $this->belongsTo(User::class ,'assigned_user_id', 'id');
    }
    
    public function invitations()
    {
        return $this->hasMany(InvoiceInvitation::class);
    }
    public function client()
    {
        return $this->belongsTo(Client::class);
    }
    public function documents()
    {
        return $this->morphMany(Document::class, 'documentable');
    }
    public function payments()
    {
        return $this->morphToMany(Payment::class, 'paymentable');
    }
    public function company_ledger()
    {
        return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
    }
    /* ---------------- */
    /* Settings getters */
    /* ---------------- */
    /**
     * 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');
    }
    /**
     * 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));
    }
    public function markViewed() :void
    {
        $this->last_viewed = Carbon::now()->format('Y-m-d H:i');
        $this->save();
    }
    
    public function isPayable() : bool
    {
        if($this->status_id == Invoice::STATUS_SENT && $this->due_date > Carbon::now())
            return true;
        else if($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date > Carbon::now())
            return true;
        else if($this->status_id == Invoice::STATUS_SENT && $this->due_date < Carbon::now())
            return true;
        else if($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date < Carbon::now())
            return true;
        else
            return false;
    }
    public static function badgeForStatus(int $status)
    {
        switch ($status) {
            case Invoice::STATUS_DRAFT:
                return '
'.ctrans('texts.draft').'
';
                break;
            case Invoice::STATUS_SENT:
                return ''.ctrans('texts.sent').'
';
                break;
            case Invoice::STATUS_PARTIAL:
                return ''.ctrans('texts.partial').'
';
                break;
            case Invoice::STATUS_PAID:
                return ''.ctrans('texts.paid').'
';
                break;
            case Invoice::STATUS_CANCELLED:
                return ''.ctrans('texts.cancelled').'
';
                break;
            case Invoice::STATUS_OVERDUE:
                return ''.ctrans('texts.overdue').'
';
                break;
            case Invoice::STATUS_UNPAID:
                return ''.ctrans('texts.unpaid').'
';
                break;      
            case Invoice::STATUS_REVERSED:
                return ''.ctrans('texts.reversed').'
';
                break;           
            default:
                # code...
                break;
        }
    }
    /**
     * Returns the template for the invoice
     * 
     * @return string Either the template view, OR the template HTML string
     * @todo  this needs attention, invoice->settings needs clarification
     */
    public function design() :string
    {
        if($this->client->getSetting('design'))
            return File::exists(resource_path($this->client->getSetting('design'))) ? File::get(resource_path($this->client->getSetting('design'))) : File::get(resource_path('views/pdf/design1.blade.php'));
        else
            return File::get(resource_path('views/pdf/design1.blade.php'));
    }
    /**
     * 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, $this);
        
        return $invoice_calc->build();
    }
    /** TODO// DOCUMENT THIS FUNCTIONALITY */
    public function pdf_url()
    {
        $public_path = 'storage/' . $this->client->client_hash . '/invoices/'. $this->invoice_number . '.pdf';
        $storage_path = 'public/' . $this->client->client_hash . '/invoices/'. $this->invoice_number . '.pdf';
        if(!Storage::exists($storage_path)) {
            event(new InvoiceWasUpdated($this));
        }
        return $public_path;
    }
    public function pdf_file_path()
    {
        $storage_path = 'storage/' . $this->client->client_hash . '/invoices/'. $this->invoice_number . '.pdf';
        if(!Storage::exists($storage_path)) {
            CreateInvoicePdf::dispatchNow($this);
        }
        return $storage_path;        
    }
    /**
     * @param bool $save
     */
    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();
            }
        }
    }
    /**
     * @return bool
     */
    public function hasPartial() : bool
    {
        return ($this->partial && $this->partial > 0) === true;
    }
    /**
     * @return bool
     */
    public function isPartial() : bool
    {
        return $this->status_id >= self::STATUS_PARTIAL;
    }
    /**
     * Clear partial fields
     * @return void 
     */
    public function clearPartial() : void
    {
        $this->partial = null;
        $this->partial_due_date = null;
        $this->save();
    }
    /**
     * @param float $balance_adjustment
     */
    public function updateBalance($balance_adjustment)
    {
        if ($this->is_deleted) 
            return;
        
        $balance_adjustment = floatval($balance_adjustment);
        
        $this->balance = $this->balance + $balance_adjustment;
        if($this->balance == 0) {
            $this->status_id = self::STATUS_PAID;
            $this->save();
            event(new InvoiceWasPaid($this));
            return;
        }
        $this->save();
    }
    public function setDueDate()
    {
        $this->due_date = Carbon::now()->addDays(PaymentTerm::find($this->company->settings->payment_terms_id)->num_days);
        $this->save();
    }
    public function setStatus($status)
    {
        $this->status_id = $status;
        $this->save();
    }
    public function markSent()
    {
        /* Return immediately if status is not draft */
        if($this->status_id != Invoice::STATUS_DRAFT)
            return $this;
        $this->status_id = Invoice::STATUS_SENT;
        $this->markInvitationsSent();
        event(new InvoiceWasMarkedSent($this));
        UpdateClientBalance::dispatchNow($this->client, $this->balance);
        
        $this->save();
    }
    /**
     * Updates Invites to SENT
     *
     */
    private function markInvitationsSent()
    {
        $this->invitations->each(function($invitation) {
            if(!isset($invitation->sent_date))
            {
                $invitation->sent_date = Carbon::now();
                $invitation->save();
            }
        });
    }
}