fix bug 4

This commit is contained in:
2026-07-16 17:29:18 +03:30
parent 69538226d2
commit 67762e2a0e
3 changed files with 123 additions and 24 deletions
+97 -20
View File
@@ -610,17 +610,98 @@ class WalletController extends Controller
public function updateWithdraw(Request $request, $id)
{
// پیدا کردن رکورد Withdraw با استفاده از شناسه
$withdraw = Withdraw::find($id);
// بررسی وجود رکورد
$withdraw = Withdraw::with(['Transaction.product', 'customer'])->find($id);
if (!$withdraw) {
return response()->json(['error' => 'Withdraw not found'], 404);
}
$withdraw->sts = $request->sts;
$newSts = (int) $request->sts;
$oldSts = (int) $withdraw->sts;
if (
Withdraw::isPhysicalWithdrawNote($withdraw->note)
&& $newSts === 52
&& in_array($oldSts, [50, 51], true)
) {
$this->refundPhysicalWithdrawGold($withdraw);
}
$withdraw->sts = $newSts;
$withdraw->save();
if ($withdraw->customer) {
$this->broadcastCustomerWalletAssets($withdraw->customer);
}
return response()->json(['message' => 'Withdraw updated successfully', 'withdraw' => $withdraw]);
}
/**
* برگشت موجودی کسرشدهٔ برداشت فیزیکی هنگام لغو (sts=52).
*/
protected function refundPhysicalWithdrawGold(Withdraw $withdraw): void
{
$alreadyRefunded = Transaction::query()
->where('withdraw_id', $withdraw->id)
->where('note', 'like', '%لغو سند برداشت فیزیکی%')
->exists();
if ($alreadyRefunded) {
return;
}
$customer = $withdraw->customer ?: Customer::find($withdraw->customer_id);
if (!$customer) {
return;
}
$assetSummaryService = app(AssetSummaryService::class);
foreach ($withdraw->Transaction as $transaction) {
// فقط تراکنش‌های کسر (منفی) را برگردان — نه تراکنش‌های برگشت قبلی
$gold = (float) ($transaction->gold ?? 0);
if ($gold >= 0) {
continue;
}
$amount = abs($gold);
if ($amount < 0.000001) {
continue;
}
$product = $transaction->product;
$productId = (int) ($transaction->product_id ?? ($product->id ?? 0));
if ($productId <= 0) {
continue;
}
$effectiveProductId = $assetSummaryService->effectiveProductWalletIdForCustomer(
(int) $customer->id,
(int) $customer->type,
$productId
);
$walletInterface = new WalletInterface(
(int) $customer->id,
(int) $customer->type,
null,
$effectiveProductId
);
$title = $product->title ?? 'محصول';
new TransactionInterface($walletInterface, [
'gold' => $amount,
'number' => $transaction->number ?? null,
'product_id' => $productId,
'expire_at' => now(),
'active' => true,
'title' => "برگشت برداشت فیزیکی {$title}",
'note' => "لغو سند برداشت فیزیکی شماره {$withdraw->id}",
'withdraw_id' => $withdraw->id,
], 6, true);
}
}
public function getWithdraw(Request $request, $id)
{
$withdraw = Withdraw::with(['customer', 'bank_account', 'Address', "Transaction"])->find($id);
@@ -705,11 +786,9 @@ class WalletController extends Controller
'title' => (string) $product->title,
'wallet_id' => (int) $wallet->id,
'available' => $displayQty,
'unit' => self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
'group_type' => $product->group_type ?? null,
]),
'unit' => self::productQuantityUnit(
self::productUnitPayload($product)
),
'is_main_wallet' => $representativeId !== null && (int) $product->id === $representativeId,
];
})->filter(function ($row) {
@@ -851,8 +930,8 @@ class WalletController extends Controller
continue;
}
$tradeByQuantity = (bool) ($product->trade_by_quantity ?? false);
$minValue = $tradeByQuantity ? 1.0 : 0.001;
$isCountBased = self::isCountBasedProduct(self::productUnitPayload($product));
$minValue = $isCountBased ? 1.0 : 0.001;
if ($newValue < $minValue) {
throw new UnprocessableEntityHttpException(
@@ -860,7 +939,7 @@ class WalletController extends Controller
);
}
if ($tradeByQuantity && abs($newValue - round($newValue)) > 0.000001) {
if ($isCountBased && abs($newValue - round($newValue)) > 0.000001) {
throw new UnprocessableEntityHttpException(
"مقدار {$product->title} باید عدد صحیح باشد"
);
@@ -944,7 +1023,9 @@ class WalletController extends Controller
(int) $product->id
)
: 0.0;
$tradeByQuantity = (bool) ($product->trade_by_quantity ?? false);
$isCountBased = $product
? self::isCountBasedProduct(self::productUnitPayload($product))
: false;
return [
'transaction_id' => (int) $transaction->id,
@@ -952,14 +1033,10 @@ class WalletController extends Controller
'current_value' => $current,
'available_add' => round((float) $availableAdd, 6),
'max_value' => round($current + (float) $availableAdd, 6),
'min_value' => $tradeByQuantity ? 1 : 0.001,
'allows_decimal' => !$tradeByQuantity,
'min_value' => $isCountBased ? 1 : 0.001,
'allows_decimal' => !$isCountBased,
'unit' => $product
? self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
'group_type' => $product->group_type ?? null,
])
? self::productQuantityUnit(self::productUnitPayload($product))
: 'گرم',
];
})->values();
+17 -4
View File
@@ -492,10 +492,23 @@ class Controller extends BaseController
return false;
}
// معامله مقداری → نه تعدادی
if (self::usesQuantityTrade($product)) {
return false;
}
// آبشده/کاتد همیشه وزنی است
if (self::isMeltedTradeGroup($product['group_type'] ?? null)) {
return false;
}
// trade_by_quantity صراحتاً false/0 → تعدادی
// (حتی اگر unit=گرم فقط وزن هر عدد باشد)
if (array_key_exists('trade_by_quantity', $product) && $product['trade_by_quantity'] !== null) {
return true;
}
// محصولات قدیمی بدون فلگ: واحد وزنی → مقداری
if (self::isWeightBasedProductUnit($product['unit'] ?? null)) {
return false;
}
@@ -594,15 +607,15 @@ class Controller extends BaseController
public static function productQuantityUnit(?array $product): string
{
if (self::isCountBasedProduct($product)) {
return 'عدد';
}
$unit = trim((string) ($product['unit'] ?? ''));
if ($unit !== '') {
return $unit;
}
if (self::isCountBasedProduct($product)) {
return 'عدد';
}
return self::tradeWeightUnit();
}
@@ -507,6 +507,15 @@ class WalletController extends Controller
], 422);
}
$requestValue = (float) $data['wallets'][$key]['value'];
if (Controller::isCountBasedProduct(Controller::productUnitPayload($walletProduct))) {
if (abs($requestValue - round($requestValue)) > 0.000001) {
return response()->json([
'message' => "برای {$walletProduct['title']} فقط تعداد صحیح مجاز است"
], 422);
}
$data['wallets'][$key]['value'] = (int) round($requestValue);
}
$data['wallets'][$key]['wallet_interface'] = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type, null, $effectiveProductId);
}