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

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


if ($_SESSION['project']=='Monthly maintenance') {

$table_name = $_SESSION['db_prefix']."_maintenance_contracts";
$contract_card_no_query = "SELECT contract_card_no FROM $table_name  ORDER BY contract_card_no DESC  LIMIT 1";
$contract_card_no_result = mySQL_query($contract_card_no_query);
$contract_card_no_num = mysql_num_rows($contract_card_no_result);

//if ($contract_card_no_num==0) {
//	 $contract_card_no = 100;
//}
//else {
	$contract_card_no = mysql_result($contract_card_no_result, 0, "contract_card_no");
	$contract_card_no = $contract_card_no +1;
//}
//echo '<br>*****'.$contract_card_no;

$table_name = $_SESSION['db_prefix']."_maintenance_quotations";
$update_query = "UPDATE $table_name SET  status='contract', invoice_no = '".$_SESSION['invoice_no']."',  invoice_date='".$_SESSION['invoice_date']."', maintenance_card_no='".$contract_card_no."'  WHERE quote_no = '".$_SESSION['maintenance_no']."' ";

execsql($update_query);

$table_name = $_SESSION['db_prefix']."_maintenance_quotations";
$quote_query = "SELECT cust_id, project, project_no, project_area, project_place, project_description, notes FROM $table_name WHERE quote_no = '".$_SESSION['maintenance_no']."' ";
$quote_result = mySQL_query($quote_query);
$quote_num = mysql_num_rows($quote_result);
$cust_id = mysql_result($quote_result, 0, "cust_id");
$project = mysql_result($quote_result, 0, "project");
$project_no = mysql_result($quote_result, 0, "project_no");
$project_area = mysql_result($quote_result, 0, "project_area");
$project_place = mysql_result($quote_result, 0, "project_place");
$project_description = mysql_result($quote_result, 0, "project_description");
$notes = mysql_result($quote_result, 0, "notes");



$table_name = $_SESSION['db_prefix']."_maintenance_contracts";
	$contract_query = "INSERT INTO $table_name (contract_no, contract_card_no,  cust_id, date, project, project_no, project_area, project_place, project_description, quote_note, value, quotation, invoiced, invoice_no, invoice_date) VALUES ('".$_SESSION['maintenance_no']."', '".$contract_card_no."', '".$cust_id."', '".$entry_date."', '".$project."', '".$project_no."', '".$project_area."', '".$project_place."', '".$project_description."', '".$notes."', '".$_SESSION['invoice_amount']."', 'yes', 'yes', '".$_SESSION['invoice_no']."', '".$_SESSION['invoice_date']."' ) ";
	
execsql($contract_query);

$table_name = $_SESSION['db_prefix']."_maintenance_invoices";
$invoice_query = "INSERT INTO $table_name (maintenance_no, maintenance_contract_no, cust_id, invoice_amount, invoice_no, invoice_date, invoiced_by, invoice_notes) VALUES ('".$_SESSION['maintenance_no']."', '".$contract_card_no."', '".$_SESSION['cust_id']."', '".$_SESSION['invoice_amount']."', '".$_SESSION['invoice_no']."', '".$_SESSION['invoice_date']."', '".$_SESSION['invoiced_by']."', '".$_SESSION['invoice_notes']."' ) ";

execsql($invoice_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']."', '127', '".$contract_card_no."', '".$_SESSION['cust_id']."' ,'".$_SESSION['invoiced_by']."', '".$_SESSION['invoice_no']."' )";
$trackingResult = mySQL_query($trackingQuery);

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

$maintenance_no = $_SESSION['maintenance_no'];
$invoice_no = $_SESSION['invoice_no'];

}
else {

$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;
}

$table_name = $_SESSION['db_prefix']."_maintenance_quotations";
$update_query = "UPDATE $table_name SET  status='invoiced', invoice_no = '".$_SESSION['invoice_no']."',  invoice_date='".$_SESSION['invoice_date']."', maintenance_card_no='".$maintenance_card_no."'  WHERE quote_no = '".$_SESSION['maintenance_no']."' ";
execsql($update_query);

$table_name = $_SESSION['db_prefix']."_maintenance_quotations";
$quote_query = "SELECT cust_id, project, project_no, project_area, project_place, project_description, notes FROM $table_name WHERE quote_no = '".$_SESSION['maintenance_no']."' ";
$quote_result = mySQL_query($quote_query);
$quote_num = mysql_num_rows($quote_result);
$cust_id = mysql_result($quote_result, 0, "cust_id");
$project = mysql_result($quote_result, 0, "project");
$project_no = mysql_result($quote_result, 0, "project_no");
$project_area = mysql_result($quote_result, 0, "project_area");
$project_place = mysql_result($quote_result, 0, "project_place");
$project_description = mysql_result($quote_result, 0, "project_description");
$notes = mysql_result($quote_result, 0, "notes");

$table_name = $_SESSION['db_prefix']."_maintenance_jobs";
	$job_query = "INSERT INTO $table_name (maintenance_no, maintenance_card_no, cust_id, date, project, project_no, project_area, project_place, project_description, quote_note, value, quotation, invoiced, invoice_no, invoice_date) VALUES ('".$_SESSION['maintenance_no']."', '".$maintenance_card_no."', '".$cust_id."', '".$entry_date."', '".$project."', '".$project_no."', '".$project_area."', '".$project_place."', '".$project_description."', '".$notes."', '".$_SESSION['invoice_amount']."', 'yes', 'yes', '".$_SESSION['invoice_no']."', '".$_SESSION['invoice_date']."' ) ";
execsql($job_query);

$table_name = $_SESSION['db_prefix']."_maintenance_invoices";
$invoice_query = "INSERT INTO $table_name (maintenance_no, maintenance_card_no,  cust_id, invoice_amount, invoice_no, invoice_date, invoiced_by, invoice_notes) VALUES ('".$_SESSION['maintenance_no']."', '".$maintenance_card_no."', '".$_SESSION['cust_id']."', '".$_SESSION['invoice_amount']."', '".$_SESSION['invoice_no']."', '".$_SESSION['invoice_date']."', '".$_SESSION['invoiced_by']."', '".$_SESSION['invoice_notes']."' ) ";
execsql($invoice_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', '".$_SESSION['maintenance_no']."', '".$_SESSION['cust_id']."' ,'".$_SESSION['invoiced_by']."', '".$_SESSION['invoice_no']."' )";
$trackingResult = mySQL_query($trackingQuery);

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

$maintenance_no = $_SESSION['maintenance_no'];
$invoice_no = $_SESSION['invoice_no'];
}

	$_SESSION['maintenance_no']='';
	$_SESSION['name']="";
	$_SESSION['cust_id']='';
	
	$_SESSION['invoice_amount']='';
	$_SESSION['invoice_no']='';
	$_SESSION['invoiced_by']='';
	$_SESSION['invoice_date']=date("Y-m-d");
	$_SESSION['invoice_notes']='';
?>

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


        
		  window.location="../maintenance/maintenance_invoice_report.php?maintenance_no=<?php echo $maintenance_no ?>&invoice_no=<?php echo $invoice_no ?> " ;
        
       </script>

