From 3373bc82ca5a0f6491372adf96498a9e0e06f44f Mon Sep 17 00:00:00 2001 From: adonis Date: Sat, 4 Jul 2026 22:14:18 +0330 Subject: [PATCH] added percent in product price 3 --- .../Controllers/Admin/ProductController.php | 7 ++- app/Services/ProductPriceService.php | 16 ++--- ...e_columns_to_decimal_on_products_table.php | 38 ++++++++++++ database/sql/deploy_safe_schema.sql | 58 +++++++++++++++++++ 4 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 database/migrations/2026_07_04_000002_change_cash_fee_columns_to_decimal_on_products_table.php diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 3bfedce..2590bb1 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -693,7 +693,12 @@ class ProductController extends SuperController } if (is_string($value)) { - $value = str_replace([',', ' ', '٬'], '', $value); + $value = trim(str_replace([' ', '٬'], '', $value)); + if (str_contains($value, ',') && ! str_contains($value, '.')) { + $value = str_replace(',', '.', $value); + } else { + $value = str_replace(',', '', $value); + } } return (float) $value; diff --git a/app/Services/ProductPriceService.php b/app/Services/ProductPriceService.php index b30ede5..9ca5026 100644 --- a/app/Services/ProductPriceService.php +++ b/app/Services/ProductPriceService.php @@ -6,20 +6,22 @@ use App\Models\Product; class ProductPriceService { - /** 5 => 5% ، 0.05 => 5% (اعشاری) */ - public static function feePercentRatio(float $percentValue): float + /** 0.5 => 0.5% ، 5 => 5% — همیشه عدد درصد، نه اعشار (۰.۵ ≠ ۵۰٪) */ + public static function cashFeePercentRatio(float $percentValue): float { if ($percentValue == 0.0) { return 0.0; } - if (abs($percentValue) > 0 && abs($percentValue) < 1) { - return $percentValue; - } - return $percentValue / 100; } + /** @deprecated از cashFeePercentRatio استفاده کنید */ + public static function feePercentRatio(float $percentValue): float + { + return self::cashFeePercentRatio($percentValue); + } + public static function resolveCashFeeRial(Product $product, int $direction, float $basePriceRial): float { $isBuy = $direction === 1; @@ -41,7 +43,7 @@ class ProductPriceService return 0.0; } - return round(abs($basePriceRial * self::feePercentRatio($rawFee))); + return round(abs($basePriceRial * self::cashFeePercentRatio($rawFee))); } public static function productBasePriceRial(Product $product): float diff --git a/database/migrations/2026_07_04_000002_change_cash_fee_columns_to_decimal_on_products_table.php b/database/migrations/2026_07_04_000002_change_cash_fee_columns_to_decimal_on_products_table.php new file mode 100644 index 0000000..a348816 --- /dev/null +++ b/database/migrations/2026_07_04_000002_change_cash_fee_columns_to_decimal_on_products_table.php @@ -0,0 +1,38 @@ + 0 + AND (SELECT COUNT(*) FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'cash_fee_buy') > 0, + 'ALTER TABLE `products` MODIFY COLUMN `cash_fee_buy` DECIMAL(10,4) NULL DEFAULT NULL', + 'SELECT ''skip products.cash_fee_buy decimal'' 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') > 0, + 'ALTER TABLE `products` MODIFY COLUMN `cash_fee_sell` DECIMAL(10,4) NULL DEFAULT NULL', + 'SELECT ''skip products.cash_fee_sell decimal'' AS info' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +-- --------------------------------------------------------------------------- +-- products — اعشار برای درصد (مثلاً 0.5%) و مبالغ دقیق +-- --------------------------------------------------------------------------- +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') > 0 + AND (SELECT DATA_TYPE FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'cash_fee_buy' LIMIT 1) <> 'decimal', + 'ALTER TABLE `products` MODIFY COLUMN `cash_fee_buy` DECIMAL(10,4) NULL DEFAULT NULL', + 'SELECT ''skip products.cash_fee_buy decimal'' 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') > 0 + AND (SELECT DATA_TYPE FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'products' AND COLUMN_NAME = 'cash_fee_sell' LIMIT 1) <> 'decimal', + 'ALTER TABLE `products` MODIFY COLUMN `cash_fee_sell` DECIMAL(10,4) NULL DEFAULT NULL', + 'SELECT ''skip products.cash_fee_sell decimal'' AS info' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + -- --------------------------------------------------------------------------- -- پایان — برای بورس‌مس (اختیاری، جدا اجرا کنید): -- UPDATE `settings` SET `value` = 'کیلوگرم' WHERE `key` = 'unit';