198 lines
4.6 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Expense;
use App\Models\Model;
use App\Traits\Currencies;
use App\Traits\DateTime;
2018-04-27 16:01:31 +03:00
use App\Traits\Media;
use App\Traits\Recurring;
2017-11-26 15:20:17 +03:00
use Bkwld\Cloner\Cloneable;
2017-09-14 22:21:00 +03:00
use Sofa\Eloquence\Eloquence;
2018-07-11 14:30:25 +03:00
use Date;
2017-09-14 22:21:00 +03:00
class Bill extends Model
{
2018-04-27 16:01:31 +03:00
use Cloneable, Currencies, DateTime, Eloquence, Media, Recurring;
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
*/
protected $appends = ['attachment', 'discount'];
2017-09-14 22:21:00 +03:00
protected $dates = ['deleted_at', 'billed_at', 'due_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2018-05-01 19:00:33 +03:00
protected $fillable = ['company_id', 'bill_number', 'order_number', 'bill_status_code', 'billed_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'vendor_name', 'vendor_email', 'vendor_tax_number', 'vendor_phone', 'vendor_address', 'notes', 'category_id', 'parent_id'];
2017-09-14 22:21:00 +03:00
/**
* Sortable columns.
*
* @var array
*/
2018-02-07 15:55:17 +03:00
public $sortable = ['bill_number', 'vendor_name', 'amount', 'status.name', 'billed_at', 'due_at', 'bill_status_code'];
2017-09-14 22:21:00 +03:00
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'bill_number' => 10,
'order_number' => 10,
'vendor_name' => 10,
'vendor_email' => 5,
'vendor_phone' => 2,
'vendor_address' => 1,
'notes' => 2,
];
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()
{
return $this->belongsTo('App\Models\Setting\Category');
}
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
{
2018-04-26 18:40:04 +03:00
return $this->hasMany('App\Models\Expense\BillHistory');
2017-09-14 22:21:00 +03:00
}
public function items()
{
return $this->hasMany('App\Models\Expense\BillItem');
}
2018-04-26 18:40:04 +03:00
public function payments()
{
2018-04-26 18:40:04 +03:00
return $this->hasMany('App\Models\Expense\BillPayment');
}
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 status()
2017-09-14 22:21:00 +03:00
{
2018-04-26 18:40:04 +03:00
return $this->belongsTo('App\Models\Expense\BillStatus', 'bill_status_code', 'code');
}
public function totals()
{
return $this->hasMany('App\Models\Expense\BillTotal');
}
public function vendor()
{
return $this->belongsTo('App\Models\Expense\Vendor');
2017-09-14 22:21:00 +03:00
}
2017-11-07 05:11:03 +03:00
public function scopeDue($query, $date)
{
return $query->where('due_at', '=', $date);
}
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
public function scopeAccrued($query)
{
2018-03-09 16:24:00 +03:00
return $query->where('bill_status_code', '<>', 'draft');
2017-11-07 05:11:03 +03:00
}
2018-03-09 22:00:17 +03:00
public function scopePaid($query)
{
return $query->where('bill_status_code', '=', 'paid');
}
public function scopeNotPaid($query)
{
return $query->where('bill_status_code', '<>', 'paid');
}
public function onCloning($src, $child = null)
{
$this->bill_status_code = 'draft';
}
2017-10-04 01:25:03 +03:00
/**
2017-10-21 14:23:57 +03:00
* Convert amount to double.
2017-10-04 01:25:03 +03:00
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) money($value, $this->attributes['currency_code'])->getAmount();
2017-10-04 01:25:03 +03:00
}
/**
2017-10-21 14:23:57 +03:00
* Convert currency rate to double.
2017-10-04 01:25:03 +03:00
*
* @param string $value
* @return void
*/
public function setCurrencyRateAttribute($value)
{
2017-10-21 14:23:57 +03:00
$this->attributes['currency_rate'] = (double) $value;
2017-10-04 01:25:03 +03:00
}
2018-01-02 18:20:52 +03:00
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
2018-01-02 18:20:52 +03:00
{
2018-01-10 18:21:12 +03:00
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
2018-01-10 18:21:12 +03:00
} elseif (!$this->hasMedia('attachment')) {
2018-01-02 18:20:52 +03:00
return false;
}
return $this->getMedia('attachment')->last();
}
2018-04-17 16:40:52 +03:00
/**
* Get the discount percentage.
*
* @return string
*/
public function getDiscountAttribute()
{
2018-04-18 14:34:33 +03:00
$percent = 0;
2018-04-17 16:40:52 +03:00
2018-04-18 14:34:33 +03:00
$discount = $this->totals()->where('code', 'discount')->value('amount');
2018-04-17 16:40:52 +03:00
2018-04-18 14:34:33 +03:00
if ($discount) {
$sub_total = $this->totals()->where('code', 'sub_total')->value('amount');
2018-04-17 16:40:52 +03:00
2018-04-18 14:34:33 +03:00
$percent = number_format((($discount * 100) / $sub_total), 0);
2018-04-17 16:40:52 +03:00
}
2018-04-18 14:34:33 +03:00
return $percent;
2018-04-17 16:40:52 +03:00
}
2017-09-14 22:21:00 +03:00
}