<?php

include('../inc/queries/sumQuery_Expenses.php');

if ($result->num_rows>0) { 
	// Section header	
	$objPHPExcel->getActiveSheet()->getStyle('A'. $excelRow .':F'. $excelRow .'')->applyFromArray( $style_header );
	$objPHPExcel->getActiveSheet()->mergeCells('A'. $excelRow .':C'. $excelRow .'');
	$objPHPExcel->getActiveSheet()->setCellValue('A'. $excelRow .'', 'Expenses');
	$objPHPExcel->getActiveSheet()->setCellValue('E'. $excelRow .'', 'Date');
	$objPHPExcel->getActiveSheet()->setCellValue('F'. $excelRow .'', 'Amouont');
	$objPHPExcel->getActiveSheet()->getStyle('B'. $excelRow .':F'. $excelRow .'')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

	$invTotal = 0; 
	$expTotal = 0; 
	while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
		$excelRow = $excelRow + 1; // Move to Next Excel Row
		// Format Expense Description
		$expenseDesc = $row['expName'];
		if ( (!empty($row['clientName'])) || (!empty($row['projectName'])) ) { 
			$expenseDesc .= ' (';
			if (!empty($row['clientName']))
				$expenseDesc .= $row['clientName'];
			if ( (!empty($row['clientName'])) && (!empty($row['projectName'])) )
				$expenseDesc .= ' - ';
			if (!empty($row['projectName']))
				$expenseDesc .= $row['projectName'];
			$expenseDesc .= ')';
		}		
		$objPHPExcel->getActiveSheet()->mergeCells('A'. $excelRow .':D'. $excelRow .'');
		$objPHPExcel->getActiveSheet()->setCellValue('A'. $excelRow .'', $expenseDesc);
		$objPHPExcel->getActiveSheet()->setCellValue('E'. $excelRow .'', Date::convert($row['expDate'], 'Y-m-d', 'm/d/Y'));
		$objPHPExcel->getActiveSheet()->getCell('F'. $excelRow .'')->setValueExplicit(number_format($row['expAmount'],2,'.',''), PHPExcel_Cell_DataType::TYPE_NUMERIC);
		$objPHPExcel->getActiveSheet()->getStyle('F'. $excelRow .'')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
		$expTotal = $expTotal+$row['expAmount'];
	}
	$excelRow = $excelRow + 1; // Move to Next Excel Row
	$objPHPExcel->getActiveSheet()->getStyle('A'. $excelRow .'')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
	$objPHPExcel->getActiveSheet()->setCellValue('A'. $excelRow .'', 'Totals: ');
	$objPHPExcel->getActiveSheet()->setCellValue('B'. $excelRow .'', '');
	$objPHPExcel->getActiveSheet()->setCellValue('C'. $excelRow .'', '');
	$objPHPExcel->getActiveSheet()->setCellValue('D'. $excelRow .'', '');
	$objPHPExcel->getActiveSheet()->setCellValue('E'. $excelRow .'', '');
	$objPHPExcel->getActiveSheet()->getCell('F'. $excelRow .'')->setValueExplicit(number_format($expTotal, 2,'.',''), PHPExcel_Cell_DataType::TYPE_NUMERIC);
	$objPHPExcel->getActiveSheet()->getStyle('F'. $excelRow .'')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
	$objPHPExcel->getActiveSheet()->getStyle('A'. $excelRow .':F'. $excelRow .'')->applyFromArray( $style_totals );
	
	$repTotalComish = $repTotalComish+$expTotal;
	$excelRow = $excelRow + 3; // Move to Next Excel 
}


?>