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(); $products = Product::with(['file'])->where('active', 1)->get();
if ($this->isCustomerApiRequest()) { if ($this->isCustomerApiRequest()) {
$products = $this->filterProductsForCustomerList($products);
$this->applyCustomerShow($products); $this->applyCustomerShow($products);
} }
@@ -278,6 +279,8 @@ class ProductController extends SuperController
}); });
} }
$this->applyTvFormulaOverrides($products);
return response()->json($products->values()->all()); return response()->json($products->values()->all());
} }
@@ -584,6 +587,7 @@ class ProductController extends SuperController
public function customerStoreProductListPayload(): array public function customerStoreProductListPayload(): array
{ {
$products = Product::with(['file'])->where('active', 1)->get(); $products = Product::with(['file'])->where('active', 1)->get();
$products = $this->filterProductsForCustomerList($products);
$categoryService = app(CategoryService::class); $categoryService = app(CategoryService::class);
$products = $categoryService->filterProductsByVisibleCategories($products); $products = $categoryService->filterProductsByVisibleCategories($products);
$products = $categoryService->sortProductsForStore($products); $products = $categoryService->sortProductsForStore($products);
@@ -592,6 +596,67 @@ class ProductController extends SuperController
return $products->values()->all(); 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 private function applyCustomerShow($products): void
{ {
$products = $products instanceof \Illuminate\Support\Collection $products = $products instanceof \Illuminate\Support\Collection
@@ -695,6 +760,12 @@ class ProductController extends SuperController
$data['show_on_tv'] = !empty($data['show_on_tv']); $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()) { if (!Product::hasUseLastPriceIfZeroColumn()) {
unset($data['use_last_price_if_zero']); unset($data['use_last_price_if_zero']);
} elseif (array_key_exists('use_last_price_if_zero', $data)) { } 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()) { if (!Product::hasInventoryColumn()) {
unset($data['inventory']); unset($data['inventory']);
} }
+53
View File
@@ -20,6 +20,7 @@ class Product extends Model
private static ?bool $orderLimitColumnsExist = null; private static ?bool $orderLimitColumnsExist = null;
private static ?bool $showInAssetsColumnExists = null; private static ?bool $showInAssetsColumnExists = null;
private static ?bool $showOnTvColumnExists = null; private static ?bool $showOnTvColumnExists = null;
private static ?bool $showInListColumnExists = null;
private static ?bool $useLastPriceIfZeroColumnExists = null; private static ?bool $useLastPriceIfZeroColumnExists = null;
private static ?bool $directPriceColumnExists = null; private static ?bool $directPriceColumnExists = null;
private static ?bool $appPriceColumnExists = null; private static ?bool $appPriceColumnExists = null;
@@ -29,6 +30,7 @@ class Product extends Model
private static ?bool $cashFeePercentModeColumnsExist = null; private static ?bool $cashFeePercentModeColumnsExist = null;
private static ?bool $physicalDeliveryColumnsExist = null; private static ?bool $physicalDeliveryColumnsExist = null;
private static ?bool $descriptionColumnExists = null; private static ?bool $descriptionColumnExists = null;
private static ?bool $tvPriceFormulaColumnsExist = null;
private static ?bool $priceFormulaColumnsExist = null; private static ?bool $priceFormulaColumnsExist = null;
protected static function booted(): void protected static function booted(): void
@@ -50,6 +52,9 @@ class Product extends Model
if (!static::hasShowOnTvColumn()) { if (!static::hasShowOnTvColumn()) {
unset($product->attributes['show_on_tv']); unset($product->attributes['show_on_tv']);
} }
if (!static::hasShowInListColumn()) {
unset($product->attributes['show_in_list']);
}
if (!static::hasUseLastPriceIfZeroColumn()) { if (!static::hasUseLastPriceIfZeroColumn()) {
unset($product->attributes['use_last_price_if_zero']); unset($product->attributes['use_last_price_if_zero']);
} }
@@ -89,9 +94,32 @@ class Product extends Model
$product->attributes['price_formula'] $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 public static function hasPriceFormulaColumns(): bool
{ {
if (static::$priceFormulaColumnsExist !== null) { if (static::$priceFormulaColumnsExist !== null) {
@@ -253,6 +281,24 @@ class Product extends Model
return static::$showOnTvColumnExists; 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 public static function hasUseLastPriceIfZeroColumn(): bool
{ {
if (static::$useLastPriceIfZeroColumnExists !== null) { if (static::$useLastPriceIfZeroColumnExists !== null) {
@@ -436,6 +482,7 @@ class Product extends Model
'show', // Whether to show the record (boolean). 'show', // Whether to show the record (boolean).
'show_in_assets', // Show balance on customer assets page cards 'show_in_assets', // Show balance on customer assets page cards
'show_on_tv', // Show this product on customer /tv board 'show_on_tv', // Show this product on customer /tv board
'show_in_list', // Show this product in customer product list
'trade_by_quantity', // UI معامله مقداری: ارزش کل + مقدار 'trade_by_quantity', // UI معامله مقداری: ارزش کل + مقدار
'use_last_price_if_zero', // Keep last DB price when automatic provider returns zero 'use_last_price_if_zero', // Keep last DB price when automatic provider returns zero
'open_sell', // Whether selling is open (boolean). 'open_sell', // Whether selling is open (boolean).
@@ -467,17 +514,21 @@ class Product extends Model
'app_price', // روشن: قیمت واحد از قیمت زندهٔ اپ هر گرم × وزن محاسبه می‌شود 'app_price', // روشن: قیمت واحد از قیمت زندهٔ اپ هر گرم × وزن محاسبه می‌شود
'price_formula_enabled', 'price_formula_enabled',
'price_formula', 'price_formula',
'tv_price_formula_enabled',
'tv_price_formula',
]; ];
protected $attributes = [ protected $attributes = [
'show_in_assets' => false, 'show_in_assets' => false,
'show_on_tv' => false, 'show_on_tv' => false,
'show_in_list' => true,
'trade_by_quantity' => false, 'trade_by_quantity' => false,
'use_last_price_if_zero' => false, 'use_last_price_if_zero' => false,
'cash_fee_buy_is_percent' => false, 'cash_fee_buy_is_percent' => false,
'cash_fee_sell_is_percent' => false, 'cash_fee_sell_is_percent' => false,
'app_price' => false, 'app_price' => false,
'price_formula_enabled' => false, 'price_formula_enabled' => false,
'tv_price_formula_enabled' => false,
]; ];
protected $casts = [ protected $casts = [
@@ -486,6 +537,7 @@ class Product extends Model
'show' => 'boolean', // Cast to boolean. 'show' => 'boolean', // Cast to boolean.
'show_in_assets' => 'boolean', 'show_in_assets' => 'boolean',
'show_on_tv' => 'boolean', 'show_on_tv' => 'boolean',
'show_in_list' => 'boolean',
'trade_by_quantity' => 'boolean', 'trade_by_quantity' => 'boolean',
'use_last_price_if_zero' => 'boolean', 'use_last_price_if_zero' => 'boolean',
'override_trading_hours' => 'boolean', 'override_trading_hours' => 'boolean',
@@ -510,6 +562,7 @@ class Product extends Model
'price_profit' => 'int', 'price_profit' => 'int',
'app_price' => 'boolean', 'app_price' => 'boolean',
'price_formula_enabled' => 'boolean', 'price_formula_enabled' => 'boolean',
'tv_price_formula_enabled' => 'boolean',
]; ];
static $METAL_ID = [ static $METAL_ID = [
+35
View File
@@ -21,6 +21,41 @@ class FormulaPriceService
return (bool) ($product?->price_formula_enabled); 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) * P = قیمت وسط مرجع (بدون تفاوت ارائه‌دهنده / بدون price_offset_percent)
* *
@@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
if (!Schema::hasTable('products')) {
return;
}
if (!Schema::hasColumn('products', 'show_in_list')) {
Schema::table('products', function (Blueprint $table) {
if (Schema::hasColumn('products', 'show_on_tv')) {
$table->boolean('show_in_list')->default(true)->after('show_on_tv');
} elseif (Schema::hasColumn('products', 'show_in_assets')) {
$table->boolean('show_in_list')->default(true)->after('show_in_assets');
} elseif (Schema::hasColumn('products', 'show')) {
$table->boolean('show_in_list')->default(true)->after('show');
} else {
$table->boolean('show_in_list')->default(true);
}
});
} else {
DB::statement(
'ALTER TABLE `products` MODIFY COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری'''
);
}
}
public function down(): void
{
if (!Schema::hasTable('products') || !Schema::hasColumn('products', 'show_in_list')) {
return;
}
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('show_in_list');
});
}
};
@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('products', function (Blueprint $table) {
if (! Schema::hasColumn('products', 'tv_price_formula_enabled')) {
if (Schema::hasColumn('products', 'price_formula')) {
$table->boolean('tv_price_formula_enabled')->default(false)->after('price_formula');
} else {
$table->boolean('tv_price_formula_enabled')->default(false);
}
}
if (! Schema::hasColumn('products', 'tv_price_formula')) {
$table->text('tv_price_formula')->nullable()->after('tv_price_formula_enabled');
}
});
}
public function down(): void
{
Schema::table('products', function (Blueprint $table) {
$drop = [];
if (Schema::hasColumn('products', 'tv_price_formula_enabled')) {
$drop[] = 'tv_price_formula_enabled';
}
if (Schema::hasColumn('products', 'tv_price_formula')) {
$drop[] = 'tv_price_formula';
}
if ($drop) {
$table->dropColumn($drop);
}
});
}
};
+39
View File
@@ -0,0 +1,39 @@
-- products.show_in_list — نمایش در لیست محصولات اپ مشتری
SET @db = DATABASE();
SET @sql = IF(
(SELECT COUNT(*) FROM information_schema.TABLES
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products') > 0
AND (SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'show_in_list') = 0,
IF(
(SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'show_on_tv') > 0,
'ALTER TABLE `products` ADD COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری'' AFTER `show_on_tv`',
IF(
(SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'show_in_assets') > 0,
'ALTER TABLE `products` ADD COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری'' AFTER `show_in_assets`',
IF(
(SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'show') > 0,
'ALTER TABLE `products` ADD COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری'' AFTER `show`',
'ALTER TABLE `products` ADD COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری'''
)
)
),
'SELECT ''skip: show_in_list exists'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sql = IF(
(SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'show_in_list') > 0,
'ALTER TABLE `products` MODIFY COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری''',
'SELECT ''skip: show_in_list modify'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
+53
View File
@@ -68,6 +68,36 @@ PREPARE stmt FROM @sql;
EXECUTE stmt; EXECUTE stmt;
DEALLOCATE PREPARE stmt; DEALLOCATE PREPARE stmt;
-- ---------------------------------------------------------------------------
-- products.show_in_list (لیست محصولات اپ مشتری)
-- ---------------------------------------------------------------------------
SET @sql = IF(
(SELECT COUNT(*) FROM information_schema.TABLES
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products') > 0
AND (SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'show_in_list') = 0,
IF(
(SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'show_on_tv') > 0,
'ALTER TABLE `products` ADD COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری'' AFTER `show_on_tv`',
IF(
(SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'show_in_assets') > 0,
'ALTER TABLE `products` ADD COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری'' AFTER `show_in_assets`',
IF(
(SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'show') > 0,
'ALTER TABLE `products` ADD COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری'' AFTER `show`',
'ALTER TABLE `products` ADD COLUMN `show_in_list` TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''نمایش در لیست محصولات مشتری'''
)
)
),
'SELECT ''skip products.show_in_list'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- --------------------------------------------------------------------------- -- ---------------------------------------------------------------------------
-- products.use_last_price_if_zero (fallback وقتی provider صفر است) -- products.use_last_price_if_zero (fallback وقتی provider صفر است)
-- --------------------------------------------------------------------------- -- ---------------------------------------------------------------------------
@@ -555,6 +585,29 @@ PREPARE stmt FROM @sql;
EXECUTE stmt; EXECUTE stmt;
DEALLOCATE PREPARE stmt; DEALLOCATE PREPARE stmt;
-- tv_price_formula_enabled / tv_price_formula ---
SET @sql = IF(
(SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'tv_price_formula_enabled') = 0,
CONCAT('ALTER TABLE `products` ADD COLUMN `tv_price_formula_enabled` TINYINT(1) NOT NULL DEFAULT 0 COMMENT ''فرمول قیمت TV فعال'' AFTER ',
IF((SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'price_formula') > 0,
'`price_formula`', '`show_on_tv`')),
'SELECT ''skip tv_price_formula_enabled'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sql = IF(
(SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'tv_price_formula') = 0,
'ALTER TABLE `products` ADD COLUMN `tv_price_formula` TEXT NULL COMMENT ''فرمول قیمت مخصوص TV'' AFTER `tv_price_formula_enabled`',
'SELECT ''skip tv_price_formula'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- --------------------------------------------------------------------------- -- ---------------------------------------------------------------------------
-- پایان — برای بورس‌مس (اختیاری، جدا اجرا کنید): -- پایان — برای بورس‌مس (اختیاری، جدا اجرا کنید):
-- UPDATE `settings` SET `value` = 'کیلوگرم' WHERE `key` = 'unit'; -- UPDATE `settings` SET `value` = 'کیلوگرم' WHERE `key` = 'unit';