akaunting/app/Models/Common/Contact.php

147 lines
3.5 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Models\Common;
use App\Abstracts\Model;
use Bkwld\Cloner\Cloneable;
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;
use Illuminate\Notifications\Notifiable;
class Contact extends Model
{
2020-04-07 14:39:35 +03:00
use Cloneable, Contacts, Currencies, Media, Notifiable;
2019-11-16 10:21:14 +03:00
protected $table = 'contacts';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'type', 'name', 'email', 'user_id', 'tax_number', 'phone', 'address', 'website', 'currency_code', 'reference', 'enabled'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'email', 'phone', 'enabled'];
public function bills()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\Bill');
2019-11-16 10:21:14 +03:00
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function expense_transactions()
2019-11-16 10:21:14 +03:00
{
return $this->transactions()->where('type', 'expense');
2019-11-16 10:21:14 +03:00
}
public function income_transactions()
2019-11-16 10:21:14 +03:00
{
return $this->transactions()->where('type', 'income');
2019-11-16 10:21:14 +03:00
}
public function invoices()
2019-11-16 10:21:14 +03:00
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\Invoice');
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;
}
2020-02-22 00:30:45 +03:00
return $query->whereIn($this->table . '.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)
{
return $query->whereIn($this->table . '.type', (array) $this->getVendorTypes());
}
/**
* Scope to include only customers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCustomer($query)
{
return $query->whereIn($this->table . '.type', (array) $this->getCustomerTypes());
}
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;
$amount += $this->convertToDefault($unpaid, $item->currency_code, $item->currency_rate);
2019-11-16 10:21:14 +03:00
});
return $amount;
}
}