<?php

CLASS llrk extends RasioProject
{
	protected $set;
	protected $enableCk = false;
	protected $report;
	protected $baseTitle;
	protected $kdac1 = '0.0.06.09';
	protected $kdac2 = '0.0.06.14';

	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     	= 'Laba Rugi Kotor';
		$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("Cetak", "Pdf", "Excel");
		$this->set->groupDis  	= array();
		$this->set->groupBy   	= "";
		$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();
		
		$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" => "Account", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kdac1', "size" => 10, 'value' => $this->kdac1, 'after' => 'load("mgud", "false", "false")')));
		$form[] = array('row' => $row, 'col' => 250, "label" => array("name" => "S/d", "width" => 30), "input" => array("type" => 'text',	'attr' => array("name" => 'kdac2', "size" => 10, 'value' => $this->kdac2, 'after' => 'load("mgud", "false", "false")')));

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

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 35, "label" => array("name" => "S/d Barang", "width" => 90), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_inv2', "size" => 9, 'after' => 'load("minv", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_inv2=>nm_inv')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_inv2', "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 display()
	{
		$res[] = array('id' => 'kdac',	'nama' => 'Account ',  	'type' => 'text',   'width' => 80,  'size' => 100);
		$res[] = array('id' => 'kd_inv',	'nama' => 'Kode ',  	'type' => 'text',   'width' => 200,  'size' => 100);
		$res[] = array('id' => 'nm_inv',	'nama' => 'Nama',  	'type' => 'text',   'width' => 250,  'size' => 30);

		$res[] = array('id' => 'jual',		'nama' => 'Penjualan',  'type' => 'numeric', 'width' => 130,  'size' => 15, 'dec' => 2, 'link' => array("url" => $this->baseUrl.'llrk/view/&paper=P&convert=cetak&mode=det&date1='.$_GET['date1'].'&date2='.$_GET['date2'], 'attr' => array("kd_inv", "kdac")));
		$res[] = array('id' => 'hpp',		'nama' => 'HPP', 	'type' => 'numeric', 'width' => 130,  'size' => 15, 'dec' => 2);
		$res[] = array('id' => 'margin',	'nama' => 'Margin', 'type' => 'numeric', 'width' => 100,  'size' => 15, 'dec' => 2);
		$res[] = array('id' => 'marginp',	'nama' => '%',   	'type' => 'numeric', 'width' => 50,  'size' => 15, 'dec' => 2);
		$res[] = array('id' => 'qty',		'nama' => 'Qty',   	'type' => 'numeric', 'width' => 70,  'size' => 15, 'dec' => 2);

		return $res;
	}
	
	protected function display_det()
	{
		$res[] = array('id' => 'tgl',	'nama' => 'Tanggal ',  	'type' => 'text',   'width' => 80,  'size' => 100);
		$res[] = array('id' => 'nojnl',	'nama' => 'No. Transaksi ',  	'type' => 'text',   'width' => 120,  'size' => 100);
		$res[] = array('id' => 'kdac',	'nama' => 'Account ',  	'type' => 'text',   'width' => 80,  'size' => 100);
		$res[] = array('id' => 'kd_inv',	'nama' => 'Kode ',  	'type' => 'text',   'width' => 200,  'size' => 100);

		$res[] = array('id' => 'qty',		'nama' => 'Qty',   	'type' => 'numeric', 'width' => 70,  'size' => 15, 'dec' => 2);
		$res[] = array('id' => 'harga',		'nama' => 'Harga',   	'type' => 'numeric', 'width' => 100,  'size' => 15, 'dec' => 2);
		$res[] = array('id' => 'mar',		'nama' => 'HPP',   	'type' => 'numeric', 'width' => 100,  'size' => 15, 'dec' => 2);

		$res[] = array('id' => 'jual',		'nama' => 'Penjualan',  'type' => 'numeric', 'width' => 130,  'size' => 15, 'dec' => 2, 'induk' => 'Total');
		$res[] = array('id' => 'hpp',		'nama' => 'HPP', 	'type' => 'numeric', 'width' => 130,  'size' => 15, 'dec' => 2, 'induk' => 'Total');
		$res[] = array('id' => 'margin',	'nama' => 'Margin', 'type' => 'numeric', 'width' => 100,  'size' => 15, 'dec' => 2, 'induk' => 'Total');
		$res[] = array('id' => 'marginp',	'nama' => '%',   	'type' => 'numeric', 'width' => 50,  'size' => 15, 'dec' => 2);

		return $res;
	}


	public function query()
	{
		$filter = $this->set->filter;
		
		if($_GET['mode'] == 'det'){
			$sql = $this->em->addSelect("a.nojnl, a.tgl, a.kd_inv, a.kd_counter as kdac, a.nm_inv, b.nmac, a.total as jual, a.debet as hpp, a.masuk as qty, a.harga");
		}else{
			$sql = $this->em->addSelect("a.kd_inv, a.kd_counter as kdac, max(a.nm_inv) as nm_inv, max(b.nmac) as nmac, sum(COALESCE(a.total,0)) as jual, sum(COALESCE(a.debet,0)) as hpp, sum(COALESCE(a.masuk,0)) as qty");
		}
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->addLeftJoin("acc", 'b', array("a.kdac = b.kdac"));
		$sql = $this->em->getWhere("(a.kdac = '3.1.01.10' )");
		$sql = $this->em->andWhere("a.alias = 'NJL'");
		$sql = $this->em->andWhere("a.tgl >= '".date('Y-m-d 00:00:00', strtotime($filter->date1->value))."'");
		$sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($filter->date2->value))."'");
		if(!empty($filter->kdac1->value)){
			$sql = $this->em->andWhere("a.kd_counter >= '".$filter->kdac1->value."'");
		}
		if(!empty($filter->kdac2->value)){
			$sql = $this->em->andWhere("a.kd_counter <= '".$filter->kdac2->value."'");
		}
		if(!empty($filter->kd_inv->value)){
			$sql = $this->em->andWhere("a.kd_inv >= '".$filter->kd_inv->value."'");
		}
		if(!empty($filter->kd_inv2->value)){
			$sql = $this->em->andWhere("a.kd_inv <= '".$filter->kd_inv2->value."'");
		}
		if($_GET['mode'] == 'det'){
			$sql = $this->em->andWhere("a.kd_inv = '".$_GET['kd_inv']."'");
			$sql = $this->em->andWhere("a.kd_counter = '".$_GET['kdac']."'");
			$sql = $this->em->getMultiSort("a.tgl, a.nojnl");
		}else{
			$sql = $this->em->getGroupBy("a.kd_inv, a.kd_counter");
			$sql = $this->em->getMultiSort("a.kd_counter, a.kd_inv");
		}

		ini_set("display_errors", "On");
		$eks = $this->em->queryEks($sql);
		ini_set("display_errors", "Off");
		$tmp = $this->em->getFetchObject($eks);
		
		return $tmp;
	}

	public function query_det($tmp)
	{
		$res = array();
		$tot = array();
		foreach($tmp as $r){
			$r->tgl = date("d/m/Y", strtotime($r->tgl));
			$r->mar = $r->hpp / $r->qty;
			$r->margin = $r->jual - $r->hpp;
			$r->marginp = ($r->margin * 100) / $r->jual;

			$tot['jual'] 	= $tot['jual'] + $r->jual;
			$tot['hpp'] 	= $tot['hpp'] + $r->hpp;
			$tot['margin'] 	= $tot['margin'] + $r->margin;

			$res[] = $r;
		}
		
		$i = (object)'apank';
		$i->kd_inv 	= 'T O T A L';
		$i->jual 	= $tot['jual'];
		$i->hpp 	= $tot['hpp'];
		$i->margin 	= $tot['margin'];
		$i->marginp = ($i->margin * 100) / $i->jual;
		$i->css 	= 'padding-bottom: 10px; padding-top: 10px; font-weight: bold; font-style: italic;';
		$i->nolink 	= true;
		$i->garisa 	= true;
		$res[] = $i;
			
		return $res;
	}

	public function query_rekap($tmp)
	{
		$res = array();
		$gTot = array();
		foreach($tmp as $r){
			$r->margin = $r->jual - $r->hpp;
			$r->marginp = ($r->margin * 100) / $r->jual;
			
			$res[$r->kdac]['kode'] = $r->kdac;
			$res[$r->kdac]['nama'] = $r->nmac;
			$res[$r->kdac]['data'][$r->kd_inv] = $r;
			
			$gTot['jual'] 	= $gTot['jual'] + $r->jual;
			$gTot['hpp'] 	= $gTot['hpp'] + $r->hpp;
			$gTot['margin'] = $gTot['margin'] + $r->margin;
		}
		
		
		$tmp = $res;
		$res = array();
		$gTot = array();
		foreach($tmp as $kdac => $row){
			$tot = array();
			foreach($row['data'] as $k => $r){
				$tot['jual'] 	= $tot['jual'] + $r->jual;
				$tot['hpp'] 	= $tot['hpp'] + $r->hpp;
				$tot['margin'] 	= $tot['margin'] + $r->margin;

				$gTot['jual'] 	= $gTot['jual'] + $r->jual;
				$gTot['hpp'] 	= $gTot['hpp'] + $r->hpp;
				$gTot['margin'] = $gTot['margin'] + $r->margin;

				$res[] = $r;
			}

			$i = (object)'apank';
			$i->nm_inv 	= 'T O T A L';
			$i->jual 	= $tot['jual'];
			$i->hpp 	= $tot['hpp'];
			$i->margin 	= $tot['margin'];
			$i->marginp = ($i->margin * 100) / $i->jual;
			$i->css 	= 'padding-bottom: 10px; padding-top: 10px; font-weight: bold; font-style: italic;';
			$i->nolink 	= true;
			$i->garisa 	= true;
			$res[] = $i;

			$i = (object)'apank';
			$i->kdac 	= '&nbsp;';
			$i->nolink 	= true;
			$res[] = $i;
		}

		$i = (object)'apank';
		$i->nm_inv 	= 'GRAND TOTAL';
		$i->jual 	= $gTot['jual'];
		$i->hpp 	= $gTot['hpp'];
		$i->margin 	= $gTot['margin'];
		$i->marginp = ($i->margin * 100) / $i->jual;
			$i->css 	= 'padding-bottom: 10px; padding-top: 10px; font-weight: bold;';
		$i->nolink 	= true;
		$i->garisa 	= true;
		$res[] = $i;

		$i = (object)'apank';
		$i->kdac 	= '&nbsp;';
		$i->nolink 	= true;
		$res[] = $i;

		$i = (object)'apank';
		$i->kdac 	= '&nbsp;';
		$i->nolink 	= true;
		$res[] = $i;

		$i = (object)'apank';
		$i->kd_inv 	= 'Disetujui Oleh,';
		$i->nm_inv 	= 'Dibuat Oleh,';
		$i->nolink 	= true;
		$i->css 	= 'text-align: center; ';
		$res[] = $i;

		$i = (object)'apank';
		$i->kd_inv 	= '( SUSAN )';
		$i->nm_inv 	= '( '.$this->login['kd_user'].' )';
		$i->nolink 	= true;
		$i->css 	= 'text-align: center; padding-top: 30px';
		$res[] = $i;
		
		return $res;
	}

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

	public function View()
	{
		$mode 	= strtolower('detail');
		$query 	= "query_".$mode;
		$tmp = $this->query();
		$this->set->query 	= false;
		
		if($_GET['mode'] == 'det'){
			$this->set->dataLoad= $this->query_det($tmp);
			$this->set->display	= $this->display_det();
		}else{
			$this->set->dataLoad= $this->query_rekap($tmp);
			$this->set->display	= $this->display();
		}
		


		$this->data = $this->report->getDef($this->set);
		$this->layout = "report";
	}
}