osb/app/Models/Account.php

216 lines
4.3 KiB
PHP
Raw Normal View History

2018-07-06 06:57:49 +00:00
<?php
namespace App\Models;
use Awobaz\Compoships\Compoships;
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
use App\Models\Scopes\SiteScope;
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
{
use Compoships,HasFactory,ScopeActive,SiteID;
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');
}
/**
* @return mixed
* @todo This needs to be optimised, to only return outstanding invoices and invoices for a specific age (eg: 2 years worth)
*/
2019-06-11 02:36:58 +00:00
public function invoices()
{
return $this->hasMany(Invoice::class)
->active()
->with(['items.taxes','paymentitems.payment']);
2019-06-11 02:36:58 +00:00
}
2018-07-13 04:53:44 +00:00
public function language()
{
return $this->belongsTo(Language::class);
}
public function payments()
{
return $this->hasMany(Payment::class)
->active()
->with(['items']);
}
public function providers()
{
2023-05-10 07:37:43 +00:00
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,['account_id','site_id'],['id','site_id'])
->withoutGlobalScope(SiteScope::class)
->with(['product.translate','invoice_items']);
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;
});
}
2023-05-05 05:48:24 +00:00
/**
* Return the taxed value of a value
*
* @param float $value
* @return float
*/
public function taxed(float $value): float
{
return Tax::calc($value,$this->taxes);
}
2018-07-06 06:57:49 +00:00
}