<?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);
    // echo "aaaaaa";
}
if($_GET){
    extract($_GET); 
    // var_dump($_GET);
    // echo "bbbbbb";
}






if($mode=='insert'){
		
	$queryins="Insert into cylinder (Ledger_number,cylinder_type,cylinder_name,cylinder_qty,cylinder_size,cylinder_transactiondate,cylinder_receiptiondate,cylinder_belong) 
			values ('$ledger_number','".$cylinder_type."','".$cylinder_name."',$cylinder_qty,".$cylinder_size.", ".formatdate($cylinder_transactiondate).", ".formatdate($cylinder_receiptiondate).", '".$cylinder_belong."')"; 
			//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'){
	
	$query_update="update  cylinder set Ledger_number='".$ledger_number."',
					cylinder_type='".$cylinder_type."',
					cylinder_name= '".$cylinder_name."',
					cylinder_qty= $cylinder_qty, 
					cylinder_size = '".$cylinder_size."',
                    cylinder_transactiondate = ".formatdate($cylinder_transactiondate).",
					cylinder_receiptiondate = ".formatdate($cylinder_receiptiondate).",
					cylinder_belong = '".$cylinder_belong."'
                    where cylinder_id=$cylinder_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 cylinder where cylinder_id='".$cylinder_id."'";
   	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	if(mysql_error()){
		$message = mysql_error();
	}else{
		$lastinsertedid= mysql_insert_id($connection);
		$message = "Cylinder Number ($cylinder_id) Deleted Successfully";
	}

}
 


if($mode=="insert"){
    header("location: ./Form_cylinder.php?mode=edit&cylinder_id=$lastinsertedid&message=$message");
}
elseif ($mode=="edit"){
	header("location: ./Form_cylinder.php?mode=insert&message=$message");
}
elseif ($mode=="delete"){
	header("location: ./Form_cylinder.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;
}


?>
