<?php

$rumus = new RumusReport;

function setData($data, $val, $modul = '')
{
	if($modul == 'Laporan Neraca Percobaan'){
		$bag = explode("</span>", $val);
		if(!empty($bag[1])){
			$val = $bag[1];
		}else{
			$val = $bag[0];
		}
	}
	
	if(!empty($data)){
		$data.= ",";
	}
	$data.= '"'.str_ireplace('"', "'", str_ireplace('&nbsp;', " ", $val)).'"';
	return $data;
}

$data = "";
foreach($this->data->set->display as $r){
	$data = setData($data, $r['nama']);
}
$data.= "\n";

$date = new Date;
foreach($this->data->data as $n => $i){
	$res = "";
	foreach($this->data->set->display as $r){
		$id = $r['id'];
		$val = $i->$id;
		if(!empty($r['set'])){
			$funct = 'set'.$r['set'];
			$val = $rumus->$funct($i);
		}
		
		if($r['type'] == 'date'){
			$val = $date->serialDate($val);
		}
		if($r['type'] == 'numeric'){
			$int = (int)$r['dec'];
			$val = number_format($val, $int, '.', '');
		}
		$res = setData($res, $val, $this->data->set->modul);
	}
	$res.= "\n";
	$data.= $res;
}

$nama = str_ireplace(" ", "_", $this->data->set->modul);
$filename = $nama;
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
echo $data; 
exit();