104 lines
2.5 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace App\Models\Purchase;
2017-09-14 22:21:00 +03:00
2020-02-11 21:55:13 +03:00
use App\Abstracts\DocumentModel;
use App\Traits\Purchases;
2017-09-14 22:21:00 +03:00
2020-02-11 21:55:13 +03:00
class Bill extends DocumentModel
2017-09-14 22:21:00 +03:00
{
2020-02-11 21:55:13 +03:00
use Purchases;
2017-09-14 22:21:00 +03:00
protected $table = 'bills';
2018-04-17 16:40:52 +03:00
/**
* The accessors to append to the model's array form.
*
* @var array
*/
2020-01-11 16:57:32 +03:00
protected $appends = ['attachment', 'amount_without_tax', 'discount', 'paid', 'status_label'];
2018-04-17 16:40:52 +03:00
2017-09-14 22:21:00 +03:00
protected $dates = ['deleted_at', 'billed_at', 'due_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2020-01-11 16:57:32 +03:00
protected $fillable = ['company_id', 'bill_number', 'order_number', 'status', 'billed_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'contact_id', 'contact_name', 'contact_email', 'contact_tax_number', 'contact_phone', 'contact_address', 'notes', 'category_id', 'parent_id'];
2017-09-14 22:21:00 +03:00
/**
* Sortable columns.
*
* @var array
*/
2020-01-11 16:57:32 +03:00
public $sortable = ['bill_number', 'contact_name', 'amount', 'status', 'billed_at', 'due_at'];
2017-09-14 22:21:00 +03:00
2017-11-26 15:20:17 +03:00
/**
* Clonable relationships.
*
* @var array
*/
2018-05-01 19:00:33 +03:00
public $cloneable_relations = ['items', 'recurring', 'totals'];
2017-11-26 15:20:17 +03:00
2018-04-23 22:17:20 +03:00
public function category()
{
2019-11-16 10:21:14 +03:00
return $this->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')]);
2018-04-23 22:17:20 +03:00
}
2017-09-14 22:21:00 +03:00
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
2018-04-26 18:40:04 +03:00
public function histories()
2017-09-14 22:21:00 +03:00
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\BillHistory');
2017-09-14 22:21:00 +03:00
}
public function items()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\BillItem');
2017-09-14 22:21:00 +03:00
}
2019-01-07 18:38:34 +03:00
public function item_taxes()
2018-10-22 16:57:35 +03:00
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\BillItemTax');
2018-10-22 16:57:35 +03:00
}
2018-04-26 18:40:04 +03:00
public function recurring()
2017-09-14 22:21:00 +03:00
{
2018-04-27 17:42:45 +03:00
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
2017-09-14 22:21:00 +03:00
}
2018-04-26 18:40:04 +03:00
public function totals()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\BillTotal');
2018-04-26 18:40:04 +03:00
}
2019-11-16 10:21:14 +03:00
public function transactions()
2018-04-26 18:40:04 +03:00
{
2020-01-16 17:08:26 +03:00
return $this->hasMany('App\Models\Banking\Transaction', 'document_id')->where('type', 'expense');
2017-09-14 22:21:00 +03:00
}
2017-11-07 05:11:03 +03:00
public function scopeLatest($query)
{
2019-11-16 10:21:14 +03:00
return $query->orderBy('billed_at', 'desc');
2017-11-07 05:11:03 +03:00
}
2020-01-20 23:50:29 +03:00
public function scopeNumber($query, $number)
{
return $query->where('bill_number', '=', $number);
}
public function onCloning($src, $child = null)
{
2020-01-11 16:57:32 +03:00
$this->status = 'draft';
}
2017-09-14 22:21:00 +03:00
}