<?php

class Report
{
	public $em;
	
	public function setFilter($get, $form)
	{
		$date = new Date;
	
		foreach($form as $row){
			$id = $row['input']['attr']['name'];
			if($row['input']['mode'] == 'date'){
				$get[$id] = $date->serialDate($get[$id]);
			}
			$res->$id->label = $row['label']['name'];
			$res->$id->value = $get[$id];
		}
		return $res;
	}
	
	public function disFilter($set)
	{
		$filter = $set->filter;
		$res = '';
		foreach($set->form as $row){
			$label = trim($row['label']['name']);
			if($label == 's/d'){
				$label = ' s/d ';
			}else{
				if(!empty($res)){
					$label = ', '.$label.' : ';
				}else{
					$label = $label.' : ';
				}
			}
			
			$key = $row['input']['attr']['name'];
			$val = $filter->$key->value;
			if($row['input']['mode'] == 'date'){
				$val = date("d/m/Y", strtotime($val));
			}
			
			$res.= $label.$val;
		}
		return $res;
	}
	
	protected function getNumeric($val, $dec, $nokomma = false)
	{
		$del = ',';
		if($nokomma == true){
			$del = '';
		}
		
		return number_format($val, $dec, '.', "$del");
	}
	
	public function crHead($set, $widthDiv, $height, $pg)
	{
		$date = '';
		if(!empty($set->date)){
			$date = $set->date.' | ';
		}
	
		$level1 = array();
		$level2 = array();
		$rowspan = 1;
		foreach($set->display as $no => $row){
			$idI = $row['id'];
			$judul = $row['nama'];
			if(!empty($row['induk'])){
				$idI = $row['induk'];
				$judul = $row['induk'];
				$row['align'] = 'center';
				
				$idD = $row['id'];
				$level2[$idD] = $row;
				$rowspan = 2;
			}
			if(empty($level1[$idI])){
				$level1[$idI] = $row;
				$level1[$idI]['nama'] = $judul;
			}
			$level1[$idI]['child'][] = $row['id'];
		}
		
		
		$data = '<div class="_bodyLap" style="height: '.$height.'px; width: '.$widthDiv.'px">';
		$data.= '<div class="_page">'.$date.$set->kode.' | Page. '.$pg.'</div>';
		$data.= '<div class="_header">'.strtoupper($set->modul).'</div>';
		$data.= '<div class="_filter">'.$this->disFilter($set).'</div>';
		
		$data.= '<table class="_repTable" cellspacing="0" cellpadding="0">';
		$data.= '<tr>';
		foreach($level1 as $no => $row){
			$width = $row['width'];
			if($row['resize'] == true){
				$width = '';
			}
			
			$align = 'left';
			if(strtoupper($row['type']) == strtoupper('numeric')){
				$align = 'right';
			}
			if(strtoupper($row['type']) == strtoupper('date')){
				$align = 'center';
			}

			$rows = 1;
			$cols = count($row['child']);
			if(count($row['child']) > 1){
				$align = 'center';
			}else{
				if(count($level2) > 0){
					$rows = 2;
				}
			}
			
			$data.= '<td class="_th" rowspan="'.$rows.'" colspan="'.$cols.'" width="'.$width.'px" style="text-align: '.$align.'">'.$row['nama'].'</a>';
		}
		$data.= '</tr>';
		
		if(count($level2) > 0){
			$data.= '<tr>';
			foreach($level2 as $no => $row){
				$width = $row['width'];
				if($row['resize'] == true){
					$width = '';
				}
				
				$align = 'left';
				if(strtoupper($row['type']) == strtoupper('numeric')){
					$align = 'right';
				}
				if(strtoupper($row['type']) == strtoupper('date')){
					$align = 'center';
				}

				$data.= '<td class="_th" width="'.$width.'px" style="text-align: '.$align.'">'.$row['nama'].'</a>';
			}
			$data.= '</tr>';
		}
		
		return $data;
	}
	
	protected function setHit($set)
	{
		$hit = array();
		foreach($set->hitung as $row){
			$row = explode("=>", $row);
			$row = trim($row[0]);
			$hit[$row] = $row;
		}
	
		$cel = array();
		$idN = 0;
		$aw  = 1;
		$noP = 1;
		$no = 1;
		foreach($set->display as $row){
			$id = $row['id'];
			if(!empty($hit[$id])){
			
				$cl = $no - $aw;
				$aw = $no + 1;
			
				$cel[$idN]->cl = $cl;
				$cel[$idN]->nm = '';
				$idN++;

				$cel[$idN]->cl = 1;
				$cel[$idN]->nm = $id;
				$idN++;

			}
			$no++;
		}
		if(count($cel) > 0){
			$cel[$idN]->cl = 100;
			$cel[$idN]->nm = '';
			$idN++;
		}

		return $cel;
	}
	
	public function setFoter($set, $i)
	{
		$jum = count($set->display);
		$res = '<tr>';
		$res.= '<td class="_td" colspan="'.$jum.'"><label style="padding-left: 20px">&nbsp;</label>';
		foreach($set->groupByFo as $key => $row){
			$dat = $row['data'];
			$var = $row['id'];
			$idK = $i->$var;
			$val = $set->$dat;
			$val = $val[$idK]->$row['attr'];
			
			if($row['type'] == 'numeric'){
				$val = $this->getNumeric($val, (int)$row['dec'], ',');
			}
			if($row['type'] == 'integer'){
				$val = $this->getNumeric($val, (int)$row['dec'], '');
			}
			
			
			if($row['attr'] == 'spasi'){
				$res.= '<br><label style="padding-left: 20px">&nbsp;</label>';
			}else{
				$res.= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &raquo; '.$key.' : '.$val;
			}
		}
		$res.= '</td>';
		$res.= '</tr>';
		$res.= '<tr>';
		$res.= '<td class="_td" colspan="'.$jum.'"><label style="padding-left: 20px">&nbsp;</label></td>';
		$res.= '</tr>';
		return $res;
	}
	
	public function getDef2($set)
	{
		$this->em = $set->em;
		
		if($set->query == false){
			$jum = count($set->dataLoad);
			$dta = $set->dataLoad;
		}else{
			$eks = $this->em->queryEks($set->query);
			$dta = $this->em->getFetchObject($eks);
			$jum = $this->em->getNumRows($eks);
		}
		
		$filter = $set->filter;

		$browser = new BrowserDetection;
		$browser = strtoupper($browser->getBrowser());

		if((int)$set->rowPage > 0){
			$row = (int)$set->rowPage;
		}else{
			if(strtoupper($filter->paper->value) == 'P'){
				$row = 80;
			}else{
				$row = 35;
			}
		}
		
		$hit = $this->setHit($set);

		$width = 0;
		foreach($set->display as $r){
			$width = $width + 5;
			$width = $width + $r['width'];
		}
		
		$widthDiv = $width + 4;
		
		$ima = $set->groupBy;
		// if(!empty($grp)){
			// $ima = $grp['kd'];
			// $nma = $grp['val'];
		// }
		
		$vHit = array();
		$mHit = array();
		foreach($set->hitung as $rHit){
			$rHit2 = explode("=>", $rHit);
			$rHit = trim($rHit2[0]);
			$vHit[$rHit] = $rHit;
			
			if(count($rHit2) > 0){
				$ex2 = explode(",", $rHit2[1]);
				foreach($ex2 as $r){
					$ex3 = explode(":", $r);
					$idR = trim($ex3[0]);
					$nmR = trim($ex3[1]);
					if(!empty($idR)){
						$mHit[$rHit]->$idR  = trim($nmR);
					}
				}
			}
			
		}
		$vDec = array();
		$nokomma = array();
		foreach($set->display as $rHit){
			$id = $rHit['id'];
			$vDec[$id] = (int)$rHit['dec'];
			$nokomma[$id] = $rHit['nokomma'];
		}
		
		$data = '';
		
		if(!empty($set->header)){
			$data = $set->header;
		}
		
		$nom = 1;
		$pg = 1;
		$induk = '';
		$noType = 1;
		$no = 1;
		$urut = 1;
		$subTot = array();
		$tot = array();
		$rumus = new RumusReport;
		
		if(!empty($set->before)){
			$data.= $set->before;
		}
		
		if($jum <= 0){
			echo "<script>alert('Not Found');</script>";
		}
		$indDisNot = array();
		$noRumus = 0;
		foreach($dta as $r){
			$noRumus++;
			if($nom == 1){
				$indDisNot = array();
				$data.= $this->crHead($set, $widthDiv, $height, $pg);
			}
			
			if(!empty($ima)){
				if($induk != $r->$ima){
					// if($r->$ima != $r->$nma){
						// $r->$ima = $r->$ima.' &rsaquo; '.$r->$nma;
					// }
				
					$nmGrup = '';
					foreach($set->groupDis as $kGrup => $nGrup){
						if(!empty($nmGrup)){
							$nmGrup.= ', ';
						}
						if(!empty($kGrup)){
							$nmGrup.= $kGrup.' : '.$r->$nGrup;
						}else{
							$nmGrup.= $r->$nGrup;
						}
					}
				
					if($urut > 1){
						if(count($set->hitung) > 0){
							$data.= '<tr>';
							foreach($hit as $kHit => $rHit){
								if($rHit->cl > 0){
									$nm = $rHit->nm;
									if(!empty($nm)){
										
										$nm = $subTot[$nm];
										if(!empty($mHit[$rHit->nm]->funct)){
											$setModeHitung = $mHit[$rHit->nm]->funct;
											$nm = $this->$setModeHitung($set, $iNew, $mHit[$rHit->nm]);
										}
										$nm = $this->getNumeric($nm, $vDec[$rHit->nm], $nokomma[$rHit->nm]);
									}
									if($kHit == 0){
										$nm = 'Total';
									}
									$data.= '<td align="right" class="_hitung" colspan="'.$rHit->cl.'">'.$nm.'</td>';
								}
							}
							$data.= '</tr>';
							$nom++;
						}
						if(count($set->groupByFo) > 0){
							$data.= $this->setFoter($set, $iNew);
							$nom++;
						}
						
						if($set->cutGroup == true){
							$data.= "</table>";
							if(!empty($set->sisipGroup[$induk])){
								$data.= $set->sisipGroup[$induk];
							}
							$data.= "</div>";
							$data.= '<div class="page-break"></div>';
							$nom = 1;
							$pg = $pg + 1;
							
							$data.= $this->crHead($set, $widthDiv, $height, $pg);
						}
					}
					
					$subTot = array();
					$induk = $r->$ima;
					$data.= '<tr><td colspan="1000" class="_tb" style="font-weight: bold">'.$nmGrup.'</td></tr>';
					$nom++;
					$no = 1;
					$indDisNot = array();
				}
			}
			
			$data.= '<tr>';
			foreach($set->display as $i){
				$key = $i['id'];
				$nil = $r->$key;
				$iNew = $r;
				
				if(!empty($i['set'])){
					$funct = 'set'.$i['set'];
					$nil = $rumus->$funct($r, $filter, $noRumus);
				}
				$val = substr($nil, 0, (int)$i['size']);
				
				$align = 'left';
				if(strtoupper($i['type']) == strtoupper('no')){
					$val = $no++;
				}
				if(strtoupper($i['type']) == strtoupper('numeric')){
					if($i['null'] == true){
						if($val <> 0){
							$val = $this->getNumeric($val, (int)$i['dec'], $i['nokomma']);
						}else{
							$val = '&nbsp;';
						}
					}else{
						$val = $this->getNumeric($val, (int)$i['dec'], $i['nokomma']);
					}
					$align = 'right';
				}
				// if(strtoupper($i['type']) == strtoupper('date')){
					// $align = 'center';
				// }
				
				if(!empty($i['align'])){
					$align = $i['align'];
				}
				
				if(!empty($vHit[$key])){
					$subTot[$key] = $subTot[$key] + $nil;
					$tot[$key] = $tot[$key] + $nil;
				}
				
				if($i['noduplicate'] == true){
					if($indDisNot[$key] != $val){
						$indDisNot[$key] = $val;
					}else{
						$val = '&nbsp;';
					}
					
				}
				if($i['input'] == true){
					$val = $nil;
				}
				
				$linkAw = '';
				$linkAk = '';
				if(!empty($i['link'])){
				
					$strLink = '';
					if(!empty($i['link']['attr'])){
						foreach($i['link']['attr'] as $rL){
							$strLink.= "&$rL=".$r->$rL;
						}
					}
					if(!empty($i['link']['filter'])){
						foreach($i['link']['filter'] as $rL){
							$strLink.= "&$rL=".$filter->$row;
						}
					}
				
					$target = 'target="_blank"';
					if($i['link']['tetap'] == true){
						$target = '';
					}
				
					$linkAw = '<a class="'.$i['link']['class'].'" href="'.$i['link']['url'].$strLink.'" '.$target.' style="text-decoration: none">';
					$linkAk = '</a>';
				}
						
				$data.= '<td align="'.$align.'" class="_td">'.$linkAw.$val.$linkAk.'</td>';
			}
			$data.= '</tr>';
			
			
			$nom++;
			if($nom > $row){
				$data.= "</table>";
				$data.= "</div>";
				$data.= '<div class="page-break"></div>';
				$nom = 1;
				$pg = $pg + 1;
			}
			$urut++;
		}
		if(count($set->hitung) > 0){
			$data.= '<tr>';
			foreach($hit as $kHit => $rHit){
				if($rHit->cl > 0){
					$nm = $rHit->nm;
					if(!empty($nm)){
						$nm = $subTot[$nm];
						if(!empty($mHit[$rHit->nm]->funct)){
							$setModeHitung = $mHit[$rHit->nm]->funct;
							$nm = $this->$setModeHitung($set, $iNew, $mHit[$rHit->nm]);
						}
						$nm = $this->getNumeric($nm, $vDec[$rHit->nm], $nokomma[$rHit->nm]);
					}
					if($kHit == 0){
						$nm = 'Total';
					}
					if($set->groupBy != false){
						$data.= '<td align="right" class="_hitung" colspan="'.$rHit->cl.'">'.$nm.'</td>';
					}
				}
			}
			$data.= '</tr>';
		}
		if($set->cutGroup == true){
			$data.= "</table>";
			if(!empty($set->sisipGroup[$induk])){
				$data.= $set->sisipGroup[$induk];
			}
			$data.= "</div>";
			$data.= '<div class="page-break"></div>';
			$nom = 1;
			$pg = $pg + 1;
		}
			
		if(count($set->groupByFo) > 0){
			$data.= $this->setFoter($set, $iNew);
			$nom++;
		}
		if(count($set->hitung) > 0 and $set->grandtotal == true){
			$data.= '<tr>';
			foreach($hit as $kHit => $rHit){
				if($rHit->cl > 0){
					$nm = $rHit->nm;
					if(!empty($nm)){
						$nm = $tot[$nm];
						if(!empty($mHit[$rHit->nm]->funct)){
							$setModeHitung = $mHit[$rHit->nm]->funct;
							$nm = $this->$setModeHitung($set, $iNew, $mHit[$rHit->nm]);
						}
						$nm = $this->getNumeric($nm, $vDec[$rHit->nm], $nokomma[$rHit->nm]);
					}
					if($kHit == 0){
						$nm = 'Grand Total';
					}
					$data.= '<td align="right" class="_hitung" colspan="'.$rHit->cl.'" style="padding-top: 10px">'.$nm.'</td>';
				}
			}
			$data.= '</tr>';
		}
		$data.= "</table>";
		
		if(!empty($set->footer) and $set->footer != false and count($set->footer) > 0){
			foreach($set->footer as $row){
				$data.= '<div class="page-break">';
				$data.= '<div class="_bodyLap">';
				$data.= '<div class="_header">'.$row->header.'</div>';
				
				$data.= $row->body;
				$data.= $row->footer;
				$data.= '</div>';
			}
		}
		
		$data.= "</div>";
		
		if(!empty($set->after)){
			$data.= $set->after;
		}
		
		$res->display  = $data;
		$res->fontSize = $set->fontSize;
		return $res;
	}
	
	public function setFoter2($set, $i)
	{
		$jum = count($set->display);
		foreach($set->groupByFo as $key => $row){
			$dat = $row['data'];
			$var = $row['id'];
			$idK = $i->$var;
			$val = $set->$dat;
			$val = $val[$idK]->$row['attr'];
			
			if($row['type'] == 'numeric'){
				$val = $this->getNumeric($val, (int)$row['dec'], ',');
			}
			if($row['type'] == 'integer'){
				$val = $this->getNumeric($val, (int)$row['dec'], '');
			}
			
			
			if($row['attr'] == 'spasi'){
				$res.= '<br><label style="padding-left: 20px">&nbsp;</label>';
			}else{
				$res.= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &raquo; '.$key.' : '.$val;
			}
		}
		return $res;
	}
	
	public function listReport($set)
	{
		$this->em = $set->em;
		if($set->query == false){
			$data = $set->dataLoad;
		}else{
			$eks = $this->em->queryEks($set->query);
			$data = $this->em->getFetchObject($eks);
		}
	
		$res->data  	= $data;
		$res->fontSize 	= $set->fontSize;
		$res->set 		= $set;
		$res->filter 	= $this->disFilter($set);
		$res->hit 		= $this->setHit($set);
		return $res;
	}
	
	public function getDef($set)
	{
		if($_GET['convert'] == 'cetak'){
			return $this->getDef2($set);
		}else{
			return $this->listReport($set);
		}
	}
}