/application/controllers/customer/operasi.php
PHP | 535 lines | 363 code | 42 blank | 130 comment | 48 complexity | 4f5ffe471fe12a867ad4a094e593fdd4 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0, LGPL-2.1, GPL-3.0
- <?php
- class operasi extends CI_Controller
- {
- /*------------------------------------------------------------------------------*/
- /* Beginning of master data Operasi */
- /*------------------------------------------------------------------------------*/
- private $IdNegara;
-
- function __construct()
- {
- parent::__construct();
- $this->load->library(array("jqlib","mnuauth"));
- $this->load->helper(array("form","download"));
- $this->lang->load("abdi");
- $webserverpath= "/www/crm_test/assets/restricted";
- $serverpath= "./assets/restricted";
- $params= array("webserverpath"=>$webserverpath,"serverpath"=>$serverpath);
- $this->load->library("ftplib", $params);
-
- $this->IdNegara= $this->session->userdata("idNegara");
- $this->path_self = $this->uri->segment("2") . "/" . __CLASS__ . "/";
-
- if($this->session->userdata('login')!==TRUE){
- redirect($this->configlib->site_url().'/login');
- }
- ini_set('max_execution_time', 86400);
- }
-
- function cekdir(){
- $link= opendir("./assets/restricted");
- while( $file= readdir($link) ){
- echo $file . "<br/>";
- }
- }
-
- function import()
- {
- show_template($this->path_self . "vImport");
- }
-
- function do_import(){
- foreach($_FILES as $key=>$val){
- foreach( $val as $subkey=>$subval ){
- $$subkey= $subval;
- }
- }
-
- $info= pathinfo($name);
- foreach( $info as $key=>$value ){
- $$key= $value;
- }
-
- if( !preg_match("#^[\w \.!@\#\$\%\^&\-\=\+\{\}\[\];',]+$#", $filename) ){
- echo lang("abdi.alert_45");
- exit;
- }
-
- $IdKlinik= $this->session->userdata('IdKlinik');
- $username= $this->session->userdata('username');
- $name= $IdKlinik . "-" . $username . ".csv";
- #$name= "customer.csv";
-
- $fullpath= "./assets/restricted/" . $name;
- #$fullpath= "D:/webroot/assets/restricted/" . $name;
- /*
- echo $tmp_name . "<br/>";
- echo $fullpath;
- exit;
- */
- #$fullpath= "./assets/restricted/produk_harga.csv";
- //$maxsize= "2048000";
- if( !empty($name) ){
- if( !preg_match("#^(csv)$#", $extension ) ){
- echo "File isn't document !";
- exit();
- }else{
- if( move_uploaded_file($tmp_name, $fullpath) ){
- /*
- if( move_uploaded_file($_FILES['file']['tmp_name'], $fullpath) ){
- exit;
- */
- // Open Upload File
- $fp= fopen($fullpath, 'r');
-
- $tblname= "";
- $pmkey= "";
- while( !feof($fp) ) {
- $buffer= fgets($fp);
- if( !empty($buffer) ){
- if( preg_match('#^\-.+$#', $buffer) ){
- $tblname= preg_replace(array("/-/","/;/","/ /"),array("","",""),$buffer);
- }elseif( preg_match('#^\*.+$#', $buffer) ){
- #$pmkey= preg_replace(array("/\*/","/;/"),array("",""),$buffer);
- }else{
- #echo $buffer . "<br/>";
- #$buffer= substr($buffer, 0, -2);
- $tblname= trim($tblname);
-
- $fields= $this->db->field_data(trim($tblname));
- $arrfield= array();
- $fieldname= "";
- $pmkey= "";
- foreach( $fields as $field ){
- if( $field->primary_key == 1 && $field->type != 'int'){
- $pmkey.= $field->name . ",";
- $fieldtype= $field->type;
- $fieldname= $field->name;
- $arrfield[]= $fieldname;
- }
- if( $field->primary_key != 1 ){
- $fieldname= $field->name;
- $arrfield[]= $fieldname;
- }
- }
-
- #print_r($arrfield);
-
- $pmkey= substr($pmkey, 0, -1);
- $arrpm= explode(",", $pmkey); // Array Primary Key
- $piedata= explode(";", $buffer); // Array Data
-
- $insdata= array();
- foreach( $piedata as $key=>$value ){
- $value= trim($value);
- #echo $key . "<br/>";
- if( array_key_exists($key, $arrfield) ){
- #echo $arrfield[$key] . "<br/>";
- $nmfield= $arrfield[$key];
- if( strlen($value) ){
- /*
- $value= str_replace("&semicolon&", ";", $value);
- #$value= preg_replace(array('/\s+/', '/&semicolon&/'), array('',';'), $value);
- */
- $value= str_replace("&semicolon&", ";", $value);
- $insdata[$nmfield]= $value;
- }else{
- $insdata[$nmfield]= '';
- }
- /* Old Script
- if( !empty($value) ){
- $value= str_replace("&semicolon&", ";", $value);
- $insdata[$nmfield]= $value;
- }else{
- $insdata[$nmfield]= '';
- }
- */
- }
-
- /*
- print_r($insdata);
- echo "<br/>";
- if( !empty($key) ){
- }
- */
- }
-
- if( !empty($pmkey) ){
- // Jika Primary key lebih dari 1
- $where= array();
- foreach( $arrpm as $key=>$value ){
- $where[$value]= $insdata[$value];
- }
-
- if( $tblname == "shipping_db" ){
- $stwhere= "";
- foreach( $where as $key=>$value ){
- $stwhere.= $key . "='" . $value . "' AND ";
- }
- $stwhere= substr($stwhere, 0, -5);
- $stwhere.= " OR ";
- $endwhere= "";
- $endwhere.= "Source='" . $where['Destination'] . "' AND Destination='" . $where['Source'] . "'";
- $where= "";
- $where= "" . $stwhere . $endwhere . "";
- $this->db->where($where, NULL, FALSE);
- }else{
- $this->db->where($where);
- }
-
- $jml= $this->db->get($tblname)->num_rows();
- if( empty($jml) ){
- $this->Gmodel->add($tblname,$insdata);
- #$this->db->insert($tblname, $insdata);
- #echo "insert" . "<br/>";
- }else{
- if( $tblname == "shipping_db" ){
- $this->db->where($where, NULL, FALSE);
- }else{
- $this->db->where($where);
- }
- $this->db->update($tblname, $insdata);
- #echo "update" . "<br/>";
- }
- }else{
- $this->Gmodel->add($tblname,$insdata);
- #$this->db->insert($tblname, $insdata);
- #echo "insert" . "<br/>";
- }
- }
- }
- }
- fclose($fp);
-
- echo "Import Data is successful";
- unlink($fullpath);
- exit;
- }
- }
- }
- }
-
- function import_file(){
-
- foreach($_FILES as $key=>$val){
- foreach( $val as $subkey=>$subval ){
- $$subkey= $subval;
- }
- }
-
- $name= "marketing.csv";
- $name= "produk_harga.csv";
-
- $info= pathinfo($name);
- foreach( $info as $key=>$value ){
- $$key= $value;
- }
-
- if( !preg_match("#^[\w \.!@\#\$\%\^&\-\=\+\{\}\[\];',]+$#", $filename) ){
- echo lang("abdi.alert_45");
- exit;
- }
-
- $IdKlinik= $this->session->userdata('IdKlinik');
- $username= $this->session->userdata('username');
- #$name= $IdKlinik . "-" . $username . ".csv";
- #$name= "upload_cus.csv";#"customer.csv";
-
- #$fullpath= "./assets/restricted/" . $name;
- $fullpath= "./assets/restricted/02001-joko.csv";
- //$maxsize= "2048000";
- if( !empty($name) ){
- if( !preg_match("#^(csv)$#", $extension ) ){
- echo "File isn't document !";
- exit();
- }else{
- #if( move_uploaded_file($tmp_name, $fullpath) ) {
- // Open Upload File
- $fp= fopen($fullpath, 'r');
-
- $tblname= "";
- $pmkey= "";
- while( !feof($fp) ) {
- $buffer= fgets($fp);
-
- if( !empty($buffer) ){
- if( preg_match('#^\-.+$#', $buffer) ){
- $tblname= preg_replace(array("/-/","/;/","/ /"),array("","",""),$buffer);
- echo $tblname . "<br/>";
- }elseif( preg_match('#^\*.+$#', $buffer) ){
- #$pmkey= preg_replace(array("/\*/","/;/"),array("",""),$buffer);
- }else{
- #echo $buffer . "<br/>";
- #$buffer= substr($buffer, 0, -2);
- $tblname= trim($tblname);
- if( $tblname == 'konsumen_keluarga' ){
- /*
- echo $buffer . "\n";
- echo strlen($nmfield);
- echo "\n";
- echo $buffer . "\n";
- */
- }
- $fields= $this->db->field_data(trim($tblname));
- $arrfield= array();
- $fieldname= "";
- $pmkey= "";
- foreach( $fields as $field ){
- if( $field->primary_key == 1 && $field->type != 'int'){
- $pmkey.= $field->name . ",";
- $fieldtype= $field->type;
- $fieldname= $field->name;
- $arrfield[]= $fieldname;
- }
- if( $field->primary_key != 1 ){
- $fieldname= $field->name;
- $arrfield[]= $fieldname;
- }
- }
- #print_r($arrfield);
-
- $pmkey= substr($pmkey, 0, -1);
- $arrpm= explode(",", $pmkey); // Array Primary Key
- $piedata= explode(";", $buffer); // Array Data
-
- $insdata= array();
- foreach( $piedata as $key=>$value ){
- $value= trim($value);
- #echo $key . "<br/>";
- if( array_key_exists($key, $arrfield) ){
- #echo $arrfield[$key] . "<br/>";
- $nmfield= $arrfield[$key];
- if( strlen($value) ){
- #$value= str_replace("&semicolon&", ";", $value);
- $value= preg_replace(array('/\s+/', '/&semicolon&/'), array('',';'), $value);
- $insdata[$nmfield]= $value;
- }else{
- $insdata[$nmfield]= '';
- }
- /*
- if( !empty($value) ){
- $value= str_replace("&semicolon&", ";", $value);
- $insdata[$nmfield]= $value;
- }else{
- $insdata[$nmfield]= '';
- }
- */
- }
-
- /*
- print_r($insdata);
- echo "<br/>";
- if( !empty($key) ){
- }
- */
- }
- echo $tblname . "<br/>";
- print_r($insdata);
- #echo $insdata['Model'];
- echo "<br/>";
- echo $tblname . "<br/>";
- /*
- echo "<br/>";
- #echo $buffer . "<br/>";
- echo $tblname . "<br/>";
-
- print_r($arrfield);
- echo "<br/>";
- print_r($piedata);
- */
- #if( $tblname == 'konsumen_keluarga' ){
-
- if( !empty($pmkey) ){
- // Jika Primary key lebih dari 1
- $where= array();
- foreach( $arrpm as $key=>$value ){
- #echo $value . "<br/>";
- /*
- print_r($insdata);
- echo "<br/>";
- */
- $where[$value]= $insdata[$value];
- }
- #$this->db->flush_cache();
- if( $tblname == "shipping_db" ){
- $stwhere= "";
- foreach( $where as $key=>$value ){
- $stwhere.= $key . "='" . $value . "' AND ";
- }
- $stwhere= substr($stwhere, 0, -5);
- $stwhere.= " OR ";
- $endwhere= "";
- $endwhere.= "Source='" . $where['Destination'] . "' AND Destination='" .
- $where['Source'] . "'";
- $where= "";
- $where= "" . $stwhere . $endwhere . "";
- $this->db->where($where, NULL, FALSE);
- }else{
- $this->db->where($where);
- }
- /*
- echo $tblname . "<br/>";
- print_r($insdata);
- echo "<br/>";
- */
- $jml= $this->db->get($tblname)->num_rows();
- if( $tblname == 'm_keluarga' ){
- #echo $this->db->last_query();
- #print_r($where);
- #print_r($insdata);
- #print_r($arrfield);
- }
-
- if( empty($jml) ){
- #$this->Gmodel->add($tblname, $insdata);
- #$this->db->insert($tblname, $insdata);
- #echo "insert" . "<br/>";
- }else{
- if( $tblname == "shipping_db" ){
- $this->db->where($where, NULL, FALSE);
- }else{
- $this->db->where($where);
- }
- $this->db->update($tblname, $insdata);
- #echo "update" . "<br/>";
- }
- }else{
- $this->Gmodel->add($tblname, $insdata);
- #$this->db->insert($tblname, $insdata);
- #echo "insert" . "<br/>";
- }
- }
- }
- #}
- /*
- echo "Import Data is successful";
- unlink($fullpath);
- exit;
- */
- }
- #fclose($fp);
- }
- }
- }
-
- function export()
- {
- // Set seluruh nama tabel yg ada d setiap modul
- $customer= array("konsumen","konsumen_keluarga","m_keluarga","m_agama","m_sapaan",
- "m_negara","m_region","m_kota","m_pekerjaan","dokter_referal",
- "upload_cust_info","komentar","area","info_klinis","audiogram",
- "shipping_db","pengiriman","pengiriman_detail","repair","repair_sparepart");
- $marketing= array("m_media_type","m_media_kategori","m_media","produk","produk_kategori",
- "produk_vendor","produk_sn","produk_harga");
- $logistic= array("produk","produk_sn","produk_harga","branch_order","branch_order_detail",
- "branch_delivery","branch_delivery_detail","branch_receive","branch_receive_detail");
-
- $data['select']['customer']= $customer;
- $data['select']['marketing']= $marketing;
- $data['select']['logistic']= $logistic;
-
- show_template($this->path_self . "vExport", $data);
- }
-
- function do_export()
- {
- $post= $this->input->post();
- /*
- print_r($post);
- exit;
- $post['module']= "upload_cust_info,komentar,area,info_klinis,audiogram,shipping_db,pengiriman,pengiriman_detail,repair,repair_sparepart";
- */
- $module= array('module'=>$post['module']);
- $this->session->set_userdata($module);
- echo "ok";
- }
-
- function download(){
- $module= $this->session->userdata('module');
- #$module= "produk,produk_sn,produk_harga";
- #$module= "m_media,m_media_type,m_media_kategori";
- $this->session->unset_userdata('module');
- if( empty($module) ){
- exit;
- }
-
- $gromodule= explode(",", $module);
- if( in_array("konsumen", $gromodule) ){
- // Upload Data dalam folder assets\restricted
- $ftp_server= "ftp.thsgcrmid.com";
- $username= "thsgcrmi";
- $pass= "tHsc3900173!";
- $this->ftplib->ftp_params($ftp_server, $username, $pass);
- $this->ftplib->upload_all();
- }
-
- $tabels= explode(",", $module);
- $jmltabel= count($tabels);
- $csvdata= "";
- foreach( $tabels as $tabelname ){
- // Get Field Primary key
- $getfield= "";
- $fields= $this->db->field_data($tabelname);
- foreach( $fields as $field ){
- if( $field->primary_key == 1 && $field->type != 'int' ){
- $getfield.= $field->name . ", ";
- }
- if( $field->primary_key != 1 ){
- $getfield.= $field->name . ", ";
- }
- }
- $getfield= substr($getfield, 0, -2);
-
- $tabels= array("produk", "produk_harga", "produk_sn", "m_media");
- if( in_array($tabelname, $tabels) ){
- $fields= explode(",", $getfield);
- $getfield= "";
- foreach( $fields as $field ){
- $getfield.= "a." . trim($field) . ", ";
- }
- #echo substr($getfield, 0, -2) . "<br/>";
- $getfield= substr($getfield, 0, -2);
- $sql= "SELECT " . $getfield . " FROM " . $tabelname . " a
- INNER JOIN user b ON b.Username= a.AddUser
- INNER JOIN klinik c ON c.IdKlinik= b.IdKlinik
- WHERE c.IdNegara= '" . $this->IdNegara . "'";
- #echo $sql . "<br/>";
- $query= $this->db->query($sql);
- }else{
- $this->db->select($getfield);
- $query= $this->db->get($tabelname);
- }
- $data= $query->result_array();
-
- // Get All data
- $writedt= "";
- foreach( $data as $key=>$list ){
- /*
- print_r($list);
- echo "<br/><br/>";
- */
- $writedt.= "'";
- foreach( $list as $value ){
- $newstr= str_replace(array("\r\n", "\r", "\n"), "<br/>", $value);
- $newstr= str_replace(";", "&semicolon&", $newstr);
- $writedt.= $newstr . ";";
- }
- $writedt.= "\n";
- }
-
- $csvdata.= "-".$tabelname."\n";
- $csvdata.= $writedt;
- }
-
- $data= $csvdata;
- $name= 'export.csv';
- force_download($name, $data);
- }
-
- /*------------------------------------------------------------------------------*/
- /* End of Operasi */
- /*------------------------------------------------------------------------------*/
- }
- ?>