2017-11-03 05:26:07 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-29 03:18:52 +00:00
|
|
|
namespace App\Models;
|
2017-11-03 05:26:07 +00:00
|
|
|
|
2022-04-21 04:41:26 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2017-12-12 05:28:49 +00:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
2020-01-12 12:42:32 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
|
2022-04-21 04:41:26 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2020-07-21 06:39:23 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-04-21 04:41:26 +00:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2018-07-13 04:53:44 +00:00
|
|
|
use Laravel\Passport\HasApiTokens;
|
2022-04-21 06:25:29 +00:00
|
|
|
use Leenooks\Traits\ScopeActive;
|
2018-07-13 04:53:44 +00:00
|
|
|
use Leenooks\Traits\UserSwitch;
|
2017-11-03 05:26:07 +00:00
|
|
|
|
2022-04-22 01:47:46 +00:00
|
|
|
use App\Interfaces\IDs;
|
2022-09-29 07:26:03 +00:00
|
|
|
use App\Models\Scopes\SiteScope;
|
2020-07-21 06:39:23 +00:00
|
|
|
use App\Notifications\ResetPassword as ResetPasswordNotification;
|
2022-04-21 04:41:26 +00:00
|
|
|
use App\Traits\{QueryCacheableConfig,SiteID};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class User
|
|
|
|
*
|
|
|
|
* Attributes for users:
|
|
|
|
* + role : User's role
|
|
|
|
*/
|
2022-04-22 01:47:46 +00:00
|
|
|
class User extends Authenticatable implements IDs
|
2017-11-03 05:26:07 +00:00
|
|
|
{
|
2022-04-21 06:25:29 +00:00
|
|
|
use HasFactory,HasApiTokens,Notifiable,UserSwitch,QueryCacheableConfig,SiteID,ScopeActive;
|
2022-04-21 04:41:26 +00:00
|
|
|
|
|
|
|
private const CACHE_TIME = 3600;
|
2020-02-10 11:07:46 +00:00
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
protected $dates = [
|
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
|
|
|
'last_access'
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2022-04-21 06:25:29 +00:00
|
|
|
'name',
|
|
|
|
'email',
|
|
|
|
'password',
|
2020-02-08 11:51:50 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
'password',
|
|
|
|
'remember_token',
|
|
|
|
];
|
2018-04-10 11:23:13 +00:00
|
|
|
|
2020-04-18 22:33:41 +00:00
|
|
|
/**
|
|
|
|
* Role hierarchy order
|
2022-04-21 06:25:29 +00:00
|
|
|
*
|
2020-04-18 22:33:41 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2022-04-21 06:25:29 +00:00
|
|
|
public const role_order = [
|
2020-04-18 22:33:41 +00:00
|
|
|
'wholesaler',
|
|
|
|
'reseller',
|
|
|
|
'customer',
|
|
|
|
];
|
|
|
|
|
2022-04-21 06:25:29 +00:00
|
|
|
/* OVERRIDES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Users password reset email notification
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
*/
|
|
|
|
public function sendPasswordResetNotification($token)
|
|
|
|
{
|
|
|
|
$this->notify((new ResetPasswordNotification($token))->onQueue('high'));
|
|
|
|
}
|
|
|
|
|
2022-04-22 01:47:46 +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());
|
|
|
|
}
|
|
|
|
|
2021-07-13 02:31:56 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
/**
|
|
|
|
* The accounts that this user manages
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
2022-04-21 04:41:26 +00:00
|
|
|
* @note This cannot be loaded with "with"?
|
2020-02-08 11:51:50 +00:00
|
|
|
*/
|
2018-07-17 04:10:40 +00:00
|
|
|
public function accounts()
|
2020-02-08 11:51:50 +00:00
|
|
|
{
|
2022-04-21 04:41:26 +00:00
|
|
|
return $this->hasMany(Account::class)
|
|
|
|
->orWhereIn('id',$this->rtm_accounts()->pluck('id'))
|
2023-05-09 10:28:51 +00:00
|
|
|
->active();
|
2020-02-08 11:51:50 +00:00
|
|
|
}
|
2018-05-20 12:53:14 +00:00
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
/**
|
2022-04-21 06:25:29 +00:00
|
|
|
* This users invoices
|
2020-02-08 11:51:50 +00:00
|
|
|
*
|
2022-04-21 06:25:29 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
2023-05-09 10:28:51 +00:00
|
|
|
* @deprecated Accounts have invoices, not users
|
2020-02-08 11:51:50 +00:00
|
|
|
*/
|
2022-04-21 06:25:29 +00:00
|
|
|
public function invoices()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(Invoice::class,Account::class)
|
|
|
|
->active();
|
2018-07-13 04:53:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
/**
|
|
|
|
* This users language configuration
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2018-08-01 07:09:38 +00:00
|
|
|
public function language()
|
|
|
|
{
|
2021-06-29 03:18:52 +00:00
|
|
|
return $this->belongsTo(Language::class);
|
2018-08-01 07:09:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 04:41:26 +00:00
|
|
|
/**
|
|
|
|
* Return the routes to market account for this user
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
|
|
|
|
*/
|
|
|
|
public function rtm()
|
|
|
|
{
|
|
|
|
return $this->hasOneThrough(Rtm::class,Account::class);
|
|
|
|
}
|
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
/**
|
2023-05-09 10:28:51 +00:00
|
|
|
* The services this user has
|
2020-02-08 11:51:50 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
2023-05-09 10:28:51 +00:00
|
|
|
* @deprecated Accounts have services, not users
|
2020-02-08 11:51:50 +00:00
|
|
|
*/
|
2018-07-13 04:53:44 +00:00
|
|
|
public function services()
|
|
|
|
{
|
2021-06-29 06:36:34 +00:00
|
|
|
return $this->hasManyThrough(Service::class,Account::class)
|
2022-02-02 06:12:17 +00:00
|
|
|
->with(['product.type'])
|
2021-06-29 06:36:34 +00:00
|
|
|
->active();
|
2018-07-13 04:53:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 10:28:51 +00:00
|
|
|
/**
|
|
|
|
* Supplier configuration for this user
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
* @deprecated Move to account->suppliers()
|
|
|
|
*/
|
2022-08-07 02:17:20 +00:00
|
|
|
public function suppliers()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Supplier::class)
|
|
|
|
->where('supplier_user.site_id',$this->site_id)
|
|
|
|
->withPivot('id','created_at');
|
|
|
|
}
|
|
|
|
|
2022-04-21 06:25:29 +00:00
|
|
|
/* ATTRIBUTES */
|
2018-07-13 04:53:44 +00:00
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
/**
|
2022-04-21 06:25:29 +00:00
|
|
|
* This is an alias method, as it is used by the framework
|
2020-02-08 11:51:50 +00:00
|
|
|
*
|
2022-04-21 06:25:29 +00:00
|
|
|
* @return string
|
2020-02-08 11:51:50 +00:00
|
|
|
*/
|
2022-04-21 06:25:29 +00:00
|
|
|
public function getNameAttribute(): string
|
2018-08-08 23:33:51 +00:00
|
|
|
{
|
2023-05-09 10:28:51 +00:00
|
|
|
return $this->full_name;
|
2018-08-08 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 06:57:49 +00:00
|
|
|
/**
|
|
|
|
* Logged in users full name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-02-08 11:51:50 +00:00
|
|
|
public function getFullNameAttribute(): string
|
2018-07-06 06:57:49 +00:00
|
|
|
{
|
|
|
|
return sprintf('%s %s',$this->firstname,$this->lastname);
|
|
|
|
}
|
|
|
|
|
2022-04-21 04:41:26 +00:00
|
|
|
/**
|
2023-05-09 10:28:51 +00:00
|
|
|
* Return my accounts, but only those accounts with the same group_id
|
2022-04-21 04:41:26 +00:00
|
|
|
*
|
2023-05-09 10:28:51 +00:00
|
|
|
* @note Users can only manage accounts with the same group ID, thereby ensuring they dont see different
|
|
|
|
* pricing options - since prices can be controlled by groups
|
2022-04-21 04:41:26 +00:00
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getMyAccountsAttribute(): Collection
|
|
|
|
{
|
2023-05-09 10:28:51 +00:00
|
|
|
$result = $this->accounts->where('user_id',$this->id);
|
|
|
|
|
|
|
|
if (! $result->count())
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
return $this->isReseller() ? $result : $result->groupBy('group.id')->first();
|
2022-04-21 04:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a friendly string of this persons role
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRoleAttribute(): string
|
|
|
|
{
|
|
|
|
return ucfirst($this->role());
|
|
|
|
}
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
public function getSurFirstNameAttribute()
|
2018-08-08 23:33:51 +00:00
|
|
|
{
|
2018-08-09 14:10:51 +00:00
|
|
|
return sprintf('%s, %s',$this->lastname,$this->firstname);
|
2018-08-08 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 02:31:56 +00:00
|
|
|
/* SCOPES */
|
2018-08-09 14:10:51 +00:00
|
|
|
|
2020-02-06 22:11:02 +00:00
|
|
|
/**
|
|
|
|
* Search for a record
|
|
|
|
*
|
2020-02-08 11:51:50 +00:00
|
|
|
* @param $query
|
2020-02-06 22:11:02 +00:00
|
|
|
* @param string $term
|
|
|
|
*/
|
|
|
|
public function scopeSearch($query,string $term)
|
|
|
|
{
|
|
|
|
// Build our where clause
|
|
|
|
// First Name, Last name
|
|
|
|
if (preg_match('/\ /',$term)) {
|
2020-04-14 07:40:47 +00:00
|
|
|
[$fn,$ln] = explode(' ',$term,2);
|
2020-02-06 22:11:02 +00:00
|
|
|
|
|
|
|
$query->where(function($query1) use ($fn,$ln,$term) {
|
|
|
|
$query1->where(function($query2) use ($fn,$ln) {
|
|
|
|
return $query2
|
|
|
|
->where('firstname','like','%'.$fn.'%')
|
|
|
|
->where('lastname','like','%'.$ln.'%');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
} elseif (is_numeric($term)) {
|
2022-08-07 02:17:20 +00:00
|
|
|
$query->where('users.id','like','%'.$term.'%');
|
2020-02-06 22:11:02 +00:00
|
|
|
|
|
|
|
} elseif (preg_match('/\@/',$term)) {
|
|
|
|
$query->where('email','like','%'.$term.'%');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$query
|
|
|
|
->Where('firstname','like','%'.$term.'%')
|
|
|
|
->orWhere('lastname','like','%'.$term.'%');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2022-04-21 04:41:26 +00:00
|
|
|
/* METHODS */
|
2021-07-13 02:31:56 +00:00
|
|
|
|
2020-04-14 07:40:47 +00:00
|
|
|
/**
|
|
|
|
* Show this user's clients with service movements
|
|
|
|
*
|
|
|
|
* A service movement, is an active service where the status is not ACTIVE
|
|
|
|
*
|
|
|
|
* @return DatabaseCollection
|
|
|
|
*/
|
|
|
|
public function client_service_movements(): DatabaseCollection
|
|
|
|
{
|
|
|
|
return Service::active()
|
2022-04-21 04:41:26 +00:00
|
|
|
->serviceUserAuthorised($this)
|
2020-04-14 07:40:47 +00:00
|
|
|
->where('order_status','!=','ACTIVE')
|
|
|
|
->with(['account','product'])
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
2022-04-21 06:25:29 +00:00
|
|
|
/**
|
|
|
|
* Determine if the user is an admin of the user with $id
|
|
|
|
*
|
2022-06-28 11:57:55 +00:00
|
|
|
* @param User|null $user
|
2022-04-21 06:25:29 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2022-06-28 11:57:55 +00:00
|
|
|
public function isAdmin(User $user=NULL): bool
|
2022-04-21 06:25:29 +00:00
|
|
|
{
|
2022-06-28 11:57:55 +00:00
|
|
|
return $user->exists AND $this->isReseller() AND $this->accounts->pluck('user_id')->contains($user->id);
|
2022-04-21 06:25:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
/**
|
|
|
|
* Determine if the logged in user is a reseller or wholesaler
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-02-08 11:51:50 +00:00
|
|
|
public function isReseller(): bool
|
2018-08-09 14:10:51 +00:00
|
|
|
{
|
|
|
|
return in_array($this->role(),['wholesaler','reseller']);
|
|
|
|
}
|
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
/**
|
|
|
|
* Determine if the logged in user is a wholesaler
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isWholesaler(): bool
|
2018-08-23 05:17:26 +00:00
|
|
|
{
|
|
|
|
return in_array($this->role(),['wholesaler']);
|
|
|
|
}
|
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
/**
|
|
|
|
* Get all the items for the next invoice
|
|
|
|
*
|
2020-04-14 07:40:47 +00:00
|
|
|
* @param bool $future
|
|
|
|
* @return DatabaseCollection
|
2020-02-08 11:51:50 +00:00
|
|
|
*/
|
|
|
|
public function next_invoice_items(bool $future=FALSE): DatabaseCollection
|
|
|
|
{
|
|
|
|
$result = new DatabaseCollection;
|
2022-06-14 06:51:18 +00:00
|
|
|
$this->services->load(['invoice_items.taxes']);
|
2020-02-08 11:51:50 +00:00
|
|
|
|
|
|
|
foreach ($this->services as $o) {
|
|
|
|
if ($future) {
|
|
|
|
if ($o->invoice_next->subDays(config('app.invoice_inadvance'))->isPast())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if ($o->invoice_next->subDays(config('app.invoice_inadvance'))->isFuture())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-12 10:32:57 +00:00
|
|
|
foreach ($o->next_invoice_items($future) as $oo)
|
2020-02-08 11:51:50 +00:00
|
|
|
$result->push($oo);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result->load([
|
2022-10-18 12:23:45 +00:00
|
|
|
'product.translate',
|
2020-02-08 11:51:50 +00:00
|
|
|
'service.type',
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-07-21 06:39:23 +00:00
|
|
|
/**
|
|
|
|
* Return an SQL query that will return a list of invoices
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Query\Builder
|
|
|
|
*/
|
|
|
|
private function query_invoice_items()
|
|
|
|
{
|
2022-04-22 04:41:18 +00:00
|
|
|
return DB::table('invoice_items')
|
2020-07-21 06:39:23 +00:00
|
|
|
->select([
|
|
|
|
'invoice_id',
|
2022-04-22 04:41:18 +00:00
|
|
|
DB::raw('invoice_items.id AS invoice_item_id'),
|
2024-07-03 03:52:16 +00:00
|
|
|
DB::raw('COALESCE(invoice_items.discount_amt,0) AS discount'),
|
|
|
|
DB::raw('quantity*price_base AS base'),
|
|
|
|
DB::raw('invoice_item_taxes.amount AS tax'),
|
2020-07-21 06:39:23 +00:00
|
|
|
])
|
2022-04-22 04:41:18 +00:00
|
|
|
->leftjoin('invoice_item_taxes',['invoice_item_taxes.invoice_item_id'=>'invoice_items.id'])
|
2024-07-03 03:52:16 +00:00
|
|
|
->where('invoice_items.active',TRUE);
|
2020-07-21 06:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an SQL query that will return payment summaries by invoices.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Query\Builder
|
|
|
|
*/
|
|
|
|
private function query_payment_items()
|
|
|
|
{
|
2021-07-23 07:49:59 +00:00
|
|
|
return DB::table('payment_items')
|
2020-07-21 06:39:23 +00:00
|
|
|
->select([
|
|
|
|
'payment_id',
|
|
|
|
'invoice_id',
|
2022-04-22 05:23:08 +00:00
|
|
|
DB::raw('SUM(amount) AS allocate'),
|
2020-07-21 06:39:23 +00:00
|
|
|
])
|
2022-04-22 05:23:08 +00:00
|
|
|
->where('amount','>',0)
|
2020-07-21 06:39:23 +00:00
|
|
|
->groupBy(['invoice_id','payment_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an SQL query that will summarise invoices with payments
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Query\Builder
|
2021-06-29 06:36:34 +00:00
|
|
|
* @todo change this to just return outstanding invoices as a collection.
|
2024-07-03 03:52:16 +00:00
|
|
|
* @deprecated Use account->invoiceSummary
|
2020-07-21 06:39:23 +00:00
|
|
|
*/
|
|
|
|
public function query_invoice_summary()
|
|
|
|
{
|
|
|
|
$invoices = (new Invoice)
|
|
|
|
->select([
|
|
|
|
'invoice_id',
|
|
|
|
DB::raw('SUM(discount) AS discount'),
|
|
|
|
DB::raw('SUM(base) AS base'),
|
|
|
|
DB::raw('SUM(tax) AS tax'),
|
2024-07-03 03:52:16 +00:00
|
|
|
DB::raw('base+tax-discount AS total'),
|
|
|
|
DB::raw('0 AS payments'),
|
|
|
|
DB::raw('0 AS payment_fees'),
|
2020-07-21 06:39:23 +00:00
|
|
|
])
|
|
|
|
->from($this->query_invoice_items(),'II')
|
2022-04-22 04:41:18 +00:00
|
|
|
->join('invoices',['invoices.id'=>'II.invoice_id'])
|
2022-04-21 04:41:26 +00:00
|
|
|
->whereIN('account_id',$this->accounts->pluck('id'))
|
2022-04-22 04:41:18 +00:00
|
|
|
->where('invoices.active',TRUE)
|
2024-07-03 03:52:16 +00:00
|
|
|
->groupBy(['invoice_id','base','tax','discount']);
|
2020-07-21 06:39:23 +00:00
|
|
|
|
|
|
|
$payments = (new Payment)
|
|
|
|
->select([
|
|
|
|
'invoice_id',
|
2024-07-03 03:52:16 +00:00
|
|
|
DB::raw('0 AS discount'),
|
|
|
|
DB::raw('0 AS base'),
|
|
|
|
DB::raw('0 AS tax'),
|
|
|
|
DB::raw('0 AS total'),
|
2020-07-21 06:39:23 +00:00
|
|
|
DB::raw('SUM(allocate) AS payments'),
|
|
|
|
DB::raw('SUM(fees_amt) AS payment_fees'),
|
|
|
|
])
|
|
|
|
->from($this->query_payment_items(),'PI')
|
2021-07-23 07:49:59 +00:00
|
|
|
->join('payments',['payments.id'=>'PI.payment_id'])
|
2022-04-21 04:41:26 +00:00
|
|
|
->whereIN('account_id',$this->accounts->pluck('id'))
|
2021-07-23 07:49:59 +00:00
|
|
|
//->where('payments.active',TRUE) // @todo To implement
|
2020-07-21 06:39:23 +00:00
|
|
|
->groupBy(['invoice_id']);
|
|
|
|
|
|
|
|
$summary = (new Invoice)
|
2022-09-29 07:26:03 +00:00
|
|
|
->withoutGlobalScope(SiteScope::class)
|
2020-07-21 06:39:23 +00:00
|
|
|
->select([
|
|
|
|
'invoice_id',
|
|
|
|
DB::raw('SUM(discount) AS discount'),
|
|
|
|
DB::raw('SUM(base) AS invoice_base'),
|
|
|
|
DB::raw('SUM(tax) AS invoice_tax'),
|
|
|
|
DB::raw('SUM(total) AS invoice_total'),
|
|
|
|
DB::raw('SUM(payments) AS payments'),
|
|
|
|
DB::raw('SUM(payment_fees) AS payment_fees'),
|
|
|
|
])
|
|
|
|
->from($invoices->unionAll($payments),'invoices')
|
|
|
|
->groupBy(['invoice_id']);
|
|
|
|
|
2022-02-02 06:12:17 +00:00
|
|
|
return (new Invoice)
|
2020-07-21 06:39:23 +00:00
|
|
|
->select([
|
|
|
|
'account_id',
|
|
|
|
'id',
|
2022-04-22 04:41:18 +00:00
|
|
|
'due_at',
|
|
|
|
'created_at',
|
2020-07-21 06:39:23 +00:00
|
|
|
'discount',
|
|
|
|
'invoice_base',
|
|
|
|
'invoice_tax',
|
|
|
|
'invoice_total',
|
|
|
|
'payments',
|
|
|
|
'payment_fees',
|
2024-07-03 03:52:16 +00:00
|
|
|
DB::raw('invoice_total-payments AS balance'),
|
2020-07-21 06:39:23 +00:00
|
|
|
])
|
2022-04-22 04:41:18 +00:00
|
|
|
->join('invoices',['invoices.id'=>'invoice_id'])
|
2022-02-02 06:12:17 +00:00
|
|
|
->with(['items.taxes'])
|
2024-07-03 03:52:16 +00:00
|
|
|
->from($summary,'summary')
|
|
|
|
->groupBy(['id','account_id','discount','invoice_base','invoice_tax','invoice_total','payments','payment_fees']);
|
2020-07-21 06:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 02:31:56 +00:00
|
|
|
/**
|
|
|
|
* Determine what the logged in user's role is
|
|
|
|
* + Wholesaler - aka Super User
|
|
|
|
* + Reseller - services accounts on behalf of their customers
|
|
|
|
* + Customer - end user customer
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-07-06 06:57:49 +00:00
|
|
|
public function role()
|
|
|
|
{
|
2022-04-21 04:41:26 +00:00
|
|
|
// Cache our role for this session
|
|
|
|
$cache_key = sprintf('%s:%s:%s',$this->id,__METHOD__,Session::getId());
|
|
|
|
|
|
|
|
return Cache::remember($cache_key,self::CACHE_TIME,function() {
|
|
|
|
// Get the RTM for our accounts
|
|
|
|
$rtms = Rtm::whereIn('account_id',$this->accounts->pluck('id'))->get();
|
|
|
|
|
|
|
|
// If I have no parent, I am the wholesaler
|
|
|
|
if ($rtms->whereNull('parent_id')->count())
|
|
|
|
return 'wholesaler';
|
|
|
|
|
|
|
|
// If I exist in the RTM table, I'm a reseller
|
|
|
|
else if ($rtms->count())
|
|
|
|
return 'reseller';
|
|
|
|
|
|
|
|
// Otherwise a client
|
|
|
|
else
|
|
|
|
return 'customer';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the accounts that this user can manage
|
|
|
|
* This method is a helper to User::accounts() - use $user->accounts instead
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
private function rtm_accounts(): Collection
|
|
|
|
{
|
|
|
|
return Account::whereIn('rtm_id',$this->rtm_list()->pluck('id'))
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the RTM hierarchy that this user can manage
|
|
|
|
*
|
|
|
|
* @param Rtm|null $rtm
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function rtm_list(Rtm $rtm=NULL): Collection
|
|
|
|
{
|
|
|
|
// If this user doesnt manage any accounts
|
|
|
|
if (! $this->exists || ! $this->rtm)
|
|
|
|
return collect();
|
|
|
|
|
|
|
|
$list = collect();
|
|
|
|
|
|
|
|
// Add this RTM to the list
|
|
|
|
if (! $rtm) {
|
|
|
|
$list->push($this->rtm);
|
|
|
|
$children = $this->rtm->children;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$list->push($rtm);
|
|
|
|
$children =$rtm->children;
|
|
|
|
}
|
2018-07-06 06:57:49 +00:00
|
|
|
|
2022-04-21 04:41:26 +00:00
|
|
|
// Capture any children
|
|
|
|
foreach ($children as $child)
|
|
|
|
$list->push($this->rtm_list($child));
|
2018-07-06 06:57:49 +00:00
|
|
|
|
2022-04-21 04:41:26 +00:00
|
|
|
return $rtm ? $list : $list->flatten()->unique(function($item) { return $item->id; });
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|