akaunting/app/Models/Common/Contact.php

375 lines
10 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Models\Common;
2022-06-01 10:15:55 +03:00
use App\Traits\Media;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Model;
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\Documents;
2020-08-26 15:14:16 +03:00
use App\Traits\Transactions;
2022-06-01 10:15:55 +03:00
use App\Scopes\Contact as Scope;
use App\Models\Document\Document;
use App\Utilities\Date;
use App\Utilities\Str;
2020-12-26 16:13:34 +03:00
use Bkwld\Cloner\Cloneable;
2019-11-16 10:21:14 +03:00
use Illuminate\Notifications\Notifiable;
2022-06-01 10:15:55 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2019-11-16 10:21:14 +03:00
class Contact extends Model
{
use Cloneable, Contacts, Currencies, Documents, HasFactory, Media, Notifiable, Transactions;
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
public const CUSTOMER_TYPE = 'customer';
public const VENDOR_TYPE = 'vendor';
public const EMPLOYEE_TYPE = 'employee';
2019-11-16 10:21:14 +03:00
protected $table = 'contacts';
2023-05-31 10:02:11 +03:00
/**
* The relationships that should always be loaded.
*
* @var array
*/
protected $with = ['media'];
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',
'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
/**
* 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');
}
public function document_recurring()
{
return $this->documents()->whereIn('documents.type', $this->getRecurringDocumentTypes());
}
2019-11-16 10:21:14 +03:00
public function bills()
{
2022-06-10 19:26:32 +03:00
return $this->documents()->where('documents.type', Document::BILL_TYPE);
2019-11-16 10:21:14 +03:00
}
public function bill_recurring()
{
return $this->documents()->where('documents.type', Document::BILL_RECURRING_TYPE);
}
public function invoices()
{
return $this->documents()->where('documents.type', Document::INVOICE_TYPE);
}
public function invoice_recurring()
{
return $this->documents()->where('documents.type', Document::INVOICE_RECURRING_TYPE);
}
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
{
2022-06-10 19:26:32 +03:00
return $this->transactions()->whereIn('transactions.type', (array) $this->getExpenseTypes());
2019-11-16 10:21:14 +03:00
}
public function income_transactions()
2019-11-16 10:21:14 +03:00
{
2022-06-10 19:26:32 +03:00
return $this->transactions()->whereIn('transactions.type', (array) $this->getIncomeTypes());
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
}
2023-05-02 11:21:36 +03:00
/**
* Scope to include only employees.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeEmployee($query)
{
return $query->whereIn($this->qualifyColumn('type'), (array) $this->getEmployeeTypes());
}
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;
}
2022-06-01 10:15:55 +03:00
public function getInitialsAttribute($value)
{
return Str::getInitials($this->name);
}
2019-11-16 10:21:14 +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();
}
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
2022-06-01 10:15:55 +03:00
$this->$collection->whereIn('status', ['sent', 'received', 'viewed', 'partial'])->each(function ($item) use (&$amount) {
$amount += $this->convertToDefault($item->amount_due, $item->currency_code, $item->currency_rate);
});
return $amount;
}
public function getOpenAttribute()
{
$amount = 0;
$today = Date::today()->toDateString();
$collection = $this->isCustomer() ? 'invoices' : 'bills';
$this->$collection->whereIn('status', ['sent', 'received', 'viewed', 'partial'])->where('due_at', '>=', $today)->each(function ($item) use (&$amount) {
$amount += $this->convertToDefault($item->amount_due, $item->currency_code, $item->currency_rate);
});
return $amount;
}
public function getOverdueAttribute()
{
$amount = 0;
$today = Date::today()->toDateString();
$collection = $this->isCustomer() ? 'invoices' : 'bills';
$this->$collection->whereIn('status', ['sent', 'received', 'viewed', 'partial'])->where('due_at', '<', $today)->each(function ($item) use (&$amount) {
2021-12-14 10:57:59 +03:00
$amount += $this->convertToDefault($item->amount_due, $item->currency_code, $item->currency_rate);
2019-11-16 10:21:14 +03:00
});
return $amount;
}
2020-10-14 17:07:59 +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;
}
2023-06-01 16:42:11 +03:00
if ($this->country && array_key_exists($this->country, trans('countries'))) {
$location[] = trans('countries.' . $this->country);
}
return implode(', ', $location);
}
2022-06-01 10:15:55 +03:00
/**
* Get the line actions.
*
* @return array
*/
public function getLineActionsAttribute()
{
$actions = [];
$group = config('type.contact.' . $this->type . '.group');
$prefix = config('type.contact.' . $this->type . '.route.prefix');
$permission_prefix = config('type.contact.' . $this->type . '.permission.prefix');
$translation_prefix = config('type.contact.' . $this->type . '.translation.prefix');
if (empty($prefix)) {
if (in_array($this->type, (array) $this->getCustomerTypes())) {
$prefix = config('type.contact.customer.route.prefix');
} elseif (in_array($this->type, (array) $this->getVendorTypes())) {
$prefix = config('type.contact.vendor.route.prefix');
} else {
return $actions;
}
}
try {
$actions[] = [
'title' => trans('general.show'),
'icon' => 'visibility',
'url' => route($prefix . '.show', $this->id),
'permission' => 'read-' . $group . '-' . $permission_prefix,
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-show-' . $this->type . '-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
} catch (\Exception $e) {}
try {
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route($prefix . '.edit', $this->id),
'permission' => 'update-' . $group . '-' . $permission_prefix,
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-edit-' . $this->type . '-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
} catch (\Exception $e) {}
try {
$actions[] = [
'title' => trans('general.duplicate'),
'icon' => 'file_copy',
'url' => route($prefix . '.duplicate', $this->id),
'permission' => 'create-' . $group . '-' . $permission_prefix,
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-duplicate-' . $this->type . '-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
} catch (\Exception $e) {}
try {
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
'title' => $translation_prefix,
'route' => $prefix . '.destroy',
'permission' => 'delete-' . $group . '-' . $permission_prefix,
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-delete-' . $this->type . '-' . $this->id,
],
2022-06-01 10:15:55 +03:00
'model' => $this,
];
} catch (\Exception $e) {}
return $actions;
}
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
}