File manager - Edit - /var/www/payraty/inventory_main/app/Http/Controllers/Admin/Customer/CustomersController.php
Back
<?php namespace App\Http\Controllers\Admin\Customer; use App\DataTables\CustomerDataTable; use App\Models\Customer; use App\Models\AccountingCustomers; use App\Models\Invoice; use App\Models\SystemCountry; use App\Http\Controllers\Controller; use App\Http\Requests\CustomerRequest; use App\Services\Customer\CustomerService; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use Illuminate\Http\Request; use PDF; use Excel; use App\Exports\CustomersExport; use Illuminate\Support\Facades\DB; class CustomersController extends Controller { protected $customerService; /** * __construct * * @param mixed $customerService * @return void */ public function __construct(CustomerService $customerService) { $this->customerService = $customerService; $this->middleware(['permission:List Customer'])->only(['index']); $this->middleware(['permission:Add Customer'])->only(['create']); $this->middleware(['permission:Edit Customer'])->only(['edit']); $this->middleware(['permission:Delete Customer'])->only(['destroy']); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(CustomerDataTable $dataTable) { set_page_meta(__('custom.customers')); $user = Auth::user(); $orgId = $user->organisation_id; $customers = Customer::where('organisation_id', $orgId)->paginate(10); // return $dataTable->render('admin.customers.index'); $profile = DB::table('profiles')->where('user_id', $user->id)->first(); return view('admin.customers.index', compact('customers', 'profile')); } public function filter(Request $request) { $user = Auth::user(); $orgId = $user->organisation_id; $period = $request->get('period', 'all'); $query = Customer::where('organisation_id', $orgId); switch ($period) { case 'monthly': $query->whereMonth('created_at', now()->month); break; case 'yearly': $query->whereYear('created_at', now()->year); break; // 'all' case doesn't need additional filtering } $customers = $query->paginate(10); if ($request->ajax()) { return response()->json([ 'customers' => $customers ]); } // Fallback for non-AJAX requests return redirect()->route('admin.customers.index'); } public function search(Request $request) { $user = Auth::user(); $orgId = $user->organisation_id; $searchTerm = $request->get('q'); $customers = Customer::where('organisation_id', $orgId) ->where(function ($query) use ($searchTerm) { $query->where('first_name', 'LIKE', "%{$searchTerm}%") ->orWhere('first_name', 'LIKE', "%{$searchTerm}%") ->orWhere('email', 'LIKE', "%{$searchTerm}%") ->orWhere('phone', 'LIKE', "%{$searchTerm}%"); }) ->paginate(10); if ($request->ajax()) { return response()->json([ 'customers' => $customers ]); } // Fallback for non-AJAX requests return view('admin.customers.index', compact('customers')); } /** * exportCustomers * * @param mixed $request * @return void */ public function exportCustomers(Request $request) { $sales = []; $type = $request->type; $user = Auth::user(); $orgId = $user->organisation_id; $customers = Customer::where('organisation_id', $orgId)->get(); $name = 'Customer_' . now()->format('YmdHis'); if ($type == 'pdf') { $pdf = PDF::loadView('admin.customers.pdf.index', ['customers' => $customers]); return $pdf->download($name . '.pdf'); } else if ($type == 'csv') { return Excel::download(new CustomersExport($customers), $name . '.csv'); } else if ($type == 'excel') { return Excel::download(new CustomersExport($customers), $name . '.xlsx'); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // TODO: convert to service $countries = SystemCountry::get(); set_page_meta(__('custom.add_customer')); return view('admin.customers.create', compact('countries')); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(CustomerRequest $request) { $data = $request->validated(); $adminUser = Auth::user(); $creatorId = ($adminUser->type == 'company' || $adminUser->type == 'super admin') ? $adminUser->id : $adminUser->created_by; if (isset($data['password']) && $data['password']) { $data['password'] = Hash::make($data['password']); } if ($this->customerService->createOrUpdateWithFile($data, 'avatar')) { //Create Replica in Accounting software $accounting_customer = new AccountingCustomers(); $accounting_customer->customer_id = $this->customerNumber(); $accounting_customer->name = $data["first_name"] . ' ' . $data["last_name"]; $accounting_customer->contact = $data["phone"]; $accounting_customer->email = $data["email"]; $accounting_customer->created_by = $creatorId; $accounting_customer->billing_name = $data["first_name"] . '' . $data["last_name"]; $accounting_customer->billing_country = $data["country"]; $accounting_customer->billing_state = $data["state"] ?? null; $accounting_customer->billing_city = $data["city"] ?? null; $accounting_customer->billing_phone = $data["phone"]; $accounting_customer->billing_zip = $data["zipcode"]; $accounting_customer->billing_address = $data["short_address"]; $accounting_customer->organisation_id = $adminUser->organisation_id; if (isset($data['password']) && $data['password']) { $accounting_customer->password = Hash::make($data['password']); } $accounting_customer->lang = ''; $accounting_customer->save(); flash(__('custom.customer_create_successful'))->success(); } else { flash(__('custom.customer_create_failed'))->error(); } return redirect()->route('admin.customers.index'); } function customerNumber() { $adminUser = Auth::user(); $creatorId = ($adminUser->type == 'company' || $adminUser->type == 'super admin') ? $adminUser->id : $adminUser->created_by; $latest = AccountingCustomers::where('created_by', '=', $creatorId)->latest()->first(); if (!$latest) { return 1; } return $latest->customer_id + 1; } /** * Display the specified resource. * * @param int $customer * @return \Illuminate\Http\Response */ public function show(Customer $customer) { set_page_meta(__('custom.customer_details')); return view('admin.customers.show', $this->customerService->customerShowDetails($customer)); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $customer = $this->customerService->get($id); if (!$customer) abort(404); $countries = SystemCountry::get(); set_page_meta(__('custom.edit_customer')); return view('admin.customers.edit', compact('customer', 'countries')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(CustomerRequest $request, $id) { // dd($request->all()); $data = $request->validated(); // dd($data); if (!isset($data['billing_same'])) { $data['billing_same'] = 0; } if (isset($data['password']) && $data['password']) { $data['password'] = Hash::make($data['password']); } if ($this->customerService->createOrUpdateWithFile($data, 'avatar', $id)) { //Edit Also on accounting DB $inventory_customer = Customer::where("id", $id)->first(); $accounting_customer = AccountingCustomers::where('email', $inventory_customer->email)->first(); $accounting_customer->name = $data["first_name"] . ' ' . $data["last_name"]; $accounting_customer->contact = $data["phone"]; $accounting_customer->email = $data["email"]; $accounting_customer->billing_name = $data["first_name"] . '' . $data["last_name"]; $accounting_customer->billing_country = $data["country"]; $accounting_customer->billing_state = $data["state"] ?? null; $accounting_customer->billing_city = $data["city"] ?? null; $accounting_customer->billing_phone = $data["phone"]; $accounting_customer->billing_zip = $data["zipcode"]; $accounting_customer->billing_address = $data["short_address"]; if (isset($data['password']) && $data['password']) { $accounting_customer->password = Hash::make($data['password']); } $accounting_customer->lang = ''; $accounting_customer->save(); flash(__('custom.customer_updated_successful'))->success(); } else { flash(__('custom.customer_updated_failed'))->error(); } return redirect()->route('admin.customers.index'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { try { if ($this->customerService->delete($id)) { flash(__('custom.customer_deleted_successful'))->success(); } else { flash(__('custom.customer_deleted_failed'))->error(); } } catch (\Throwable $th) { flash(__('custom.this_record_already_used'))->warning(); } return redirect()->route('admin.customers.index'); } public function verifyUnverify($id) { $customer = $this->customerService->get($id); if (!$customer) abort(404); if ($customer->is_verified == Customer::STATUS_VERIFIED) { $customer->is_verified = Customer::STATUS_UNVERIFIED; } else { $customer->is_verified = Customer::STATUS_VERIFIED; } $customer->save(); return redirect()->route('admin.customers.index'); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.30 | Generation time: 0 |
proxy
|
phpinfo
|
Settings