<?php

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

include("config.inc.php");
include("function.inc.php");
include("Functions_Synchro.php");
include("supplier_function.php");
$lastinsertedid="";
$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) 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);
}

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp 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);
}








if($_POST){
	extract($_POST);
	// var_dump($_POST);
}
if($_GET){
	extract($_GET);
	// var_dump($_GET);
}

if($mode=='insert'){

	// getting total received cylinders
	$querySum = "select SUM(cylinder_qty) as ReceivedQty 
	from cylinder
	where ledger_number='".$ledger_number."'";

	$queryResSum =  mysql_query($querySum,$connection) or die(mysql_error());
	$cylinder_qty= mysql_fetch_array($queryResSum);
	extract($cylinder_qty); 
	// end getting total received cylinders




	// getting total received cylinders
	$querySumFill = "select SUM(cylinderfill_qty) as sentqty 
	from cylinderfill
	where ledger_number='".$ledger_number."'";

	$queryResSumFill =  mysql_query($querySumFill,$connection) or die(mysql_error());
	$cylinderfill_qty= mysql_fetch_array($queryResSumFill);
	extract($cylinderfill_qty); 
	// die($sentqty);
	// end getting total received cylinders

	$sentqty = $sentqty + $cylinderfill_qty2;

	$cylinderfill_qty = $ReceivedQty - $sentqty;

	$queryins="Insert into cylinderfill (Ledger_number,cylinderfill_type,cylinderfill_number,cylinderfill_qty,cylinderfill_qty2,cylinderfill_size,cylinderfill_transactiondate,cylinderfill_deliverydate,cylinderfill_todate) 
			values ('$ledger_number','".$cylinderfill_type."',$cylinderfill_number,$cylinderfill_qty,$cylinderfill_qty2,$cylinderfill_size,".formatdate($cylinderfill_transactiondate).", ".formatdate($cylinderfill_deliverydate).", ".formatdate($cylinderfill_todate).")";
		// echo $queryins;exit;
	$result  = mysql_query($queryins, $connection);
	
    
	if(mysql_error()){
		$message = mysql_error();
	}else{
		$lastinsertedid= mysql_insert_id($connection);
		$message = "inserted Successfully";
	}
    

}

if($mode=='edit'){ 


	// //getting total received cylinders
	// $querySum = "select SUM(cylinder_qty) as ReceivedQty 
	// from cylinder
	// where ledger_number='".$ledger_number."'";

	// $queryResSum =  mysql_query($querySum,$connection) or die(mysql_error());
	// $cylinder_qty= mysql_fetch_array($queryResSum);
	// extract($cylinder_qty); 
	// // end getting total received cylinders




	// // getting total received cylinders
	// $CurrquerySumFill = "select cylinderfill_qty2  as currentQty
	// from cylinderfill
	// where cylinderfill_id = $cylinderfill_id";

	// $CurrqueryResSumFill =  mysql_query($CurrquerySumFill,$connection) or die(mysql_error());
	// $Currcylinderfill_qty= mysql_fetch_array($CurrqueryResSumFill);
	// extract($Currcylinderfill_qty); 
	// die($currentQty);
	// // end getting total received cylinders



	// // getting total received cylinders
	// $querySumFill = "select SUM(cylinderfill_qty) as sentqty 
	// from cylinderfill
	// where ledger_number='".$ledger_number."'";

	// $queryResSumFill =  mysql_query($querySumFill,$connection) or die(mysql_error());
	// $cylinderfill_qty= mysql_fetch_array($queryResSumFill);
	// extract($cylinderfill_qty); 
	// // die($sentqty);
	// // end getting total received cylinders

	// $sentqty = $sentqty = $currentQty;
	// $sentqty = $sentqty + $cylinderfill_qty2;

	// $cylinderfill_qty = $ReceivedQty - $sentqty;



	$query_update="update  cylinderfill set Ledger_number='$ledger_number',
					cylinderfill_type='".$cylinderfill_type."',
					cylinderfill_number= $cylinderfill_number, 
					cylinderfill_qty2 = $cylinderfill_qty2,
					cylinderfill_size= $cylinderfill_size,
                    cylinderfill_transactiondate = ".formatdate($cylinderfill_transactiondate).", 
					cylinderfill_deliverydate = ".formatdate($cylinderfill_deliverydate).",
					cylinderfill_todate = ".formatdate($cylinderfill_todate)."
                    where cylinderfill_id=$cylinderfill_id";
            
	//echo $query_update;exit;
    $resultedit = mysql_query($query_update, $connection);
	 
	
	if(mysql_error()){
		$message = mysql_error();
	}else{ 
		$message = "Updated Successfully";
	}
}

if($mode=='delete')
{		
	$query="delete from cylinderfill where cylinderfill_id='".$cylinderfill_id."'";
   	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	if(mysql_error()){
		$message = mysql_error();
	}else{
		$lastinsertedid= mysql_insert_id($connection);
		$message = "Cylinder Number ($cylinderfill_id) Deleted Successfully";
	}

}


if($mode=="insert"){
    header("location: ./form_cylinder_fill.php?mode=edit&cylinderfill_id=$lastinsertedid&message=$message");
}
elseif ($mode=="edit"){
	header("location: ./form_cylinder_fill.php?mode=insert&message=$message");
}
elseif ($mode=="delete"){
	header("location: ./form_cylinder_fill.php?mode=insert&message=$message");
}


function formatdate($dateval){
	if($dateval != ''){
		$ndate = explode("/", $dateval);
		if(count($ndate) > 1){
			$dateval = $ndate[2]."-".$ndate[1]."-".$ndate[0];
			$dateval = "'".$dateval."'";
			 
		}else{
			$ndate = explode("-", $dateval);
			$dateval = $ndate[2]."-".$ndate[1]."-".$ndate[0];
			$dateval = "'".$dateval."'";
		}
	}
		return $dateval;
}


?>
