From a321ebdaad9cf60edcd8187386ec4829b45c4947 Mon Sep 17 00:00:00 2001 From: adonis Date: Wed, 19 Aug 2026 11:51:54 +0330 Subject: [PATCH] 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 --- .../Controllers/Admin/ProductController.php | 83 +++++++++++++++++++ app/Models/Product.php | 53 ++++++++++++ app/Services/FormulaPriceService.php | 35 ++++++++ ...001_add_show_in_list_to_products_table.php | 45 ++++++++++ ...price_formula_fields_to_products_table.php | 40 +++++++++ database/sql/add_product_show_in_list.sql | 39 +++++++++ database/sql/deploy_safe_schema.sql | 53 ++++++++++++ 7 files changed, 348 insertions(+) create mode 100644 database/migrations/2026_08_19_000001_add_show_in_list_to_products_table.php create mode 100644 database/migrations/2026_08_19_000002_add_tv_price_formula_fields_to_products_table.php create mode 100644 database/sql/add_product_show_in_list.sql diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 7a9b978..fcda7ce 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -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|iterable $products + * @return \Illuminate\Support\Collection + */ + /** + * اگر محصول فرمول 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']); } diff --git a/app/Models/Product.php b/app/Models/Product.php index d154416..e289ce9 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -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 = [ diff --git a/app/Services/FormulaPriceService.php b/app/Services/FormulaPriceService.php index 96cdc18..30f3e39 100644 --- a/app/Services/FormulaPriceService.php +++ b/app/Services/FormulaPriceService.php @@ -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|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) * diff --git a/database/migrations/2026_08_19_000001_add_show_in_list_to_products_table.php b/database/migrations/2026_08_19_000001_add_show_in_list_to_products_table.php new file mode 100644 index 0000000..dec2adc --- /dev/null +++ b/database/migrations/2026_08_19_000001_add_show_in_list_to_products_table.php @@ -0,0 +1,45 @@ +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'); + }); + } +}; diff --git a/database/migrations/2026_08_19_000002_add_tv_price_formula_fields_to_products_table.php b/database/migrations/2026_08_19_000002_add_tv_price_formula_fields_to_products_table.php new file mode 100644 index 0000000..766eb78 --- /dev/null +++ b/database/migrations/2026_08_19_000002_add_tv_price_formula_fields_to_products_table.php @@ -0,0 +1,40 @@ +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); + } + }); + } +}; diff --git a/database/sql/add_product_show_in_list.sql b/database/sql/add_product_show_in_list.sql new file mode 100644 index 0000000..ee1614f --- /dev/null +++ b/database/sql/add_product_show_in_list.sql @@ -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; diff --git a/database/sql/deploy_safe_schema.sql b/database/sql/deploy_safe_schema.sql index 246b21f..365fe55 100644 --- a/database/sql/deploy_safe_schema.sql +++ b/database/sql/deploy_safe_schema.sql @@ -68,6 +68,36 @@ PREPARE stmt FROM @sql; EXECUTE 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 صفر است) -- --------------------------------------------------------------------------- @@ -555,6 +585,29 @@ PREPARE stmt FROM @sql; EXECUTE 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';