<?php
include('includes/session.inc');
$Title = _('CEM biometric data upload');
include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');
echo '<p class="page_title_text"><img alt="" src="' . $RootPath . '/css/' . $Theme .
		'/images/maintenance.png" title="' .
		_('CEM biometric data upload from CSV file') . '" />' . ' ' .
		_('CEM biometric data upload from CSV file') . '</p>';


$currentdate = date('Y-m-d h:i:s', time());
$FieldHeadings = array( 'Payroll Number',			//  0 'STOCKID',
						'Date',	
						'Swipe In',	
					   	'Swipe Out',
					   	'Minutes');

if (isset($_FILES['CostUpdateFile']) and $_FILES['CostUpdateFile']['name']) { //start file processing
	
	$FileName = $_FILES['CostUpdateFile']['name'];
	$TempName  = $_FILES['CostUpdateFile']['tmp_name'];
	$FileSize = $_FILES['CostUpdateFile']['size'];
	$InputError = 0;
	
	//get file handle
	$FileHandle = fopen($TempName, 'r');
	//get the header row
	$HeadRow = fgetcsv($FileHandle, 10000, ',');

	

	
	
	//start database transaction
	DB_Txn_Begin();

	//loop through file rows
	$LineNumber = 7;
	while ( ($myrow = fgetcsv($FileHandle, 10000, ',')) !== FALSE ) {

		
		$employeeid = $myrow[1];
		$date = $myrow[3];
		$in = $myrow[4];
         $out= $myrow[5];
		$hours= $myrow[6];
		$sqldate = FormatDateForSQL($date);
			
$SQL = "INSERT INTO cembiometric (
												employeeid,
												
												date,
												swipe_in,
												swipe_out,
												hours)
										VALUES ( $employeeid ,
												'" . $sqldate . "',
												'" . $in. "',
												'" .$out. "',
												'" . $hours/60 . "')";
					
			$ErrMsg = _('The upload for CEM biometric data for Airport Nadi could not be updated because');
			$DbgMsg = _('The SQL that failed was');
			$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);

				

		$LineNumber++;
	}

	DB_Txn_Commit();
	prnMsg( _('Batch Update of CEM biometric data ') .' ' . $FileName  . ' '. _('has been completed. All transactions committed to the database.'),'success');

	fclose($FileHandle);

} else { //show file upload form

	echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '" method="post" enctype="multipart/form-data">';
	echo '<div class="centre">';
	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
	echo '<div class="page_help_text">' .
			_('This function uploads CEM biometric data  from a comma separated variable (csv) file. First create job costing number in a project before uploading this file ') . '<br />' .
			_('The file must be exactly like how biometric sends. Do not edit the headers :') . 
			_('Please ensure no formating or borders there and file save as CSV file.') .  '<br />' .
			_('Enjoy!.') . '</div>';

	echo '<br /><input type="hidden" name="MAX_FILE_SIZE" value="1000000" />' ._('Upload file') . ': <input name="CostUpdateFile" type="file" />
			<input type="submit" name="submit" value="' . _('Send File') . '" />
		</div>
		</form>';
}

include('includes/footer.inc');

?>