2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Expense;
|
|
|
|
|
|
|
|
use App\Models\Model;
|
2017-11-26 15:20:17 +03:00
|
|
|
use Bkwld\Cloner\Cloneable;
|
2018-10-10 18:18:15 +03:00
|
|
|
use App\Traits\Currencies;
|
2017-09-14 22:21:00 +03:00
|
|
|
use Sofa\Eloquence\Eloquence;
|
2018-03-06 17:22:59 +03:00
|
|
|
use App\Traits\Media;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Vendor extends Model
|
|
|
|
{
|
2018-10-10 18:18:15 +03:00
|
|
|
use Cloneable, Currencies, Eloquence, Media;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
protected $table = 'vendors';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-09-26 18:05:54 +03:00
|
|
|
protected $fillable = ['company_id', 'name', 'email', 'tax_number', 'phone', 'address', 'website', 'currency_code', 'reference', 'enabled'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sortable columns.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $sortable = ['name', 'email', 'phone', 'enabled'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Searchable rules.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $searchableColumns = [
|
|
|
|
'name' => 10,
|
|
|
|
'email' => 5,
|
|
|
|
'phone' => 2,
|
|
|
|
'website' => 2,
|
|
|
|
'address' => 1,
|
|
|
|
];
|
|
|
|
|
|
|
|
public function bills()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Expense\Bill');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function payments()
|
|
|
|
{
|
2017-10-28 01:57:49 +03:00
|
|
|
return $this->hasMany('App\Models\Expense\Payment');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function currency()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
|
|
|
|
}
|
2018-03-06 17:22:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current balance.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLogoAttribute($value)
|
|
|
|
{
|
|
|
|
if (!empty($value) && !$this->hasMedia('logo')) {
|
|
|
|
return $value;
|
|
|
|
} elseif (!$this->hasMedia('logo')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getMedia('logo')->last();
|
|
|
|
}
|
2018-10-09 23:15:47 +03:00
|
|
|
|
2018-10-10 18:04:10 +03:00
|
|
|
public function getUnpaidAttribute()
|
2018-10-09 23:15:47 +03:00
|
|
|
{
|
2018-10-10 18:04:10 +03:00
|
|
|
$amount = 0;
|
2018-10-10 18:18:15 +03:00
|
|
|
|
2018-10-10 18:04:10 +03:00
|
|
|
$bills = $this->bills()->accrued()->notPaid()->get();
|
2018-10-09 23:15:47 +03:00
|
|
|
|
2018-10-10 18:04:10 +03:00
|
|
|
foreach ($bills as $bill) {
|
|
|
|
$bill_amount = $bill->amount - $bill->paid;
|
2018-10-10 18:18:15 +03:00
|
|
|
|
2018-10-10 18:04:10 +03:00
|
|
|
$amount += $this->dynamicConvert(setting('general.default_currency'), $bill_amount, $bill->currency_code, $bill->currency_rate, false);
|
|
|
|
}
|
2018-10-10 18:18:15 +03:00
|
|
|
|
2018-10-10 18:04:10 +03:00
|
|
|
return $amount;
|
2018-10-09 23:15:47 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|