From e8d17c1c044710e8b55a208123ef6d92f3b57cc5 Mon Sep 17 00:00:00 2001 From: adonis Date: Mon, 22 Jun 2026 21:29:25 +0330 Subject: [PATCH] =?UTF-8?q?fixed=20=D8=B9=D8=AF=D9=87=D9=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Controller.php | 82 ++++++++++++++++++- .../Controllers/Customer/WalletController.php | 55 +++++++++---- .../rooyesh/wallet/src/models/Transaction.php | 11 ++- packages/rooyesh/wallet/src/models/Wallet.php | 8 +- 4 files changed, 136 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index adc83ec..e1cb245 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -416,6 +416,86 @@ class Controller extends BaseController return in_array($group, ['سکه', 'گوی', 'مکعب', 'شمش', 'سکه پارسیان'], true); } + public static function productUnitPayload($product): array + { + if (!$product) { + return []; + } + + if (is_array($product)) { + return $product; + } + + return [ + 'unit' => $product->unit ?? null, + 'direct_price' => $product->direct_price ?? null, + 'group_type' => $product->group_type ?? null, + 'amount' => $product->amount ?? null, + 'trade_by_quantity' => $product->trade_by_quantity ?? null, + ]; + } + + public static function convertWeight(float $value, ?string $fromUnit, ?string $toUnit): float + { + $from = trim((string) ($fromUnit ?? '')); + $to = trim((string) ($toUnit ?? '')); + + if ($from === '' || $to === '' || $from === $to) { + return $value; + } + + $grams = self::weightToGrams($value, $from); + + return $to === 'کیلوگرم' ? $grams / 1000 : $grams; + } + + /** + * مقدار و واحد نمایشی تراکنش — منبع حقیقت: unit محصول در ادمین. + * معامله مقداری + وزن دارد: تعداد ذخیره‌شده × وزن هر واحد. + */ + public static function resolveTransactionDisplayGoldAndUnit(?array $payload, float $gold, bool $onMainWallet = false): array + { + if ($payload === []) { + return [$gold, self::tradeWeightUnit()]; + } + + $sign = $gold < 0 ? -1 : 1; + $absGold = abs($gold); + + if ( + self::usesQuantityTrade($payload) + && self::hasProductUnitWeight($payload) + && self::isWeightBasedProductUnit($payload['unit'] ?? null) + ) { + $factor = (float) ($payload['amount'] ?? 1); + + return [ + round($absGold * $factor, 3) * $sign, + trim((string) $payload['unit']), + ]; + } + + $unit = self::productQuantityUnit($payload); + $productWeightUnit = self::isWeightBasedProductUnit($payload['unit'] ?? null) + ? trim((string) $payload['unit']) + : null; + + if ($onMainWallet && $productWeightUnit) { + $tradeUnit = self::tradeWeightUnit(); + + if ($productWeightUnit !== $tradeUnit) { + return [ + round(self::convertWeight($absGold, $tradeUnit, $productWeightUnit), 3) * $sign, + $productWeightUnit, + ]; + } + + return [$gold, $productWeightUnit]; + } + + return [$gold, $unit]; + } + public static function productQuantityUnit(?array $product): string { $unit = trim((string) ($product['unit'] ?? '')); @@ -427,7 +507,7 @@ class Controller extends BaseController return 'عدد'; } - return 'گرم'; + return self::tradeWeightUnit(); } /** مطابق ادمین: وزن «دارد» => amount > 0 ؛ «ندارد» => amount = 0 */ diff --git a/app/Http/Controllers/Customer/WalletController.php b/app/Http/Controllers/Customer/WalletController.php index bf1de39..0bafceb 100644 --- a/app/Http/Controllers/Customer/WalletController.php +++ b/app/Http/Controllers/Customer/WalletController.php @@ -137,11 +137,7 @@ class WalletController extends Controller } else { $record->title = ($record->gold > 0 ? self::productBuyTitle($wallet->product->title) : self::productSellTitle($wallet->product->title)); } - $record->unit = self::productQuantityUnit([ - 'unit' => $wallet->product->unit ?? null, - 'direct_price' => $wallet->product->direct_price ?? null, - 'group_type' => $wallet->product->group_type ?? null, - ]); + $record->unit = self::productQuantityUnit(self::productUnitPayload($wallet->product)); } else { $record->title = $this->resolveMainWalletTransactionTitle($record); } @@ -560,30 +556,55 @@ class WalletController extends Controller } if (!empty($record->wallet?->product)) { - $record->unit = self::productQuantityUnit([ - 'unit' => $record->wallet->product->unit ?? null, - 'direct_price' => $record->wallet->product->direct_price ?? null, - 'group_type' => $record->wallet->product->group_type ?? null, - ]); + $this->applyProductTransactionUnit($record); } } + private function resolveTransactionProduct($record): ?Product + { + $partial = $record->wallet->product ?? $record->product ?? null; + $productId = (int) ($partial->id ?? $record->product_id ?? 0); + + if ($productId <= 0) { + return $partial instanceof Product ? $partial : null; + } + + return Product::query() + ->select([ + 'id', + 'title', + 'group_type', + 'unit', + 'direct_price', + 'amount', + 'trade_by_quantity', + 'metal_id', + ]) + ->find($productId) ?? ($partial instanceof Product ? $partial : null); + } + private function applyProductTransactionUnit($record): void { - $product = $record->wallet->product ?? $record->product ?? null; + $product = $this->resolveTransactionProduct($record); + if (!$product) { - if (!empty($record->gold) && empty($record->wallet?->product)) { + if (!empty($record->gold)) { $record->unit = self::tradeWeightUnit(); } return; } - $record->unit = self::productQuantityUnit([ - 'unit' => $product->unit ?? null, - 'direct_price' => $product->direct_price ?? null, - 'group_type' => $product->group_type ?? null, - ]); + $payload = self::productUnitPayload($product); + $onMainWallet = empty($record->wallet?->product_id); + [$displayGold, $unit] = self::resolveTransactionDisplayGoldAndUnit( + $payload, + (float) ($record->gold ?? 0), + $onMainWallet + ); + + $record->gold = $displayGold; + $record->unit = $unit; } private function isProductWalletTransaction($record): bool diff --git a/packages/rooyesh/wallet/src/models/Transaction.php b/packages/rooyesh/wallet/src/models/Transaction.php index 3fab637..9843a8f 100644 --- a/packages/rooyesh/wallet/src/models/Transaction.php +++ b/packages/rooyesh/wallet/src/models/Transaction.php @@ -43,7 +43,16 @@ class Transaction extends Model } public function product() { - return $this->belongsTo('App\Models\Product','product_id')->select(['id','title','group_type']); + return $this->belongsTo('App\Models\Product', 'product_id')->select([ + 'id', + 'title', + 'group_type', + 'unit', + 'direct_price', + 'amount', + 'trade_by_quantity', + 'metal_id', + ]); } public function order() { diff --git a/packages/rooyesh/wallet/src/models/Wallet.php b/packages/rooyesh/wallet/src/models/Wallet.php index f1d1e88..763761e 100644 --- a/packages/rooyesh/wallet/src/models/Wallet.php +++ b/packages/rooyesh/wallet/src/models/Wallet.php @@ -22,7 +22,13 @@ class Wallet extends Model } public function Product() { - return $this->belongsTo('App\Models\Product','product_id'); + return $this->belongsTo('App\Models\Product', 'product_id'); + } + + /** @alias Product */ + public function product() + { + return $this->Product(); } /** کیف اصلی مشتری — بدون fund و بدون product */