Viewing file: CustomerController.php (7.13 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers\Admin;
use App\Events\SendEMail; use App\Http\Controllers\Controller; use App\Models\BillingRequest; use App\Models\Customer; use App\Models\CustomerSettings; use App\Models\Plan; 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::where('id','!=', 1)->select(['id', 'first_name', 'last_name', 'email', 'status', 'created_at','email_verified_at']);
return datatables()->of($customers) ->addColumn('full_name', function ($q) { return $q->full_name; }) ->addColumn('action', function (Customer $q) { $verify=''; if (!$q->email_verified_at){ $verify ="<a class='btn btn-sm btn-info' href='" . route('admin.customer.verified.user', ['id'=>$q->id]) . "'>Verify</a> "; } return "<a class='btn btn-sm btn-info' href='" . route('admin.customers.edit', [$q->id]) . "'>Edit</a> ".$verify. '<button class="btn btn-sm btn-primary" data-message="You will be logged in as customer?" data-action='.route('admin.customer.login.as').' data-input={"id":'.$q->id.'} data-toggle="modal" data-target="#modal-confirm">Login As</button>'; }) ->rawColumns(['action']) ->toJson(); }
public function verified(Request $request){ $customer=Customer::where('id', $request->id)->firstOrFail();
$customer->email_verified_at=now(); $customer->status='active'; $customer->save();
return redirect()->back()->with('success', trans('customer.verified')); }
public function create() { $data['plans'] = Plan::where('status','active')->get(); return view('admin.customers.create',$data); }
public function store(Request $request) { if (config("app.demo")){ return redirect()->back()->withErrors(['message' => trans('admin.app_demo_message')]); }
$request->validate([ 'first_name' => 'required|max:191', 'last_name' => 'required|max:191', 'email' => 'required|unique:customers|max:191', 'password' => 'required|max:191', 'status' => 'required', 'plan' => 'required|numeric' ]); $plan = Plan::where('id',$request->plan)->first(); $request['email_verified_at']=now();
$customer=auth()->user()->customers()->create($request->all());
//Assigning plan to customer
$customer->plan()->create(['plan_id' => $plan->id, 'email_limit' => $plan->email_limit, 'available_emails' => $plan->email_limit, 'price' => $plan->price,'contact_limit'=>$plan->contact_limit]);
return back()->with('success', 'Customer successfully created'); }
public function edit(Customer $customer) { $data['customer'] = $customer; $data['activePlans'] = auth()->user()->active_plans; $data['plans'] = Plan::where('status','active')->get(); return view('admin.customers.edit', $data); }
public function update(Customer $customer, Request $request) { if (config("app.demo")){ return redirect()->back()->withErrors(['message' => trans('admin.app_demo_message')]); } $request->validate([ 'first_name' => 'required|max:191', 'last_name' => 'required|max:191', 'email' => 'required|max:191|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 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->plan;
if(isset($pre_plan->plan_id)){ $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');
$billingRequest->status = $request->status; $billingRequest->save();
if ($request->status == 'rejected') 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'); $mail_from=get_settings('mail_from'); $mail_name=get_settings('mail_name'); if ($request->status=='accepted' && $emailTemplate && config('mail.from.address') && config('mail.from.name')) { $regTemp = str_replace('{customer_name}', $customer->first_name.' '.$customer->last_name, $emailTemplate->body); $config=new \stdClass(); $config->value=json_encode([ 'username'=>config('mail.username'), 'password'=>config('mail.password'), 'hostname'=>config('mail.host'), 'port'=>config('mail.port'), ]); SendEMail::dispatch('smtp',config('mail.from.address'),config('mail.from.name'),$customer->email,$emailTemplate->title,$regTemp,config('mail.from.address'),null,$config); }
//delete previous plan //TODO: suggestion: might need to change plan status in future without deleting plan if ($pre_plan) { $customer->plan()->delete(); } $customer->plan()->create(['plan_id' => $plan->id, 'email_limit' => $plan->email_limit,'available_emails'=>$plan->email_limit, 'price' => $plan->price, 'contact_limit'=>$plan->contact_limit, 'contact_limit'=>$plan->contact_limit]);
// TODO:: send email here
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')); }
}
|