<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Uplin extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->modul = 'uplin';
		$this->title = 'UPLOAD DATA INCOMING';
		$this->kd_cmpy=$this->session->userdata('kd_cmpy');
	}
	
	public function index()
	{
		$data = array(
			'content' 	=> 	$this->modul.'/index'
		);
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/_cjs');
	}
	
	public function run_upload(){
		$file   = explode('.',$_FILES['h']['name']);
		$length = count($file);
		if($file[$length -1] == 'xlsx' || $file[$length -1] == 'xls'){//jagain barangkali uploadnya selain file excel <span class="wp-smiley wp-emoji wp-emoji-smile" title=":-)">:-)</span>
			$tmp    = $_FILES['h']['tmp_name'];//Baca dari tmp folder jadi file ga perlu jadi sampah di server :-p
			$this->load->library('excel');//Load library excelnya
			$read   = PHPExcel_IOFactory::createReaderForFile($tmp);
			$read->setReadDataOnly(true);
			$excel  = $read->load($tmp);
			$_sheet = $excel->setActiveSheetIndex(0);//Kunci sheetnye biar kagak lepas :-p
			$maxRow = $_sheet->getHighestRow();
			// nilai awal counter untuk jumlah data yang sukses dan yang gagal diimport
			$sukses = 0;
			$gagal = 0;
			$child_no = 1;
			$tot_amount= 0;
			$kd_cmpy = $this->kd_cmpy;
			for($i = 5; $i <= $maxRow; $i++){
				$child_no = $_sheet->getCell('A'.$i)->getCalculatedValue();
				$mat_id = trim($_sheet->getCell('B'.$i)->getCalculatedValue());
				$NmBarang = trim($_sheet->getCell('C'.$i)->getCalculatedValue());
				$Sat = trim($_sheet->getCell('D'.$i)->getCalculatedValue());
				$qty = $_sheet->getCell('E'.$i)->getCalculatedValue();
				$Dqty = $_sheet->getCell('E'.$i)->getCalculatedValue();
				$price = $_sheet->getCell('F'.$i)->getCalculatedValue();
				$amount = $_sheet->getCell('G'.$i)->getCalculatedValue();
				$currency = $_sheet->getCell('H'.$i)->getCalculatedValue();
				$kurs = $_sheet->getCell('S'.$i)->getCalculatedValue();
				$mat_type = $_sheet->getCell('I'.$i)->getCalculatedValue();
				$matin_no = $_sheet->getCell('J'.$i)->getCalculatedValue();
				
				// Add Some into Header and Detail --ir
				$has_edited="yes";
				$all_in="yes";
				$excel_matin_date = $_sheet->getCell('L'.$i)->getValue();
				$UNIX_DATE = ($excel_matin_date - 25569) * 86400;
				$matin_date = gmdate("Y-m-d", $UNIX_DATE);
				$matin_type = $_sheet->getCell('K'.$i)->getCalculatedValue();
				$supplier = $_sheet->getCell('M'.$i)->getCalculatedValue();
				$KdJnsDok = $_sheet->getCell('N'.$i)->getCalculatedValue();
				$NoDaf = $_sheet->getCell('O'.$i)->getCalculatedValue();
				$excel_TgDaf = $_sheet->getCell('P'.$i)->getValue();
				$UNIX_DATE = ($excel_TgDaf - 25569) * 86400;
				$TgDaf = gmdate("Y-m-d", $UNIX_DATE);
				$po_no = $_sheet->getCell('Q'.$i)->getCalculatedValue();
				$excel_po_date = $_sheet->getCell('R'.$i)->getValue();
				$UNIX_DATE = ($excel_po_date - 25569) * 86400;
				$po_date = gmdate("Y-m-d", $UNIX_DATE);
				$NoAju = $_sheet->getCell('T'.$i)->getCalculatedValue();
				$excel_TgAju = $_sheet->getCell('U'.$i)->getValue();
				$UNIX_DATE = ($excel_TgAju - 25569) * 86400;
				$TgAju = gmdate("Y-m-d", $UNIX_DATE);
				$q01 = "SELECT po_id
					FROM pur_pohdr 
					WHERE po_type='0' 
					AND po_no = '$po_no' 
					AND po_date = '$po_date' 
					LIMIT 1 ";
					// die($q01);
				$row = $this->db->query($q01)->row();
				$po_id = ($row)?$row->po_id:0;
				
				// setelah data dibaca, sisipkan ke dalam tabel
				if ($matin_no_tmp != $matin_no) {
					$q = "DELETE FROM mat_inchdr_sub WHERE matin_no = '$matin_no' AND matin_date = '$matin_date'";
					$this->db->query($q);
					
					$q = "INSERT INTO mat_inchdr_sub (
						mat_type,matin_type,matin_no,matin_date,supplier,
						currency,kurs,KdJnsDok,NoDaf,TgDaf,po_id,po_no,NoAju,TgAju
					) VALUES (
						'$mat_type','$matin_type','$matin_no','$matin_date','$supplier',
						'$currency','$kurs','$KdJnsDok','$NoDaf','$TgDaf','$po_id','$po_no','$NoAju','$TgAju'
					)";
					// die($q);
					$this->db->query($q);
					$matin_id=$this->db->insert_id();
					$matin_no_tmp=$matin_no;
					$child_no = 1;
					$tot_amount = 0;
				}
				$q= "INSERT INTO mat_incdet_sub (
					matin_id,child_no,mat_id,qty,Dqty,price,has_edited,all_in
				  ) VALUES (
					'$matin_id','$child_no','$mat_id','$qty','$Dqty','$price','$has_edited','$all_in'
				  )";
				$run=$this->db->query($q);
				
				$tot_amount = $tot_amount+$amount;
				$q= "UPDATE mat_inchdr_sub SET tot_amount = '$tot_amount' WHERE matin_no = '$matin_no'";
				$run=$this->db->query($q);
				if($this->db->affected_rows()>0){
					$sukses++;
					$child_no++;
				} else {
					$gagal++;
				}
				// $q= "INSERT IGNORE INTO mst_barang (
				// 		KdBarang,NmBarang,Sat,TpBarang
				// 		) VALUES (
				// 		'$mat_id','$NmBarang','$Sat','$mat_type'
				// 		)";
				// $this->db->query($q);
			}
			// tampilan status sukses dan gagal
			echo "<h3>Proses upload data selesai.</h3>";
			//echo "<p>Jumlah data yang sukses di upload : ".$sukses."<br>";
			//echo "Jumlah data yang gagal di upload : ".$gagal."</p>";
		}else{
			echo 'do not allowed to upload';//pesan error tipe file tidak tepat
		}
	}
	
}
