<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Matcons_cb extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();
		
		$this->modul = 'matcons_cb';
		$this->title = 'BILL OF MATERIAL CB';
		
		$this->tbl = 'mat_bomhdr_sub';		
		$this->tbl2 = 'mat_bomdet_sub';
		$this->tbl3 = 'mst_barang';
		$this->tbl4 = 'mst_perusahaan';
		$this->tbl5 = 'satuan';
		$this->type = 'bom_type';
		
		$this->pk = 'bom_id';
		$this->pk2 = 'child_no';
		$this->pk3 = 'KdBarang';
		$this->pk4 = 'KdPrshn';
		$this->pk5 = 'KdSat';
		$this->pkh = 'bom_no';	
		
		$this->load->model($this->modul.'_mdl','mdl');
	}
	
	public function index()
	{	
		$data = array(
			'content' 	=> 	$this->modul.'/'.$this->modul,
			't'			=>	$this->session->userdata('t'),
			'u'			=>	$this->session->userdata('u'),
			'h'			=>	$this->session->userdata('h')
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/'.$this->modul.'_cjs');
		$this->load->view($this->modul.'/'.$this->modul.'_mjs');
	}
	
	public function trx()
	{		
		$this->aksi='t';
		$data = array(
			'content' 	=> 	$this->modul.'/'.$this->modul.'_frm'
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/'.$this->modul.'_frm_cjs');
		$this->load->view($this->modul.'/'.$this->modul.'_frm_cjst');
		$this->load->view($this->modul.'/'.$this->modul.'_frm_mjs');
		
	}
	
	public function tambah()
	{		
		$this->aksi='t';
		$data = array(
			'content' 	=> 	$this->modul.'/'.$this->modul.'_frm'
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/'.$this->modul.'_frm_cjs');
		$this->load->view($this->modul.'/'.$this->modul.'_frm_cjst');
		$this->load->view($this->modul.'/'.$this->modul.'_frm_mjs');
		
	}
	
	public function ubah($id)
	{		
		$this->aksi='u';
		$data = array(
			'content' 	=> $this->modul.'/'.$this->modul.'_frm',
			'r' 		=> $this->mdl->getRow($id)
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/'.$this->modul.'_frm_cjs');
		$this->load->view($this->modul.'/'.$this->modul.'_frm_cjsu');
		$this->load->view($this->modul.'/'.$this->modul.'_frm_mjs');
		
	}
	
	function dataHdr(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 100;	
		$key = $_REQUEST["q"];
		$rs = $this->mdl->getHdr($page,$rows,$key);
		
		echo json_encode($rs);
	}
	
	function dataDet($key=''){
		if($key==''){
			$key=$_REQUEST['q'];
		}
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 100;	
		$key = $_REQUEST["q"];
		$rs = $this->mdl->getDet($page,$rows,$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"];
		$rs = $this->mdl->getHdrDet($page,$rows,$key);
		
		echo json_encode($rs);
	}
	
	function getform($cmd){
		$form_data = $this->input->post();
		$form_data['bom_date']=$this->dmys2ymd($this->input->post('bom_date'));
		$form_data['kd_cmpy']=$this->session->userdata('kd_cmpy');
		$form_data['bom_type']="CB";
		
		$pk[$this->pk]=$form_data[$this->pk];
		unset($form_data['NmBarang0']);
		unset($form_data['Sat0']);
		if ($cmd=="add"){	
			$this->add($form_data);	
		} else {
			$this->edit($form_data,$pk);
		}
	}
	
	function getformDet($cmd){
		$form_data = $this->input->post();
		
		$pk=array(
			'bom_id' => $form_data[$this->pk],
			'child_no' => $form_data[$this->pk2]
			);
		if ($cmd=="add"){	
			$this->addDet($form_data);	
		} else {
			$this->editDet($form_data,$pk);
		}
	}
	
	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->modul.'/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->modul.'/slip',$data);
		
	}
	
	function barcode($id)
	{
		$data = array(
			'rhdr' 	=> 	$this->mdl->getRow($id)
		);
		
		$this->load->library('pdf');
		$this->load->view($this->modul.'/slip2',$data);
		
	}
}
