Optimise users home page

This commit is contained in:
Deon George
2021-06-29 16:36:34 +10:00
parent 638472fb4f
commit bac4fd6227
34 changed files with 915 additions and 627 deletions

View File

@@ -3,12 +3,25 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Leenooks\Traits\ScopeActive;
use App\Interfaces\IDs;
use App\Traits\NextKey;
class Account extends Model
/**
* 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
{
use NextKey;
use NextKey,ScopeActive;
const RECORD_ID = 'account';
public $incrementing = FALSE;
@@ -29,6 +42,8 @@ class Account extends Model
'switch_url',
];
/* RELATIONS */
/**
* Return the country the user belongs to
*/
@@ -69,15 +84,7 @@ class Account extends Model
return $this->belongsTo(User::class);
}
/** SCOPES */
/**
* Only query active categories
*/
public function scopeActive($query)
{
return $query->where('active',TRUE);
}
/* SCOPES */
/**
* Search for a record
@@ -118,7 +125,7 @@ class Account extends Model
return $query;
}
/** ATTRIBUTES **/
/* ATTRIBUTES */
public function getActiveDisplayAttribute($value)
{
@@ -144,10 +151,21 @@ class Account extends Model
/**
* Return the Account Unique Identifier
* @return string
* @deprecated use getSIDAttribute()
*/
public function getAIDAttribute()
{
return sprintf('%02s-%04s',$this->site_id,$this->id);
return $this->getSIDAttribute();
}
/**
* Account Local ID
*
* @return string
*/
public function getLIDAttribute(): string
{
return sprintf('%04s',$this->id);
}
public function getNameAttribute()
@@ -160,9 +178,19 @@ class Account extends Model
return sprintf('%s <small>/%s</small>',$this->services()->noEagerLoads()->where('active',TRUE)->count(),$this->services()->noEagerLoads()->count());
}
/**
* Account System ID
*
* @return string
*/
public function getSIDAttribute(): string
{
return sprintf('%02s-%s',$this->site_id,$this->getLIDAttribute());
}
public function getSwitchUrlAttribute()
{
return sprintf('<a href="/r/switch/start/%s"><i class="fa fa-external-link"></i></a>',$this->user_id);
return sprintf('<a href="/r/switch/start/%s"><i class="fas fa-external-link-alt"></i></a>',$this->user_id);
}
public function getTypeAttribute()

View File

@@ -6,14 +6,35 @@ use Carbon\Carbon;
use Clarkeash\Doorman\Facades\Doorman;
use Clarkeash\Doorman\Models\Invite;
use Illuminate\Database\Eloquent\Model;
use App\Traits\NextKey;
use App\Traits\PushNew;
use Illuminate\Support\Arr;
use Leenooks\Traits\ScopeActive;
class Invoice extends Model
use App\Interfaces\IDs;
use App\Traits\{NextKey,PushNew};
/**
* Class Invoice
* Invoices that belong to an Account
*
* Attributes for services:
* + due : Balance due on an invoice
* + due_date : Date the invoice is due
* + invoice_date : Date the invoice was created
* + lid : Local ID for invoice
* + paid : Total of payments received (excluding pending)
* + paid_date : Date the invoice was paid in full
* + paid_pending : Total of pending payments received
* + sid : System ID for invoice
* + total_sub : Invoice sub-total before taxes
* + total_tax : Invoices total of taxes
* + total : Invoice total
*
* @package App\Models
*/
class Invoice extends Model implements IDs
{
use NextKey,PushNew;
use NextKey,PushNew,ScopeActive;
const RECORD_ID = 'invoice';
public $incrementing = FALSE;
@@ -33,8 +54,12 @@ class Invoice extends Model
'paymentitems'
];
private $_total = 0;
private $_total_tax = 0;
// Caching variables
private int $_paid = 0;
private int $_total = 0;
private int $_total_tax = 0;
/* RELATIONS */
public function account()
{
@@ -43,7 +68,13 @@ class Invoice extends Model
public function items()
{
return $this->hasMany(InvoiceItem::class)->where('active',1);
return $this->hasMany(InvoiceItem::class)
->where('active',TRUE);
}
public function payments()
{
return $this->hasManyThrough(Payment::class,PaymentItem::class,NULL,'id',NULL,'payment_id');
}
public function paymentitems()
@@ -51,56 +82,114 @@ class Invoice extends Model
return $this->hasMany(PaymentItem::class);
}
/** SCOPES **/
/* SCOPES */
/**
* Search for a record
*
* @param $query
* @param string $term
* @return
* @return mixed
*/
public function scopeSearch($query,string $term)
{
return $query->where('id','like','%'.$term.'%');
}
/** ATTRIBUTES **/
/* ATTRIBUTES */
public function getDueAttribute()
/**
* Balance due on an invoice
* @return string
*/
public function getDueAttribute(): float
{
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total - $this->paid);
return sprintf('%4.'.$this->currency()->rounding.'f',$this->total-$this->paid);
}
/**
* @return mixed
* @deprecated use self::due_date;
*/
public function getDateDueAttribute()
{
return $this->due_date->format('Y-m-d');
}
public function getInvoiceDateAttribute()
/**
* Date the invoices was created
*
* @return Carbon
*/
public function getInvoiceDateAttribute(): Carbon
{
return $this->date_orig->format('Y-m-d');
return $this->date_orig;
}
/**
* Get account System ID
* @return string
* @deprecated use getSIDAttribute()
*/
public function getInvoiceAccountIdAttribute()
{
return sprintf('%02s-%04s-%06s',$this->site_id,$this->account_id,$this->invoice_id);
return $this->getSIDAttribute();
}
// @todo Move this to a site configuration
public function getInvoiceTextAttribute()
{
return sprintf('Thank you for using %s for your Internet Services.',config('SITE_SETUP')->site_name);
}
public function getPaidAttribute()
/**
* Invoice Local ID
*
* @return string
*/
public function getLIDAttribute(): string
{
return $this->currency()->round(
$this->paymentitems
->filter(function($item) { return ! $item->payment->pending_status; })
->sum('alloc_amt'));
return sprintf('%06s',$this->id);
}
public function getPendingPaidAttribute()
/**
* Total of payments received for this invoice
* excluding pending payments
*
* @return float
*/
public function getPaidAttribute(): float
{
if (! $this->_paid)
$this->_paid = $this->currency()->round(
$this->paymentitems
->filter(function($item) { return ! $item->payment->pending_status; })
->sum('alloc_amt'));
return $this->_paid;
}
/**
* Get the date that the invoice was paid in full.
* We assume the last payment received pays it in full.
*
* @return Carbon|null
*/
public function getPaidDateAttribute(): ?Carbon
{
$o = $this->payments
->filter(function($item) { return ! $item->pending_status; })
->last();
return $o ? $o->date_payment : NULL;
}
/**
* Total of pending payments received for this invoice
*
* @return mixed
*/
public function getPaidPendingAttribute(): float
{
return $this->currency()->round(
$this->paymentitems
@@ -108,35 +197,87 @@ class Invoice extends Model
->sum('alloc_amt'));
}
public function getSubTotalAttribute()
/**
* Total of pending payments received for this invoice
*
* @return mixed
* @deprecated use getPaidPendingAttribute()
*/
public function getPendingPaidAttribute(): float
{
return $this->getPaidPendingAttribute();
}
/**
* Invoice System ID
*
* @return string
*/
public function getSIDAttribute(): string
{
return sprintf('%02s-%04s-%s',$this->site_id,$this->account_id,$this->getLIDAttribute());
}
/**
* Get invoice subtotal before taxes
*
* @return float
* @deprecated use getTotalSubAttribute()
*/
public function getSubTotalAttribute(): float
{
return $this->getTotalSubAttribute();
}
/**
* Get invoice subtotal before taxes
*
* @return float
*/
public function getTotalSubAttribute(): float
{
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total-$this->tax_total);
}
public function getTaxTotalAttribute()
/**
* Get the invoices taxes total
*
* @return float
* @deprecated use getTotalTaxAttribute();
*/
public function getTaxTotalAttribute(): float
{
return $this->getTotalTaxAttribute();
}
/**
* Get the invoices taxes total
*
* @return float
*/
public function getTotalTaxAttribute(): float
{
if (! $this->_total_tax)
{
foreach ($this->items as $o)
{
foreach ($this->items as $o) {
if ($o->active)
$this->_total_tax += $this->currency()->round($o->tax);
}
}
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total_tax);
}
public function getTotalAttribute()
/**
* Invoice total due
*
* @return string
*/
public function getTotalAttribute(): float
{
if (! $this->_total)
{
foreach ($this->items as $o)
{
foreach ($this->items as $o) {
if ($o->active)
$this->_total += $this->currency()->round($o->total);
}
}
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total);
}

View File

@@ -4,12 +4,25 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\PushNew;
use App\Traits\NextKey;
use App\Interfaces\IDs;
use App\Traits\{NextKey,PushNew};
class Payment extends Model
/**
* Class Payment
* Payments that belong to an account
*
* Attributes for payments:
* + lid : Local ID for payment
* + payment_date : Date payment received
* + sid : System ID for payment
* + total : Payment total
*
* @package App\Models
*/
class Payment extends Model implements IDs
{
use NextKey,PushNew;
const RECORD_ID = 'payment';
public $incrementing = FALSE;
@@ -24,6 +37,8 @@ class Payment extends Model
// Array of items that can be updated with PushNew
protected $pushable = ['items'];
/* RELATIONS */
public function account()
{
return $this->belongsTo(Account::class);
@@ -34,11 +49,37 @@ class Payment extends Model
return $this->hasMany(PaymentItem::class);
}
/* ATTRIBUTES */
/**
* @return mixed
* @deprecated use date_payment directly.
*/
public function getDatePaidAttribute()
{
return $this->date_payment->format('Y-m-d');
}
/**
* Payment Local ID
*
* @return string
*/
public function getLIDattribute(): string
{
return sprintf('%06s',$this->id);
}
/**
* Payment System ID
*
* @return string
*/
public function getSIDAttribute(): string
{
return sprintf('%02s-%04s#%s',$this->site_id,$this->account_id,$this->getLIDattribute());
}
public function getTotalAttribute()
{
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total_amt);

View File

@@ -4,12 +4,22 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Interfaces\IDs;
use App\Traits\NextKey;
use Illuminate\Support\Facades\Log;
class Product extends Model
/**
* Class Product
* Products that are available to sale, and appear on invoices
*
* Attributes for products:
* + lid : Local ID for product (part number)
*
* @package App\Models
*/
class Product extends Model implements IDs
{
use NextKey;
const RECORD_ID = 'product';
@@ -97,6 +107,16 @@ class Product extends Model
return $this->description($this->getDefaultLanguage());
}
/**
* Product Local ID
*
* @return string
*/
public function getLIDattribute(): string
{
return sprintf('%04s',$this->id);
}
public function getMinimumCostAttribute()
{
$table = [
@@ -165,6 +185,16 @@ class Product extends Model
return Arr::get($this->price_array,sprintf('%s.1.price_setup',$this->price_recurr_default))*1.1;
}
/**
* Product System ID
*
* @return string
*/
public function getSIDattribute(): string
{
return sprintf('%02s-%s',$this->site_id,$this->getLIDattribute());
}
/**
* Return if this product captures usage data
*

View File

@@ -12,17 +12,30 @@ use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Leenooks\Carbon;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Leenooks\Carbon;
use App\Interfaces\IDs;
use App\Traits\NextKey;
class Service extends Model
/**
* Class Service
* Services that belong to an account
*
* Attributes for services:
* + name_short : Service Product short name, eg: phone number, domain name, certificate CN
* + sid : System ID for service
*
* @package App\Models
*/
class Service extends Model implements IDs
{
use NextKey;
const RECORD_ID = 'service';
public $incrementing = FALSE;
protected $table = 'ab_service';
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
@@ -51,8 +64,6 @@ class Service extends Model
];
public $dateFormat = 'U';
protected $table = 'ab_service';
protected $visible = [
'account_name',
'admin_service_id_url',
@@ -268,6 +279,7 @@ class Service extends Model
->where('item_type','=',0)
->orderBy('date_start');
// @todo Change to $query->active();
if ($active)
$query->where('active','=',TRUE);
@@ -344,7 +356,7 @@ class Service extends Model
public function scopeActive($query)
{
return $query->where(function () use ($query) {
$query->where('active',TRUE)
$query->where($this->getTable().'.active',TRUE)
->orWhereNotIn('order_status',$this->inactive_status);
});
}
@@ -358,7 +370,7 @@ class Service extends Model
public function scopeInActive($query)
{
return $query->where(function () use ($query) {
$query->where('active',FALSE)
$query->where($this->getTable().'.active',FALSE)
->orWhereIn('order_status',$this->inactive_status);
});
}
@@ -391,6 +403,7 @@ class Service extends Model
* Name of the account for this service
*
* @return mixed
* @deprecated use $this->>account->name directly
*/
public function getAccountNameAttribute(): string
{
@@ -409,6 +422,7 @@ class Service extends Model
* Return the auto billing details
*
* @return mixed
* @deprecated use billing directly?
*/
public function getAutoPayAttribute()
{
@@ -454,6 +468,7 @@ class Service extends Model
* Date the service expires, also represents when it is paid up to
*
* @return string
* @todo
*/
public function getExpiresAttribute(): string
{
@@ -613,6 +628,16 @@ class Service extends Model
return $result;
}
/**
* Service Local ID
*
* @return string
*/
public function getLIDattribute(): string
{
return sprintf('%05s',$this->id);
}
public function getNameAttribute(): string
{
return $this->product->name_short.': '.$this->getNameShortAttribute();
@@ -670,6 +695,7 @@ class Service extends Model
/**
* Get the Product's Category for this service
*
* @deprecated use product->category directly
*/
public function getProductCategoryAttribute(): string
{
@@ -680,6 +706,7 @@ class Service extends Model
* Get the Product's Short Name for the service
*
* @return string
* @deprecated use product->name directly
*/
public function getProductNameAttribute(): string
{
@@ -717,13 +744,13 @@ class Service extends Model
}
/**
* Services Unique Identifier
* Services System ID
*
* @return string
*/
public function getSIDAttribute(): string
{
return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id);
return sprintf('%02s-%04s.%s',$this->site_id,$this->account_id,$this->getLIDattribute());
}
/**
@@ -849,17 +876,19 @@ class Service extends Model
/* SETTERS */
// @todo is this required?
public function setDateOrigAttribute($value)
{
$this->attributes['date_orig'] = $value->timestamp;
}
// @todo is this required?
public function setDateLastAttribute($value)
{
$this->attributes['date_last'] = $value->timestamp;
}
/* FUNCTIONS */
/* GENERAL METHODS */
// The action methods will return: NULL for no progress|FALSE for a failed status|next stage name.

View File

@@ -87,9 +87,11 @@ class Site extends Model
public function getSiteLogoAttribute()
{
$return = $this->getSiteDetailValue('site_logo')->value;
//$return = $this->getSiteDetailValue('site_logo')->value;
// @todo Get from DB.
$return = 'site/1/gth-horseradishfont-full.png';
return $return ? 'storage/'.$return : '/image/generic/150/20/fff';
return $return ? '/storage/'.$return : '/image/generic/150/20/fff';
}
private function getSiteDetailValue($key)

View File

@@ -121,7 +121,8 @@ class User extends Authenticatable
*/
public function invoices()
{
return $this->hasManyThrough(Invoice::class,Account::class);
return $this->hasManyThrough(Invoice::class,Account::class)
->active();
}
/**
@@ -141,7 +142,8 @@ class User extends Authenticatable
*/
public function services()
{
return $this->hasManyThrough(Service::class,Account::class);
return $this->hasManyThrough(Service::class,Account::class)
->active();
}
/**
@@ -241,20 +243,6 @@ class User extends Authenticatable
->reverse();
}
/**
* The users active services
*
* @return mixed
*/
public function getServicesActiveAttribute()
{
return $this->services
->filter(function($item)
{
return $item->isActive();
});
}
public function getServicesCountHtmlAttribute()
{
return sprintf('%s <small>/%s</small>',$this->services->where('active',TRUE)->count(),$this->services->count());
@@ -523,7 +511,7 @@ class User extends Authenticatable
DB::raw('ROUND(ab_invoice_item_tax.amount,2) AS tax'),
])
->join('ab_invoice_item_tax',['ab_invoice_item_tax.invoice_item_id'=>'ab_invoice_item.id'])
->leftjoin('ab_invoice_item_tax',['ab_invoice_item_tax.invoice_item_id'=>'ab_invoice_item.id'])
->where('active',TRUE);
}
@@ -548,6 +536,7 @@ class User extends Authenticatable
* Return an SQL query that will summarise invoices with payments
*
* @return \Illuminate\Database\Query\Builder
* @todo change this to just return outstanding invoices as a collection.
*/
public function query_invoice_summary()
{