Add formula-based product pricing (P/W/MG) for Parsian-style unit prices.
Port FormulaPriceService from colleague app so admins can compute a single unit price from reference mid, weight, and wage instead of offset coefficients. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -95,6 +95,11 @@ class ProductDisplayPriceService
|
||||
continue;
|
||||
}
|
||||
|
||||
if (FormulaPriceService::isEnabled($product) && $product->reading_method && is_array($zaarboxFeed)) {
|
||||
$this->applyFormulaFeedPrice($product, $zaarboxFeed, $zaarbox);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->usesAppPrice($product)) {
|
||||
$base = $this->productUsesLastPriceIfZero($product)
|
||||
? $this->resolveAppBasePrice()
|
||||
@@ -308,6 +313,45 @@ class ProductDisplayPriceService
|
||||
return $this->extractBaseFromPayload($cached);
|
||||
}
|
||||
|
||||
/** @param array<int, array<string, mixed>> $feed */
|
||||
private function applyFormulaFeedPrice(Product $product, array $feed, ZaarboxPriceService $zaarbox): void
|
||||
{
|
||||
$useLast = $this->productUsesLastPriceIfZero($product);
|
||||
$row = $zaarbox->findFeedRow($feed, (string) $product->reading_method);
|
||||
if ($row === null) {
|
||||
$product->setAttribute('reference_provider_active', $useLast);
|
||||
$this->applyPriceOrFallback($product, 0, $useLast);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$parts = $zaarbox->resolveReferenceBaseProfit($row);
|
||||
$providerActive = $useLast || $parts['price'] > 0;
|
||||
$product->setAttribute('reference_provider_active', $providerActive);
|
||||
|
||||
if (! $useLast && $parts['price'] <= 0) {
|
||||
$this->zeroProductPrice($product);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$computed = FormulaPriceService::calculateForProduct($product, [
|
||||
'price' => $parts['price'],
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
if ($computed === null || $computed <= 0) {
|
||||
$this->applyPriceOrFallback($product, 0, $useLast);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$product->setAttribute('price', $computed);
|
||||
if (Schema::hasColumn($product->getTable(), 'price_profit')) {
|
||||
$product->setAttribute('price_profit', 0);
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<int, array<string, mixed>> $feed */
|
||||
private function applyReferenceFeedPrice(Product $product, array $feed, ZaarboxPriceService $zaarbox): void
|
||||
{
|
||||
@@ -410,6 +454,10 @@ class ProductDisplayPriceService
|
||||
}
|
||||
}
|
||||
|
||||
if (FormulaPriceService::isEnabled($product)) {
|
||||
return (int) (($product->price ?? 0) + ($product->price_profit ?? 0));
|
||||
}
|
||||
|
||||
if ((int) ($product->direct_price ?? 0) === 1) {
|
||||
return (int) (($product->price ?? 0) + ($product->price_profit ?? 0));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user