관리-도구
편집 파일: UserController.php
<?php namespace App\Http\Controllers; use App\Models\Admin; use App\Models\Deposit; use App\Models\Transaction; use App\Models\Withdraw; use App\Models\WithdrawGateway; use App\Models\Payment; use App\Models\UserInterest; use App\Models\GeneralSetting; use App\Models\RefferedCommission; use App\Models\User; use Illuminate\Http\Request; use Nette\Utils\Random; use App\Models\BonusReport; use Purifier; use Auth; use Hash; use Illuminate\Support\Str; class UserController extends Controller { public function dashboard() { $pageTitle = "Dashboard"; $totalInvest = Payment::where('user_id', Auth::id())->where('payment_status', 1)->sum('amount'); $currentInvest = Payment::where('user_id', Auth::id())->where('payment_status', 1)->latest()->first('amount'); $currentPlan = Payment::with('property')->where('user_id', Auth::id())->where('payment_status', 1)->latest()->first(); $allPlan = Payment::with('property')->where('user_id', Auth::id())->latest()->paginate(10,['*'], 'property'); $withdraw = Withdraw::where('user_id', Auth::id())->where('status', 1)->sum('withdraw_amount'); $interestLogs = UserInterest::with('payment')->where('user_id', Auth::id())->sum('interest_amount'); $commison = RefferedCommission::where('reffered_to', Auth::id())->sum('amount'); $pendingInvest = Payment::where('user_id', Auth::id())->where('payment_status', 2)->sum('amount'); $pendingWithdraw = Withdraw::where('user_id', Auth::id())->where('status', 0)->sum('withdraw_amount'); $totalWithdraw = Withdraw::where('user_id', Auth::id())->where('status', 1)->sum('withdraw_amount'); $totalDeposit = Deposit::where('user_id', Auth::id())->where('payment_status', 1)->sum('amount'); $totalPendingDeposit = Deposit::where('user_id', Auth::id())->where('payment_status',2)->sum('amount'); return view('frontend.user.dashboard', compact('commison','pageTitle','interestLogs', 'totalInvest', 'currentInvest', 'currentPlan', 'allPlan', 'withdraw','pendingInvest','pendingWithdraw','totalWithdraw','totalDeposit','totalPendingDeposit')); } public function profile() { $pageTitle = 'Profile Edit'; $user = auth()->user(); return view('frontend.user.profile', compact('pageTitle', 'user')); } public function profileUpdate(Request $request) { $request->validate([ 'fname' => 'required', 'lname' => 'required', 'username' => 'required|unique:users,username,' . Auth::id(), 'image' => 'sometimes|image|mimes:jpg,png,jpeg', 'email' => 'required|unique:users,email,' . Auth::id(), 'phone' => 'unique:users,id,'. Auth::id(), ], [ 'fname.required' => 'First Name is required', 'lname.required' => 'Last Name is required', ]); $user = auth()->user(); if ($request->hasFile('image')) { $filename = uploadImage($request->image, filePath('user'), $user->image); $user->image = $filename; } $data = [ 'country' => $request->country, 'city' => $request->city, 'zip' => $request->zip, 'state' => $request->state, ]; $user->fname = $request->fname; $user->lname = $request->lname; $user->username = $request->username; $user->phone = $request->phone; $user->email = $request->email; $user->address = $data; $user->save(); $notify[] = ['success', 'Successfully Updated Profile']; return back()->withNotify($notify); } public function changePassword(){ $pageTitle = 'Change Password'; return view('frontend.user.auth.changepassword',compact('pageTitle')); } public function updatePassword(Request $request){ $request->validate([ 'oldpassword' => 'required|min:6', 'password' => 'min:6|confirmed', ]); $user=User::find(Auth::id()); if (!Hash::check($request->oldpassword, $user->password)) { return redirect()->back()->with('error', 'Old password do not match'); } else { $user->password = bcrypt($request->password); $user->save(); return redirect()->back()->with('success', 'Password Updated'); } } public function transaction() { $pageTitle = "Transactions"; $transactions = Transaction::where('user_id', auth()->id())->latest()->with('user')->paginate(); return view('frontend.user.transaction', compact('pageTitle', 'transactions')); } public function withdraw() { $pageTitle = 'Withdraw Money'; $withdraws = WithdrawGateway::where('status', 1)->latest()->get(); return view('frontend.user.withdraw.index', compact('pageTitle', 'withdraws')); } public function withdrawCompleted(Request $request) { $request->validate([ 'method' => 'required|integer', 'amount' => 'required|numeric', 'final_amo' => 'required|numeric', 'email' => 'required|email' ]); $payment = Payment::where('user_id', auth()->id())->where('payment_status',1)->count(); if($payment <= 0){ $notify[] = ['error', 'You have to invest on a plan to withdraw']; return back()->withNotify($notify); } $withdraw = WithdrawGateway::findOrFail($request->method); if (auth()->user()->balance < $request->final_amo) { $notify[] = ['error', 'Insuficient Balance']; return back()->withNotify($notify); } if ($request->final_amo < $withdraw->min_amount || $request->final_amo > $withdraw->max_amount) { $notify[] = ['error', 'Please follow the withdraw limits']; return back()->withNotify($notify); } if ($withdraw->charge_type == 'percent') { $charge = ($withdraw->charge * $request->amount) / 100; $total = $request->amount + ($withdraw->charge * $request->amount) / 100; } else { $total = $request->amount + $withdraw->charge; $charge = $withdraw->charge; } if ($total != $request->final_amo) { $notify[] = ['error', 'Invalid Amount']; return back()->withNotify($notify); } auth()->user()->balance = auth()->user()->balance - $total; auth()->user()->save(); $data = [ 'email' => $request->email, 'account_information' => Purifier::clean($request->account_information), 'note' => Purifier::clean($request->note) ]; $mailData = Withdraw::create([ 'user_id' => auth()->id(), 'withdraw_method_id' => $request->method, 'transaction_id' => strtoupper(Random::generate(15)), 'user_withdraw_prof' => $data, 'withdraw_charge' =>$charge, 'withdraw_amount' => $total, 'status' => 0 ]); $admin = Admin::first(); $notify[] = ['success', 'Withdraw Successfully done']; return back()->withNotify($notify); } public function withdrawFetch(Request $request) { $withdraw = WithdrawGateway::findOrFail($request->id); return $withdraw; } public function allWithdraw() { $pageTitle = 'All withdraw'; $withdrawlogs = Withdraw::where('user_id', auth()->id())->latest()->with('withdrawMethod')->paginate(10); return view('frontend.user.withdraw.withdraw_log', compact('pageTitle', 'withdrawlogs')); } public function pendingWithdraw() { $pageTitle = 'Pending withdraw'; $withdrawlogs = Withdraw::where('user_id', auth()->id())->where('status', 0)->latest()->with('withdrawMethod')->paginate(10); return view('frontend.user.withdraw.withdraw_log', compact('pageTitle', 'withdrawlogs')); } public function completeWithdraw() { $pageTitle = 'Complete withdraw'; $withdrawlogs = Withdraw::where('user_id', auth()->id())->where('status', 1)->latest()->with('withdrawMethod')->paginate(10); return view('frontend.user.withdraw.withdraw_log', compact('pageTitle', 'withdrawlogs')); } public function commision(){ $pageTitle = 'Complete withdraw'; $commison = RefferedCommission::where('reffered_to', Auth::id())->latest()->paginate(10,['*'], 'commison'); return view('frontend.user.commision_log', compact('pageTitle', 'commison')); } public function returnInterest() { $general = GeneralSetting::first(); $invests = Payment::with('property', 'user')->where('payment_status', 1)->latest()->get(); foreach ($invests as $invest) { //check_user $user = User::where('id', $invest->user->id)->firstOrFail(); if ($invest->next_payment_date) { //check current time == paymentdate if (now()->greaterThanOrEqualTo($invest->next_payment_date)) { //find interest rate $interestRate = $invest->property->return_interest; $returnAmount = 0; if ($invest->property->interest_status == 'percentage') { $returnAmount = ($invest->amount * $interestRate) / 100; } if ($invest->property->interest_status == 'fixed') { $returnAmount = $invest->property->return_interest; } $user->balance += $returnAmount; $updatePaymentDate = $invest->next_payment_date->addHour($invest->property->time->time); $interestAmount = $returnAmount; //paymentupdate on next date $updatePayment = Payment::where('property_id',$invest->property_id)->where('next_payment_date', $invest->next_payment_date)->first(); $count = Payment::where('property_id',$invest->property_id)->where('next_payment_date', $invest->next_payment_date)->sum('pay_count'); if($invest->property->return_for==1){ if ($count < $invest->property->how_many_time) { $updatePayment->next_payment_date = $updatePaymentDate; $updatePayment->interest_amount += $interestAmount; $updatePayment->pay_count += 1; UserInterest::create([ 'user_id'=>$user->id, 'payment_id'=>$invest->id, 'interest_amount'=>$interestAmount ]); sendMail('RETURN_INTEREST', [ 'property' => $invest->property->property_name, 'amount' => $returnAmount, 'currency' =>$general->site_currency ], $invest->user); $updatePayment->save(); $user->save(); refferMoney($user->id, 'interest', $returnAmount); if($invest->property->capital_back==1){ if ($updatePayment->pay_count == $invest->property->how_many_time ) { $user->balance +=$invest->amount; $updatePayment->next_payment_date = null; $updatePayment->pay_count=null; $updatePayment->save(); $user->save(); refferMoney($user->id, 'interest', $returnAmount); } } } } else{ $updatePayment->next_payment_date = $updatePaymentDate; $updatePayment->interest_amount += $interestAmount; $updatePayment->pay_count += 1; UserInterest::create([ 'user_id'=>$user->id, 'payment_id'=>$invest->id, 'interest_amount'=>$interestAmount ]); sendMail('RETURN_INTEREST', [ 'property' => $invest->property->property_name, 'amount' => $returnAmount, 'currency' =>$general->site_currency ], $invest->user); $updatePayment->save(); $user->save(); refferMoney($user->id, 'interest', $returnAmount); } } } } } public function pendingInvest() { $data['payments'] = Payment::where('user_id', Auth::id())->where('payment_status', 2)->latest()->get(); $data['pageTitle'] = 'Pending Invest'; return view('frontend.user.pending_invest')->with($data); } public function allInvest () { $data['payments'] = Payment::where('user_id', Auth::id())->where('payment_status','!=', 0)->latest()->get(); $data['pageTitle'] = 'All Invest'; return view('frontend.user.pending_invest')->with($data); } public function allInvested() { $data['properties'] = Payment::where('user_id', Auth::id())->where('payment_status','!=', 0)->latest()->get(); $data['pageTitle'] = 'All Invested'; return view('frontend.pages.invested')->with($data); } public function interestLog() { $data['interestLogs'] = UserInterest::with('payment')->where('user_id', Auth::id())->latest()->paginate(); $data['pageTitle'] = 'Return interest Log'; return view('frontend.user.interest_log')->with($data); } public function transferMoney(Request $request) { $request->validate([ 'email' => 'required|email', 'amount' => 'required|numeric|gt:0' ]); $payment = Payment::where('user_id', auth()->id())->where('payment_status',1)->count(); if($payment <= 0){ $notify[] = ['error', 'You have to invest on a plan to use Signup Balance']; return back()->withNotify($notify); } $receiver = User::where('email', $request->email)->first(); if(auth()->user()->email == $request->email){ $notify[] = ['error', 'You can not send money to your account']; return back()->withNotify($notify); } if(!$receiver){ $notify[] = ['error', 'No User Found With this email']; return back()->withNotify($notify); } if(auth()->user()->balance < $request->amount){ $notify[] = ['error', 'Insufficient Balance']; return back()->withNotify($notify); } $user = auth()->user(); $user->balance = $user->balance - $request->amount; $user->save(); $general = GeneralSetting::first(); $trx = strtoupper(Str::random()); Transaction::create([ 'trx' => $trx, 'gateway_id' => 0, 'amount' => $request->amount, 'currency' => $general->site_currency, 'details' => 'Send Money', 'charge' => 0, 'type' => '-', 'gateway_transaction' =>$trx , 'user_id' => auth()->id(), 'payment_status' => 1 ]); $receiver->balance = $receiver->balance + $request->amount; $receiver->save(); $trx = strtoupper(Str::random()); Transaction::create([ 'trx' => $trx, 'gateway_id' => 0, 'amount' => $request->amount, 'currency' => $general->site_currency, 'details' => 'Receive Money', 'charge' => 0, 'type' => '-', 'gateway_transaction' =>$trx , 'user_id' => $receiver->id, 'payment_status' => 1 ]); $notify[] = ['success', 'Successfully Send Money']; return back()->withNotify($notify); } public function transactionLog() { $pageTitle = 'Transaction Log'; $transactions = Transaction::where('user_id', auth()->id())->where('payment_status', 1)->latest()->paginate(); return view('frontend.user.transaction',compact('pageTitle','transactions')); } }