Viewing file: CustomerController.php (8.68 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\Admin\Tenant; use App\Models\Admin\TenantPlan; use App\Models\Plan; use App\Models\Setting; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str;
class CustomerController extends Controller { public function index() { return view('admin.customers.index'); }
public function getAll() { $customers = Tenant::with('currentPlan')->get(); return datatables()->of($customers) ->addColumn('action', function ($q) { return '<div class="btn-group"> <button type="button" data-toggle="dropdown" class="border-0"> <i class="fas fa-ellipsis-h"></i> </button> <div class="dropdown-menu"> <a class="dropdown-item" href="' . route('admin.customers.edit', [$q->id]) . '">Edit</a> <a data-message="Are you sure you want to delete this Store?" data-action=' . route('admin.customers.destroy', [$q]) . ' data-input={"_method":"delete"} data-toggle="modal" data-target="#modal-confirm" data-toggle="tooltip" data-placement="top" title="Delete" class="dropdown-item" href="#">Delete</a> </div> </div>'; }) ->addColumn('status', function ($q) { if ($q->status == 'pending') { return '<span class="pl-2 pr-2 pt-1 pb-1 bg-warning" style="border-radius:25px;color:white !important;">' . $q->status . '</span>'; } elseif ($q->status == 'accepted') { return '<span class="pl-2 pr-2 pt-1 pb-1 bg-success" style="border-radius:25px;">' . $q->status . '</span>'; } elseif ($q->status == 'rejected') { return '<span class="pl-2 pr-2 pt-1 pb-1 bg-danger" style="border-radius:25px;">' . $q->status . '</span>'; } elseif ($q->status == 'blocked') { return '<span class="pl-2 pr-2 pt-1 pb-1 bg-danger" style="border-radius:25px;">' . $q->status . '</span>'; } }) ->addColumn('store_name', function ($q) { return $q->store_name; }) ->addColumn('plan', function ($q) { return $q->currentPlan->plan->title; }) ->addColumn('sms_limit', function ($q) { return $q->currentPlan->sms_limit; }) ->addColumn('cost', function ($q) { return $q->currentPlan->cost; }) ->addColumn('expired_at', function ($q) { return $q->currentPlan->expired_date; }) ->rawColumns(['status', 'action','plan']) ->toJson(); }
public function getSubDomain($storeName) { $username = Str::slug($storeName, ''); $userRows = DB::table('domains')->select('id')->where("domain", "REGEXP", "^$username([0-9]*)?")->get(); $countUser = count($userRows) + 1; return ($countUser > 1) ? "{$username}{$countUser}" : $username; }
public function create() { $data['plans'] = Plan::active()->get(); return view('admin.customers.create', $data); }
public function store(Request $request) {
$request->validate([ 'store_name' => 'required', 'email' => 'required|email', 'password' => 'required', 'plan_id' => 'required', ]); $plan = Plan::findOrFail($request->plan_id); $host = parse_url(env('APP_URL'))['host'];
$tenant = Tenant::create([ 'plan' => '' . $plan->id, 'store_name' => $request->store_name, 'email' => $request->email, ]); $expired_date = null; if ($plan->recurring_type == 'monthly') { $expired_date = now()->addMonth(); } elseif ($plan->recurring_type == 'semiyearly') { $expired_date = now()->addMonths(6); } elseif ($plan->recurring_type == 'yearly') { $expired_date = now()->addYear(); } $tenantPlan = new TenantPlan(); $tenantPlan->tenant_id = $tenant->id; $tenantPlan->plan_id = $plan->id; $tenantPlan->start_date = now(); $tenantPlan->expired_date = $expired_date; $tenantPlan->cost = $plan->cost; $tenantPlan->sms_limit = $plan->sms_limit; $tenantPlan->payment_status = 'unpaid'; $tenantPlan->save();
$subDomain = $this->getSubDomain($request->store_name); $domain = "{$subDomain}.{$host}"; $tenant->domains()->create([ 'domain' => $domain, ]); $request['sub_domain'] = $subDomain;
$tenant->run(function (Tenant $t) use ($request) { $storage_path = storage_path(); mkdir("$storage_path/framework/cache", 0777, true); Artisan::call("tenants:seed --tenants=" . $t->id); Artisan::call("passport:keys"); $storeName = explode(' ', strtolower($request->store_name)); $firstName = $storeName[0]; unset($storeName[0]); $lastName = implode(' ', $storeName); DB::statement("SET FOREIGN_KEY_CHECKS=0;"); User::truncate(); User::insert( array( 'id' => 1, 'firstname' => $firstName, 'lastname' => $lastName, 'username' => $request->sub_domain, 'email' => $request->email, 'password' => bcrypt($request->password), 'avatar' => 'no_avatar.png', 'phone' => '', 'role_id' => 1, 'statut' => 1, 'is_all_warehouses' => 1, ) ); Setting::where('id', 1)->update(['tenant_id' => $t->id]); DB::statement("SET FOREIGN_KEY_CHECKS=1;"); });
return redirect()->back()->with('success', 'Store created successfully'); }
public function edit($id) { $data['store'] = Tenant::findOrFail($id); $data['plans'] = Plan::active()->get(); return view('admin.customers.edit', $data); }
public function update($id, Request $request) { $request->validate([ 'store_name' => 'required', 'email' => 'required|email', 'plan_id' => 'required', 'payment_status' => 'required|in:paid,unpaid', 'status' => 'required|in:pending,accepted,rejected,blocked,expired', ]);
$plan = Plan::findOrFail($request->plan_id);
$tenant = Tenant::findOrFail($id); $tenant->email = $request->email; $tenant->plan = "" . $plan->id; $tenant->store_name = $request->store_name; $tenant->status = $request->status; $tenant->save();
$expired_date = $tenant->currentPlan->expired_date; if ($tenant->plan_id != $request->plan_id) { if ($plan->recurring_type == 'monthly') { $expired_date = now()->addMonth(); } elseif ($plan->recurring_type == 'semiyearly') { $expired_date = now()->addMonths(6); } elseif ($plan->recurring_type == 'yearly') { $expired_date = now()->addYear(); } }
$tenantPlan = $tenant->currentPlan; $tenantPlan->plan_id = $plan->id; $tenantPlan->start_date = $tenant->plan_id == $request->plan_id ? $tenant->currentPlan->start_date : now(); $tenantPlan->expired_date = $expired_date; $tenantPlan->cost = $plan->cost; $tenantPlan->sms_limit = $plan->sms_limit; $tenantPlan->payment_status = $request->payment_status; $tenantPlan->save();
$tenant->run(function (Tenant $t) use ($request) { $storeName = explode(' ', strtolower($request->store_name)); $firstName = $storeName[0]; unset($storeName[0]); $lastName = implode(' ', $storeName); $user = User::first(); $user->firstname = $firstName; $user->lastname = $lastName; $user->email = $request->email; if ($request->password) { $user->password = bcrypt($request->password); } $user->save(); });
return redirect()->back()->with('success', 'Store updated successfully');
}
public function destroy() {
} }
|