<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Uplpo extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->modul = 'uplpo';
		$this->title = 'UPLOAD DATA PURCHASE ORDER';
		$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;
			$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();
				$price = $_sheet->getCell('F'.$i)->getCalculatedValue();
				$amount = $_sheet->getCell('G'.$i)->getCalculatedValue();
				$currency = $_sheet->getCell('H'.$i)->getCalculatedValue();
				$kurs = $_sheet->getCell('I'.$i)->getCalculatedValue();
				$po_type = 0;
				$po_no=$_sheet->getCell('J'.$i)->getCalculatedValue();
				$excel_po_date = $_sheet->getCell('K'.$i)->getValue();
				$UNIX_DATE = ($excel_po_date - 25569) * 86400;
				$po_date = gmdate("Y-m-d", $UNIX_DATE);
				$supplier = $_sheet->getCell('L'.$i)->getCalculatedValue();
				$attn=$_sheet->getCell('M'.$i)->getCalculatedValue();
				$terms=$_sheet->getCell('N'.$i)->getCalculatedValue();
				
				$excel_dlv_date = $_sheet->getCell('O'.$i)->getValue();
				$UNIX_DATE = ($excel_dlv_date - 25569) * 86400;
				$dlv_date = gmdate("Y-m-d", $UNIX_DATE);
				$notes = $_sheet->getCell('P'.$i)->getCalculatedValue();
				$so_no=$_sheet->getCell('Q'.$i)->getCalculatedValue();
				$excel_so_date = $_sheet->getCell('R'.$i)->getValue();
				$UNIX_DATE = ($excel_so_date - 25569) * 86400;
				$so_date = gmdate("Y-m-d", $UNIX_DATE);
				$q01="SELECT po_id
					FROM pur_pohdr 
					WHERE po_type='2' 
					AND po_no = '$so_no' 
					AND po_date = '$so_date' 
					LIMIT 1 ";
				$row=$this->db->query($q01)->row();
				$so_id=$row->po_id;
				
				// setelah data dibaca, sisipkan ke dalam tabel
				if ($po_no_tmp != $po_no) {
					$q= "DELETE FROM pur_pohdr  WHERE po_type='0' AND po_no = '$po_no' AND po_date = '$po_date'";
					$this->db->query($q);
					$q= "INSERT INTO pur_pohdr (
						po_type,attn,po_no,po_date,supplier,currency,kurs,notes,dlv_date,terms,ref_id,ref_no
					) VALUES (
						'$po_type','$attn','$po_no','$po_date','$supplier','$currency','$kurs','$notes','$dlv_date','$terms','$so_id','$so_no'
					)";
					// die($q);
					$this->db->query($q);
					$po_id=$this->db->insert_id();
					$po_no_tmp=$po_no;
					$child_no=1;
				}
				$q= "INSERT INTO pur_podet (
					po_id,child_no,mat_id,qty,price
				  ) VALUES (
					'$po_id','$child_no','$mat_id','$qty','$price'
				  )";
				$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
		}
	}
	
}
