
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Bahanbaku extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->modul='Item Master';
		$this->title='ITEM & UOM';
		
		$this->load->model('bahanbaku_mdl','mdl');
	}
	
	public function index()
	{		
		$data = array(
			'title' 	=> 	'Raw Material',
			'content' 	=> 	'bahanbaku/bahanbaku_frm',
			't'			=>	$this->session->userdata('t'),
			'u'			=>	$this->session->userdata('u'),
			'h'			=>	$this->session->userdata('h')
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('bahanbaku/bahanbaku_frm_cjs');
		$this->load->view('bahanbaku/bahanbaku_frm_mjs');
		
	}
	
	function grid(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 100;	
		$TpBarang = isset($_POST["TpBarang"]) ? $_POST["TpBarang"] : '';	
		$key = $_REQUEST["q"];
		$rs = $this->mdl->getAll($page,$rows,$key,$TpBarang);
		
		echo json_encode($rs);
	}
	
	function getform($cmd){
		$form_data = $this->input->post();
		$id=$form_data['id'];
		$KdBarang=$form_data['KdBarang'];
		
		// GET ITEM COUNT #ir
		$checkItem = $this->mdl->checkItem($KdBarang);
		foreach ($checkItem as $x) {
			$countItem = $x->KdBarang;
			break;
		}	

		unset($form_data['id']);
		if ($cmd=="add"){	
			if ($countItem > 0) {
				$bol=false;
				$msg="Item sudah ada!";		
			} else {
				$success = $this->mdl->add($form_data);	
				if ($success){				
					$bol=true;	
					$msg="Item berhasil ditambah!";
				} else {
					$bol=false;
					$msg="Tambah data gagal.";		
				}
			}
			echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
		} else {
			$this->edit($form_data,$id);
		}
	}
	
	function cetak()
	{
		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(
			'title' => 	'Raw Material',
			'rs' 	=> 	$this->mdl->getAll2($page,$rows,$key),
		);	
		
		$this->load->view('bahanbaku/print',$data);
		
	}
	
	function excel()
	{
		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(
			'title' => 	'Raw Material',
			'rs' 	=> 	$this->mdl->getAll2($page,$rows,$key),
		);
		
		$filename ="Raw Material.xls";
		header('Content-type: application/ms-excel');
		header('Content-Disposition: attachment; filename='.$filename);
		$this->load->view('bahanbaku/print',$data);
	}
}