File: /home/posscale/www/printmanager/app/Http/Controllers/Tenant/CustomersController.php
<?php
namespace App\Http\Controllers\Tenant;
use App\Http\Controllers\Controller;
use App\Http\Requests\CustomerRequest;
use App\Models\Address;
use App\Models\Customer;
use App\Models\customers;
use App\Models\UserColumChoise;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class CustomersController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
abort_unless(auth()->user()?->can('manage_customers'), 403);
$search = $request->input('search');
$sort_by = $request->input('sort_by', 'name');
$sort = $request->input('sort', 'asc');
$column_choise = ["customer","primary_contact","number"];
$UserColumChoise = UserColumChoise::whereCustomer_id(auth()->user()->id)->whereNotNull('column_choise')->first();
if($UserColumChoise){
$column_choise = json_decode($UserColumChoise->column_choise);
}
$customers = Customer::whereAny([
'name',
'email',
'phone'
], 'LIKE', '%' . $search . '%')
->orderBy($sort_by,$sort)
->paginate(15);
return view('tenant.customers.list', ['customers' => $customers, 'search' => $search, 'sort_by' => $sort_by , 'sort' => $sort,'column_choise'=> $column_choise]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
abort_unless(auth()->user()?->can('manage_customers'), 403);
return view('tenant.customers.form', [ 'customer' => [], 'action' => 'Add Customer']);
}
/**
* Store a newly created resource in storage.
*/
public function store(CustomerRequest $request)
{
abort_unless(auth()->user()?->can('manage_customers'), 403);
$validated = $request->validated();
$customer = new Customer();
$customer->name = $validated['name'];
$customer->email = $validated['email'];
$customer->phone = $validated['phone'];
$customer->save();
return Redirect::route('customers.index')->with('message', 'Customer added successfully.');
}
/**
* Display the specified resource.
*/
public function show(Customer $customer)
{
// $jobs = $printer->jobs()->orderBy('created_at', 'desc')->simplePaginate(15);
return view('tenant.customers.show', ['customer' => $customer, 'addresses' => $customer->addresses()->get()]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Customer $customer)
{
abort_unless(auth()->user()?->can('manage_customers'), 403);
return view('tenant.customers.form', [ 'customer' => $customer->toArray(), 'action' => 'Edit Customer']);
}
/**
* Update the specified resource in storage.
*/
public function update(CustomerRequest $request, Customer $customer)
{
abort_unless(auth()->user()?->can('manage_customers'), 403);
$validated = $request->validated();
$customer->name = $validated['name'];
$customer->email = $validated['email'];
$customer->phone = $validated['phone'];
$customer->save();
return Redirect::route('customers.index')->with('message', 'Customer updated successfully.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Customer $customer)
{
abort_unless(auth()->user()?->can('manage_customers'), 403);
$customer->delete();
return Redirect::route('customers.index')->with('message', 'Customer delete successfully.');
}
public function user_column_choise(Request $request)
{
abort_unless(auth()->user()?->can('manage_customers'), 403);
$array_value=[];
$array_value['customer_id'] = auth()->user()->id;
$array_value['user_type']='customer';
$array_value['column_choise']=($request->user_column_choise)?json_encode($request->user_column_choise):null;
$flight = UserColumChoise::updateOrCreate(['customer_id' => $array_value['customer_id']],$array_value);
return Redirect::route('customers.index')->with('message', 'Customer column updated successfully.');
}
}