belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]); } public function contact() { return $this->belongsTo('App\Models\Common\Contact')->withDefault(['name' => trans('general.na')]); } public function currency() { return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code'); } public function histories() { return $this->hasMany('App\Models\Purchase\BillHistory'); } public function items() { return $this->hasMany('App\Models\Purchase\BillItem'); } public function item_taxes() { return $this->hasMany('App\Models\Purchase\BillItemTax'); } public function recurring() { return $this->morphOne('App\Models\Common\Recurring', 'recurable'); } public function totals() { return $this->hasMany('App\Models\Purchase\BillTotal'); } public function transactions() { return $this->hasMany('App\Models\Banking\Transaction', 'document_id')->where('type', 'expense'); } public function scopeLatest($query) { return $query->orderBy('billed_at', 'desc'); } public function scopeNumber($query, $number) { return $query->where('bill_number', '=', $number); } public function onCloning($src, $child = null) { $this->status = 'draft'; $this->bill_number = $this->getNextBillNumber(); } public function getReceivedAtAttribute($value) { $received = $this->histories()->where('status', 'received')->first(); return ($received) ? $received->created_at : null; } }