Viewing file: ReportController.php (9.12 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Models\Customer; use App\Models\Report; use App\Models\Transactions; use Carbon\Carbon; use Illuminate\Http\Request;
class ReportController extends Controller { public function report(Request $request){ $data['request_data']=$request->only('customer_id','main_type','sub_type','date'); $data['customers']=Customer::orderByDesc('created_at')->get();
return view('admin.report.index', $data); }
public function getCustomers(Request $request){
if($request->type){ $request->validate([ 'type'=>'required|in:master_reseller,reseller,customer' ]); $customers=Customer::orderByDesc('created_at')->where('type', $request->type)->select('id', 'first_name','last_name','email')->get(); }else{ $customers=Customer::orderByDesc('created_at')->select('id', 'first_name','last_name','email')->get(); }
return response()->json(['data'=>$customers]); }
public function getAllReports(Request $request){
if($request->type) { if ($request->type == 'topup') { $reports = Report::orderByDesc('created_at')->where('type', 'topup'); } else if ($request->type == 'message') { $reports = Report::orderByDesc('created_at')->where('type', 'message'); } else { $reports = Report::orderByDesc('created_at'); } }else{ $reports = Report::orderByDesc('created_at'); if ($request->customer_id) { $reports=$reports->where('customer_id', $request->customer_id); } if($request->main_type){ $reports = $reports->where('type', str_replace('_','',$request->main_type)); } if($request->sub_type){ $reports = $reports->where('sub_type', $request->sub_type); } if($request->date){ $dates= explode('-', $request->date); $fromDate= isset($dates[0])?$dates[0]:Carbon::now()->subDays(3); $toDate= isset($dates[1])?$dates[1]:Carbon::now(); $reports = $reports->whereBetween('created_at',[$fromDate, $toDate]); } } // $reports=$reports->get();
return datatables()->of($reports)
->addColumn('customer', function ($q) { $profile='<div><h6 class="d-block">'.$q->customer->full_name.'</h6><b>'.$q->customer->email.'</b></div>'; return $profile; }) ->addColumn('amount', function ($q) { $otherType= substr($q->amount, 0, 3); if (isset($otherType) && $otherType=='add'){ $amount ='+'.substr($q->amount, 3, 10000); }else if(isset($otherType) && $otherType=='sub'){ $amount ='-'.substr($q->amount, 3, 10000); }else{ $amount=$q->amount; }
return $amount; }) ->addColumn('date', function ($q) { return formatDate($q->created_at); })
->addColumn('type', function ($q) { if($q->type=='topup') { $addSub= substr($q->amount, 0, 3); $amountType=''; if(isset($addSub) && $addSub=='add'){ $amountType=' <span class="badge badge-success">Add</span>'; }else if(isset($addSub) && $addSub=='sub'){ $amountType=' <span class="badge badge-danger">Subtract</span>'; }
return '<b>' . ucfirst($q->type) .$amountType. '</b>'; }else { return '<b>' . ucfirst($q->type) . '</b>'; } })
->addColumn('amount', function ($q) { $amountType= substr($q->amount, 0, 1); $otherType= substr($q->amount, 0, 3); $amount=$q->amount;
if (isset($otherType) && $otherType=='add'){ $amount ='+'.substr($q->amount, 3, 10000); }else if(isset($otherType) && $otherType=='sub'){ $amount ='-'.substr($q->amount, 3, 10000); }
if (isset($amountType) && $amountType=='+') { $formatAmount = explode('+', $q->amount); }else { $formatAmount = explode('-', $q->amount); }
$formatAmount=isset($formatAmount[1])?$formatAmount[1]:0; if (isset($otherType) && $otherType=='add'){ $formatAmount =substr($q->amount, 3, 10000); }else if(isset($otherType) && $otherType=='sub'){ $formatAmount =substr($q->amount, 3, 10000); }
$showAmount=''; if($q->type=='topup'){ if(isset($q->customer->plan->masking_rate) && $q->sub_type=='masking'){ $formatAmount=(float)$q->customer->plan->masking_rate * (float)$formatAmount; }elseif(isset($q->customer->plan->non_masking_rate) && $q->sub_type=='non_masking'){ $formatAmount=(float)$q->customer->plan->non_masking_rate * (float)$formatAmount; } $showAmount=formatNumberWithCurrSymbol($formatAmount); }
if(isset($amountType) && $amountType=='+' || isset($otherType) && $otherType=='add'){ $amountType=$amount.' <span class="badge badge-success">Credit</span><hr>'.$showAmount; }else if(isset($amountType) && $amountType=='-' || isset($otherType) && $otherType=='sub'){ $amountType=$amount.' <span class="badge badge-danger">Debit</span><hr>'.$showAmount; }else{ $amountType=$amount; } return $amountType; })
->addColumn('sub_type', function ($q) use($request) { $type=''; if($request->type=='plan'){ $type='<span class="badge badge-success">Plan-Purchase</span>'; }else{ if($q->sub_type=='non_masking') { $type = '<span class=" badge bg-green">'.ucfirst(str_replace('_', ' ', $q->sub_type)).'</span>'; }else if($q->sub_type=='masking'){ $type = '<span class=" badge bg-blue">'.ucfirst(str_replace('_', ' ', $q->sub_type)).'</span>'; }else if($q->sub_type=='whatsapp'){ $type = '<span class="badge bg-info">'.ucfirst(str_replace('_', ' ', $q->sub_type)).'</span>'; }else{ $type = '<span class="badge bg-danger">'.ucfirst(str_replace('_', ' ', $q->sub_type)).'</span>'; } } return $type; })
->rawColumns(['customer','amount','date','type','sub_type'])
->toJson(); }
// Transactions Report public function transactions(Request $request){
$data['request_data']=$request->only('customer_id','main_type','sub_type','date'); $data['customers']=Customer::orderByDesc('created_at')->get();
return view('admin.report.transactions', $data); }
//getAlltransactions
public function getAllTransactions(Request $request){
$customers=Customer::where('added_by', 'admin')->pluck('id'); $reports = Transactions::orderByDesc('created_at')->whereIn('added_by', ['reseller','normal'])->whereIn('customer_id', $customers); if ($request->customer_id) { $reports=$reports->where('customer_id', $request->customer_id); } if($request->main_type){ $reports = $reports->where('type', $request->main_type); }
if($request->date){ $dates= explode('-', $request->date); $fromDate= isset($dates[0])?$dates[0]:Carbon::now()->subDays(3); $toDate= isset($dates[1])?$dates[1]:Carbon::now(); $reports = $reports->whereBetween('created_at',[$fromDate, $toDate]); }
// $reports=$reports->get();
return datatables()->of($reports)
->addColumn('profile', function ($q) { $profile='<div><h6 class="d-block">'.$q->customer->full_name.'</h6><b>'.$q->customer->email.'</b></div>'; return $profile; })
->addColumn('amount', function ($q) {
return formatNumberWithCurrSymbol($q->amount); })
->addColumn('type', function ($q) {
return ucwords(str_replace('_','-', $q->type)); })
->addColumn('status', function ($q) { if($q->status=='paid'){ $type=' <span class="badge badge-success">Paid</span>'; }else{ $type=' <span class="badge badge-danger">Unpaid</span>'; } return $type; })
->rawColumns(['profile','amount','status','type',])
->toJson(); }
}
|