<?php

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

if ($result->num_rows>0) { 
	// Section header	
	$objPHPExcel->getActiveSheet()->getStyle('A'. $excelRow .':F'. $excelRow .'')->applyFromArray( $style_header );
	$objPHPExcel->getActiveSheet()->setCellValue('A'. $excelRow .'', 'Client Hourly Support Work');
	$objPHPExcel->getActiveSheet()->setCellValue('B'. $excelRow .'', 'Hours');
	$objPHPExcel->getActiveSheet()->setCellValue('C'. $excelRow .'', 'Hourly Rate');
	$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);

	$hourTotal = 0; 
	$invTotal = 0; 
	$comissionTotal = 0; 
	
	while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
		
		// Get Correct Project Rate
		$pRate = $row['pRate']; // New Style Project Rate
		$projComish = $comishPercent; // Old Style Commission Percentage
		if (empty($pRate))
			$pRate = $row['pRateOLD'];
		else {
			$currentRates = checkRateCurrent($row['rateID'],$dateFrom,$dateTo,$con);
			$pRate = $currentRates[0];
			$projComish = $currentRates[1];
		}
		
		$excelRow = $excelRow + 1; // Move to Next Excel Row
		$pTotal = $row['phours']*$pRate;
		$comission = number_format($pTotal*$projComish, 2,'.','');
		$objPHPExcel->getActiveSheet()->setCellValue('A'. $excelRow .'', $row['cName'] . ' - ' . $row['pName']);
		$objPHPExcel->getActiveSheet()->setCellValue('B'. $excelRow .'', $row['phours']);
		$objPHPExcel->getActiveSheet()->setCellValue('C'. $excelRow .'', $pRate);
		$objPHPExcel->getActiveSheet()->getCell('D'. $excelRow .'')->setValueExplicit(number_format($pTotal,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);
		$hourTotal = $hourTotal+$row['phours'];				
		$invTotal = $invTotal+$pTotal;
		$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 .'', $hourTotal);
	$objPHPExcel->getActiveSheet()->setCellValue('C'. $excelRow .'', $pRate);
	$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
}

?>