Files
sam-manage/app/Models/Rd/AiQuotePriceTable.php

79 lines
2.0 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Models\Rd;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
class AiQuotePriceTable extends Model
{
use BelongsToTenant;
protected $table = 'ai_quote_price_tables';
protected $fillable = [
'tenant_id',
'product_category',
'price_type',
'min_value',
'max_value',
'unit_price',
'labor_rate',
'install_rate',
'is_active',
'options',
];
protected $casts = [
'is_active' => 'boolean',
'options' => 'array',
'min_value' => 'decimal:4',
'max_value' => 'decimal:4',
'unit_price' => 'decimal:2',
'labor_rate' => 'decimal:2',
'install_rate' => 'decimal:2',
];
public function scopeActive($query)
{
return $query->where('is_active', true);
}
public function scopeByCategory($query, string $category)
{
return $query->where('product_category', $category);
}
public static function getPriceTablesForPrompt(?string $productCategory = null): array
{
$query = static::active()->orderBy('product_category')->orderBy('min_value');
if ($productCategory) {
$query->byCategory($productCategory);
}
return $query->get()->map(fn ($row) => [
'product_category' => $row->product_category,
'price_type' => $row->price_type,
'min_value' => (float) $row->min_value,
'max_value' => (float) $row->max_value,
'unit_price' => (float) $row->unit_price,
'labor_rate' => (float) $row->labor_rate,
'install_rate' => (float) $row->install_rate,
])->toArray();
}
public function getOption(string $key, $default = null)
{
return data_get($this->options, $key, $default);
}
public function setOption(string $key, $value): void
{
$options = $this->options ?? [];
data_set($options, $key, $value);
$this->options = $options;
$this->save();
}
}