fixed عدهف
This commit is contained in:
@@ -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 */
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user