<?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");

$mainaction 			= $_POST["mainaction"]; 		 if ($mainaction == ""){			$mainaction 		 = $_GET["mainaction"];}   
$actionmode     		= $_POST['actionmode']; 		 if($actionmode == '') 				$actionmode 		 = $_GET['actionmode'];
$product_id 			= $_POST['product_id']; 		 if($product_id == '') 				$product_id	 		 = $_GET['product_id'];
$product_name   		= $_POST['product_name']; 		 if($product_name == '') 			$product_name 		 = $_GET['product_name'];
$archive_id 			= $_POST["archive_id"]; 		 if ($archive_id == ""){			$archive_id 		 = $_GET["archive_id"];}  
$archive_company 		= $_POST["archive_company"]; 	 if ($archive_company == ""){		$archive_company 	 = $_GET["archive_company"];}  
$archive_issuedate 		= $_POST["archive_issuedate"]; 	 if ($archive_issuedate == ""){		$archive_issuedate 	 = $_GET["archive_issuedate"];}  
$archive_vessel 		= $_POST["archive_vessel"]; 	 if ($archive_vessel == ""){		$archive_vessel 	 = $_GET["archive_vessel"];}  
$archive_consigned 		= $_POST["archive_consigned"];   if ($archive_consigned == ""){		$archive_consigned 	 = $_GET["archive_consigned"];}  
$archive_loading 		= $_POST["archive_loading"]; 	 if ($archive_loading == ""){		$archive_loading 	 = $_GET["archive_loading"];}  
$archive_discharge 		= $_POST["archive_discharge"]; 	 if ($archive_discharge == ""){		$archive_discharge 	 = $_GET["archive_discharge"];}  
$archive_freight 		= $_POST["archive_freight"]; 	 if ($archive_freight == ""){		$archive_freight 	 = $_GET["archive_freight"];}  
$archive_producttype 	= $_POST["archive_producttype"]; if ($archive_producttype == ""){	$archive_producttype = $_GET["archive_producttype"];} 
$archive_docnumber	 	= $_POST["archive_docnumber"]; 	 if ($archive_docnumber == ""){		$archive_docnumber 	 = $_GET["archive_docnumber"];} 

$archive_container	 	= $_POST["archive_container"]; 	 if ($archive_container == ""){		$archive_container 	 = $_GET["archive_container"];} 
$archive_ForwardL	 	= $_POST["archive_ForwardL"]; 	 if ($archive_ForwardL == ""){		$archive_ForwardL 	 = $_GET["archive_ForwardL"];} 
$archive_ForwardB	 	= $_POST["archive_ForwardB"]; 	 if ($archive_ForwardB == ""){		$archive_ForwardB 	 = $_GET["archive_ForwardB"];} 
$archive_clearing	 	= $_POST["archive_clearing"]; 	 if ($archive_clearing == ""){		$archive_clearing 	 = $_GET["archive_clearing"];} 
$period_id			 	= $_POST["period_id"]; 			 if ($period_id == ""){		$period_id 	 = $_GET["period_id"];} 

$archive_company1 = $archive_company;
if($archive_company == '') 		$archive_company  	  = "NULL";
else 					   		$archive_company 	  = "'".ereg_replace("'", "\'", trim($archive_company))."'";
if($archive_consigned == '') 	$archive_consigned    = "NULL";
else 					   		$archive_consigned 	  = "'".ereg_replace("'", "\'", trim($archive_consigned))."'";
if($archive_loading == '') 		$archive_loading  	  = "NULL";
else 					  	 	$archive_loading 	  = "'".ereg_replace("'", "\'", trim($archive_loading))."'";
if($archive_freight == '') 		$archive_freight  	  = "NULL";
else 					   		$archive_freight 	  = "'".ereg_replace("'", "\'", trim($archive_freight))."'";
if($archive_producttype == '') 	$archive_producttype  = "NULL";
else 					   		$archive_producttype  = "'".ereg_replace("'", "\'", trim($archive_producttype))."'";
if($archive_shipper == '') 		$archive_shipper  	  = "NULL";
else 					   		$archive_shipper  	  = "'".ereg_replace("'", "\'", trim($archive_shipper))."'";
if($archive_docnumber == '') 	$archive_docnumber  	  = "NULL";
else 					   		$archive_docnumber  	  = "'".ereg_replace("'", "\'", trim($archive_docnumber))."'";


if($archive_container == '') 	$archive_container  	  = "NULL";
else 					   		$archive_container  	  = "'".ereg_replace("'", "\'", trim($archive_container))."'";
if($archive_ForwarderL == '') 	$archive_ForwarderL  	  = "NULL";
else 					   		$archive_ForwarderL  	  = "'".ereg_replace("'", "\'", trim($archive_ForwarderL))."'";
if($archive_ForwarderB == '') 	$archive_ForwarderB  	  = "NULL";
else 					   		$archive_ForwarderB 	  = "'".ereg_replace("'", "\'", trim($archive_ForwarderB))."'";
if($archive_clearing == '') 	$archive_clearing  	  = "NULL";
else 					   		$archive_clearing  	  = "'".ereg_replace("'", "\'", trim($archive_clearing))."'";

if($period_id == '') 			$period_id  	  = "NULL";
else 					   		$period_id  	  = "'".ereg_replace("'", "\'", trim($period_id))."'";

if($archive_issuedate != ''){	
$ndate = explode("/", $archive_issuedate);
$archive_issuedate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
} else $archive_issuedate = "NULL";

$sysdate = date('Y-m-d');
if($actionmode == 'SetPage'){
	$parameters = $_GET['parameters'];
	$perpage 	= $_GET['perpage'];
	$startrange = $_GET['startrange'];
	$parameters = json_decode($parameters, true);
	echo "[BRK]";
	filterGrid($parameters, $perpage, $startrange, $connection);
	exit;
}
if($actionmode == 'insertproduct' && $product_id == ''){
	$query = "insert into product (name) values ('$product_name');";
	$result = mysql_query($query);
	getProductGrid('',$connection);
	echo "[BRK]";
	getAllProductGrid('',$connection);
	echo "[BRK]";
	exit;
} else if($actionmode == 'updateproduct' && $product_id != ''){
	$query = "update product set name = '$product_name' where Auto = $product_id;";
	$result = mysql_query($query);
	getProductGrid('',$connection);
	echo "[BRK]";
	getAllProductGrid('',$connection);
	echo "[BRK]";
	exit;
} else if($actionmode == 'deleteproduct' && $product_id != ''){
	$query = "delete from product where Auto = $product_id;";
	$result = mysql_query($query);
	getProductGrid('',$connection);
	echo "[BRK]";
	getAllProductGrid('',$connection);
	echo "[BRK]";
	exit;
} else if($actionmode == 'editProduct' && $product_id != ''){
	getProductGrid($product_id,$connection);
	echo "[BRK]";
	getAllProductGrid($product_id,$connection);
	echo "[BRK]";
	exit;
}


if($mainaction == 'insert' && $archive_id == ''){
	$queryid = "select ifnull(max(auto),0)+1 from archiving";
	$rs = mysql_query($queryid,$connection);
	$row = mysql_fetch_row($rs);
	$archive_id = $row[0];
	echo $mainaction." ". $archive_id;echo "<br>";

	$query = " insert into archiving (auto, companyname, consigned, portofloading, issuedate, producttype, freight, ".
			 " sysdate, filename, shipper, documentnumber,containertype,ForwarderL,ForwarderB,Clearingtime, period_id) values ".
			 " ($archive_id,$archive_company, $archive_consigned, $archive_loading, $archive_issuedate, ".
			 " $archive_producttype, $archive_freight, null, null, $archive_shipper, $archive_docnumber,$archive_container,$archive_ForwarderL,$archive_ForwarderB,$archive_clearing, $period_id )"	;
	$result = mysql_query($query,$connection);
	
} else if($mainaction == 'edit' && $archive_id != ''){ 

	$query = " update archiving set companyname = $archive_company, consigned = $archive_consigned, 
			portofloading =  $archive_loading, issuedate = $archive_issuedate, producttype = $archive_producttype,
			freight = $archive_freight, sysdate = '$sysdate', filename = null, shipper =  $archive_shipper, documentnumber = $archive_docnumber,
			containertype=$archive_container, ForwarderL=$archive_ForwarderL, ForwarderB=$archive_ForwarderB, Clearingtime=$archive_clearing, period_id = $period_id 
			where auto = $archive_id "; //echo $query;exit;
	$result = mysql_query($query,$connection);
	
}

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

	if (!is_dir($foldername)){
		mkdir($foldername, 0700, true);}
					
	$dest1 = $foldername.'\\'.$fileName1;
	
	$filenames=$archive_company1."\\\\".$fileName1;

	$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 archiving set filename='$filenames' where auto = $archive_id";
//echo $update;exit;
		$queryres=mysql_query($update,$connection);
}

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


?>