fixed some bugs 4

This commit is contained in:
2026-07-08 17:58:55 +03:30
parent bb05a541e2
commit eb48179b2a
2 changed files with 143 additions and 60 deletions
+109 -27
View File
@@ -806,11 +806,12 @@ class WalletController extends Controller
public function adjustPhysicalWithdraw(Request $request, $id, AssetSummaryService $assetSummaryService) public function adjustPhysicalWithdraw(Request $request, $id, AssetSummaryService $assetSummaryService)
{ {
$payload = $request->validate([ $payload = $request->validate([
'transaction_id' => 'required|integer|exists:transactions,id', 'items' => 'required|array|min:1',
'direction' => 'required|in:increase,decrease', 'items.*.transaction_id' => 'required|integer|exists:transactions,id',
'items.*.value' => 'required|numeric|min:0.0001',
]); ]);
$withdraw = Withdraw::with(['Transaction.product'])->findOrFail($id); $withdraw = Withdraw::with(['Transaction.product', 'customer'])->findOrFail($id);
if (!Withdraw::isPhysicalWithdrawNote($withdraw->note)) { if (!Withdraw::isPhysicalWithdrawNote($withdraw->note)) {
return response()->json(['message' => 'این سند برداشت فیزیکی نیست'], 422); return response()->json(['message' => 'این سند برداشت فیزیکی نیست'], 422);
} }
@@ -818,25 +819,49 @@ class WalletController extends Controller
return response()->json(['message' => 'تغییر مقدار فقط در وضعیت «در انتظار ارسال» مجاز است'], 422); return response()->json(['message' => 'تغییر مقدار فقط در وضعیت «در انتظار ارسال» مجاز است'], 422);
} }
$transaction = Transaction::with('product')->findOrFail($payload['transaction_id']); $customer = Customer::findOrFail($withdraw->customer_id);
if ((int) $transaction->withdraw_id !== (int) $withdraw->id) { $transactionsById = $withdraw->Transaction->keyBy('id');
return response()->json(['message' => 'تراکنش به این سند تعلق ندارد'], 422);
DB::transaction(function () use (
$payload,
$withdraw,
$customer,
$transactionsById,
$assetSummaryService
) {
foreach ($payload['items'] as $item) {
$transaction = $transactionsById->get((int) $item['transaction_id']);
if (!$transaction) {
throw new UnprocessableEntityHttpException('تراکنش به این سند تعلق ندارد');
} }
$product = $transaction->product; $product = $transaction->product;
if (!$product) { if (!$product) {
return response()->json(['message' => 'محصول تراکنش یافت نشد'], 422); throw new UnprocessableEntityHttpException('محصول تراکنش یافت نشد');
} }
$step = 1.0; $newValue = round((float) $item['value'], 6);
$currentUnits = abs((float) $transaction->gold); $currentUnits = round(abs((float) $transaction->gold), 6);
$direction = $payload['direction']; if (abs($newValue - $currentUnits) < 0.000001) {
continue;
if ($direction === 'decrease' && $currentUnits <= $step) {
return response()->json(['message' => 'حداقل مقدار قابل برداشت یک واحد است'], 422);
} }
$customer = Customer::findOrFail($withdraw->customer_id); $tradeByQuantity = (bool) ($product->trade_by_quantity ?? false);
$minValue = $tradeByQuantity ? 1.0 : 0.001;
if ($newValue < $minValue) {
throw new UnprocessableEntityHttpException(
"حداقل مقدار برای {$product->title} برابر {$minValue} است"
);
}
if ($tradeByQuantity && abs($newValue - round($newValue)) > 0.000001) {
throw new UnprocessableEntityHttpException(
"مقدار {$product->title} باید عدد صحیح باشد"
);
}
$delta = round($newValue - $currentUnits, 6);
$effectiveProductId = $assetSummaryService->effectiveProductWalletIdForCustomer( $effectiveProductId = $assetSummaryService->effectiveProductWalletIdForCustomer(
(int) $customer->id, (int) $customer->id,
(int) $customer->type, (int) $customer->type,
@@ -850,38 +875,95 @@ class WalletController extends Controller
); );
$wallet = $walletInterface->wallet; $wallet = $walletInterface->wallet;
if (!$wallet) { if (!$wallet) {
return response()->json(['message' => 'کیف پول مشتری یافت نشد'], 422); throw new UnprocessableEntityHttpException('کیف پول مشتری یافت نشد');
} }
if ($direction === 'increase') { if ($delta > 0) {
$available = $assetSummaryService->productDisplayQuantity( $available = $assetSummaryService->productDisplayQuantity(
(int) $customer->id, (int) $customer->id,
(int) $customer->type, (int) $customer->type,
(int) $product->id (int) $product->id
); );
if ($available < $step) { if ($delta > $available + 0.000001) {
return response()->json(['message' => "موجودی {$product->title} کافی نیست"], 422); throw new UnprocessableEntityHttpException(
"موجودی {$product->title} کافی نیست. حداکثر قابل افزودن: {$available}"
);
} }
$wallet->gold -= $delta;
$wallet->gold -= $step;
$transaction->gold = -($currentUnits + $step);
$transaction->number = (float) $transaction->number + ((float) $product->amount * $step);
} else { } else {
$wallet->gold += $step; $wallet->gold += abs($delta);
$transaction->gold = -($currentUnits - $step);
$transaction->number = max(0, (float) $transaction->number - ((float) $product->amount * $step));
} }
$transaction->gold = -$newValue;
$transaction->number = (float) $product->amount * $newValue;
$transaction->balance_gold = $wallet->gold; $transaction->balance_gold = $wallet->gold;
$wallet->save(); $wallet->save();
$transaction->save(); $transaction->save();
$walletInterface->set_hash(); $walletInterface->set_hash();
}
});
$this->broadcastCustomerWalletAssets($customer); $this->broadcastCustomerWalletAssets($customer);
return response()->json([ return response()->json([
'message' => 'مقدار برداشت به‌روزرسانی شد', 'message' => 'مقادیر برداشت به‌روزرسانی شد',
'transaction' => $transaction->fresh('product'), 'withdraw' => $withdraw->fresh(['Transaction.product', 'customer']),
]);
}
public function physicalWithdrawAdjustInfo($id, AssetSummaryService $assetSummaryService)
{
$withdraw = Withdraw::with(['Transaction.product', 'customer'])->findOrFail($id);
if (!Withdraw::isPhysicalWithdrawNote($withdraw->note)) {
return response()->json(['message' => 'این سند برداشت فیزیکی نیست'], 422);
}
if ((int) $withdraw->sts !== 50) {
return response()->json(['message' => 'تغییر مقدار فقط در وضعیت «در انتظار ارسال» مجاز است'], 422);
}
$customer = $withdraw->customer;
if (!$customer) {
return response()->json(['message' => 'مشتری یافت نشد'], 422);
}
$lines = $withdraw->Transaction->map(function (Transaction $transaction) use (
$customer,
$assetSummaryService
) {
$product = $transaction->product;
$current = round(abs((float) $transaction->gold), 6);
$availableAdd = $product
? $assetSummaryService->productDisplayQuantity(
(int) $customer->id,
(int) $customer->type,
(int) $product->id
)
: 0.0;
$tradeByQuantity = (bool) ($product->trade_by_quantity ?? false);
return [
'transaction_id' => (int) $transaction->id,
'title' => trim((string) $transaction->title) ?: ($product->title ?? 'محصول'),
'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,
'unit' => $product
? self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
'group_type' => $product->group_type ?? null,
])
: 'گرم',
];
})->values();
return response()->json([
'withdraw_id' => (int) $withdraw->id,
'customer_name' => (string) ($customer->name ?? ''),
'customer_mobile' => (string) ($customer->mobile ?? ''),
'lines' => $lines,
]); ]);
} }
+1
View File
@@ -125,6 +125,7 @@ Route::middleware('auth:user')->group(function (){
Route::post('/wallet/withdraw_physical/table',[WalletController::class,'indexPhysicalWithdraw']); Route::post('/wallet/withdraw_physical/table',[WalletController::class,'indexPhysicalWithdraw']);
Route::get('/wallet/withdraw_physical/options/{customerId}',[WalletController::class,'physicalWithdrawOptions']); Route::get('/wallet/withdraw_physical/options/{customerId}',[WalletController::class,'physicalWithdrawOptions']);
Route::post('/wallet/withdraw_physical',[WalletController::class,'createPhysicalWithdraw']); Route::post('/wallet/withdraw_physical',[WalletController::class,'createPhysicalWithdraw']);
Route::get('/wallet/withdraw_physical/{id}/adjust-info',[WalletController::class,'physicalWithdrawAdjustInfo']);
Route::post('/wallet/withdraw_physical/{id}/adjust',[WalletController::class,'adjustPhysicalWithdraw']); Route::post('/wallet/withdraw_physical/{id}/adjust',[WalletController::class,'adjustPhysicalWithdraw']);
//---------------order //---------------order