osb/app/Models/Account.php

251 lines
4.8 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
use App\Traits\NextKey;
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
*
* @package App\Models
*/
class Account extends Model implements IDs
2018-07-06 06:57:49 +00:00
{
use HasFactory,NextKey,ScopeActive;
2021-06-29 06:36:34 +00:00
const RECORD_ID = 'account';
2018-08-20 12:15:28 +00:00
public $incrementing = FALSE;
2021-07-07 07:45:16 +00:00
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
2018-07-13 04:53:44 +00:00
2018-07-17 04:10:40 +00:00
protected $appends = [
'active_display',
'name',
2018-08-08 23:33:51 +00:00
'services_count_html',
'switch_url',
2018-07-17 04:10:40 +00:00
];
2021-07-09 02:47:10 +00:00
public $dateFormat = 'U';
2018-07-17 04:10:40 +00:00
protected $visible = [
'id',
'active_display',
'name',
2018-08-08 23:33:51 +00:00
'services_count_html',
'switch_url',
2018-07-17 04:10:40 +00:00
];
2021-06-29 06:36:34 +00:00
/* RELATIONS */
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');
}
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
}
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
*/
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)
{
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
{
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()
{
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()
{
2021-06-29 06:36:34 +00:00
return $this->getSIDAttribute();
}
/**
* Account Local ID
*
* @return string
*/
public function getLIDAttribute(): string
{
return sprintf('%04s',$this->id);
2018-08-01 07:09:38 +00:00
}
public function getNameAttribute()
{
return $this->company ?: ($this->user_id ? $this->user->SurFirstName : 'AID:'.$this->id);
}
2018-08-08 23:33:51 +00:00
public function getServicesCountHtmlAttribute()
{
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
}
2021-06-29 06:36:34 +00:00
/**
* Account System ID
*
* @return string
*/
public function getSIDAttribute(): string
{
return sprintf('%02s-%s',$this->site_id,$this->getLIDAttribute());
}
2018-08-08 23:33:51 +00:00
public function getSwitchUrlAttribute()
{
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
{
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
{
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
}