<?php

CLASS lnjls extends RasioProject
{
	protected $set;
	protected $enableCk = false;
	protected $report;
	protected $baseTitle;

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

	protected function set()
	{
		$this->report       	= new Report;
		$this->set->em      	= $this->em;
		$this->set->kode    	= 'ACC-001';
		$this->set->date      	= date("d/m/Y H:i");
		$this->set->modul     	= 'Laporan Penjualan Per Sales';
		$this->set->form      	= $this->setForm();
		$this->set->filter    	= $this->report->setFilter($_GET, $this->setForm());
		$this->set->notNull   	= array("date1", "date2", "paper");
		$this->set->button    	= array("View", "Cetak", "Pdf", "Excel", "Csv");
		$this->set->groupDis  	= array("Sales" => "kd_sales", " " => "nm_sales");
		$this->set->groupBy   	= "kd_sales";
		$this->set->hitung   	= array();
		$this->set->rowPage   	= 60;
		$this->baseTitle      	= $this->set->modul;
		
		return $this->set;
	}

	protected function defValue()
	{
		$lap = new Lap;
		$res->cabang  = '';
		return $res; 
	}

	protected function setForm()
	{
		$this->defVal = $this->defValue();

		if(!empty($_GET['nonjl'])){
			$form[] = array('row' =>  0,  'col' => 25,   "label" => array("name" => "No.Nota",   "width" => 100),  "input" => array("type" => 'text',  'attr' => array("name" => 'nonjl')));
		}
		
		$row = 0;
		$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "Periode",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date1', "size" => 10, 'class' => 'tanggal', 'value' => date("01/m/Y"))));
		$form[] = array('row' =>  $row,  'col' => 250, "label" => array("name" => "s/d",     "width" => 30),   "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date2', "size" => 10, 'class' => 'tanggal', 'value' => date("d/m/Y"))));

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Profit Center", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_cabang', "size" => 9, 'after' => 'load("cabang", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_cabang=>nm_cabang')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_cabang', "size" => 30, 'readonly' => true)));

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Customer", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 9, 'after' => 'load("mastercust", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub=>nm_sub')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30, 'readonly' => true)));

		$form[] = array('row' => $row, 'col' => 430, "label" => array("name" => "S/d", "width" => 30), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub2', "size" => 9, 'after' => 'load("mastercust", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub2=>nm_sub')));
		$form[] = array('row' => $row, 'col' => 550, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub2', "size" => 30, 'readonly' => true)));

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Sales", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sales', "size" => 9, 'after' => 'load("msales", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sales=>nm_sub', 'load' => 'sales')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sales', "size" => 30, 'readonly' => true)));

		$form[] = array('row' => $row, 'col' => 430, "label" => array("name" => "S/d", "width" => 30), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sales2', "size" => 9, 'after' => 'load("msales", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sales2=>nm_sub', 'load' => 'sales')));
		$form[] = array('row' => $row, 'col' => 550, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sales2', "size" => 30, 'readonly' => true)));

		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "Paper",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'paper', "size" => 2, 'value' => 'P', 'after' => 'select()', "select" => 'paper')));

		return $form;
	}

	protected function setReport()
	{
		$res[] = array('id' => '_no',		'nama' => 'No',   		'type' => 'no', 	'width' => 20,  'size' => 10);
		$res[] = array('id' => 'tgl',		'nama' => 'Tanggal',   	'type' => 'text', 	'width' => 100,  'size' => 10);
		$res[] = array('id' => 'nonjl',		'nama' => 'No.Nota',  	'type' => 'text',   'width' => 130,  'size' => 30, 'link' => array("attr" => array("nonjl"), 'url' => $this->baseUrl.'ljnota/View/&date1=01/10/2010&date2='.date("d/m/Y").'&mode=DETAIL&paper=P&convert=cetak'));
		$res[] = array('id' => 'total',		'nama' => 'Nilai',  	'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric');
		$res[] = array('id' => 'jt',		'nama' => 'Jt. Tempo',   'type' => 'text', 	'width' => 100,  'size' => 10, 'align' => 'center');
		
		$res[] = array('id' => 'ket',		'nama' => 'Keterangan', 'type' => 'text',   'width' => 200,  'size' => 60);

		return $res;
	}

	public function getData()
	{
		$filter = $this->set->filter;

		$sql = $this->em->addSelect("
						a.*, b.*
						, to_char(a.tgl, 'dd/mm/yyyy') as tgl
						,a.tgl as tgla
						");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin("njld", 'b', array("a.nonjl = b.nonjl"));
		$sql = $this->em->getWhere("a.control = '1'");
		if(!empty($filter->date1->value)){
			$sql = $this->em->andWhere("a.tgl >= '".date('Y-m-d 00:00:00', strtotime($filter->date1->value))."'");
		}
		if(!empty($filter->date2->value)){
			$sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($filter->date2->value))."'");
		}
		if(!empty($filter->kd_cabang->value)){
			$sql = $this->em->andWhere("a.kd_cabang = '".$filter->kd_cabang->value."'");
		}
		if(!empty($filter->kd_sub->value)){
			$sql = $this->em->andWhere("a.kd_sub >= '".$filter->kd_sub->value."'");
		}
		if(!empty($filter->kd_sub2->value)){
			$sql = $this->em->andWhere("a.kd_sub <= '".$filter->kd_sub2->value."'");
		}
		if(!empty($filter->kd_sales->value)){
			$sql = $this->em->andWhere("a.kd_sales >= '".$filter->kd_sales->value."'");
		}
		if(!empty($filter->kd_sales2->value)){
			$sql = $this->em->andWhere("a.kd_sales <= '".$filter->kd_sales2->value."'");
		}
		if(!empty($filter->kd_inv->value)){
			$sql = $this->em->andWhere("b.kd_inv = '".$filter->kd_inv->value."'");
		}
		if(!empty($_GET['nonjl'])){
			$sql = $this->em->andWhere("a.nonjl = '".$_GET['nonjl']."'");
		}

		$sql = $this->em->getMultiSort("a.kd_sales, a.tgl asc, a.nonjl asc");
		$eks = $this->em->queryEks($sql);
		$tmp = $this->em->getFetchObject($eks);
		$res = array();
		$dte = new Date;
		$det = array();
		$byr = array();
		$kde = new Kode;
		foreach($tmp as $r){
			$tot = $kde->totBertingkat($r);
			
			$i = '';
			$i->nonjl 		= $r->nonjl;
			$i->tgl 		= $r->tgl;
			$i->kd_sales 	= $r->kd_sales;
			$i->nm_sales 	= $r->nm_sales;
			$i->jen_bayar 	= $r->jen_bayar;
			$i->jt 			= $dte->getPenjumlahanTanggal($r->tgla, 0, (int)$r->tob, 0);
			$i->jt			= date("d/m/Y", strtotime($i->jt));
			$i->total		= $r->gtot;
			$i->ket			= $r->jen_bayar.' '.$r->ket.' '.$r->ket2.' '.$r->ket3;
			
			$id = $r->nonjl;
			$res[$id] = $i;
			
			
			$id = $r->kd_sales;
			$det[$id][$r->kd_inv]->kd_inv = $r->kd_inv;
			$det[$id][$r->kd_inv]->nm_inv = $r->nm_inv;
			$det[$id][$r->kd_inv]->total  = $det[$id][$r->kd_inv]->total + $tot['tot'];
		}
		
		foreach($res as $r){
			$id = $r->kd_sales;
			$byr[$id][$r->jen_bayar] = $byr[$id][$r->jen_bayar] + $r->total;
		}

		$rep = new SELECT_APP_PENJUALAN;
			
		$rekap = array();
		foreach($byr as $sls => $row){
			$dis = "<table>";
			foreach($rep->JEN_BAYAR() as $iB => $rB){
				
				$nom = 0;
				if(!empty($row[$iB])){
					$nom = $row[$iB];
				}
				
				$dis.= "<tr>";
				$dis.= "<td style='width: 255px; font-size: 12px'>".$iB."</td>";
				$dis.= "<td style='width: 100px; font-size: 12px; text-align: right'>".number_format($nom)."</td>";
				$dis.= "</tr>";
			}
			
			$dis.= "<tr>";
			$dis.= "<td colspan='2'><hr></td>";
			$dis.= "</tr>";
			foreach($det[$sls] as $rD){
				$dis.= "<tr>";
				$dis.= "<td style='width: 200px; font-size: 12px'>".$rD->nm_inv."</td>";
				$dis.= "<td style='width: 100px; font-size: 12px; text-align: right'>".number_format($rD->total)."</td>";
				$dis.= "</tr>";
			}
			
			$dis.= "</table>";
			
			$rekap[$sls] = $dis;
		}
		
		$this->set->cutGroup = true;
		$this->set->sisipGroup = $rekap;
		
		// var_dump($this->set->sisipGroup);
		
		return $res;
	}

	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/report";
	}

	public function View()
	{
	    $this->set->header = "<style>*{font-weight: bold;}</style>";
		$this->set->display	= $this->setReport();
		$this->set->query 	= false;
		$this->set->dataLoad = $this->getData();
		$this->data = $this->report->getDef($this->set);
		$this->layout = "report";
	}
}