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;
|
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-01-02 18:20:52 +03:00
|
|
|
use Plank\Mediable\Mediable;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Bill extends Model
|
|
|
|
{
|
2018-01-02 18:20:52 +03:00
|
|
|
use Cloneable, Currencies, DateTime, Eloquence, Mediable;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
protected $table = 'bills';
|
|
|
|
|
|
|
|
protected $dates = ['deleted_at', 'billed_at', 'due_at'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-09 01:10:43 +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'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sortable columns.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $sortable = ['bill_number', 'vendor_name', 'amount', 'status.name', 'billed_at', 'due_at'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-11-26 19:45:49 +03:00
|
|
|
protected $cloneable_relations = ['items', 'totals'];
|
2017-11-26 15:20:17 +03:00
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
public function vendor()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Expense\Vendor');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function currency()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function status()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Expense\BillStatus', 'bill_status_code', 'code');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function items()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Expense\BillItem');
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:52:35 +03:00
|
|
|
public function totals()
|
|
|
|
{
|
2017-10-13 18:04:38 +03:00
|
|
|
return $this->hasMany('App\Models\Expense\BillTotal');
|
2017-10-11 19:52:35 +03:00
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
public function payments()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Expense\BillPayment');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function histories()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Expense\BillHistory');
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return $query->where('bill_status_code', '!=', 'new');
|
|
|
|
}
|
|
|
|
|
2017-11-26 19:45:49 +03:00
|
|
|
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)
|
|
|
|
{
|
2017-10-21 14:23:57 +03:00
|
|
|
$this->attributes['amount'] = (double) $value;
|
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
|
|
|
|
*/
|
2018-01-03 20:08:56 +03:00
|
|
|
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')) {
|
2018-01-03 20:08:56 +03:00
|
|
|
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();
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|