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

class Ap extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();

		$this->modul = 'ap';
		
		$this->title = 'ACCOUNT PAYABLE';
		$this->type = 'ap';
		
		$this->tbl = 'acc_araphdr';		
		$this->tbl2 = 'acc_arapdet';
		$this->tbl3 = 'mst_perusahaan';
		$this->tbl4 = 'mst_barang';
		$this->tbl5 = 'mat_inchdr_sub';
		$this->pk = 'id';
		$this->pk2 = 'arap_id';
		$this->pk3 = 'KdPrshn';
		$this->pk4 = 'KdBarang';
		$this->pk5 = 'matin_id';
		$this->pkh = 'no';

		$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['rap'] = $this->mdl->getAutoNoAp();
		$data['rap'] = $this->mdl->buat_kodeAp();
		$this->load('tpl',$data['content'], $data);
		$this->load->view($this->modul.'/_cjs');
		$this->load->view($this->modul.'/_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['type']=$this->type;
		$form_data['date']=$this->dmys2ymd($this->input->post('date'));
		$form_data['dateInv']=$this->dmys2ymd($this->input->post('dateInv'));
		$form_data['rate']=$this->input->post('rate');
		$ref_id=$form_data['ref_id'];
		$ap_date=$form_data['date'];
		
		$year	=date("yy",strtotime($ap_date));
		$month	=date("m",strtotime($ap_date));
		$month2	=$month+0;
		$countAP=$this->mdl->countAP($month2);
		$countAP2=$countAP->no+1;
		$countAP=$countAP->no+1;
		$countAP=sprintf("%04d", $countAP);

		// 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"){	
			$ap_no	="AP-".$month."-".$year."-".$countAP;
			$form_data['no']	= $ap_no;
			if ($countItem > 0) {
				$bol=false;
				$msg="Nomor Incoming ini sudah dimasukan di AP!";		
			} else {
				$check = "false";
				while ($check == "false") {
					$checkAP = $this->mdl->checkAP($ap_no);
					foreach ($checkAP as $y) {
						if($y->no > 0){
							$ap_no	="AP-".$month."-".$year."-".sprintf("%04d", $countAP2);
							$form_data['no']	= $ap_no;
							$countAP2++;
						}else{
							$check = "true";
							break;
						}
					}
				}
				$success1 = $this->mdl->add($form_data);	
				
				// INSERT & UPDATE TO AP DET --ir
				$key = $form_data['ref_id'];
				$getMatindet = $this->mdl->getMatindet($key);
				$child_no = 1;
				foreach ($getMatindet as $getMatindet) {
					$amount 		= $getMatindet->price * $getMatindet->qty;
					$arap_id		= $getMatindet->id;	
					$dataDet = array(
						'id' 				=> '',
						'arap_id' 	=> $getMatindet->id,
						'item_id'		=> $getMatindet->mat_id,
						'qty'				=> $getMatindet->qty,
						'price'			=> $getMatindet->price,
						'amount'			=> $amount
					);

					// $getTotAmount = $this->mdl->getTotAmount($key);
					// foreach ($getTotAmount as $getTotAmount) {
					// 	$tot_amount = $getTotAmount->tot_amount;
					// 	break;
					// }

					// $tot_amount = $tot_amount + $amount;
					// $updateHdr = array(
					// 	'tot_amount' 	=> $tot_amount
					// );

					$child_no++;
					$success2 = $this->mdl->addDet($dataDet);
					//$success3 = $this->mdl->editAmount($updateHdr,$arap_id);
				}
				if ($success1 && $success2){				
					$bol=true;	
					$msg="AP berhasil ditambah!";
				} else {
					$bol=false;
					$msg="Tambah data AP gagal.";		
				}
			}
			echo json_encode(array('success'=>$bol,'msg'=>$msg)); 
		} else {
			$this->editarap($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'=> "no"
			);
		}
			$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)
		);
		
		$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);
		
	}
}