Move category/category_name product::class methods into __get(), no functional changes

This commit is contained in:
2025-05-22 12:01:31 +10:00
parent 04ae35b1dd
commit 5ef1a27a64
23 changed files with 63 additions and 85 deletions

View File

@@ -52,7 +52,7 @@ class ServiceList extends Command
if ((! $this->option('inactive')) && (! $o->is_active))
continue;
if ($this->option('type') && ($o->product->getCategoryAttribute() !== $this->option('type')))
if ($this->option('type') && ($o->product->category_lc !== strtolower($this->option('type'))))
continue;
$c = $o->invoiced_items
@@ -67,7 +67,7 @@ class ServiceList extends Command
$this->info(sprintf($header,
$o->lid,
$o->product->getCategoryNameAttribute(),
$o->product->category_name,
substr($o->product->getNameAttribute(),0,35),
substr($o->name_short,0,40),
$o->active ? 'active' : 'inactive',

View File

@@ -401,10 +401,10 @@ class ServiceController extends Controller
$request->post(),
collect($o->type->validation())
->keys()
->map(fn($item)=>sprintf('%s.%s',$o->product->category,$item))
->map(fn($item)=>sprintf('%s.%s',$o->product->category_lc,$item))
->combine(array_values($o->type->validation()))
->map(fn($item)=>is_string($item)
? preg_replace('/^exclude_without:/',sprintf('exclude_without:%s.',$o->product->category),$item)
? preg_replace('/^exclude_without:/',sprintf('exclude_without:%s.',$o->product->category_lc),$item)
: $item)
->merge(
[
@@ -416,7 +416,7 @@ class ServiceController extends Controller
'price' => 'nullable|numeric|min:0', // Price we charge the client, if we dont charge supplied/price
'cost' => 'nullable|numeric|min:0', // Price we are charged by supplier, if we arent charged supplier/price
'supplierid' => 'nullable|string|min:1', // As used on invoices
$o->product->category => 'array|min:1',
$o->product->category_lc => 'array|min:1',
]
)
->toArray()
@@ -432,13 +432,13 @@ class ServiceController extends Controller
$validated = collect($validator->validated());
// Store our service type values
$o->type->forceFill($validated->get($o->product->category));
$o->type->forceFill($validated->get($o->product->category_lc));
// Some special handling
switch ($o->product->category) {
switch ($o->product->category_lc) {
case 'broadband':
// If pppoe is not set, then we dont need username/password
$o->type->pppoe = ($x=data_get($validated,$o->product->category.'.pppoe',FALSE));
$o->type->pppoe = ($x=data_get($validated,$o->product->category_lc.'.pppoe',FALSE));
if (! $x) {
$o->type->service_username = NULL;
@@ -470,7 +470,7 @@ class ServiceController extends Controller
else {
// For broadband, start_at is connect_at in the type record
switch ($o->product->category) {
switch ($o->product->category_lc) {
case 'broadband':
$o->start_at = $o->type->connect_at;
break;

View File

@@ -131,7 +131,7 @@ class ImportCosts implements ShouldQueue
if ($so) {
// r[1] = Monthly Charge or Extra Charge,r[2] = "On Plan", r[3] = Plan Info
$r = [];
switch ($so->product->category) {
switch ($so->product->category_lc) {
case 'broadband':
$to = Cost\Broadband::where('site_id',$this->co->site_id)
->where('cost_id',$this->co->id)

View File

@@ -31,19 +31,17 @@ class OrderRequestApprove extends Mailable
/**
* Build the message.
*
* @return $this
*/
public function build()
{
Config::set('site',$this->so->site);
// @todo This is not consistent with Cancel/Change Request
switch ($this->so->product->category) {
case 'broadband': $subject = sprintf('%s: %s',$this->so->product->category,$this->so->type->service_address);
switch ($this->so->product->category_lc) {
case 'broadband': $subject = sprintf('%s: %s',$this->so->product->category_name,$this->so->type->service_address);
break;
case 'phone': $subject = sprintf('%s: %s',$this->so->product->category,$this->so->type->service_number);
case 'phone': $subject = sprintf('%s: %s',$this->so->product->category_name,$this->so->type->service_number);
break;
default:

View File

@@ -77,6 +77,10 @@ class Product extends Model implements IDs
return match ($key) {
'base_cost' => round($this->supplied->base_cost,2)*Invoice::billing_change($this->type->billing_interval,$this->billing_interval) ?: 0,
'category' => $this->supplied->category,
'category_lc' => strtolower($this->category),
'category_name' => $this->supplied->category_name,
default => parent::__get($key),
};
}
@@ -181,29 +185,6 @@ class Product extends Model implements IDs
return max($this->price_recur_default,$this->type->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.
* 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
*/
public function getCategoryNameAttribute(): string
{
return $this->supplied->category_name;
}
/**
* How long must this product be purchased for as a service.
*

View File

@@ -49,8 +49,8 @@ class Supplier extends Model
if ($so) {
// If we have a connections configuration for that supplier, then build the child relationships
if (Arr::get($so->detail->connections,$class->category)) {
$result->put($class->category,(object)[
if (Arr::get($so->detail->connections,$class->category_lc)) {
$result->put($class->category_lc,(object)[
'type' => $class->category_name,
'items' => $class->where('supplier_detail_id',$so->detail->id),
]);
@@ -63,13 +63,13 @@ class Supplier extends Model
$o->where('supplier_detail_id',$so->detail->id);
if ($o->count())
$result->put($class->category,(object)[
$result->put($class->category_lc,(object)[
'type' => $class->category_name,
'items' => $class->where('supplier_detail_id',$so->detail->id),
]);
} else {
$result->put($class->category_name,$class);
$result->put($class->category_lc,$class);
}
}

View File

@@ -10,10 +10,30 @@ use Leenooks\Traits\ScopeActive;
use App\Models\{Invoice, Supplier, SupplierDetail};
use App\Traits\SiteID;
/**
* Class Supplier\Type
*
* Attributes for supplier types:
* + category : This will return the category of the product (eg: domain, hosting, etc), based on the class, which is the basis for all other logic of these types.
* + category_name : A friendly name that can override category
*/
abstract class Type extends Model
{
use SiteID,ScopeActive;
protected const category_name = NULL;
public function __get($key): mixed
{
return match ($key) {
'category' => (new \ReflectionClass($this))->getShortName(),
'category_name' => static::category_name ?: $this->category,
default => parent::__get($key),
};
}
/* RELATIONS */
/**
@@ -33,27 +53,6 @@ abstract class Type extends Model
/* ATTRIBUTES */
/**
* 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
*/
final public function getCategoryAttribute(): string
{
return strtolower((new \ReflectionClass($this))->getShortName());
}
/**
* Return a friendly name for this product, used for display
*
* @return string
*/
final public function getCategoryNameAttribute(): string
{
return static::category_name;
}
final public function getContractTermAttribute(?int $val): int
{
return $val ?: 1;