Viewing file: CustomerController.php (29.58 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\BillingRequest; use App\Models\Customer; use App\Models\CustomerMealPlan; use App\Models\CustomerMealPlanDetail; use App\Models\CustomerMealPlanTask; use App\Models\CustomerSettings; use App\Models\CustomerStep; use App\Models\CustomerStepDetail; use App\Models\CustomerWorkoutPlan; use App\Models\CustomerWorkoutPlanSection; use App\Models\CustomerWorkOutPlanTask; use App\Models\MealPlan; use App\Models\Plan; use App\Models\User; use App\Models\Weight; use App\Models\WorkOutPlan; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB;
class CustomerController extends Controller { public function index() { $data['trainers'] = User::where('type','trainer')->get(); return view('admin.customers.index',$data); }
public function reseller() {
return view('admin.customers.reseller'); } public function masterReseller() {
return view('admin.customers.master_reseller'); } public function getAll(Request $request) { $auth = auth()->user(); if($auth->type=='trainer'){
$customers = Customer::where('added_by', $auth->id)->orderByDesc('created_at'); }else{ $customers = Customer::orderByDesc('created_at'); }
return datatables()->of($customers) ->addColumn('profile', function ($q) {
$name = '<h6>' . $q->full_name . '</h6>'; $email = '<h6>' . $q->email . '</h6>'; return '<div>' . $name . $email . '</div>'; }) ->addColumn('plan_details', function ($q) { $meal_plan = ''; $workout_plan = ''; $live_support = ''; $details = ''; if (isset($q->plan->meal_plan) && $q->plan->meal_plan == 'yes') { $meal_plan = '<span class="badge badge-success p-1">Meal Plan</span>'; } if (isset($q->plan->workout_plan) && $q->plan->workout_plan == 'yes') { $workout_plan = '<span class="badge badge-success p-1">Workout Plan</span>'; } if (isset($q->plan->live_support) && $q->plan->live_support == 'yes') { $live_support = '<span class="badge badge-success p-1">Live Support</span>'; } $details = "<div>" . $meal_plan . ' ' . $workout_plan . ' ' . $live_support . "</div>"; return $details; })
->addColumn('assign_to', function ($q) { $assign_to = ''; if (auth()->user()->type == 'admin') { $assign_to = '<span class="badge badge-success p-1">'.$q->user->name.'</span>'; }
return '<div>' . $assign_to . '</div>'; })
->addColumn('status', function (Customer $q) { if ($q->status == 'Active') { $status = '<strong class="text-white bg-success px-2 py-1 rounded status-font-size"> ' . ucfirst($q->status) . ' </strong>'; } else { $status = '<strong class="text-white bg-danger px-2 py-1 rounded status-font-size"> ' . ucfirst($q->status) . ' </strong>'; } return $status; })
->addColumn('is_verified', function (Customer $q) { $status = '';
return $status; }) ->addColumn('action', function (Customer $q) { $edit = ''; $loginAs = ''; $meal_plan = ''; $workout_plan = ''; $details_btn = ''; $assign_trainer = ''; $weight_details_btn = ''; if (auth()->user()->type == 'admin') { $edit = '<a class="dropdown-item" href="' . route('admin.customers.edit', [$q->id]) . '">Edit</a>'; $loginAs = '<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>'; if(!$q->added_by){ $assign_trainer = '<a class="dropdown-item assign-trainer" href="#" data-id="'.$q->id.'">Assign Trainer</a>'; }
} else { $current_plan = $q->plan;
if ($current_plan || $current_plan->workout_plan || $current_plan->workout_plan == 'yes') { $workout_plan = '<a class="dropdown-item" href="' . route('admin.customer.assign.workout.plan', [$q->id]) . '">Assign Workout Plan</a>'; }
if ($current_plan || $current_plan->meal_plan || $current_plan->meal_plan == 'yes') { $meal_plan = '<a class="dropdown-item" href="' . route('admin.customer.assign.meal.plan', [$q->id]) . '">Assign Meal Plan</a>'; }
$details_btn = '<a class="dropdown-item" href="' . route('admin.customer.steps', [$q->id]) . '">Step Details</a>'; $weight_details_btn = '<a class="dropdown-item" href="' . route('admin.customer.weight.details', [$q->id]) . '">Weight Details</a>'; }
$btn = '<div class="btn-group"> <button type="button" class="btn btn-success rounded" data-toggle="dropdown" aria-expanded="false"> <i class="fas fa-ellipsis-v"></i> </button> <div class="dropdown-menu" role="menu" style="">' . $meal_plan . $workout_plan . $details_btn . $edit . $loginAs . $assign_trainer . $weight_details_btn . '</div> </div>'; return $btn; }) ->rawColumns(['action', 'status', 'plan_details', 'is_verified', 'profile','assign_to']) ->toJson(); }
public function customerSteps($id) { $data['customer_steps'] = CustomerStep::where('customer_id', $id)->get();
return view('admin.customers.steps', $data); }
public function customerStepDetail($id) { $data['customer_step_details'] = CustomerStepDetail::where('customer_step_id', $id)->get(); $customer_step = CustomerStep::findOrFail($id); $customer = Customer::findOrFail($customer_step->customer_id);
$data['current_plan'] = $customer->plan;
return view('admin.customers.customer_step_detail', $data); }
public function customerWeightDetail($id) {
$data['customer_weight_details'] = Weight::where('customer_id', $id)->get(); $data['customer_info'] = Customer::findOrFail($id);
return view('admin.customers.customer_weight_detail', $data); }
public function assign_meal_plan($id) { $customer = Customer::findOrFail($id); $current_plan = $customer->plan;
if (!$current_plan || !$current_plan->meal_plan || $current_plan->meal_plan == 'no') { return abort('404'); } if ($customer->current_meal_plan) { $data['customer_meal_plan'] =$customer->current_meal_plan; }
$data['meal_plans'] = MealPlan::where('status', 'active')->get(); $data['customer'] = $customer;
return view('admin.customers.meal_plan', $data);
}
public function assign_workout_plan($id) {
$customer = Customer::findOrFail($id); $current_plan = $customer->plan;
if (!$current_plan || !$current_plan->workout_plan || $current_plan->workout_plan == 'no') { return abort('404'); } if ($customer->current_workout_plan) { $data['customer_workout_plan'] = $customer->current_workout_plan()->with('customer_workout_sections')->first(); }
$data['workout_plans'] = WorkOutPlan::where('status', 'active')->get(); $data['customer'] = $customer;
return view('admin.customers.workout_plan', $data);
}
public function change_meal_plan(Request $request) { $customer = Customer::findOrFail($request->customer_id); $meal_plan = MealPlan::findOrFail($request->plan_id); DB::beginTransaction(); try { $customer->meal_plans()->update(['is_current' => 'no']);
$new_meal_plan = new CustomerMealPlan(); $new_meal_plan->customer_id = $customer->id; $new_meal_plan->plan_id = $meal_plan->id; $new_meal_plan->is_current = 'yes'; $new_meal_plan->save();
if ($meal_plan->plan_details) {
foreach ($meal_plan->plan_details as $plan_detail) {
$new_detail = new CustomerMealPlanDetail(); $new_detail->plan_id = $new_meal_plan->id; $new_detail->day = $plan_detail->day; $new_detail->customer_id = $customer->id; $new_detail->save();
foreach($plan_detail->detail_tasks as $detail_task){
$meal_task=new CustomerMealPlanTask(); $meal_task->customer_id=$customer->id; $meal_task->detail_id=$new_detail->id; $meal_task->type=$detail_task->type; $meal_task->task=$detail_task->task->name; $meal_task->save(); } } }
DB::commit(); return redirect()->back()->with('success', 'Meal plan successfully assigned'); } catch (\Exception $ex) { DB::rollBack();
return redirect()->back()->withErrors(['failed' => $ex->getMessage()]); } }
public function change_workout_plan(Request $request) { DB::beginTransaction();
try { $customer = Customer::findOrFail($request->customer_id); $workout_plan = WorkOutPlan::with('sections','sections.tasks')->findOrFail($request->plan_id);
$customer->workout_plans()->update(['is_current' => 'no']);
$new_workout_plan = new CustomerWorkoutPlan(); $new_workout_plan->customer_id = $customer->id; $new_workout_plan->plan_id = $workout_plan->id; $new_workout_plan->is_current = 'yes'; $new_workout_plan->save();
if($workout_plan->sections) { foreach ($workout_plan->sections as $section) { $new_section = new CustomerWorkoutPlanSection(); $new_section->title = $section->title; $new_section->plan_id = $new_workout_plan->id; $new_section->save();
if($section->tasks) { foreach ($section->tasks as $task) { $new_cus_task = new CustomerWorkOutPlanTask(); $new_cus_task->section_id = $new_section->id; $new_cus_task->customer_id = $customer->id; $new_cus_task->task = $task->task; $new_cus_task->repetation_and_set = $task->repetation_and_set; $new_cus_task->rest_time = $task->rest_time; $new_cus_task->note = $task->note; $new_cus_task->save(); } } } }
DB::commit(); return redirect()->back()->with('success', 'Workout plan successfully assigned'); }catch(\Exception $ex){ DB::rollBack();
return redirect()->back()->withErrors(['failed'=>$ex->getMessage()]); }
} public function change_plan(Request $request) { $customer = Customer::findOrFail($request->id);
$plan = Plan::where('id', $request->plan_id)->firstOrFail(); if ($plan->recurring_type == 'weekly') { $time = \Illuminate\Support\Carbon::now()->addWeek(); } else if ($plan->recurring_type == 'monthly') { $time = Carbon::now()->addMonth(); } else if ($plan->recurring_type == 'yearly') { $time = Carbon::now()->addYear(); } else if ($plan->recurring_type == 'custom') { $date = json_decode($plan->custom_date); $time = isset($date->from) ? new \DateTime($date->from) : ''; }
$customer->customer_plans()->update(['is_current' => 'no']); $customer->plan()->create([ 'is_current' => 'yes', 'price' => $plan->price, 'expire_date' => $time, 'plan_id' => $plan->id, 'meal_plan' => $plan->meal_plan, 'workout_plan' => $plan->workout_plan, 'live_support' => $plan->live_support ]);
return redirect()->back()->with('success', 'Plan successfully changed'); }
public function getCustomerInfo(Request $request) {
$customer = Customer::where('id', $request->id)->first(); if (!$customer) { return response()->json(['message' => 'Invalid customer']); } $wallet = $customer->wallet;
$data = [ 'credit' => $wallet->credit, ];
return response()->json(['data' => $data]); }
public function editCustomerPLan(Customer $customer) { $currPLan = $customer->plan()->first(); $data['current_plan'] = $currPLan; return view('admin.customers.edit_plan', $data); }
public function updateCustomerPLan($customer, Request $request) { $customer = Customer::findOrFail($customer); $currPLan = $customer->plan()->first();
if ($request->sms_unit_price) { $price = $request->sms_unit_price; } else { $price = 0; }
$currPLan->sms_unit_price = $price; $currPLan->save(); cache()->forget('current_plan_' . $customer->id);
if ($customer->type == 'reseller') { return redirect()->route('admin.reseller')->with('success', trans('admin.message.updated_customer_plan')); } else { return redirect()->route('admin.customers.index')->with('success', trans('admin.message.updated_customer_plan')); } } public function create() { $auth = auth()->user(); if($auth->type=='admin'){ $data['plans'] = Plan::orderByDesc('created_at')->where('status', 'active')->get(); return view('admin.customers.create', $data); }else{ return abort('404'); }
}
public function store(Request $request) { if(auth()->user()->type !=='admin'){ return abort('404'); }else{ if (env("APP_DEMO")) { return redirect()->back()->withErrors(['msg' => trans('admin.app_demo_message')]); } DB::beginTransaction();
try { $request->validate([ 'first_name' => 'required', 'last_name' => 'required', 'email' => 'required|unique:customers', 'password' => 'required', 'status' => 'required', 'plan_id' => 'required', ]);
$request['email_verified_at'] = now();
$customer = auth()->user()->customers()->create($request->all());
$setting = new CustomerSettings(); $setting->customer_id = $customer->id; $setting->name = 'email_notification'; $setting->value = 'true'; $setting->save();
// For Payment Gateway $setting = new CustomerSettings(); $setting->customer_id = $customer->id; $setting->name = 'payment_gateway'; $setting->value = json_encode($request->payment_gateway); $setting->save();
//Assigning plan to customer $plan = Plan::where('id', $request->plan_id)->firstOrFail(); if ($plan->recurring_type == 'weekly') { $time = \Illuminate\Support\Carbon::now()->addWeek(); } else if ($plan->recurring_type == 'monthly') { $time = Carbon::now()->addMonth(); } else if ($plan->recurring_type == 'yearly') { $time = Carbon::now()->addYear(); } else if ($plan->recurring_type == 'custom') { $date = json_decode($plan->custom_date); $time = isset($date->from) ? new \DateTime($date->from) : ''; }
$customer->plan()->create([ 'is_current' => 'yes', 'price' => $plan->price, 'expire_date' => $time, 'plan_id' => $plan->id, 'meal_plan' => $plan->meal_plan, 'workout_plan' => $plan->workout_plan, 'live_support' => $plan->live_support ]);
DB::commit(); return back()->with('success', 'Customer successfully created'); } catch (\Throwable $ex) { DB::rollback(); return redirect()->back()->withErrors(['fail' => $ex->getMessage()]); } }
}
public function edit(Customer $customer) { if(auth()->user()->type !=='admin'){ return abort('404'); }else{ $data['customer'] = $customer;
$data['plans'] = Plan::orderByDesc('created_at')->where('status', 'active')->get();
return view('admin.customers.edit', $data); } }
public function update(Customer $customer, Request $request) {
if (env("APP_DEMO")) { return redirect()->back()->withErrors(['msg' => trans('admin.app_demo_message')]); }
$request->validate([ 'first_name' => 'required', 'last_name' => 'required', 'email' => 'required|unique:customers,email,' . $customer->id, 'status' => 'required', ]);
//Check for password availability if (!$request->password) unset($request['password']);
//update the model $customer->update($request->all());
return back()->with('success', 'Customer successfully updated'); }
public function assignNumber(Request $request) { $this->validate($request, [ 'id' => 'required', 'customer_id' => 'required|exists:customers,id', ]);
$customer = auth()->user()->customers()->where('id', $request->customer_id)->first(); if (!$customer) return back()->with('fail', 'Customer not found');
$number = Number::find($request->id); if (!$number) return back()->with('fail', 'Number not found');
$isAssigned = $customer->numbers()->where('number_id', $number->id)->first(); if ($isAssigned) return back()->with('fail', 'Number already assigned to this customer');
$time = Carbon::now()->addMonths(1);
$numb = $customer->numbers()->create([ 'number_id' => $number->id, 'dynamic_gateway_id' => $number->dynamic_gateway_id, 'number' => $number->number, 'expire_date' => $time, 'cost' => $number->sell_price, 'sms_capability' => $number->sms_capability, 'mms_capability' => $number->mms_capability, 'voice_capability' => $number->voice_capability, 'whatsapp_capability' => $number->whatsapp_capability ]);
return back()->with('success', 'Number successfully added to the customer'); }
public function removeNumber(Request $request) { $this->validate($request, [ 'id' => 'required', 'customer_id' => 'required|exists:customers,id', ]);
$customer = auth()->user()->customers()->where('id', $request->customer_id)->first(); if (!$customer) return back()->with('fail', 'Customer not found');
$number = Number::find($request->id); if (!$number) return back()->with('fail', 'Number not found');
$isAssigned = $customer->numbers()->where('number_id', $number->id)->first(); if (!$isAssigned) return back()->with('fail', 'Number haven\'t assigned to this customer');
$numberRequest = NumberRequest::where('customer_id', $customer->id)->where('number_id', $isAssigned->id)->first();
if ($numberRequest) { $numberRequest->delete(); } $keyword = Keyword::where('customer_number_id', $isAssigned->id)->first(); KeywordContact::where('keyword_id', $keyword->id)->delete(); Keyword::where('customer_number_id', $isAssigned->id)->delete(); $isAssigned->delete();
return back()->with('success', 'Number successfully removed from the customer'); }
public function changePlan(Request $request) { DB::beginTransaction(); try { $request->validate([ 'id' => 'required', 'customer_id' => 'required', ]);
$customer = Customer::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->plan; if ($pre_plan) { $isAssigned = $pre_plan->plan_id == $plan->id; if ($isAssigned) return back()->with('fail', 'This Plan is already assigned to this customer'); }
if (isset($request->from)) {
if ($request->from == 'request' && $request->billing_id && in_array($request->status, ['accepted', 'rejected'])) { $billingRequest = BillingRequest::find($request->billing_id); if (!$billingRequest) return back()->with('fail', 'Billing request not found');
// if ($request->status == 'accepted') { // $transactions = Transactions::where('ref_id', $billingRequest->id)->where('type', 'plan')->first(); // if ($transactions) { // $transactions->status = 'paid'; // $transactions->save(); // } // }
$billingRequest->status = $request->status; $billingRequest->save();
if ($request->status == 'rejected'){
DB::commit();
return back()->with('success', 'Status successfully cancelled for the customer');
}
} else return back()->with('fail', 'Invalid data for billing request'); }
$emailTemplate = get_email_template('plan_accepted'); if ($request->status == 'accepted' && $emailTemplate) { $regTemp = str_replace('{customer_name}', $customer->first_name . ' ' . $customer->last_name, $emailTemplate->body); SendMail::dispatch($customer->email, $emailTemplate->subject, $regTemp); }
//delete previous plan //TODO: suggestion: might need to change plan status in future without deleting plan if ($pre_plan) { $customer->plan()->update(['is_current' => 'no']); } if ($plan->recurring_type == 'weekly') { $time = \Illuminate\Support\Carbon::now()->addWeek(); } else if ($plan->recurring_type == 'monthly') { $time = Carbon::now()->addMonth(); } else if ($plan->recurring_type == 'yearly') { $time = Carbon::now()->addYear(); } else if ($plan->recurring_type == 'custom') { $date = json_decode($plan->custom_date); $time = isset($date->from) ? new \DateTime($date->from) : ''; }
$customer->plan()->create([ 'is_current' => 'yes', 'price' => $plan->price, 'expire_date' => $time, 'plan_id' => $plan->id, 'sms_sending_limit' => $plan->sms_sending_limit, 'max_contact' => $plan->max_contact, 'contact_group_limit' => $plan->contact_group_limit, 'sms_unit_price' => $plan->sms_unit_price, 'free_sms_credit' => $plan->free_sms_credit, 'country' => $plan->country, 'api_availability' => $plan->api_availability, 'sender_id_verification' => $plan->sender_id_verification, 'unlimited_sms_send' => $plan->unlimited_sms_send, 'unlimited_contact' => $plan->unlimited_contact, 'unlimited_contact_group' => $plan->unlimited_contact_group ]);
cache()->forget('current_plan_' . $customer->id);
cache()->forget('wallet_' . $customer->id);
DB::commit(); return back()->with('success', 'Plan successfully updated for the customer'); } catch (\Exception $ex) { DB::rollBack(); return redirect()->back()->withErrors(['failed' => $ex->getMessage()]); } }
public function subtract(Request $request) {
DB::beginTransaction();
try { $customer = Customer::where('id', $request->customer_id)->firstOrFail();
$current_plan = $customer->plan;
$unit_price = $current_plan->sms_unit_price ? $current_plan->sms_unit_price : 0;
$wallet = $customer->wallet;
if (isset($request->select_type) && $request->select_type == 'add') { if (isset($request->credit) && $request->credit > 0) { $wallet->credit = $wallet->credit + $request->credit; $wallet->save();
} cache()->forget('wallet_' . $customer->id);
//Transaction Report $transaction = new Transactions(); $transaction->added_by = $customer->type; $transaction->customer_id = $customer->id; $transaction->type = 'top_up'; $transaction->amount = $request->credit * $unit_price; $transaction->status = 'paid'; $transaction->save();
DB::commit(); return redirect()->back()->with('success', 'Credit Successfully Added'); }
if (isset($request->select_type) && $request->select_type == 'subtract') { if (isset($request->pre_credit) && $request->pre_credit > 0 && $wallet->credit >= $request->pre_credit) { $wallet->credit = $wallet->credit - $request->pre_credit; $wallet->save();
$transaction = new Transactions(); $transaction->added_by = $customer->type; $transaction->customer_id = $customer->id; $transaction->type = 'subtract'; $transaction->amount = $request->pre_credit * $unit_price; $transaction->status = 'paid'; $transaction->ref_id = $wallet->id; $transaction->save();
} cache()->forget('wallet_' . $customer->id); DB::commit(); return redirect()->back()->with('success', 'Credit Successfully Subtracted'); }
} catch (\Exception $ex) { DB::rollBack(); return redirect()->back()->withErrors(['failed' => $ex->getMessage()]); } }
public function assignSenderId() {
$data['availableSMSGateway'] = DynamicGateway::where('status', 'active')->orderByDesc('created_at')->get(); $data['customers'] = Customer::where('status', 'active')->get();
return view('admin.customers.assign_senderid', $data); }
public function saveAssignSenderId(Request $request) { $request->validate([ 'sender_id' => 'required|unique:sender_ids,sender_id', ]); $customer = Customer::where('id', $request->customer_id)->firstOrFail();
$request['status'] = 'approved'; $request['is_paid'] = 'yes'; $request['dynamic_gateway_id'] = $request->dynamic_gateway_id;
$customer->sender_ids()->create($request->only('sender_id', 'dynamic_gateway_id', 'expire_date', 'from', 'status', 'is_paid'));
return redirect()->back()->with('success', 'SenderID Successfully Assigned'); }
public function loginAs(Request $request) { if (!$request->id) abort(404); auth('customer')->loginUsingId($request->id); cache()->flush(); return redirect()->route('customer.dashboard')->with('success', trans('You are now logged as customer')); }
public function assign_trainer(Request $request){ $customer = Customer::where('id', $request->customer_id)->firstOrFail(); $auth = auth()->user(); if($auth->type!='admin'){ return abort('404'); } $user = User::where('id', $request->added_by)->firstOrFail(); $customer->added_by = $user->id; $customer->save(); cache()->flush(); return redirect()->back()->with('success', 'Trainer Successfully Assigned');
}
}
|