	<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Brcr extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->modul = 'brcr';
		
		$this->title = 'BANK & CASH RECEIVABLE';
		$this->type = 'brcr';
		
		$this->tbl0 = 'acc_brbp';		
		$this->tbl = 'acc_araphdr';		
		$this->tbl2 = 'acc_arapdet';
		$this->tbl3 = 'mst_perusahaan';
		$this->tbl4 = 'mst_barang';
		$this->tbl5 = 'mat_outhdr_sub';
		$this->pk0 = 'ref_id';
		$this->pk = 'id';
		$this->pk2 = 'arap_id';
		$this->pk3 = 'KdPrshn';
		$this->pk4 = 'KdBarang';
		$this->pk5 = 'matout_id';
		$this->load->model($this->modul.'_mdl','mdl');
	}
	
	public function index()
	{	
		$data = array(
			'content' 	=> 	$this->modul.'/index',
			't'			=>	$this->session->userdata('t'),
			'u'			=>	$this->session->userdata('u'),
			'h'			=>	$this->session->userdata('h')
			);		
		// $data['ra'] = $this->mdl->getAutoNoAr();
		// $data['ra'] = $this->mdl->buat_kodeAr();
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/_cjs');
		$this->load->view($this->modul.'/_mjs');
	}
	
	public function trx()
	{		
		$this->aksi='t';
		$data = array(
			'content' 	=> 	$this->modul.'/_frm'
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/_frm_cjs');
		$this->load->view($this->modul.'/_frm_cjst');
		$this->load->view($this->modul.'/_frm_mjs');
		
	}
	
	public function tambah()
	{		
		$this->aksi='t';
		$data = array(
			'content' 	=> 	$this->modul.'/_frm'
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/_frm_cjs');
		$this->load->view($this->modul.'/_frm_cjst');
		$this->load->view($this->modul.'/_frm_mjs');
		
	}
	
	public function ubah($id)
	{		
		$this->aksi='u';
		$data = array(
			'content' 	=> $this->modul.'/_frm',
			'r' 		=> $this->mdl->getRow($id)
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/_frm_cjs');
		$this->load->view($this->modul.'/_frm_cjsu');
		$this->load->view($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(){
		$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['date']=$this->dmys2ymd($this->input->post('date'));
		$ref_id=$form_data['ref_id'];
		unset($form_data['customer']);
		// unset($form_data['currency']);
		// unset($form_data['rate']);
		unset($form_data['tot_amount']);
		unset($form_data['tax']);
		unset($form_data['tax_amount']);
		unset($form_data['after_tax']);
		$ar_date=$form_data['date'];


		$year=date("yy",strtotime($ar_date));
		$month =date("m",strtotime($ar_date));
		$month2 =$month+0;
		$countAR= $this->mdl->countAR($month2);
		$countAR2=$countAR->no+1;
		$countAR= $countAR->no + 1;
		$countAR= sprintf("%04d", $countAR);

		// GET ITEM COUNT #ir
		$checkDO = $this->mdl->checkDO2($ref_id);
		foreach ($checkDO as $x) {
			$countDO = $x->ref_id;
			break;
		}	
		
		$pk[$this->pk]=$form_data[$this->pk];
		if ($cmd=="add"){
			$ar_no="BR-".$month."-".$year."-".$countAR;
			$form_data['no'] = $ar_no;	
			if ($countDO > 0) {
				$bol=false;
				$msg="DO ini sudah dimasukan di BR / CR!";		
			} else {
				$check = "false";
				while ($check == "false") {
					$checkAR = $this->mdl->checkAR($ar_no);
					foreach ($checkAR as $y) {
						if($y->no > 0){
							$ar_no	="BR-".$month."-".$year."-".sprintf("%04d", $countAR2);
							$form_data['no']	= $ar_no;
							$countAR2++;
						}else{
							$check = "true";
							break;
						}
					}
				}
				$success1 = $this->mdl->add($form_data);
				if ($success1){				
					$bol=true;	
					$msg="BR / CR ini berhasil ditambah!";
				} else {
					$bol=false;
					$msg="Tambah data BR / CR gagal.";		
				}
			}
			echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
		} else {
			$this->editbrbp($form_data,$pk);
		}
	}
	
	function getformDet($cmd){
		$form_data = $this->input->post();
		
		$pk=array(
			'id' => $form_data[$this->pk]
			);
		if ($cmd=="add"){	
			$this->addDet($form_data);	
		} else {
			$this->editDet($form_data,$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)); 
	}
	
	function reset_journal(){
		$id=$_REQUEST['id'];
		$reset=$_REQUEST['created_journal'];
		$pk=$id; 

		$getHdrRow = $this->mdl->getRow($id);
		foreach ($getHdrRow as $getHdrRow) {
			if($reset == "no") $reset="yes"; else $reset="no";
			$data = array(
				'created_journal'=> $reset
			);
		}
			$success = $this->mdl->editJournal($data,$id);
			if ($success){				
				$bol=true;
				$msg="Reset Journal berhasil.";
			} else {
				$bol=false;
				$msg="Gagal Dirubah.";
			}
		
		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->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),
			'image' => 'http://'.$_SERVER['SERVER_NAME'].':80/doerpsgp/assets/images/sgp.png'
		);
		
		$this->load->library('pdf');
		$this->load->view($this->modul.'/slip',$data);
		
	}

	function slipinv($id)
	{
		$data = array(
			'cmpy_nm'=>$this->session->userdata('nm_cmpy'),
			'rhdr' 	=> 	$this->mdl->getRow($id),
			'rdet' 	=> 	$this->mdl->getDetAll($id),
			'image' => 'http://'.$_SERVER['SERVER_NAME'].':80/doerpsgp/assets/images/sgp.png'
		);
		
		$this->load->library('pdf');
		$this->load->view($this->modul.'/slipinv',$data);
		
	}
	
	function barcode($id)
	{
		$data = array(
			'rhdr' 	=> 	$this->mdl->getRow($id)
		);
		
		$this->load->library('pdf');
		$this->load->view($this->modul.'/slip2',$data);
		
	}
}