<?php
class CT_barangbahanbaku extends CI_Controller{
	function __construct()
	{
	  parent::__construct();
		if (!$this->dx_auth->is_logged_in()) {
			header('HTTP/1.1 401 Unauthorize');die('expire'); 
		}
		
		$uid = $this->session->userdata('DX_user_id');
		
		if(!permissionAuth(2) && $uid != RID){ 
			header('HTTP/1.1 401 Unauthorize');die('noaccess');
		}
		
		$this->load->model("settings/mdl_settings", 'mdl_settings');

	}
	
	function index(){
	
		$this->load->view('settings/view_bahanbaku');
	
	}
	
	function loadData(){
	
		$search = isset($_POST['_search'])?$_POST['_search']:false;
		$searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
		$searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
		$filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
		$limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
			
		$where = " 1=1 ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "part_no" ) {
						$searchString = $k->data;
						$where .= " and part_no like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_name" ) {
						$searchString = $k->data;
						$where .= " and part_name like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_name_customs" ) {
						$searchString = $k->data;
						$where .= " and part_name_customs like  '%".$searchString."%'"; 
					} else if ( $k->field == "hs_code" ) {
						$searchString = $k->data;
						$where .= " and hs_code like  '%".$searchString."%'"; 
					} else if ( $k->field == "bm" ) {
						$searchString = $k->data;
						$where .= " and bm like  '%".$searchString."%'"; 
					} else if ( $k->field == "cek_price" ) {
						$searchString = $k->data;
						$where .= " and cek_price like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_no2" ) {
						$searchString = $k->data;
						$where .= " and part_no2 like  '%".$searchString."%'"; 
					} else if ( $k->field == "kode_barang" ) {
						$searchString = $k->data;
						$where .= " and kode_barang like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_settings->datapart_hs_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_settings->datapart_hs_CountAll($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages;
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
		foreach($records as $r){
						
			$responce->rows[$i]['id']=$r->id; 
			$responce->rows[$i]['part_no']=$r->part_no; 
			$responce->rows[$i]['part_name']=$r->part_name;
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs;
			$responce->rows[$i]['hs_code']=$r->hs_code;
			$responce->rows[$i]['bm']=$r->bm;
			$responce->rows[$i]['cek_price']=$r->cek_price;
			$responce->rows[$i]['part_no2']=$r->part_no2;
			$responce->rows[$i]['kode_barang']=$r->kode_barang;
			$i++;
		}
		echo json_encode($responce);		
	
	
	}
	
	function view_bahanbaku_update(){
		
		$part_no  = (isset($_POST['part_no']) && !empty($_POST['part_no']))?$_POST['part_no']:'';
		$part_name_customs  = (isset($_POST['part_name_customs']) && !empty($_POST['part_name_customs']))?$_POST['part_name_customs']:'';
		$part_name  = (isset($_POST['part_name']) && !empty($_POST['part_name']))?$_POST['part_name']:'';
		$hs_code  = (isset($_POST['hs_code']) && !empty($_POST['hs_code']))?$_POST['hs_code']:'';
		$bm  = (isset($_POST['bm']) && !empty($_POST['bm']))?$_POST['bm']:'';
		$cek_price  = (isset($_POST['cek_price']) && !empty($_POST['cek_price']))?$_POST['cek_price']:'';
		$idx = (isset($_POST['idx']) && !empty($_POST['idx']))?$_POST['idx']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$part_no2 = (isset($_POST['part_no2']) && !empty($_POST['part_no2']))?$_POST['part_no2']:'';
		$kode_barang = (isset($_POST['kode_barang']) && !empty($_POST['kode_barang']))?$_POST['kode_barang']:'';
		
		$part_number2 = str_replace(" ", "",$part_no);
		
		$data["part_no"] = $part_no;
		$data["part_no2"] = $part_no2; 
		$data["part_name_customs"] = $part_name_customs; 
		$data["idx"] = $idx; 
		$data["part_name"] = $part_name; 
		$data["hs_code"] = $hs_code; 
		$data["bm"] = $bm; 
		$data["cek_price"] = $cek_price; 
		$data["kode_barang"] = $kode_barang; 
		
		
		
		
		$this->load->view('settings/view_bahanbaku_update', $data);
	
	}
	
	function getdataitemlist(){
		$qry = $this->db->query("
			BEGIN TRY
			
			  exec('sp_item_list')
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			
			 ");
		 $rs = $qry->result_array();
         // $r = $qry->result();		
		// var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' Data Loaded');
		 }  
		 echo json_encode($responce);
		 
		  // $stmt = " exec('sp_item_list') ";
		 // $qry = mssql_query($stmt); 
		 // $row = mssql_fetch_array($qry); 
		 // var_dump($row);die();
	
	}
	
	function getdataparths(){
		
		$qry = $this->db->query("
			BEGIN TRY
			  exec('sp_part_hs')
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			
			 ");
		 $rs = $qry->result_array();
         //// $r = $qry->result();		
		//// var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' File/s Loaded');
		 }  
		 echo json_encode($responce);
		 
		 // $stmt = " exec('sp_part_hs') ";
		 // $qry = mssql_query($stmt); 
		 // $row = mssql_fetch_array($qry); 
		 // var_dump($row);die();
	
	
	}
	
	function view_bahanbaku_itemlist(){
	
		$this->load->view('settings/view_bahanbaku_itemlist');
	
	}
	
	function data_bahanbaku_itemlist(){
	
		$search = isset($_POST['_search'])?$_POST['_search']:false;
		$searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
		$searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
		$filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
		$limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
			
		$where = " 1=1 ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "PART_NO" ) {
						$searchString = $k->data;
						$where .= " and PART_NO like  '%".$searchString."%'"; 
					} else if ( $k->field == "PART_NAME" ) {
						$searchString = $k->data;
						$where .= " and PART_NAME like  '%".$searchString."%'"; 
					} else if ( $k->field == "CUST_NAME" ) {
						$searchString = $k->data;
						$where .= " and CUST_NAME like  '%".$searchString."%'"; 
					} else if ( $k->field == "CUST_PNO" ) {
						$searchString = $k->data;
						$where .= " and CUST_PNO like  '%".$searchString."%'"; 
					} else if ( $k->field == "ORG_CTRY" ) {
						$searchString = $k->data;
						$where .= " and ORG_CTRY like  '%".$searchString."%'"; 
					} else if ( $k->field == "SUPP_PNO" ) {
						$searchString = $k->data;
						$where .= " and SUPP_PNO like  '%".$searchString."%'"; 
					} else if ( $k->field == "SUPP_CODE" ) {
						$searchString = $k->data;
						$where .= " and SUPP_CODE like  '%".$searchString."%'"; 
					} else if ( $k->field == "PARTNO2" ) {
						$searchString = $k->data;
						$where .= " and SUPP_CODE like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_settings->dataitemlist_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_settings->dataitemlist_CountAll($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages;
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
		foreach($records as $r){
						
			$responce->rows[$i]['id_item_list']=$r->id_item_list; 
			$responce->rows[$i]['PART_NO']=$r->PART_NO; 
			$responce->rows[$i]['PART_NAME']=$r->PART_NAME;
			$responce->rows[$i]['CUST_PNO']=$r->CUST_PNO;
			$responce->rows[$i]['CUST_NAME']=$r->CUST_NAME;
			$responce->rows[$i]['SUPP_PNO']=$r->SUPP_PNO;
			$responce->rows[$i]['SUPP_CODE']=$r->SUPP_CODE;
			$responce->rows[$i]['PCT_SUP']=$r->PCT_SUP;
			$responce->rows[$i]['BAL_FAC']=$r->BAL_FAC;
			$responce->rows[$i]['WEIGTH']=$r->WEIGTH;
			$responce->rows[$i]['ASS_STAGE']=$r->ASS_STAGE;
			$responce->rows[$i]['MIN_PACK']=$r->MIN_PACK;
			$responce->rows[$i]['UNIT']=$r->UNIT;
			$responce->rows[$i]['CURRENCY']=$r->CURRENCY;
			$responce->rows[$i]['WASTE']=$r->WASTE;
			$responce->rows[$i]['ORG_CTRY']=$r->ORG_CTRY;
			$responce->rows[$i]['SUPP_STAT']=$r->SUPP_STAT;
			$responce->rows[$i]['CLASS']=$r->clas;
			$responce->rows[$i]['LOCATION']=$r->LOCATION;
			$responce->rows[$i]['MIN_ORDQTY']=$r->MIN_ORDQTY;
			$responce->rows[$i]['BEG_BAL']=$r->BEG_BAL;
			$responce->rows[$i]['TOTAL_IN']=$r->TOTAL_IN;
			$responce->rows[$i]['TOTAL_OUT']=$r->TOTAL_OUT;
			$responce->rows[$i]['CUMM_REQ']=$r->CUMM_REQ;
			$responce->rows[$i]['CUMM_IN']=$r->CUMM_IN;
			$responce->rows[$i]['PART_CODE']=$r->PART_CODE;
			$responce->rows[$i]['QTY1BOXROL']=$r->QTY1BOXROL;
			$responce->rows[$i]['QTY1PALLET']=$r->QTY1PALLET;
			$responce->rows[$i]['FOB']=$r->FOB;
			$responce->rows[$i]['CIF']=$r->CIF;
			$responce->rows[$i]['FOBNI']=$r->FOBNI;
			$responce->rows[$i]['CIFNI']=$r->CIFNI;
			$responce->rows[$i]['QUOTEFOB']=$r->QUOTEFOB;
			$responce->rows[$i]['QUOTECIF']=$r->QUOTECIF;
			$responce->rows[$i]['QUOTEFOBNI']=$r->QUOTEFOBNI;
			$responce->rows[$i]['QUOTECIFNI']=$r->QUOTECIFNI;
			$responce->rows[$i]['QUOTE_CURR']=$r->QUOTE_CURR;
			$responce->rows[$i]['NFOB']=$r->NFOB;
			$responce->rows[$i]['NCIF']=$r->NCIF;
			$responce->rows[$i]['NFOBNI']=$r->NFOBNI;
			$responce->rows[$i]['NCIFNI']=$r->NCIFNI;
			$responce->rows[$i]['NQUOTEFOB']=$r->NQUOTEFOB;
			$responce->rows[$i]['NQUOTECIF']=$r->NQUOTECIF;
			$responce->rows[$i]['NQUOTEFOBNI']=$r->NQUOTEFOBNI;
			$responce->rows[$i]['NQUOTECIFNI']=$r->NQUOTECIFNI;
			$responce->rows[$i]['FINAL_PRE']=$r->FINAL_PRE;
			$responce->rows[$i]['PARTNO2']=$r->PARTNO2;
			$responce->rows[$i]['PERIODE']=$r->PERIODE;
			$responce->rows[$i]['PERIODE1']=$r->PERIODE1;
			$responce->rows[$i]['PRICE']=$r->PRICE;
			$responce->rows[$i]['SUPP_NAME']=$r->SUPP_NAME;
			$responce->rows[$i]['AMOUNT']=$r->AMOUNT;
			$responce->rows[$i]['COST']=$r->COST;
			$responce->rows[$i]['FREIGHT']=$r->FREIGHT;
			$responce->rows[$i]['INVOICE']=$r->INVOICE;
			$responce->rows[$i]['FLAG']=$r->FLAG;
			$responce->rows[$i]['NFLAG']=$r->NFLAG;
			$responce->rows[$i]['MAKER_CODE']=$r->MAKER_CODE;
			$responce->rows[$i]['RACK_LOC']=$r->RACK_LOC;
			$responce->rows[$i]['CREATED_BY']=$r->CREATED_BY;
			$responce->rows[$i]['CREATED_DATE']=$r->CREATED_DATE;
			$responce->rows[$i]['MODIFIED_BY']=$r->MODIFIED_BY;
			$responce->rows[$i]['MODIFIED_DATE']=$r->MODIFIED_DATE;
			$responce->rows[$i]['NON_GSP_STATUS']=$r->NON_GSP_STATUS;
			$responce->rows[$i]['COMODITY_CODE']=$r->COMODITY_CODE;
			$responce->rows[$i]['filename']=$r->filename;
			$responce->rows[$i]['execdate']=$r->execdate;
			$i++;
		}
		echo json_encode($responce);		
	
	}
	
	function view_bahanbaku_stockawal(){
	
		$this->load->view('settings/view_bahanbaku_stockawal');
	
	}
	
	function loaddata_bahanbaku_stockawal(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
		$searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
		$searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
		$filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
		$limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
			
		$where = " 1=1 and typebahan='RM' ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "part_no" ) {
						$searchString = $k->data;
						$where .= " and part_no like  '%".$searchString."%'"; 
					} else if ( $k->field == "date_stock" ) {
						$searchString = $k->data;
						$where .= " and date_stock = '$searchString'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_settings->stockawal_bahanbaku_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_settings->stockawal_bahanbaku_CountAll($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages;
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
		foreach($records as $r){
						
			$responce->rows[$i]['id_stock_awal']=$r->id_stock_awal; 
			$responce->rows[$i]['date_stock']=$r->date_stock; 
			$responce->rows[$i]['part_no']=$r->part_no;
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs;
			$responce->rows[$i]['part_name']=$r->part_name;
			$responce->rows[$i]['qty']=$r->qty;
			$responce->rows[$i]['unit']=$r->unit;
			$responce->rows[$i]['unit_konversi']=$r->unit_konversi;
			$responce->rows[$i]['pengali']=$r->pengali;
			$responce->rows[$i]['netto']=$r->netto;
			$responce->rows[$i]['brutto']=$r->brutto;
			$responce->rows[$i]['typebahan']=$r->typebahan;
			$responce->rows[$i]['create_date']=$r->create_date;
			$responce->rows[$i]['create_by']=$r->create_by;
			$responce->rows[$i]['update_date']=$r->update_date;
			$responce->rows[$i]['update_by']=$r->update_by;
			
			$i++;
		}
		echo json_encode($responce);		
	}
	
	function proses_stockawal_add(){
	
		$filename = (isset($_POST['filename']) && !empty($_POST['filename']))?$_POST['filename']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$tipe = (isset($_POST['tipe']) && !empty($_POST['tipe']))?$_POST['tipe']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_stockawal_upload = (isset($_POST['id_stockawal_upload']) && !empty($_POST['id_stockawal_upload']))?$_POST['id_stockawal_upload']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$username = $this->session->userdata('DX_username');
		$typebahan = 'RM';
		/* echo "exec('spi_stock_awal @filename=''$filename'', @id_stockawal_upload=$id_stockawal_upload, @tipe=''$tipe'',  @username=''$username''  ')";
		die(); */
		
		$qry = $this->db->query("
			BEGIN TRY
			  exec('spi_stock_awal @filename=''$filename'', @id_stockawal_upload=$id_stockawal_upload, @tipe=''$tipe'',  @username=''$username'', @typebahan=''$typebahan''  ')
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			
			 ");
		 $rs = $qry->result_array();
         //// $r = $qry->result();		
		//// var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' ');
		 }  
		 echo json_encode($responce);
		
	
	}
	function view_bahanbaku_rekappart(){
	
		$this->load->view('settings/view_bahanbaku_rekappart');	
	
	}
	
	
	function downloadrekapxls(){
		
		$tgawal = (isset($_GET['tgawal']) && !empty($_GET['tgawal']))?$_GET['tgawal']:die('{"sts":"ERROR","desc":" Param  tgawal Tidak Ditemukan"}');
	    $tgakhir = (isset($_GET['tgakhir']) && !empty($_GET['tgakhir']))?$_GET['tgakhir']:die('{"sts":"ERROR","desc":" Param  tglakhir Tidak Ditemukan"}');
	    
	
		include_once ( APPPATH."libraries/PHPExcel.php");	
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		$phpExcel = new PHPExcel();
		$objTpl = PHPExcel_IOFactory::load(APPPATH."libraries/PHPExcel/templates/rekap_data_part.xls");	
		$objTpl->setActiveSheetIndex(0);  //set second sheet as active
	
		$qry_data_in = $this->db->query("
				
				;with
					ct_kodebrg (kodebarang, partcustoms, satuan) as
							(
								select kode_barang, nama_barang, satuan from tbl_kode_barang
								
							) ,
					ct_data_in (part_number, part_name, part_name_customs, qty) as (
					select 
					part_number, part_name, part_name_customs, 
								round(sum(qty_in * isnull(pengali, 1)), 2)  --sum(qty_in) 
								from [tbl_data_in_dtl] 
								inner join tbl_data_in_hdr on [tbl_data_in_dtl].id_data_in_hdr=tbl_data_in_hdr.id_data_in_hdr
								
								where type_brg = 1 and tbl_data_in_hdr.doc_in_date between '$tgawal' and '$tgakhir' and part_name_customs not like '%UNKNOWN%'

								group by part_name_customs,part_number, part_name )

					select part_number, part_name, kodebarang, part_name_customs, qty, satuan 
					 from ct_kodebrg  
							 left outer join ct_data_in on ct_kodebrg.partcustoms = ct_data_in.part_name_customs
				
				");
		
		$rs_data_in = $qry_data_in->result_array();
	
		$i = 1;
		$j = '';
		$k = '';
		foreach ($rs_data_in as $r ){
			$k = 1 + $i;
			$j = "$k";
			
									
			$objTpl->getActiveSheet()->setCellValueExplicit("A".$j, $r["part_number"], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("B".$j, $r["part_name"], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("C".$j, $r["kodebarang"], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("D".$j, $r["part_name_customs"], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("E".$j, $r["qty"], PHPExcel_Cell_DataType::TYPE_NUMERIC);
			$objTpl->getActiveSheet()->setCellValueExplicit("F".$j, $r["satuan"], PHPExcel_Cell_DataType::TYPE_STRING);
		
			$i++;
			
		
		}
		

		$objTpl->setActiveSheetIndex(1);  //set second sheet as active	
		
		$qry_data_in = $this->db->query("
				
				;with
				ct_kodebrg (kodebarang, partcustoms, satuan) as
						(
							select kode_barang, nama_barang, satuan from tbl_kode_barang
							
						) ,
				ct_data_out (part_number, part_name_customs, qty) as (
						select 
						 
						 part_number,  [part_name_customs], 
						sum([qty] * isnull(pengali, 1)) as qty
							  
								
									FROM    tbl_data_out_dtl INNER JOIN
										 tbl_data_out_hdr ON tbl_data_out_dtl.id_data_out_hdr = tbl_data_out_hdr.id_data_out_hdr 
								where [type_items] = 'RM' and tbl_data_out_hdr.approve_date is not null
								and  [tbl_data_out_dtl].out_date  between '2013-09-01' and '2013-12-31'
								group by [part_name_customs], part_number
							
							)

				select part_number, kodebarang, part_name_customs, qty, satuan 
				 from ct_kodebrg  
						 left outer join ct_data_out on ct_kodebrg.partcustoms = ct_data_out.part_name_customs
				order by 	kodebarang	 
				
				");
		
		$rs_data_in = $qry_data_in->result_array();
	
		$i = 1;
		$j = '';
		$k = '';
		foreach ($rs_data_in as $r ){
			$k = 1 + $i;
			$j = "$k";
			
									
			$objTpl->getActiveSheet()->setCellValueExplicit("A".$j, $r["part_number"], PHPExcel_Cell_DataType::TYPE_STRING);
			//$objTpl->getActiveSheet()->setCellValueExplicit("B".$j, $r["part_name"], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("C".$j, $r["kodebarang"], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("D".$j, $r["part_name_customs"], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("E".$j, $r["qty"], PHPExcel_Cell_DataType::TYPE_NUMERIC);
			$objTpl->getActiveSheet()->setCellValueExplicit("F".$j, $r["satuan"], PHPExcel_Cell_DataType::TYPE_STRING);
		
			$i++;
			
		
		}
		
		$filename = 'rekap_data_part_'.$tgawal. '_SD_'.$tgakhir;
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!
	
	}
	
	function delete_data_part_hs(){
		$idx = (isset($_POST['idx']) && !empty($_POST['idx']))?$_POST['idx']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$qry = $this->db->query("
			BEGIN TRY
				delete from tbl_data_part_hs where [id] = $idx
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			SELECT 
				'00' AS status
				,'DATA DELETED' AS description;
		");
		
		 $rs = $qry->result_array();
         //// $r = $qry->result();		
		//// var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' ');
		 }  
		 echo json_encode($responce);
	
	}
	
	function view_konversi_part(){
		$part_no = (isset($_POST['part_no']) && !empty($_POST['part_no']))?$_POST['part_no']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$part_no2 = (isset($_POST['part_no2']) && !empty($_POST['part_no2']))?$_POST['part_no2']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$data['part_no'] = $part_no;
		$data['part_no2'] = $part_no2;
		$this->load->view("settings/view_konversi_part", $data);
	
	}
	function tbl_data_part_hs_article(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id_konv_part'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 ";
		
		if ($search){
		   	if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					$searchString = $k->data;
					$where .= " and " . $k->field . " like  '%".$searchString."%'"; 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_settings->data_tbl_data_part_hs_article_All($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_settings->data_tbl_data_part_hs_article_CountAll($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
						
			$responce->rows[$i]['id_konv_part']=$r->id_konv_part; 
			$responce->rows[$i]['part_no']=$r->part_no; 
			$responce->rows[$i]['part_no2']=$r->part_no2; 
			$responce->rows[$i]['article_code']=$r->article_code; 
			$i++;
		}
		echo json_encode($responce);	
	
	}
	
	function update_konv_part(){
		$id_konv_part = (isset($_POST['id_konv_part']) && !empty($_POST['id_konv_part']))?$_POST['id_konv_part']:'';
		$part_no = (isset($_POST['part_no']) && !empty($_POST['part_no']))?$_POST['part_no']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$part_no2 = (isset($_POST['part_no2']) && !empty($_POST['part_no2']))?$_POST['part_no2']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$article_code = (isset($_POST['article_code']) && !empty($_POST['article_code']))?$_POST['article_code']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		if($id_konv_part == ''){
			$q = $this->db->query("
			if (select count(*) from tbl_data_part_hs_article where part_no2='$part_no2') > 0
			begin
				update tbl_data_part_hs_article 
				set 
					part_no = '$part_no',
					part_no2 = '$part_no2',
					article_code = '$article_code'
				where part_no2 = 	'$part_no2'
				SELECT 
				'00' AS status
				,'DATA UPDATED' AS description;	
				return 
			end
			BEGIN TRY
				insert into tbl_data_part_hs_article 
				(
					part_no,
					part_no2,
					article_code
				) values (
					'$part_no',
					'$part_no2',
					'$article_code'
				)
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			SELECT 
				'00' AS status
				,'DATA ADDED' AS description;	
			");
			foreach ($q->result_array() as $row){
				$responce = array("sts" => $row['status'], "desc" => $row['description']);
			}  
			echo json_encode($responce);
		} else {
			$q = $this->db->query("
			BEGIN TRY
				update tbl_data_part_hs_article 
				set 
					part_no = '$part_no',
					part_no2 = '$part_no2',
					article_code = '$article_code'
				where id_konv_part = 	$id_konv_part
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			SELECT 
				'00' AS status
				,'DATA UPDATED' AS description;
			");
			foreach ($q->result_array() as $row){
				$responce = array("sts" => $row['status'], "desc" => $row['description']);
			}  
			echo json_encode($responce);
			
		}
		
	}
	
	function konv_part_del(){
		$id_konv_part = (isset($_POST['id_konv_part']) && !empty($_POST['id_konv_part']))?$_POST['id_konv_part']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$q = $this->db->query("
			BEGIN TRY
				delete from tbl_data_part_hs_article  where id_konv_part = 	$id_konv_part
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			SELECT 
				'00' AS status
				,'DATA DELETED' AS description;
			");
			foreach ($q->result_array() as $row){
				$responce = array("sts" => $row['status'], "desc" => $row['description']);
			}  
			echo json_encode($responce);
	}
	
	function update_kdbrg(){
		$id_brg = (isset($_POST['id_brg']) && !empty($_POST['id_brg']))?$_POST['id_brg']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$q = $this->db->query("
			declare @kdbrg varchar(50),@part_customs varchar(50)
			set @part_customs = (select part_name_customs from tbl_data_part_hs where id=$id_brg)
			set @kdbrg = (select kode_barang from [tbl_kode_barang] where [nama_barang] = @part_customs)			
			IF @kdbrg is null
			BEGIN
				select '05' AS status
				,' data ' + @part_customs + ' tidak ada' AS description; 
				return
				
			END
			
			BEGIN TRY
			 update tbl_data_part_hs set kode_barang = 	@kdbrg where id=$id_brg
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			SELECT 
				'00' AS status
				,'DATA UPDATED' AS description; 
			 
		");
		foreach ($q->result_array() as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	}
	
	
 }