<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class Uplopn extends MY_Controller {

	

	function __construct()

	{

		parent::__construct();



		$this->modul = 'uplopn';

		$this->title = 'UPLOAD DATA STOCK OPNAME';

		$this->kd_cmpy=$this->session->userdata('kd_cmpy');



		

		$this->tbl = 'mat_opnamehdr_sub';		

		$this->tbl2 = 'mat_opnamedet_sub';

		$this->tbl3 = 'mst_barang';

		$this->tbl4 = 'mst_perusahaan';

		

		$this->pk = 'opname_id';

		$this->pk2 = 'child_no';

		$this->pk3 = 'KdBarang';

		$this->pk4 = 'KdPrshn';

		$this->pkh = 'opname_no';





	}

	

	public function index()

	{

		$data = array(

			'content' 	=> 	$this->modul.'/'.$this->modul

		);

		$this->load('tpl',$data['content'], $data);

		$this->load->view($this->modul.'/'.$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;

			$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();

				$mat_type=$_sheet->getCell('F'.$i)->getCalculatedValue();

				$wh_id=$_sheet->getCell('G'.$i)->getCalculatedValue();

				$opname_no = $_sheet->getCell('H'.$i)->getCalculatedValue();

				$excel_opname_date = $_sheet->getCell('I'.$i)->getValue();

				$opname_type = $_sheet->getCell('J'.$i)->getCalculatedValue();

				$UNIX_DATE = ($excel_opname_date - 25569) * 86400;

				$opname_date = gmdate("Y-m-d", $UNIX_DATE);


//				if(PHPExcel_Shared_Date::isDateTime($_sheet->getCell('I'.$i))) {

//					$opname_date = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($opname_date));

//				}



				// setelah data dibaca, sisipkan ke dalam tabel

				if ($opname_no_tmp != $opname_no) {

					$q= "INSERT INTO mat_opnamehdr_sub (

						mat_type,wh_id,opname_no,opname_date,opname_type,kd_cmpy

					) VALUES (

						'$mat_type','$wh_id','$opname_no','$opname_date','$opname_type','$kd_cmpy'

					)";

//					echo $q;

					$this->db->query($q);

					$opname_id=$this->db->insert_id();

					$opname_no_tmp=$opname_no;

				}



				$q= "INSERT INTO mat_opnamedet_sub (

					opname_id,child_no,mat_id,qty

				  ) VALUES (

					'$opname_id','$child_no','$mat_id','$qty'

				  )";

				$run=$this->db->query($q);



				if($this->db->affected_rows()>0){

					$sukses++;

				} 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

		}

	}

	



}