db = $this->load->database('regional', true); $this->db = $this->load->database('onedev', true); } function create($mouID) { $sql = "select * from m_mou where M_MouID = ?"; $qry = $this->db->query($sql, array($mouID)); if (! $qry ) { echo 'Err : ' . print_r($this->db->error(),true). '\n'; exit; } $rows = $qry->result_array(); if (count($rows) == 0 ) { echo "Err : No Mou Found!"; exit; } $mouName = $rows[0]["M_MouName"]; $companyID = $rows[0]["M_MouM_CompanyID"]; echo "$mouName \n"; //Test kecuali Profile $sql = "select distinct T_PriceM_MouID, T_TestID, T_TestName, 'N' IsFromPanel, Nat_TestID, T_PriceT_TestID, T_PriceIsCito, T_PriceM_CompanyID, T_PriceM_MouID, T_PricePriority, T_PriceAmount, T_PriceDisc, T_PriceDiscRp, T_PriceSubTotal, T_PriceOther, T_PriceTotal, T_TestForceSell, 'N' is_packet, 0 packet_id, 'PX' px_type, '[]' nat_test, '[]' child_test, 'N' IsFavourite, Nat_TestNat_TestTypeID, T_TestSasCode, $mouID Ss_PriceMouM_MouID from t_price join t_test on T_PriceT_TestID = T_TestID and T_PriceIsActive = 'Y' and T_TestIsActive = 'Y' and T_TestIsPrice = 'Y' join nat_test on T_TestNat_TestID = Nat_TestID and Nat_TestIsActive = 'Y' and Nat_TestNat_TestTypeID <> 5 where T_PriceM_MouID = ? and length(T_TestSasCode) = 8 "; $qry = $this->db->query($sql, array($mouID)); if (! $qry ) { echo 'Err : ' . print_r($this->db->error(),true). '\n'; exit; } $rows = $qry->result_array(); $flag_error = false; foreach($rows as $idx => $r) { $nat_testType = $r["Nat_TestNat_TestTypeID"]; switch($nat_testType) { case 1: //Single $rows[$idx]['nat_test'] = '[' . $r['Nat_TestID'] . ']'; break; case 3: //Multi case 4: //Panel $sasCode = $r["T_TestSasCode"] . '%'; $sql = "select T_TestNat_TestID from t_test where T_TestIsResult = 'Y' and T_TestSasCode like ? and T_TestIsActive = 'Y'"; $qry = $this->db->query($sql,array($sasCode)); if (!$qry ) { echo "Err : " . print_r($this->db->error(),true) . "\n"; $flag_error = true; } $nt_rows = $qry->result_array(); $t_rows = array($r["Nat_TestID"]); foreach($nt_rows as $nr) { $t_rows[] = $nr["T_TestNat_TestID"]; } $rows[$idx]['nat_test'] = "[" . join(",",$t_rows) . "]"; break; default : $rows[$idx]['nat_test'] = '[' . $r['Nat_TestID'] . ']'; break; } unset($rows[$idx]["Nat_TestNat_TestTypeID"]); unset($rows[$idx]["T_TestSasCode"]); } //Test Profile $sql = "select distinct $mouID T_PriceM_MouID, T_TestID, T_TestName, 'N' IsFromPanel, Nat_TestID, T_TestID T_PriceT_TestID, 'N' T_PriceIsCito, $companyID T_PriceM_CompanyID, $mouID T_PriceM_MouID, 0 T_PricePriority, 0 T_PriceAmount, 0 T_PriceDisc, 0 T_PriceDiscRp, 0 T_PriceSubTotal, 0 T_PriceOther, 0 T_PriceTotal, T_TestForceSell, 'N' is_packet, 0 packet_id, 'PXR' px_type, '[]' nat_test, '[]' child_test, 'N' IsFavourite, Nat_TestNat_TestTypeID, T_TestSasCode, $mouID Ss_PriceMouM_MouID from t_test join nat_test on T_TestNat_TestID = Nat_TestID and Nat_TestIsActive = 'Y' and Nat_TestNat_TestTypeID = 5 where length(T_TestSasCode) = 8 "; $qry = $this->db->query($sql, array($mouID)); if (! $qry ) { echo 'Err : ' . print_r($this->db->error(),true). '\n'; exit; } $p_rows = $qry->result_array(); $flag_error = false; foreach($p_rows as $idx => $r) { $T_TestName= $r["T_TestName"] ; $sasCode = $r["T_TestSasCode"]; $sql = "select T_PriceM_MouID, T_TestID, T_TestName, 'N' IsFromPanel, Nat_TestID, T_PriceT_TestID, T_PriceIsCito, T_PriceM_CompanyID, T_PriceM_MouID, T_PricePriority, T_PriceAmount, T_PriceDisc, T_PriceDiscRp, T_PriceSubTotal, T_PriceOther, T_PriceTotal, T_TestForceSell, 'N' is_packet, 0 packet_id, 'PX' px_type, '[]' nat_test, '[]' child_test, 'N' IsFavourite, Nat_TestNat_TestTypeID, T_TestSasCode from t_price join t_test on T_PriceT_TestID = T_TestID and T_PriceIsActive = 'Y' and T_TestIsActive = 'Y' and T_TestIsPrice = 'Y' and T_PriceIsCito = 'N' and T_TestSasCode like ? and length(T_TestSasCode) = 10 join nat_test on T_TestNat_TestID = Nat_TestID and Nat_TestIsActive = 'Y' where T_PriceM_MouID = ? "; $qry = $this->db->query($sql,array($sasCode . '%',$mouID)); if (!$qry ) { echo "Err : " . print_r($this->db->error(),true) . "\n"; $flag_error = true; } $ct_rows = $qry->result_array(); $p_nat_test = array(); foreach($ct_rows as $ct_idx => $cr) { $sasCode = $cr["T_TestSasCode"] . '%'; $sql = "select T_TestNat_TestID from t_test where ( T_TestIsResult = 'Y' or T_TestIsPrice = 'Y' ) and T_TestSasCode like ? and T_TestIsActive = 'Y'"; $qry = $this->db->query($sql,array($sasCode)); if (!$qry ) { echo "Err : " . print_r($this->db->error(),true) . "\n"; $flag_error = true; } $nt_rows = $qry->result_array(); $t_rows = array(); foreach($nt_rows as $nr) { $t_rows[] = $nr["T_TestNat_TestID"]; $p_nat_test[]=$nr["T_TestNat_TestID"]; } $ct_rows[$ct_idx]['nat_test'] = "[" . join(",",$t_rows) . "]"; } if (count($ct_rows) > 0 ) { $x_arr = array(); foreach($ct_rows as $x_cr) { $x_arr[] = $x_cr; } $p_rows[$idx]['child_test'] = json_encode($x_arr,true); $p_rows[$idx]['nat_test'] = "[" . join( ",", $p_nat_test) . "]"; unset($p_rows[$idx]["Nat_TestNat_TestTypeID"]); unset($p_rows[$idx]["T_TestSasCode"]); } else { unset($p_rows[$idx]); } } //Paket Panel / Profile $sql = "select distinct $mouID T_PriceM_MouID, T_PacketID T_TestID, T_PacketName T_TestName, 'N' IsFromPanel, 0 Nat_TestID, 0 T_PriceT_TestID, 'N' T_PriceIsCito, $companyID T_PriceM_CompanyID, $mouID T_PriceM_MouID, 0 T_PricePriority, T_PacketOriginalPrice T_PriceAmount, 0 T_PriceDisc, (T_PacketOriginalPrice - T_PacketPrice) T_PriceDiscRp, 0 T_PriceSubTotal, 0 T_PriceOther, T_PacketPrice T_PriceTotal, 'Y' T_TestForceSell, 'Y' is_packet, T_PacketID packet_id, T_PacketType px_type, '[]' nat_test, '[]' child_test, 'N' IsFavourite, $mouID Ss_PriceMouM_MouID from t_packet where T_PacketIsActive = 'Y' and T_PacketM_MouID = ?"; $qry = $this->db->query($sql, array($mouID)); if (! $qry ) { echo 'Err : ' . print_r($this->db->error(),true). '\n'; exit; } $pn_rows = $qry->result_array(); foreach($pn_rows as $idx => $pnr) { $packetID = $pnr["packet_id"]; //child test $sql = "select $mouID T_PriceM_MouID, T_TestID, T_TestName, 'N' IsFromPanel, Nat_TestID, T_TestID T_PriceT_TestID, 'N' T_PriceIsCito, $companyID T_PriceM_CompanyID, $mouID T_PriceM_MouID, 0 T_PricePriority, T_PacketDetailPrice T_PriceAmount, 0 T_PriceDisc, 0 T_PriceDiscRp, T_PacketDetailPrice T_PriceSubTotal, 0 T_PriceOther, T_PacketDetailPrice T_PriceTotal, 'Y' T_TestForceSell, 'N' is_packet, 0 packet_id, 'PX' px_type, '[]' nat_test, '[]' child_test, 'N' IsFavourite, T_TestSasCode from t_packetdetail join t_test on T_PacketDetailT_TestID = T_TestID and T_PacketDetailIsActive = 'Y' and T_TestIsActive = 'Y' and T_PacketDetailT_PacketID = ? join nat_test on T_TestNat_TestID = Nat_TestID and Nat_TestIsActive = 'Y' "; $qry = $this->db->query($sql,array($packetID)); //echo $this->db->last_query() . "\n"; exit; if (!$qry ) { echo "Err : " . print_r($this->db->error(),true) . "\n"; $flag_error = true; exit; } $ct_rows = $qry->result_array(); $p_nat_test = array(); foreach($ct_rows as $ct_idx => $cr) { $sasCode = $cr["T_TestSasCode"] . '%'; $sql = "select distinct T_TestNat_TestID from t_test where T_TestSasCode like ? and T_TestIsActive = 'Y'"; $qry = $this->db->query($sql,array($sasCode)); if (!$qry ) { echo "Err : " . print_r($this->db->error(),true) . "\n"; $flag_error = true; } $nt_rows = $qry->result_array(); $t_rows = array(); foreach($nt_rows as $nr) { $t_rows[] = intval( $nr["T_TestNat_TestID"]); $p_nat_test[]= intval( $nr["T_TestNat_TestID"]); } $ct_rows[$ct_idx]['nat_test'] = json_encode($t_rows,JSON_NUMERIC_CHECK); } if (count($ct_rows) > 0 ) { $x_arr = array(); foreach($ct_rows as $x_cr) { $x_arr[] = $x_cr; } $pn_rows[$idx]['child_test'] = json_encode($x_arr,true); $pn_rows[$idx]['nat_test'] = json_encode($p_nat_test,true); } unset($pn_rows[$idx]["Nat_TestNat_TestTypeID"]); unset($pn_rows[$idx]["T_TestSasCode"]); } $rows = array_merge($rows,$p_rows, $pn_rows); echo "Inserting " . count($rows) . " records\n"; $qry = $this->db->query("delete from ss_price_mou_v3 where Ss_PriceMouM_MouID=?", array($mouID)); if ( ! $qry ) { echo "Err : " . print_r($this->db->error(), true) . "\n"; exit; } $qry = $this->db->insert_batch("ss_price_mou_v3", $rows); if ( ! $qry ) { echo "Err : " . print_r($this->db->error(), true) . "\n"; exit; } echo "Done"; } function upload($prm_date) { $sql = "select * from ssr_mr03 where date(SsrMr03T_OrderHeaderDate) = ? and SsrMr03IsSent = 'N' and SsrMr03Retry < 10"; $qry = $this->db->query($sql,array($prm_date)); if (! $qry) { echo "Err : " . print_r($this->db->error(),true); exit; } $rows = $qry->result_array(); $data = json_encode($rows); $md5 = md5($data); $param = array("data" => $data, "md5" => $md5); $jparam = json_encode($param); $url = "http://bandungraya.aplikasi.web.id/one-api/tools/marketing/r_mr03"; $url = "http://devbandungraya.aplikasi.web.id/one-api/tools/marketing/r_mr03"; echo "Uploading : $prm_date, total " . count($rows) . " records\n"; $j_result = $this->post($url,$jparam); $result = json_decode($j_result,true); $sql = "update ssr_mr03 set SsrMr03Retry = SsrMr03Retry + 1 where date(SsrMr03T_OrderHeaderDate) = ?"; $this->db->query($sql,array($prm_date)); if ($result["status"] == "OK") { $sql = "update ssr_mr03 set SsrMr03IsSent = 'Y' where SsrMr03ID = ?"; foreach($result["SsrMr03ID"] as $id ) { $this->db->query($sql, array($id)); } echo $result["status"] . ", total " . count($result["SsrMr03ID"]) . "\n"; exit; } echo "ERR : " . $result["message"] . "\n"; } function post($url,$data) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data)) ); $result = curl_exec($ch); return $result; } }