<?php
ob_start();
include('includes/session.inc');
$Title = _('Convert Airport Nadi Roster to Timesheet');
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.
		_('Outstanding Approved Job Costing ') . '" /> ' .// Icon title.
		_('Choose below the approved rostering that is approved and not locked') . '</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 Convert to Timesheet ') . ':</td>
							<td><select name="Convert">';
							
						$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="ConvertTimesheet" value="' ._('Convert to timesheet') . '" />
		</div>
		';



echo '</div>';

echo '</div>';
$rosterid = $_POST['Convert'];
//$quotedateraw = $_POST['quotedate'] ;

$quotedate = date("d-m-Y", strtotime($quotedateraw));
$quotedated = date("Y-m-d", strtotime($quotedateraw));

	if (isset($_POST['ConvertTimesheet']) && ($_POST['Convert'] == 0)){


	
	$SQL4 = "SELECT 						rswestid,
											
											start_date,
				                            end_date,
											client,
											patrol_location,
											account_number,
											department,
											status
		                            FROM rosteringwest
									WHERE status = 'approved'
		                             ";

$result4=DB_query($SQL4);
		
		while ($myrow4=DB_fetch_array($result4)){
	
	
	//insert in table airportnadi
	$sql5 = "INSERT INTO timesheetwest (
							twestid,
						
							start_date,
							end_date,
							client,
							patrol_location,
							account_number,
							department,
							status)
				VALUES ('" . $myrow4['rswestid,'] ."',
						
						'" . $myrow4['start_date'] ."',
						'" . $myrow4['end_date'] ."',
						'" . $myrow4['client'] ."',
						'" . $myrow4['patrol_location'] . "',
						'" . $myrow4['account_number']. "',
						'" . $myrow4['department']. "',
						'pending')";

			$ErrMsg = _('This customer could not be added because');  
			$result5 = DB_query($sql5,$ErrMsg);
	
	
	
//update roster to locked

 $sq5 = "UPDATE rosteringwest SET	status='locked' WHERE status ='approved'";

			 
			$ErrMsg = _('The customer could not be updated because');
			$result5 = DB_query($sq5,$ErrMsg);
			prnMsg( _('Rosters Locked'),'success');
			echo '<br />';



//get data from aflrosteringtimesheet
$SQL7 = "SELECT 					*
											
		                            FROM rosteringtimesheetwest
									WHERE twestid = '" . $rosterid . "'
		                             ";

$result7=DB_query($SQL7);
	
	
	
while ($myrow7=DB_fetch_array($result7)){

//insert in airportnaditimesheet
$sql8 = "INSERT INTO timesheetwestdetails 

				VALUES ('" . $myrow7['rswesttimeid'] ."',
						'" . $myrow7['rswestid'] ."',
						'" . $myrow7['employeeid'] ."',
						'" . $myrow7['mon_shift'] ."',
						'" . $myrow7['mon_hrs'] ."',
						'" . $myrow7['tues_shift'] . "',
						'" . $myrow7['tues_hrs']. "',
						'" . $myrow7['wed_shift']. "',
						'" . $myrow7['wed_hrs'] . "',
						'" . $myrow7['thurs_shift']. "',
						'" . $myrow7['thurs_hrs']. "',
						'" . $myrow7['fri_shift']. "',
						'" . $myrow7['fri_hrs']. "',
						'" . $myrow7['sat_shift']. "',
						'" . $myrow7['sat_hrs']. "',
						'" . $myrow7['sun_shift']. "',
						'" . $myrow7['sun_hrs']. "',
						
						'" . $myrow7['total_hrs']. "',
						'" . $myrow7['normal_hours']. "',
						'" . $myrow7['time_half_hours']. "',
						'" . $myrow7['double_hours']. "')";

			$ErrMsg = _('This customer could not be added because');
			$result8 = DB_query($sql8,$ErrMsg);
	
	

	
}
}
	echo '<p class="page_title_text"><a href=Airport-Nadi-Timesheet.php>' . _('All approved Rosters for last week Converted to this weeks timesheet successfully. Go and grab coffee!and then View the Airport Nadi timesheet ');
		
	}

elseif (isset($_POST['ConvertTimesheet']) && ($_POST['Convert'] == $rosterid)){
	//get data from rostering table
	
	$SQL1 = "SELECT 						rswestid,
											period,
											start_date,
				                            end_date,
											client,
											patrol_location,
											account_number,
											department,
											status,
											last_modified_by
		                            FROM rosteringwest
									WHERE rswestid = '" . $rosterid . "'
		                             ";

$result1=DB_query($SQL1);
	$myrow1=DB_fetch_array($result1);
	
	$test = 'test';
	//insert in table airportnadi
	$sql = "INSERT INTO timesheetwest (
							twestid,
							period,
							start_date,
							end_date,
							client,
							patrol_location,
							account_number,
							department,
							status,
							last_modified_by)
				VALUES ('" . $myrow1['rswestid'] ."',
						'" . $myrow1['period'] ."',
						'" . $myrow1['start_date'] ."',
						'" . $myrow1['end_date'] ."',
						'" . $myrow1['client'] ."',
						'" . $myrow1['patrol_location'] . "',
						'" . $myrow1['account_number']. "',
						'" . $myrow1['department']. "',
						'pending',
						'" . $myrow1['last_modified_by']. "')";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
	
	
	
//update roster to locked

 $sql = "UPDATE rosteringwest SET	status='locked' WHERE rswestid ='" . $rosterid . "'";

			 
			$ErrMsg = _('The customer could not be updated because');
			$result = DB_query($sql,$ErrMsg);
			prnMsg( _('Rosters Locked'),'success');
			echo '<br />';



//get data from aflrosteringtimesheet
$SQL2 = "SELECT 						rswesttimeid,
										rswestid,
											employeeid,
											mon_shift,
				                           mon_hrs,
											tues_shift,
				                           tues_hrs,
										   wed_shift,
				                           wed_hrs,
										   thurs_shift,
				                           thurs_hrs,
										   fri_shift,
				                           fri_hrs,
										   sat_shift,
				                           sat_hrs,
										   sun_shift,
				                           sun_hrs,
											total_hrs,
											normal_hours,
											time_half_hours,
											double_hours
											
		                            FROM rosteringtimesheetwest
									WHERE rswestid = '" . $rosterid . "'
		                             ";

$result2=DB_query($SQL2);
	
	
	
while ($myrow2=DB_fetch_array($result2)){

//insert in airportnaditimesheet
$sql3 = "INSERT INTO timesheetwestdetails (
							twesttimeid,
							twestid,
							employeeid,
							mon_shift,
				                           mon_hrs,
											tues_shift,
				                           tues_hrs,
										   wed_shift,
				                           wed_hrs,
										   thurs_shift,
				                           thurs_hrs,
										   fri_shift,
				                           fri_hrs,
										   sat_shift,
				                           sat_hrs,
										   sun_shift,
				                           sun_hrs,
										   
											total_hrs,
											normal_hours,
											time_half_hours,
											double_hours)
				VALUES ('" . $myrow2['rswesttimeid'] ."',
						'" . $myrow2['rswestid'] ."',
						'" . $myrow2['employeeid'] ."',
						'" . $myrow2['mon_shift'] ."',
						'" . $myrow2['mon_hrs'] ."',
						'" . $myrow2['tues_shift'] . "',
						'" . $myrow2['tues_hrs']. "',
						'" . $myrow2['wed_shift']. "',
						'" . $myrow2['wed_hrs'] . "',
						'" . $myrow2['thurs_shift']. "',
						'" . $myrow2['thurs_hrs']. "',
						'" . $myrow2['fri_shift']. "',
						'" . $myrow2['fri_hrs']. "',
						'" . $myrow2['sat_shift']. "',
						'" . $myrow2['sat_hrs']. "',
						'" . $myrow2['sun_shift']. "',
						'" . $myrow2['sun_hrs']. "',
						'" . $myrow2['total_hrs']. "',
						'" . $myrow2['normal_hours']. "',
						'" . $myrow2['time_half_hours']. "',
						'" . $myrow2['double_hours']. "')";

			$ErrMsg = _('This customer could not be added because');
			$result3 = DB_query($sql3,$ErrMsg);
	
	

	
	
}
	echo '<p class="page_title_text"><a href=TimesheetWestGFW.php>' . _('Roster Converted to timesheet successfully. Go and grab coffee!and then View the Nadi GFW timesheet ');
}

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

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

?>
