<?php
include("../include/session.php");

$userId = $_SESSION['username'];
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s"); 


$line_num = $_GET['line_num'];
$i=0;
while ($i<$line_num) {
	
$table_name = $_SESSION['db_prefix']."_maintenance_jobs";
$card_no_query = "SELECT maintenance_card_no FROM $table_name  ORDER BY maintenance_card_no DESC  LIMIT 1";
$card_no_result = mySQL_query($card_no_query);
$card_no_num = mysql_num_rows($card_no_result);

if ($card_no_num==0) {
	 $maintenance_card_no = 100;
}
else {
	$maintenance_card_no = mysql_result($card_no_result, 0, "maintenance_card_no");
	$maintenance_card_no = $maintenance_card_no +1;
}


$contract_card_no = $_GET['contract_card_no_text'.$i];
$cycle_no = $_GET['cycle_no_text'.$i];

$table_name = $_SESSION['db_prefix']."_maintenance_contracts";
$contract_query = "SELECT cust_id, project_no, project_area, project_place  FROM $table_name WHERE contract_card_no = '".$contract_card_no."'  ";
$contract_result = mySQL_query($contract_query);
$contract_num = mysql_num_rows($contract_result);
$cust_id = mysql_result($contract_result, 0, "cust_id");

$project_no = mysql_result($contract_result, 0, "project_no");
$project_area = mysql_result($contract_result, 0, "project_area");
$project_place = mysql_result($contract_result, 0, "project_place");

$table_name = $_SESSION['db_prefix']."_maintenance_jobs";
	$job_query = "INSERT INTO $table_name (maintenance_no, maintenance_card_no, cycle_no, cust_id, date, start_date, end_date, team, project, project_no, project_area, project_place, project_description, quote_note, value, call_out_charge, additional_charge, quotation, invoiced, invoice_no, invoice_date, card_opened) VALUES ('".$contract_card_no."', '".$maintenance_card_no."', '".$cycle_no."', '".$cust_id."', '".$entry_date."', '".$_SESSION['schedule_date']."', '".$_SESSION['schedule_date']."', '".$_SESSION['team']."', 'Monthly maintenance', '".$project_no."', '".$project_area."', '".$project_place."', 'Monthly maintenance', '', '375', '100', '0', 'yes', 'yes', '', '', 'yes' ) ";
execsql($job_query);


$table_name = $_SESSION['db_prefix']."_tracking_maintenance";
$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '122', '".$contract_card_no."', '".$cust_id."' ,'".$maintenance_card_no."', '' )";
$trackingResult = mySQL_query($trackingQuery);

//include('../include/maintenance_invoiced_alert.php');

$i++;


}



$_SESSION['schedule_date'] = date("Y-m-d"); 
$_SESSION['cluster'] = '';
$_SESSION['team'] = '';

?>

<script language="javascript" type="text/javascript">


        
		  window.location="../maintenance/maintenance_main.php" ;
        
       </script>

