<?php

$invTotal = 0; 
$comissionTotal = 0; 

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

if ($result->num_rows>0) { 
	// Section header	
	$objPHPExcel->getActiveSheet()->getStyle('A'. $excelRow .':F'. $excelRow .'')->applyFromArray( $style_header );
	$objPHPExcel->getActiveSheet()->setCellValue('A'. $excelRow .'', 'Client Project Work');
	$objPHPExcel->getActiveSheet()->setCellValue('B'. $excelRow .'', '');
	$objPHPExcel->getActiveSheet()->setCellValue('C'. $excelRow .'', '');
	$objPHPExcel->getActiveSheet()->setCellValue('D'. $excelRow .'', $amtInv);
	$objPHPExcel->getActiveSheet()->setCellValue('E'. $excelRow .'', $comishRate);
	$objPHPExcel->getActiveSheet()->setCellValue('F'. $excelRow .'', $comDue);
	$objPHPExcel->getActiveSheet()->getStyle('B'. $excelRow .':F'. $excelRow .'')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

	while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
		// Get Correct Project Rate
		$projComish = $comishPercent; // New Style Commission Percentage
		if (empty($row['compPercent']))
			$projComish = $comishPercent;
		else {
			$currentRates = checkRateCurrent($row['rateID'],$dateFrom,$dateTo,$con);
			$projComish = $currentRates[1];
		}
		$excelRow = $excelRow + 1; // Move to Next Excel Row
		$comission = number_format( $row['invTotal']*$projComish, 2,'.','');
		$objPHPExcel->getActiveSheet()->setCellValue('A'. $excelRow .'', $row['cName'] . ' - ' . $row['pName']);
		$objPHPExcel->getActiveSheet()->setCellValue('B'. $excelRow .'', '');
		$objPHPExcel->getActiveSheet()->setCellValue('C'. $excelRow .'', '');
		$objPHPExcel->getActiveSheet()->getCell('D'. $excelRow .'')->setValueExplicit(number_format($row['invTotal'],2,'.',''), PHPExcel_Cell_DataType::TYPE_NUMERIC);
		$objPHPExcel->getActiveSheet()->getStyle('D'. $excelRow .'')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
		$objPHPExcel->getActiveSheet()->getCell('E'. $excelRow .'')->setValueExplicit(number_format($projComish,2,'.',''), PHPExcel_Cell_DataType::TYPE_NUMERIC);
		$objPHPExcel->getActiveSheet()->getStyle('E'. $excelRow .'')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE);
		$objPHPExcel->getActiveSheet()->getCell('F'. $excelRow .'')->setValueExplicit(number_format($comission,2,'.',''), PHPExcel_Cell_DataType::TYPE_NUMERIC);
		$objPHPExcel->getActiveSheet()->getStyle('F'. $excelRow .'')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
		$invTotal = $invTotal+$row['invTotal'];
		$comissionTotal = $comissionTotal+$comission;
	}
	$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()->getCell('D'. $excelRow .'')->setValueExplicit(number_format($invTotal, 2,'.',''), PHPExcel_Cell_DataType::TYPE_NUMERIC);
	$objPHPExcel->getActiveSheet()->getStyle('D'. $excelRow .'')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
	//$objPHPExcel->getActiveSheet()->getCell('E'. $excelRow .'')->setValueExplicit(number_format($comishPercent,2,'.',''), PHPExcel_Cell_DataType::TYPE_NUMERIC);
	$objPHPExcel->getActiveSheet()->getStyle('E'. $excelRow .'')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE);
	$objPHPExcel->getActiveSheet()->getCell('F'. $excelRow .'')->setValueExplicit(number_format($comissionTotal, 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 );
	//$objPHPExcel->getActiveSheet()->getStyle('F'. $excelRow .'')->getBold()->setFormatCode(PHPExcel_Style_Font::Bold);
	
	$repTotalInv = (is_numeric($invTotal) ? $repTotalInv+$invTotal : $repTotalInv);
	$repTotalComish = (is_numeric($comissionTotal) ? $repTotalComish+$comissionTotal : $repTotalComish);
	$excelRow = $excelRow + 3; // Move to Next Excel Row
}

?>