<?php
ob_start();
include('includes/session.inc');
$Title = _('Print Roster- GFW');
include('includes/SQL_CommonFunctions.inc');

include('includes/header.inc');
echo '<p> </p> <p></p><p class="page_title_text"><img alt="" src="'.$RootPath.'/css/'.$Theme.
		'/images/printer.png" title="' .// Icon image.
		_('Guardforce West Roster') . '" /> ' .// Icon title.
		_('Choose below the roster to print ') . '</p>';// Page title.
echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
	echo '<div class="col-sm-10">';// div class=?
	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<table class="table">';

echo '<td>' . _('Select Roster to Print ') . ':</td>
							<td><select name="RosterPeriod">';
							
							$SQL = "SELECT rswestid,
											start_date,
				                            end_date,
											client,
											patrol_location,
											account_number,
											department,
											status
		                            FROM rosteringwest
									WHERE status = 'approved'
		                             ";

$result=DB_query($SQL);
//echo '<option value="0">0 - ' . _('None Available') . '</option>';
 echo '<option value="0">0 - ' . _('Print All Approved Rosters Only') . '</option>';
while ($myrow=DB_fetch_array($result)){
	$SQLpatrol = "SELECT 
											patrollocation,
											debtorno,
											account_number
											
		                            FROM guardforceclient
		                            WHERE clientid ='" . $myrow['patrol_location']."' ";
	$resultP=DB_query($SQLpatrol);
	
	while ($myrowP=DB_fetch_array($resultP)){
		
		$SQLdebtors = "SELECT 
											
											name
											
		                            FROM debtorsmaster
		                            WHERE debtorno ='" . $myrowP['debtorno']."' ";
	$resultR=DB_query($SQLdebtors);
	
	while ($myrowR=DB_fetch_array($resultR)) {
		
		
		
		
	if (isset($_POST['RosterPeriod']) AND $_POST['RosterPeriod']==$myrow['rswestid']) {
	    
		echo '<option selected="selected" value="' . $myrow['rswestid'] . '">' . $myrow['rswestid']. ' - ' .$myrow['start_date'] . ' - ' .$myrow['end_date'] .' -' .$myrowR['name'] .' -' .$myrowP['patrollocation'] .' -' .$myrow['department'] . '-' .$myrow['status'] .'</option>';
	} else {
			echo '<option value="' . $myrow['rswestid'] . '">' . $myrow['rswestid']. ' - ' .$myrow['start_date'] . ' - ' .$myrow['end_date'] .' -' .$myrowR['namet'] .' -' .$myrowP['patrollocation'] .' -' .$myrow['department'] . '-' .$myrow['status'] .'</option>';
	}
	
	}
	}
}
echo '</select>';


echo '<input type="submit" name="PrintRoster" value="' ._('Print Roster') . '" />
		</div>
		';



echo '</div>';

echo '</div>';
$rosterid = $_POST['RosterPeriod'];

$printdate = date("d-m-Y");


	if (isset($_POST['PrintRoster']) && ($_POST['RosterPeriod'] == 0)){


	echo ' <b><font style="color: red">all rosters will be printed. </font></b>';	
		
	}

elseif (isset($_POST['PrintRoster']) && ($_POST['RosterPeriod'] == $rosterid)){
		

	
	
	
	
if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');

/** Include PHPExcel */
//include_once('Classes/PHPExcel.php');


include_once('Classes/PHPExcel/IOFactory.php');

$inputFileName = 'printing/rostergfw.xlsx';
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);


/*check point*/

// Read the existing excel file
//$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(false);
$objPHPExcel = $objReader->load($inputFileName);

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
//$objPHPExcel->setActiveSheetIndex();

// Add months and till date information derived from above
//$objPHPExcel->getActiveSheet()->duplicateStyle($objPHPExcel-‌​>getActiveSheet()->g‌​etStyle('B2'), 'B3:B20');

	//header
	
		$SQL = "SELECT rswestid,
											start_date,
				                            end_date,
											client,
											patrol_location,
											account_number,
											department,
											status
		                            FROM rosteringwest
									WHERE rswestid = '" . $rosterid."'
		                             ";

$result=DB_query($SQL);
	$myrow2=DB_fetch_array($result);
	
	
        $objPHPExcel->getActiveSheet()->setCellValue('C2', $myrow2['start_date']);
		$objPHPExcel->getActiveSheet()->setCellValue('B2', $NumberOfMonths);
		$objPHPExcel->getActiveSheet()->setCellValue('D2', $PeriodToDate);
		

	
	
	//employee schedules

  $SQL = "SELECT rosteringtimesheetwest.employeeid,
  				
                  employeemaster.first_name,
				  employeemaster.last_name
				  
				FROM rosteringtimesheetwest INNER JOIN employeemaster
				ON rosteringtimesheetwest.employeeid= employeemaster.employeeid 
				WHERE rosteringtimesheetwest.rswestid='" . $rosterid."'
				";

  
  			

	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;	
while ($rowCount< $num) {
	$cellcount = 10;	
	
	while ($myrow3=DB_fetch_array($AccountsResult)) {
		
		
	
		 $employee = $myrow3['employeeid'].' '.$myrow3['first_name'].' '.$myrow3['first_name'];
	
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$cellcount, $employee);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$cellcount, $myrow3['montime_in']);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$cellcount,  $myrow3['montime_out']);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$cellcount, $myrow3['montime_in1']);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$cellcount, $myrow3['montime_out1']);
		$cellcount++;
	
		
	}
		
		$rowCount++;
	

}
   
// Generate an updated excel file
// Redirect output to a client’s web browser (Excel2007)



//save the file to the server (Excel2007)

//include('includes/header.inc');
	


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//$objWriter->save($inputFileName);
$objWriter->save($inputFileName);


    
    //<?php
	
echo '<br /><p></p><p></p> <p></p> <p></p> <p></p>  <p class="page_title_text"><a href="' . $inputFileName . '">' . _('File generated o.k. Click to view this  excel File ') . '</p>';




//echo $AccountCode;



$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
		
				
}

			
		
			
		
			
		
		
		
			    
		
	
//echo 'Quotation Created Successfully'. $jobno.'!';




//echo 'Quotation Created Successfully'. $jobno.'!';
echo '</div>';
echo '</div>';

//include('includes/footer.inc'); 

?>
