<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Trxin extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();
		
		// LOCAL
		$this->tbl = 'mat_inchdr_sub';		
		$this->tbl2 = 'mat_incdet_sub';
		$this->tbl3 = 'mst_in_type';
		$this->tbl4 = 'mst_perusahaan';
		$this->tbl5 = 'mst_barang';
		$this->tbl6 = 'jenis_dok';
		$this->pk = 'matinids_po_id';
		$this->pk2 = 'child_no';
		$this->pk3 = 'matin_type';
		$this->pk4 = 'KdPrshn';
		$this->pk5 = 'KdBarang';
		$this->pk6 = 'KdJnsDok';
		$this->pkh = 'matin_no';
		// TPB
		$this->tpbdb						= 'tpbdb';
		$this->tpb_header				= 'tpb_header';		
		$this->tpb_pemasok			= 'referensi_pemasok';		
		$this->tpb_pengusaha		= 'referensi_pengusaha';		
		$this->tpb_pk_id				= 'ID';
		$this->tpb_fk_id				= 'ID_TPB';
		$this->tpb_fk_header		= 'ID_HEADER';
	}
	
	public function index()
	{	
		$content	=	$this->pmodul.'/index';
		$cjs			=	$this->pmodul.'/_cjs';
		$mjs			=	$this->pmodul.'/_mjs';
		// if($this->url == site_url('wipin')){
		// 	$content 	= $this->pmodul.'/index_wip';
		// 	$cjs 			= $this->pmodul.'/_cjs_wip';
		// 	$mjs 			= $this->pmodul.'/_mjs_wip';
		// }
		$data = array(
			'content' 	=> 	$content,
			't'			=>	$this->session->userdata('t'),
			'u'			=>	$this->session->userdata('u'),
			'h'			=>	$this->session->userdata('h')
			);
		$data['ri'] = $this->mdl->buat_kodeIn();		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($cjs);
		$this->load->view($mjs);
	}
	
	function dataHdr(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 100;	
		$KdJnsDok = isset($_POST["KdJnsDok"]) ? $_POST["KdJnsDok"] : '';	
		$key = $_REQUEST["q"];
		$rs = $this->mdl->getHdr($page,$rows,$key,$KdJnsDok);
		
		echo json_encode($rs);
	}
	
	function dataDet(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 100;	
		$matin_id = $_REQUEST["matin_id"];
		$key = $_REQUEST["q"];
		$rs = $this->mdl->getDet($page,$rows,$matin_id,$key);
		// $this->url = 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		// if($this->url == site_url('wipin')){
		// 	$rs = $this->mdl->getDetWip($page,$rows,$matin_id,$key);
		// }else{
		// 	$rs = $this->mdl->getDet($page,$rows,$matin_id,$key);
		// }
		echo json_encode($rs);
	}
	
	function dataHdrDet(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 100;	
		$key = $_REQUEST["q"];
		if($this->url == site_url('wipin')){
			$rs = $this->mdl->getHdrDetWip($page,$rows,$key);
		}else{
			$rs = $this->mdl->getHdrDet($page,$rows,$key);
		}
		
		
		echo json_encode($rs);
	}
	
	function getform($cmd){
		$form_data = $this->input->post();
		$form_data['mat_type']=$this->mat_type;
		$form_data['matin_date']=$this->dmys2ymd($this->input->post('matin_date'));
		$form_data['TgDaf']=$this->dmys2ymd($this->input->post('TgDaf'));
		$form_data['TgAju']=$this->dmys2ymd($this->input->post('TgAju'));
		$form_data['TgTTD']=$this->dmys2ymd($this->input->post('TgTTD'));
		$form_data['mat_type']=$this->mat_type;
		$form_data['kd_cmpy']=$this->session->userdata('kd_cmpy');
		$form_data['notes']="JI".substr($this->input->post('matin_no'),2,-1);
		// $form_data['tot_amount']=0;
		
		$pk[$this->pk]=$form_data[$this->pk];
		if ($cmd=="add"){	
			$this->add($form_data);
			
			// INSERT & UPDATE TO MATIN DET --ir
			$key = $form_data['matin_no'];
			$key2 = $form_data['po_no'];
			$getCountPO = $this->mdl->getCountPO($key2);
			foreach ($getCountPO as $getCountPO) {
				$countPO = $getCountPO->po_no;
			}			
			$getPartial = $this->mdl->getPartial2($key2);
			foreach ($getPartial as $getPartial) {
				$getPart = $getPartial->partial;
			}			
			if($countPO <= 1){
				$getPodet = $this->mdl->getPodet($key);
				
				// ARRAY DETAIL INCOMING -ir
				$child_no = 1;
				foreach ($getPodet as $getPodet) {
					$dataDet = array(
						'matin_id'	=> $getPodet->matin_id,
						'child_no'	=> $child_no,
						'mat_id'		=> $getPodet->mat_id,
						'weight'		=> $getPart,
						'poqty'			=> $getPodet->qty,
						'balance'		=> $getPodet->qty,
						'qty'				=> 0,
						'price'			=> $getPodet->price
					);
					$child_no++;
					
					// INSERT DETAIL INCOMING -ir
					$this->mdl->addDet($dataDet);
				}
			} else if($countPO > 1) {
				$child_no = 1;
				
				// ARRAY DETAIL INCOMING -ir
				$getMatin = $this->mdl->getMatin($key);
				foreach ($getMatin as $getMatin) {
					$matin_id = $getMatin->matin_id;
				}
				
				$getPodet = $this->mdl->getPodet2($key2,$getPart);
				foreach ($getPodet as $getPodet) {
					$dataDet = array(
						'matin_id' 	=> $matin_id,
						'child_no'	=> $child_no,
						'mat_id'		=> $getPodet->mat_id,
						'weight'		=> $getPart,
						'poqty'			=> $getPodet->poqty,
						'qty'				=> 0,
						'Dqty'			=> $getPodet->Dqty,
						'balance'		=> $getPodet->balance,
						'price'			=> $getPodet->price
					);
					$child_no++;
					
					// INSERT DETAIL INCOMING -ir
					$this->mdl->addDet($dataDet);
				}
				
				$id					= $matin_id;	
				$updatePartial = array(
					'partial'			=> $getPodet->partial + 1
				);
				$this->mdl->editPartial($updatePartial, $id);
			}
		} else {
			$this->edit($form_data,$pk);
		}
	}
	
	function getformDet($cmd){
		$form_data = $this->input->post();
		
		$pk=array(
			'matin_id' => $form_data[$this->pk],
			'child_no' => $form_data[$this->pk2],
			'mat_id' 		=> $form_data['mat_id']
			);
		if ($cmd=="add"){	
			$this->addDet($form_data);	
		} else {
			$this->editDet($form_data,$pk);
			// IF BALANCE=0 THEN ALL IN #ir
			$balance = $form_data['balance'];
			if($balance == 0) $all_in = 'yes';
			else $all_in = 'no';
			// UPDATE DETAIL TRANSACTIONS #ir
			$updateDetail = array(
				'has_edited'			=> 'yes',
				'all_in'					=> $all_in
			);
			$this->mdl->editDetail($updateDetail, $pk);
			
			// CALCULATE TOTAL AMOUNT #ir
			$key = $form_data[$this->pk];
			$getTotAmount = $this->mdl->getTotAmount($key);
			foreach ($getTotAmount as $getTotAmount) {
			 	$tot_amount = $getTotAmount->tot_amount;
			 	break;
			}
			// GET ITEM COUNT #ir
			$getCountDetailIn = $this->mdl->getCountDetailIn($key);
			foreach ($getCountDetailIn as $x) {
				$countItem = $x->matin_id;
			}			
			// UPDATE HEADER TRANSACTION #ir
			$updateHeader = array(
				'tot_amount'			=> $tot_amount + $form_data['amount'],
				'HargaSerah'			=> $tot_amount + $form_data['amount'],
				'JmlBarang'				=> $countItem
			);
			$pk = $form_data[$this->pk];
			$this->mdl->editHdr($updateHeader, $pk);
		}
	}
	
	// Create Journal #ir
	function create_journal(){
		$id=$_REQUEST['id'];
		$created_journal=$_REQUEST['created_journal'];
		
		if($created_journal == 'yes'){
			$bol=false;
			$msg="Data ini sudah dibuatkan jurnalnya!";
		} else {
			$success = $this->mdl->createJournal($id);
			if ($success){				
				$bol=true;
				$msg="Create Journal berhasil.";
			} else {
				$bol=false;
				$msg="Create Journal GAGAL.";
			}
		}
		echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
	}
	// Lock Item --ir
	function lock(){
		$matin_id=$_REQUEST['matin_id'];
		$child_no=$_REQUEST['child_no'];
		$mat_id=$_REQUEST['mat_id'];
		$locked=$_REQUEST['locked'];
		$pk=array(
			$this->pk 	=> $matin_id,
			$this->pk2 	=> $child_no,
			'mat_id' 		=> $mat_id
		);
		$getDetRow = $this->mdl->getDetRow($matin_id, $child_no, $mat_id);
		foreach ($getDetRow as $getDetRow) {
			if($locked == "no") $locked="yes"; else $locked="no";
			$dataDet = array(
				'locked'=> $locked
			);
			$success = $this->mdl->editDet($dataDet,$pk);
		}
		if ($success){				
			$bol=true;
			if($_REQUEST['locked'] == "no") $msg="Item berhasil dikunci"; else $msg="Item berhasil dibuka";
		} else {
			$bol=false;
			$msg="Gagal Dirubah.";
		}
		echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
	}
	// Reset Qty --ir
	function reset_qty(){
		$matin_id=$_REQUEST['matin_id'];
		$child_no=$_REQUEST['child_no'];
		$mat_id=$_REQUEST['mat_id'];
		$pk=array(
			$this->pk 	=> $matin_id,
			$this->pk2 	=> $child_no,
			'mat_id' 		=> $mat_id
		);
		// CALCULATE QTY IN DETAIL #ir
		$getDetRow = $this->mdl->getDetRow($matin_id, $child_no, $mat_id);
		foreach ($getDetRow as $getDetRow) {
			$Dqty 		= ($getDetRow->Dqty) - ($getDetRow->qty);
			$balance 	= ($getDetRow->balance) + ($getDetRow->qty);
			$dataDet = array(
				'qty'			 	=> 0,
				'Dqty'			=> $Dqty,
				'balance'		=> $balance,
				'has_edited'=> "no",
				'all_in'		=> "no"
			);
			// CALCULATE TOTAL AMOUNT #ir
			$getTotAmount = $this->mdl->getTotAmount($matin_id);
			foreach ($getTotAmount as $getTotAmount) {
			 	$tot_amount = $getTotAmount->tot_amount;
			 	break;
			}
			// GET ITEM COUNT #ir
			$getCountDetailIn = $this->mdl->getCountDetailIn($matin_id);
			foreach ($getCountDetailIn as $y) {
				$countItem = $y->matin_id;
				break;
			}			
			// UPDATE HEADER TRANSACRION #ir
			$amount = $getDetRow->qty * $getDetRow->price;
			$updateHeader = array(
				'tot_amount'			=> $tot_amount - $amount,
				'HargaSerah'			=> $tot_amount - $amount,
				'JmlBarang'				=> $countItem - 1
			);
			
			$pk2 = $matin_id;
			$this->mdl->editHdr($updateHeader, $pk2);
			$success = $this->mdl->editDet($dataDet,$pk);
		}
		if ($success){				
			$bol=true;
			$msg="Reset Incoming Qty berhasil.";
		} else {
			$bol=false;
			$msg="Reset Incoming Qty GAGAL.";
		}
		echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
	}
	// Reset Qty All Items --ir
	function reset_qty_all(){
		$matin_id=$_REQUEST['matin_id'];
		$has_edited = 'yes';
		// CALCULATE QTY DETAIL #ir
		$getDetRow = $this->mdl->getDetRowAll($matin_id,$has_edited);
		foreach ($getDetRow as $getDetRow) {
			$Dqty 		= ($getDetRow->Dqty) - ($getDetRow->qty);
			$balance 	= ($getDetRow->balance) + ($getDetRow->qty);
			$dataDet = array(
				'qty'			 	=> 0,
				'Dqty'			=> $Dqty,
				'balance'		=> $balance,
				'has_edited'=> "no",
				'all_in'		=> "no"
			);
			$pk=array(
				$this->pk 	=> $matin_id,
				$this->pk2 	=> $getDetRow->child_no,
				'mat_id' 		=> $getDetRow->mat_id
			);
			// CALCULATE TOTAL AMOUNT #ir
			$getTotAmount = $this->mdl->getTotAmount($matin_id);
			foreach ($getTotAmount as $getTotAmount) {
			 	$tot_amount = $getTotAmount->tot_amount;
			 	break;
			}
			// GET ITEM COUNT #ir
			$getCountDetailIn = $this->mdl->getCountDetailIn($matin_id);
			foreach ($getCountDetailIn as $x) {
				$countItem = $x->matin_id;
			}			
			// UPDATE DETAIL TRANSACTIONS
			$amount = $getDetRow->qty * $getDetRow->price;
			$updateHeader = array(
				'tot_amount'			=> $tot_amount - $amount,
				'HargaSerah'			=> $tot_amount - $amount,
				'JmlBarang'				=> $countItem - 1
			);
			
			$pk2 = $matin_id;
			$this->mdl->editHdr($updateHeader, $pk2);
			$success = $this->mdl->editDet($dataDet,$pk);
		}
		if ($success){				
			$bol=true;
			$msg="Reset Incoming Qty SEMUA Item berhasil.";
		} else {
			$bol=false;
			$msg="Reset Incoming Qty SEMUA Item GAGAL.";
		}
		echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
	}
	// All Qty All Items --ir
	function all_in_qty(){
		$matin_id=$_REQUEST['matin_id'];
		$child_no=$_REQUEST['child_no'];
		$mat_id=$_REQUEST['mat_id'];
		$pk=array(
			$this->pk 	=> $matin_id,
			$this->pk2 	=> $child_no,
			'mat_id' 		=> $mat_id
		);
		
		// CALCULATE QTY DETAIL #ir
		$getDetRow = $this->mdl->getDetRow($matin_id, $child_no, $mat_id);
		foreach ($getDetRow as $getDetRow) {
			$qty = ($getDetRow->qty) + ($getDetRow->balance);
			$Dqty = ($getDetRow->Dqty) + ($getDetRow->balance);
			$dataDet = array(
				'qty'			 	=> $qty,
				'Dqty'			=> $Dqty,
				'balance'		=> 0,
				'has_edited'=> "yes",
				'all_in'		=> "yes"
			);
			// CALCULATE TOTAL AMOUNT #ir
			$getTotAmount = $this->mdl->getTotAmount($matin_id);
			foreach ($getTotAmount as $getTotAmount) {
			 	$tot_amount = $getTotAmount->tot_amount;
			 	break;
			}
			// GET ITEM COUNT #ir
			$getCountDetailIn = $this->mdl->getCountDetailIn($matin_id);
			foreach ($getCountDetailIn as $x) {
				$countItem = $x->matin_id;
			}		
			
			// UPDATE HEADER TRANSACTIONS #ir
			$amount = ($qty * $getDetRow->price) - ($getDetRow->qty * $getDetRow->price);
			if($getDetRow->qty == 0) $countItem = $countItem + 1;
			else $countItem = $countItem;
			$updateHeader = array(
				'tot_amount'			=> $tot_amount + $amount,
				'HargaSerah'			=> $tot_amount + $amount,
				'JmlBarang'				=> $countItem
			);
			
			$pk2 = $matin_id;
			$this->mdl->editHdr($updateHeader, $pk2);
			$success = $this->mdl->editDet($dataDet,$pk);
		}
		if ($success){				
			$bol=true;
			$msg="Input All In Qty berhasil.";
		} else {
			$bol=false;
			$msg="Input All In Qty GAGAL.";
		}
		echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
	}
	// All In Qty All Items --ir
	function all_in_qty_all(){
		$matin_id=$_REQUEST['matin_id'];
		$all_in = 'no';
		// CALCULATE QTY DETAIL #ir 
		$getDetRow = $this->mdl->getDetRowAll2($matin_id,$all_in);
		foreach ($getDetRow as $getDetRow) {
			$qty = ($getDetRow->qty) + ($getDetRow->balance);
			$Dqty = ($getDetRow->Dqty) + ($getDetRow->balance);
			$dataDet = array(
				'qty'			 	=> $qty,
				'Dqty'			=> $Dqty,
				'balance'		=> 0,
				'has_edited'=> "yes",
				'all_in'		=> "yes"
			);
			$pk=array(
				$this->pk 	=> $matin_id,
				$this->pk2 	=> $getDetRow->child_no,
				'mat_id' 		=> $getDetRow->mat_id
			);
			// CALCULATE TOTAL AMOUNT #ir
			$getTotAmount = $this->mdl->getTotAmount($matin_id);
			foreach ($getTotAmount as $getTotAmount) {
			 	$tot_amount = $getTotAmount->tot_amount;
			 	break;
			}
			// GET ITEM COUNT #ir
			$getCountDetailIn = $this->mdl->getCountDetailIn($matin_id);
			foreach ($getCountDetailIn as $x) {
				$countItem = $x->matin_id;
			}		
			// UPDATE DETAIL TRANSACTIONS #ir
			$amount = ($qty * $getDetRow->price) - ($getDetRow->qty * $getDetRow->price); 
			if($getDetRow->qty == 0) $countItem = $countItem + 1;
			else $countItem = $countItem;
			$updateHeader = array(
				'tot_amount'			=> $tot_amount + $amount,
				'HargaSerah'			=> $tot_amount + $amount,
				'JmlBarang'				=> $countItem
			);
			
			$pk2 = $matin_id;
			$this->mdl->editHdr($updateHeader, $pk2);
			$success = $this->mdl->editDet($dataDet,$pk);
		}
		if ($success){				
			$bol=true;
			$msg="Input All Incoming Qty SEMUA Item berhasil.";
		} else {
			$bol=false;
			$msg="Input All Incoming Qty SEMUA Item GAGAL.";
		}
		echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
	}
	// Transfer to Ceisa --ir
	function transfer_ceisa(){
		$id=$_REQUEST['id'];
		$transfer_ceisa=$_REQUEST['transfer_ceisa'];
		
		if ($transfer_ceisa == 'yes'){
			$bol=false;
			$msg="Data ini sudah ditransfer ke Ceisa!";
		} else {
			$success = $this->mdl->transferCeisa($id);
			if ($success){				
				$bol=true;
				$msg="Transfer to Ceisa berhasil.";
			} else {
				$bol=false;
				$msg="Transfer to Ceisa GAGAL.";
			}
		}
		echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
	}
	function addBarScan(){
		$form_data = $this->input->post();
		$mat_id=substr($form_data,0,14);
		$qty=substr($form_data,14,5);
		
		$pk=array(
			'matin_id' => $form_data[$this->pk]
			);
			
		$this->addDet($form_data);	
	}
	
	// function aksi($cmd){
	// 	ini_set('memory_limit', '-1');
		
	// 	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
	// 	$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 100;	
	// 	$key = $_REQUEST["q"];
		
	// 	$data = array(
	// 		'rs' 	=> 	$this->mdl->getHdrDet($page,$rows,$key)
	// 	);	
		
	// 	if ($cmd=='excel'){
	// 		$filename =$this->title.date(Ymd).".xls";
	// 		header('Content-type: application/ms-excel');
	// 		header('Content-Disposition: attachment; filename='.$filename);
	// 	}
		
	// 	$this->load->view($this->pmodul.'/print',$data);
		
	// }

	function aksi($cmd){
		ini_set('memory_limit', '-1');
		
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 100;	
		$key = $_REQUEST["q2"];
		$dtdari = $_REQUEST["dtdari"];
		$dtsampai = $_REQUEST["dtsampai"];
		$rs = $this->mdl->getHdrDet($page,$rows,$key,$dtdari,$dtsampai);
		
		if ($cmd=='cari'){
			echo json_encode($rs);
		} else {
			// $dtdari = '';
			// $dtsampai = '';
			$rs = $this->mdl->getHdrDet($page,$rows,$key,$dtdari,$dtsampai);

			$data = array(
				'rs' 	=> 	$rs
			);	
		
			if ($cmd=='xls'){
				$filename =$this->title.date(Ymd).".xls";
				header('Content-type: application/ms-excel');
				header('Content-Disposition: attachment; filename='.$filename);
			}
			$this->load->view($this->pmodul.'/print',$data);
		}	
	}
	
	function slip($id)
	{
		$data = array(
			'cmpy_nm'=>$this->session->userdata('nm_cmpy'),
			'rhdr' 	=> 	$this->mdl->getRow($id),
			'rdet' 	=> 	$this->mdl->getDetAll($id)
		);
		
		$this->load->library('pdf');
		$this->load->view($this->pmodul.'/slip',$data);
		
	}
	
	function barcode($id)
	{
		$data = array(
			'rhdr' 	=> 	$this->mdl->getRow($id)
		);
		
		$this->load->library('pdf');
		$this->load->view($this->pmodul.'/slip2',$data);
		
	}
	function excelCeisa($id)
	{
		$rh =	$this->mdl->getRow($id);
		$rsd = $this->mdl->getDetAll($id);
		$inputFileName = FCPATH . '/template/BC27K.xls';
		$inputFileType = IOFactory::identify($inputFileName);
		$reader = IOFactory::createReader($inputFileType);
		$spreadsheet = $reader->load($inputFileName);
		$writer = IOFactory::createWriter($spreadsheet, $inputFileType);
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$spreadsheet->setActiveSheetIndexByName('Header');
		$FNoAju = '150327005860'.$rh->F2TgAju.$rh->NoAju;
		$spreadsheet->getActiveSheet()->setCellValue('A2', $FNoAju);
		$spreadsheet->getActiveSheet()->setCellValue('T2', $rh->Bruto);
		$spreadsheet->getActiveSheet()->setCellValue('AD2', $rh->HargaSerah);
		$spreadsheet->getActiveSheet()->setCellValue('AN2', $rh->JmlBarang);
		$spreadsheet->getActiveSheet()->setCellValue('CT2', $rh->Netto);
		$spreadsheet->getActiveSheet()->setCellValue('DT2', $rh->FTgTTD);
		$y = 2;
        $y_bb = 2;
		$seri_barang = 1;
		foreach($rsd as $rd){
			$spreadsheet->setActiveSheetIndexByName('Barang');
			$spreadsheet->getActiveSheet()->setCellValue('A'.$y, $FNoAju);
			$spreadsheet->getActiveSheet()->setCellValue('B'.$y, $seri_barang);
			$spreadsheet->getActiveSheet()->setCellValue('L'.$y, $rd->amount);
			$spreadsheet->getActiveSheet()->setCellValue('O'.$y, $rd->JmlMat);
			$spreadsheet->getActiveSheet()->setCellValue('Q'.$y, $rd->qty);
			$spreadsheet->getActiveSheet()->setCellValue('U'.$y, $rd->mat_id);
			$spreadsheet->getActiveSheet()->setCellValue('AB'.$y, $rd->Sat);
			$spreadsheet->getActiveSheet()->setCellValue('AG'.$y, '');
			$spreadsheet->getActiveSheet()->setCellValue('AK'.$y, '');
			$spreadsheet->getActiveSheet()->setCellValue('AQ'.$y, $rd->NmBarang);
			$spreadsheet->setActiveSheetIndexByName('BahanBaku');
			$rsb = $this->mdl->getBomAll($rd->mat_id, $rh->ref_id);
			
			foreach($rsb as $rb){
				$no_aju_dok_asal = '150327005860'.$rb->FTgAju.$rb->NoAju;
				$seri_bb = 1;
				$JmlSatuan = $rb->qty * $rd->qty;
				$spreadsheet->getActiveSheet()->setCellValue('A'.$y_bb, $FNoAju);
				$spreadsheet->getActiveSheet()->setCellValue('B'.$y_bb, $seri_barang);
				$spreadsheet->getActiveSheet()->setCellValue('C'.$y_bb, $seri_bb);
				$spreadsheet->getActiveSheet()->setCellValue('H'.$y_bb, $rb->Sat);
				$spreadsheet->getActiveSheet()->setCellValue('I'.$y_bb, $JmlSatuan);
				$spreadsheet->getActiveSheet()->setCellValue('K'.$y_bb, $rb->mat_id);
				$spreadsheet->getActiveSheet()->setCellValue('T'.$y_bb, $no_aju_dok_asal);
				$spreadsheet->getActiveSheet()->setCellValue('U'.$y_bb, $rb->NoDaf);
				$spreadsheet->getActiveSheet()->setCellValue('V'.$y_bb, '');
				$spreadsheet->getActiveSheet()->setCellValue('Y'.$y_bb, $rb->FTgDaf);
				$spreadsheet->getActiveSheet()->setCellValue('AB'.$y_bb, $rb->NmBarang);
				$y_bb += 1;
				$seri_bb += 1;
			}
			$y += 1;
			$seri_barang += 1;
		}
		// Redirect output to a client’s web browser (Xlsx)
		$filename = "BC27K_".date('YmdHis').".xls";
		header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
		// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		// If you're serving to IE over SSL, then the following may be needed
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
		header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header('Pragma: public'); // HTTP/1.0
		$writer->save('php://output');
		exit;
		// $data = array(
		// 	'cmpy_nm'=>$this->session->userdata('nm_cmpy'),
		// 	'rhdr' 	=> 	$this->mdl->getRow($id),
		// 	'rdet' 	=> 	$this->mdl->getDetAll($id)
		// );
		
		// $this->load->library('pdf');
		// $this->load->view($this->pmodul.'/slip',$data);
		
	}
}