<?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 aflrosterid,
											start_date,
				                            end_date,
											client,
											roster_group,
											patrol_location,
											department,
											status
		                            FROM aflrostering
									WHERE status = 'approved' 
		                             ";

$result=DB_query($SQL);
//echo '<option value="0">0 - ' . _('None Available') . '</option>';
 echo '<option value="0">0 - ' . _('Convert All Approved Rosters Only') . '</option>';
while ($myrow=DB_fetch_array($result)){
	
	$SQLpatrol = "SELECT 
											guard_location,
											debtorno
		                            FROM aflpatrollocations
		                            WHERE aflid ='" . $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['aflrosterid']) {
	    
		echo '<option selected="selected" value="' . $myrow['aflrosterid'] . '">' . $myrow['aflrosterid']. ' - ' .$myrow['start_date'] . ' - ' .$myrow['end_date'] .' -' .$myrowR['name'] .' -' .$myrowP['guard_location'] .' -' .$myrow['roster_group'].' -' .$myrow['department'] . '-' .$myrow['status'] .'</option>';
	} else {
			echo '<option value="' . $myrow['aflrosterid'] . '">' . $myrow['aflrosterid'].' - ' .$myrow['start_date'] . ' - ' .$myrow['end_date'] .' -' .$myrowR['name'] .' -' .$myrowP['guard_location'] .' -' .$myrow['roster_group'].' -' .$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 						aflrosterid,
											
											start_date,
				                            end_date,
											client,
											patrol_location,
											department,
											status,
											last_modified_by
		                            FROM aflrostering
									WHERE status = 'approved'
		                             ";

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

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

 $sq5 = "UPDATE aflrostering 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 aflrosteringtimesheet
									WHERE aflrosterid = '" . $rosterid . "'
		                             ";

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

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

			$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 						aflrosterid,
											
											start_date,
				                            end_date,
											client,
											patrol_location,
											account_number,
											department,
											status,
											last_modified_by
		                            FROM aflrostering
									WHERE aflrosterid = '" . $rosterid . "'
		                             ";

$result1=DB_query($SQL1);
	$myrow1=DB_fetch_array($result1);
	
	$test = 'test';
	//insert in table airportnadi
	$sql = "INSERT INTO airportnadi (
							airportnadiid,
						
							start_date,
							end_date,
							client,
							patrol_location,
							account_number,
							department,
							status,
							last_modified_by)
				VALUES ('" . $myrow1['aflrosterid'] ."',
					
						'" . $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 aflrostering SET	status='Locked' WHERE aflrosterid ='" . $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 						*
											
		                            FROM aflrosteringtimesheet
									WHERE aflrosterid = '" . $rosterid . "'
		                             ";

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

//insert in airportnaditimesheet
$sql3 = "INSERT INTO airportnaditimesheet SELECT * FROM aflrosteringtimesheet
			WHERE aflrosterid = '" . $rosterid . "'";


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

	
	
}
	echo '<p class="page_title_text"><a href=Airport-Nadi-Timesheet.php>' . _('Roster Converted to timesheet successfully. Hurray! Click here to View the Airport Nadi timesheet ');
}

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

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

?>
