load->library('session'); $this->load->library('rhlib'); } function retValOrNull($val){ //especially for combo & looukup with no item selected $val = ($val=='')? null : $val; return $val; } function get_tbpti(){ $q = "SELECT * FROM v_tbpti ORDER BY kdptitbpti"; $this->rhlib->jsonFromQueryWLimit($q); } function get_stptitbkod(){ $q = "SELECT kdkodtbkod AS kode, nmkodtbkod AS nama FROM tbkod where kdapltbkod='13'"; $this->rhlib->jsonFromQuery($q); } function insert_tbpti(){ $dataArray = $this->getFieldsAndValues(); $ret['tbptipriv'] = $this->rhlib->insertRecord('tbpti',$dataArray); $ret['tbptipub'] = $this->rhlib->insertRecordPublic('tbpti',$dataArray); if ($ret['tbptipriv'] && $ret['tbptipub']) { $ret["success"]=true; } else { $ret["success"]=false; } echo json_encode($ret); } function update_tbpti(){ $fieldsAndValues = $this->getFieldsAndValues(); //UPDATE $where['kdptitbpti'] = $_POST['kdptitbpti']; $this->db->where('kdptitbpti', $_POST['kdptitbpti']); $this->db->update('tbpti', $fieldsAndValues); $ret["success"]=true; if($this->db->affected_rows()){ $ret["success"]=true; $ret["msg"]='Update Data Berhasil'; $ret["num"]=$this->db->affected_rows(); $this->rhlib->updateRecordPublic('tbpti',$fieldsAndValues,$where); } echo json_encode($ret); } function delete_tbpti(){ $where['kdptitbpti'] = $_POST['kdptitbpti']; $ret = $this->rhlib->deleteRecord('tbpti',$where); $this->rhlib->deleteRecordPublic('tbpti',$where); return $ret; } function getFieldsAndValues(){ $dataArray = array( 'kdptitbpti'=> $this->retValOrNull($_POST['kdptitbpti']), 'nmptitbpti'=> $this->retValOrNull($_POST['nmptitbpti']), 'tgenttbpti'=> $this->retValOrNull($_POST['tgenttbpti']), 'tgtuptbpti'=> $this->retValOrNull($_POST['tgtuptbpti']), 'kotaatbpti'=> $this->retValOrNull($_POST['kotaatbpti']), 'kdstatbpti'=> $this->retValOrNull($_POST['kdstatbpti']), 'kdkabtbpti'=> $this->retValOrNull($_POST['kdkabtbpti']), ); return $dataArray; } //import data excel function getcountdata($where,$tbl){ $this->db->select("*"); $this->db->from($tbl); $this->db->where($where); $query = $this->db->get(); return $query->num_rows(); } function getcountdatapublic($where,$tbl){ $KEDUA= $this->load->database('second', TRUE); $KEDUA->select("*"); $KEDUA->from($tbl); $KEDUA->where($where); $query = $KEDUA->get(); return $query->num_rows(); } function update_import($data,$where,$tbl){ $this->db->where($where); $this->db->update($tbl, $data); if($this->db->affected_rows()){ $ret["success"]=true; $ret["msg"]='Update Data Berhasil'; $ret["num"]=$this->db->affected_rows(); }else{ $ret["success"]=false; $ret["msg"]= 'Update Data Gagal'; } return $ret; } function import_from_Excel(){ $ori_dir = 'resources/files/'; $upload; if ($_FILES['f_excel_import_pti']['name']) { $upload = move_uploaded_file($_FILES['f_excel_import_pti']['tmp_name'], $ori_dir . $_FILES['f_excel_import_pti']['name']); } if ($upload) { include_once ( APPPATH."libraries/excel_reader2.php"); $file = $this->input->post("f_excel"); $data = new Spreadsheet_Excel_Reader('resources/files/'.$file); $baris = $data->rowcount($sheet_index=0); $countinsert = 0; $countupdate = 0; if (strtolower($data->val(1, 1)) =='kdptitbpti') { for ($i=2; $i<=$baris; $i++) { $temptgenttbpti = explode('/',substr($data->val($i, 2),0,10)); $tgenttbpti = ($data->val($i, 2)) ? $temptgenttbpti[2]."-".$temptgenttbpti[0]."-".$temptgenttbpti[1]:null; $temptgtuptbpti = explode('/',substr($data->val($i, 3),0,10)); $tgtuptbpti = ($data->val($i, 3)) ? $temptgtuptbpti[2]."-".$temptgtuptbpti[0]."-".$temptgtuptbpti[1]:null; $dataArray = array( 'kdptitbpti'=> $this->retValOrNull($data->val($i, 1)), 'tgenttbpti'=> $this->retValOrNull($tgenttbpti), 'tgtuptbpti'=> $this->retValOrNull($tgtuptbpti), 'nmptitbpti'=> $this->retValOrNull($data->val($i, 4)), 'kotaatbpti'=> $this->retValOrNull($data->val($i, 5)), 'kdstatbpti'=> $this->retValOrNull($data->val($i, 6)), 'kdkabtbpti'=> $this->retValOrNull($data->val($i, 7)) ); $where['kdptitbpti'] = $this->retValOrNull($data->val($i, 1)); //private $countdatapriv = $this->getcountdata($where,'tbpti'); if ($countdatapriv > 0) { $this->update_import($dataArray,$where,'tbpti'); } else { $privinsert = $this->rhlib->insertRecord('tbpti',$dataArray); } //public $countdatapub = $this->getcountdatapublic($where,'tbpti'); if ($countdatapub > 0) { $this->rhlib->updateRecordPublic('tbpti',$dataArray,$where); } else { $pubinsert = $this->rhlib->insertRecordPublic('tbpti',$dataArray); } if ($countdatapriv > 0 && $countdatapub > 0) { $countupdate = $countupdate + 1; } if ($privinsert && $pubinsert) { $countinsert = $countinsert + 1; } } $return["success"]=true; $return["countinsert"]=$countinsert; $return["countupdate"]=$countupdate; unlink('resources/files/'.$file); } else { $return["success"]=false; $return["countinsert"]=0; $return["countupdate"]=0; } echo json_encode($return); } } } ?>