<?php
ob_start();
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
include('includes/session.inc');
$Title = _('Print Timesheet for GFS');
include('includes/SQL_CommonFunctions.inc');
 
include('includes/header.inc');
if (isset($_GET['rosterid'])) {
		$_SESSION['rosterid'] = stripslashes($_GET['rosterid']);
	}
	$rosterid = $_SESSION['rosterid'];


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



		
	

				$SQL = "SELECT timesheetsuva.timesuvaid,
											
											timesheetsuva.id,
											guardforceclient.debtorno,
											guardforceclient.patrollocation,
											guardforceclient.sectors,
											guardforceclient.account_number,
											guardforceclient.department,
										    
											timesheetsuva.status,
											calendar_management.start_date,
											calendar_management.end_date
											
		                            FROM timesheetsuva 
									inner join guardforceclient on guardforceclient.clientid = timesheetsuva.clientid
									inner join calendar_management on calendar_management.id = timesheetsuva.id
		                            WHERE timesuvaid= '" . $rosterid."' 
									
									";

$result=DB_query($SQL);
	
	$myrow50=DB_fetch_array($result);

	
	$startdate= date("d-m-Y", strtotime($myrow50['start_date']));
	$enddate= date("d-m-Y", strtotime($myrow50['end_date']));
	$date = date('d-m-Y');


$SQLdepart = "SELECT departmentname
											
		                            FROM department
		                            WHERE departmentid ='" . $myrow50['department']."' ";
	$resultdepart=DB_query($SQLdepart);
	
$myrowdepart=DB_fetch_array($resultdepart);




	$SQLsectors = "SELECT name,supervisor_details_for_rosterprintheaders
											
		                            FROM sectors
		                            WHERE id ='" . $myrow50['sectors']."' ";
	$resultQ=DB_query($SQLsectors);
	
	$myrow200=DB_fetch_array($resultQ);

	$SQLdebtors = "SELECT 
											
											name
											
		                            FROM debtorsmaster
		                            WHERE debtorno ='" . $myrow50['debtorno']."' ";
	$resultR=DB_query($SQLdebtors);
	
	
	$myrow300=DB_fetch_array($resultR);
	

	
$PaperSize = 'A4_Landscape';// PDFStarter.php: $Page_Width=842; $Page_Height=595; $Top_Margin=30; $Bottom_Margin=30; $Left_Margin=40; $Right_Margin=30;
include('includes/PDFStarter.php');
$pdf->addInfo('Title', _('timesheet') );
$pdf->addInfo('Subject', _('timesheet') );
$FontSize = 12;
	
$line_height = 12;// Recoeommended: $line_height = $x * $FontSize.
	$headinggap = 6;	
		include('includes/PDFtimesheetheader.inc');

  $topheading = $YPos + 35;
 $SQL = "SELECT timesheetsuvadetails.*,
  				
                  employeemaster.first_name,
				  employeemaster.last_name
				  
				  
				FROM timesheetsuvadetails INNER JOIN employeemaster
				ON timesheetsuvadetails.employeeid = employeemaster.employeeid 
				WHERE timesheetsuvadetails.timesuvaid='" . $rosterid."'
				";

  
  			

	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;
	$cellcount = 0;	
	$cellcount2 = 6;
$montotalhours = 0;	
		$tuestotalhours = 0; 	$wedtotalhours = 0;		$thurstotalhours = 0;	$fritotalhours = 0; 	$sattotalhours = 0;
	 	$suntotalhours = 0; 	$mon2totalhours = 0; $totalhours = 0;$totalnormal = 0; $totaltimehalf = 0 ; $totaldouble = 0;
while ($rowCount < $num) {
	


   
	while ($myrow3=DB_fetch_array($AccountsResult)) {
		
		
		
				
				  $ListCount ++;
				

		$YPos -= $line_height;// Increment a line down for the next line item.
				
      
		if ((mb_strlen($myrow1['narrative']) >200 AND $YPos-$line_height <= 75)
			OR (mb_strlen($myrow1['narrative']) >1 AND $YPos-$line_height <= 62)
			OR $YPos-$line_height <= 50){
		/* We reached the end of the page so finsih off the page and start a newy */
			include ('includes/PDFTimesheetheader.inc');
		} //end if need a new page headed up

			
								//$myrow6 = DB_fetch_array($result6) ;
				
				
				//write to pdf
				
				//$total = $myrow7979['quantity'] * $myrow3['materialcost'] ;
			    
			 
			  // $grandtotal += $total;
				
				//$costprice = locale_number_format($myrow3['materialcost'],2);
				
				//$roundtotal= locale_number_format($total,2);

	
		
			$FontSize =8;// Font size for the line item.
 $employee = $myrow3['employeeid'].'-'.$myrow3['first_name'];
		$LeftOvers = $pdf->addTextWrap(20, $YPos+12-$cellcount,200,$FontSize,$employee,'left');
		
		
	$FontSize =7;
		
		if ($myrow3['mon_absent'] !=='None' ){
			
			//$montimein = date("g:i a", strtotime($myrow3['montime_in']));
			//$montimeout = date("g:i a", strtotime($myrow3['montime_out']));
			//$mon = $montimein.'-'.$montimeout;
			$LeftOvers = $pdf->addTextWrap(130, $YPos+12 - $cellcount,200,$FontSize,$myrow3['mon_absent'],'left');
		}
	
		if (round($myrow3['mon_total']) > 0.0){
			
			$montimein = date("g:i a", strtotime($myrow3['montime_in']));
			$montimeout = date("g:i a", strtotime($myrow3['montime_out']));
			$mon = $montimein.'-'.$montimeout;
			$LeftOvers = $pdf->addTextWrap(130, $YPos+12 - $cellcount,200,$FontSize,$mon._(' ').$myrow3['mon_total'],'left');
		}
		
		if ($myrow3['tues_absent'] !=='None' ){
			
			//$montimein = date("g:i a", strtotime($myrow3['montime_in']));
			//$montimeout = date("g:i a", strtotime($myrow3['montime_out']));
			//$mon = $montimein.'-'.$montimeout;
			$LeftOvers = $pdf->addTextWrap(220, $YPos+12 - $cellcount,200,$FontSize,$myrow3['mon_absent'],'left');
		}
		
		if (round($myrow3['tues_total']) > 0.0){
		
		$tuestimein = date("g:i a", strtotime($myrow3['tuestime_in']));
			$tuestimeout = date("g:i a", strtotime($myrow3['tuestime_out']));
			$tues = $tuestimein.'-'.$tuestimeout;
			
			
			$LeftOvers = $pdf->addTextWrap(220, $YPos+12 - $cellcount,200,$FontSize,$tues._(' ').$myrow3['tues_total'],'left');
				
		}
		
if ($myrow3['wed_absent'] !=='None' ){
			
			//$montimein = date("g:i a", strtotime($myrow3['montime_in']));
			//$montimeout = date("g:i a", strtotime($myrow3['montime_out']));
			//$mon = $montimein.'-'.$montimeout;
			$LeftOvers = $pdf->addTextWrap(300, $YPos+12 - $cellcount,200,$FontSize,$myrow3['wed_absent'],'left');
		}
		if (round($myrow3['wed_total']) > 0.0){
			$wedtimein = date("g:i a", strtotime($myrow3['wedtime_in']));
			$wedtimeout = date("g:i a", strtotime($myrow3['wedtime_out']));
			$wed = $wedtimein.'-'.$wedtimeout;
			
			
			$LeftOvers = $pdf->addTextWrap(300, $YPos+12 - $cellcount,200,$FontSize,$wed._(' ').$myrow3['wed_total'],'left');
		
		 
		}
		

if ($myrow3['thurs_absent'] !=='None' ){
			
			//$montimein = date("g:i a", strtotime($myrow3['montime_in']));
			//$montimeout = date("g:i a", strtotime($myrow3['montime_out']));
			//$mon = $montimein.'-'.$montimeout;
			$LeftOvers = $pdf->addTextWrap(380, $YPos+12 - $cellcount,200,$FontSize,$myrow3['thurs_absent'],'left');
		}
		if (round($myrow3['thurs_total']) > 0.0){
				$thurstimein = date("g:i a", strtotime($myrow3['thurstime_in']));
			$thurstimeout = date("g:i a", strtotime($myrow3['thurstime_out']));
			$thurs = $thurstimein.'-'.$thurstimeout;
			
			
			$LeftOvers = $pdf->addTextWrap(380, $YPos+12 - $cellcount,200,$FontSize,$thurs._(' ').$myrow3['thurs_total'],'left');
				
		   
		 
		}
	
      if ($myrow3['fri_absent'] !=='None' ){
			
			//$montimein = date("g:i a", strtotime($myrow3['montime_in']));
			//$montimeout = date("g:i a", strtotime($myrow3['montime_out']));
			//$mon = $montimein.'-'.$montimeout;
			$LeftOvers = $pdf->addTextWrap(460, $YPos+12 - $cellcount,200,$FontSize,$myrow3['fri_absent'],'left');
		}
	
       if (round($myrow3['fri_total']) > 0.0){
			$fritimein = date("g:i a", strtotime($myrow3['fritime_in']));
			$fritimeout = date("g:i a", strtotime($myrow3['fritime_out']));
			$fri = $fritimein.'-'.$fritimeout;
			
			$LeftOvers = $pdf->addTextWrap(460, $YPos+12 - $cellcount,200,$FontSize,$fri._(' ').$myrow3['fri_total'],'left');
				
		
		}
		
		if ($myrow3['sat_absent'] !=='None' ){
			
			//$montimein = date("g:i a", strtotime($myrow3['montime_in']));
			//$montimeout = date("g:i a", strtotime($myrow3['montime_out']));
			//$mon = $montimein.'-'.$montimeout;
			$LeftOvers = $pdf->addTextWrap(540, $YPos+12 - $cellcount,200,$FontSize,$myrow3['sat_absent'],'left');
		}
		
		if (round($myrow3['sat_total']) > 0.0){
			$sattimein = date("g:i a", strtotime($myrow3['sattime_in']));
			$sattimeout = date("g:i a", strtotime($myrow3['sattime_out']));
			$sat = $sattimein.'-'.$sattimeout;
			
			$LeftOvers = $pdf->addTextWrap(540, $YPos+12 - $cellcount,200,$FontSize,$sat._(' ').$myrow3['sat_total'],'left');
					
		 
		}
		
		if ($myrow3['sun_absent'] !=='None' ){
			
			//$montimein = date("g:i a", strtotime($myrow3['montime_in']));
			//$montimeout = date("g:i a", strtotime($myrow3['montime_out']));
			//$mon = $montimein.'-'.$montimeout;
			$LeftOvers = $pdf->addTextWrap(620, $YPos+12 - $cellcount,200,$FontSize,$myrow3['sun_absent'],'left');
		}
		
		if (round($myrow3['sun_total']) > 0.0){
			$sunttimein = date("g:i a", strtotime($myrow3['sunttime_in']));
			$sunttimeout = date("g:i a", strtotime($myrow3['sunttime_out']));
			$sunt = $sunttimein.'-'.$sunttimeout;
			
			
			$LeftOvers = $pdf->addTextWrap(620, $YPos+12 - $cellcount,200,$FontSize,$sunt._(' ').$myrow3['sun_total'],'left');
				
		 
		}

		
		if ($myrow3['mon2_absent'] !=='None' ){
			
			//$montimein = date("g:i a", strtotime($myrow3['montime_in']));
			//$montimeout = date("g:i a", strtotime($myrow3['montime_out']));
			//$mon = $montimein.'-'.$montimeout;
			$LeftOvers = $pdf->addTextWrap(700, $YPos+12 - $cellcount,200,$FontSize,$myrow3['mon2_absent'],'left');
		}
			if (round($myrow3['mon2_total']) > 0.0){
			$mon2timein = date("g:i a", strtotime($myrow3['mon2time_in']));
			$mon2timeout = date("g:i a", strtotime($myrow3['mon2time_out']));
			$mon2= $mon2timein.'-'.$mon2timeout;
			
			
			$LeftOvers = $pdf->addTextWrap(700, $YPos+12 - $cellcount,200,$FontSize,$mon2._(' ').$myrow3['mon2_total'],'left');
				
		 
		}
		
	//tallies
		
		$LeftOvers = $pdf->addTextWrap(800, $YPos+12 - $cellcount,200,$FontSize,$myrow3['total_hrs'],'left');
		
			
		
	
		
		$monday   = $myrow3['mon_total'] ;
		$montotalhours +=$monday;
		
		$tuesday   = $myrow3['tues_total'] ;
		$tuestotalhours +=$tuesday;
		
		$wednesday   = $myrow3['wed_total'] ;
		$wedtotalhours +=$wednesday;
		
		$thursday   = $myrow3['thurs_total'] ;
		$thurstotalhours +=$thursday;
		
		$friday   = $myrow3['fri_total'];
		$fritotalhours +=$friday;
		
		$satday   = $myrow3['sat_total'] ;
		$sattotalhours +=$satday;
		
		$sunday   = $myrow3['sun_total'];
		$suntotalhours +=$sunday;
		
			$monday2   = $myrow3['mon2_total'];
		$mon2totalhours +=$monday2;
		
		
		$totalhours += $myrow3['total_hrs'];
		
		$cellcount = $cellcount + 6;
		$cellcount2 = $cellcount2 + 6;
		
		
			
			
			// Prints salesorderdetails.narrative:
		$FontSize2 = $FontSize*0.8;// Font size to print salesorderdetails.narrative.
		$Width2 = $Page_Width-$Right_Margin-145;// Width to print salesorderdetails.narrative.
		$LeftOvers = trim($myrow1['narrative']);
		//**********
		$LeftOvers = str_replace('\n', ' ', $LeftOvers);// Replaces line feed character.
		$LeftOvers = str_replace('\r', '', $LeftOvers);// Delete carriage return character
		$LeftOvers = str_replace('\t', '', $LeftOvers);// Delete tabulator character
			
			while (mb_strlen($LeftOvers) > 1) {
			$YPos -= $FontSize2;
			if ($YPos < ($Bottom_Margin)) {// Begins new page.
				include ('includes/PDFtimesheetheader.inc');
			}
			$LeftOvers = $pdf->addTextWrap(145, $YPos, $Width2, $FontSize2, $LeftOvers);
		}
			
			if ((mb_strlen($quoteterms) >200 AND $YPos-$line_height <= 75)
			OR (mb_strlen($quoteterms) >1 AND $YPos-$line_height <= 62)
			OR $YPos-$line_height <= 50){
		/* We reached the end of the page so finish off the page and start a newy */
			include ('includes/PDFtimesheetheader.inc');
	} //end if need a new page headed up

				
				
				
				
		}	
	$rowCount++;
}


//total
		
		$pdf->addTextWrap(60, $Bottom_Margin+(2*$line_height),200,$FontSize,"TOTAL",'left');
         
$pdf->addTextWrap(140, $Bottom_Margin+(2*$line_height),200,$FontSize,$montotalhours,'left');
$pdf->addTextWrap(230, $Bottom_Margin+(2*$line_height),200,$FontSize,$tuestotalhours,'left');
$pdf->addTextWrap(313, $Bottom_Margin+(2*$line_height),200,$FontSize,$wedtotalhours,'left');
$pdf->addTextWrap(393, $Bottom_Margin+(2*$line_height),200,$FontSize,$thurstotalhours,'left');
$pdf->addTextWrap(472, $Bottom_Margin+(2*$line_height),200,$FontSize,$fritotalhours,'left');
$pdf->addTextWrap(546, $Bottom_Margin+(2*$line_height),200,$FontSize,$sattotalhours,'left');
$pdf->addTextWrap(630, $Bottom_Margin+(2*$line_height),200,$FontSize,$suntotalhours,'left');
$pdf->addTextWrap(710, $Bottom_Margin+(2*$line_height),200,$FontSize,$mon2totalhours,'left');
$pdf->addTextWrap(800, $Bottom_Margin+(2*$line_height),200,$FontSize,$totalhours,'left');
		/* Print a column vertical line  with enough space for the footer */
			/* draw the vertical column lines to 4 lines shy of the bottom to leave space for invoice footer info ie totals etc */
			//the first part is the horizontal path, second is from top 
//$pdf->line(143, $topheading,143,$Bottom_Margin+(3*$line_height));
$pdf->line(120, $topheading,120,$Bottom_Margin+(2*$line_height));
$pdf->line(210, $topheading,210,$Bottom_Margin+(2*$line_height));
$pdf->line(300, $topheading,300,$Bottom_Margin+(2*$line_height));
$pdf->line(380, $topheading,380,$Bottom_Margin+(2*$line_height));
$pdf->line(459, $topheading,459,$Bottom_Margin+(2*$line_height));
$pdf->line(533, $topheading,533,$Bottom_Margin+(2*$line_height));
$pdf->line(617, $topheading,617,$Bottom_Margin+(2*$line_height));
$pdf->line(700, $topheading,700,$Bottom_Margin+(2*$line_height));
//tallies columns
$pdf->line(790, $topheading,790,$Bottom_Margin+(2*$line_height));

		$FontSize = 10;
		

	$pdf->line($Page_Width-$Right_Margin-810, $Bottom_Margin+(2*$line_height),$Page_Width-$Right_Margin,$Bottom_Margin+(2*$line_height));		$pdf->line($Page_Width-$Right_Margin-810, $Bottom_Margin+(2*$line_height+20),$Page_Width-$Right_Margin,$Bottom_Margin+(2*$line_height+20));
	
	//$pdf->addTextWrap(60, $Bottom_Margin+(2*$line_height),200,$FontSize,"Supervisor Signature:",'left');
//$pdf->addTextWrap(160, $Bottom_Margin+(2*$line_height),200,$FontSize,"_________________",'left');

	//$pdf->addTextWrap(280, $Bottom_Margin+(2*$line_height),200,$FontSize,"Manager Signature:",'left');
//$pdf->addTextWrap(380, $Bottom_Margin+(2*$line_height),200,$FontSize,"_________________",'left');
	//$grand = locale_number_format($grandtotal,2);

//$pdf->addTextWrap(480, $Bottom_Margin+(2*$line_height),200,$FontSize,"Date Signed:",'left');
//$pdf->addTextWrap(550, $Bottom_Margin+(2*$line_height),200,$FontSize,"_________________",'left');
	//$grand = locale_number_format($grandtotal,2);

	$pdf->addTextWrap(60, $Bottom_Margin+(0.5*$line_height),800,8,"Supervisor _____________________          Manager ______________________       Date: ___________________",'left');
//$pdf->addTextWrap(30, $Bottom_Margin+(0.5*$line_height),800,8,"Any guard on AWOL will be taken off the roster.",'left');
			$DisplayFreight = 0;
			
			
			
	$YPos -= $line_height;
	/*rule off for total */
		//	$pdf->line($Page_Width-$Right_Margin-222, $YPos+7,$Page_Width-$Right_Margin,$YPos+7);
$YPos -= $FontSize;
	//$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90-655, $YPos, 655, $FontSize, _('GRAND TOTAL'), 'right');
	//$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos, 90, $FontSize, _('$').$grand, 'right');
	$YPos -= $FontSize;





	// Print salesorders.comments:
	$YPos -= $FontSize*3;
	//$pdf->addText($XPos+550, $YPos+$FontSize, $FontSize, _('Supervision Signiture By:'));
    //$pdf->addText($XPos+650, $YPos+$FontSize, $FontSize, $received_by);
//$pdf->line($Page_Width-$Right_Margin-222, $YPos+$FontSize*-1,$Page_Width-$Right_Margin,$YPos+$FontSize*-1);

//
	//$pdf->addText($XPos+550, $YPos+$FontSize*-2, $FontSize, _('Authorized By:'));
   // $pdf->addText($XPos+650, $YPos+$FontSize*-2, $FontSize, $authorized_by);
//$pdf->line($Page_Width-$Right_Margin-222, $YPos+$FontSize*-3.5,$Page_Width-$Right_Margin,$YPos+$FontSize*-3.5);

	//$pdf->addText($XPos, $YPos+$FontSize-45, $FontSize, _('Comments').':');
	$Width2 = $Page_Width-$Right_Margin-120;// Width to print salesorders.comments.
$LeftOvers =	$pdf->addText($XPos, $YPos+$FontSize-55, $FontSize, $narrative);
	//$LeftOvers = trim($quoteterms);
	//**********
	$LeftOvers = str_replace('\n', ' ', $LeftOvers);// Replaces line feed character.
	$LeftOvers = str_replace('\r', '', $LeftOvers);// Delete carriage return character
	$LeftOvers = str_replace('\t', '', $LeftOvers);// Delete tabulator character
	//**********
	while(mb_strlen($LeftOvers) > 1) {
		$YPos -= $FontSize;
		if ($YPos < ($Bottom_Margin)) {// Begins new page.
				include ('includes/PDFtimesheetheader.inc');
		}
		$LeftOvers = $pdf->addTextWrap(40, $YPos, $Width2, $FontSize, $LeftOvers);
	}


	
	


	
	

ob_end_clean();
	 //its not an email just print the invoice to PDF
		$pdf->OutputD('Timesheet_' . $rosterid. '.pdf');

		
		
	
	$pdf->__destruct();
		
			

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




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

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

?>

