<?php require_once '../../masterpage/webservice_master.php';
header("Access-Control-Allow-Origin: *");
extract($_POST);

ini_set('memory_limit','-1');
ini_set('max_execution_time', 0);
ini_set('post_max_size', 200428800);

//print_r($door_installation_survey['main_question_arrray']);

          
// echo "<pre>";
 //print_r($_POST);
//   exit;

  define('UPLOAD_DIR', '../../../images/door_images/');
  define('UPLOAD_DIR_SIGN', '../../../images/sing/');
if($_POST['door_installation_survey']){
if($door_installation_survey['installation_door_ID'] == 0){
        
  
      if($door_installation_survey['ext_img']!=''){
      $img1 = $door_installation_survey['ext_img'];
      $img1 = str_replace('data:image/png;base64,', '', $img1);
      $img1 = str_replace(' ', '+', $img1);
      $data1 = base64_decode($img1);
      $ext_img = uniqid() . '.png';
      $file1 = UPLOAD_DIR .$ext_img ;
      $success1 = file_put_contents($file1, $data1);
      }else{
        $ext_img='';
      }
     if($door_installation_survey['int_img']!=''){
      $img2 = $door_installation_survey['int_img'];
      $img2 = str_replace('data:image/png;base64,', '', $img2);
      $img2 = str_replace(' ', '+', $img2);
      $data2 = base64_decode($img2);
      $int_img = uniqid() . '.png';
      $file2 = UPLOAD_DIR .$int_img;
      $success2 = file_put_contents($file2, $data2);
      }else{
      $int_img='';
      }

      if($door_installation_survey['ext_img1']!=''){
      $img1 = $door_installation_survey['ext_img1'];
      $img1 = str_replace('data:image/png;base64,', '', $img1);
      $img1 = str_replace(' ', '+', $img1);
      $data2 = base64_decode($img1);
      $ext_img1 = uniqid() . '.png';
      $file1 = UPLOAD_DIR .$ext_img1 ;
      $success1 = file_put_contents($file1, $data2);
      }else{
        $ext_img1='';
      }
     if($door_installation_survey['int_img1']!=''){
      $img3 = $door_installation_survey['int_img1'];
      $img3 = str_replace('data:image/png;base64,', '', $img3);
      $img3 = str_replace(' ', '+', $img3);
      $data3 = base64_decode($img3);
      $int_img1 = uniqid() . '.png';
      $file2 = UPLOAD_DIR .$int_img1;
      $success2 = file_put_contents($file2, $data3);
      }else{
      $int_img1='';
      }

      if($door_installation_survey['firestopping_external']!=''){
      $img1 = $door_installation_survey['firestopping_external'];
      $img1 = str_replace('data:image/png;base64,', '', $img1);
      $img1 = str_replace(' ', '+', $img1);
      $data2 = base64_decode($img1);
      $firestopping_external = uniqid() . '.png';
      $file1 = UPLOAD_DIR .$firestopping_external ;
      $success1 = file_put_contents($file1, $data2);
      }else{
        $firestopping_external='';
      }
     if($door_installation_survey['firestopping_internal']!=''){
      $img3 = $door_installation_survey['firestopping_internal'];
      $img3 = str_replace('data:image/png;base64,', '', $img3);
      $img3 = str_replace(' ', '+', $img3);
      $data3 = base64_decode($img3);
      $firestopping_internal = uniqid() . '.png';
      $file2 = UPLOAD_DIR .$firestopping_internal;
      $success2 = file_put_contents($file2, $data3);
      }else{
      $firestopping_internal='';
      }

    if($door_installation_survey['signed_installer']!=''){
      $img2 = $door_installation_survey['signed_installer'];
      $img2 = str_replace('data:image/png;base64,', '', $img2);
      $img2 = str_replace(' ', '+', $img2);
      $data1 = base64_decode($img2);
      $signed_installer = uniqid() . '.png';
      $file1 = UPLOAD_DIR_SIGN .$signed_installer ;
      $success1 = file_put_contents($file1, $data1);
      }else{
        $signed_installer='';
      }
     if($door_installation_survey['signed_resident']!=''){
      $img2 = $door_installation_survey['signed_resident'];
      $img2 = str_replace('data:image/png;base64,', '', $img2);
      $img2 = str_replace(' ', '+', $img2);
      $data2 = base64_decode($img2);
      $signed_resident = uniqid() . '.png';
      $file2 = UPLOAD_DIR_SIGN .$signed_resident;
      $success2 = file_put_contents($file2, $data2);
      }else{
      $signed_resident='';
      }
       if($door_installation_survey['signed_supervisor']!=''){
      $img3 = $door_installation_survey['signed_supervisor'];
      $img3 = str_replace('data:image/png;base64,', '', $img3);
      $img3 = str_replace(' ', '+', $img3);
      $data3 = base64_decode($img3);
      $signed_supervisor = uniqid() . '.png';
      $file3 = UPLOAD_DIR_SIGN .$signed_supervisor;
      $success3 = file_put_contents($file3, $data3);
      }else{
      $signed_supervisor='';
      }


      //-------------------------
          /*Door Ordering functionality*/  
  $last_door_count = $obj->fetchRow("lfs_fire_door_installation_survey","project_ID = ".$door_installation_survey['project_ID']." ORDER BY installation_door_ID DESC LIMIT 1");
  
    $door_installation_survey_details = $obj->fetchRow("lfs_fire_door_installation_survey","project_ID = ".$door_installation_survey['project_ID']." AND installation_door_ID=".$door_installation_survey['installation_door_ID']);
    $oldfloor = $door_installation_survey_details['floor_No'];
    $ordernumber= (int)$door_installation_survey_details['install_Order'];
   // echo $oldfloor .'!='.$door_installation_survey['floor_No'];
    if($oldfloor != $door_installation_survey['floor_No']){
      /*if the floor is changed then*/

      /*
      Ordering Logic
      Minus Ordering for Basement eg. -10001
      Single digit ordering for Ground floor eg. 1
      Five Digit ordering for normal floor eg. 10001
      Six Digit ordering for normal Customfloor eg. 120001
      Seven Digit Ordering for text Custom floor eg.2010001
      */


    $doors = $obj->fetchRow("lfs_fire_door_installation_survey","project_ID = ".$door_installation_survey['project_ID']." AND floor_No='".$door_installation_survey['floor_No']."'");

    
      //echo "<pre>";print_r($doors);exit;
    if($doors){
      if(count($doors) > 0){
        $lastorder = ($doors['install_Order'] != NULL || $doors['install_Order'] !="")?(int)$doors['install_Order']:0;
      }else{
        $lastorder = 0;
      }
    }else{
      $lastorder = 0;
    }
      //echo $lastorder."<br>";

      if($door_installation_survey['floor_No'] == "G"){
        /*For Ground Floor*/
        
        $startupenum = $lastorder+1;

      }else if($door_installation_survey['floor_No'] == "B"){
        /*For Basement Floor*/
        if($lastorder != 0){
          $tempfloor = $lastorder-1;
          $startupenum = $tempfloor;  // Apend three zero

        }else{
          $tempfloor = $lastorder+1;
          $startupenum = "-1".sprintf("%04d", $tempfloor);
        }


      }else{

        if((int)$door_installation_survey['floor_No'] > 100 ){ // here floor id will be checked.
          /*For Custom Floor*/
          $tempfloor = $lastorder+1;
          $selfloor = $obj->fetchRow("lfs_custom_floor","floorNo='".$door_installation_survey['floor_No']."' AND project_ID=".$door_installation_survey['project_ID']);
          $getfloorno = $selfloor['floorNo'];
          $getfloororder = $selfloor['floor_Order'];
          /*Below condition is for checking whether custome floor is for normal floor or custom named floor*/
          if((int)$getfloorno == 0){
            /*Customenamed floor*/
            
            if($lastorder != 0){
              $startupenum = $tempfloor;
            }else{
              $startupenum = $door_installation_survey['floor_No'].sprintf("%04d", $tempfloor);
            }
          }else{
            /*Regular floor's Custome floor*/
            if($lastorder != 0){
              $startupenum = $tempfloor;
            }else{
              $startupenum = $getfloorno.$getfloororder.sprintf("%04d", $tempfloor);  
            }
          }
        }else{
          /*For Normal Floor*/
          $tempfloor = $lastorder+1;
          //echo $tempfloor;exit;
          if($lastorder != 0){
            $startupenum = $tempfloor;   // Apend two zero
          }else{  
            $startupenum = $door_installation_survey['floor_No'].sprintf("%03d", $tempfloor);   // Apend two zero
          }

        }        
      }
      $ordernumber = (int)$startupenum;
    }

    /*Door Ordering functionality*/
    $last_door_count = $obj->fetchRowAll("lfs_fire_door_installation_survey","project_ID = ".$door_installation_survey['project_ID']." ORDER BY installation_door_ID DESC");
    if($last_door_count){
      $door_count=count($last_door_count)+1;
    }else{
      $door_count=1;
    }
          $date1=str_replace("/","-",$door_installation_survey['createdDate']);
          $date2=str_replace("/","-",$door_installation_survey['updatedDate']);
          $date3=str_replace("/","-",$door_installation_survey['date_sup_sign']);
          $date4=str_replace("/","-",$door_installation_survey['date_of_sign']);
          $createdDate=date('Y-m-d H:i:s',strtotime($date1));
          $updatedDate=date('Y-m-d H:i:s',strtotime($date2));
          $date_sup_sign=date('Y-m-d H:i:s',strtotime($date3));
          $date_of_sign=date('Y-m-d H:i:s',strtotime($date4));
     
     
      $field_new_fire_survey = array('install_Order','project_ID','floor_No','door_no_Name','location','client_door_reference','Barcode_ref','reference_number','type_of_install','fire_rating','construction','ext_img','int_img','ext_img1','int_img1','firestopping_external','firestopping_internal','installer_comment','installer_name','signed_installer','date_installed','resident_que1','operated_before_installer','have_3nr_keys_provided','recommand_to_other','resident_communi_during','resident_staff_attitude','resident_quality','resident_workmanship','resident_cleanliness','resident_comment','resident_name','signed_resident','date_of_sign','client_representative_que','client_representative','signed_supervisor','date_sup_sign','createdBy','updatedBy','createdDate','updatedDate','isActive','isDelete');

      $value_new_fire_survey = array($ordernumber,$door_installation_survey['project_ID'],$door_installation_survey['floor_No'],$door_installation_survey['door_no_Name'],$door_installation_survey['location'],$door_installation_survey['client_door_reference'],$door_installation_survey['Barcode_ref'],$door_installation_survey['reference_number'],$door_installation_survey['type_of_install'],$door_installation_survey['fire_rating'],$door_installation_survey['construction'],$ext_img,$int_img,$ext_img1,$int_img1,$firestopping_external,$firestopping_internal,$door_installation_survey['installer_comment'],$door_installation_survey['installer_name'],$signed_installer,$date_installed,$door_installation_survey['resident_que1'],$door_installation_survey['operated_before_installer'],$door_installation_survey['have_3nr_keys_provided'],$door_installation_survey['recommand_to_other'],$door_installation_survey['resident_communi_during'],$door_installation_survey['resident_staff_attitude'],$door_installation_survey['resident_quality'],$door_installation_survey['resident_workmanship'],$door_installation_survey['resident_cleanliness'],$door_installation_survey['resident_comment'],$door_installation_survey['resident_name'],$signed_resident,$date_of_sign,$door_installation_survey['client_representative_que'],$door_installation_survey['client_representative'],$signed_supervisor,$date_sup_sign,$door_installation_survey['createdBy'],$door_installation_survey['updatedBy'],$createdDate,$updatedDate,$door_installation_survey['isActive'],$door_installation_survey['isDelete']);
        
        $Ins_installation_door_ID = $obj->insert($field_new_fire_survey,$value_new_fire_survey,'lfs_fire_door_installation_survey');

/********for firedoor survey add basic details and delete fail door details*******************/

        if($door_installation_survey['firedoor_ID']=='0'){

                $doorcounts = $obj->fetchNumOfRow('lfs_fire_door_installation_survey','project_ID='.$door_installation_survey['project_ID'].' AND isActive=1 AND isDelete=0');
                $doorcount=$doorcount+1;

                $field_new_fire_survey = array('project_ID','Barcode_ref','floor_No','property_no_Name','location','client_door_reference','door_count','lfs_fire_door_result','isActive','isDelete','createdDate','updatedDate');

                $value_new_fire_survey = array($door_installation_survey['project_ID'],$door_installation_survey['Barcode_ref'],$door_installation_survey['floor_No'],$door_installation_survey['door_no_Name'],$door_installation_survey['location'],$door_installation_survey['client_door_reference'],$doorcount,'3',1,0,$door_installation_survey['createdBy'],$door_installation_survey['updatedBy']);

                $firedoor_IDS = $obj->insert($field_new_fire_survey,$value_new_fire_survey,'lfs_firedoor_survey');
      }else{

                $condis="firedoor_ID=".$door_installation_survey['firedoor_ID'];
                $obj->delete('lfs_firedoor_survey_color',$condis);
                $obj->delete('lfs_fds_pricelist_ans',$condis);
                $obj->delete('lfs_fds_custom_pricelist_ans',$condis);
                $obj->delete('lfs_fire_door_question_answ',$condis);
                $obj->delete('lfs_fire_door_art_question_answ',$condis);
                $obj->delete('lfs_full_door_cate_additional_fields',$condis);
                $obj->delete('lfs_full_door_cate_status',$condis);
                $obj->delete('lfs_fds_category_na_feature',$condis);
              
               $field_new_fire_survey = array(
                'project_ID',
                'firedoor_Order',
               'door_reference',
               'Barcode_ref',
               'floor_No',
               'property_no_Name',
               'location',
               'client_door_reference',
               'ext_door_image1',
               'ext_door_image2',
               'floor_plan',
               'full_access',
               'reason_no_access',
               'add_info',
               'manufaturer',
               'installer',
               'type_of_door',
               'type_of_dooradd',
               'construction_type',
               'construction_typeadd',
               'fanlight_present',
               'sidelight_present',
               'vision_panel',
               'inteded_fire_rating',
               'continue_assess_fire_door',
               'cont_ass_fire_art_17',
               'cont_ass_fire_art_18',
               'cont_ass_fire_art_20',
               'cont_ass_fire_add_field',
               'cont_ass_fire_add_image1',
               'cont_ass_fire_add_image2',
               'na_immediate_fail',
               'asses_fire_rated',
               'fire_rate_asses_reason',
               'door_plug',
               'door_plug_img1',
               'door_plug_img2',
               'door_plug_description',
               'test_evidance',
               'hanging',
               'hanging_NA',
               'structural_opening',
               'structural_opening_NA',
               'leaf_size',
               'leaf_size_NA',
               'leaf_color',
               'leaf_color_NA',
               'leaf_colortxt',
               'frame_color',
               'frame_colortxt',
               'frame_color_NA',
               'door_closer_type',
               'name_known_door',
               'O_Lock',
               'name_known_lock',
               'O_Lock_add_field',
               'door_status',
               'door_statustxt',
               'summary_of_findings',
               'summary_add_info',
               'certification',
               'lfs_fire_door_result',
               'isRepairs',
               'operative_ID',
               'operative_Status',
               'Operative_comments',
               'Operative_updated',
               'notes_for_operative',
               'isActive',
               'isDelete',
               'updatedBy',
               'updatedDate',
               'NextMaintenanceDate');


                 $value_new_fire_survey = array(
                  $door_installation_survey['project_ID'],
                  '',
                 '',
                 $door_installation_survey['Barcode_ref'],
                 $door_installation_survey['floor_No'],
                 $door_installation_survey['door_no_Name'],
                 $door_installation_survey['location'],
                 $door_installation_survey['client_door_reference'],
                 '',
                 '',
                 '',
                 '3',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '3',
                 '3',
                 '3',
                 '',
                 '3',
                 '3',
                 '3',
                 '3',
                 '3',
                 '3',
                 '3',
                 '',
                 '',
                 '',
                 '3',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '3',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '3',
                 '',
                 '',
                 '',
                 '',
                 '',
                 '',
                 1,
                 0,
                 '',
                 $door_installation_survey['updatedDate'],
                 '0000-00-00 00:00:00');
    
                $updatesfiredoor = $obj->update($field_new_fire_survey,$value_new_fire_survey,$condis,'lfs_firedoor_survey');

      }
/*****************************************************************************************************/
        //---insert question answer
        if($main_question_arrray){
         foreach ($main_question_arrray as $keyvalue) {
            

           $field_new_door_survey_field = array('fdi_Q_ID','type_of_install','installation_door_ID','answer');
            $field_new_door_survey_value = array($keyvalue['fdi_Q_ID'],$keyvalue['type_of_install'],$Ins_installation_door_ID,$keyvalue['answer']);


            $insert_queanswer = $obj->insert($field_new_door_survey_field,$field_new_door_survey_value,'lfs_fire_door_installation_question_ans');

          }
        }
        
        //---insert sub question answer
        if($_POST['sub_question_array']){
         foreach ($sub_question_array as $keyvalue) {
            

           $field_new_door_survey_field = array('fdi_SQ_ID','type_of_install','installation_door_ID','answer');
            $field_new_door_survey_value = array($keyvalue['fdi_SQ_ID'],$keyvalue['type_of_install'],$Ins_installation_door_ID,$keyvalue['answer']);


            $insert_subqueanswer = $obj->insert($field_new_door_survey_field,$field_new_door_survey_value,'lfs_fire_door_installation_subquestion_ans');

          }
        }

        //---insert AdditionalInfo

        // print_r($_POST['AdditionalInfo']);
        if($AdditionalInfo){

         $additionalinfo_result=array();
          $b=0;
           
           foreach ($AdditionalInfo as $keyvalues) {
           $lfs_fdi_additionalinfobox_field = array('fdi_Q_ID','type_of_install','installation_door_ID','additional_info_box');
            $lfs_fdi_additionalinfobox_value = array($keyvalues['fdi_Q_ID'],$keyvalues['type_of_install'],$Ins_installation_door_ID,$keyvalues['additional_info_box']);


            $insert_subqueansweradd = $obj->insert($lfs_fdi_additionalinfobox_field,$lfs_fdi_additionalinfobox_value,'lfs_fdi_additionalinfobox');
                $additionalinfo_result[$b]['result']='Insert';
                $additionalinfo_result[$b]['adi_SQ_A_ID']=$insert_subqueansweradd;
                $additionalinfo_result[$b]['ID']=$keyvalues['ID'];

                $b++;

          }

        }
          

      echo json_encode(array('result'=>'Insert','installation_door_ID'=>$Ins_installation_door_ID,'Local_Installation_Door_ID'=>$door_installation_survey['Local_Installation_Door_ID'],'additionalInfo'=>$additionalinfo_result));

}else{
 //-------------------------------------------update door-----------------------------------------

      
      if($door_installation_survey['ext_img']!=''){
      $img1 = $door_installation_survey['ext_img'];
      $img1 = str_replace('data:image/png;base64,', '', $img1);
      $img1 = str_replace(' ', '+', $img1);
      $data1 = base64_decode($img1);
      $ext_img = uniqid() . '.png';
      $file1 = UPLOAD_DIR .$ext_img ;
      $success1 = file_put_contents($file1, $data1);
      }else{
        $ext_img='';
      }
     if($door_installation_survey['int_img']!=''){
      $img2 = $door_installation_survey['int_img'];
      $img2 = str_replace('data:image/png;base64,', '', $img2);
      $img2 = str_replace(' ', '+', $img2);
      $data2 = base64_decode($img2);
      $int_img = uniqid() . '.png';
      $file2 = UPLOAD_DIR .$int_img;
      $success2 = file_put_contents($file2, $data2);
      }else{
      $int_img='';
      }

      if($door_installation_survey['ext_img1']!=''){
      $img1 = $door_installation_survey['ext_img1'];
      $img1 = str_replace('data:image/png;base64,', '', $img1);
      $img1 = str_replace(' ', '+', $img1);
      $data2 = base64_decode($img1);
      $ext_img1 = uniqid() . '.png';
      $file1 = UPLOAD_DIR .$ext_img1 ;
      $success1 = file_put_contents($file1, $data2);
      }else{
        $ext_img1='';
      }
     if($door_installation_survey['int_img1']!=''){
      $img3 = $door_installation_survey['int_img1'];
      $img3 = str_replace('data:image/png;base64,', '', $img3);
      $img3 = str_replace(' ', '+', $img3);
      $data3 = base64_decode($img3);
      $int_img1 = uniqid() . '.png';
      $file2 = UPLOAD_DIR .$int_img1;
      $success2 = file_put_contents($file2, $data3);
      }else{
      $int_img1='';
      }

      if($door_installation_survey['firestopping_external']!=''){
      $img1 = $door_installation_survey['firestopping_external'];
      $img1 = str_replace('data:image/png;base64,', '', $img1);
      $img1 = str_replace(' ', '+', $img1);
      $data2 = base64_decode($img1);
      $firestopping_external = uniqid() . '.png';
      $file1 = UPLOAD_DIR .$firestopping_external ;
      $success1 = file_put_contents($file1, $data2);
      }else{
        $firestopping_external='';
      }
     if($door_installation_survey['firestopping_internal']!=''){
      $img3 = $door_installation_survey['firestopping_internal'];
      $img3 = str_replace('data:image/png;base64,', '', $img3);
      $img3 = str_replace(' ', '+', $img3);
      $data3 = base64_decode($img3);
      $firestopping_internal = uniqid() . '.png';
      $file2 = UPLOAD_DIR .$firestopping_internal;
      $success2 = file_put_contents($file2, $data3);
      }else{
      $firestopping_internal='';
      }
      
    if($door_installation_survey['signed_installer']!=''){
      $img2 = $door_installation_survey['signed_installer'];
      $img2 = str_replace('data:image/png;base64,', '', $img2);
      $img2 = str_replace(' ', '+', $img2);
      $data1 = base64_decode($img2);
      $signed_installer = uniqid() . '.png';
      $file1 = UPLOAD_DIR_SIGN .$signed_installer ;
      $success1 = file_put_contents($file1, $data1);
      }else{
        $signed_installer='';
      }
     if($door_installation_survey['signed_resident']!=''){
      $img2 = $door_installation_survey['signed_resident'];
      $img2 = str_replace('data:image/png;base64,', '', $img2);
      $img2 = str_replace(' ', '+', $img2);
      $data2 = base64_decode($img2);
      $signed_resident = uniqid() . '.png';
      $file2 = UPLOAD_DIR_SIGN .$signed_resident;
      $success2 = file_put_contents($file2, $data2);
      }else{
      $signed_resident='';
      }
      if($door_installation_survey['signed_supervisor']!=''){
      $img3 = $door_installation_survey['signed_supervisor'];
      $img3 = str_replace('data:image/png;base64,', '', $img3);
      $img3 = str_replace(' ', '+', $img3);
      $data3 = base64_decode($img3);
      $signed_supervisor = uniqid() . '.png';
      $file3 = UPLOAD_DIR_SIGN .$signed_supervisor;
      $success3 = file_put_contents($file3, $data3);
      }else{
      $signed_supervisor='';
      }


        $last_door_count = $obj->fetchRow("lfs_fire_door_installation_survey","project_ID = ".$door_installation_survey['project_ID']." ORDER BY installation_door_ID DESC LIMIT 1");
               /*Door Ordering functionality*/  
   // echo "<pre>";print_r($door_installation_survey_details);exit;
        $last_door_count = $obj->fetchRow("lfs_fire_door_installation_survey","project_ID = ".$door_installation_survey['project_ID']." ORDER BY installation_door_ID DESC LIMIT 1");
    $door_installation_survey_details = $obj->fetchRow("lfs_fire_door_installation_survey","project_ID = ".$door_installation_survey['project_ID']." and installation_door_ID = ".$door_installation_survey['installation_door_ID']);

    $oldfloor = $door_installation_survey_details['floor_No'];
    $ordernumber= (int)$door_installation_survey_details['install_Order'];
    if($oldfloor != $door_installation_survey['floor_No']){
      /*if the floor is changed then*/

      /*
      Ordering Logic
      Minus Ordering for Basement eg. -10001
      Single digit ordering for Ground floor eg. 1
      Five Digit ordering for normal floor eg. 10001
      Six Digit ordering for normal Customfloor eg. 120001
      Seven Digit Ordering for text Custom floor eg.2010001
      */
      $doors = $obj->fetchRow("lfs_fire_door_installation_survey","project_ID='".$door_installation_survey['project_ID']."' AND floor_No = '".$door_installation_survey['floor_No']."' order by install_Order DESC LIMIT 1");
      //echo "<pre>";print_r($doors);exit;
      if(count($doors) > 0){
        $lastorder = ($doors['install_Order'] != NULL || $doors['install_Order'] !="")?(int)$doors['install_Order']:0;
      }else{
        $lastorder = 0;
      }
      //echo $lastorder."<br>";

      if($door_installation_survey['floor_No'] == "G"){
        /*For Ground Floor*/
        $startupenum = $lastorder+1;

      }else if($door_installation_survey['floor_No'] == "B"){
        /*For Basement Floor*/
        if($lastorder != 0){
          $tempfloor = $lastorder-1;
          $startupenum = $tempfloor;  // Apend three zero

        }else{
          $tempfloor = $lastorder+1;
          $startupenum = "-1".sprintf("%04d", $tempfloor);
        }


      }else{

        if((int)$door_installation_survey['floor_No'] > 100 ){ // here floor id will be checked.
          /*For Custom Floor*/
          $tempfloor = $lastorder+1;
          $selfloor = $obj->fetchRow("lfs_custom_floor","floorNo='".$door_installation_survey['floor_No']."' AND project_ID=".$door_installation_survey['project_ID']);
          $getfloorno = $selfloor['floorNo'];
          $getfloororder = $selfloor['floor_Order'];
          /*Below condition is for checking whether custome floor is for normal floor or custom named floor*/
          if((int)$getfloorno == 0){
            /*Customenamed floor*/
            
            if($lastorder != 0){
              $startupenum = $tempfloor;
            }else{
              $startupenum = $door_installation_survey['floor_No'].sprintf("%04d", $tempfloor);
            }
          }else{
            /*Regular floor's Custome floor*/
            if($lastorder != 0){
              $startupenum = $tempfloor;
            }else{
              $startupenum = $getfloorno.$getfloororder.sprintf("%04d", $tempfloor);  
            }
          }
        }else{
          /*For Normal Floor*/
          $tempfloor = $lastorder+1;
          //echo $tempfloor;exit;
          if($lastorder != 0){
            $startupenum = $tempfloor;   // Apend two zero
          }else{  
            $startupenum = $door_installation_survey['floor_No'].sprintf("%03d", $tempfloor);   // Apend two zero
          }

        }        
      }
      $ordernumber = (int)$startupenum;
    }


         
          $date2=str_replace("/","-",$door_installation_survey['updatedDate']);
          $date3=str_replace("/","-",$door_installation_survey['date_sup_sign']);
          $date4=str_replace("/","-",$door_installation_survey['date_of_sign']);
          
          $updatedDate=date('Y-m-d H:i:s',strtotime($date2));
          $date_sup_sign=date('Y-m-d H:i:s',strtotime($date3));
          $date_of_sign=date('Y-m-d H:i:s',strtotime($date4));
          

        $field_new_fire_survey = array('install_Order','project_ID','floor_No','door_no_Name','location','client_door_reference','Barcode_ref','reference_number','type_of_install','fire_rating','construction','ext_img','int_img','ext_img1','int_img1','firestopping_external','firestopping_internal','installer_comment','installer_name','signed_installer','date_installed','resident_que1','operated_before_installer','have_3nr_keys_provided','recommand_to_other','resident_communi_during','resident_staff_attitude','resident_quality','resident_workmanship','resident_cleanliness','resident_comment','resident_name','signed_resident','date_of_sign','client_representative_que','client_representative','signed_supervisor','date_sup_sign','updatedBy','updatedDate','isActive','isDelete');

        $value_new_fire_survey = array($ordernumber,$door_installation_survey['project_ID'],$door_installation_survey['floor_No'],$door_installation_survey['door_no_Name'],$door_installation_survey['location'],$door_installation_survey['client_door_reference'],$door_installation_survey['Barcode_ref'],$door_installation_survey['reference_number'],$door_installation_survey['type_of_install'],$door_installation_survey['fire_rating'],$door_installation_survey['construction'],$ext_img,$int_img,$ext_img1,$int_img1,$firestopping_external,$firestopping_internal,$door_installation_survey['installer_comment'],$door_installation_survey['installer_name'],$signed_installer,$date_installed,$door_installation_survey['resident_que1'],$door_installation_survey['operated_before_installer'],$door_installation_survey['have_3nr_keys_provided'],$door_installation_survey['recommand_to_other'],$door_installation_survey['resident_communi_during'],$door_installation_survey['resident_staff_attitude'],$door_installation_survey['resident_quality'],$door_installation_survey['resident_workmanship'],$door_installation_survey['resident_cleanliness'],$door_installation_survey['resident_comment'],$door_installation_survey['resident_name'],$signed_resident,$date_of_sign,$door_installation_survey['client_representative_que'],$door_installation_survey['client_representative'],$signed_supervisor,$date_sup_sign,$door_installation_survey['updatedBy'],$updatedDate,$door_installation_survey['isActive'],$door_installation_survey['isDelete']);


       $Update_installation_door_ID = $obj->update($field_new_fire_survey,$value_new_fire_survey,'installation_door_ID = '.$door_installation_survey['installation_door_ID'],'lfs_fire_door_installation_survey');

 //---insert question answer
        if($main_question_arrray){
         foreach ($main_question_arrray as $keyvalue) {
           $field_new_door_survey_field = array('fdi_Q_ID','type_of_install','installation_door_ID','answer');
           $field_new_door_survey_value = array($keyvalue['fdi_Q_ID'],$keyvalue['type_of_install'],$Ins_installation_door_ID,$keyvalue['answer']);

             $Update_installation_queanswer = $obj->update($field_new_door_survey_field,$field_new_door_survey_value,'installation_door_ID = '.$door_installation_survey['installation_door_ID'].' AND fdi_Q_ID='.$keyvalue['fdi_Q_ID'],'lfs_fire_door_installation_question_ans');
            

          }
        }
        //---update sub question answer
        if($sub_question_array){
         foreach ($sub_question_array as $keyvalue) {
            

           $field_new_door_survey_field = array('fdi_SQ_ID','type_of_install','answer');
            $field_new_door_survey_value = array($keyvalue['fdi_SQ_ID'],$keyvalue['type_of_install'],$keyvalue['answer']);


            $Update_installation_queanswer = $obj->update($field_new_door_survey_field,$field_new_door_survey_value,'installation_door_ID = '.$door_installation_survey['installation_door_ID'].' AND fdi_SQ_ID='.$keyvalue['fdi_SQ_ID'],'lfs_fire_door_installation_subquestion_ans');

          }
        }
          
            //---insert AdditionalInfo
        $additionalinfo_result=array();
        if($AdditionalInfo){
          $b=0;
         foreach ($AdditionalInfo as $keyvalue) {
            
          $lfs_fdi_additionalinfobox_field = array('fdi_Q_ID','type_of_install','installation_door_ID','additional_info_box');
            $lfs_fdi_additionalinfobox_value = array($keyvalue['fdi_Q_ID'],$keyvalue['type_of_install'],$keyvalue['installation_door_ID'],$keyvalue['additional_info_box']);


           

           if($keyvalue['adi_SQ_A_ID']==0){
                 $insert_subqueansweradd = $obj->insert($lfs_fdi_additionalinfobox_field,$lfs_fdi_additionalinfobox_value,'lfs_fdi_additionalinfobox');

                $result_array[$b]['result']='Insert';
                $result_array[$b]['adi_SQ_A_ID']=$insert_subqueansweradd;
                $result_array[$b]['ID']=$keyvalue['ID'];

           }else{

                $Update_subqueansweradd = $obj->update($lfs_fdi_additionalinfobox_field,$lfs_fdi_additionalinfobox_value,'adi_SQ_A_ID = '.$door_installation_survey['adi_SQ_A_ID'],'lfs_fdi_additionalinfobox');

                $result_array[$b]['result']='Update';
                $result_array[$b]['adi_SQ_A_ID']=$keyvalue['adi_SQ_A_ID'];
                $result_array[$b]['ID']=$keyvalue['ID'];
           }
           $b++;
          }
        }

      echo json_encode(array('result'=>'update','installation_door_ID'=>$door_installation_survey['installation_door_ID'],'Local_Installation_Door_ID'=>$door_installation_survey['Local_Installation_Door_ID'],'additionalInfo'=>$result_array));

}
}else{
  echo json_encode(0);
}

  
?>
