feat(product): add show_in_list toggle and TV price formula

- New `show_in_list` column to control product visibility in customer list
- New `tv_price_formula_enabled` + `tv_price_formula` columns for separate TV pricing
- FormulaPriceService: isTvEnabled() and calculateTvForProduct()
- listTvProducts applies TV formula overrides before returning
- filterProductData handles both new field sets

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-19 11:51:54 +03:30
co-authored by Cursor
parent b0296c6e94
commit a321ebdaad
7 changed files with 348 additions and 0 deletions
@@ -230,6 +230,7 @@ class ProductController extends SuperController
$products = Product::with(['file'])->where('active', 1)->get();
if ($this->isCustomerApiRequest()) {
$products = $this->filterProductsForCustomerList($products);
$this->applyCustomerShow($products);
}
@@ -278,6 +279,8 @@ class ProductController extends SuperController
});
}
$this->applyTvFormulaOverrides($products);
return response()->json($products->values()->all());
}
@@ -584,6 +587,7 @@ class ProductController extends SuperController
public function customerStoreProductListPayload(): array
{
$products = Product::with(['file'])->where('active', 1)->get();
$products = $this->filterProductsForCustomerList($products);
$categoryService = app(CategoryService::class);
$products = $categoryService->filterProductsByVisibleCategories($products);
$products = $categoryService->sortProductsForStore($products);
@@ -592,6 +596,67 @@ class ProductController extends SuperController
return $products->values()->all();
}
/**
* @param \Illuminate\Support\Collection<int, Product>|iterable<Product> $products
* @return \Illuminate\Support\Collection<int, Product>
*/
/**
* اگر محصول فرمول TV فعال دارد، قیمت را با فرمول TV جایگزین می‌کند.
* این فقط روی خروجی TV اعمال می‌شود و قیمت عادی محصول را تغییر نمی‌دهد.
*/
private function applyTvFormulaOverrides($products): void
{
if (! Product::hasTvPriceFormulaColumns()) {
return;
}
$zaarbox = app(ZaarboxPriceService::class);
$feed = $zaarbox->cachedPriceList(false);
foreach ($products as $product) {
if (! ($product instanceof Product) || ! FormulaPriceService::isTvEnabled($product)) {
continue;
}
$mazanehRow = null;
if ($product->reading_method && is_array($feed)) {
$row = $zaarbox->findFeedRow($feed, (string) $product->reading_method);
if ($row) {
$parts = $zaarbox->resolveReferenceBaseProfit($row);
$mazanehRow = ['price' => $parts['price'], 'active' => true];
}
}
if ($mazanehRow === null) {
$livePrice = app(ProductDisplayPriceService::class)->resolveAppBasePrice();
if ($livePrice > 0) {
$mazanehRow = ['price' => $livePrice, 'active' => true];
}
}
$computed = FormulaPriceService::calculateTvForProduct($product, $mazanehRow);
if ($computed !== null && $computed > 0) {
$product->setAttribute('price', $computed);
$product->setAttribute('price_profit', 0);
}
}
}
private function filterProductsForCustomerList($products)
{
$collection = $products instanceof \Illuminate\Support\Collection
? $products
: collect($products);
if (! Product::hasShowInListColumn()) {
return $collection;
}
return $collection->filter(function (Product $product) {
return (bool) $product->show_in_list;
})->values();
}
private function applyCustomerShow($products): void
{
$products = $products instanceof \Illuminate\Support\Collection
@@ -695,6 +760,12 @@ class ProductController extends SuperController
$data['show_on_tv'] = !empty($data['show_on_tv']);
}
if (!Product::hasShowInListColumn()) {
unset($data['show_in_list']);
} elseif (array_key_exists('show_in_list', $data)) {
$data['show_in_list'] = !empty($data['show_in_list']);
}
if (!Product::hasUseLastPriceIfZeroColumn()) {
unset($data['use_last_price_if_zero']);
} elseif (array_key_exists('use_last_price_if_zero', $data)) {
@@ -756,6 +827,18 @@ class ProductController extends SuperController
}
}
if (!Product::hasTvPriceFormulaColumns()) {
unset($data['tv_price_formula_enabled'], $data['tv_price_formula']);
} else {
$tvFormulaEnabled = filter_var($data['tv_price_formula_enabled'] ?? false, FILTER_VALIDATE_BOOLEAN);
$tvFormula = trim((string) ($data['tv_price_formula'] ?? ''));
$showOnTv = !empty($data['show_on_tv'] ?? false);
$data['tv_price_formula_enabled'] = $tvFormulaEnabled && $showOnTv;
$data['tv_price_formula'] = $tvFormulaEnabled && $tvFormula !== ''
? $tvFormula
: ($tvFormulaEnabled ? FormulaPriceService::DEFAULT_FORMULA : ($tvFormula !== '' ? $tvFormula : null));
}
if (!Product::hasInventoryColumn()) {
unset($data['inventory']);
}
+53
View File
@@ -20,6 +20,7 @@ class Product extends Model
private static ?bool $orderLimitColumnsExist = null;
private static ?bool $showInAssetsColumnExists = null;
private static ?bool $showOnTvColumnExists = null;
private static ?bool $showInListColumnExists = null;
private static ?bool $useLastPriceIfZeroColumnExists = null;
private static ?bool $directPriceColumnExists = null;
private static ?bool $appPriceColumnExists = null;
@@ -29,6 +30,7 @@ class Product extends Model
private static ?bool $cashFeePercentModeColumnsExist = null;
private static ?bool $physicalDeliveryColumnsExist = null;
private static ?bool $descriptionColumnExists = null;
private static ?bool $tvPriceFormulaColumnsExist = null;
private static ?bool $priceFormulaColumnsExist = null;
protected static function booted(): void
@@ -50,6 +52,9 @@ class Product extends Model
if (!static::hasShowOnTvColumn()) {
unset($product->attributes['show_on_tv']);
}
if (!static::hasShowInListColumn()) {
unset($product->attributes['show_in_list']);
}
if (!static::hasUseLastPriceIfZeroColumn()) {
unset($product->attributes['use_last_price_if_zero']);
}
@@ -89,9 +94,32 @@ class Product extends Model
$product->attributes['price_formula']
);
}
if (!static::hasTvPriceFormulaColumns()) {
unset(
$product->attributes['tv_price_formula_enabled'],
$product->attributes['tv_price_formula']
);
}
});
}
public static function hasTvPriceFormulaColumns(): bool
{
if (static::$tvPriceFormulaColumnsExist !== null) {
return static::$tvPriceFormulaColumnsExist;
}
try {
$table = (new static)->getTable();
static::$tvPriceFormulaColumnsExist = Schema::hasColumn($table, 'tv_price_formula_enabled')
&& Schema::hasColumn($table, 'tv_price_formula');
} catch (\Throwable) {
static::$tvPriceFormulaColumnsExist = false;
}
return static::$tvPriceFormulaColumnsExist;
}
public static function hasPriceFormulaColumns(): bool
{
if (static::$priceFormulaColumnsExist !== null) {
@@ -253,6 +281,24 @@ class Product extends Model
return static::$showOnTvColumnExists;
}
public static function hasShowInListColumn(): bool
{
if (static::$showInListColumnExists !== null) {
return static::$showInListColumnExists;
}
try {
static::$showInListColumnExists = Schema::hasColumn(
(new static)->getTable(),
'show_in_list'
);
} catch (\Throwable) {
static::$showInListColumnExists = false;
}
return static::$showInListColumnExists;
}
public static function hasUseLastPriceIfZeroColumn(): bool
{
if (static::$useLastPriceIfZeroColumnExists !== null) {
@@ -436,6 +482,7 @@ class Product extends Model
'show', // Whether to show the record (boolean).
'show_in_assets', // Show balance on customer assets page cards
'show_on_tv', // Show this product on customer /tv board
'show_in_list', // Show this product in customer product list
'trade_by_quantity', // UI معامله مقداری: ارزش کل + مقدار
'use_last_price_if_zero', // Keep last DB price when automatic provider returns zero
'open_sell', // Whether selling is open (boolean).
@@ -467,17 +514,21 @@ class Product extends Model
'app_price', // روشن: قیمت واحد از قیمت زندهٔ اپ هر گرم × وزن محاسبه می‌شود
'price_formula_enabled',
'price_formula',
'tv_price_formula_enabled',
'tv_price_formula',
];
protected $attributes = [
'show_in_assets' => false,
'show_on_tv' => false,
'show_in_list' => true,
'trade_by_quantity' => false,
'use_last_price_if_zero' => false,
'cash_fee_buy_is_percent' => false,
'cash_fee_sell_is_percent' => false,
'app_price' => false,
'price_formula_enabled' => false,
'tv_price_formula_enabled' => false,
];
protected $casts = [
@@ -486,6 +537,7 @@ class Product extends Model
'show' => 'boolean', // Cast to boolean.
'show_in_assets' => 'boolean',
'show_on_tv' => 'boolean',
'show_in_list' => 'boolean',
'trade_by_quantity' => 'boolean',
'use_last_price_if_zero' => 'boolean',
'override_trading_hours' => 'boolean',
@@ -510,6 +562,7 @@ class Product extends Model
'price_profit' => 'int',
'app_price' => 'boolean',
'price_formula_enabled' => 'boolean',
'tv_price_formula_enabled' => 'boolean',
];
static $METAL_ID = [
+35
View File
@@ -21,6 +21,41 @@ class FormulaPriceService
return (bool) ($product?->price_formula_enabled);
}
public static function isTvEnabled(?Product $product): bool
{
if (! Product::hasTvPriceFormulaColumns()) {
return false;
}
return (bool) ($product?->show_on_tv) && (bool) ($product?->tv_price_formula_enabled);
}
/**
* @param array<string, mixed>|null $mazanehRow
*/
public static function calculateTvForProduct(Product $product, ?array $mazanehRow): ?int
{
if (! self::isTvEnabled($product) || $mazanehRow === null) {
return null;
}
if (array_key_exists('active', $mazanehRow) && $mazanehRow['active'] == false) {
return null;
}
$p = (float) ($mazanehRow['price'] ?? 0);
if ($p <= 0) {
return null;
}
$formula = trim((string) ($product->tv_price_formula ?? ''));
if ($formula === '') {
$formula = self::DEFAULT_FORMULA;
}
return self::evaluate($formula, $p, self::resolveWeight($product->amount ?? 0));
}
/**
* P = قیمت وسط مرجع (بدون تفاوت ارائه‌دهنده / بدون price_offset_percent)
*