diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 3e00a51..a70bd47 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -92,7 +92,12 @@ class ProductController extends SuperController // return 1; $data = $this->filterProductData($request->all()); - if (isset($data["price"])){ + $clearingReadingMethod = array_key_exists('reading_method', $data) + && ($data['reading_method'] === null || $data['reading_method'] === ''); + if ($clearingReadingMethod) { + $data['reading_method'] = null; + } + if (isset($data["price"])) { $data["reading_method"] = null; if (Product::hasPriceOffsetColumn()) { $data['price_offset_percent'] = 0; @@ -121,6 +126,7 @@ class ProductController extends SuperController Product::hasPriceOffsetColumn() && $product->reading_method && array_key_exists('price_offset_percent', $data) + && !$clearingReadingMethod ) { ProductPriceService::reapplyOffsetPercent( $product, @@ -150,7 +156,11 @@ class ProductController extends SuperController $products = Product::with(['file'])->where('active', 1)->get(); if (Product::hasPriceOffsetColumn() && str_contains(request()->path(), '/customer/')) { - $products->each->makeHidden(['price_offset_percent']); + $products->each(function (Product $product) { + if ($product->reading_method) { + $product->makeHidden(['price_offset_percent']); + } + }); } return response()->json($products); diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 4dded41..065883f 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -280,12 +280,64 @@ class Controller extends BaseController return self::productSellTitle(self::metalTypeLabel()); } - /** واحد وزن معاملات فلز اصلی — مس: کیلوگرم، طلا: گرم */ + /** واحد وزن معاملات فلز — از ستینگ unit؛ پیش‌فرض گرم */ public static function tradeWeightUnit(): string { - $label = self::metalTypeLabel(); + return self::resolveTradeWeightUnit( + Setting::where('key', 'unit')->value('value') + ); + } - return str_contains($label, 'مس') ? 'کیلوگرم' : 'گرم'; + public static function resolveTradeWeightUnit(?string $value): string + { + $unit = trim((string) $value); + + if ($unit === 'کیلوگرم' || $unit === 'kilogram' || $unit === 'kg') { + return 'کیلوگرم'; + } + + return 'گرم'; + } + + public static function weightToGrams(float $value, ?string $fromUnit = null): float + { + $from = $fromUnit ?? self::tradeWeightUnit(); + + return $from === 'کیلوگرم' ? $value * 1000 : $value; + } + + public static function gramsToTradeUnit(float $grams): float + { + return self::tradeWeightUnit() === 'کیلوگرم' ? $grams / 1000 : $grams; + } + + public static function productStorageWeightUnit($product): string + { + $unit = trim((string) (is_object($product) ? ($product->unit ?? '') : ($product['unit'] ?? ''))); + + if (self::isWeightBasedProductUnit($unit)) { + return $unit; + } + + return 'گرم'; + } + + public static function productLineWeightToGrams($product, $quantity): float + { + $groupType = is_object($product) ? ($product->group_type ?? '') : ($product['group_type'] ?? ''); + if ((string) $groupType === 'سکه') { + return 0.0; + } + + $qty = (float) $quantity; + $unit = trim((string) (is_object($product) ? ($product->unit ?? '') : ($product['unit'] ?? ''))); + $amount = (float) (is_object($product) ? ($product->amount ?? 0) : ($product['amount'] ?? 0)); + + if (self::isWeightBasedProductUnit($unit)) { + return self::weightToGrams($qty, $unit); + } + + return self::weightToGrams($amount * $qty, self::productStorageWeightUnit($product)); } public static function isWeightBasedProductUnit(?string $unit): bool @@ -330,7 +382,7 @@ class Controller extends BaseController } $group = trim((string) ($product['group_type'] ?? '')); - if (in_array($group, ['مفتول مس', 'کاتد مس'], true)) { + if (in_array($group, ['مفتول مس'], true)) { return true; } diff --git a/app/Http/Controllers/Customer/CustomerController.php b/app/Http/Controllers/Customer/CustomerController.php index 6386b0d..f2a05b5 100644 --- a/app/Http/Controllers/Customer/CustomerController.php +++ b/app/Http/Controllers/Customer/CustomerController.php @@ -50,72 +50,71 @@ class CustomerController extends Controller ->where('owner_type', $user->type) ->whereNull('fund_id') ->first() ?? new Wallet(); - $wallet->gold_ab = round(Wallet::where('owner', $user->id)->where('product_id', null)->sum('gold'), 2); + + $treasureGrams = 0; $wallet->wp = Wallet::with('product')->where('owner', $user->id)->get(); - foreach ($wallet->wp as $row){ - if ($row->product_id != null){ - $wallet->treasure +=$row->product->amount*$row->gold; + foreach ($wallet->wp as $row) { + if ($row->product_id != null) { + $treasureGrams += self::productLineWeightToGrams($row->product, $row->gold); } - $wallet->treasure_sum =$user->gold+$wallet->treasure; } - $wallet->gold_sum = $wallet->treasure_sum+$wallet->gold_ab; + + $goldAbGrams = self::weightToGrams((float) Wallet::where('owner', $user->id)->where('product_id', null)->sum('gold')); + $userGoldGrams = self::weightToGrams((float) $user->gold); + + $wallet->treasure = round(self::gramsToTradeUnit($treasureGrams), 3); + $wallet->treasure_sum = round(self::gramsToTradeUnit($userGoldGrams + $treasureGrams), 3); + $wallet->gold_ab = round(self::gramsToTradeUnit($goldAbGrams), 2); + $wallet->gold_sum = round($wallet->treasure_sum + $wallet->gold_ab, 2); $user->wallets = Wallet::with(['product'])->where('owner', $user->id)->get() ?? new Wallet(); - $count_gold =0; - $count_silver = 0; // وزن نقره - $count_copper = 0; // وزن مس - $count_cash_from_gold = 0; // مبلغ حاصل از طلا - $count_cash_from_silver = 0; // مبلغ حاصل از نقره - $count_cash_from_copper = 0; // مبلغ حاصل از مس - $count_cash_pure = 0; // نقدینگی خالص بدون ارزش محصولات + $count_gold = 0; + $count_silver = 0; + $count_copper = 0; + $count_cash_from_gold = 0; + $count_cash_from_silver = 0; + $count_cash_from_copper = 0; + $count_cash_pure = 0; - $count_product =0; + $count_product = 0; foreach ($user->wallets as $walletSingle) { if ($walletSingle->product_id) { - // محاسبه برای طلا (metal_id = 1) if ($walletSingle->product->metal_id == 1) { - - if (!($walletSingle->product->group_type == "سکه")){ - (float)$count_gold += $walletSingle->product->amount*$walletSingle->gold; - } + if (!($walletSingle->product->group_type == "سکه")) { + $count_gold += self::productLineWeightToGrams($walletSingle->product, $walletSingle->gold); + } $count_cash_from_gold += $walletSingle->product->price * $walletSingle->gold; $count_product += $walletSingle->gold; - } - // محاسبه برای نقره (metal_id = 2) - elseif ($walletSingle->product->metal_id == 2) { - if (!($walletSingle->product->group_type == "سکه")){ - (float)$count_silver += $walletSingle->product->amount * $walletSingle->gold; + } elseif ($walletSingle->product->metal_id == 2) { + if (!($walletSingle->product->group_type == "سکه")) { + $count_silver += self::productLineWeightToGrams($walletSingle->product, $walletSingle->gold); } $count_cash_from_silver += ($walletSingle->product->price * $walletSingle->product->amount) * $walletSingle->gold; $count_product += $walletSingle->gold; - } - // محاسبه برای مس (metal_id = 3) - elseif ($walletSingle->product->metal_id == 3) { - if (!($walletSingle->product->group_type == "سکه")){ - (float)$count_copper += $walletSingle->product->amount * $walletSingle->gold; + } elseif ($walletSingle->product->metal_id == 3) { + if (!($walletSingle->product->group_type == "سکه")) { + $count_copper += self::productLineWeightToGrams($walletSingle->product, $walletSingle->gold); } $count_cash_from_copper += ($walletSingle->product->price * $walletSingle->product->amount) * $walletSingle->gold; $count_product += $walletSingle->gold; } } else { - (float)$count_gold += $walletSingle->gold; - (float)$count_cash_pure += $walletSingle->cash; - $wallet->count_cash_wallet = $walletSingle->cash; - $wallet->count_gold_wallet = $walletSingle->gold; + $count_gold += self::weightToGrams((float) $walletSingle->gold); + $count_cash_pure += $walletSingle->cash; + $wallet->count_cash_wallet = $walletSingle->cash; + $wallet->count_gold_wallet = round(self::gramsToTradeUnit(self::weightToGrams((float) $walletSingle->gold)), 3); } } - // محاسبه مجموع کل ارزش دارایی $count_cash_total = $count_cash_from_gold + $count_cash_from_silver + $count_cash_from_copper + $count_cash_pure; $wallet->count_cash_wallets = $count_cash_total - $wallet->count_cash_wallet; - $wallet->count_gold_wallets = $count_gold - $wallet->count_gold_wallet; - - $wallet->count_gold = $count_gold; - $wallet->count_silver = $count_silver; - $wallet->count_copper = $count_copper; + $wallet->count_gold = round(self::gramsToTradeUnit($count_gold), 3); + $wallet->count_gold_wallets = round($wallet->count_gold - $wallet->count_gold_wallet, 3); + $wallet->count_silver = round(self::gramsToTradeUnit($count_silver), 3); + $wallet->count_copper = round(self::gramsToTradeUnit($count_copper), 3); $wallet->count_cash = $count_cash_pure; $wallet->count_product = $count_product; $wallet->cash_from_gold = $count_cash_from_gold; diff --git a/database/sql/add_trade_unit_setting.sql b/database/sql/add_trade_unit_setting.sql new file mode 100644 index 0000000..03f71f2 --- /dev/null +++ b/database/sql/add_trade_unit_setting.sql @@ -0,0 +1,16 @@ +-- واحد نمایش وزن دارایی‌ها و معاملات فلز (گرم / کیلوگرم) +-- مقدار خالی = گرم (پیش‌فرض در اپ و ادمین) + +INSERT INTO `settings` (`key`, `value`, `title_fa`, `can_edit`, `is_active`, `description`, `type`) +SELECT + 'unit', + '', + 'واحد وزن دارایی و معاملات', + 1, + 1, + 'واحد نمایش وزن فلز در دارایی‌ها، کیف پول و تراکنش‌ها|گرم,کیلوگرم', + 'select' +WHERE NOT EXISTS (SELECT 1 FROM `settings` WHERE `key` = 'unit' LIMIT 1); + +-- برای بورس مس که داده‌ها بر اساس کیلوگرم ذخیره شده‌اند (اختیاری — فقط در صورت نیاز اجرا کنید): +-- UPDATE `settings` SET `value` = 'کیلوگرم' WHERE `key` = 'unit';