2018-07-06 06:57:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-06-30 04:00:41 +00:00
|
|
|
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;
|
2022-08-18 13:29:42 +00:00
|
|
|
use App\Traits\SiteID;
|
2018-08-20 12:15:28 +00:00
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Class Account
|
|
|
|
* Service Accounts
|
|
|
|
*
|
|
|
|
* Attributes for accounts:
|
2021-12-24 01:14:01 +00:00
|
|
|
* + 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
|
|
|
{
|
2022-08-18 13:29:42 +00:00
|
|
|
use SiteID;
|
2021-12-24 01:14:01 +00:00
|
|
|
use HasFactory,ScopeActive;
|
2018-08-20 12:15:28 +00:00
|
|
|
|
2022-04-21 04:41:26 +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 */
|
|
|
|
|
2021-10-01 04:59:04 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-07 06:54:27 +00:00
|
|
|
public function payments()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Payment::class);
|
|
|
|
}
|
|
|
|
|
2022-08-19 10:12:08 +00:00
|
|
|
public function providers()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(ProviderOauth::class,'account_provider')
|
|
|
|
->where('account_provider.site_id',$this->site_id)
|
|
|
|
->withPivot('ref','synctoken','created_at','updated_at');
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:55:05 +00:00
|
|
|
public function services($active=FALSE)
|
2018-08-08 23:33:51 +00:00
|
|
|
{
|
2019-07-04 04:55:05 +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
|
|
|
}
|
|
|
|
|
2022-03-05 00:05:35 +00:00
|
|
|
public function site()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Site::class);
|
|
|
|
}
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
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
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* Search for a record
|
|
|
|
*
|
2019-06-29 00:14:12 +00:00
|
|
|
* @param $query
|
|
|
|
* @param string $term
|
2021-07-23 07:25:26 +00:00
|
|
|
* @return mixed
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
|
|
|
public function scopeSearch($query,string $term)
|
|
|
|
{
|
|
|
|
// Build our where clause
|
2021-07-07 07:45:16 +00:00
|
|
|
if (is_numeric($term)) {
|
2019-06-21 06:21:48 +00:00
|
|
|
$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.'%');
|
2019-06-21 06:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/* ATTRIBUTES */
|
2019-06-21 06:21:48 +00:00
|
|
|
|
2021-07-01 23:12:34 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-07-29 07:45:17 +00:00
|
|
|
/**
|
|
|
|
* Account breadcrumb to render on pages
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getBreadcrumbAttribute(): array
|
|
|
|
{
|
|
|
|
return [$this->name => url('u/home',$this->user_id)];
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
2022-04-21 04:41:26 +00:00
|
|
|
* Return the account name
|
2021-06-29 06:36:34 +00:00
|
|
|
*
|
2022-04-21 04:41:26 +00:00
|
|
|
* @return mixed|string
|
2021-06-29 06:36:34 +00:00
|
|
|
*/
|
2022-04-21 04:41:26 +00:00
|
|
|
public function getNameAttribute(): string
|
2019-06-07 06:54:27 +00:00
|
|
|
{
|
2022-04-21 06:25:29 +00:00
|
|
|
return $this->company ?: ($this->user_id ? $this->user->getSurFirstNameAttribute() : 'LID:'.$this->id);
|
2018-08-08 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 01:21:20 +00:00
|
|
|
/**
|
|
|
|
* Return the type of account this is - if it has a company name, then its a business account.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-06-12 06:25:15 +00:00
|
|
|
public function getTypeAttribute()
|
|
|
|
{
|
|
|
|
return $this->company ? 'Business' : 'Private';
|
|
|
|
}
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
/* 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;
|
|
|
|
});
|
|
|
|
}
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|