<?php
ob_start();
include('includes/session.inc');
$Title = _('GFS Roster Template');
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 Creation') . '" /> ' .// Icon title.
		_('Choose below the Template Rosters for GFS') . '</p>';// Page title.

echo '<div class="col-sm-10">';// div class=?
echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
	
	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<table class="table">';

echo '<td>' . _('Select Template rosters to create roster') . ':</td>
							<td><select name="ConvertGFS">';
							
							$SQL = "SELECT rosterid,
											
											start_date,
				                            end_date,
											sectors,
											patrol_location,
											account_number,
											department,
											status,
											last_modified_by
							
		                            FROM rostering
									WHERE end_date = '2017-07-30 10:00:00'
		                             ";

$result=DB_query($SQL);
//echo '<option value="0">0 - ' . _('None Available') . '</option>';
 echo '<option value="0">0 - ' . _('Create All 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['RosterPeriod']) AND $_POST['RosterPeriod']==$myrow['rosterid']){
	    
		echo '<option selected="selected" value="' . $myrow['rosterid'] . '">' . $myrow['rosterid'].' - ' .$myrow['period'] . ' - ' .$myrow['start_date'] . ' - ' .$myrow['end_date'] .' -' .$myrowS['name'] .' -' .$myrowP['patrollocation'] .' -' .$myrow['department'] . '-' .$myrow['status'] .'</option>';
	} else {
			echo '<option value="' . $myrow['rosterid'] . '">' . $myrow['rosterid'].' - ' .$myrow['period'] . ' - ' .$myrow['start_date'] . ' - ' .$myrow['end_date'] .' -' .$myrowS['name'] .' -' .$myrowP['patrollocation'] .' -' .$myrow['department'] . '-' .$myrow['status'] .'</option>';
	}
	
	}
	
}
echo '</select>';

echo '	<tr>
				<td>' . _('Start Date') . ' (' . $_SESSION['DefaultDateFormat'] . '):</td>
				<td><input ' . (in_array('ClientSince',$Errors) ?  'class="inputerror"' : '' ) .' type="text" class="date" alt="'.$_SESSION['DefaultDateFormat'].'" name="startdate" size="11" maxlength="10" value="' . $_POST['startdate'] . '" /></td>
			</tr>';


echo '	<tr>
				<td>' . _('End Date') . ' (' . $_SESSION['DefaultDateFormat'] . '):</td>
				<td><input ' . (in_array('ClientSince',$Errors) ?  'class="inputerror"' : '' ) .' type="text" class="date" alt="'.$_SESSION['DefaultDateFormat'].'" name="enddate" size="11" maxlength="10" value="' . $_POST['enddate'] . '" /></td>
			</tr>';
echo '<br>';
echo '<input type="submit" name="ConvertTimesheetSuva" value="' ._('Create Roster ') . '" />

</table>
		</div>
		';



echo '</div>';


$rosterid = $_POST['ConvertGFS'];

$startdate1 = $_POST['startdate'];
$enddate1 = $_POST['enddate'];
$user =  $_SESSION['userid'];

$timestamp1 = strtotime($startdate);
$timestamp2 = strtotime($enddate);

$startdate = date("Y-m-d H:i:s", $timestamp1);
$enddate = date("Y-m-d H:i:s", $timestamp2);

//$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 						
											
											start_date,
				                            end_date,
											sectors,
											patrol_location,
											account_number,
											department,
											status,
											last_modified_by
		                            FROM rostering
									WHERE end_date= '2017-07-30 10:00:00'
									AND
									rosterid = '" . $rosterid . "'
		                             ";

$result4=DB_query($SQL4);
		
		
		
while ($myrow4=DB_fetch_array($result4)){
	
	
	//insert in table rostering
	$sql5 = "INSERT INTO rostering (
							
						
							start_date,
							end_date,
							sectors,
							patrol_location,
							account_number,
							department,
							status,
							last_modified_by)
				VALUES (
						'" . $startdate ."',
						'" . $enddate ."',
						'" . $myrow4['sectors'] ."',
						'" . $myrow4['patrol_location'] . "',
						'" . $myrow4['account_number']. "',
						'" . $myrow4['department']. "',
						'pending',
						'" . $user. "')";

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


//get data from rosteringtimesheet
$SQL7 = "SELECT 						
											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,
										   mon_shift_2,
										   mon_hrs_2,
											total_hrs,
											normal_hours,
											time_half_hours,
											double_hours
											
		                            FROM rosteringtimesheet
									WHERE rosterid = '" . $rosterid . "'
		                             ";

$result7=DB_query($SQL7);
	
		//quickly check the new roster created.
	
	$SQL780 = "SELECT 						
											rosterid,
											start_date,
				                            end_date,
											sectors,
											patrol_location,
											account_number,
											department,
											status,
											last_modified_by
		                            FROM rostering
									ORDER BY rosterid DESC LIMIT 1
		                             
		                             ";

     $result80=DB_query($SQL80);
	$myrow80=DB_fetch_array($result80);
	
while ($myrow7=DB_fetch_array($result7)){


//insert in airportnaditimesheet
$sql8 = "INSERT INTO rosteringtimesheet(
							rosterid,
							
							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,
										   mon_shift_2,
										   mon_hrs_2,
											total_hrs,
											normal_hours,
											time_half_hours,
											double_hours)
				VALUES (
						'" . $myrow80['rosterid'] ."',
						'" . $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['mon_shift_2']. "',
						'" . $myrow7['mon_hrs_2']. "',
						'" . $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=rostering.php>' . _('All GFS Rosters Created for selected start date and end date.');
		
	}

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


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

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

	



//get data from rosteringtimesheet
$SQL2 = "SELECT 						
											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,
										   mon_shift_2,
										   mon_hrs_2,
											total_hrs,
											normal_hours,
											time_half_hours,
											double_hours
											
		                            FROM rosteringtimesheet
									WHERE rosterid = '" . $rosterid . "'
		                             ";

$result2=DB_query($SQL2);
	
	
	
while ($myrow2=DB_fetch_array($result2)){
//quickly check the new roster created.
	
	$SQL780 = "SELECT 						
											rosterid,
											start_date,
				                            end_date,
											sectors,
											patrol_location,
											account_number,
											department,
											status,
											last_modified_by
		                            FROM rostering
									ORDER BY rosterid DESC LIMIT 1
		                             
		                             ";

     $result80=DB_query($SQL80);
	$myrow80=DB_fetch_array($result80);

//insert in airportnaditimesheet
$sql3 = "INSERT INTO rosteringtimesheet (
							
							rosterid,
							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,
										   mon_shift_2,
										   mon_hrs_2,
										   
											total_hrs,
											normal_hours,
											time_half_hours,
											double_hours)
				VALUES (
						'" . $myrow80['rosterid'] ."',
						'" . $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['mon_shift_2']. "',
						'" . $myrow2['mon_hrs_2']. "',
						'" . $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=rostering.php>' . _('Suva GFS Roster Created for selected start and end date');
}

//echo 'Quotation Created Successfully'. $jobno.'!';
echo '</div>';
echo '</div>';
echo '</div>';
//include('includes/footer.inc'); 

?>
