<?php
$nama = str_ireplace(" ", "_", $this->data->set->modul);
$filename = $nama.".xls";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);

$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;
}
?>
<style>
._headerPt{
	font-size: 12px;
	text-align: center;
}
._headerFilter{
	font-size: 10px;
	text-align: center;
}
._footer{
	font-size: 10px;
}
._spasi{
	height: 10px;
}
._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;
}
._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>
    <header style="height: 100px">
		<div class="_headerPt">
			<?php echo $login['pt'] ?>
			<br>
			<?php echo $this->data->set->modul ?>
		</div>
		<div class="_headerFilter">
			<?php echo $this->data->filter ?>
		</div>
    <header>
    <table class="_tableData" cellspacing="0" cellpadding="0">
        <thead>
				<?php
				if(count($hed1) > 0){
				?>
				<tr>
					<?php 
					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";
					?>	
						<th class="<?php echo $clsHd ?>" style="<?php echo $style ?>" <?php echo $colspan ?>>
							<?php echo strtoupper($row['nama']) ?>
						</th>
					<?php
					}
					?>
				</tr>
				<?php
				}
				?>
				<?php
				if($hedJ > 1){
				?>
				<tr>
					<?php 
					foreach($hed2 as $row){
						$align = 'left';
						if($row['type'] == 'numeric'){
							$align = 'right';
						}
					
						$style = "width: ".$row['width']."px; align: $align";
					?>	
						<th class="_tableReportHead" style="<?php echo $style ?>">
							<?php echo $row['nama'] ?>
						</th>
					<?php
					} 
					?>
				</tr>
				<?php
				}
				?>
		</thead>
        <tbody>
				<?php
				$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){
									?>
										<tr>
											<?php 
											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;";
													}
									?>
										<td colspan="<?php echo $rHit->cl ?>" class="_tableReportTd">
											<?php echo $nm ?>">
										</td>
									<?php 
										}
									} 
									?>
								</tr>
							<?php
							}
							
						}
						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;
								}
							}
						?>
						<tr>
							<td class="_tableReportTdGrup" colspan="<?php echo count($this->data->set->display) ?>" >
								<?php echo $nmGrup ?>
							</td>
						</tr>
				<?php
					}
					$grup = $i->$grbB;
					if(count($this->data->set->groupByFo) > 0 and $no > 1){
						$grupFotDis = $repo->setFoter2($this->data->set, $iNew);
				?>
					<tr>
						<td class="_tableReportTdGrup" colspan="<?php echo count($this->data->set->display) ?>">
							<?php echo $grupFotDis ?>
						</td>
					</tr>
				<?php
								}
							}
						}
					}
					if($no < 10000000000000000){
				?>
					<tr>
					<?php
					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 = '';
							}
						}
						
					?>
						<td class="_tableReportTd">
							<?php echo $val ?>
						</td>
					<?php 
					}
					?>
					</tr>
				<?php
					$iNew = $i;
					}
				}
				?>
        </tbody>
    </table>
    <br>
<?php
if(!empty($this->data->set->footer) and $this->data->set->footer != false and count($this->data->set->footer) > 0){
	foreach($this->data->set->footer as $row){
?>
	<page backtop="10mm" backbottom="10mm" backleft="20mm" backright="20mm">
		<page_header style="height: 100px">
			<div class="_headerFilter">
				<?php echo $row->header ?>
			</div>
		</page_header>
		<?php echo $row->body ?>
	</page>
<?php
	}
}
?>
