<?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.
		_('GFS ROSTER CONVERT TO TIMESHEET') . '" /> ' .// Icon title.
		_('Choose below the approved rostering for GFS 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="ConvertGFS">';
							
							$SQL = "SELECT rosterid,
											
											start_date,
				                            end_date,
											sectors,
											patrol_location,
											account_number,
											department,
											status,
											last_modified_by,
											lastupdate
							
		                            FROM rostering
									WHERE status = 'approved'
		                             ";

$result=DB_query($SQL);
//echo '<option value="0">0 - ' . _('None Available') . '</option>';
 echo '<option value="0">0 - ' . _('Convert All Approved Rosters for Suva-GFS') . '</option>';
while ($myrow=DB_fetch_array($result)){
	
	$SQLpatrol = "SELECT 
											patrollocation
		                            FROM guardforceclient
		                            WHERE clientid ='" . $myrow['patrol_location']."' ";
	$resultP=DB_query($SQLpatrol);
	
	$SQLsector = "SELECT 
											name
		                            FROM sectors
		                            WHERE id='" . $myrow['sectors']."' ";
	$resultS=DB_query($SQLsector);
	
	while ($myrowP=DB_fetch_array($resultP) AND $myrowS=DB_fetch_array($resultS)){
	if (isset($_POST['ConvertGFS']) AND $_POST['ConvertGFS']==$myrow['rosterid']){
	    
		echo '<option selected="selected" value="' . $myrow['rosterid'] . '">' . $myrow['rosterid'] .$myrow['start_date'] . ' - ' .$myrow['end_date'] .' -' .$myrowS['name'] .' -' .$myrowP['patrollocation'] .' -' .$myrow['department'] . '-' .$myrow['status'] .'</option>';
	} else {
			echo '<option value="' . $myrow['rosterid'] . '">' . $myrow['rosterid'] . ' - ' .$myrow['start_date'] . ' - ' .$myrow['end_date'] .' -' .$myrowS['name'] .' -' .$myrowP['patrollocation'] .' -' .$myrow['department'] . '-' .$myrow['status'] .'</option>';
	}
	
	}
	
}
echo '</select>';


echo '<input type="submit" name="ConvertTimesheetSuva" value="' ._('Convert to timesheet') . '" />
		</div>
		';



echo '</div>';

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

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

	if (isset($_POST['ConvertTimesheetSuva']) && ($_POST['ConvertGFS'] == 0)){


	
	$SQL4 = "SELECT 						rosterid,
											
											start_date,
				                            end_date,
											sectors,
											patrol_location,
											account_number,
											department,
											status,
											last_modified_by,
											lastupdate
		                            FROM rostering
									WHERE status = 'approved'
		                             ";

$result4=DB_query($SQL4);
		
		while ($myrow4=DB_fetch_array($result4)){
	
	
	//insert in table airportnadi
	$sql5 = "INSERT INTO timesheetsuva (
							timesuvaid,
						
							start_date,
							end_date,
							sectors,
							patrol_location,
							account_number,
							department,
							status,
							last_modified_by,
							lastupdate)
				VALUES ('" . $myrow4['rosterid'] ."',
						
						'" . $myrow4['start_date'] ."',
						'" . $myrow4['end_date'] ."',
						'" . $myrow4['sectors'] ."',
						'" . $myrow4['patrol_location'] . "',
						'" . $myrow4['account_number']. "',
						'" . $myrow4['department']. "',
						'pending',
						'" . $myrow4['last_modified_by']. "',
						'" . $myrow4['lastupdate']. "')";

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

 $sq5 = "UPDATE rostering 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 rosteringtimesheet
$SQL7 = "SELECT 						*
											
		                            FROM rosteringtimesheet
									WHERE rosterid = '" . $rosterid . "'
		                             ";

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

//insert in airportnaditimesheet
$sql8 = "INSERT INTO timesheetsuvadetails SELECT * FROM rosteringtimesheet
			WHERE rosterid = '" . $rosterid . "'";

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

	
}
}
	echo '<p class="page_title_text"><a href=timesheetGFS.php>' . _('All approved GFS Rosters for last week Converted to this weeks timesheet successfully. Go and grab coffee!and then View the GFS timesheet by clicking here or going to respective section in the module.');
		
	}

elseif (isset($_POST['ConvertTimesheetSuva']) && ($_POST['ConvertGFS'] == $rosterid)){
	//get data from rostering table
	
	$SQL1 = "SELECT 						rosterid,
										
											start_date,
				                            end_date,
											sectors,
											patrol_location,
											account_number,
											department,
											status,
											last_modified_by,
											lastupdate
		                            FROM rostering
									WHERE rosterid = '" . $rosterid . "'
		                             ";

$result1=DB_query($SQL1);
	$myrow1=DB_fetch_array($result1);
	
	$test = 'test';
	//insert in table airportnadi
	$sql = "INSERT INTO timesheetsuva (
							timesuvaid,
							
							start_date,
							end_date,
							sectors,
							patrol_location,
							account_number,
							department,
							status,
							last_modified_by,
							lastupdate)
				VALUES ('" . $myrow1['rosterid'] ."',
						
						'" . $myrow1['start_date'] ."',
						'" . $myrow1['end_date'] ."',
						'" . $myrow1['sectors'] ."',
						'" . $myrow1['patrol_location'] . "',
						'" . $myrow1['account_number']. "',
						'" . $myrow1['department']. "',
						'pending',
						'" . $myrow1['last_modified_by']. "',
						'" . $myrow1['lastupdate']. "')";

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

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

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



//get data from rosteringtimesheet
$SQL2 = "SELECT 						*
											
		                            FROM rosteringtimesheet
									WHERE rosterid = '" . $rosterid . "'
		                             ";

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

//insert in airportnaditimesheet
$sql3 = "INSERT INTO timesheetsuvadetails SELECT * FROM rosteringtimesheet
			WHERE rosterid = '" . $rosterid . "'";
			$ErrMsg = _('This customer could not be added because');
			$result3 = DB_query($sql3,$ErrMsg);
	
	

	
	

	echo '<p class="page_title_text"><a href=timesheetGFS.php>' . _('Suva GFS Roster Converted to timesheet successfully. Go and grab coffee!and then View this timesheet by clicking here or from respective section in the module');
}

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

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

?>
