akaunting/app/Models/Purchase/Bill.php

123 lines
3.1 KiB
PHP
Raw Normal View History

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