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

class Trxout_sub extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();
		
		$this->tbl = 'mat_outhdr_sub';		
		$this->tbl2 = 'mat_outdet_sub';
		$this->tbl3 = 'mst_out_type';
		$this->tbl4 = 'mst_perusahaan';
		$this->tbl5 = 'mst_barang';
		$this->tbl6 = 'jenis_dok';
		
		$this->pk = 'matout_id';
		$this->pk2 = 'child_no';
		$this->pk3 = 'matout_type';
		$this->pk4 = 'KdPrshn';
		$this->pk5 = 'KdBarang';
		$this->pk6 = 'KdJnsDok';
		$this->pkh = 'matout_no';
				
	}
	
	public function index()
	{	
		$data = array(
			'content' 	=> 	$this->pmodul.'/'.$this->pmodul,
			'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->pmodul.'/'.$this->pmodul.'_cjs');
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_mjs');
	}
	
	public function trx()
	{		
		$this->aksi='t';
		$data = array(
			'content' 	=> 	$this->pmodul.'/'.$this->pmodul.'_frm'
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_frm_cjs');
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_frm_cjst');
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_frm_mjs');
		
	}
	
	public function tambah()
	{		
		$this->aksi='t';
		$data = array(
			'content' 	=> 	$this->pmodul.'/'.$this->pmodul.'_frm'
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_frm_cjs');
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_frm_cjst');
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_frm_mjs');
		
	}
	
	public function ubah($id)
	{		
		$this->aksi='u';
		$data = array(
			'content' 	=> $this->pmodul.'/'.$this->pmodul.'_frm',
			'r' 		=> $this->mdl->getRow($id)
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_frm_cjs');
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_frm_cjsu');
		$this->load->view($this->pmodul.'/'.$this->pmodul.'_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($id=''){
		$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['mat_type']=$this->mat_type;
		$form_data['matout_date']=$this->dmys2ymd($this->input->post('matout_date'));
		$form_data['TgDaf']=$this->dmys2ymd($this->input->post('TgDaf'));
		$form_data['mat_type']=$this->mat_type;
		$form_data['kd_cmpy']=$this->session->userdata('kd_cmpy');
		
		$pk[$this->pk]=$form_data[$this->pk];
		if ($cmd=="add"){	
			$this->add($form_data);	
		} else {
			$form_data['pic']=$this->session->userdata('username');
			$this->edit($form_data,$pk);
		}
	}
	
	function getformDet($cmd){
		$form_data = $this->input->post();
		$form_data['lot_no']=$this->dmys2ymd($this->input->post('lot_no'));
		
		$pk=array(
			'matout_id' => $form_data[$this->pk],
			'child_no' => $form_data[$this->pk2]
			);
		$stok=$this->mdl->getStock($form_data);	
		if ($cmd=="add"){
			if ($stok['success'] == true){
				$success = $this->mdl->addDet($form_data);	
		
				if ($success){				
					$bol=true;
					$msg="Tambah data berhasil.";
				} else {
					$bol=false;
					$msg="Tambah data gagal.";		
				}
				
				echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
			} else {
				echo json_encode( array('success'=>false,'msg'=>$stok['msg']) ); 
			}
		} else {
			$this->editDet($form_data,$pk);
		}
	}
	
	function addBarScan(){
		$form_data = $this->input->post();
		$data=array(
			'matout_id' => $form_data[$this->pk],
			'mat_id'=>substr($form_data['barcode'],0,14),
			'qty'=>substr($form_data['barcode'],14,5)
		);
			
		$this->addDet($data);	
		//print_r($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 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);
		
	}
}