From ed5bd74986c009582374d8d76546f01e3796846c Mon Sep 17 00:00:00 2001 From: adonis Date: Sat, 4 Jul 2026 22:06:20 +0330 Subject: [PATCH] added percent in product price 2 --- .../Controllers/Admin/ProductController.php | 29 +++++++++++++++ app/Models/Product.php | 4 +- database/sql/deploy_safe_schema.sql | 37 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 48f35af..3bfedce 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -667,6 +667,35 @@ class ProductController extends SuperController $data['override_trading_hours'] = !empty($data['override_trading_hours']); } + $data['cash_fee_buy'] = $this->normalizeCashFeeValue($data['cash_fee_buy'] ?? 0); + $data['cash_fee_sell'] = $this->normalizeCashFeeValue($data['cash_fee_sell'] ?? 0); + + if (Product::hasCashFeePercentModeColumns()) { + $data['cash_fee_buy_is_percent'] = filter_var( + $data['cash_fee_buy_is_percent'] ?? false, + FILTER_VALIDATE_BOOLEAN + ); + $data['cash_fee_sell_is_percent'] = filter_var( + $data['cash_fee_sell_is_percent'] ?? false, + FILTER_VALIDATE_BOOLEAN + ); + } else { + unset($data['cash_fee_buy_is_percent'], $data['cash_fee_sell_is_percent']); + } + return $data; } + + private function normalizeCashFeeValue(mixed $value): float + { + if ($value === null || $value === '') { + return 0.0; + } + + if (is_string($value)) { + $value = str_replace([',', ' ', '٬'], '', $value); + } + + return (float) $value; + } } diff --git a/app/Models/Product.php b/app/Models/Product.php index 96c74a3..73d57d9 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -353,8 +353,8 @@ class Product extends Model 'order_limit_enabled' => 'boolean', 'min_order_cash' => 'int', 'max_order_cash' => 'int', - 'cash_fee_buy' => 'int', // Cast to int or decimal if needed - 'cash_fee_sell' => 'int', // Cast to int or decimal if needed + 'cash_fee_buy' => 'float', + 'cash_fee_sell' => 'float', 'cash_fee_buy_is_percent' => 'boolean', 'cash_fee_sell_is_percent' => 'boolean', 'price_offset_percent' => 'float', diff --git a/database/sql/deploy_safe_schema.sql b/database/sql/deploy_safe_schema.sql index b581129..507c5a9 100644 --- a/database/sql/deploy_safe_schema.sql +++ b/database/sql/deploy_safe_schema.sql @@ -330,6 +330,43 @@ AND NOT EXISTS ( UPDATE `settings` SET `can_edit` = 0 WHERE `key` = 'main_wallet_products'; +-- --------------------------------------------------------------------------- +-- products — نوع هزینه خرید/فروش: نقدی (ریال) یا درصدی +-- --------------------------------------------------------------------------- +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 = 'cash_fee_buy_is_percent') = 0, + IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'cash_fee_buy') > 0, + 'ALTER TABLE `products` ADD COLUMN `cash_fee_buy_is_percent` TINYINT(1) NOT NULL DEFAULT 0 AFTER `cash_fee_buy`', + 'ALTER TABLE `products` ADD COLUMN `cash_fee_buy_is_percent` TINYINT(1) NOT NULL DEFAULT 0' + ), + 'SELECT ''skip products.cash_fee_buy_is_percent'' AS info' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +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 = 'cash_fee_sell_is_percent') = 0, + IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'cash_fee_sell') > 0, + 'ALTER TABLE `products` ADD COLUMN `cash_fee_sell_is_percent` TINYINT(1) NOT NULL DEFAULT 0 AFTER `cash_fee_sell`', + 'ALTER TABLE `products` ADD COLUMN `cash_fee_sell_is_percent` TINYINT(1) NOT NULL DEFAULT 0' + ), + 'SELECT ''skip products.cash_fee_sell_is_percent'' AS info' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + -- --------------------------------------------------------------------------- -- پایان — برای بورس‌مس (اختیاری، جدا اجرا کنید): -- UPDATE `settings` SET `value` = 'کیلوگرم' WHERE `key` = 'unit';