From 1b310b47523958bd7d7edb5b36c4ad75e4d89e0d Mon Sep 17 00:00:00 2001 From: adonis Date: Tue, 23 Jun 2026 21:59:38 +0330 Subject: [PATCH] fixed kilo in metals 5 --- .../Controllers/Admin/WalletController.php | 156 ++++++++++++++++++ routes/admin.php | 2 + 2 files changed, 158 insertions(+) diff --git a/app/Http/Controllers/Admin/WalletController.php b/app/Http/Controllers/Admin/WalletController.php index 3484e4b..5adf6bf 100644 --- a/app/Http/Controllers/Admin/WalletController.php +++ b/app/Http/Controllers/Admin/WalletController.php @@ -21,6 +21,7 @@ use App\Http\Controllers\Admin\TahesabController; use App\Models\Setting; use App\Models\User; use App\Services\BalanceReportService; +use App\Services\ConfigService; use App\Services\WalletBalanceService; use Hekmatinasser\Verta\Verta; @@ -218,6 +219,161 @@ class WalletController extends Controller return response()->json(true); } + /** گزینه‌های شارژ کیف پول — نقدی + محصولات فعال */ + public function chargeTargets() + { + $configService = app(ConfigService::class); + $mainWalletProductIds = $configService->mainWalletProductIds(); + $balanceReport = app(BalanceReportService::class); + + $targets = [ + [ + 'key' => 'money', + 'kind' => 'money', + 'label' => 'نقدی (ریال)', + 'unit' => 'ریال', + ], + ]; + + $products = Product::query() + ->where('active', 1) + ->orderBy('metal_id') + ->orderBy('id') + ->get(['id', 'title', 'unit', 'metal_id', 'group_type', 'trade_by_quantity', 'amount', 'direct_price']); + + foreach ($products as $product) { + $usesMainWallet = in_array((int) $product->id, $mainWalletProductIds, true); + $label = (string) $product->title; + if ($usesMainWallet) { + $label .= ' (کیف اصلی)'; + } + + $targets[] = [ + 'key' => 'product:' . $product->id, + 'kind' => 'metal', + 'product_id' => (int) $product->id, + 'label' => $label, + 'unit' => $this->resolveProductChargeUnit($product), + 'metal_id' => max(1, (int) ($product->metal_id ?? 1)), + 'uses_main_wallet' => $usesMainWallet, + 'trade_by_quantity' => (bool) $product->trade_by_quantity, + 'group_type' => (string) ($product->group_type ?? ''), + ]; + } + + return response()->json([ + 'targets' => $targets, + 'catalogMetals' => $balanceReport->catalogMetalsMeta(), + ]); + } + + /** + * شارژ یکپارچه کیف پول — نقدی یا محصول (با رعایت کیف اصلی برای آبشده). + */ + public function charge_wallet(Request $request) + { + $productId = (int) $request->input('product_id', 0); + if ($productId > 0) { + return $this->chargeProductMetal($request, $productId); + } + + return $this->charge($request); + } + + private function chargeProductMetal(Request $request, int $productId) + { + $data = $request->all(); + $product = Product::findOrFail($productId); + $customer = Customer::findOrFail($data['customer_id']); + $deposit = ((float) ($data['value'] ?? 0)) > 0; + + $effectiveProductId = Controller::effectiveProductWalletId($productId); + $wallet_interface = new WalletInterface( + (int) $data['customer_id'], + (int) $customer->type, + null, + $effectiveProductId + ); + $wallet = $wallet_interface->wallet; + + if (!$deposit && $wallet !== null && (float) $wallet->gold < abs((float) $data['value'])) { + throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست'); + } + + $goldBlock = 0; + $user = auth()->user(); + $unitLabel = $this->resolveProductChargeUnit($product); + + $Withdraw = Withdraw::create([ + 'user_id' => $user->id, + 'customer_id' => $customer->id, + 'gold' => $data['value'], + 'note' => $data['note'] ?? null, + 'sts' => 4, + ]); + + new TransactionInterface($wallet_interface, [ + 'cash' => 0, + 'gold' => $data['value'], + 'product_id' => $productId, + 'expire_at' => now(), + 'note' => $data['note'] ?? null, + 'withdraw_id' => $Withdraw->id, + 'active' => true, + ], 7, $deposit, false, $goldBlock); + + if ($this->Config('send_to_tahesab') == 'true') { + try { + $data['direction'] = $deposit ? 1 : 0; + $tahesab = new TahesabController(); + $tahesab->walletGold($data); + } catch (\Exception $e) { + return response($e->getMessage(), 500); + } + } + + $status = $deposit ? ' شارژ شد ' : ' کسر شد '; + $formatted = self::formatSmsGoldWeight($data['value']); + + if ($this->Config('charge_notif') == 'true') { + $message = 'کیف پول کاربر ' . $customer->name . ' به مقدار ' . $formatted . ' ' . $unitLabel . ' در تاریخ ' . verta()->format('Y/n/j H:i:s') . $status . 'بابت' . "\n" + . ($data['note'] ?? '') + . "\n" + . $this->Config('name_fa'); + User::where('send_sms', 1)->get()->each(function ($admin) use ($message) { + $this->send($admin->mobile, $message); + }); + } + + $message = 'کیف پول شما به اندازه ' . $formatted . ' ' . $unitLabel . ' در تاریخ ' . verta()->format('Y/n/j H:i:s') . $status . ' بابت' . "\n" + . ($data['note'] ?? '') + . "\n" + . $this->Config('name_fa'); + $this->Send($customer->mobile, $message); + + return response()->json(true); + } + + private function resolveProductChargeUnit(Product $product): string + { + $payload = Controller::productUnitPayload($product); + + if ( + Controller::usesQuantityTrade($payload) + && !Controller::isWeightBasedProductUnit($payload['unit'] ?? null) + ) { + $unit = trim((string) ($payload['unit'] ?? '')); + + return $unit !== '' ? $unit : 'عدد'; + } + + if (Controller::isWeightBasedProductUnit($payload['unit'] ?? null)) { + return trim((string) $payload['unit']); + } + + return Controller::productStorageWeightUnit($product); + } + public function index(Request $request) { $balanceReport = app(BalanceReportService::class); diff --git a/routes/admin.php b/routes/admin.php index 19f5d83..201a319 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -104,6 +104,8 @@ Route::middleware('auth:user')->group(function (){ //-------------wallet Route::post('/wallet/charge',[WalletController::class,'charge']); Route::post('/wallet/charge_gold',[WalletController::class,'charge_gold']); + Route::post('/wallet/charge_wallet',[WalletController::class,'charge_wallet']); + Route::get('/wallet/charge-targets',[WalletController::class,'chargeTargets']); Route::post('/wallet/table',[WalletController::class,'indexWallet']); Route::post('/wallet/transaction/table',[WalletController::class,'index']); Route::post('/wallet/transaction/update',[WalletController::class,'updateTransaction']);