<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class TxBranchStatus{

   public function update_multi($stage,$ids ,$username)
   {
      $CI =& get_instance();
      $this->db = $CI->load->database("onedev",true);
      $s_ids = join(",",$ids);
      $sql = "select T_OrderDetailT_TestID, T_OrderDetailT_OrderHeaderID 
         from 
         t_orderdetail 
         where T_OrderDetailID in ($s_ids)  ";
      $qry = $this->db->query($sql);
      $incomingRefID = 0;
      $s_detail_ids = "0";
      $s_child_ids = "0";
      $headerID = 0;

      if ($qry) {
         $rows = $qry->result_array();
         if(count($rows) > 0 ) {
            $headerID = $rows[0]["T_OrderDetailT_OrderHeaderID"];
            $s_test = "0";
            foreach($rows as $r) {
               $s_test .= "," . $r["T_OrderDetailT_TestID"];
            }
            $sql = "select incomingRefDetailID , incomingRefDetailIncomingRefID
               from incoming_ref_detail
               where incomingRefDetailNewT_OrderHeaderID = ?
               and incomingRefDetailT_TestID in ($s_test) ";
            $qryd = $this->db->query($sql, array($headerID));
            $flag_found = false;
            if($qryd) {
               $rowsd = $qryd->result_array();
               if ( count($rowsd) > 0 ) {
                  $incomingRefID = $rowsd[0]["incomingRefDetailIncomingRefID"];
                  foreach($rowsd as $r) {
                     $s_detail_ids .= "," . $r["incomingRefDetailID"];
                  }
               }
            } 
            //check child
            if(! $flag_found ) {
               $sql = "select incomingRefChildID , incomingRefChildIncomingRefID
                  from incoming_ref_child
                  where incomingRefChildNewT_OrderHeaderID = ?
                  and incomingRefChildT_TestID in ($s_test) ";
               $qryd = $this->db->query($sql, array($headerID));
               if($qryd) {
                  $rowsd = $qryd->result_array();
                  if ( count($rowsd) > 0 ) {
                     $incomingRefID = $rowsd[0]["incomingRefChildIncomingRefID"];
                     foreach($rowsd as $r) {
                        $s_child_ids .=  "," . $r["incomingRefChildID"];
                     }
                  }
               }
            } 
         }
      }
      if ($s_detail_ids == "0" && $s_child_ids == "0" ) {
         return false;
      } 
      $sql = "select M_BranchID, M_BranchIPAddress
         from m_branch
         join incoming_ref
         on M_BranchID = incomingRefM_BranchID
         where incomingRefID = ?";
      $qry = $this->db->query($sql, array($incomingRefID));
      $branchID = 0;
      if ($qry) {
         $rows = $qry->result_array();
         if(count($rows)>0) {
            $branchID = $rows[0]["M_BranchID"];
            $branchIPAddress = $rows[0]["M_BranchIPAddress"];
         }
      }   
      if ($branchID == 0 ) {
         return false;
      }  
      $val_note = "";
      $val_note_int = "";
      if ($headerID > 0 ) {
         $sql = "select T_OrderHeaderAddOnValidationNote, T_OrderHeaderAddOnValidationInternal
            from t_orderheaderaddon
            where T_orderHeaderAddOnT_OrderHeaderID = ?";
         $qry = $this->db->query($sql,array($headerID));
         if ($qry) {
            $rows = $qry->result_array();
            if (count($rows) > 0 ) {
               $val_note = $rows[0]["T_OrderHeaderAddOnValidationNote"];
               $val_note_int  = $rows[0]["T_OrderHeaderAddOnValidationInternal"];
               if ( $val_note == null ) $val_note = "";
               if ( $val_note_int == null ) $val_note_int = "";
            }
         } 
      }
      $note = "$stage by $username";
      if ($s_detail_ids!= "0" ) {
         $sql = "select 
            incomingRefT_RefDeliveryOrderID, 
            incomingRefDetailT_OrderDetailID,
            incomingRefDetailStatus,
            T_OrderDetailResult,
            T_OrderDetailNat_NormalValueID,
            T_OrderDetailVerification,
            T_OrderDetailValidation,
            T_OrderDetailNote, 
            ? note,
            ? validation_note,
            ? validation_note_internal
            from incoming_ref_detail
            join incoming_ref on incomingRefID = incomingRefDetailIncomingRefID
            and incomingRefDetailID in ($s_detail_ids)
            left join t_orderdetail on incomingRefDetailNewT_OrderHeaderID = T_OrderDetailT_OrderHeaderID
            and T_OrderDetailIsActive = 'Y' and incomingRefDetailT_TestID = T_OrderDetailT_TestID";
         $qry = $this->db->query($sql, array($note,$val_note,$val_note_int));
         if ($qry) {
            $rows = $qry->result_array();
            $param = json_encode($rows);
            //insert to 
            $sql = "insert into tx_branch_status(TxBranchStatusStage, TxBranchStatusM_BranchID,
               TxBranchStatusM_BranchIP, TxBranchStatusJson,TxBranchStatusNote )
               values (?,?,?,?,?)";
            $qry = $this->db->query($sql, array($stage,$branchID, $branchIPAddress, $param, $note));
         }
      }   
      if ($s_child_ids!= "0" ) {
         $sql = "select 
            incomingRefT_RefDeliveryOrderID, 
            incomingRefChildT_OrderDetailID incomingRefDetailT_OrderDetailID,
            '' incomingRefDetailStatus,
            T_OrderDetailResult,
            T_OrderDetailNat_NormalValueID,
            T_OrderDetailVerification,
            T_OrderDetailValidation,
            T_OrderDetailNote, 
            ? note,
            ? validation_note,
            ? validation_note_internal
            from incoming_ref_child
            join incoming_ref on incomingRefID = incomingRefChildIncomingRefID
            and incomingRefChildID in ($s_child_ids)
            left join t_orderdetail on incomingRefChildNewT_OrderHeaderID = T_OrderDetailT_OrderHeaderID
            and T_OrderDetailIsActive = 'Y' and incomingRefChildT_TestID = T_OrderDetailT_TestID";
         $qry = $this->db->query($sql, array($note,$val_note,$val_note_int));
         if ($qry) {
            $rows = $qry->result_array();
            $param = json_encode($rows);
            //insert to 
            $sql = "insert into tx_branch_status(TxBranchStatusStage, TxBranchStatusM_BranchID,
               TxBranchStatusM_BranchIP, TxBranchStatusJson,TxBranchStatusNote )
               values (?,?,?,?,?)";
            $qry = $this->db->query($sql, array($stage,$branchID, $branchIPAddress, $param, $note));
         }   
      }
   }

   public function update($stage,$orderDetailID,$username)
   {
      $CI =& get_instance();
      $this->db = $CI->load->database("onedev",true);
      $sql = "select T_OrderDetailT_TestID, T_OrderDetailT_OrderHeaderID 
         from 
         t_orderdetail 
         where T_OrderDetailID = ? ";
      $qry = $this->db->query($sql, array($orderDetailID));
      $incomingRefID = 0;
      $incomingRefDetailID = 0;
      $flag_child = false;
      if ($qry) {
         $rows = $qry->result_array();
         $flag_found = false;
         if(count($rows) > 0 ) {
            $headerID = $rows[0]["T_OrderDetailT_OrderHeaderID"];
            $testID = $rows[0]["T_OrderDetailT_TestID"];
            $sql = "select incomingRefDetailID , incomingRefDetailIncomingRefID
               from incoming_ref_detail
               where incomingRefDetailNewT_OrderHeaderID = ?
               and incomingRefDetailT_TestID = ?";
            $qryd = $this->db->query($sql, array($headerID,$testID));
            if($qryd) {
               $rowsd = $qryd->result_array();
               if ( count($rowsd) > 0 ) {
                  $incomingRefID = $rowsd[0]["incomingRefDetailIncomingRefID"];
                  $incomingRefDetailID = $rowsd[0]["incomingRefDetailID"];
                  $flag_found = true;
               }
            }
            //check child
            if(! $flag_found ) {
               $sql = "select incomingRefChildID , incomingRefChildIncomingRefID
                  from incoming_ref_child
                  where incomingRefChildNewT_OrderHeaderID = ?
                  and incomingRefChildT_TestID = ?";
               $qryd = $this->db->query($sql, array($headerID,$testID));
               if($qryd) {
                  $rowsd = $qryd->result_array();
                  if ( count($rowsd) > 0 ) {
                     $incomingRefID = $rowsd[0]["incomingRefChildIncomingRefID"];
                     $incomingRefDetailID = $rowsd[0]["incomingRefChildID"];
                     $flag_found = true;
                     $flag_child = true;
                  }
               }
            } 
         }
      }
      if ($incomingRefDetailID == 0 || $incomingRefDetailID == "") {
         return false;
      } 
      $sql = "select M_BranchID, M_BranchIPAddress
         from m_branch
         join incoming_ref
         on M_BranchID = incomingRefM_BranchID
         where incomingRefID = ?";
      $qry = $this->db->query($sql, array($incomingRefID));
      $branchID = 0;
      if ($qry) {
         $rows = $qry->result_array();
         if(count($rows)>0) {
            $branchID = $rows[0]["M_BranchID"];
            $branchIPAddress = $rows[0]["M_BranchIPAddress"];
         }
      }   
      if ($branchID == 0 ) {
         return false;
      }  

      $val_note = "";
      $val_note_int = "";
      if ($headerID > 0 ) {
         $sql = "select T_OrderHeaderAddOnValidationNote, T_OrderHeaderAddOnValidationInternal
            from t_orderheaderaddon
            where T_orderHeaderAddOnT_OrderHeaderID = ?";
         $qry = $this->db->query($sql,array($headerID));
         if ($qry) {
            $rows = $qry->result_array();
            if (count($rows) > 0 ) {
               $val_note = $rows[0]["T_OrderHeaderAddOnValidationNote"];
               $val_note_int = $rows[0]["T_OrderHeaderAddOnValidationInternal"];
               if ( $val_note == null ) $val_note = "";
               if ( $val_note_int == null ) $val_note_int = "";
            }
         } 
      }
      $note = "$stage by $username";
      $sql = "select 
         incomingRefT_RefDeliveryOrderID, 
         incomingRefDetailT_OrderDetailID,
         incomingRefDetailStatus,
         T_OrderDetailResult,
         T_OrderDetailNat_NormalValueID,
         T_OrderDetailVerification,
         T_OrderDetailValidation,
         T_OrderDetailNote, 
         ? note,
         ? validation_note,
         ? validation_note_internal
         from incoming_ref_detail
         join incoming_ref on incomingRefID = incomingRefDetailIncomingRefID
         and incomingRefDetailID = ?
         left join t_orderdetail on incomingRefDetailNewT_OrderHeaderID = T_OrderDetailT_OrderHeaderID
         and T_OrderDetailIsActive = 'Y' and incomingRefDetailT_TestID = T_OrderDetailT_TestID";
      if ($flag_child ) {
         $sql = "select 
            incomingRefT_RefDeliveryOrderID, 
            incomingRefChildT_OrderDetailID incomingRefDetailT_OrderDetailID,
            '' incomingRefDetailStatus,
            T_OrderDetailResult,
            T_OrderDetailNat_NormalValueID,
            T_OrderDetailVerification,
            T_OrderDetailValidation,
            T_OrderDetailNote, 
            ? note,
            ? validation_note,
            ? validation_note_internal
            from incoming_ref_child
            join incoming_ref on incomingRefID = incomingRefChildIncomingRefID
            and incomingRefChildID = ?
            left join t_orderdetail on incomingRefChildNewT_OrderHeaderID = T_OrderDetailT_OrderHeaderID
            and T_OrderDetailIsActive = 'Y' and incomingRefChildT_TestID = T_OrderDetailT_TestID";
      }
      $qry = $this->db->query($sql, array($note, $val_note, $val_note_int, $incomingRefDetailID));
      if ($qry) {
         $rows = $qry->result_array();
         $param = json_encode($rows);
         //insert to 
         $sql = "insert into tx_branch_status(TxBranchStatusStage, TxBranchStatusM_BranchID,
            TxBranchStatusM_BranchIP, TxBranchStatusJson,TxBranchStatusNote )
            values (?,?,?,?,?)";
         $qry = $this->db->query($sql, array($stage,$branchID, $branchIPAddress, $param, $note));
      }   
   }
}
