diff --git a/app/Http/Controllers/Customer/WalletController.php b/app/Http/Controllers/Customer/WalletController.php index a19e0d7..0e89ac0 100644 --- a/app/Http/Controllers/Customer/WalletController.php +++ b/app/Http/Controllers/Customer/WalletController.php @@ -11,6 +11,7 @@ use App\Models\User; use App\Models\Withdraw; use App\Models\BankAccount; use App\Models\Price; +use App\Models\Setting; use App\Http\Controllers\Admin\TahesabController; use App\Services\ZaarboxPriceService; use Illuminate\Http\Request; @@ -185,6 +186,15 @@ class WalletController extends Controller if ($data['value'] > $wallet->cash) { throw new UnprocessableEntityHttpException('مقدار برداشت بیشتر از موجودی است'); } + + $min_withdraw = Setting::where('is_active', 1) + ->where('key', 'min_withdraw') + ->first(); + + if ($min_withdraw && (int) $min_withdraw->value > $data['value']) { + throw new UnprocessableEntityHttpException('حداقل مقدار برداشت ' . number_format((int) $min_withdraw->value) . ' ریال است'); + } + $Withdraw = Withdraw::create([ 'customer_id' => auth('customer')->id(), "cash" => $data['value'], diff --git a/app/Http/Controllers/Single/SingleOrderController.php b/app/Http/Controllers/Single/SingleOrderController.php index 62859d8..43dd385 100644 --- a/app/Http/Controllers/Single/SingleOrderController.php +++ b/app/Http/Controllers/Single/SingleOrderController.php @@ -60,6 +60,16 @@ class SingleOrderController extends Controller // throw new UnprocessableEntityHttpException('به دلیل نوسانات سفارشات بسته میباشد.'); $transactionType = $data['direction'] == 1 ? "خرید" : "فروش"; + $customer = Customer::find(auth('customer')->id() ?? ($data['customer_id'] ?? null)); + + if ($min_sell = Setting::where('key', 'minimum_sell')->where('is_active', 1)->first()) { + if ((int) $data['direction'] === 2 && (float) $min_sell->value > (float) $data['amount']) { + throw new UnprocessableEntityHttpException('حداقل میزان فروش ' . $min_sell->value . ' گرم میباشد'); + } + } + + $data['first_buy'] = auth('customer')->id() && $customer && $customer->first_buy_order !== 1; + if (auth('customer')->id()) { $data['customer_id'] = auth('customer')->id(); $latestPrice = Price::latest()->first(); @@ -187,6 +197,14 @@ class SingleOrderController extends Controller } } // $response['reward'] = $this->give_order_reward($data, $wallet, $wallet_interface); + if ((int) $data['direction'] === 1 && $customer) { + $referrer = Customer::where('referral_id', $customer->referral_to_id)->first(); + if (!empty($referrer) && $referrer->first_buy_order == 1 && $customer->first_buy_order !== 1) { + $customer->first_buy_order = true; + $customer->save(); + $response['bonus_gift'] = $this->bonusGift($customer, $referrer); + } + } if (isset($data['order'])) { $response['Notifications'] = $this->sendTransactionNotifications($data, $transactionType); } @@ -201,6 +219,12 @@ class SingleOrderController extends Controller $data['total_price'] = $data['amount'] * $data['price']; $data['payment_amount'] = $data['total_price']; + if (!empty($data['first_buy']) && ($minFirstBuy = Setting::where('is_active', 1)->where('key', 'min_first_buy')->value('value'))) { + if ($data['total_price'] < (int) $minFirstBuy) { + throw new UnprocessableEntityHttpException('حداقل سفارش اول ' . number_format((int) $minFirstBuy) . ' ریال میباشد.'); + } + } + $data['service_charge'] = isset($data['fee_free']) && $data['fee_free'] === true ? 0 : $data['service_charge_percentage'] * $data['amount']; $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; $data['amount'] -= $data['service_charge']; @@ -231,6 +255,13 @@ class SingleOrderController extends Controller $data['total_amount'] = $data['amount'] + ($data['product']['percent_fee_buy'] * $data['amount']); $data['total_price'] = $data['total_amount'] * $data['price']; $data['payment_amount'] = $data['total_price']; + + if (!empty($data['first_buy']) && ($minFirstBuy = Setting::where('is_active', 1)->where('key', 'min_first_buy')->value('value'))) { + if ($data['total_price'] < (int) $minFirstBuy) { + throw new UnprocessableEntityHttpException('حداقل سفارش اول ' . number_format((int) $minFirstBuy) . ' ریال میباشد.'); + } + } + $data['service_charge'] = isset($data['fee_free']) && $data['fee_free'] === true ? 0 : ($data['product']['percent_fee_buy'] * $data['amount']); $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; $data['wallet'] = $wallet; @@ -724,4 +755,92 @@ class SingleOrderController extends Controller return (float) ($direction === 1 ? $product->cash_fee_buy : $product->cash_fee_sell); } + + private function bonusGift($customer, $referrer) + { + $bonus = Setting::where('key', 'referral_bonus_amount')->where('is_active', 1)->first(); + if ($bonus === null) { + return false; + } + + $bonus = json_decode($bonus->value, true); + $customer_bonus = $bonus[0]['customer'] ?? 0; + $referrer_bonus = $bonus[0]['referrer'] ?? 0; + + if ($customer_bonus > 0) { + $wallet_interface = new WalletInterface($customer->id, $customer->type); + + $Withdraw = Withdraw::create([ + 'customer_id' => $customer->id, + 'cash' => $customer_bonus, + 'note' => 'پاداش اولین سفارش', + 'sts' => 4, + ]); + $transaction = new TransactionInterface($wallet_interface, $_inp = [ + 'gold' => 0, + 'cash' => $customer_bonus, + 'expire_at' => now(), + 'note' => 'پاداش اولین سفارش', + 'withdraw_id' => $Withdraw->id, + 'active' => true, + ], 3, true, true, 0); + $message = 'کیف پول شما به اندازه ' . number_format($customer_bonus) . ' ریال در تاریخ ' . verta()->format('Y/n/j H:i:s') . " شارژ شد بابت\n" + . 'پاداش اولین سفارش' + . ' ' + . $this->Config('name_fa'); + $this->Send($customer->mobile, $message); + TransactionsAdmin::create([ + 'customer_id' => $customer->id, + 'customer_to_id' => $customer->referral->id ?? null, + 'user_id' => $customer->id, + 'wallet' => null, + 'transaction_id' => $transaction->id ?? null, + 'cash' => $customer_bonus, + 'note' => 'پاداش خرید اول', + 'type' => 1, + 'gold' => 0, + 'balance_cash' => 0, + 'withdraw_id' => $Withdraw->id, + ]); + } + + if ($referrer_bonus > 0) { + $wallet_interface = new WalletInterface($referrer->id, $referrer->type); + + $Withdraw = Withdraw::create([ + 'customer_id' => $referrer->id, + 'cash' => $referrer_bonus, + 'note' => 'پاداش اولین سفارش زیرمجموعه', + 'sts' => 4, + ]); + $referrerTransaction = new TransactionInterface($wallet_interface, $_inp = [ + 'gold' => 0, + 'cash' => $referrer_bonus, + 'expire_at' => now(), + 'note' => 'پاداش اولین سفارش زیرمجموعه', + 'withdraw_id' => $Withdraw->id, + 'active' => true, + ], 3, true, true, 0); + $message = 'کیف پول شما به اندازه ' . number_format($referrer_bonus) . ' ریال در تاریخ ' . verta()->format('Y/n/j H:i:s') . " شارژ شد بابت\n" + . 'بابت پاداش اولین سفارش زیرمجموعه' + . ' ' + . $this->Config('name_fa'); + $this->Send($referrer->mobile, $message); + TransactionsAdmin::create([ + 'customer_id' => $customer->id, + 'customer_to_id' => $referrer->id, + 'user_id' => $customer->id, + 'wallet' => null, + 'transaction_id' => $referrerTransaction->id ?? null, + 'cash' => $referrer_bonus, + 'note' => 'پاداش خرید اول زیرمجموعه', + 'type' => 1, + 'gold' => 0, + 'balance_cash' => 0, + 'withdraw_id' => $Withdraw->id, + ]); + } + + return false; + } }