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

class Trxpo extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();
		
		$this->tbl = 'pur_pohdr';		
		$this->tbl2 = 'pur_podet';
		$this->tbl4 = 'mst_perusahaan';
		$this->tbl5 = 'mst_barang';
		$this->tbl6 = 'jenis_dok';
		$this->pk = 'po_id';
		$this->pk2 = 'id';
		$this->pk4 = 'KdPrshn';
		$this->pk5 = 'KdBarang';
		$this->pk6 = 'KdJnsDok';
		$this->pkh = 'po_no';
	}
	
	public function index()
	{	
		$data = array(
			'content' 	=> 	$this->pmodul.'/index',
			't'			=>	$this->session->userdata('t'),
			'u'			=>	$this->session->userdata('u'),
			'h'			=>	$this->session->userdata('h')
			);
		// $data['rp'] = $this->pmdl->getAutoNoPo();
		// $data['rp'] = $this->pmdl->buat_kodePo();		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->pmodul.'/_cjs');
		$this->load->view($this->pmodul.'/_mjs');
	}
	
	public function trx()
	{		
		$this->aksi='t';
		$data = array(
			'content' 	=> 	$this->pmodul.'/_frm'
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->pmodul.'/_frm_cjs');
		$this->load->view($this->pmodul.'/_frm_cjst');
		$this->load->view($this->pmodul.'/_frm_mjs');
		
	}
	
	public function tambah()
	{		
		$this->aksi='t';
		$data = array(
			'content' 	=> 	$this->pmodul.'/_frm'
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->pmodul.'/_frm_cjs');
		$this->load->view($this->pmodul.'/_frm_cjst');
		$this->load->view($this->pmodul.'/_frm_mjs');
		
	}
	
	public function ubah($id)
	{		
		$this->aksi='u';
		$data = array(
			'content' 	=> $this->pmodul.'/_frm',
			'r' 		=> $this->mdl->getRow($id)
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->pmodul.'/_frm_cjs');
		$this->load->view($this->pmodul.'/_frm_cjsu');
		$this->load->view($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(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 100;	
		$po_id = $_REQUEST["po_id"];
		$key = $_REQUEST["q"];
		$rs = $this->mdl->getDet($page,$rows,$po_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"];
		$rs = $this->mdl->getHdrDet($page,$rows,$key);
		
		echo json_encode($rs);
	}

	function getform($cmd){
		$form_data = $this->input->post();
		$form_data['po_type'] = $this->po_type;
		$form_data['po_date']=$this->dmys2ymd($this->input->post('po_date'));
		$form_data['dlv_date']=$this->dmys2ymd($this->input->post('dlv_date'));
		$po_no=$form_data['po_no'];
		$pk[$this->pk]=$form_data[$this->pk];
		
		// GET Po No #iqro
		$checkNo = $this->mdl->checkNo($po_no);
		foreach ($checkNo as $x) {
			$countNo = $x->po_no;
			break;
		}

		//unset($form_data[$this->pk]);
		if ($cmd=="add"){	
			if ($countNo > 0) {
				$bol=false;
				$msg="PO NO tidak boleh sama!";		
			} else {
				$success = $this->mdl->add($form_data);	
				if ($success){				
					$bol=true;	
					$msg="Data Header berhasil ditambah!";
				} else {
					$bol=false;
					$msg="Tambah data gagal.";		
				}
			}
			echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
		} else {
			$this->edit($form_data,$pk);
		}
	}
	
	function getformDet($cmd){
		$form_data = $this->input->post();
		$mat_id=$form_data['mat_id'];
		$po_id=$form_data['po_id'];
		
		$pk=array(
			'po_id' => $form_data[$this->pk],
			'id' => $form_data[$this->pk2]
			);

		if ($cmd=="add"){
				$this->addDet($form_data);
				// CALCULATE TOTAL AMOUNT #ir
				$key = $form_data[$this->pk];
				$getTotAmount = $this->mdl->getTotAmount($key);
				foreach ($getTotAmount as $getTotAmount) {
					$tot_amount = $getTotAmount->tot_amount;
					break;
				}
				// CALCULATE TOTAL Qty #iqro
				$getTotQty = $this->mdl->getTotQty($key);
				foreach ($getTotQty as $getTotQty) {
					$tot_qty = $getTotQty->tot_qty;
				}		
				// UPDATE HEADER TRANSACTION #ir
				$updateHeader = array(
					'tot_amount'			=> $tot_amount,
					'tot_qty'				=> $tot_qty
				);
				$pk = $form_data[$this->pk];
				$this->mdl->editHdr($updateHeader, $pk);	
			} else {
				$this->editDet($form_data,$pk);
				$po_id=$_REQUEST['po_id'];
				$id=$_REQUEST['id'];
				$pk=array(
					'po_id' => $matin_id,
					'id' => $id
				);				
				// CALCULATE TOTAL AMOUNT #ir
				$key = $form_data[$this->pk];
				$getTotAmount = $this->mdl->getTotAmount($key);
				foreach ($getTotAmount as $getTotAmount) {
					$tot_amount = $getTotAmount->tot_amount;
					break;
				}
				// CALCULATE TOTAL Qty #iqro
				$getTotQty = $this->mdl->getTotQty($key);
				foreach ($getTotQty as $getTotQty) {
					$tot_qty = $getTotQty->tot_qty;
				}		
				// UPDATE HEADER TRANSACTION #ir
				$updateHeader = array(
					'tot_amount'			=> $tot_amount,
					'tot_qty'				=> $tot_qty
				);
				$pk = $form_data[$this->pk];
				$this->mdl->editHdr($updateHeader, $pk);
			}
	}
	
	function addBarScan(){
		$form_data = $this->input->post();
		$mat_id=substr($form_data,0,14);
		$qty=substr($form_data,14,5);
		
		$pk=array(
			'po_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 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);
		
	}
}