Viewing file: CustomerController.php (29.3 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers\Admin;
use App\Events\SendMail; use App\Http\Controllers\Controller; use App\Models\Customer; use App\Models\CustomerPlan; use App\Models\CustomerSettings; use App\Models\ImageUrl; use App\Models\Plan; use App\Models\Transaction; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB;
class CustomerController extends Controller { public function index() { return view('admin.customers.index'); }
public function getAll() { $customers = Customer::select(['id', 'first_name', 'last_name', 'email', 'status', 'created_at']);
return datatables()->of($customers) ->addColumn('profile', function ($q) { $name='<p class="m-0">'.'<b>'.trans('admin.name').'</b>'.': '.'<span class="text-muted">'.$q->full_name.'</span>'.'</p>'; $email='<p class="m-0">'.'<b>'.trans('admin.email').'</b>'.': '.'<span class="text-muted">'.$q->email.'</span>'.'</p>'; return '<div>'.$name.$email.'</div>'; })
->addColumn('plan_details', function ($q) { if($q->plan && $q->plan->plan) { $plan = $q->plan->plan; $expireDate = now(); if ($plan->recurring_type == 'weekly') { $expireDate = new Carbon($plan->created_at); $expireDate->addWeek(); } else if ($plan->recurring_type == 'monthly') { $expireDate = new Carbon($plan->created_at); $expireDate->addMonth(); } else if ($plan->recurring_type == 'yearly') { $expireDate = new Carbon($plan->created_at); $expireDate->addYear(); }
$title = '<p class="m-0 d-block">' . '<b>'.trans('admin.plan_name').'</b>' . ': ' .'<span class="text-muted">'. urlencode($plan->title).'</span>'. '</p>'; $recurring_type = '<p class="m-0 d-block">' . '<b>'.trans('admin.recurring_type') .'</b>'. ': ' . '<span class="text-muted">'.$plan->recurring_type.'</span>' . '</p>'; $purchaseAt = '<p class="m-0 d-block">' .'<b>'. trans('admin.purchase_at').'</b>' . ': ' . '<span class="text-muted">'.formatDate($plan->created_at).'</span>' . '</p>'; $expiredAt = '<p class="m-0 d-block">' . '<b>'.trans('admin.expired_at') .'</b>'. ': ' . '<span class="text-muted">'.formatDate($expireDate).'</span>' . '</p>';
return '<div class="d-block">' . $title . $recurring_type . $purchaseAt . $expiredAt . '</div>'; } })
->addColumn('wallet_details', function ($q) { if($q->plan) { $current_plan = $q->plan; if($q->is_word_limit=='yes'){ $words='Unlimited'; }else { $words = '<span class="text-muted">' . '<b>' . $current_plan->available_word . '</b>/' . '<small>' . $current_plan->word_limit . '</small>' . '</span>'; }
if($q->is_image_limit=='yes'){ $images='Unlimited'; }else { $images = '<span class="text-muted">' . '<b>' . $current_plan->available_image . '</b>/' . '<small>' . $current_plan->image_limit . '</small>' . '</span>'; }
if($q->is_code_limit=='yes'){ $codes='Unlimited'; }else { $codes = '<span class="text-muted">' . '<b>' . $current_plan->available_code . '</b>/' . '<small>' . $current_plan->code_limit . '</small>' . '</span>'; }
$wordLimit = '<p class="m-0 d-block">' . '<b>'.trans('admin.word_limit').'</b>' . ': ' .$words. '</p>'; $imageLimit = '<p class="m-0 d-block">' . '<b>'.trans('admin.image_limit') .'</b>'. ': ' . $images. '</p>'; $codeLimit = '<p class="m-0 d-block">' .'<b>'. trans('admin.code_limit').'</b>' . ': ' .$codes . '</p>';
return '<div class="d-block">' . $wordLimit . $imageLimit . $codeLimit . '</div>'; } })
->addColumn('status', function ($q) {
if($q->status=='Active'){ $status='<span class="badge bg-green custom-badge">Active</span>'; }else{ $status='<span class="badge bg-danger custom-badge">Banned</span>'; } return $status; })
->addColumn('action', function (Customer $q) { if($q->status=='Active'){ $status=' <a class="dropdown-item" href="#" data-message="'.trans('admin.message.customer_status_change_to_banned_warn').'" data-action='.route('admin.customer.change.status',['id'=>$q->id,'status'=>'banned']).' data-input={"_method":"get"} data-toggle="modal" data-target="#modal-confirm">'.trans('admin.banned').'</a>'; }else{ $status='<a class="dropdown-item" href="#" data-message="'.trans('admin.message.customer_status_change_to_actv_warn').'" data-action='.route('admin.customer.change.status',['id'=>$q->id,'status'=>'active']).' data-input={"_method":"get"} data-toggle="modal" data-target="#modal-confirm">'.trans('admin.active').'</a>'; }
$actions='<div class="btn-group-vertical"> <div class="btn-group"> <i class="fa fa-ellipsis-h text-success" type="button" data-toggle="dropdown" aria-expanded="false"></i> <ul class="dropdown-menu" style=""> <li><a class="dropdown-item " href="'.route('admin.customers.edit', [$q->id]). '" >Edit</a></li> <li>'.$status.'</li> <li><a class="dropdown-item" href="#" data-message="Are you sure you want to delete ?" data-action='.route('admin.customers.destroy',[$q]).' data-input={"_method":"delete"} data-toggle="modal" data-target="#modal-confirm" >Delete</a> </li> <li><button class="dropdown-item" data-message="You will be logged in as customer?" data-action='.route('admin.customer.login.ass').' data-input={"id":'.$q->id.'} data-toggle="modal" data-target="#modal-confirm">Login As</button></li> </ul> </div> </div>';
return $actions; }) ->rawColumns(['action','status','plan_details', 'profile','wallet_details']) ->toJson(); }
public function bannedUser() { return view('admin.customers.banned_list'); } public function getAllBannedUser() { $customers = Customer::select(['id', 'first_name', 'last_name', 'email', 'status', 'created_at'])->where('status', 'banned');
return datatables()->of($customers) ->addColumn('profile', function ($q) { $name='<p class="m-0">'.'<b>'.trans('admin.name').'</b>'.': '.'<span class="text-muted">'.$q->full_name.'</span>'.'</p>'; $email='<p class="m-0">'.'<b>'.trans('admin.email').'</b>'.': '.'<span class="text-muted">'.$q->email.'</span>'.'</p>'; return '<div>'.$name.$email.'</div>'; })
->addColumn('plan_details', function ($q) { if($q->plan && $q->plan->plan) { $plan = $q->plan->plan; $expireDate = now(); if ($plan->recurring_type == 'weekly') { $expireDate = new Carbon($plan->created_at); $expireDate->addWeek(); } else if ($plan->recurring_type == 'monthly') { $expireDate = new Carbon($plan->created_at); $expireDate->addMonth(); } else if ($plan->recurring_type == 'yearly') { $expireDate = new Carbon($plan->created_at); $expireDate->addYear(); }
$title = '<p class="m-0 d-block">' . '<b>'.trans('admin.plan_name').'</b>' . ': ' .'<span class="text-muted">'. urlencode($plan->title).'</span>'. '</p>'; $recurring_type = '<p class="m-0 d-block">' . '<b>'.trans('admin.recurring_type') .'</b>'. ': ' . '<span class="text-muted">'.$plan->recurring_type.'</span>' . '</p>'; $purchaseAt = '<p class="m-0 d-block">' .'<b>'. trans('admin.purchase_at').'</b>' . ': ' . '<span class="text-muted">'.formatDate($plan->created_at).'</span>' . '</p>'; $expiredAt = '<p class="m-0 d-block">' . '<b>'.trans('admin.expired_at') .'</b>'. ': ' . '<span class="text-muted">'.formatDate($expireDate).'</span>' . '</p>';
return '<div class="d-block">' . $title . $recurring_type . $purchaseAt . $expiredAt . '</div>'; } })
->addColumn('wallet_details', function ($q) { if($q->plan) { $current_plan = $q->plan; if($q->is_word_limit=='yes'){ $words='Unlimited'; }else { $words = '<span class="text-muted">' . '<b>' . $current_plan->available_word . '</b>/' . '<small>' . $current_plan->word_limit . '</small>' . '</span>'; }
if($q->is_image_limit=='yes'){ $images='Unlimited'; }else { $images = '<span class="text-muted">' . '<b>' . $current_plan->available_image . '</b>/' . '<small>' . $current_plan->image_limit . '</small>' . '</span>'; }
if($q->is_code_limit=='yes'){ $codes='Unlimited'; }else { $codes = '<span class="text-muted">' . '<b>' . $current_plan->available_code . '</b>/' . '<small>' . $current_plan->code_limit . '</small>' . '</span>'; }
$wordLimit = '<p class="m-0 d-block">' . '<b>'.trans('admin.word_limit').'</b>' . ': ' .$words. '</p>'; $imageLimit = '<p class="m-0 d-block">' . '<b>'.trans('admin.image_limit') .'</b>'. ': ' . $images. '</p>'; $codeLimit = '<p class="m-0 d-block">' .'<b>'. trans('admin.code_limit').'</b>' . ': ' .$codes . '</p>';
return '<div class="d-block">' . $wordLimit . $imageLimit . $codeLimit . '</div>'; } })
->addColumn('status', function ($q) {
if($q->status=='Active'){ $status='<span class="badge bg-green custom-badge">Active</span>'; }else{ $status='<span class="badge bg-danger custom-badge">Banned</span>'; } return $status; })
->addColumn('action', function (Customer $q) { if($q->status=='Active'){ $status=' <a class="dropdown-item" href="#" data-message="'.trans('admin.message.customer_status_change_to_banned_warn').'" data-action='.route('admin.customer.change.status',['id'=>$q->id,'status'=>'banned']).' data-input={"_method":"get"} data-toggle="modal" data-target="#modal-confirm">'.trans('admin.banned').'</a>'; }else{ $status='<a class="dropdown-item" href="#" data-message="'.trans('admin.message.customer_status_change_to_actv_warn').'" data-action='.route('admin.customer.change.status',['id'=>$q->id,'status'=>'active']).' data-input={"_method":"get"} data-toggle="modal" data-target="#modal-confirm">'.trans('admin.active').'</a>'; }
$actions='<div class="btn-group-vertical"> <div class="btn-group"> <i class="fa fa-ellipsis-h text-success" type="button" data-toggle="dropdown" aria-expanded="false"></i> <ul class="dropdown-menu" style=""> <li><a class="dropdown-item " href="'.route('admin.customers.edit', [$q->id]). '" >Edit</a></li> <li>'.$status.'</li> <li><a class="dropdown-item" href="#" data-message="Are you sure you want to delete ?" data-action='.route('admin.customers.destroy',[$q]).' data-input={"_method":"delete"} data-toggle="modal" data-target="#modal-confirm" >Delete</a> </li> <li><button class="dropdown-item" data-message="You will be logged in as customer?" data-action='.route('admin.customer.login.ass').' data-input={"id":'.$q->id.'} data-toggle="modal" data-target="#modal-confirm">Login As</button></li> </ul> </div> </div>';
return $actions; }) ->rawColumns(['action','status','plan_details', 'profile','wallet_details']) ->toJson(); }
public function activedUser() { return view('admin.customers.active_list'); } public function getAllActiveUser(Request $request) { if($request->limit) { $customers = Customer::select(['id', 'first_name', 'last_name', 'email', 'status', 'created_at'])->where('status', 'active')->limit('10')->get(); }else { $customers = Customer::select(['id', 'first_name', 'last_name', 'email', 'status', 'created_at'])->where('status', 'active'); }
return datatables()->of($customers) ->addColumn('profile', function ($q) { $name='<p class="m-0">'.'<b>'.trans('admin.name').'</b>'.': '.'<span class="text-muted">'.$q->full_name.'</span>'.'</p>'; $email='<p class="m-0">'.'<b>'.trans('admin.email').'</b>'.': '.'<span class="text-muted">'.$q->email.'</span>'.'</p>'; return '<div>'.$name.$email.'</div>'; })
->addColumn('plan_details', function ($q) { if($q->plan && $q->plan->plan) { $plan = $q->plan->plan; $expireDate = now(); if ($plan->recurring_type == 'weekly') { $expireDate = new Carbon($plan->created_at); $expireDate->addWeek(); } else if ($plan->recurring_type == 'monthly') { $expireDate = new Carbon($plan->created_at); $expireDate->addMonth(); } else if ($plan->recurring_type == 'yearly') { $expireDate = new Carbon($plan->created_at); $expireDate->addYear(); }
$title = '<p class="m-0 d-block">' . '<b>'.trans('admin.plan_name').'</b>' . ': ' .'<span class="text-muted">'. urlencode($plan->title).'</span>'. '</p>'; $recurring_type = '<p class="m-0 d-block">' . '<b>'.trans('admin.recurring_type') .'</b>'. ': ' . '<span class="text-muted">'.$plan->recurring_type.'</span>' . '</p>'; $purchaseAt = '<p class="m-0 d-block">' .'<b>'. trans('admin.purchase_at').'</b>' . ': ' . '<span class="text-muted">'.formatDate($plan->created_at).'</span>' . '</p>'; $expiredAt = '<p class="m-0 d-block">' . '<b>'.trans('admin.expired_at') .'</b>'. ': ' . '<span class="text-muted">'.formatDate($expireDate).'</span>' . '</p>';
return '<div class="d-block">' . $title . $recurring_type . $purchaseAt . $expiredAt . '</div>'; } })
->addColumn('wallet_details', function ($q) { if($q->plan) { $current_plan = $q->plan; if($q->is_word_limit=='yes'){ $words='Unlimited'; }else { $words = '<span class="text-muted">' . '<b>' . $current_plan->available_word . '</b>/' . '<small>' . $current_plan->word_limit . '</small>' . '</span>'; }
if($q->is_image_limit=='yes'){ $images='Unlimited'; }else { $images = '<span class="text-muted">' . '<b>' . $current_plan->available_image . '</b>/' . '<small>' . $current_plan->image_limit . '</small>' . '</span>'; }
if($q->is_code_limit=='yes'){ $codes='Unlimited'; }else { $codes = '<span class="text-muted">' . '<b>' . $current_plan->available_code . '</b>/' . '<small>' . $current_plan->code_limit . '</small>' . '</span>'; }
$wordLimit = '<p class="m-0 d-block">' . '<b>'.trans('admin.word_limit').'</b>' . ': ' .$words. '</p>'; $imageLimit = '<p class="m-0 d-block">' . '<b>'.trans('admin.image_limit') .'</b>'. ': ' . $images. '</p>'; $codeLimit = '<p class="m-0 d-block">' .'<b>'. trans('admin.code_limit').'</b>' . ': ' .$codes . '</p>';
return '<div class="d-block">' . $wordLimit . $imageLimit . $codeLimit . '</div>'; } })
->addColumn('status', function ($q) {
if($q->status=='Active'){ $status='<span class="badge bg-green custom-badge">Active</span>'; }else{ $status='<span class="badge bg-danger custom-badge">Banned</span>'; } return $status; })
->addColumn('action', function (Customer $q) {
if($q->status=='Active'){ $status=' <a class="dropdown-item" href="#" data-message="'.trans('admin.message.customer_status_change_to_banned_warn').'" data-action='.route('admin.customer.change.status',['id'=>$q->id,'status'=>'banned']).' data-input={"_method":"get"} data-toggle="modal" data-target="#modal-confirm">'.trans('admin.banned').'</a>'; }else{ $status='<a class="dropdown-item" href="#" data-message="'.trans('admin.message.customer_status_change_to_actv_warn').'" data-action='.route('admin.customer.change.status',['id'=>$q->id,'status'=>'active']).' data-input={"_method":"get"} data-toggle="modal" data-target="#modal-confirm">'.trans('admin.active').'</a>'; }
$actions='<div class="btn-group-vertical"> <div class="btn-group"> <i class="fa fa-ellipsis-h text-success" type="button" data-toggle="dropdown" aria-expanded="false"></i> <ul class="dropdown-menu" style=""> <li><a class="dropdown-item " href="'.route('admin.customers.edit', [$q->id]). '" >Edit</a></li> <li>'.$status.'</li> <li><a class="dropdown-item" href="#" data-message="Are you sure you want to delete ?" data-action='.route('admin.customers.destroy',[$q]).' data-input={"_method":"delete"} data-toggle="modal" data-target="#modal-confirm" >Delete</a> </li> <li><button class="dropdown-item" data-message="You will be logged in as customer?" data-action='.route('admin.customer.login.ass').' data-input={"id":'.$q->id.'} data-toggle="modal" data-target="#modal-confirm">Login As</button></li> </ul> </div> </div>';
return $actions;
}) ->rawColumns(['action','status','plan_details', 'profile','wallet_details']) ->toJson(); }
public function changeStatus(Request $request){ $request->validate([ 'status'=>'required|in:active,banned' ]);
$customer= Customer::where('id', $request->id)->firstOrFail();
$customer->status=$request->status; $customer->save(); return redirect()->back()->with('success', trans('admin.message.customer_status_change')); } public function create() { $data['plans'] = Plan::where('status', 'active')->get(); return view('admin.customers.create',$data); }
public function store(Request $request) { $request->validate([ 'first_name' => 'required', 'last_name' => 'required', 'email' => 'required|email|unique:customers', 'password' => 'required', 'status'=>'required|in:active,inactive', 'plan_id'=>'required|numeric', ]);
$request['email_verified_at']=now(); $plan = Plan::where('id',$request->plan_id)->firstOrFail(); $customer = auth()->user()->customers()->create($request->only('first_name', 'last_name', 'email', 'password', 'status', 'email_verified_at'));
$setting= new CustomerSettings(); $setting->customer_id = $customer->id; $setting->name = 'email_notification'; $setting->value = 'true'; $setting->save();
$expiredDate = null; if ($plan->recurring_type == 'weekly') { $expiredDate = now()->addWeek(); } else if ($plan->recurring_type == 'monthly') { $expiredDate = now()->addMonth(); } else if ($plan->recurring_type == 'yearly') { $expiredDate = now()->addYear(); } $customer->customer_plans()->create([ 'plan_id' => $plan->id, 'start_date' => now(), 'expired_date' => $expiredDate, 'price' => $plan->price, 'is_current' => 'yes', 'status' => 'approved', 'word_limit' => $plan->word_limit, 'available_word' => $plan->word_limit, 'is_word_limit'=> $plan->is_word_limit, 'code_limit' => $plan->code_limit, 'available_code' => $plan->code_limit, 'is_code_limit'=> $plan->is_code_limit, 'image_limit' => $plan->image_limit, 'available_image' => $plan->image_limit, 'is_image_limit'=> $plan->is_image_limit, ]); return redirect()->route('admin.customers.index')->with('success', 'Customer successfully created'); }
public function edit(Customer $customer) { $data['plans'] = Plan::where('status','active')->get(); $data['customer_plan'] = $customer->current_plan; $data['customer'] = $customer; return view('admin.customers.edit', $data); }
public function update(Customer $customer, Request $request) { $request->validate([ 'first_name' => 'required', 'last_name' => 'required', 'email' => 'required|email|unique:customers,email,' . $customer->id, 'status'=>'required|in:active,inactive', 'plan_id'=>'required|numeric', ]);
//Check for password availability if (!$request->password) unset($request['password']); $plan = Plan::findOrFail($request->plan_id); //update the model $customer->update($request->only('first_name', 'last_name', 'email', 'password', 'status', 'email_verified_at')); $customer->customer_plans()->update([ 'is_current' => 'no']);
$expiredDate = null; if ($plan->recurring_type == 'weekly') { $expiredDate = now()->addWeek(); } else if ($plan->recurring_type == 'monthly') { $expiredDate = now()->addMonth(); } else if ($plan->recurring_type == 'yearly') { $expiredDate = now()->addYear(); }
$customer->customer_plans()->create([ 'plan_id' => $plan->id, 'start_date' => now(), 'expired_date' => $expiredDate, 'price' => $plan->price, 'is_current' => 'yes', 'status' => 'approved', 'word_limit' => $plan->word_limit, 'available_word' => $plan->word_limit, 'is_word_limit'=> $plan->is_word_limit, 'code_limit' => $plan->code_limit, 'available_code' => $plan->code_limit, 'is_code_limit'=> $plan->is_code_limit, 'image_limit' => $plan->image_limit, 'available_image' => $plan->image_limit, 'is_image_limit'=> $plan->is_image_limit, ]); return redirect()->route('admin.customers.index')->with('success', 'Customer successfully updated'); }
public function changePlan(Request $request) { $request->validate([ 'id' => 'required', 'customer_id' => 'required', ]);
$customer = auth()->user()->customers()->where('id', $request->customer_id)->first();
if (!$customer) return back()->with('fail', 'Customer not found');
$plan = Plan::find($request->id);
if (!$plan) return back()->with('fail', 'Plan not found');
$pre_plan = $customer->customer_plans()->where('plan_id',$plan->id)->where('is_current','yes')->first();
if ($pre_plan) return back()->with('fail', 'This Plan is already assigned to this customer');
if (isset($request->plan_request_id)) {
if ($request->status=='rejected'){ $customerPlanRequest = CustomerPlan::find($request->plan_request_id);
if (!$customerPlanRequest) return back()->with('fail', 'Billing request not found'); $customerPlanRequest->status = $request->status; $customerPlanRequest->save(); return back()->with('success', 'Status successfully cancelled for the customer'); } if ($request->status=='approved') { $customer->current_plan()->update(['is_current'=>'no']); $recentPlan = CustomerPlan::where('id', $request->plan_request_id)->first(); CustomerPlan::where('id',$request->plan_request_id)->update(['is_current'=>'yes','status'=>'approved']); if ($recentPlan && $recentPlan->transaction_id) { $transaction = new Transaction(); $transaction->customer_id = $customer->id; $transaction->amount = $plan->price; $transaction->ref_id = $plan->id; $transaction->transaction_id = $recentPlan->transaction_id; $transaction->ref_type = 'plan'; $transaction->save(); }
} else return back()->with('fail', 'Invalid data for billing request'); }
$emailTemplate = get_email_template('plan_accepted'); if ($request->status=='approved' && $emailTemplate) { $regTemp = str_replace('{customer_name}', $customer->first_name.' '.$customer->last_name, $emailTemplate->body); SendMail::dispatch($customer->email, $emailTemplate->subject, $regTemp); }
return back()->with('success', 'Plan successfully updated for the customer'); }
public function loginAs(Request $request){ if(!$request->id) abort(404); auth('customer')->loginUsingId($request->id); return redirect()->route('customer.dashboard')->with('success',trans('You are now logged as customer')); } public function destroy(Customer $customer){ if(!$customer) abort(404); DB::beginTransaction(); try { $customer->plan()->delete(); $customer->settings()->delete(); $customer->documents()->delete(); $customer->codes()->delete(); $image = $customer->images()->get(); $imageID = $image->pluck('id'); ImageUrl::whereIn('image_id',$imageID)->delete(); $customer->images()->delete(); $customer->delete(); DB::commit(); return back()->with('success', 'Customer deleted successfully'); } catch (\Exception $ex) { DB::rollBack(); return back()->withErrors(['failed'=> $ex->getMessage()]); } } }
|