<?php
$pathClass = BASEPATH.'assets/pdf/phpToPDF/phpToPDF.php';
require_once($pathClass);

$login = SESSION;
$login = $_SESSION[$login]['login'];

$width = 0;
$vWdth = array();
$hed1 = array();
$hed2 = array();
$hedJ = 1;

foreach($this->data->set->display as $k => $row){
	$this->data->set->display[$k]['width'] = round($row['width'] / 2);
}
foreach($this->data->set->display as $row){
	$width = $width + ($row['width'] + 3);
	
	$id = $row['id'];
	$vWdth[$id]->width 	= $row['width'];
	$vWdth[$id]->dec 	= $row['dec'];
	
	$row2 = $row;
	$idH = $row['id'];
	if(!empty($row['induk'])){
		$idH = $row['induk'];
		$hedJ = 2;
	}else{
		$row2['nama'] = '';
	}
	if(empty($hed1[$idH])){
		$hed1[$idH] = $row;
	}
	$hed1[$idH]['widthH'] = $hed1[$idH]['widthH'] + ($row['width']);
	$hed1[$idH]['child'][] = $row;
	
	$hed2[] = $row2;
}

$rumus = new RumusReport;

$atrHit = array();
foreach($this->data->set->hitung as $r){
	$atrHit[$r] = $r;
}


$my_html = "
<head>
	<title>".$this->data->set->modul."</title>
</head>
<style>
._tableData{
	width: 100%;
	border-top: 1px solid #616363;
	border-left: 1px solid #616363;
	border-bottom: 1px solid #616363;
}
._tableReportHead{
	border-bottom: 1px solid #616363;
	border-right: 1px solid #616363;
	font-family: arial;
	font-size: 9px;
	background-color: #DEE2E3;
	padding: 2px;
}
._tableReportHead2{
	border-right: 1px solid #616363;
	font-family: arial;
	font-size: 9px;
	background-color: #DEE2E3;
	padding: 2px;
}
._tableReportTd{
	border-right: 1px solid #616363;
	font-family: arial;
	font-size: 9px;
	padding: 2px;
	vertical-align: top;
}
._tableReportTdGrup{
	border-top: 1px solid #616363;
	border-bottom: 1px solid #616363;
	border-right: 1px solid #616363;
	font-family: arial;
	font-size: 9px;
	padding: 2px;
}
._repTable{
	width: 100%;
	border-top: 1px solid #616363;
	border-left: 1px solid #616363;
	border-bottom: 1px solid #616363;
}
._th{
	border-bottom: 1px solid #616363;
	border-right: 1px solid #616363;
	font-family: arial;
	font-size: 9px;
	background-color: #DEE2E3;
	padding: 2px;
}
._td{
	border-right: 1px solid #616363;
	font-family: arial;
	font-size: 9px;
	padding: 2px;
}
</style>
";

$my_html.= '<table class="_tableData" cellspacing="0" cellpadding="0">';
$my_html.= "<thead>";
if(count($hed1) > 0){
	$my_html.= "<tr>";
	foreach($hed1 as $inNm => $row){
		$clsHd = '_tableReportHead';
		$align = 'left';
		if($row['type'] == 'numeric'){
			$align = 'right';
		}
		$plusHed = 0;
		$rowspan = 1;
		$colspan = '';
		if(count($row['child']) > 1){
			$plusHed 	= (count($row['child']) * 4) - 2;
			$align 		= 'center';
			$colspan	= 'colspan="'.count($row['child']).'"';
		}else{
			$rowspan 	= 2;
			if($hedJ > 1){
				$clsHd = '_tableReportHead2';
			}
		}
		$row['widthH'] = $plusHed + $row['widthH'];
		
		$style = "width: ".$row['widthH']."px; align: $align";
		$my_html.= "<th class='$clsHd' style='$style' $colspan>".strtoupper($inNm)."</th>";
	}
	$my_html.= "</tr>";
}
if($hedJ > 1){
	$my_html.= "<tr>";
	foreach($hed2 as $row){
		$align = 'left';
		if($row['type'] == 'numeric'){
			$align = 'right';
		}
	
		$style = "width: ".$row['width']."px; align: $align";
		$my_html.= "<th class='_tableReportHead' style='$style'>".$row['nama']."</th>";
	}
}	
$my_html.= "</tr>";
$my_html.= "</thead>";
$my_html.= "<tbody>";
				$no = 0;
				$grup = '';
				$vHit = array();
				$grbB = $this->data->set->groupBy;
				$repo = new Report;
				foreach($this->data->data as $n => $i){
					$no = $no + 1;
					if(count($this->data->set->hitung) > 0 or !empty($grbB)){
						if(!empty($grbB)){
							if($grup != $i->$grbB){
								if($n > 0){
									if(count($this->data->set->hitung) > 0){
										$my_html.= "<tr>";
											foreach($this->data->hit as $kHit => $rHit){ 
												if($rHit->cl > 0){
													$nm = $rHit->nm;
													$_idG = $rHit->nm;
													$pd = '';
													if(!empty($nm)){
														$nm = $vHit[$grup]->$nm;
														$nm = number_format($nm, (int)$vWdth[$_idG]->dec);
													}
													if($kHit == 0){
														$nm = 'Total';
														$pd = "; padding: 2px;";
													}
										$my_html.= "<td colspan='".$rHit->cl."' class='_tableReportTd'>$nm</td>";
										}
									} 
								$my_html.= "</tr>";
							}
							
						}
						if(count($this->data->set->groupDis) > 0){
							$nmGrup = '';
							foreach($this->data->set->groupDis as $kGrup => $nGrup){
								if(!empty($nmGrup)){
									$nmGrup.= ', ';
								}
								if(!empty($kGrup)){
									$nmGrup.= $kGrup.' : '.$i->$nGrup;
								}else{
									$nmGrup.= $i->$nGrup;
								}
							}
						$my_html.= "<tr>
										<td class='_tableReportTdGrup' colspan='".count($this->data->set->display)."'>
											$nmGrup
										</td>
									</tr>";
					}
					$grup = $i->$grbB;
					if(count($this->data->set->groupByFo) > 0 and $no > 1){
						$grupFotDis = $repo->setFoter2($this->data->set, $iNew);
							$my_html.= "<tr>
											<td class='_tableReportTdGrup' colspan='".count($this->data->set->display)."'>
												$grupFotDis
											</td>
										</tr>";
								}
							}
						}
					}
					if($no < 10000000000000000){
					$my_html.= "<tr>";
					foreach($this->data->set->display as $row){
						$align = '';
						$id = $row['id']; 
						
						$val = $i->$id;
						if(!empty($row['set'])){
							$funct = 'set'.$row['set'];
							$val = $rumus->$funct($i);
						}
						
						if(!empty($atrHit[$id])){
							$idGrpH = '';
							if(!empty($grbB)){
								$idGrpH = $i->$grbB;
							}
							$vHit[$idGrpH]->$id = $vHit[$idGrpH]->$id + $val;
						}
						
						if($row['type'] == 'numeric'){
							$align = 'right';
							$val = number_format($val, (int)$row['dec']);
						}
						
						if($row['_number'] == true){
							$val = $no;
						}
								
						if($row['noduplicate'] == true){
							if($sama[$id] != $val){
								$sama[$id] = $val;
								
							}else{
								$val = '';
							}
						}
						
						$my_html.= "<td class='_tableReportTd'>$val</td>";
					}
					$my_html.= "</tr>";
					$iNew = $i;
					}
				}
$my_html.= "</tbody>";
$my_html.= "</table>";



$my_html_header="
<style>
._headerPt{
	font-size: 12px;
	text-align: center;
	font-weight: bold;
}
._headerFilter{
	font-size: 10px;
	text-align: center;
	font-weight: bold;
}
</style>
<div class='_headerrPdf'>
  <div style=\"float:left; width:33%; text-align:left; font-size: 10px; font-family: arial\">
          &nbsp;
  </div>
  <div style=\"float:left; width:33%; text-align:center;; font-size: 10px; font-family: arial\">
          &nbsp;
  </div>
  <div style=\"float:left; width:33%; text-align:right; font-size: 10px; font-family: arial;\">
         Page phptopdf_on_page_number of phptopdf_pages_total
   </div>
   <br style=\"clear:left;\"/>
   <div class='_headerPt'>".$login['pt']."</div>
   <div class='_headerFilter'>".$this->data->filter."</div>
</div>";



$my_html_footer="
<div class='_footerPdf'>
  <div style=\"float:left; width:33%; text-align:left; font-size: 10px; font-family: arial\">
          ".date("d M Y H:i")."
  </div>
  <div style=\"float:left; width:33%; text-align:center;; font-size: 10px; font-family: arial\">
          &nbsp;
  </div>
  <div style=\"float:left; width:33%; text-align:right; font-size: 10px; font-family: arial;\">
         Page phptopdf_on_page_number of phptopdf_pages_total
   </div>
   <br style=\"clear:left;\"/>
</div>";




$pdf_options = array(
  "source_type" => 'html',
  "source" => $my_html,
  "action" => 'view',
  "page_size" => 'A4',
  "page_orientation" => 'portrait',
  "save_directory" => '',
  "file_name" => 'sample_pdf_report.pdf',
  "header" => $my_html_header,
  "footer" => $my_html_footer);


phptopdf($pdf_options);
