2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Model;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Models\Document\Document;
|
2020-12-26 16:13:34 +03:00
|
|
|
use App\Scopes\Contact as Scope;
|
2020-04-07 14:39:35 +03:00
|
|
|
use App\Traits\Contacts;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Traits\Currencies;
|
|
|
|
use App\Traits\Media;
|
2020-08-26 15:14:16 +03:00
|
|
|
use App\Traits\Transactions;
|
2020-12-26 16:13:34 +03:00
|
|
|
use Bkwld\Cloner\Cloneable;
|
2020-10-14 17:07:59 +03:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2019-11-16 10:21:14 +03:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
|
|
|
|
class Contact extends Model
|
|
|
|
{
|
2020-10-14 17:07:59 +03:00
|
|
|
use Cloneable, Contacts, Currencies, HasFactory, Media, Notifiable, Transactions;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
protected $table = 'contacts';
|
|
|
|
|
2021-09-03 12:53:35 +03:00
|
|
|
/**
|
|
|
|
* The accessors to append to the model's array form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $appends = ['location'];
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-06-17 10:59:07 +03:00
|
|
|
protected $fillable = [
|
|
|
|
'company_id',
|
|
|
|
'type',
|
|
|
|
'name',
|
|
|
|
'email',
|
|
|
|
'user_id',
|
|
|
|
'tax_number',
|
|
|
|
'phone',
|
|
|
|
'address',
|
2021-09-03 11:43:55 +03:00
|
|
|
'city',
|
|
|
|
'zip_code',
|
|
|
|
'state',
|
|
|
|
'country',
|
2021-06-17 10:59:07 +03:00
|
|
|
'website',
|
|
|
|
'currency_code',
|
|
|
|
'reference',
|
|
|
|
'enabled',
|
2021-09-07 10:33:34 +03:00
|
|
|
'created_from',
|
2021-06-17 10:59:07 +03:00
|
|
|
'created_by',
|
|
|
|
];
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-11-13 15:15:27 +03:00
|
|
|
/**
|
|
|
|
* The attributes that should be cast.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'enabled' => 'boolean',
|
|
|
|
];
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
/**
|
|
|
|
* Sortable columns.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $sortable = ['name', 'email', 'phone', 'enabled'];
|
|
|
|
|
2020-12-26 16:13:34 +03:00
|
|
|
/**
|
2021-01-19 17:27:19 +03:00
|
|
|
* The "booted" method of the model.
|
2020-12-26 16:13:34 +03:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2021-01-19 17:27:19 +03:00
|
|
|
protected static function booted()
|
2020-12-26 16:13:34 +03:00
|
|
|
{
|
2021-01-21 14:37:10 +03:00
|
|
|
parent::booted();
|
|
|
|
|
2020-12-26 16:13:34 +03:00
|
|
|
static::addGlobalScope(new Scope);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
public function documents()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Document\Document');
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function bills()
|
|
|
|
{
|
2020-12-24 01:28:38 +03:00
|
|
|
return $this->documents()->where('type', Document::BILL_TYPE);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function currency()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function expense_transactions()
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2020-08-26 15:14:16 +03:00
|
|
|
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function income_transactions()
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2020-08-26 15:14:16 +03:00
|
|
|
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function invoices()
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2020-12-24 01:28:38 +03:00
|
|
|
return $this->documents()->where('type', Document::INVOICE_TYPE);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function transactions()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Banking\Transaction');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Auth\User', 'user_id', 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope to only include contacts of a given type.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @param mixed $types
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeType($query, $types)
|
|
|
|
{
|
|
|
|
if (empty($types)) {
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->whereIn($this->qualifyColumn('type'), (array) $types);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:39:35 +03:00
|
|
|
/**
|
|
|
|
* Scope to include only vendors.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeVendor($query)
|
|
|
|
{
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->whereIn($this->qualifyColumn('type'), (array) $this->getVendorTypes());
|
2020-04-07 14:39:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope to include only customers.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeCustomer($query)
|
|
|
|
{
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->whereIn($this->qualifyColumn('type'), (array) $this->getCustomerTypes());
|
2020-04-07 14:39:35 +03:00
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:49 +03:00
|
|
|
public function scopeEmail($query, $email)
|
|
|
|
{
|
|
|
|
return $query->where('email', '=', $email);
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function onCloning($src, $child = null)
|
|
|
|
{
|
2020-04-18 14:33:03 +03:00
|
|
|
$this->email = null;
|
2019-11-16 10:21:14 +03:00
|
|
|
$this->user_id = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUnpaidAttribute()
|
|
|
|
{
|
|
|
|
$amount = 0;
|
|
|
|
|
2020-06-08 23:09:43 +03:00
|
|
|
$collection = $this->isCustomer() ? 'invoices' : 'bills';
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-08 22:42:38 +03:00
|
|
|
$this->$collection->whereNotIn('status', ['draft', 'cancelled', 'paid'])->each(function ($item) use (&$amount) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$unpaid = $item->amount - $item->paid;
|
|
|
|
|
2020-04-10 10:38:03 -06:00
|
|
|
$amount += $this->convertToDefault($unpaid, $item->currency_code, $item->currency_rate);
|
2019-11-16 10:21:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
2020-10-14 17:07:59 +03:00
|
|
|
|
2021-09-03 11:43:55 +03:00
|
|
|
public function getLocationAttribute()
|
|
|
|
{
|
|
|
|
$location = [];
|
|
|
|
|
|
|
|
if ($this->city) {
|
|
|
|
$location[] = $this->city;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->zip_code) {
|
|
|
|
$location[] = $this->zip_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->state) {
|
|
|
|
$location[] = $this->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->country) {
|
|
|
|
$location[] = trans('countries.' . $this->country);
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(', ', $location);
|
|
|
|
}
|
|
|
|
|
2020-10-14 17:07:59 +03:00
|
|
|
/**
|
|
|
|
* Create a new factory instance for the model.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Factories\Factory
|
|
|
|
*/
|
|
|
|
protected static function newFactory()
|
|
|
|
{
|
|
|
|
return \Database\Factories\Contact::new();
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|