Viewing file: TopUpController.php (8.06 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller; use App\Models\Customer; use App\Models\Plan; use App\Models\Report; use App\Models\TopUpRequest; use App\Models\Transactions; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB;
class TopUpController extends Controller { public function request() {
return view('customer.topup.request'); }
public function getAllRequest() { $customer = auth('customer')->user(); if ($customer->type == 'master_reseller') { $topup_requests = TopUpRequest::with('customer')->where('admin_id', $customer->id)->whereIn('customer_type', ['reseller', 'master_reseller_customer'])->orderByDesc('created_at')->get(); } else if ($customer->type == 'reseller') { $topup_requests = TopUpRequest::with('customer')->where('admin_id', $customer->id)->where('customer_type', 'reseller_customer')->orderByDesc('created_at')->get(); }
$sellerWallet = $customer->wallet()->first(); $credit = $sellerWallet->credit;
return datatables()->of($topup_requests) ->addColumn('customer', function ($q) { return "<a href='".route('customer.reseller-customers.edit',['reseller_customer'=>$q->customer->id])."'>" . $q->customer->full_name . "</a>"; }) ->addColumn('credit', function ($q) { $formatAmount=0; if(isset($q->customer->plan->masking_rate) && $q->credit_type=='masking'){ $formatAmount=$q->customer->plan->masking_rate * $q->credit; }elseif(isset($q->customer->plan->non_masking_rate) && $q->credit_type=='non_masking'){ $formatAmount=$q->customer->plan->non_masking_rate * $q->credit; } $credit=$q->credit.'<hr class="m-0"><span>'.formatNumberWithCurrSymbol($formatAmount).'</span>'; return $credit; }) ->addColumn('created_at', function ($q) { $date=formatDate($q->created_at); return $date; }) ->addColumn('credit_type', function ($q) { if($q->credit_type=='masking'){ return "SenderID"; }else{ return "Non SenderID"; } })
->addColumn('payment_status', function ($q) { if($q->payment_status=='paid'){ $payment_status= '<strong class="text-success"> '.ucfirst($q->payment_status).' </strong>'; }else{ $payment_status= '<strong class="text-danger"> '.ucfirst($q->payment_status).' </strong>'; } return $payment_status; })
->addColumn('status', function ($q) { $status=''; if($q->status=='pending'){ $status='<span class="badge badge-danger p-2">'.ucfirst($q->status).'</span>'; }else if($q->status=='approved'){ $status='<span class="badge badge-success p-2">'.ucfirst($q->status).'</span>'; }else{ $status='<span class="badge badge-info p-2">'.ucfirst($q->status).'</span>'; } return $status; }) ->addColumn('action', function ($q) use ($credit) { $approveBtn = ''; if ($q->status == 'pending') { if ($q->credit <= $credit) { $approveBtn = '<button class="mr-1 btn btn-sm btn-info" data-message="Are you sure you want to approved the request ?" data-action=' . route('customer.topup.request.status') . ' data-input={"id":"' . $q->id . '","status":"approved"} data-toggle="modal" data-target="#modal-confirm" >Approve</button>'; } else { $approveBtn = '<button class="mr-1 btn btn-sm btn-warning disabled" disabled>Low Credit</button>'; }
return $approveBtn . ' <button class="btn btn-sm btn-danger" data-message="Are you sure you want to reject the request ?" data-action=' . route('customer.topup.request.status') . ' data-input={"id":"' . $q->id . '","status":"rejected"} data-toggle="modal" data-target="#modal-confirm" >Reject</button>'; }else if($q->status=='rejected'){ return '<button class="mr-1 btn btn-sm btn-danger disabled" disabled>' . ucfirst($q->status) . '</button>'; } else { return '<button class="mr-1 btn btn-sm btn-success disabled" disabled>' . ucfirst($q->status) . '</button>'; } }) ->rawColumns(['action','status', 'customer','created_at','payment_status','credit']) ->toJson(); }
public function requestStatus(Request $request) {
$request->validate([ 'status' => 'required|in:approved,rejected' ]);
DB::beginTransaction();
try {
$reseller=auth('customer')->user(); $topup_request = TopUpRequest::where('id', $request->id)->firstOrFail(); $customer = Customer::where('id', $topup_request->customer_id)->firstOrFail();
if ($request->status == 'approved') {
/* Customer Wallet */ $wallet = $customer->wallet; $wallet->credit = $wallet->credit + $topup_request->credit; $wallet->save();
/* Reseller Wallet */ $resellerWallet = $reseller->wallet; $resellerWallet->credit = $resellerWallet->credit - $topup_request->credit; $resellerWallet->save();
$topup_request->status = 'approved'; $topup_request->payment_status='paid'; $topup_request->save();
/* Customer Report */ $report= new Report(); $report->customer_id=$customer->id; $report->ref_id=$wallet->id; $report->type='topup'; $report->sub_type=$topup_request->credit_type; $report->amount='+'.$topup_request->credit; $report->save();
/* Reseller Report */ $report= new Report(); $report->customer_id=$reseller->id; $report->ref_id=$resellerWallet->id; $report->type='topup'; $report->sub_type=$topup_request->credit_type; $report->amount='-'.$topup_request->credit; $report->save();
if ($topup_request->invoice_id){ $invoice=Invoice::find($topup_request->invoice_id); if ($invoice){ if ($request->status == 'approved') { $invoice->payment_status = 'approved'; }else{ $invoice->payment_status = 'rejected'; } $invoice->save(); } }
$transactions = Transactions::where('ref_id', $topup_request->id)->where('type','top_up')->first(); if ($transactions) { $transactions->status = 'paid'; $transactions->save(); }
}
if ($request->status == 'rejected') { $topup_request->status = 'rejected'; $topup_request->save(); }
cache()->forget('wallet_'.$customer->id); cache()->forget('wallet_'.$reseller->id); DB::commit(); return redirect()->back()->with('success', 'TopUp Request Successfully Accepted'); } catch (\Throwable $ex) { DB::rollback(); return redirect()->back()->withErrors(['msg' => $ex->getMessage()]); } }
}
|