<?php

session_start();
header("Content-type: text/html; charset=utf8");

if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("Forms_DataGrid_Archive.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");


$mainmode 			= $_POST["mainmode"]; 		 	if (mainmode){					$mainmode 			 = $_GET["mainmode"];}   
$mainaction 		= $_POST["mainaction"]; 		if ($mainaction == ""){			$mainaction 		 = $_GET["mainaction"];}   
$actionmode     	= $_POST['actionmode']; 		if($actionmode == '') 			$actionmode 		 = $_GET['actionmode'];
$offer_id 			= $_POST["offer_id"]; 		    if ($offer_id == ""){			$offer_id 		     = $_GET["offer_id"];}  
$auto 				= $_POST["auto"]; 		    	if ($auto == ""){				$auto 		     	 = $_GET["auto"];}  
$archive_offer	 	= $_POST["archive_offer"]; 	 	if ($archive_offer == ""){		$archive_offer 		 = $_GET["archive_offer"];} 
$archive_issue_date	= $_POST["archive_issue_date"]; if ($archive_issue_date == ""){	$archive_issue_date  = $_GET["archive_issue_date"];} 
$archive_expire_date= $_POST["archive_expire_date"];if ($archive_expire_date == ""){$archive_expire_date = $_GET["archive_expire_date"];} 
$archive_account	= $_POST["archive_account"]; 	if ($archive_account == ""){	$archive_account 	 = $_GET["archive_account"];} 
//$archive_size1	 	= $_POST["archive_size1"]; 	 	if ($archive_size1 == ""){		$archive_size1 		 = $_GET["archive_size1"];} 
//$archive_size2	 	= $_POST["archive_size2"]; 		if ($archive_size2 == ""){		$archive_size2 	 	 = $_GET["archive_size2"];} 
$archive_POL	 	= $_POST["archive_POL"]; 	 	if ($archive_POL == ""){		$archive_POL 	 	 = $_GET["archive_POL"];} 
$archive_POD	 	= $_POST["archive_POD"]; 	 	if ($archive_POD == ""){		$archive_POD 		 = $_GET["archive_POD"];} 
$archive_FOD	 	= $_POST["archive_FOD"]; 	 	if ($archive_FOD == ""){		$archive_FOD 		 = $_GET["archive_FOD"];} 
$archive_type	 	= $_POST["archive_type"]; 	 	if ($archive_type == ""){		$archive_type 		 = $_GET["archive_type"];} 
$txtclientcode		= $_POST["txtclientcode"]; 	 	if ($txtclientcode == ""){		$txtclientcode 		 = $_GET["txtclientcode"];} 
$freedt				= $_POST["freedt"]; 	 		if ($freedt == ""){				$freedt 		 	 = $_GET["freedt"];} 

$archive_account1=$archive_account;
$archive_offer1=$archive_offer;
		
$archive_type 	  = "'".ereg_replace("'", "\'", trim($archive_type))."'";
$archive_FOD 	  = "'".ereg_replace("'", "\'", trim($archive_FOD))."'";
$archive_POD 	  = "'".ereg_replace("'", "\'", trim($archive_POD))."'";
$archive_POL 	  = "'".ereg_replace("'", "\'", trim($archive_POL))."'";
//$archive_size2 	  = "'".ereg_replace("'", "\'", trim($archive_size2))."'";
//$archive_size1 	  = "'".ereg_replace("'", "\'", trim($archive_size1))."'";
$archive_offer	  = "'".ereg_replace("'", "\'", trim($archive_offer))."'";
$archive_account  = "'".ereg_replace("'", "\'", trim($archive_account))."'";
$txtclientcode	= "'".ereg_replace("'", "\'", trim($txtclientcode))."'";

$ndate = explode("/", $archive_issue_date);
$archive_issue_date = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	
$ndate = explode("/", $archive_expire_date);
$archive_expire_date = $ndate[2]."-".$ndate[1]."-".$ndate[0];

$sysdate = date('Y-m-d');

if ($freedt == '') $freedt = 0;

//details
$itemcode 			= $_GET["itemcode"]; 
$txtdescription 	= $_GET["txtdescription"]; 
$txtamount		 	= $_GET["txtamount"]; 
$txtqty 			= $_GET["txtqty"]; 
$txtdiscount 		= $_GET["txtdiscount"]; 
$txtcurrency_code 	= $_GET["txtcurrency_code"]; 
$offerdt_id 		= $_GET["offerdt_id"]; 

 if ($mainaction == 'setItemInfo'){
	$queryitem = "select item_description from items where item_code = '$itemcode' ";
	$resitem = mysql_query($queryitem, $connection);
	$rwitem = mysql_fetch_row($resitem);
	$itemdesc = $rwitem[0];
	
	echo "[BRK]";
	echo $itemdesc;
	exit;	
} 


 if ($mainaction == 'inserdt'){
	$errorExists = 0;
	$message = "";
	 if ($offer_id == ''){
		
		$querychk = "select 1 from archivingoffer where Offer_code = $archive_offer ";	
		$reschk = 	mysql_query($querychk,$connection);
		$numr = mysql_num_rows($reschk);
		if ($numr > 0){
			$message = "Offer Code Already Exists!";
			$errorExists = 1;
		} else {
			$insertoffer="insert into archivingoffer ( Offer_code,issuedate, account, POL, POD, Expiredate, FOD, type, client_code, freetimedetention)
						values($archive_offer,'$archive_issue_date'	 ,$archive_account	 ,$archive_POL,$archive_POD,'$archive_expire_date',$archive_FOD, $archive_type, $txtclientcode, $freedt)";		
			$resinsert = mysql_query($insertoffer,$connection);
			$offer_id = mysql_insert_id();
		}
	} 
	
	  if ($errorExists == 0){	
		$insertofferdt = " insert into archivingofferdt (offer_id, item_code, description, amount, qty, discount, currency_code) values 
						  ($offer_id, ".strformat($itemcode).", ".strformat($txtdescription).", ".strformat($txtamount).", ".strformat($txtqty).", ".strformat($txtdiscount).",".strformat($txtcurrency_code).") ";			
		$resinsertdt = 	mysql_query($insertofferdt,$connection);		
	} 
		echo "[BRK]";
		echo $message;
		echo "[BRK]";
		echo $offer_id;
		echo "[BRK]";
		offerdtGrid($offer_id, $connection);
		
	exit; 
} 
 if ($mainaction == 'editdt' && $offerdt_id != ''){
	$updateofferdt = " update archivingofferdt set offer_id = $offer_id, item_code = ".strformat($itemcode).", description = ".strformat($txtdescription).", 
									amount = ".strformat($txtamount).", qty = ".strformat($txtqty).", discount = ".strformat($txtdiscount).", currency_code = ".strformat($txtcurrency_code)." 
					  where offerdt_id = $offerdt_id ";			
	$resupdatedt = 	mysql_query($updateofferdt,$connection);	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	echo $offer_id;
	echo "[BRK]";
	offerdtGrid($offer_id, $connection);
	exit;
} 

if ($mainaction == 'deletedt' && $offerdt_id != ''){
	$del = "delete from archivingofferdt where offerdt_id = $offerdt_id ";
	$resdel = mysql_query($del,$connection);	
	if (!$resdel) $message = mysql_error();
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	offerdtGrid($offer_id, $connection);
	exit;
}

if ($mainaction == 'insert' && $offer_id == ''){
	$insertoffer="insert into archivingoffer ( Offer_code,issuedate, account, POL, POD, Expiredate, FOD, type, client_code, freetimedetention)
	values($archive_offer,'$archive_issue_date'	 ,$archive_account	 ,$archive_POL,$archive_POD,'$archive_expire_date',$archive_FOD,
	 $archive_type, $txtclientcode, $freedt	)";
		
		//echo $insertoffer;
	 	$result = mysql_query($insertoffer,$connection)or die (mysql_error());
		
		$insertofferauto="select max(auto) from archivingoffer";
		$result = mysql_query($insertofferauto,$connection)or die (mysql_error());
		$row=mysql_fetch_array($result);
		$offer_id=$row[0];
		//echo "offer=".$offer_id;

}
	
	else if($mainaction == 'edit' && $offer_id != ''){
		
		$updatetoffer="update archivingoffer set Offer_code=$archive_offer,issuedate='$archive_issue_date', account=$archive_account,
		POL=$archive_POL, POD=$archive_POD, Expiredate='$archive_expire_date',
		FOD=$archive_FOD, type=$archive_type, client_code = $txtclientcode,
		freetimedetention = $freedt where auto='$offer_id'";
	
		
		//echo $updatetoffer;
	 	$result = mysql_query($updatetoffer,$connection)or die (mysql_error());
		//header ("Location: ./Form_offer.php");

	}

else if ($mainmode=='delete'){
	$query="delete from archivingoffer where auto='$offer_id'";
	
	$queryres=mysql_query($query,$connection);

header ("Location: ./Form_offerSearch.php");
exit;
	
}


if(is_uploaded_file($_FILES['offerfile']['tmp_name'])) {
	 
	$fileType1 = $_FILES['offerfile']['type'];$fileName1 = $_FILES['offerfile']['name'];
	$tmpName1  = $_FILES['offerfile']['tmp_name'];$fileSize1 = $_FILES['offerfile']['size'];
	
	//if($archive_path == '') $archive_path = "Offer";
	
	//$dest_dir="./".$archive_path;
	//$foldername = $dest_dir."/";

	//if (!is_dir($foldername)){
	//	mkdir($foldername, 0700, true);
		//echo "dfgdfrd";}
					
	//$dest1 = $foldername.'\\'.$fileName1;
	
	$filenames=$fileName1;
//echo "filenames".$filenames."<br>";
	$fp1 = fopen($tmpName1, 'r');
	$content1 = fread($fp1, $fileSize1);
	//$content1 = addslashes($content1);
	fclose($fp1);
	if(!get_magic_quotes_gpc())
	$fileName1 = addslashes($fileName1);

	//$r1 = move_uploaded_file ($tmpName1, $dest1);
	if($filenames != '')
		$update =  "update archivingoffer set filename='$filenames' where auto = $offer_id";

		$queryres=mysql_query($update,$connection);
		//echo $update;
		//echo "<br>archive_path=$archive_path<br>";
		echo "<br>filenames=$filenames<br>";
		//exit;
}

else if($mainmode =='confirm' && $auto != ''){
	
	$confirm ="update archivingoffer set confirmed=1 where auto=$auto";
	$queryres=mysql_query($confirm,$connection);
	//header ("Location: ./Form_offersearch.php?PMode=1");
	header("location:javascript://history.go(-1)");
	exit;
}
else if($mainmode =='unconfirm' && $auto != ''){
	$confirm ="update archivingoffer set confirmed=0 where auto=$auto";
	$queryres=mysql_query($confirm,$connection);
	header("location:javascript://history.go(-1)");
	exit;
}

header ("Location: ./Form_offer.php");
exit; 

function strformat($str){
	if($str == '') $str = "NULL";
	else $str = "'".ereg_replace("'", "\'", trim($str))."'";
	return $str;
}  


?>