<?php
/**
	Tenders Form
**/
session_start(); 
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("Functions_Sales.php");
include("function.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}


$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2,maxperpage from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


$querylog=" SELECT FI_Tender as loginaccess, login_belongstogrp, ifNull(FI_TenderDel,0) as FI_TenderDel, ifNull(FI_TenderMod,0) as FI_TenderMod, ifNull(FI_TenderApp,0) as FI_TenderApp, Language 
            from ".$_SESSION['dbLabel'].".login  where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}
//echo  $querylog;
if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");

$actionmode 	= $_POST['actionmode'];
$tenderid 		= $_POST['tenderid'];
$txtClientCode 	= $_POST['txtClientCode'];
$txtProjectCode = $_POST['txtProjectCode'];
$txtRcvdDate 	= $_POST['txtRcvdDate'];
$txtDueDate 	= $_POST['txtDueDate'];
$txtSubmitDate 	= $_POST['txtSubmitDate'];

$txtScope 		= $_POST['txtScope'];
$txtAvaDocs 	= $_POST['txtAvaDocs'];
$txtActionReq 	= $_POST['txtActionReq'];
$txtStatusAt 	= $_POST['txtStatusAt'];
$txtAmount 		= $_POST['txtAmount'];

$txtDirCost 	= $_POST['txtDirCost'];
$txtMargin 		= $_POST['txtMargin'];
$txtMarginPer 	= $_POST['txtMarginPer'];
$txtAwardStat 	= $_POST['txtAwardStat'];
$txtReviewReady = $_POST['txtReviewReady'];

$ErrorExists = 0;
$message = "";
	
if ($txtRcvdDate == '') 	$txtRcvdDate = "NULL";
else 						$txtRcvdDate = "'".insertMySQLDate($txtRcvdDate)."'";
if ($txtDueDate == '') 		$txtDueDate = "NULL";
else 						$txtDueDate = "'".insertMySQLDate($txtDueDate)."'";
if ($txtSubmitDate == '') 	$txtSubmitDate = "NULL";
else 						$txtSubmitDate = "'".insertMySQLDate($txtSubmitDate)."'";


if ($actionmode == 'editmode' && $actionmode == 'insert'){
	if ($txtProjectCode != '') {
			$sub_query = " Select Count(Project_Code) From Project ".
						 " Where Project_Code = '".$txtProjectCode."'";
			$sub_result = selectData($connection, $sub_query);
			if ($sub_result[0] == 0) {
				$ErrorExists = 1;
				$message = 'Insert not allowed. Please check for Project Code.';
			}
		}
		
	if ($txtClientCode != '') {
		$sub_query = " Select Count(Ledger_Number) From Clients ".
					 " Where Ledger_Number = '".$txtClientCode."'";
		$sub_result = selectData($connection, $sub_query);
		if ($sub_result[0] == 0) {
			$ErrorExists = 1;
			$message = 'Insert not allowed. Please check for Client Code.';
		}
	}
}

if ($actionmode == 'insert' && $tenderid == '' && $ErrorExists == 0){
	 
	$Ref = get_nextsequence('tender_reference','',$connection);	
	
	$insert  = " insert into tenders (tender_reference, project_code, client_code, tender_receivedDate, tender_documents, tender_scope, tender_dueDate, tender_reviewReady, tender_actionReq, tender_statusAt, ".
			   " tender_submitDate, tender_amount, tender_directCost, tender_margin, tender_marginPer, tender_awardStatus) values ".
			   " ('$Ref', '$txtProjectCode', '$txtClientCode', $txtRcvdDate, ".GetSQLValueString($txtAvaDocs,'text').",  ".GetSQLValueString($txtScope,'text').", $txtDueDate, ".GetSQLValueString($txtReviewReady,'int').", ".
			   " ".GetSQLValueString($txtActionReq,'text').", ".GetSQLValueString($txtStatusAt,'text').", $txtSubmitDate, ".GetSQLValueString($txtAmount,'double').", ".GetSQLValueString($txtDirCost,'double').", ".
			   " ".GetSQLValueString($txtMargin,'double').", ".GetSQLValueString($txtMarginPer,'double').", ".GetSQLValueString($txtAwardStat,'int').") ";
	$result = mysql_query($insert, $connection);
	$message = mysql_error();
}
else if ($actionmode == 'editmode' && $tenderid != '' && $ErrorExists == 0){
	$update = " update tenders set project_code = '$txtProjectCode', client_code = '$txtClientCode', tender_receivedDate = $txtRcvdDate, tender_documents = ".GetSQLValueString($txtAvaDocs,'text').",".
			  " tender_scope = ".GetSQLValueString($txtScope,'text').", tender_dueDate = $txtDueDate, tender_reviewReady = ".GetSQLValueString($txtReviewReady,'int').", tender_actionReq = ".GetSQLValueString($txtActionReq,'text').", ".
			  " tender_statusAt = ".GetSQLValueString($txtStatusAt,'text').", tender_submitDate = $txtSubmitDate, tender_amount = ".GetSQLValueString($txtAmount,'double').", tender_directCost = ".GetSQLValueString($txtDirCost,'double').",".
			  " tender_margin = ".GetSQLValueString($txtMargin,'double').", tender_marginPer = ".GetSQLValueString($txtMarginPer,'double').", tender_awardStatus = ".GetSQLValueString($txtAwardStat,'int')." ".
			  " where tender_id = $tenderid ";
	$result = mysql_query($update, $connection);
	$message = mysql_error();
}

else if ($actionmode == 'delete' && $tenderid != ''){
	$del = "delete from tenders where tender_id = $tenderid ";
	$res = mysql_query($del, $connection);
	$message = mysql_error();
	header("Location: ./Form_TenderSearch.php");
	exit;
}

echo "<html><head><title></title></head><body>";
echo "<form name='tenderaction' method='POST'>";
if ($actionmode != 'delete') {
	echo "<input type='hidden' name='tenderid' value=''>";
	echo "<input type='hidden' name='actionmode' value='' >";
}
echo "<input type='hidden' name='message' value='$message'>";

echo "</form>";
echo "<script> ";
if ($actionmode == 'delete') 
	echo " document.tenderaction.action = 'Form_TenderSearch.php';";
else 	
	echo " document.tenderaction.action = 'Form_Tenders.php';";
echo " document.tenderaction.submit();";
echo "</script>";
echo "</body></html>";


exit;
?>
