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

   public function create($mouID) {
      $CI =& get_instance();
      $this->db = $CI->load->database("onedev",true);

      $sql = "select * from ss_price_mou where Ss_PriceMouM_MouID = ? and px_type in ('PR','PXR')";
      $qry = $this->db->query($sql, array($mouID));
      if ( ! $qry ) {
         return array(false, "Ss Price Mou " . print_r($this->db->error(),true));
      }
      $rows = $qry->result_array();
      $sql ="delete from ss_price_mou_px where Ss_PriceMouPxSs_PriceMouID in ( select Ss_PriceMouID from ss_price_mou
         where Ss_PriceMouM_MouID = ? )";
      $qry = $this->db->query($sql, array($mouID));
      if ( ! $qry ) {
         return array(false, "Clear Ss Price Mou Px" . print_r($this->db->error(),true));
      }
      $a_data = array();
      foreach($rows as $r) {
         $j_ct = $r["child_test"];
         $ct = json_decode($j_ct,true);
         foreach($ct as $c) {
            $a_data[] = array(
               "Ss_PriceMouPxM_MouID" => $r["Ss_PriceMouM_MouID"],
               "Ss_PriceMouPxSs_PriceMouID" => $r["Ss_PriceMouID"],
                "Ss_PriceMouPxT_TestID" => $c["T_TestID"],
                "Ss_PriceMouPxT_TestName" => $c["T_TestName"],
               "Ss_PriceMouPxT_PriceIsCito" => $c["T_PriceIsCito"],
                "Ss_PriceMouPxT_PriceM_CompanyID" => $c["T_PriceM_CompanyID"],
                "Ss_PriceMouPxT_PricePriority" => $c["T_PricePriority"],
               "Ss_PriceMouPxT_PriceAmount" => $c["T_PriceAmount"],
                "Ss_PriceMouPxT_PriceDisc" => $c["T_PriceDisc"],
                "Ss_PriceMouPxT_PriceDiscRp" => $c["T_PriceDiscRp"],
               "Ss_PriceMouPxT_PriceSubTotal" => $c["T_PriceSubTotal"],
                "Ss_PriceMouPxT_PriceOther" => $c["T_PriceOther"],
                "Ss_PriceMouPxT_PriceTotal" => $c["T_PriceTotal"],
               "Ss_PriceMouPxT_TestForceSell" => $c["T_TestForSell"],
                "nat_test" => $c["nat_test"]
            );
         }
      }
      $qry = $this->db->insert_batch("ss_price_mou_px",$a_data);
      if ( ! $qry ) {
         return array(false, "Batch Ss Price Mou Px" . print_r($this->db->error(),true));
      }

      return array(true, "");
   }
}
