<?php

CLASS jcolect extends RasioProject
{
	protected $set;
	protected $enableCk = true;
	protected $jenis = 11;

	public function __construct($config)
	{
		$this->getConfig($config);
		$this->validation();
		$this->set();
	}

	protected function set()
	{
		$this->set->em 			= $this->em;
		$this->set->modul 		= 'Penagihan';
		$this->set->dBaseH 		= 'jpak';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('nopak');
		$this->set->where 		= array("(jenis = '".$this->jenis."')");
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array();
		$this->set->notNull 	= array("kd_sopir");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("Save", "First", "Prev", "Next", "Last", "Browse", "Add", "Del", "Edit", "Final", "prinT");
		$this->set->addClear 	= true;
		$this->set->detail 		= $this->setDetail();
		$this->set->token 		= strtoupper('COL'.date("ymdHis"));
		$this->set->funct 		= '_creatingPak';
		
		if($_GET['nota'] == 'undefined' or $_GET['app'] == 'undefined'){
			$this->set->where 	= array("(jenis = '".$this->jenis."')", "upper(status) = 'FINAL'", "tgl_app is null");
		}
		
		return $this->set;
	}
	
	protected function setForm()
	{

		$form[] = array('row' => 0, 'col' => 25, "label" => array("name" => "No. Persiapan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nopak', "size" => 20, 'readonly' => true)));
		
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "Customer", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sgudang', "size" => 9, 	'after' => 'load("master-cust", "false", "loadDisplay")', 'load' => 'dist', 'display' => 'loadDisplay', 'target' => 'nm_sgudang=>nm_sub')));
		$form[] = array('row' => 25, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sgudang', "size" => 30, 'readonly' => true)));

		$form[] = array('row' => 50, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sales', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Sales", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sales', "size" => 9, 'after' => 'select("")', 'select' => 'sales', 'select_width' => 280, 'select_height' => 245, 'select_label' => "Kode=>50;Name=>200", 'select_target' => "name=>nm_sales", 'select_warning' => true, 'display' => 'selectDisplay')));
		
		$form[] = array('row' => 0, 'col' => 500, "label" => array("name" => "Tanggal", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl', "size" => 10, 'class' => 'tanggal')));
		$form[] = array('row' => 0, 'col' => 690, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'control', "size" => 10, 'readonly' => true)));

		$form[] = array('row' => 25, 'col' => 670, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sopir', "size" => 25, 'readonly' => true)));
		$form[] = array('row' => 25, 'col' => 500, "label" => array("name" => "Collector", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sopir', "size" => 6, 'after' => 'load("mkolektor", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sopir=>nm_sub')));
		
		// $form[] = array('row' => 50, 'col' => 500, "label" => array("name" => "No.Pol", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nopol', "size" => 10)));
		
		$form[] = array('row' => 50, 'col' => 600, "label" => array("name" => "<input type='button' value='Browsing Nota' id='_loadSo'>", "width" => 100), "input" => false);
		
		$form[] = array('row' => 400, 'col' => 25, "label" => array("name" => "Keterangan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', "size" => 40)));
		$form[] = array('row' => 425, 'col' => 25, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket2', "size" => 40)));
		$form[] = array('row' => 450, 'col' => 25, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket3', "size" => 40)));


		
		$form[] = array('row' => 400, 'col' => 650, "label" => array("name" => "Total", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'total', 'size' => 10, 'readonly' => true, 'class' => 'decimal')));
		
		$form[] = array('row' => 500, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 500, 'col' => 500, "label" => array("name" => "Last", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chtime', 'size' => 20, 'readonly' => true)));
		return $form;
	}
	
	public function setDetail()
	{
		$res->width 	= 900;
		$res->height	= 250;
		$res->col		= 25;
		$res->row		= 105;
		$res->data		= 'jpakd';
		$res->pk		= 'nopak';
		$res->sort		= 'nopakd';
		$res->mode		= 'view';
		
		$res->attr[] = array("label" => 'No. Nota',	"attr" => 'nonjl', 	'width' => 130, 'readonly' => true);
		$res->attr[] = array("label" => 'Tgl. Nota',"attr" => 'nota_tgl', 	'width' => 80, 'readonly' => true, 'mode' => 'date');
		$res->attr[] = array("label" => 'Tgl. JT',	"attr" => 'nota_jt', 	'width' => 80, 'readonly' => true, 'mode' => 'date');
		$res->attr[] = array("label" => 'Customer',	"attr" => 'nm_cust', 	'width' => 150, 'readonly' => true);
		$res->attr[] = array("label" => 'Alamat',	"attr" => 'alamat', 	'width' => 250, 'readonly' => true);
		$res->attr[] = array("label" => 'Nilai',	"attr" => 'harga', 		'width' => 100, 'class' => 'decimal', 'readonly' => true);

		return $res;
	}
	
	protected function setGrid()
	{
		$kode = new Kode;
		$res->page 	= 50;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		
		$res->attr[]	= array("label" => 'No. Transaksi', 'attr' => 'a.nopak', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Tanggal', 	'attr' => 'a.tgl', "sort" => 'true', "width" => 70, 'align' => 'center', 'type' => 'date');
		$res->attr[]	= array("label" => 'Customer', 'attr' => 'a.nm_sgudang', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Sopir', 'attr' => 'a.nm_sales', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Colector', 'attr' => 'a.nm_sopir', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Status', 'attr' => 'a.status', "sort" => 'true', "width" => 60, 'align' => 'left');
		
		return $res;
	}
	
	protected function setPrint()
	{
		$kode = new Kode;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		$res->width = '1900px';
		
		$res->attr[]	= array("label" => 'No. Persiapan', 'attr' => 'a.nopak', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true, 'default' => 'nopak');
		return $res;
	}
	
	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/index";
	}
	
	public function data()
	{
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayData";
		$this->layout = "view";
	}
	
	public function Find()
	{
		$basic = new Basic;
		$this->data = $basic->find($_GET, $this->set);
		$this->view = "default/displayForm";
		$this->layout = "view";
	}
	
	public function cekStatus($nojnl)
	{
		return true;
	}
	
	protected function validasi()
	{
		$basic = new Basic;
		$error[] = $basic->validasiBasic($_POST, $this->set);
		$error[] = $this->cekStatus($_POST['nopak']);
		
		return $basic->getError($error);
	}
	

	protected function kode($post)
	{
		$kode  = new Kode;
		$basic = new Basic;
		$prefik = 'COL/IKD/'.date("ym", strtotime($post['tgl'])).'/';
		$lastU  = $basic->getLastNum($this->set->dBaseH, 'nopak', $prefik, '', 5, $this->set);
		return $prefik.$kode->getRight($lastU, 5, 0);
	}
	
	protected function nKode($post)
	{
		if($post['_prosesAction'] == 'simpan'){
			return $this->kode($post);
		}else{
			$kode = new Kode;
			$ex = explode(".", $post['nopak']);
			$asli = $ex[0];
			$tamb = $ex[1];
			$next = $kode->setAbj($tamb, 1);
			return $asli.'.'.$next;
		}
	}
	
	public function saveDet($id, $post, $nom)
	{
		$date = new Date;
		$kode = new Kode;
		$attr = array();
		foreach($this->set->detail->attr as $k => $r){
			$no = $r['attr'];
			$attr[$no] = $post[$no][$id];
		}
		$attr['nopakd'] = $post['nopak'].'.'.$kode->getRight($nom, 2, 0);
		$attr['nopak']  = $post['nopak'];
		$attr['nota_jt'] = $date->serialDate($attr['nota_jt']);
		$attr['nota_tgl'] = $date->serialDate($attr['nota_tgl']);
		
		$sql = $this->em->addInsert($this->set->detail->data);
		$sql = $this->em->addAttrIns($attr);

		return $sql;
	}
	
	public function deleteDet($nopak)
	{
		$sql = $this->em->addDelete($this->set->detail->data);
		$sql = $this->em->getWhere("nopak = '$nopak'");
		return $sql;
	}
	
	public function proses()
	{
		$basic = new Basic;
		$validasi = $this->validasi();
		if($validasi == true){
			
			ini_set('display_errors',1); 
			$date = new Date;
			
			$tambah = array();
			$tambah['jenis'] = $this->jenis;
			$tambah['control'] = 1;
			$_POST['tgl'] = $date->serialDate($_POST['tgl']);
			
			if($_POST['_prosesAction'] == 'simpan'){
				$_POST['nopak'] = $this->kode($_POST);
			}
			
			$no = 1;
			$qb = array();
			$qb[] = $this->deleteDet($_POST['nopak']);
			$no = 1;
			
			foreach($_POST['nonjl'] as $n => $row){
				if(!empty($row)){
					$qb[] = $this->saveDet($n, $_POST, $no++);
				}
			}
			
			
			// foreach($qb as $r){
				// echo $r.';<br>';
			// }
			
			$basic->proses($_POST, $this->set, $tambah, $qb);
		}
		$this->layout = "null";
	}
	
	public function Delete()
	{
		$_GET['nopak'] = str_ireplace("__--__", "/", $_GET['nopak']);
		
		$simpan = $this->cekStatus($_GET['nopak'], true);
		if($simpan == true){
			$basic = new Basic;
			$this->data = $basic->delete2($_GET, $this->set);
		}
		$this->layout = "null";
	}
	
	public function cetak()
	{
		$data = array();
		
		$no = 1;
		$data[$no]->label 	= "Cetak Pengiriman";
		$data[$no]->link 	= $this->baseUrl.'Jgud/cetakPersiapan/&filPrintanopak='.$_GET['filPrintanopak'];

		$no++;
		$data[$no]->label 	= "Cetak Pengiriman Detail";
		$data[$no]->link 	= $this->baseUrl.'Jgud/cetakPerincian/&filPrintanopak='.$_GET['filPrintanopak'];
		
		$this->data->judul 	= "Cetak No. ".$_GET['filPrintanopak'];
		$this->data->form 	= $data;
		$this->layout = "pilForm";
	}
	
	public function cetakPersiapan()
	{
		if(!empty($_GET['nojnl'])){
			$_GET['filPrintanopak'] = $_GET['nojnl'];
		}
		$this->data->kode  = $_GET['filPrintanopak'];
		$this->data->nojnl = $_GET['filPrintanopak'];
		$this->data->pdf = 'kirim';
		$this->layout = "viewPdf";
	}
	
	public function Cetakperincian()
	{
		if(!empty($_GET['nojnl'])){
			$_GET['filPrintanopak'] = $_GET['nojnl'];
		}
		$this->data->kode  = $_GET['filPrintanopak'];
		$this->data->nojnl = $_GET['filPrintanopak'];
		$this->data->pdf = 'kirimd';
		$this->layout = "viewPdf";
	}
	
	public function detailPak($nojnl)
	{
		$sql = $this->em->addSelect("a.kd_inv, a.nm_inv, sum(a.qtyk * COALESCE(b.skala, 0)) as qty, min(d.bruto) as bruto, max(d.netto) as netto, max(c.skala) as skala");
		$sql = $this->em->getFrom("jpakd", 'a');
		$sql = $this->em->addLeftJoin('sat', 'b', array("a.kd_inv = b.kd_inv", "upper(a.satuan) = upper(b.nm_sat)"));
		$sql = $this->em->addLeftJoin('sat', 'c', array("a.kd_inv = c.kd_inv", " upper(c.nm_sat) = 'DOS'"));
		$sql = $this->em->addLeftJoin('sat', 'd', array("a.kd_inv = d.kd_inv", " d.skala = '1'"));
		$sql = $this->em->getWhere("a.nopak = '$nojnl'");
		$sql = $this->em->getGroupBy("a.kd_inv, a.nm_inv");
		$sql = $this->em->getMultiSort("a.kd_inv");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function detailPakRinci($nojnl)
	{
		$sql = $this->em->addSelect("b.*
									");
		$sql = $this->em->getFrom("jpakd", 'a');
		$sql = $this->em->addLeftJoin('njld', 'b', array("a.nonjl = b.nonjl"));
		// $sql = $this->em->addLeftJoin('sat', 'c', array("a.kd_inv = c.kd_inv", " upper(c.nm_sat) = 'DOS'"));
		// $sql = $this->em->addLeftJoin('sat', 'd', array("a.kd_inv = d.kd_inv", " d.skala = '1'"));
		// $sql = $this->em->addLeftJoin('jsod', 'e', array("a.nojsod = e.nojsod"));
		// $sql = $this->em->addLeftJoin('jso', 'f', array("e.nojso = f.nojso"));
		$sql = $this->em->getWhere("a.nopak = '$nojnl'");
		$sql = $this->em->getMultiSort("a.nonjl");

		$eks = $this->em->queryEks($sql);
		$tmp = $this->em->getFetchObject($eks);
		
		$data = array();
		foreach($tmp as $r){
			$id = $r->kd_inv;
			if(empty($data[$id])){
				$data[$id]->kd_inv = $r->kd_inv;
				$data[$id]->nm_inv = $r->nm_inv;
			}
			
			if($r->skala <= 0){
				$r->skala = 1;
			}
			
			$data[$id]->qty = $data[$id]->qty + ($r->qty * $r->skala);
		}
		
		foreach($data as $k => $r){
			
			$sat = $this->em->getFindBy("satuan", "*", array("kd_inv = '".$r->kd_inv."'"), "skala desc");
			$sat = $sat[0];
			$data[$k]->karton = $sat->nmsat;
			$data[$k]->skala  = $sat->skala;
		}
		
		return $data;
	}
	
	public function kirimd()
	{
		$this->data->data 	= $this->em->getFind("jpak", "*", array("nopak = '".$_GET['nojnl']."'"));
		$this->data->det 	= $this->detailPakRinci($_GET['nojnl']);
		
		$this->data->set 	= $this->set;
		$this->data->display= "kirimd";
		$this->view 	= "rangkap";
		$this->layout = "printFormPdf";
	}
	
	public function kirim()
	{
		$this->data->data 	= $this->em->getFind("jpak", "*", array("nopak = '".$_GET['nojnl']."'"));
		$this->data->det 	= $this->em->getFindBy("jpakd", "*", array("nopak = '".$_GET['nojnl']."'"), "nopakd");
		$this->data->set 	= $this->set;
		$this->data->display= "kirim";
		$this->view 		= "rangkap";
		$this->layout = "printFormPdf";
	}
	
	public function load()
	{
		$basic = new Basic;
		$this->data = $basic->load($_GET, $this->set);
		$this->layout = "null";
	}
	
	public function popUp()
	{
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayDataPopUp";
		$this->layout = "view";
	}
	
	public function detSiap()
	{
		$this->layout = "null";
		
		if(empty($_POST['so'])){
			echo "<script>alert('Tidak ada Nota yang dipilih');</script>";
			$this->layout = "null";
			return;
		}
		
		$jso = "";
		foreach($_POST['so'] as $k){
			if(!empty($jso)){
				$jso.= ' or ';
			}
			$jso.= "(a.nonjl = '$k')";
		}
		
		
		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin('sub', 'b', array("a.kd_sub = b.kd_sub"));
		$sql = $this->em->getWhere("a.control = '1'");
		$sql = $this->em->andWhere("$jso");
		$sql = $this->em->getMultiSort("a.nonjl");
		$eks = $this->em->queryEks($sql);
		$tmp = $this->em->getFetchObject($eks);
		
		$token = $_GET['token'];
		$det = $_SESSION['det'][$token];
		
		$date = new Date;
		foreach($tmp as $r){
			
			$jt = $date->getPenjumlahanTanggal($r->tgl, 0, (int)$r->top, 0);
			
			$i = '';
			$i->nonjl 		= $r->nonjl;
			$i->kd_cust 	= $r->kd_sub;
			$i->nm_cust 	= $r->nm_sub;
			$i->nota_tgl 	= date("d/m/Y", strtotime($r->tgl));
			$i->nota_jt 	= date("d/m/Y", strtotime($jt));
			$i->alamat 		= $r->alamat_kirim.' '.$r->alamat_kirim2.' '.$r->alamat_kirim3;
			$i->harga 		= $r->gtot;
			
			
			$det[$r->nonjl] = $i;
		}
		$_SESSION['det'][$token] = $det;
		
		$this->sessionDet(__CLASS__, $this->set, $_SESSION['det'][$token]);
		echo "<script>_listLoadDetail('".__CLASS__."', '_hitPak')</script>";
		echo "<script>$('#apankForm').empty()</script>";
	}
	
	public function Loadso()
	{
		$sql = $this->em->addSelect("b.nonjl");
		$sql = $this->em->getFrom("jpak", 'a');
		$sql = $this->em->addLeftJoin("jpakd", 'b', array("a.nopak = b.nopak"));
		$sql = $this->em->getWhere("a.control = '1'");
		$sql = $this->em->andWhere("a.jenis = '".$this->jenis."'");
		$sql = $this->em->getGroupBy("b.nonjl");
		$pak = $sql;
		
		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin("sub", 'b', array("a.kd_sub = b.kd_sub"));
		$sql = $this->em->addLeftJoin("($pak)", 'c', array("a.nonjl = c.nonjl"));
		$sql = $this->em->getWhere("a.control = '1'");
		$sql = $this->em->andWhere("a.nonjl like 'INV%'");
		$sql = $this->em->andWhere("c.nonjl is null");
		
		if(!empty($_GET['cust'])){
			$sql = $this->em->andWhere("a.kd_sub = '".$_GET['cust']."'");
		}
		if(!empty($_GET['sales'])){
			$sql = $this->em->andWhere("a.kd_sales = '".$_GET['sales']."'");
		}
		
		// $token = $_GET['token'];
		// $det = $_SESSION['det'][$token];
		// foreach($det as $r){
			// $sql = $this->em->andWhere("a.nonjl != '".$r->nonjl."'");
		// }
		
		$sql = $this->em->getMultiSort("a.tgl, a.nonjl, a.nm_sub");
		$eks = $this->em->queryEks($sql);
		$tmp = $this->em->getFetchObject($eks);
		
		$data = "<form method='POST' id='_pilihSo' action=''>";
		$data.= "<table id='_tabBonus'>";
		$data.= "<thead>";
		$data.= "<tr>";
		$data.= "<th width='30' align='center'><input type='checkbox' id='cekAll'></th>";
		$data.= "<th width='130'>No. Nota</th>";
		$data.= "<th width='60'>Tgl. Nota</th>";
		$data.= "<th width='60'>Tgl. JT</th>";
		$data.= "<th width='60'>Kode Cust</th>";
		$data.= "<th width='200'>Nama Cust</th>";
		$data.= "<th width='100'>Sales</th>";
		$data.= "<th width='200'>Alamat</th>";
		$data.= "<th width='200'>Gudang</th>";
		$data.= "<th width='100' align='right'>Nilai</th>";
		$data.= "</tr>";
		$data.= "</thead>";
		$data.= "<tbody>";
		$date = new Date;
		foreach($tmp as $r){
			$jt = $date->getPenjumlahanTanggal($r->tgl, 0, (int)$r->top, 0);
			$id = str_ireplace("/", "_", $r->nonjl);
			$id = str_ireplace(".", "_", $id);
			$data.= "<tr>";
			$data.= "<td><input type='checkbox' class='_cekSo' name='so[".$row->nonjl."]' id='cek_$id' value='".$r->nonjl."'></td>";
			$data.= "<td>".$r->nonjl."</td>";
			$data.= "<td>".date("d/m/Y", strtotime($r->tgl))."</td>";
			$data.= "<td>".date("d/m/Y", strtotime($jt))."</td>";
			$data.= "<td>".$r->kd_sub."</td>";
			$data.= "<td>".$r->nm_sub."</td>";
			$data.= "<td>".$r->nm_sales."</td>";
			$data.= "<td>".$r->alamat_kirim.' '.$r->alamat_kirim2.' '.$r->alamat_kirim3."</td>";
			$data.= "<td>".$r->nm_gudang."</td>";
			$data.= "<td>".number_format($r->gtot)."</td>";
			$data.= "</tr>";
		}
		$data.= "</tbody>";
		$data.= "</table>";
		$data.= "<p style='text-align: center'><input type='submit' value='Pilih'></p>";
		$data.= "</form>";
		
		echo $data;
		echo "<script type='text/javascript'>
			$(document).ready(function(){
				$('#_tabBonus').flexigrid({
					width: 840,
					height: 200,
					title: 'Daftar Nota belum dikirm',
				});
			})
		</script>";

		
		$this->layout = "null";
	}
	
	public function loadDisplay()
	{
		$pk = $this->set->dBasePk[0];
		$data = $this->em->getFind($this->set->dBaseH, '*', array("$pk = '".$_GET['idRef']."'"));
		$data->tgl= date("d/m/Y", strtotime($data->tgl));
		$basic = new Basic;
		$res = $basic->loadDisplay($data, $_GET);
		echo json_encode($res);
		$this->layout = "null";
	}
	
	public function clearDet()
	{
		$det = array();
		if(!empty($_GET['nopak'])){
			$det = $this->em->getFindBy("jpakd", "*", array("nopak = '".$_GET['nopak']."'"), "nopakd");
		}
		$token = $_GET['token'];
		$_SESSION['det'][$token] = $det;
		$this->layout = "null";
	}
}