diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 3fa53ef..b7e02d7 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -24,7 +24,7 @@ class ProductController extends Controller return Product\Broadband::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; }) + ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) ->sortBy('name') ->values(); @@ -32,7 +32,7 @@ class ProductController extends Controller return Product\Domain::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; }) + ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) ->sortBy('name') ->values(); @@ -40,7 +40,7 @@ class ProductController extends Controller return Product\Email::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; }) + ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) ->sortBy('name') ->values(); @@ -48,7 +48,7 @@ class ProductController extends Controller return Product\Generic::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; }) + ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) ->sortBy('name') ->values(); @@ -56,7 +56,7 @@ class ProductController extends Controller return Product\Host::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; }) + ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) ->sortBy('name') ->values(); @@ -64,7 +64,7 @@ class ProductController extends Controller return Product\Phone::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; }) + ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) ->sortBy('name') ->values(); diff --git a/app/Jobs/ImportCosts.php b/app/Jobs/ImportCosts.php index d1fd70e..3f60fd9 100644 --- a/app/Jobs/ImportCosts.php +++ b/app/Jobs/ImportCosts.php @@ -161,7 +161,7 @@ class ImportCosts implements ShouldQueue break; - case 'phone': + case 'telephone': $to = Cost\Phone::where('site_id',$this->co->site_id) ->where('cost_id',$this->co->id) ->where('service_phone_id',$so->type->id) diff --git a/app/Mail/OrderRequestApprove.php b/app/Mail/OrderRequestApprove.php index 48208b5..eb41159 100644 --- a/app/Mail/OrderRequestApprove.php +++ b/app/Mail/OrderRequestApprove.php @@ -40,10 +40,10 @@ class OrderRequestApprove extends Mailable // @todo This is not consistent with Cancel/Change Request switch ($this->service->category) { - case 'BROADBAND': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_address); + case 'broadband': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_address); break; - case 'PHONE': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_number); + case 'telephone': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_number); break; default: diff --git a/app/Models/Product.php b/app/Models/Product.php index f04bc35..2dbcaf4 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -6,11 +6,12 @@ use Awobaz\Compoships\Compoships; use Illuminate\Container\Container; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; -use Illuminate\Http\Request; use Illuminate\Support\Facades\File; +use Illuminate\Support\Str; use Leenooks\Traits\ScopeActive; use App\Interfaces\{IDs,ProductItem}; @@ -166,7 +167,7 @@ class Product extends Model implements IDs */ public function getBaseChargeAttribute(int $timeperiod=NULL,Group $go=NULL): float { - return $this->getCharge('base',$timeperiod,$go); + return $this->_charge('base',$timeperiod,$go); } /** @@ -176,6 +177,7 @@ class Product extends Model implements IDs * @param Group|null $go * @param Collection|NULL $taxes * @return float + * @deprecated move to account::tax_calc */ public function getBaseChargeTaxableAttribute(int $timeperiod=NULL,Group $go=NULL,Collection $taxes=NULL): float { @@ -189,7 +191,7 @@ class Product extends Model implements IDs */ public function getBaseCostAttribute(): float { - return round($this->getSuppliedAttribute()->base_cost*Invoice::billing_change($this->getSuppliedAttribute()->getBillingIntervalAttribute(),$this->getBillingIntervalAttribute()) ?: 0,2); + return $this->supplied->base_cost*Invoice::billing_change($this->supplied->billing_interval,$this->billing_interval) ?: 0; } /** @@ -197,6 +199,7 @@ class Product extends Model implements IDs * * @param Collection|NULL $taxes * @return float + * @deprecated move to account::tax_calc */ public function getBaseCostTaxableAttribute(Collection $taxes=NULL): float { @@ -211,20 +214,22 @@ class Product extends Model implements IDs */ public function getBillingIntervalAttribute(): int { - return max($this->price_recur_default,$this->getSuppliedAttribute()->getBillingIntervalAttribute()); + return max($this->price_recur_default,$this->supplied->billing_interval); + } + + /** + * This will return the category of the product (eg: domain, webhosting, etc) which is the basis for all + * other logic of these types. + * + * @return string + */ + public function getCategoryAttribute(): string + { + return strtolower(Str::studly($this->category_name)); } /** * Return the type of service is provided. eg: Broadband, Phone. - * - * @return string|null - */ - public function getCategoryAttribute(): ?string - { - return $this->supplied ? $this->supplied->getCategoryAttribute() : NULL; - } - - /** * This will return the category of the product (eg: domain, hosting, etc) which is the basis for all * other logic of these types. * @@ -232,7 +237,7 @@ class Product extends Model implements IDs */ public function getCategoryNameAttribute(): string { - return $this->supplied->getCategoryNameAttribute(); + return $this->supplied->category_name; } /** @@ -242,7 +247,7 @@ class Product extends Model implements IDs */ public function getContractTermAttribute(): int { - return $this->type->getContractTermAttribute(); + return $this->type->contract_term; } /** @@ -252,11 +257,11 @@ class Product extends Model implements IDs */ public function getDescriptionAttribute(): string { - return (($x=$this->translate) && $x->description) ? $x->description : 'No Description'; + return $this->translate->description; } /** - * Get the minimum cost of this product + * Get the minimum charge for this product * * @param int|null $timeperiod * @param Group|null $go @@ -264,7 +269,7 @@ class Product extends Model implements IDs */ public function getMinChargeAttribute(int $timeperiod=NULL,Group $go=NULL): float { - return $this->getSetupChargeAttribute($timeperiod,$go)+$this->getBaseChargeAttribute($timeperiod,$go)*Invoice::billing_term($this->getContractTermAttribute(),$this->getBillingIntervalAttribute()); + return $this->getSetupChargeAttribute($timeperiod,$go)+$this->getBaseChargeAttribute($timeperiod,$go)*Invoice::billing_term($this->type->contract_term,$this->getBillingIntervalAttribute()); } /** @@ -274,6 +279,7 @@ class Product extends Model implements IDs * @param Group|null $go * @param Collection|NULL $taxes * @return float + * @deprecated move to account::tax_calc */ public function getMinChargeTaxableAttribute(int $timeperiod=NULL,Group $go=NULL,Collection $taxes=NULL): float { @@ -297,7 +303,7 @@ class Product extends Model implements IDs */ public function getNameDetailAttribute(): string { - return $this->translate ? $this->translate->name_detail : 'Unknown Name'; + return $this->translate->name_detail; } /** @@ -307,30 +313,29 @@ class Product extends Model implements IDs */ public function getNameShortAttribute(): string { - return $this->translate ? $this->translate->name_short : 'Unknown PID'; - } - - /** - * Suppliers - * - * @return Model|null - */ - public function getSupplierAttribute(): ?Model - { - return $this->getSuppliedAttribute() ? $this->getSuppliedAttribute()->supplier_detail->supplier : NULL; + return $this->translate->name_short; } /** * Suppliers product * * @return Model - * @todo make internal as other values off this attribute are used, not the model itself */ public function getSuppliedAttribute(): Model { return $this->type->supplied; } + /** + * Suppliers of this product + * + * @return Model|null + */ + public function getSupplierAttribute(): ?Model + { + return $this->supplied->supplier; + } + /** * The charge to setup this service * @@ -340,7 +345,7 @@ class Product extends Model implements IDs */ public function getSetupChargeAttribute(int $timeperiod=NULL,Group $go=NULL): float { - return $this->getCharge('setup',$timeperiod,$go); + return $this->_charge('setup',$timeperiod,$go); } /** @@ -350,6 +355,7 @@ class Product extends Model implements IDs * @param Group|null $go * @param Collection|null $taxes * @return float + * @deprecated move to account::tax_calc */ public function getSetupChargeTaxableAttribute(int $timeperiod=NULL,Group $go=NULL,Collection $taxes=NULL): float { @@ -357,13 +363,13 @@ class Product extends Model implements IDs } /** - * The charge to setup this service + * The cost to setup this service * * @return float */ public function getSetupCostAttribute(): float { - return $this->getSuppliedAttribute()->setup_cost ?: 0; + return $this->supplied->setup_cost ?: 0; } /** @@ -371,6 +377,7 @@ class Product extends Model implements IDs * * @param Collection|null $taxes * @return float + * @deprecated move to account::tax_calc */ public function getSetupCostTaxableAttribute(Collection $taxes=NULL): float { @@ -379,6 +386,48 @@ class Product extends Model implements IDs /* METHODS */ + /** + * Get a charge value from the pricing array + * + * This will traverse the groups to the parent group to find an appropriate charge, if one is not found, + * it will extrapolate a charge from the parent group, from another time period. + * + * @param string $type + * @param int|NULL $timeperiod + * @param Group|NULL $go + * @return float + */ + private function _charge(string $type,int $timeperiod=NULL,Group $go=NULL): float + { + // We'll cache this for performance + static $default = NULL; + + // All public users + if (is_null($default)) + $default = Group::whereNull('parent_id')->firstOrFail(); + + if (! $go) + $go = $default; + + if (is_null($timeperiod)) + $timeperiod = $this->getBillingIntervalAttribute(); + + // If the price doesnt exist for $go->id, use $go->id = 0 which is all users. + if (! $price=$this->charge($timeperiod,$go,$type)) { + $alt_tp = $timeperiod; + + // Traverse up our timeperiods if the default group to see if there is another time period that has the value + while (is_null($price=$this->charge($alt_tp,$default,$type)) && ($alt_tp >= 0)) + $alt_tp--; + + // If we havent got a price, we'll extrapolate one, except for setup charges + if (! is_null($price) && ($alt_tp !== $timeperiod) && ($type !== 'setup')) + $price = $price*Invoice::billing_change($alt_tp,$timeperiod); + } + + return round($price,2); + } + /** * Return the charge from the pricing table for the specific time period and group * @@ -408,50 +457,13 @@ class Product extends Model implements IDs * * @note: By definition products are normalised, as their cost price is based on the default billing interval * @return float + * @todo Move tax calculations out */ public function cost_normalized(): float { return number_format(Tax::tax_calc($this->supplied->base_cost,config('site')->taxes),2); } - /** - * Get a charge value from the pricing array - * - * @param string $type - * @param int|NULL $timeperiod - * @param Group|NULL $go - * @return float - * @todo use self::charge() - */ - private function getCharge(string $type,int $timeperiod=NULL,Group $go=NULL): float - { - static $default = NULL; - if (! $go) { - if (is_null($default)) - $default = Group::whereNull('parent_id')->firstOrFail(); // All public users - - $go = $default; - } - - if (is_null($timeperiod)) - $timeperiod = $this->getBillingIntervalAttribute(); - - // If the price doesnt exist for $go->id, use $go->id = 0 which is all users. - if (! $price=Arr::get($this->pricing,sprintf('%d.%d.%s',$timeperiod,$go->id,$type))) { - $alt_tp = $timeperiod; - - while (is_null($price=Arr::get($this->pricing,sprintf('%d.%d.%s',$alt_tp,0,$type))) && ($alt_tp >= 0)) { - $alt_tp--; - } - - // If we havent got a price, we'll extrapolate one, except for setup charges - if (! is_null($price) && ($alt_tp !== $timeperiod) && ($type !== 'setup')) - $price = $price*Invoice::billing_change($alt_tp,$timeperiod); - } - - return round($price,2); - } - /** * Return if this product captures usage data * @@ -466,7 +478,7 @@ class Product extends Model implements IDs * When receiving an order, validate that we have all the required information for the product type * * @param Request $request - * @return mixed + * @return Model|null */ public function orderValidation(Request $request): ?Model { diff --git a/app/Models/Product/Type.php b/app/Models/Product/Type.php index 17a7d38..91690b5 100644 --- a/app/Models/Product/Type.php +++ b/app/Models/Product/Type.php @@ -37,31 +37,6 @@ abstract class Type extends Model return $this->hasOne(static::SupplierModel,'id','supplier_item_id'); } - /** - * This will return the category of the product (eg: domain, hosting, etc) which is the basis for all - * other logic of these types. - * - * @return string - * @deprecated - can this be replaced with product->supplied->category? - */ - final public function getCategoryAttribute(): string - { - abort(500,'use product->supplied->category_name'); - return strtolower((new \ReflectionClass($this))->getShortName()); - } - - /** - * Return a friendly name for this product, used for display - * - * @return string - * @deprecated - can this be replaced with product->supplied->category_name - */ - final public function getCategoryNameAttribute(): string - { - abort(500,'use product->supplied->category_name'); - return static::category_name; - } - /* METHODs */ final function normalizeBillingInterval(): int diff --git a/app/Models/ProductTranslate.php b/app/Models/ProductTranslate.php index 6e073fc..79ffb41 100644 --- a/app/Models/ProductTranslate.php +++ b/app/Models/ProductTranslate.php @@ -9,4 +9,9 @@ class ProductTranslate extends Model protected $table = 'product_translate'; public $timestamps = FALSE; + + public function getDescriptionAttribute(string $val): string + { + return $val ?: 'No Description'; + } } \ No newline at end of file diff --git a/app/Models/Supplier/Type.php b/app/Models/Supplier/Type.php index 98070c2..3348241 100644 --- a/app/Models/Supplier/Type.php +++ b/app/Models/Supplier/Type.php @@ -84,4 +84,9 @@ abstract class Type extends Model { return Tax::tax_calc($this->attributes['setup_cost'],config('site')->taxes); } + + public function getSupplierAttribute(): Model + { + return $this->supplier_detail->supplier; + } } \ No newline at end of file diff --git a/database/migrations/2023_05_05_104307_product_id_increase.php b/database/migrations/2023_05_05_104307_product_id_increase.php new file mode 100644 index 0000000..71e0563 --- /dev/null +++ b/database/migrations/2023_05_05_104307_product_id_increase.php @@ -0,0 +1,33 @@ +sid }} | | Product | {{ $service->product->name }} | @switch($service->category) -@case('Broadband') +@case('broadband') | Address | {{ $service->type->service_address }} | @break; -@case('Phone') +@case('telephone') | Number | {{ $service->type->service_number }} | | Supplier Details | {{ $service->order_info->join(':') }} | @break; diff --git a/resources/views/email/admin/order/reject.blade.php b/resources/views/email/admin/order/reject.blade.php index 206d780..2fc054f 100644 --- a/resources/views/email/admin/order/reject.blade.php +++ b/resources/views/email/admin/order/reject.blade.php @@ -10,10 +10,10 @@ | Service ID | {{ $service->sid }} | | Product | {{ $service->product->name }} | @switch($service->category) -@case('BROADBAND') +@case('broadband') | Address | {{ is_object($service->type) ? $service->type->service_address : 'Not Supplied' }} | @break; -@case('PHONE') +@case('telephone') | Address | {{ is_object($service->type) ? $service->type->service_address : 'Not Supplied' }} | | Supplier Details | {{ join(':',$service->order_info) }} | @break; diff --git a/resources/views/email/admin/service/cancel.blade.php b/resources/views/email/admin/service/cancel.blade.php index 7c15174..af346fc 100644 --- a/resources/views/email/admin/service/cancel.blade.php +++ b/resources/views/email/admin/service/cancel.blade.php @@ -12,7 +12,7 @@ Please cancel the following... @case('broadband') | Address | {{ $service->type->service_address }} | @break; -@case('phone') +@case('telephone') | Number | {{ $service->type->service_number }} | | Supplier Details | {{ $service->order_info->join(':') }} | @break; diff --git a/resources/views/email/admin/service/change.blade.php b/resources/views/email/admin/service/change.blade.php index d57192a..254575e 100644 --- a/resources/views/email/admin/service/change.blade.php +++ b/resources/views/email/admin/service/change.blade.php @@ -9,10 +9,10 @@ Please change the following... | Service ID | {{ $service->sid }} | | Product | {{ $service->product->name }} | @switch($service->category) -@case('Broadband') +@case('broadband') | Address | {{ $service->type->service_address }} | @break; -@case('Phone') +@case('telephone') | Number | {{ $service->type->service_number }} | | Supplier Details | {{ $service->order_info->join(':') }} | @break; diff --git a/resources/views/theme/backend/adminlte/service/widget/internal.blade.php b/resources/views/theme/backend/adminlte/service/widget/internal.blade.php index fdd2b72..b24c8a3 100644 --- a/resources/views/theme/backend/adminlte/service/widget/internal.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/internal.blade.php @@ -4,7 +4,7 @@