<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\TblUserAuthenticate;
use App\Mail\LeadAssigned;
use App\Jobs\SendLeadAssignedEmail;
use Auth;
use DB;
use Mail;

class MasterController extends Controller
{
   
     //api-get-view-list-lead-management
    //view list lead management
    function GetViewListLeadManagement(Request $request)
   {
        //CALL USP_GET_LIST_LEAD_MANAGEMENT('VIEW_LIST_LEAD_MANAGEMENT','SPECIFIC','1',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_LIST_LEAD_MANAGEMENT',
                   'P_ITEM' => $request -> ITEM,
                   'P_USER_SYS_ID' => $request -> USER_SYS_ID,
                  
        ];
        $response = DB::select("call USP_GET_LIST_LEAD_MANAGEMENT(:P_OPERATION_NAME, :P_ITEM,:P_USER_SYS_ID,  @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

    //api-post-add-update-lead-managent
    //add update lead managent
    function PostAddUpdateLeadManagent(Request $request) {
        $requestPostJson= json_decode(json_encode($request ->getContent()));
        //CALL USP_POST_LEAD_MANAGEMENT_ACTIVITY('ADD_UPDATE_LEAD_MANAGEMENT_ACTIVITY', '{"ITEM":"ADD","RECORD_SYS_ID":"0","LEAD_NO":"1","QUOTATION_SYS_ID":"1","LEAD_STATUS":"test","SOURCE":"test","ASSIGNED_SYS_ID":"1","ASSIGNED_TO":"test","PROPERTY_STATUS":"TEST","PROPERTY_TYPE":"TEST","FLOOR_AREA":"1","OWNER_NRIC":"1","KEY_COLLECTION":"TEST" ,"BUDGET":"12" ,"CREDIT":"12" ,"RECEIVED_DATE":"12" ,"SIGNED_DATE":"12" ,"REMARKS":"TEST" ,"CUSTOMER_NAME":"TEST" ,"CONTACT_NO":"12" ,"EMAIL":"TEST" ,"ADDRESS":"TEST" ,"POST_CODE":"1" ,"CREATED_BY":"1"}',@ERRNO,@ERRMSG);
        $payload =[
            'P_OPERATION_NAME' => 'ADD_UPDATE_LEAD_MANAGEMENT_ACTIVITY',
            'P_JSON'=>$requestPostJson,
        ];
        $response = DB::select("call USP_POST_LEAD_MANAGEMENT_ACTIVITY(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response)){
                $response_decoded = json_decode($response[0]->JSON_VALUE, true);
                //return $response_decoded;
                if(isset($request['LEAD_STATUS'])){
                        $leadStatus = $request['LEAD_STATUS'];
                        if($leadStatus == 'Assigned'){
                            if(isset($response_decoded['LEAD_SYS_ID'])){
                                $leadSysId = $response_decoded['LEAD_SYS_ID'];
                                $emailSent = $this->sendLeadAssignedEmail($leadSysId);
                                if (!$emailSent) {
                                    $response_decoded['response'] = 'Event Successfully, But Mail is not Triggered';
                                }
                            }
                        }
                }
                return $response_decoded;
        }
        else {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
    }

    private function sendLeadAssignedEmail($leadSysId){
        try {
            $mailPayload =[
                'P_OPERATION_NAME' => 'VIEW_EMAIL_CONFIG_DETAILS',
                'P_ITEM' => 'MAIL_CONFIG',
                'P_TEMPLATE' => 'LEAD_ASSIGNED',
                'P_JSON'=>'{"LEAD_SYS_ID":'.$leadSysId.'}'
            ];
            $mailResponse = DB::select("call USP_GET_MAIL_TEMPLATE_ACTIVITY(:P_OPERATION_NAME, :P_ITEM, :P_TEMPLATE, :P_JSON, @ERRNO,@ERRMSG)", $mailPayload);
            $mailResponseDecoded = json_decode($mailResponse[0]->JSON_VALUE, true);
            $resStatus = $mailResponseDecoded['status'];
            if($resStatus == 'true'){
                $emailData = $mailResponseDecoded['response'][0];
                if ($emailData) {
                    $email = explode(', ', $emailData["TO_EMAIL"]);
                    $toReceipientsEmail = [];
                    foreach($email as $index => $ce) { if($ce){ $toReceipientsEmail[$index] = trim($ce); }}
                    // Dispatch the job for sending email
                    SendLeadAssignedEmail::dispatch($emailData, $toReceipientsEmail);
                    return true;
                }
            }
            return false;
        } catch (\Exception $e) {
            // Log the exception or handle it as needed
            return false;
        }
    }
  
   //api-get-view-specific-sub-contractor-mgmt
    //view specific sub contractor mgmt
    function GetViewSpecificSubContractorMgmt(Request $request)
   {
        //CALL USP_GET_SUB_CONTRACTOR_MGMT_ACTIVITY('VIEW_SPECIFIC_SUB_CONTRACTOR_MGMT','VIEW_ALL','0',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_SPECIFIC_SUB_CONTRACTOR_MGMT',
                   'P_ITEM' => $request -> ITEM,
                   'P_SUB_CONTRACTOR_SYS_ID'=> $request -> SUB_CONTRACTOR_SYS_ID,
        ];
        $response = DB::select("call USP_GET_SUB_CONTRACTOR_MGMT_ACTIVITY(:P_OPERATION_NAME, :P_ITEM, :P_SUB_CONTRACTOR_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }
   //api-get-view-list-sub-contractor-mgmt
    //view List sub contractor mgmt
    function GetViewListSubContractorMgmt(Request $request)
   {
        //CALL USP_GET_VIEW_LIST_SUB_CONTRACTOR_ACTIVITY('VIEW_SUB_CONTRACTOR_LIST','VIEW_ALL','0',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_SUB_CONTRACTOR_LIST',
                   'P_ITEM' => $request -> ITEM,
                  
        ];
        $response = DB::select("call USP_GET_VIEW_LIST_SUB_CONTRACTOR_ACTIVITY(:P_OPERATION_NAME, :P_ITEM, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-get-view-specific-lead-management
    //view specific lead management
    function    GetViewSpecificLeadManagement(Request $request)
   {
        //CALL USP_GET_SPECIFIC_LEAD_MANAGEMENT('VIEW_SPECIFIC_LEAD_MANAGEMENT','SPECIFIC','12-09-2020','12-09-2022',NULL,@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_SPECIFIC_LEAD_MANAGEMENT',
                   'P_ITEM' => $request -> ITEM,
                   'P_LEAD_SYS_ID' => $request -> LEAD_SYS_ID,
                   'P_START_DATE'=> $request -> START_DATE,
                   'P_END_DATE'=> $request -> END_DATE,
                   'P_ASSIGNED_TO'=> $request -> ASSIGNED_TO,
                   
        ];
        $response = DB::select("call USP_GET_SPECIFIC_LEAD_MANAGEMENT(:P_OPERATION_NAME, :P_ITEM,:P_LEAD_SYS_ID,:P_START_DATE,:P_END_DATE,:P_ASSIGNED_TO, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-get-view-list-of-work
    //view list of work
    function GetViewListOfWork(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_LIST_OF_WORK','VIEW_ALL','0',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_LIST_OF_WORK',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-get-view-work-description
    //view work description
    function GetViewWorkDescription(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_WORK_DESCRIPTION','VIEW_ALL','0',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_WORK_DESCRIPTION',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-post-add-update-work-description
   //add update work description
   function PostAddUpdateWorkDescription(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_WORK_DESCRIPTION','{"ITEM":"ADD","RECORD_SYS_ID":"0","LIST_OF_WORK_SYS_ID":"1","WORK_DESCRIPTION":"TEST","UOM":"KG","ESTIMATED_COST":"500","CREATED_BY":"1"}',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_WORK_DESCRIPTION',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }

   //api-post-add-update-list-of-work
   //add update list of work
   function PostAddUpdateListOfWork(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_LIST_OF_WORK','{"ITEM":"UPDATE","RECORD_SYS_ID":"1","LIST_OF_WORK":"test","CREATED_BY":"1"}',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_LIST_OF_WORK',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }

   //api-post-add-update-country-name
   //add update country name
   function PostAddUpdateCountryName(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_COUNTRY_NAME', '[{"ITEM":"ADD","RECORD_SYS_ID":"0","COUNTRY_NAME":"DF2","CREATED_BY":1}]',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_COUNTRY_NAME',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }

   //api-get-view-country-name-info
    //view country name info
    function GetViewCountryNameInfo(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_COUNTRY_NAME','VIEW_ALL',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_COUNTRY_NAME',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-get-view-master-name-info
    //view master name info
    function GetViewMasterNameInfo(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_MASTER_NAME','VIEW_ALL','0',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_MASTER_NAME',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-get-view-tax-info
    //view tax info
    function GetViewTaxInfo(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_TAX','VIEW_ALL',0,@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_TAX',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-post-add-update-tax-info
   //add update tax info
   function PostAddUpdateTaxInfo(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_TAX', '[{"ITEM":"ADD","RECORD_SYS_ID":"0","TAX_TYPE":"GST","TAX_PERCENTAGE":"7","CREATED_BY":1}]',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_TAX',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }

   //View Organisation Info
   function GetViewMasterOrganisationInfo(Request $request)
   {
        //(CALL USP_GET_ALL_MASTER_DATA @P_OPERATION_NAME='VIEW_ORGANISATION_NAME',@P_ITEM='VIEW_ALL',@ERRNO,@ERRMSG)
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_ORGANISATION_NAME',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID'=> $request -> RECORD_SYS_ID,
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM, :P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //Add Update Organisation Info
   function PostAddUpdateOrganisationName(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       // (CALL [dbo].[USP_POST_ALL_MASTER_DATA] @P_OPERATION_NAME='ADD_UPDATE_ORGANISATION_NAME',@P_JSON='[{"ITEM":"ADD","RECORD_SYS_ID":"0","ORGANISATION_NAME":"Excel","CREATED_BY":1}]')
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_ORGANISATION_NAME',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }
   //api-post-add-update-quotation-info
   //add update quotation info
   function PostAddUpdateQuotationInfo(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       // CALL USP_POST_QUOTATION_ACTIVITY('ADD_UPDATE_QUOTATION_INFO', '{"ITEM":"ADD","QUOTATION_SYS_ID":"0","CUSTOMER_SYS_ID":"2","QUOTATION_NO":"1","CUSTOMER_NAME":"TEST","CUSTOMER_PHONE_NO":"987654321","CUSTOMER_ADDRESS":"TEST","PREPARED_BY":"TEST","SUBJECT":"TEST","QUOTATION_DATE":"07-05-2023","EXPIRY_DATE":"15-12-2023","STATUS":"TEST","PROJECT_SITE_SYS_ID":"1","PROJECT_ADDRESS":"TEST","PROJECT_POST_CODE":"100","PROJECT_COUNTRY":"TEST","BILLING_ADDRESS_SYS_ID":"1","WORK_CATEGORY_ID":"1","WORK_CATEGORY_VALUE":"100","WORK_SUB_CATEGORY_ID":"1","WORK_SUB_CATEGORY_VALUE":"100","INDEX_NO":"123","QTY":"100","UOM":"255","ESTIMETED_COST":"4563","GROSS_AMOUNT":"7852","ACTUAL_AMOUNT":"4563","CREATED_BY":"1"}',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_QUOTATION_INFO',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_QUOTATION_ACTIVITY(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }

    //api-get-view-specific-quotation-info
    //view specific quotation info
    function GetViewSpecificQuotationInfo(Request $request)
    {
         //CALL USP_GET_SPECIFIC_QUOTATION_INFO('VIEW_QUOTATION_INFO','SPECIFIC',2,@ERRNO,@ERRMSG);
         $payload = [
                    'P_OPERATION_NAME' => 'VIEW_QUOTATION_INFO',
                    'P_ITEM' => $request -> ITEM,
                    'P_QUOTATION_ID'=> $request -> QUOTATION_ID,
         ];
         $response = DB::select("call USP_GET_SPECIFIC_QUOTATION_INFO(:P_OPERATION_NAME, :P_ITEM, :P_QUOTATION_ID, @ERRNO,@ERRMSG)", $payload);
         if(!empty($response))
         {
             $response_decoded = json_decode($response[0]->JSON_VALUE, true);
             return $response_decoded;
         }
         else
         {
             $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
             return $response_data;
         }
    }

    //api-get-view-master-system-role
    //view master system role
    function GetViewMasterSystemRole(Request $request)
    {
         //CALL USP_GET_ALL_MASTER_DATA('VIEW_SYSTEM_ROLE','VIEW_ALL','0',@ERRNO,@ERRMSG);
         $payload = [
                    'P_OPERATION_NAME' => 'VIEW_SYSTEM_ROLE',
                    'P_ITEM' => $request -> ITEM,
                    'P_RECORD_SYS_ID'=> $request -> RECORD_SYS_ID,
         ];
         $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM, :P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
         if(!empty($response))
         {
             $response_decoded = json_decode($response[0]->JSON_VALUE, true);
             return $response_decoded;
         }
         else
         {
             $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
             return $response_data;
         }
    }

    //api-post-add-update-system-role
   //add update system role
   function PostAddUpdateSystemRole(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_SYSTEM_ROLE', '[{"ITEM":"ADD","RECORD_SYS_ID":"0","SYSTEM_ROLE":"DF2","ROLE_CODE":"1","CREATED_BY":1}]',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_SYSTEM_ROLE',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }

   //api-get-view-supplier-name
    //view supplier name
    function GetViewSupplierName(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_SUPPLIER_NAME','VIEW_ALL',1,@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_SUPPLIER_NAME',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

    //api-post-add-update-supplier-name
   //add update supplier name
   function PostAddUpdateSupplierName(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_SUPPLIER_NAME', '[{"ITEM":"ADD","RECORD_SYS_ID":"0","SUPPLIER_NAME":"DF2","CREATED_BY":1}]',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_SUPPLIER_NAME',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }

     //api-get-view-location
    //view location
    function GetViewLocation(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_LOCATION','VIEW_ALL',1,@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_LOCATION',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

    //api-post-add-update-location
   //add update location
   function PostAddUpdateLocation(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_LOCATION', '[{"ITEM":"ADD","RECORD_SYS_ID":"0","LOCATION":"KITCHEN","CREATED_BY":1}]',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_LOCATION',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }
      //api-get-view-hacker
    //view hacker
    function GetViewHacker(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_HACKER','VIEW_ALL',1,@ERRNO,@ERRMSG);

        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_HACKER',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                   
           
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-post-add-update-hacker
   //add update hacker
   function PostAddUpdateHacker(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_HACKER', '[{"ITEM":"ADD","RECORD_SYS_ID":"0","HACKER_NAME":"KITCHEN","NRIC":"896523","FIN":"23000","CREATED_BY":1}]',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_HACKER',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }

   //api-post-add-update-uom
  //add update uom
  function PostAddUpdateUom(Request $request) 
  {
      $requestPostJson= json_decode(json_encode($request ->getContent()));
      //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_UOM', '[{"ITEM":"ADD","RECORD_SYS_ID":"0","UOM":"PCS","CREATED_BY":1}]',@ERRNO,@ERRMSG);
      
      $payload =[
          'P_OPERATION_NAME' => 'ADD_UPDATE_UOM',
          'P_JSON'=>$requestPostJson,
      ];
      $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
      if(!empty($response))
      {
          $response_decoded = json_decode($response[0]->JSON_VALUE, true);
          return $response_decoded;
      }
      else
      {
          $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
          return $response_data;
      }
  }

  //api-get-view-uom
    //view uom
    function GetViewUom(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_UOM','VIEW_ALL','0',@ERRNO,@ERRMSG);

        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_UOM',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                   
           
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-get-view-bank-name-info
   function GetViewBankNameInfo(Request $request)
   {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_BANK_NAME','VIEW_ALL',NULL,@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_BANK_NAME',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID'=> $request -> RECORD_SYS_ID,
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM, :P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

   //api-post-add-update-bank-name
   function PostAddUpdateBankName(Request $request) 
   {
       $requestPostJson= json_decode(json_encode($request ->getContent()));
       //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_BANK', '{"ITEM":"ADD","RECORD_SYS_ID":"0","BANK_NAME":"HDFC"}',@ERRNO,@ERRMSG);
       $payload =[
           'P_OPERATION_NAME' => 'ADD_UPDATE_BANK',
           'P_JSON'=>$requestPostJson,
       ];
       $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
       if(!empty($response))
       {
           $response_decoded = json_decode($response[0]->JSON_VALUE, true);
           return $response_decoded;
       }
       else
       {
           $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
           return $response_data;
       }
   }
//View Module Info
function GetViewModuleInfo(Request $request)
{
     //CALL USP_GET_ALL_MASTER_DATA('VIEW_MODULES','VIEW_ALL',0,@ERRNO,@ERRMSG);
     $payload = [
                'P_OPERATION_NAME' => 'VIEW_MODULES',
                'P_ITEM' => $request -> ITEM,
                'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
     ];
     $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM, :P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
     if(!empty($response))
     {
         $response_decoded = json_decode($response[0]->JSON_VALUE, true);
         return $response_decoded;
     }
     else
     {
         $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
         return $response_data;
     }
}

//Add Update Module Info
function PostAddUpdateModule(Request $request) 
{
    $requestPostJson= json_decode(json_encode($request ->getContent()));
    // CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_MODULE', '[{"ITEM":"ADD","RECORD_SYS_ID":"0","MODULE":"DF2","CREATED_BY":1}]',@ERRNO,@ERRMSG);
    $payload =[
        'P_OPERATION_NAME' => 'ADD_UPDATE_MODULE',
        'P_JSON'=>$requestPostJson,
    ];
    $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
    if(!empty($response))
    {
        $response_decoded = json_decode($response[0]->JSON_VALUE, true);
        return $response_decoded;
    }
    else
    {
        $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
        return $response_data;
    }
}

//View Sub Module Info
function GetViewSubModuleInfo(Request $request)
{
     //CALL USP_GET_ALL_MASTER_DATA('VIEW_SUB_MODULES','VIEW_ALL',0,@ERRNO,@ERRMSG);
     $payload = [
                'P_OPERATION_NAME' => 'VIEW_SUB_MODULES',
                'P_ITEM' => $request -> ITEM,
                'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
     ];
     $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM, :P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
     if(!empty($response))
     {
         $response_decoded = json_decode($response[0]->JSON_VALUE, true);
         return $response_decoded;
     }
     else
     {
         $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
         return $response_data;
     }
}

//Add Update Sub Module Info
function PostAddUpdateSubModule(Request $request) 
{
    $requestPostJson= json_decode(json_encode($request ->getContent()));
    //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_SUB_MODULE', '[{"ITEM":"ADD","RECORD_SYS_ID":"0","MODULE_SYS_ID":"2","SUB_MODULE_NAME":"DF2","CREATED_BY":1}]',@ERRNO,@ERRMSG);
    $payload =[
        'P_OPERATION_NAME' => 'ADD_UPDATE_SUB_MODULE',
        'P_JSON'=>$requestPostJson,
    ];
    $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
    if(!empty($response))
    {
        $response_decoded = json_decode($response[0]->JSON_VALUE, true);
        return $response_decoded;
    }
    else
    {
        $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
        return $response_data;
    }
}

//api-get-view-icon-list
    //view icon list
    function GetViewIconList(Request $request)
    {
         //CALL USP_GET_ALL_MASTER_DATA('VIEW_ICON_LIST','VIEW_ALL','0',@ERRNO,@ERRMSG);
         $payload = [
                    'P_OPERATION_NAME' => 'VIEW_ICON_LIST',
                    'P_ITEM' => $request -> ITEM,
                    'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
         ];
         $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM, :P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
         if(!empty($response))
         {
             $response_decoded = json_decode($response[0]->JSON_VALUE, true);
             return $response_decoded;
         }
         else
         {
             $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
             return $response_data;
         }
    }
    //api-post-add-update-library-info
   //add update library info
function PostAddUpdateLibraryInfo(Request $request) 
{
    $requestPostJson= json_decode(json_encode($request ->getContent()));
    //CALL USP_POST_LIBRARY_ACTIVITY('ADD_UPDATE_LIBRARY', '{"INFO":{"ITEM":"ADD_UPDATE_DOCUMENT","DOCUMENTS_SYS_ID":"0","DOCUMENTS_TITLE":"PROJECT","CREATED_BY":"1"}}',@ERRNO,@ERRMSG);
    //CALL USP_POST_LIBRARY_ACTIVITY('ADD_UPDATE_LIBRARY', '{"INFO":{"ITEM":"ADD_UPDATE_DOCUMENT_DETAILS","DOCUMENTS_DETAILS_SYS_ID":"0","DOCUMENTS_SYS_ID":"1","DOCUMENTS_NAME":"VARIATION ORDER","FILES_NAME":"IMG.JPG","CREATED_BY":"2"}}',@ERRNO,@ERRMSG);
    $payload =[
        'P_OPERATION_NAME' => 'ADD_UPDATE_LIBRARY',
        'P_JSON'=>$requestPostJson,
    ];
    $response = DB::select("call USP_POST_LIBRARY_ACTIVITY(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
    if(!empty($response))
    {
        $response_decoded = json_decode($response[0]->JSON_VALUE, true);
        return $response_decoded;
    }
    else
    {
        $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
        return $response_data;
    }
}

    //api-get-view-specific-sub-contractor-mgmt-app
    //view specific sub contractor mgmt
    function GetViewSpecificSubContractorMgmtApp(Request $request)
   {
        //CALL USP_GET_SUB_CONTRACTOR_MGMT_ACTIVITY('VIEW_SPECIFIC_SUB_CONTRACTOR_MGMT','VIEW_ALL','0',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_SPECIFIC_SUB_CONTRACTOR_MGMT',
                   'P_ITEM' => $request -> ITEM,
                   'P_SUB_CONTRACTOR_SYS_ID'=> $request -> SUB_CONTRACTOR_SYS_ID,
        ];
        $response = DB::select("call USP_GET_SUB_CONTRACTOR_MGMT_ACTIVITY(:P_OPERATION_NAME, :P_ITEM, :P_SUB_CONTRACTOR_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
   }

    //api-get-view-terms-conditions
    //view-terms-conditions
    function GetViewTermsConditions(Request $request)
    {
        //CALL USP_GET_ALL_MASTER_DATA('VIEW_TERMS_CONDITIONS','VIEW_ALL','0',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_TERMS_CONDITIONS',
                   'P_ITEM' => $request -> ITEM,
                   'P_RECORD_SYS_ID' => $request -> RECORD_SYS_ID,
                  
        ];
        $response = DB::select("call USP_GET_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_ITEM,:P_RECORD_SYS_ID, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
    }
    //api-post-add-update-terms-conditions
    //add-update-terms-conditions
    function PostAddUpdateTermsConditions(Request $request) 
    {
        $requestPostJson= json_decode(json_encode($request ->getContent()));
        //CALL USP_POST_ALL_MASTER_DATA('ADD_UPDATE_TERMS_CONDITIONS','{"ITEM":"ADD","RECORD_SYS_ID":"0","TERMS_CONDITIONS_TEXT":"TEST1","CREATED_BY":"1"}',@ERRNO,@ERRMSG);
        $payload =[
            'P_OPERATION_NAME' => 'ADD_UPDATE_TERMS_CONDITIONS',
            'P_JSON'=>$requestPostJson,
        ];
        $response = DB::select("call USP_POST_ALL_MASTER_DATA(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
    }
    //api-post-add-update-property-size
    //add-update-property-size
    function PostAddUpdatePropertysize(Request $request) 
    {
        $requestPostJson= json_decode(json_encode($request ->getContent()));
        //CALL USP_POST_MASTER_PROPERTY_SIZE('ADD_UPDATE_PROPERTY_SIZE','{"ITEM":"ADD","RECORD_SYS_ID":"0","PROPERTY_SIZE":"Big","CREATED_BY":"1"}',@ERRNO,@ERRMSG);
        $payload =[
            'P_OPERATION_NAME' => 'ADD_UPDATE_PROPERTY_SIZE',
            'P_JSON'=>$requestPostJson,
        ];
        $response = DB::select("call USP_POST_MASTER_PROPERTY_SIZE(:P_OPERATION_NAME, :P_JSON, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
    }
    //api-get-view-property-size
    //view-property-size
    function GetViewPropertySize(Request $request)
    {
        //CALL USP_GET_MASTER_PROPERTY_SIZE_ACTIVITY('VIEW_PROPERTY_SIZE','VIEW_ALL','0',@ERRNO,@ERRMSG);
        $payload = [
                   'P_OPERATION_NAME' => 'VIEW_PROPERTY_SIZE',
                   'P_ITEM' => $request -> ITEM,
                   'P_PROPERTY_TYPE' => $request -> PROPERTY_TYPE,               
        ];
        $response = DB::select("call USP_GET_MASTER_PROPERTY_SIZE_ACTIVITY(:P_OPERATION_NAME, :P_ITEM,:P_PROPERTY_TYPE, @ERRNO,@ERRMSG)", $payload);
        if(!empty($response))
        {
            $response_decoded = json_decode($response[0]->JSON_VALUE, true);
            return $response_decoded;
        }
        else
        {
            $response_data = [ 'status' => 'false', 'response' => 'opps!! something went wrong',];
            return $response_data;
        }
    }

    
}

