HEX
Server: Apache/2.4.46 (Ubuntu)
System: Linux localhost 5.11.0-49-generic #55-Ubuntu SMP Wed Jan 12 17:36:34 UTC 2022 x86_64
User: root (0)
PHP: 7.4.16
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/fieldsblaze-heroku/app/Http/Controllers/APICustomerController-1.php
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use DB;
use App\Library\UserLib;
use App\Models\User;
use App\Models\Customer;
use App\Models\Departments;
use App\Models\CustomerType;
use App\Models\SalesArea;
use App\Models\PriceBook;
use App\Models\BeatPlan;
use App\Models\PaymentTerm;
use App\Models\Countries;
use App\Models\Contact;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;

class APICustomerController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
 
    protected $userLib;
    public function __construct(UserLib $userLib)
	{
		$this->userLib = $userLib;		 
	}


    //User Registration
    public function createCustomer(Request $request) {
		//TODO What is device_token
        $data = $request->json()->all();	        
         
        $CustomerId = $this->userLib->useridGenrator();
        $OtpPassword = $this->userLib->otpGenrator();

        $tblData = array();
        
        $CreatedDate = date('Y-m-d H:i:s',time());
         
		// $validator = Validator::make($data, [
		// 	'Mobile' => 'required|string|max:255',			  			 
        // ]);
        // //check validation
        // if ($validator->fails())
        // {
        //     $response = [
        //         'Success' => false,
        //         'ResponseMessage' => "Mobile is required!",
        //         'ResponseCode' => '220',
        //         'ApiVersion' => env('APP_VERSION'),    
        //     ];
        //     return response()->json($response);
        // }

        

        //Check Memeber is exist or not
        $custmer = Customer::where('CustomerName', $data['CustomerName'])->get();               
        if ($custmer->count() > 0)
        {   //email is not exist

            $custmer = $custmer->first();   
            $CustomerId = $custmer->CustomerId;
            $response = [
                'Success' => false,
                'ResponseMessage' => "Customer already exist!",                 
                'ResponseCode' => "201",                                              
                'CustomerId' => $CustomerId,                    
                'Customer' => $custmer, 
                'ApiUrl' => env('APP_URL'), 
                'ApiVersion' => env('APP_VERSION'),        
            ];
            return response()->json($response);

            
        }
        else
        {   //get user data                                                        
                                 
             
            $data['CustomerId'] = $CustomerId;  
            
            
            $customerName = $data['CustomerName'];
            if($customerName)
            {
                $customerName = strtoupper($customerName);
                $CustomerCode = substr($customerName, 0, 4).$this->userLib->otpGenrator();                 
                $data['CustomerCode'] = $CustomerCode;  
            }


            ////////////////////////////

            if(count($data['Contact']) > 0)
            {
                $ContactData = $data['Contact'];
                for($k=0; $k < count($ContactData); $k++)
                {
                    $condata = $ContactData[$k];

                     

                    $Contact = Contact::where('Mobile',$condata['Mobile'])->get();               
                    if ($Contact->count() > 0)
                    {   //email is not exist

                        $custmer = $Contact->first();              
                        $response = [
                            'Success' => false,
                            'ResponseMessage' => "Contact already exist!",                 
                            'ResponseCode' => "201",                                                                           
                            'Contact' => $Contact, 
                            'ApiUrl' => env('APP_URL'), 
                            'ApiVersion' => env('APP_VERSION'),        
                        ];
                        return response()->json($response);

                        
                    }
                    else
                    {   
                        
                        $Contact = Contact::where('CustomerId', $CustomerId)->where('IsPrimary', '1')->get();               
                        if ($Contact->count() > 0)
                        {
                            // $response = [
                            //     'Success' => false,
                            //     'ResponseMessage' => "Primary number already exist!",                 
                            //     'ResponseCode' => "209",                                                                                              
                            //     'ApiUrl' => env('APP_URL'), 
                            //     'ApiVersion' => env('APP_VERSION'),        
                            // ];
                            // return response()->json($response);
                            if($condata['IsPrimary'] == '1')
                            {
                                $datau =  array();
                                $datau['IsPrimary'] = '0';                     
                                $Contact_update = Contact::where('CustomerId',$CustomerId)->update($datau);
                                if($Contact_update)
                                {  
                                }
                            }                
                        }


                        $ContactT = Contact::where('CustomerId', $CustomerId)->get();               
                        if ($ContactT->count() == 0)
                        {
                            $condata['IsPrimary'] = '1';      
                        }
                                                
                        //get user data                                                                                                      
                        $condata['ContactId'] = $this->userLib->useridGenrator();                         
                        $condata['Status'] = '1'; 
                        $condata['Created_at'] = $CreatedDate; 
                        $condata['Created_by'] = $request->header('UserId'); 
                        $condata['CustomerId'] = $CustomerId;                                               
                        $Contact_insert = Contact::insert($condata);
                        if($Contact_insert)
                        {                                   
                             
                        }
                        else
                        {
                            // fail

                             
                        }                                                                                                    
                    }
                }
            }

            

            ///////////////////////////

            $data['Status'] = '1'; 
            unset($data['PriceBookName']);
            unset($data['Updated_at']);
            unset($data['Contact']);
            
            $customer_insert = Customer::insert($data);
            if($customer_insert)
            {  
                //get user details
               
                
                $custmer = Customer::select('tbl_customer.*', 'tbl_price_book.PriceBookName', 'tbl_payment_terms.Name as PaymentTermName', 'tbl_beat_plan.BeatPlanName', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer_type.Name as CustomerType', 'tbl_user.FirstName as UpdatedByName', 'tbl_user.FirstName as CreatedByName')
            ->leftJoin('tbl_user', 'tbl_customer.Created_by', '=', 'tbl_user.UserId')
            //->leftJoin('tbl_customer', 'tbl_customer.ParentCustomer', '=', 'tbl_customer.id')
            ->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
            ->leftJoin('tbl_price_book', 'tbl_customer.PriceBookId', '=', 'tbl_price_book.PriceBookId')
            ->leftJoin('tbl_beat_plan', 'tbl_customer.BeatPlan', '=', 'tbl_beat_plan.BeatPlanId')
            ->leftJoin('tbl_sales_area', 'tbl_customer.SalesArea', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_payment_terms', 'tbl_customer.PaymentTermId', '=', 'tbl_payment_terms.PaymentTermId')            
            ->where('tbl_customer.IsDeleted', '=', '0')
            ->where('tbl_customer.CustomerId', $data['CustomerId'])
            ->orderBy('tbl_customer.id', 'desc')
            ->get()->first();

            $Contact = Contact::where('CustomerId', $data['CustomerId'])->get();   
            $custmer->Contact =  $Contact;

                $response = [
                    'Success' => true,
                    'ResponseMessage' => "Customer has been registered",
                    'ResponseCode' => "200",                                              
                    'CustomerId' => $CustomerId,                    
                    'Customer' => $custmer, 
                    'ApiUrl' => env('APP_URL'),       
                    'ApiVersion' => env('APP_VERSION'),                        
                ];
                return response()->json($response);
            }
            else
            {
                // fail
                $response = [
                    'Success' => false,
                    'ResponseMessage' => "Invalid data",
                    'ResponseCode' => "202",   
                    'ApiVersion' => env('APP_VERSION'),                      
                ];
                return response()->json($response);
            }                                                                                                    
        }
    }
    
        
    //User create first time password
    public function updateCustomer(Request $request) {
		//TODO What is device_token
        $data = $request->json()->all();	      
        
        $OtpPassword = $this->userLib->otpGenrator();

        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken');

        $tblData = array();
        
        $CreatedDate = date('Y-m-d H:i:s',time());

        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }
         
		unset($data['PriceBookName']); 
        unset($data['Created_at']);
        //Check Memeber is exist or not
        $user = Customer::where('CustomerId', $data['CustomerId'])->get();               
        if ($user->count() == 0)
        {   //customer not exist
            $response = [
                'Success' => false,
                'ResponseMessage' => "The customer does not exist!",
                'ResponseCode' => '201',
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }
                    
        //update customer
        $CustomerId = $data['CustomerId']; 
        unset($data['CustomerId']);      
        unset($data['Mobile']); 
        unset($data['Contact']);                        
        $data['Updated_at'] = $CreatedDate;

      //  return response()->json($data);        
                        
        $customer_update = Customer::where('CustomerId',$CustomerId)->update($data);                    
        if($customer_update)
        {
            
            $custmer = Customer::select('tbl_customer.*', 'tbl_price_book.PriceBookName', 'tbl_beat_plan.BeatPlanName', 'tbl_payment_terms.Name as PaymentTermName', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer_type.Name as CustomerType', 'tbl_user.FirstName as UpdatedByName', 'tbl_user.FirstName as CreatedByName')
            ->leftJoin('tbl_user', 'tbl_customer.Created_by', '=', 'tbl_user.UserId')
            //->leftJoin('tbl_customer', 'tbl_customer.ParentCustomer', '=', 'tbl_customer.id')
            ->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
            ->leftJoin('tbl_price_book', 'tbl_customer.PriceBookId', '=', 'tbl_price_book.PriceBookId')
            ->leftJoin('tbl_beat_plan', 'tbl_customer.BeatPlan', '=', 'tbl_beat_plan.BeatPlanId')
            ->leftJoin('tbl_sales_area', 'tbl_customer.SalesArea', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_payment_terms', 'tbl_customer.PaymentTermId', '=', 'tbl_payment_terms.PaymentTermId')
            ->where('tbl_customer.IsDeleted', '=', '0')
            ->where('tbl_customer.CustomerId', $CustomerId)
            ->orderBy('tbl_customer.id', 'desc')
            ->get()->first();               
            // success
            $response = [
                'Success' => true,
                'ResponseMessage' => "Success",
                'ResponseCode' => "200",
                'CustomerId' => $CustomerId,                            
                'AuthToken' => $AuthToken,                 
                'Customer' => $custmer, 
                'ApiUrl' => env('APP_URL'),   
                'ApiVersion' => env('APP_VERSION'),                            
            ];
        }
        else
        {
            // fail
            $response = [
                'Success' => false,
                'ResponseMessage' => "Invalid Auth",
                'ResponseCode' => "203",                
                'ApiVersion' => env('APP_VERSION'),    
            ];
        }
            return response()->json($response);                                                         
    }  


    //edit profile picture

    //User edit profile
    public function editCustomerProfileImage(Request $request) {
		//TODO What is device_token
       // $data = $request->json()->all();	               
       
       
        $CreatedDate = date('Y-m-d H:i:s',time());

        $CustomerId = $request->input('CustomerId');
        $AuthToken = $request->header('AuthToken');
                 
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

         
        $imageName = $this->userLib->useridGenrator();

        if ($request->hasFile('CustomerProfileImage')) 
        {                        
            $file = $request->file('CustomerProfileImage');
            $extension = $file->getClientOriginalExtension();
            $newFileName = $imageName.'-'.time() . '.' . $extension;
            
            Storage::putFileAs(
                'public/customer', $file, $newFileName
            );

            $path = Storage::url('customer/'.$newFileName);  

            $data = array();
            $data['CustomerProfileImage'] = $path;
            $data['Updated_by'] = session('UserId');;
            $data['Updated_at'] = $CreatedDate;
                                      
            $customer_update = Customer::where('CustomerId',$CustomerId)->update($data);         
            
            if($customer_update)
            {            
                
                $customer_details = Customer::select('tbl_customer.*', 'tbl_price_book.PriceBookName', 'tbl_payment_terms.Name as PaymentTermName', 'tbl_beat_plan.BeatPlanName', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer_type.Name as CustomerType', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_customer.Created_by', '=', 'tbl_admin.UserId')
            //->leftJoin('tbl_customer', 'tbl_customer.ParentCustomer', '=', 'tbl_customer.id')
            ->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
            ->leftJoin('tbl_price_book', 'tbl_customer.PriceBookId', '=', 'tbl_price_book.PriceBookId')
            ->leftJoin('tbl_beat_plan', 'tbl_customer.BeatPlan', '=', 'tbl_beat_plan.BeatPlanId')
            ->leftJoin('tbl_sales_area', 'tbl_customer.SalesArea', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_payment_terms', 'tbl_customer.PaymentTermId', '=', 'tbl_payment_terms.PaymentTermId')   
            ->where('tbl_customer.IsDeleted', '=', '0')
            ->where('tbl_customer.CustomerId', $CustomerId)
            ->orderBy('tbl_customer.id', 'desc')
            ->get();

                $customer_data = $customer_details->first();

                $customer_data->ParentCustomerName = '';
                if($customer_data->ParentCustomerId)
                {
                    $ParentCustomer = Customer::select('CustomerName')                         
                    ->where('CustomerId', $customer_data->ParentCustomerId)
                    ->orderBy('tbl_customer.id', 'desc')
                    ->get()->first();
                    $customer_data->ParentCustomerName = $ParentCustomer->CustomerName;
                }

                $response = [
                    'success' => false,                
                    'error_message' => "Success",
                    'error_code' => "200",
                    'ApiVersion' => env('APP_VERSION'),    
                    'ApiUrl' => env('APP_URL'),               
                    'Customer'=>$customer_data,
                    
                ];
                return response()->json($response);
            }
            else
            {
                $response = [
                    'Success' => false,
                    'ResponseMessage' => "Invalid data",
                    'ResponseCode' => "202",    
                    'ApiVersion' => env('APP_VERSION'),                     
                ];
                return response()->json($response);
            }
        }
         
		 
    }

    //end of edit profile
    
    

    //User edit profile
    public function editCustomerProfile(Request $request) {
		//TODO What is device_token
        $data = $request->json()->all();	               
        $OtpPassword = $this->userLib->otpGenrator();
        $tblData = array();        
        $CreatedDate = date('Y-m-d H:i:s',time());

        $CustomerId = $request->header('CustomerId');
        $AuthToken = $request->header('AuthToken');
         

        

        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }
         
		$validator = Validator::make($data, [
			'Mobile' => 'required|string|max:255',	
             	  			 
        ]);
        //check validation
        if ($validator->fails())
        {
            $response = [
                'Success' => false,
                'ResponseMessage' => "Mobile is required!",
                'ResponseCode' => '220',
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        //Check Memeber is exist or not
        $user = Customer::where('Mobile', $data['Mobile'])->get();               
        if ($user->count() > 0)
        {   //email is exist
            $user = $user->first();   
             
                   
            $data['ModifiedDate'] = $CreatedDate;
             
            unset($data['TempPassword']);
            unset($data['OtpPassword']);

            $customer_update = DB::table('tbl_customer')->where('CustomerId',$CustomerId)->update($data);                    
            if($customer_update)
            {
                

                $customer_details = Customer::select('tbl_customer.*', 'tbl_price_book.PriceBookName', 'tbl_payment_terms.Name as PaymentTermName', 'tbl_beat_plan.BeatPlanName', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer_type.Name as CustomerType', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
                ->leftJoin('tbl_admin', 'tbl_customer.Created_by', '=', 'tbl_admin.UserId')
                //->leftJoin('tbl_customer', 'tbl_customer.ParentCustomer', '=', 'tbl_customer.id')
                ->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
                ->leftJoin('tbl_price_book', 'tbl_customer.PriceBookId', '=', 'tbl_price_book.PriceBookId')
                ->leftJoin('tbl_beat_plan', 'tbl_customer.BeatPlan', '=', 'tbl_beat_plan.BeatPlanId')
                ->leftJoin('tbl_sales_area', 'tbl_customer.SalesArea', '=', 'tbl_sales_area.SalesAreaId')
                ->leftJoin('tbl_payment_terms', 'tbl_customer.PaymentTermId', '=', 'tbl_payment_terms.PaymentTermId')   
                ->where('tbl_customer.IsDeleted', '=', '0')
                ->where('tbl_customer.Mobile', $data['Mobile'])
                ->orderBy('tbl_customer.id', 'desc')
                ->get();

                $customer_data = $customer_details->first(); 

                $customer_data->ParentCustomerName = '';
                if($customer_data->ParentCustomerId)
                {
                    $ParentCustomer = Customer::select('CustomerName')                         
                    ->where('CustomerId', $customer_data->ParentCustomerId)
                    ->orderBy('tbl_customer.id', 'desc')
                    ->get()->first();
                    $customer_data->ParentCustomerName = $ParentCustomer->CustomerName;
                }
                
                unset($customer_data->Password);
                unset($customer_data->ResetKey);
                //create auth token
                $response = [
                    'Success' => true,
                    'ResponseMessage' => "Success",
                    'ResponseCode' => "200",
                    'CustomerId' => $CustomerId,                            
                    'AuthToken' => $AuthToken,
                    
                    'Customer' => $customer_data, 
                    'ApiUrl' => env('APP_URL'),    
                    'ApiVersion' => env('APP_VERSION'),                           
                ];                                               
                return response()->json($response);
            } 
            else
            {
                // fail
                $response = [
                    'Success' => false,
                    'ResponseMessage' => "Invalid data",
                    'ResponseCode' => "202",    
                    'ApiVersion' => env('APP_VERSION'),                     
                ];
                return response()->json($response);
            }                                         
        }
        else
        {   
            // fail
            $response = [
                'Success' => false,
                'ResponseMessage' => "User not exist",
                'ResponseCode' => "201",   
                'ApiVersion' => env('APP_VERSION'),                      
            ];
            return response()->json($response);
                                                                                                           
        }
    }  


    
    //User get profile
    public function getCustomerProfile(Request $request, $CustomerId) {
		//TODO What is device_token
        $data = $request->json()->all();	                       
        $tblData = array();                 
         
                   
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }
         
		 
        //check validation
        if (!$CustomerId)
        {
            $response = [
                'Success' => false,
                'ResponseMessage' => "Customer is required!",
                'ResponseCode' => '220',
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        //Check Memeber is exist or not
    
        $Customer = Customer::select('tbl_customer.*', 'tbl_price_book.PriceBookName',  'tbl_beat_plan.BeatPlanName', 'tbl_payment_terms.Name as PaymentTermName', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer_type.Name as CustomerType', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_customer.Created_by', '=', 'tbl_admin.UserId')
            //->leftJoin('tbl_customer', 'tbl_customer.ParentCustomer', '=', 'tbl_customer.id')
            ->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
            ->leftJoin('tbl_price_book', 'tbl_customer.PriceBookId', '=', 'tbl_price_book.PriceBookId')
            ->leftJoin('tbl_beat_plan', 'tbl_customer.BeatPlan', '=', 'tbl_beat_plan.BeatPlanId')
            ->leftJoin('tbl_sales_area', 'tbl_customer.SalesArea', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_payment_terms', 'tbl_customer.PaymentTermId', '=', 'tbl_payment_terms.PaymentTermId')   
            ->where('tbl_customer.IsDeleted', '=', '0')
            ->where('tbl_customer.CustomerId', $CustomerId)
            ->orderBy('tbl_customer.id', 'desc')
            ->get();


        if ($Customer->count() > 0)
        {   //email is exist
            $Customer = $Customer->first();   

            $Customer->ParentCustomerName = '';
            if($Customer->ParentCustomerId)
            {
                $ParentCustomer = Customer::select('CustomerName')                         
                ->where('CustomerId', $Customer->ParentCustomerId)
                ->orderBy('tbl_customer.id', 'desc')
                ->get()->first();
                $Customer->ParentCustomerName = $ParentCustomer->CustomerName;
            }

            $Customer->DistributorName = '';
            if($Customer->DistributorId)
            {
                $Distributor = Customer::select('CustomerName')                         
                ->where('CustomerId', $Customer->DistributorId)
                ->orderBy('id', 'desc')
                ->get()->first();
                $Customer->DistributorName = $Distributor->CustomerName;
            }


            $Customer->WholeSellerName = '';
            if($Customer->WholeSellerId)
            {
                $Distributor = Customer::select('CustomerName')                         
                ->where('CustomerId', $Customer->WholeSellerId)
                ->orderBy('id', 'desc')
                ->get()->first();
                $Customer->WholeSellerName = $Distributor->CustomerName;
            }


            $Customer->ShippingCountryName = '';
            if($Customer->ShippingCountry)
            {                
                $countries = Countries::select('country_name')                         
                ->where('id', $Customer->ShippingCountry)                
                ->get()->first();
                $Customer->ShippingCountryName = $countries->country_name;
            }


            $Customer->BillingCountryName = '';
            if($Customer->BillingCountry)
            {                
                $countries = Countries::select('country_name')                         
                ->where('id', $Customer->BillingCountry)                
                ->get()->first();
                $Customer->BillingCountryName = $countries->country_name;
            }
            
            
            unset($Customer->Password);
            unset($Customer->ResetKey);
            unset($Customer->TempPassword);
            unset($Customer->OtpPassword);

            $CustomerId = $request->header('CustomerId');
            $AuthToken = $request->header('AuthToken');
            $response = [
                'Success' => true,
                'ResponseMessage' => "Success",
                'ResponseCode' => "200",
                'CustomerId' => $CustomerId,                            
                'AuthToken' => $AuthToken,                
                'Customer' => $Customer, 
                'ApiUrl' => env('APP_URL'),    
                'ApiVersion' => env('APP_VERSION'),                           
            ];                                               
            return response()->json($response);                                                                                        
        }
        else
        {   
            // fail
            $response = [
                'Success' => false,
                'ResponseMessage' => "Customer does not exist",
                'ResponseCode' => "201",   
                'ApiVersion' => env('APP_VERSION'),                      
            ];
            return response()->json($response);
                                                                                                           
        }
    }
    
    

    ### All Customer
    public function getCustomerList(Request $request, $CustomerTypeId =''){


        $data = $request->json()->all();	                       
 
        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken'); 
                   
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        if($CustomerTypeId)
        {
            $Customers = Customer::select('tbl_customer.*', 'tbl_price_book.PriceBookName', 'tbl_user.FirstName as Updated_by_name', 'tbl_user.FirstName as Created_by_name')        
            ->leftJoin('tbl_user', 'tbl_customer.Created_by', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_price_book', 'tbl_customer.PriceBookId', '=', 'tbl_price_book.PriceBookId')        
            ->where('tbl_customer.IsDeleted', '=', '0')
            ->where('tbl_customer.Status', '=', '1')
            ->where('tbl_customer.CustomerTypeId', '=', $CustomerTypeId)
            ->where('tbl_customer.Created_by',$UserId)
            ->get();
        }
        else
        {
            $Customers = Customer::select('tbl_customer.*', 'tbl_price_book.PriceBookName', 'tbl_user.FirstName as Updated_by_name', 'tbl_user.FirstName as Created_by_name')        
            ->leftJoin('tbl_user', 'tbl_customer.Created_by', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_price_book', 'tbl_customer.PriceBookId', '=', 'tbl_price_book.PriceBookId')        
            ->where('tbl_customer.IsDeleted', '=', '0')
            ->where('tbl_customer.Status', '=', '1')
            ->where('tbl_customer.Created_by',$UserId)
            ->get();
        }


        
        foreach ($Customers as $key => $CustomerValue) 
        {                         
            $ParentCustomerId = $CustomerValue->ParentCustomerId;      
            $CustomerName = '';           
            $custmer = Customer::select('CustomerName')->where('CustomerId', $ParentCustomerId)->get();               
            if ($custmer->count() > 0)
            {   //email is  exist    
                $custmer = $custmer->first();   
                $CustomerName = $custmer->CustomerName;
            }

            $CustomerValue['ParentCustomerName'] = $CustomerName;
        }
                         
        $response = [
            'Success' => true,
            'ResponseMessage' => "Success",
            'ResponseCode' => "200",                              
            'AuthToken' => $AuthToken,  

            'Customers' => $Customers, 
            'ApiUrl' => env('APP_URL'),    
            'ApiVersion' => env('APP_VERSION'),                           
        ];                                               
        return response()->json($response);    
        
       
    }


    ### All Customer
    public function getDepartmentList(Request $request){


        $data = $request->json()->all();	                       
 
        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken'); 
                   
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        $Departments = Departments::select('*')->get();                 
        $response = [
            'Success' => true,
            'ResponseMessage' => "Success",
            'ResponseCode' => "200",                              
            'AuthToken' => $AuthToken,  
            'Departments' => $Departments, 
            'ApiUrl' => env('APP_URL'),    
            'ApiVersion' => env('APP_VERSION'),                           
        ];                                               
        return response()->json($response);    
        
       
    }


    ### All CustomerType
    public function getCustomerTypeList(Request $request){


        $data = $request->json()->all();	                       
 
        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken'); 
                   
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        $CustomerType = CustomerType::select('*')
        ->where('Status', '=', '1')
        ->where('IsDeleted', '=', '0')
        ->get();                 
        $response = [
            'Success' => true,
            'ResponseMessage' => "Success",
            'ResponseCode' => "200",                              
            'AuthToken' => $AuthToken,  
            'CustomerTypes' => $CustomerType, 
            'ApiUrl' => env('APP_URL'),    
            'ApiVersion' => env('APP_VERSION'),                           
        ];                                               
        return response()->json($response);    
        
       
    }




     ### All Customer
     public function getSalesAreaList(Request $request){


        $data = $request->json()->all();	                       
 
        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken'); 
                   
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        $SalesArea = SalesArea::select('tbl_sales_area.*', 'tbl_sales_territory.Name as SalesTerritoryName')         
        ->leftJoin('tbl_sales_territory', 'tbl_sales_area.SalesTerritory', '=', 'tbl_sales_territory.id')        
        ->where('tbl_sales_area.IsDeleted', '=', '0')
        ->where('tbl_sales_area.Status', '=', '1')
        ->get();                 
        $response = [
            'Success' => true,
            'ResponseMessage' => "Success",
            'ResponseCode' => "200",                              
            'AuthToken' => $AuthToken,  

            'SalesArea' => $SalesArea, 
            'ApiUrl' => env('APP_URL'),    
            'ApiVersion' => env('APP_VERSION'),                           
        ];                                               
        return response()->json($response);    
        
       
    }




    ### All Pricebook
    public function getPriceBookList(Request $request){


        $data = $request->json()->all();	                       
 
        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken'); 
                   
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        $PriceBook = PriceBook::select('*')                         
        ->where('IsDeleted', '=', '0')
        ->where('Status', '=', '1')
        ->get();                 
        $response = [
            'Success' => true,
            'ResponseMessage' => "Success",
            'ResponseCode' => "200",                              
            'AuthToken' => $AuthToken,  

            'PriceBooks' => $PriceBook, 
            'ApiUrl' => env('APP_URL'),    
            'ApiVersion' => env('APP_VERSION'),                           
        ];                                               
        return response()->json($response);    
        
       
    }


    ### All Pricebook
    public function getBeatPlanList(Request $request){
        $data = $request->json()->all();	                        
        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken');                    
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }
        $BeatPlan = BeatPlan::select('*')                         
        ->where('IsDeleted', '=', '0')
        ->where('Status', '=', '1')
        ->get();                 
        $response = [
            'Success' => true,
            'ResponseMessage' => "Success",
            'ResponseCode' => "200",                              
            'AuthToken' => $AuthToken,  
            'BeatPlan' => $BeatPlan, 
            'ApiUrl' => env('APP_URL'),    
            'ApiVersion' => env('APP_VERSION'),                           
        ];                                               
        return response()->json($response);                   
    }



    ### All Pricebook
    public function getPaymentTearmList(Request $request){
        $data = $request->json()->all();	                        
        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken');                    
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }
        $PaymentTerm = PaymentTerm::select('*')                         
        ->where('IsDeleted', '=', '0')
        ->where('Status', '=', '1')
        ->get();                 
        $response = [
            'Success' => true,
            'ResponseMessage' => "Success",
            'ResponseCode' => "200",                              
            'AuthToken' => $AuthToken,  
            'PaymentTerms' => $PaymentTerm, 
            'ApiUrl' => env('APP_URL'),    
            'ApiVersion' => env('APP_VERSION'),                           
        ];                                               
        return response()->json($response);                   
    }



    ### All Pricebook
    public function getDistributorList(Request $request, $CustomerTypeId = ''){
        $data = $request->json()->all();	                        
        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken');                    
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }
        
        $keyname = '';
        if($CustomerTypeId == '7zbZseyyTqvA6n7n5Bk01RvCm30KpPQa8M8k7') //Retailer
        {
            $CustomerTypeId = '4zZVseyyTqvA6n7n5Bk01RvCm30KpPQa8M8k2'; //Distributor
            $keyname = "Distributor";
        }
        else if($CustomerTypeId == '4zZVseyyTqvA6n7n5Bk01RvCm30KpPQa8M8k2') //Distributor
        {
            $CustomerTypeId = 'ozbVseyyTqvA6n7n5Bk01RvCm40KpPQa8M8k3';//Whole Seller
            $keyname = "WholeSeller";
        }
        else if($CustomerTypeId == 'ozbVseyyTqvA6n7n5Bk01RvCm40KpPQa8M8k3') //Whole Seller
        {
            $CustomerTypeId = '';
            $keyname = '';
            
        }
        
        
        $Distributor = Customer::select('CustomerId', 'CustomerCode', 'CustomerName', 'Type', 'CustomerTypeId', 'Status', 'IsDeleted', 'Created_at')            
            ->where('CustomerTypeId', $CustomerTypeId)
            ->where('IsDeleted', '=', '0')
        ->where('Status', '=', '1')
            ->orderBy('id', 'desc')            
            ->get();
        if($Distributor->count() > 0)
        {
            $response = [
                'Success' => true,
                'ResponseMessage' => "Success",
                'ResponseCode' => "200",                              
                'AuthToken' => $AuthToken,  
                $keyname => $Distributor, 
                'ApiUrl' => env('APP_URL'),    
                'ApiVersion' => env('APP_VERSION'),                           
            ];   
        }    
        else
        {
            $response = [
                'Success' => false,
                'ResponseMessage' => "Data not found",
                'ResponseCode' => "201",                                                                     
                'ApiUrl' => env('APP_URL'),    
                'ApiVersion' => env('APP_VERSION'),                           
            ];
        }                                        
        return response()->json($response);                   
    }

 
    //Contact

    //Create customer contact
    public function createCustomerContact(Request $request) {
		//TODO What is device_token
        $data = $request->json()->all();	        
         
        $ContactId = $this->userLib->useridGenrator();         
        $tblData = array();        
        $CreatedDate = date('Y-m-d H:i:s',time());         
		$validator = Validator::make($data, [
			'Mobile' => 'required|string|max:255',			  			 
        ]);
        //check validation
        if ($validator->fails())
        {
            $response = [
                'Success' => false,
                'ResponseMessage' => "Mobile is required!",
                'ResponseCode' => '220',
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        $CustomerId = $data['CustomerId'];
        //Check Memeber is exist or not
        $Contact = Contact::where('Mobile', $data['Mobile'])->get();               
        if ($Contact->count() > 0)
        {   //email is not exist

            $custmer = $Contact->first();              
            $response = [
                'Success' => false,
                'ResponseMessage' => "Contact already exist!",                 
                'ResponseCode' => "201",                                                                           
                'Contact' => $Contact, 
                'ApiUrl' => env('APP_URL'), 
                'ApiVersion' => env('APP_VERSION'),        
            ];
            return response()->json($response);

            
        }
        else
        {   
            
            $Contact = Contact::where('CustomerId', $data['CustomerId'])->where('IsPrimary', '1')->get();               
            if ($Contact->count() > 0)
            {
                // $response = [
                //     'Success' => false,
                //     'ResponseMessage' => "Primary number already exist!",                 
                //     'ResponseCode' => "209",                                                                                              
                //     'ApiUrl' => env('APP_URL'), 
                //     'ApiVersion' => env('APP_VERSION'),        
                // ];
                // return response()->json($response);
                if($data['IsPrimary'] == '1')
                {
                    $datau =  array();
                    $datau['IsPrimary'] = '0';                     
                    $Contact_update = Contact::where('CustomerId',$data['CustomerId'])->update($datau);
                    if($Contact_update)
                    {  
                    }
                }                
            }


            $ContactT = Contact::where('CustomerId', $data['CustomerId'])->get();               
            if ($ContactT->count() == 0)
            {
                $data['IsPrimary'] = '1';      
            }


            
            
            //get user data                                                                                                      
            $data['ContactId'] = $ContactId;                         
            $data['Status'] = '1'; 
            $data['Created_at'] = $CreatedDate; 
            $data['Created_by'] = $request->header('UserId'); 
            

            $Contact_insert = Contact::insert($data);
            if($Contact_insert)
            {                                   
                $response = [
                    'Success' => true,
                    'ResponseMessage' => "Contact has been created",
                    'ResponseCode' => "200",                                                                         
                    
                    'ApiUrl' => env('APP_URL'),       
                    'ApiVersion' => env('APP_VERSION'),                        
                ];
                return response()->json($response);
            }
            else
            {
                // fail
                $response = [
                    'Success' => false,
                    'ResponseMessage' => "Invalid data",
                    'ResponseCode' => "202",   
                    'ApiVersion' => env('APP_VERSION'),                      
                ];
                return response()->json($response);
            }                                                                                                    
        }
    }


    //update customer contact
    public function updateCustomerContact(Request $request) {
		//TODO What is device_token
        $data = $request->json()->all();	        
         
        $ContactId = $this->userLib->useridGenrator();         
        $tblData = array();        
        $CreatedDate = date('Y-m-d H:i:s',time());         
		$validator = Validator::make($data, [
			'Mobile' => 'required|string|max:255',			  			 
        ]);
        //check validation
        if ($validator->fails())
        {
            $response = [
                'Success' => false,
                'ResponseMessage' => "Mobile is required!",
                'ResponseCode' => '220',
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        $ContactId = $data['ContactId'];
        //Check Memeber is exist or not
        $Contact = Contact::where('ContactId', $data['ContactId'])->get();               
        if ($Contact->count() == 0)
        {   //email is not exist

            $custmer = $Contact->first();              
            $response = [
                'Success' => false,
                'ResponseMessage' => "Contact does not found",                 
                'ResponseCode' => "201",                                                                           
                'Contact' => $Contact, 
                'ApiUrl' => env('APP_URL'), 
                'ApiVersion' => env('APP_VERSION'),        
            ];
            return response()->json($response);

            
        }
        else
        {   //get user data                                                                                                      
            
            // $custmer = $Contact->first();     
            // $Contact = Contact::where('CustomerId', $custmer->CustomerId)->where('IsPrimary', '1')->get();               
            // if ($Contact->count() > 0)
            // {
            //     $response = [
            //         'Success' => false,
            //         'ResponseMessage' => "Primary number already exist!",                 
            //         'ResponseCode' => "209",                                                                           
                    
            //         'ApiUrl' => env('APP_URL'), 
            //         'ApiVersion' => env('APP_VERSION'),        
            //     ];
            //     return response()->json($response);
            // }

            if($data['IsPrimary'])
            {
                $custmer = $Contact->first(); 
                $CustomerId = $custmer->CustomerId;
                $ContactT = Contact::where('CustomerId', $CustomerId)->get();               
                if ($ContactT->count() > 0)
                {  
                    $udata = array();
                    $udata['IsPrimary'] = 0;
                    $Contact_update = Contact::where('CustomerId', $CustomerId)->update($udata);  
                }
            }


            $data['Updated_at'] = $CreatedDate; 
            $data['Updated_by'] = $request->header('UserId'); 

            $Contact_update = Contact::where('ContactId',$ContactId)->update($data);
            if($Contact_update)
            {  
                //get user details                               
                $Contact = Contact::select('*')            
                ->where('IsDeleted', '=', '0')
                ->where('ContactId', $ContactId)
                ->orderBy('id', 'desc')
                ->get()->first();

                if ($Contact->CustomerId) {
                    $Customer = Customer::where('CustomerId', '=', $Contact->CustomerId)->get()->first();                    
                    $CustomerName = $Customer->CustomerName;
                    $CustomerCode = $Customer->CustomerCode;
                    $Contact->CustomerName =  $CustomerName;
                    $Contact->CustomerCode =  $CustomerCode;
                }

                $CountryName = '';
                if($Contact->Country)
                {                
                    $countries = Countries::select('country_name')                         
                    ->where('id', $Contact->Country)                
                    ->get()->first();
                    $CountryName = $countries->country_name;
                }
                $Contact->CountryName =  $CountryName;


                $response = [
                    'Success' => true,
                    'ResponseMessage' => "Contact has been created",
                    'ResponseCode' => "200",                                                                         
                    'Contact' => $Contact, 
                    'ApiUrl' => env('APP_URL'),       
                    'ApiVersion' => env('APP_VERSION'),                        
                ];
                return response()->json($response);
            }
            else
            {
                // fail
                $response = [
                    'Success' => false,
                    'ResponseMessage' => "Invalid data",
                    'ResponseCode' => "202",   
                    'ApiVersion' => env('APP_VERSION'),                      
                ];
                return response()->json($response);
            }                                                                                                    
        }
    }



    //update customer contact
    public function deleteCustomerContact(Request $request) {
		//TODO What is device_token
        $data = $request->json()->all();	        
         
        $ContactId = $this->userLib->useridGenrator();         
        $tblData = array();        
        $CreatedDate = date('Y-m-d H:i:s',time());         
		 

        $ContactId = $data['ContactId'];
        //Check Memeber is exist or not
        $Contact = Contact::where('ContactId', $data['ContactId'])->get();               
        if ($Contact->count() == 0)
        {   //email is not exist

            $custmer = $Contact->first();              
            $response = [
                'Success' => false,
                'ResponseMessage' => "Contact does not found",                 
                'ResponseCode' => "201",                                                                           
                'Contact' => $Contact, 
                'ApiUrl' => env('APP_URL'), 
                'ApiVersion' => env('APP_VERSION'),        
            ];
            return response()->json($response);

            
        }
        else
        {   //get user data                                                                                                      
            
            $data = array();
            $data['IsDeleted'] = '1'; 
            $data['Status'] = '0'; 
            $data['Updated_at'] = $CreatedDate; 
            $data['Updated_by'] = $request->header('UserId'); 
            $Contact_update = Contact::where('ContactId',$ContactId)->update($data);
            if($Contact_update)
            {  
                $response = [
                    'Success' => true,
                    'ResponseMessage' => "Contact has been deleted",
                    'ResponseCode' => "200",                                                                                             
                    'ApiUrl' => env('APP_URL'),       
                    'ApiVersion' => env('APP_VERSION'),                        
                ];
                return response()->json($response);
            }
            else
            {
                // fail
                $response = [
                    'Success' => false,
                    'ResponseMessage' => "Invalid data",
                    'ResponseCode' => "202",   
                    'ApiVersion' => env('APP_VERSION'),                      
                ];
                return response()->json($response);
            }                                                                                                    
        }
    }



    //List of contact     
    public function getCustomerContactList(Request $request, $CustomerId =''){
            $data = $request->json()->all();	                            
            $UserId = $request->header('UserId');
            $AuthToken = $request->header('AuthToken'); 
                       
            //check auth token
            if($this->userLib->tokenChecker($request) == FALSE)
            {
                $response = [
                    'success' => false,
                    'error_message' => "Authentication Failed",
                    'error_code' => "301",
                    'ApiVersion' => env('APP_VERSION'),    
                ];
                return response()->json($response);
            }
    
            if($CustomerId)
            {
                $Contacts = Contact::select('*')                                             
                ->where('IsDeleted', '=', '0')
                ->where('Status', '=', '1')
                ->where('CustomerId', '=', $CustomerId)
                ->get();
            }
            else
            {
                $Contacts = Contact::select('*')                         
                ->where('IsDeleted', '=', '0')
                ->where('Status', '=', '1')
                ->get();
            }                    
            foreach ($Contacts as $key => $CustomerValue) 
            {                         
                $CustomerId = $CustomerValue->CustomerId;      
                $CustomerName = '';           
                $custmer = Customer::select('CustomerName','CustomerCode')->where('CustomerId', $CustomerId)->get();               
                if ($custmer->count() > 0)
                {   //email is  exist    
                    $custmer = $custmer->first();   
                    $CustomerName = $custmer->CustomerName;
                    $CustomerCode = $custmer->CustomerCode;
                } 
                $Country = isset($CustomerValue->Country) ? $CustomerValue->Country : '';
                $CountryName = '';
                if($Country)
                {                
                    $countries = Countries::select('country_name')                         
                    ->where('id', $Country)                
                    ->get()->first();
                    $CountryName = $countries->country_name;
                }

                $CustomerValue['CustomerName'] = $CustomerName;
                $CustomerValue['CountryName'] = $CountryName;
            }
                             
            $response = [
                'Success' => true,
                'ResponseMessage' => "Success",
                'ResponseCode' => "200",                              
                'AuthToken' => $AuthToken,      
                'Contacts' => $Contacts, 
                'ApiUrl' => env('APP_URL'),    
                'ApiVersion' => env('APP_VERSION'),                           
            ];                                               
            return response()->json($response);                           
        }


        //List of contact     
    public function getCustomerContactDetail(Request $request, $ContactId =''){
        $data = $request->json()->all();	                            
        $UserId = $request->header('UserId');
        $AuthToken = $request->header('AuthToken'); 
                   
        //check auth token
        if($this->userLib->tokenChecker($request) == FALSE)
        {
            $response = [
                'success' => false,
                'error_message' => "Authentication Failed",
                'error_code' => "301",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        if(!$ContactId)
        {
            $response = [
                'success' => false,
                'error_message' => "ContactId Required",
                'error_code' => "202",
                'ApiVersion' => env('APP_VERSION'),    
            ];
            return response()->json($response);
        }

        $Contacts = Contact::select('*')                                             
        ->where('IsDeleted', '=', '0')
        ->where('Status', '=', '1')
        ->where('ContactId', '=', $ContactId)
        ->get();
                       
     

        if ($Contacts->count() > 0)
        {            
            $Contacts = $Contacts->first();                                  
            $CustomerId = $Contacts->CustomerId;      
            $CustomerName = '';           
            $custmer = Customer::select('CustomerName','CustomerCode')->where('CustomerId', $CustomerId)->get();               
            if ($custmer->count() > 0)
            {   //email is  exist    
                $custmer = $custmer->first();   
                $CustomerName = $custmer->CustomerName;
                $CustomerCode = $custmer->CustomerCode;
            }    
            $Contacts->CustomerName = $CustomerName;
            $Contacts->CustomerCode = $CustomerCode;
        }

        $Contacts->CountryName = '';
        $Country = isset($Contacts->Country) ? $Contacts->Country : '';
        if($Country)
        {                
            $countries = Countries::select('country_name')                         
            ->where('id', $Country)                
            ->get()->first();
            $Contacts->CountryName = $countries->country_name;
        }
                         
        $response = [
            'Success' => true,
            'ResponseMessage' => "Success",
            'ResponseCode' => "200",                              
            'AuthToken' => $AuthToken,      
            'Contacts' => $Contacts, 
            'ApiUrl' => env('APP_URL'),    
            'ApiVersion' => env('APP_VERSION'),                           
        ];                                               
        return response()->json($response);                           
    }
     
}