osb/app/Models/Account.php

254 lines
5.1 KiB
PHP
Raw Normal View History

2018-07-06 06:57:49 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2018-07-06 06:57:49 +00:00
use Illuminate\Database\Eloquent\Model;
2021-06-29 06:36:34 +00:00
use Leenooks\Traits\ScopeActive;
2018-07-06 06:57:49 +00:00
2021-06-29 06:36:34 +00:00
use App\Interfaces\IDs;
2018-08-20 12:15:28 +00:00
2021-06-29 06:36:34 +00:00
/**
* Class Account
* Service Accounts
*
* Attributes for accounts:
* + lid : Local ID for account
* + sid : System ID for account
* + name : Account Name
* + taxes : Taxes Applicable to this account
2021-06-29 06:36:34 +00:00
*
* @package App\Models
*/
class Account extends Model implements IDs
2018-07-06 06:57:49 +00:00
{
use HasFactory,ScopeActive;
2018-08-20 12:15:28 +00:00
/* INTERFACES */
public function getLIDAttribute(): string
{
return sprintf('%04s',$this->id);
}
public function getSIDAttribute(): string
{
return sprintf('%02s-%s',$this->site_id,$this->getLIDAttribute());
}
2018-07-17 04:10:40 +00:00
2021-06-29 06:36:34 +00:00
/* RELATIONS */
public function charges()
{
return $this->hasMany(Charge::class);
}
2018-07-13 04:53:44 +00:00
/**
* Return the country the user belongs to
*/
public function country()
{
return $this->belongsTo(Country::class);
}
2019-06-12 06:25:15 +00:00
public function external()
{
return $this->belongsToMany(External\Integrations::class,'external_account',NULL,'external_integration_id');
}
public function group()
{
return $this->hasOneThrough(Group::class,AccountGroup::class,'account_id','id','id','group_id');
}
2019-06-11 02:36:58 +00:00
public function invoices()
{
return $this->hasMany(Invoice::class);
}
2018-07-13 04:53:44 +00:00
public function language()
{
return $this->belongsTo(Language::class);
}
public function payments()
{
return $this->hasMany(Payment::class);
}
public function services($active=FALSE)
2018-08-08 23:33:51 +00:00
{
$query = $this->hasMany(Service::class);
2020-04-01 12:35:06 +00:00
return $active ? $query->active() : $query;
2018-08-08 23:33:51 +00:00
}
public function site()
{
return $this->belongsTo(Site::class);
}
public function taxes()
{
return $this->hasMany(Tax::class,'country_id','country_id');
}
2018-07-13 04:53:44 +00:00
public function user()
{
2021-06-29 03:18:52 +00:00
return $this->belongsTo(User::class);
2018-07-13 04:53:44 +00:00
}
2018-07-17 04:10:40 +00:00
2021-06-29 06:36:34 +00:00
/* SCOPES */
2020-04-01 12:35:06 +00:00
/**
* Search for a record
*
2019-06-29 00:14:12 +00:00
* @param $query
* @param string $term
* @return mixed
*/
public function scopeSearch($query,string $term)
{
// Build our where clause
2021-07-07 07:45:16 +00:00
if (is_numeric($term)) {
$query->where('id','like','%'.$term.'%');
} else {
2021-07-07 07:45:16 +00:00
$query->where('company','like','%'.$term.'%')
->orWhere('address1','like','%'.$term.'%')
->orWhere('address2','like','%'.$term.'%')
->orWhere('city','like','%'.$term.'%');
}
return $query;
}
2021-06-29 06:36:34 +00:00
/* ATTRIBUTES */
2018-08-01 07:09:38 +00:00
public function getActiveDisplayAttribute($value)
{
abort(500,'deprecated');
2019-06-02 05:35:48 +00:00
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'success' : 'danger',$this->active ? 'Active' : 'Inactive');
2018-08-01 07:09:38 +00:00
}
/**
* @deprecated use getAIDAttribute()
*/
2018-08-01 07:09:38 +00:00
public function getAccountIdAttribute()
2018-07-17 04:10:40 +00:00
{
abort(500,'deprecated');
return $this->getAIDAttribute();
2018-08-01 07:09:38 +00:00
}
/**
* @deprecated use getUrlAdminAttribute()
*/
2018-08-01 07:09:38 +00:00
public function getAccountIdUrlAttribute()
{
abort(500,'deprecated');
return $this->getUrlAdminAttribute();
}
/**
* Get the address for the account
*
* @return array
*/
public function getAddressAttribute(): array
{
return [
$this->address1,
$this->address2,
sprintf('%s %s %s',$this->city.(($this->state OR $this->zip) ? ',' : ''),$this->state,$this->zip)
];
}
/**
* Return the Account Unique Identifier
* @return string
2021-06-29 06:36:34 +00:00
* @deprecated use getSIDAttribute()
*/
public function getAIDAttribute()
{
abort(500,'deprecated');
2021-06-29 06:36:34 +00:00
return $this->getSIDAttribute();
}
/**
* Return the account name
2021-06-29 06:36:34 +00:00
*
* @return mixed|string
2021-06-29 06:36:34 +00:00
*/
public function getNameAttribute(): string
{
return $this->company ?: ($this->user_id ? $this->user->SurFirstName : 'AID:'.$this->id);
}
2018-08-08 23:33:51 +00:00
public function getServicesCountHtmlAttribute()
{
abort(500,'deprecated');
return sprintf('%s <small>/%s</small>',$this->services()->noEagerLoads()->where('active',TRUE)->count(),$this->services()->noEagerLoads()->count());
2018-08-08 23:33:51 +00:00
}
public function getSwitchUrlAttribute()
{
abort(500,'deprecated');
2021-06-29 06:36:34 +00:00
return sprintf('<a href="/r/switch/start/%s"><i class="fas fa-external-link-alt"></i></a>',$this->user_id);
2018-08-08 23:33:51 +00:00
}
2019-06-12 06:25:15 +00:00
public function getTypeAttribute()
{
return $this->company ? 'Business' : 'Private';
}
/**
* Return the Admin URL to manage the account
*
* @return string
*/
public function getUrlAdminAttribute(): string
{
abort(500,'deprecated');
return sprintf('<a href="/r/account/view/%s">%s</a>',$this->id,$this->account_id);
}
/**
* Return the User URL to manage the account
*
* @return string
*/
public function getUrlUserAttribute(): string
{
abort(500,'deprecated');
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->account_id);
}
/* GENERAL METHODS */
2019-06-11 02:36:58 +00:00
/**
* Get the due invoices on an account
*
* @return mixed
*/
public function dueInvoices()
{
return $this->invoices->filter(function($item) {
return $item->active AND $item->due > 0;
});
}
2019-06-12 06:25:15 +00:00
/**
* Get the external account ID for a specific integration
*
* @param External\Integrations $o
* @return mixed
*/
public function ExternalAccounting(External\Integrations $o)
{
return $this
->external()
->where('id','=',$o->id)
->where('site_id','=',$this->site_id)
->first();
}
2018-07-06 06:57:49 +00:00
}