osb/app/Models/Account.php

194 lines
3.7 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;
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:
* + 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;
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 providers()
{
return $this->belongsToMany(ProviderOauth::class,'account_provider')
->where('account_provider.site_id',$this->site_id)
->withPivot('ref','synctoken','created_at','updated_at');
}
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 */
/**
* 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)
];
}
/**
* 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
/**
* 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->getSurFirstNameAttribute() : 'LID:'.$this->id);
2018-08-08 23:33:51 +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';
}
/* 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
}