PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/application/controllers/customer/operasi.php

https://bitbucket.org/remobius/crm
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
  1. <?php
  2. class operasi extends CI_Controller
  3. {
  4. /*------------------------------------------------------------------------------*/
  5. /* Beginning of master data Operasi */
  6. /*------------------------------------------------------------------------------*/
  7. private $IdNegara;
  8. function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->library(array("jqlib","mnuauth"));
  12. $this->load->helper(array("form","download"));
  13. $this->lang->load("abdi");
  14. $webserverpath= "/www/crm_test/assets/restricted";
  15. $serverpath= "./assets/restricted";
  16. $params= array("webserverpath"=>$webserverpath,"serverpath"=>$serverpath);
  17. $this->load->library("ftplib", $params);
  18. $this->IdNegara= $this->session->userdata("idNegara");
  19. $this->path_self = $this->uri->segment("2") . "/" . __CLASS__ . "/";
  20. if($this->session->userdata('login')!==TRUE){
  21. redirect($this->configlib->site_url().'/login');
  22. }
  23. ini_set('max_execution_time', 86400);
  24. }
  25. function cekdir(){
  26. $link= opendir("./assets/restricted");
  27. while( $file= readdir($link) ){
  28. echo $file . "<br/>";
  29. }
  30. }
  31. function import()
  32. {
  33. show_template($this->path_self . "vImport");
  34. }
  35. function do_import(){
  36. foreach($_FILES as $key=>$val){
  37. foreach( $val as $subkey=>$subval ){
  38. $$subkey= $subval;
  39. }
  40. }
  41. $info= pathinfo($name);
  42. foreach( $info as $key=>$value ){
  43. $$key= $value;
  44. }
  45. if( !preg_match("#^[\w \.!@\#\$\%\^&\-\=\+\{\}\[\];',]+$#", $filename) ){
  46. echo lang("abdi.alert_45");
  47. exit;
  48. }
  49. $IdKlinik= $this->session->userdata('IdKlinik');
  50. $username= $this->session->userdata('username');
  51. $name= $IdKlinik . "-" . $username . ".csv";
  52. #$name= "customer.csv";
  53. $fullpath= "./assets/restricted/" . $name;
  54. #$fullpath= "D:/webroot/assets/restricted/" . $name;
  55. /*
  56. echo $tmp_name . "<br/>";
  57. echo $fullpath;
  58. exit;
  59. */
  60. #$fullpath= "./assets/restricted/produk_harga.csv";
  61. //$maxsize= "2048000";
  62. if( !empty($name) ){
  63. if( !preg_match("#^(csv)$#", $extension ) ){
  64. echo "File isn't document !";
  65. exit();
  66. }else{
  67. if( move_uploaded_file($tmp_name, $fullpath) ){
  68. /*
  69. if( move_uploaded_file($_FILES['file']['tmp_name'], $fullpath) ){
  70. exit;
  71. */
  72. // Open Upload File
  73. $fp= fopen($fullpath, 'r');
  74. $tblname= "";
  75. $pmkey= "";
  76. while( !feof($fp) ) {
  77. $buffer= fgets($fp);
  78. if( !empty($buffer) ){
  79. if( preg_match('#^\-.+$#', $buffer) ){
  80. $tblname= preg_replace(array("/-/","/;/","/ /"),array("","",""),$buffer);
  81. }elseif( preg_match('#^\*.+$#', $buffer) ){
  82. #$pmkey= preg_replace(array("/\*/","/;/"),array("",""),$buffer);
  83. }else{
  84. #echo $buffer . "<br/>";
  85. #$buffer= substr($buffer, 0, -2);
  86. $tblname= trim($tblname);
  87. $fields= $this->db->field_data(trim($tblname));
  88. $arrfield= array();
  89. $fieldname= "";
  90. $pmkey= "";
  91. foreach( $fields as $field ){
  92. if( $field->primary_key == 1 && $field->type != 'int'){
  93. $pmkey.= $field->name . ",";
  94. $fieldtype= $field->type;
  95. $fieldname= $field->name;
  96. $arrfield[]= $fieldname;
  97. }
  98. if( $field->primary_key != 1 ){
  99. $fieldname= $field->name;
  100. $arrfield[]= $fieldname;
  101. }
  102. }
  103. #print_r($arrfield);
  104. $pmkey= substr($pmkey, 0, -1);
  105. $arrpm= explode(",", $pmkey); // Array Primary Key
  106. $piedata= explode(";", $buffer); // Array Data
  107. $insdata= array();
  108. foreach( $piedata as $key=>$value ){
  109. $value= trim($value);
  110. #echo $key . "<br/>";
  111. if( array_key_exists($key, $arrfield) ){
  112. #echo $arrfield[$key] . "<br/>";
  113. $nmfield= $arrfield[$key];
  114. if( strlen($value) ){
  115. /*
  116. $value= str_replace("&semicolon&", ";", $value);
  117. #$value= preg_replace(array('/\s+/', '/&semicolon&/'), array('',';'), $value);
  118. */
  119. $value= str_replace("&semicolon&", ";", $value);
  120. $insdata[$nmfield]= $value;
  121. }else{
  122. $insdata[$nmfield]= '';
  123. }
  124. /* Old Script
  125. if( !empty($value) ){
  126. $value= str_replace("&semicolon&", ";", $value);
  127. $insdata[$nmfield]= $value;
  128. }else{
  129. $insdata[$nmfield]= '';
  130. }
  131. */
  132. }
  133. /*
  134. print_r($insdata);
  135. echo "<br/>";
  136. if( !empty($key) ){
  137. }
  138. */
  139. }
  140. if( !empty($pmkey) ){
  141. // Jika Primary key lebih dari 1
  142. $where= array();
  143. foreach( $arrpm as $key=>$value ){
  144. $where[$value]= $insdata[$value];
  145. }
  146. if( $tblname == "shipping_db" ){
  147. $stwhere= "";
  148. foreach( $where as $key=>$value ){
  149. $stwhere.= $key . "='" . $value . "' AND ";
  150. }
  151. $stwhere= substr($stwhere, 0, -5);
  152. $stwhere.= " OR ";
  153. $endwhere= "";
  154. $endwhere.= "Source='" . $where['Destination'] . "' AND Destination='" . $where['Source'] . "'";
  155. $where= "";
  156. $where= "" . $stwhere . $endwhere . "";
  157. $this->db->where($where, NULL, FALSE);
  158. }else{
  159. $this->db->where($where);
  160. }
  161. $jml= $this->db->get($tblname)->num_rows();
  162. if( empty($jml) ){
  163. $this->Gmodel->add($tblname,$insdata);
  164. #$this->db->insert($tblname, $insdata);
  165. #echo "insert" . "<br/>";
  166. }else{
  167. if( $tblname == "shipping_db" ){
  168. $this->db->where($where, NULL, FALSE);
  169. }else{
  170. $this->db->where($where);
  171. }
  172. $this->db->update($tblname, $insdata);
  173. #echo "update" . "<br/>";
  174. }
  175. }else{
  176. $this->Gmodel->add($tblname,$insdata);
  177. #$this->db->insert($tblname, $insdata);
  178. #echo "insert" . "<br/>";
  179. }
  180. }
  181. }
  182. }
  183. fclose($fp);
  184. echo "Import Data is successful";
  185. unlink($fullpath);
  186. exit;
  187. }
  188. }
  189. }
  190. }
  191. function import_file(){
  192. foreach($_FILES as $key=>$val){
  193. foreach( $val as $subkey=>$subval ){
  194. $$subkey= $subval;
  195. }
  196. }
  197. $name= "marketing.csv";
  198. $name= "produk_harga.csv";
  199. $info= pathinfo($name);
  200. foreach( $info as $key=>$value ){
  201. $$key= $value;
  202. }
  203. if( !preg_match("#^[\w \.!@\#\$\%\^&\-\=\+\{\}\[\];',]+$#", $filename) ){
  204. echo lang("abdi.alert_45");
  205. exit;
  206. }
  207. $IdKlinik= $this->session->userdata('IdKlinik');
  208. $username= $this->session->userdata('username');
  209. #$name= $IdKlinik . "-" . $username . ".csv";
  210. #$name= "upload_cus.csv";#"customer.csv";
  211. #$fullpath= "./assets/restricted/" . $name;
  212. $fullpath= "./assets/restricted/02001-joko.csv";
  213. //$maxsize= "2048000";
  214. if( !empty($name) ){
  215. if( !preg_match("#^(csv)$#", $extension ) ){
  216. echo "File isn't document !";
  217. exit();
  218. }else{
  219. #if( move_uploaded_file($tmp_name, $fullpath) ) {
  220. // Open Upload File
  221. $fp= fopen($fullpath, 'r');
  222. $tblname= "";
  223. $pmkey= "";
  224. while( !feof($fp) ) {
  225. $buffer= fgets($fp);
  226. if( !empty($buffer) ){
  227. if( preg_match('#^\-.+$#', $buffer) ){
  228. $tblname= preg_replace(array("/-/","/;/","/ /"),array("","",""),$buffer);
  229. echo $tblname . "<br/>";
  230. }elseif( preg_match('#^\*.+$#', $buffer) ){
  231. #$pmkey= preg_replace(array("/\*/","/;/"),array("",""),$buffer);
  232. }else{
  233. #echo $buffer . "<br/>";
  234. #$buffer= substr($buffer, 0, -2);
  235. $tblname= trim($tblname);
  236. if( $tblname == 'konsumen_keluarga' ){
  237. /*
  238. echo $buffer . "\n";
  239. echo strlen($nmfield);
  240. echo "\n";
  241. echo $buffer . "\n";
  242. */
  243. }
  244. $fields= $this->db->field_data(trim($tblname));
  245. $arrfield= array();
  246. $fieldname= "";
  247. $pmkey= "";
  248. foreach( $fields as $field ){
  249. if( $field->primary_key == 1 && $field->type != 'int'){
  250. $pmkey.= $field->name . ",";
  251. $fieldtype= $field->type;
  252. $fieldname= $field->name;
  253. $arrfield[]= $fieldname;
  254. }
  255. if( $field->primary_key != 1 ){
  256. $fieldname= $field->name;
  257. $arrfield[]= $fieldname;
  258. }
  259. }
  260. #print_r($arrfield);
  261. $pmkey= substr($pmkey, 0, -1);
  262. $arrpm= explode(",", $pmkey); // Array Primary Key
  263. $piedata= explode(";", $buffer); // Array Data
  264. $insdata= array();
  265. foreach( $piedata as $key=>$value ){
  266. $value= trim($value);
  267. #echo $key . "<br/>";
  268. if( array_key_exists($key, $arrfield) ){
  269. #echo $arrfield[$key] . "<br/>";
  270. $nmfield= $arrfield[$key];
  271. if( strlen($value) ){
  272. #$value= str_replace("&semicolon&", ";", $value);
  273. $value= preg_replace(array('/\s+/', '/&semicolon&/'), array('',';'), $value);
  274. $insdata[$nmfield]= $value;
  275. }else{
  276. $insdata[$nmfield]= '';
  277. }
  278. /*
  279. if( !empty($value) ){
  280. $value= str_replace("&semicolon&", ";", $value);
  281. $insdata[$nmfield]= $value;
  282. }else{
  283. $insdata[$nmfield]= '';
  284. }
  285. */
  286. }
  287. /*
  288. print_r($insdata);
  289. echo "<br/>";
  290. if( !empty($key) ){
  291. }
  292. */
  293. }
  294. echo $tblname . "<br/>";
  295. print_r($insdata);
  296. #echo $insdata['Model'];
  297. echo "<br/>";
  298. echo $tblname . "<br/>";
  299. /*
  300. echo "<br/>";
  301. #echo $buffer . "<br/>";
  302. echo $tblname . "<br/>";
  303. print_r($arrfield);
  304. echo "<br/>";
  305. print_r($piedata);
  306. */
  307. #if( $tblname == 'konsumen_keluarga' ){
  308. if( !empty($pmkey) ){
  309. // Jika Primary key lebih dari 1
  310. $where= array();
  311. foreach( $arrpm as $key=>$value ){
  312. #echo $value . "<br/>";
  313. /*
  314. print_r($insdata);
  315. echo "<br/>";
  316. */
  317. $where[$value]= $insdata[$value];
  318. }
  319. #$this->db->flush_cache();
  320. if( $tblname == "shipping_db" ){
  321. $stwhere= "";
  322. foreach( $where as $key=>$value ){
  323. $stwhere.= $key . "='" . $value . "' AND ";
  324. }
  325. $stwhere= substr($stwhere, 0, -5);
  326. $stwhere.= " OR ";
  327. $endwhere= "";
  328. $endwhere.= "Source='" . $where['Destination'] . "' AND Destination='" .
  329. $where['Source'] . "'";
  330. $where= "";
  331. $where= "" . $stwhere . $endwhere . "";
  332. $this->db->where($where, NULL, FALSE);
  333. }else{
  334. $this->db->where($where);
  335. }
  336. /*
  337. echo $tblname . "<br/>";
  338. print_r($insdata);
  339. echo "<br/>";
  340. */
  341. $jml= $this->db->get($tblname)->num_rows();
  342. if( $tblname == 'm_keluarga' ){
  343. #echo $this->db->last_query();
  344. #print_r($where);
  345. #print_r($insdata);
  346. #print_r($arrfield);
  347. }
  348. if( empty($jml) ){
  349. #$this->Gmodel->add($tblname, $insdata);
  350. #$this->db->insert($tblname, $insdata);
  351. #echo "insert" . "<br/>";
  352. }else{
  353. if( $tblname == "shipping_db" ){
  354. $this->db->where($where, NULL, FALSE);
  355. }else{
  356. $this->db->where($where);
  357. }
  358. $this->db->update($tblname, $insdata);
  359. #echo "update" . "<br/>";
  360. }
  361. }else{
  362. $this->Gmodel->add($tblname, $insdata);
  363. #$this->db->insert($tblname, $insdata);
  364. #echo "insert" . "<br/>";
  365. }
  366. }
  367. }
  368. #}
  369. /*
  370. echo "Import Data is successful";
  371. unlink($fullpath);
  372. exit;
  373. */
  374. }
  375. #fclose($fp);
  376. }
  377. }
  378. }
  379. function export()
  380. {
  381. // Set seluruh nama tabel yg ada d setiap modul
  382. $customer= array("konsumen","konsumen_keluarga","m_keluarga","m_agama","m_sapaan",
  383. "m_negara","m_region","m_kota","m_pekerjaan","dokter_referal",
  384. "upload_cust_info","komentar","area","info_klinis","audiogram",
  385. "shipping_db","pengiriman","pengiriman_detail","repair","repair_sparepart");
  386. $marketing= array("m_media_type","m_media_kategori","m_media","produk","produk_kategori",
  387. "produk_vendor","produk_sn","produk_harga");
  388. $logistic= array("produk","produk_sn","produk_harga","branch_order","branch_order_detail",
  389. "branch_delivery","branch_delivery_detail","branch_receive","branch_receive_detail");
  390. $data['select']['customer']= $customer;
  391. $data['select']['marketing']= $marketing;
  392. $data['select']['logistic']= $logistic;
  393. show_template($this->path_self . "vExport", $data);
  394. }
  395. function do_export()
  396. {
  397. $post= $this->input->post();
  398. /*
  399. print_r($post);
  400. exit;
  401. $post['module']= "upload_cust_info,komentar,area,info_klinis,audiogram,shipping_db,pengiriman,pengiriman_detail,repair,repair_sparepart";
  402. */
  403. $module= array('module'=>$post['module']);
  404. $this->session->set_userdata($module);
  405. echo "ok";
  406. }
  407. function download(){
  408. $module= $this->session->userdata('module');
  409. #$module= "produk,produk_sn,produk_harga";
  410. #$module= "m_media,m_media_type,m_media_kategori";
  411. $this->session->unset_userdata('module');
  412. if( empty($module) ){
  413. exit;
  414. }
  415. $gromodule= explode(",", $module);
  416. if( in_array("konsumen", $gromodule) ){
  417. // Upload Data dalam folder assets\restricted
  418. $ftp_server= "ftp.thsgcrmid.com";
  419. $username= "thsgcrmi";
  420. $pass= "tHsc3900173!";
  421. $this->ftplib->ftp_params($ftp_server, $username, $pass);
  422. $this->ftplib->upload_all();
  423. }
  424. $tabels= explode(",", $module);
  425. $jmltabel= count($tabels);
  426. $csvdata= "";
  427. foreach( $tabels as $tabelname ){
  428. // Get Field Primary key
  429. $getfield= "";
  430. $fields= $this->db->field_data($tabelname);
  431. foreach( $fields as $field ){
  432. if( $field->primary_key == 1 && $field->type != 'int' ){
  433. $getfield.= $field->name . ", ";
  434. }
  435. if( $field->primary_key != 1 ){
  436. $getfield.= $field->name . ", ";
  437. }
  438. }
  439. $getfield= substr($getfield, 0, -2);
  440. $tabels= array("produk", "produk_harga", "produk_sn", "m_media");
  441. if( in_array($tabelname, $tabels) ){
  442. $fields= explode(",", $getfield);
  443. $getfield= "";
  444. foreach( $fields as $field ){
  445. $getfield.= "a." . trim($field) . ", ";
  446. }
  447. #echo substr($getfield, 0, -2) . "<br/>";
  448. $getfield= substr($getfield, 0, -2);
  449. $sql= "SELECT " . $getfield . " FROM " . $tabelname . " a
  450. INNER JOIN user b ON b.Username= a.AddUser
  451. INNER JOIN klinik c ON c.IdKlinik= b.IdKlinik
  452. WHERE c.IdNegara= '" . $this->IdNegara . "'";
  453. #echo $sql . "<br/>";
  454. $query= $this->db->query($sql);
  455. }else{
  456. $this->db->select($getfield);
  457. $query= $this->db->get($tabelname);
  458. }
  459. $data= $query->result_array();
  460. // Get All data
  461. $writedt= "";
  462. foreach( $data as $key=>$list ){
  463. /*
  464. print_r($list);
  465. echo "<br/><br/>";
  466. */
  467. $writedt.= "'";
  468. foreach( $list as $value ){
  469. $newstr= str_replace(array("\r\n", "\r", "\n"), "<br/>", $value);
  470. $newstr= str_replace(";", "&semicolon&", $newstr);
  471. $writedt.= $newstr . ";";
  472. }
  473. $writedt.= "\n";
  474. }
  475. $csvdata.= "-".$tabelname."\n";
  476. $csvdata.= $writedt;
  477. }
  478. $data= $csvdata;
  479. $name= 'export.csv';
  480. force_download($name, $data);
  481. }
  482. /*------------------------------------------------------------------------------*/
  483. /* End of Operasi */
  484. /*------------------------------------------------------------------------------*/
  485. }
  486. ?>