40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Manage;
|
|
|
|
use App\Enums\PriceType;
|
|
use App\Models\CuttingResultPrice;
|
|
|
|
class CuttingResultPriceResolver
|
|
{
|
|
public function resolve(int $productVariantId, PriceType $priceType): ?CuttingResultPrice
|
|
{
|
|
return CuttingResultPrice::query()
|
|
->where('product_variant_id', $productVariantId)
|
|
->where('price_type', $priceType)
|
|
->whereHas('cutting', fn ($query) => $query->verified())
|
|
->join('cuttings', 'cutting_result_prices.cutting_id', '=', 'cuttings.id')
|
|
->orderByDesc('cuttings.created_at')
|
|
->select('cutting_result_prices.*')
|
|
->first();
|
|
}
|
|
|
|
/**
|
|
* @return list<CuttingResultPrice>
|
|
*/
|
|
public function latestPricesForVariant(int $productVariantId): array
|
|
{
|
|
$prices = [];
|
|
|
|
foreach (PriceType::cases() as $priceType) {
|
|
$price = $this->resolve($productVariantId, $priceType);
|
|
|
|
if ($price !== null) {
|
|
$prices[] = $price;
|
|
}
|
|
}
|
|
|
|
return $prices;
|
|
}
|
|
}
|