wallet handling

This commit is contained in:
2026-06-15 11:25:10 +03:30
parent e9ddcacdb6
commit 81dfdbdb96
5 changed files with 30 additions and 22 deletions
@@ -33,14 +33,14 @@ class WalletController extends Controller
if ($data['value'] > 0) { if ($data['value'] > 0) {
$deposit = true; $deposit = true;
} }
$wallet = Wallet::where('owner', $data['customer_id'])->whereNull('fund_id')->first(); $wallet_interface = new WalletInterface($data['customer_id'], (int) $customer->type);
$wallet = $wallet_interface->wallet;
if (is_null($wallet) && !$deposit) { if (is_null($wallet) && !$deposit) {
throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست'); throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست');
} }
if (!is_null($wallet) && $wallet->cash < abs($data['value']) && !$deposit) { if (!is_null($wallet) && $wallet->cash < abs($data['value']) && !$deposit) {
throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست'); throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست');
} }
$wallet_interface = new WalletInterface($data['customer_id'], $customer->type);
$orders = Order::where('customer_id', $data['customer_id'])->where('sts', 1)->get(); $orders = Order::where('customer_id', $data['customer_id'])->where('sts', 1)->get();
$gold_block = 0; $gold_block = 0;
if (count($orders) > 0) { if (count($orders) > 0) {
@@ -91,14 +91,14 @@ class WalletController extends Controller
if ($data['value'] > 0) { if ($data['value'] > 0) {
$deposit = true; $deposit = true;
} }
$wallet = Wallet::where('owner', $data['customer_id'])->whereNull('fund_id')->first(); $wallet_interface = new WalletInterface($data['customer_id'], (int) $customer->type);
$wallet = $wallet_interface->wallet;
if (is_null($wallet) && !$deposit) { if (is_null($wallet) && !$deposit) {
throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست'); throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست');
} }
if (!is_null($wallet) && $wallet->cash < abs($data['value']) && !$deposit) { if (!is_null($wallet) && $wallet->cash < abs($data['value']) && !$deposit) {
throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست'); throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست');
} }
$wallet_interface = new WalletInterface($data['customer_id'], $customer->type);
$orders = Order::where('customer_id', $data['customer_id'])->where('sts', 1)->get(); $orders = Order::where('customer_id', $data['customer_id'])->where('sts', 1)->get();
$gold_block = 0; $gold_block = 0;
@@ -160,14 +160,14 @@ class WalletController extends Controller
$deposit = true; $deposit = true;
} }
$wallet = Wallet::where('owner', $data['customer_id'])->whereNull('fund_id')->first(); $wallet_interface = new WalletInterface($data['customer_id'], (int) $customer->type);
$wallet = $wallet_interface->wallet;
// if (is_null($wallet) && !$deposit) { // if (is_null($wallet) && !$deposit) {
// throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست'); // throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست');
// } // }
// if (!is_null($wallet) && $wallet->cash < abs($data['value']) && !$deposit) { // if (!is_null($wallet) && $wallet->cash < abs($data['value']) && !$deposit) {
// throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست'); // throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست');
// } // }
$wallet_interface = new WalletInterface($data['customer_id'], $customer->type);
$gold_block = 0; $gold_block = 0;
$user = auth()->user(); $user = auth()->user();
$customer = Customer::findOrFail($data["customer_id"]); $customer = Customer::findOrFail($data["customer_id"]);
@@ -42,9 +42,8 @@ class OrderController extends Controller
throw new UnprocessableEntityHttpException('امکان سفارش برای شما غیرفعال است.'); throw new UnprocessableEntityHttpException('امکان سفارش برای شما غیرفعال است.');
} }
app(ConfigService::class)->assertTradingOpen(); app(ConfigService::class)->assertTradingOpen();
$wallet = Wallet::where('owner',auth('customer')->id())->whereNull('fund_id') $wallet_interface = new WalletInterface($data['customer_id'], (int) $customer->type);
->first(); $wallet = $wallet_interface->wallet;
$wallet_interface = new WalletInterface($data['customer_id'], 1);
if ($data['direction'] == 1){ if ($data['direction'] == 1){
// $order = Order::where('customer_id',auth('customer')->id())->where('sts',1)->first(); // $order = Order::where('customer_id',auth('customer')->id())->where('sts',1)->first();
// if (!is_null($order)){ // if (!is_null($order)){
@@ -111,8 +111,9 @@ class SingleOrderController extends Controller
$data['price_original'] = $data['price']; $data['price_original'] = $data['price'];
$data['payment_remind'] = 0; $data['payment_remind'] = 0;
$data['fee'] = $data['difference']; $data['fee'] = $data['difference'];
$wallet_interface = new WalletInterface($data['customer_id'], 2); $ownerType = (int) ($customer?->type ?? 2);
$wallet = Wallet::where('owner', $data['customer_id'])->whereNull('fund_id')->first(); $wallet_interface = new WalletInterface($data['customer_id'], $ownerType);
$wallet = $wallet_interface->wallet;
if (!$wallet) { if (!$wallet) {
throw new UnprocessableEntityHttpException('کیف پول یافت نشد.'); throw new UnprocessableEntityHttpException('کیف پول یافت نشد.');
} }
@@ -668,8 +669,9 @@ class SingleOrderController extends Controller
$data['payment_sts'] = 4; $data['payment_sts'] = 4;
$data['price_original'] = $data['price']; $data['price_original'] = $data['price'];
$add_sell_price = config('add_sell_price.single'); $add_sell_price = config('add_sell_price.single');
$wallet_interface = new WalletInterface($data['customer_id'], 2); $customer = Customer::findOrFail($data['customer_id']);
$wallet = Wallet::where('owner', $data['customer_id'])->whereNull('fund_id')->first(); $wallet_interface = new WalletInterface($data['customer_id'], (int) $customer->type);
$wallet = $wallet_interface->wallet;
$data['total_price_original'] = $data['amount'] * $data['price_original']; $data['total_price_original'] = $data['amount'] * $data['price_original'];
$data['payment_remind'] = 0; $data['payment_remind'] = 0;
$data['fee'] = $add_sell_price; $data['fee'] = $add_sell_price;
@@ -732,16 +734,16 @@ class SingleOrderController extends Controller
$this->Send($admin->mobile, $message); $this->Send($admin->mobile, $message);
}); });
$Walet = Wallet::where('owner', $data['customer_id'])->whereNull('fund_id')->first(); $mainWallet = Wallet::where('owner', $data['customer_id'])
$companyName = $this->Config('name_fa'); ->where('owner_type', $customer->type)
->main()
->first();
$companyurl = $this->Config('app_url'); $companyurl = $this->Config('app_url');
$weight = $data['amount']; $weight = $data['amount'];
$totalAmount = $data['total_price']; $totalAmount = $data['total_price'];
$pricePerGram = $totalAmount / $weight; $pricePerGram = $weight > 0 ? $totalAmount / $weight : 0;
$goldBalance = $mainWallet->gold ?? 0;
$goldBalance = $Walet->gold; $remainingBalance = $mainWallet->cash ?? 0;
$remainingBalance = $Walet->cash;
$message = "$companyName\n"; $message = "$companyName\n";
$message .= "خرید آبشده\n"; $message .= "خرید آبشده\n";
@@ -47,8 +47,9 @@ class RequestsController extends Controller
$data['total_fee'] = $data['amount'] * $data['difference']; $data['total_fee'] = $data['amount'] * $data['difference'];
$myCustomer = Customer::find($customer_id); $myCustomer = Customer::find($customer_id);
$wallet_interface = new WalletInterface($customer_id, 2); $ownerType = (int) ($myCustomer?->type ?? 2);
$wallet = Wallet::where('owner', $customer_id)->whereNull('fund_id')->first(); $wallet_interface = new WalletInterface($customer_id, $ownerType);
$wallet = $wallet_interface->wallet;
$response = []; $response = [];
@@ -24,4 +24,10 @@ class Wallet extends Model
{ {
return $this->belongsTo('App\Models\Product','product_id'); return $this->belongsTo('App\Models\Product','product_id');
} }
/** کیف اصلی مشتری — بدون fund و بدون product */
public function scopeMain($query)
{
return $query->whereNull('fund_id')->whereNull('product_id');
}
} }