2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Income;
|
|
|
|
|
|
|
|
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 Illuminate\Notifications\Notifiable;
|
|
|
|
use Sofa\Eloquence\Eloquence;
|
|
|
|
|
|
|
|
class Customer extends Model
|
|
|
|
{
|
2018-10-10 18:18:15 +03:00
|
|
|
use Cloneable, Currencies, Eloquence, Notifiable;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
protected $table = 'customers';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-09-26 18:05:54 +03:00
|
|
|
protected $fillable = ['company_id', 'user_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 invoices()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Income\Invoice');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function revenues()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Income\Revenue');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function currency()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
2017-12-11 00:14:24 +03:00
|
|
|
return $this->belongsTo('App\Models\Auth\User', 'user_id', 'id');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
2017-11-26 15:20:17 +03:00
|
|
|
|
|
|
|
public function onCloning($src, $child = null)
|
|
|
|
{
|
|
|
|
$this->user_id = null;
|
|
|
|
}
|
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
|
|
|
$invoices = $this->invoices()->accrued()->notPaid()->get();
|
2018-10-09 23:15:47 +03:00
|
|
|
|
2018-10-10 18:04:10 +03:00
|
|
|
foreach ($invoices as $invoice) {
|
|
|
|
$invoice_amount = $invoice->amount - $invoice->paid;
|
2018-10-10 18:18:15 +03:00
|
|
|
|
2018-10-10 18:04:10 +03:00
|
|
|
$amount += $this->dynamicConvert(setting('general.default_currency'), $invoice_amount, $invoice->currency_code, $invoice->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
|
|
|
}
|