<?php
require '../Conexion/class.sql.inc.php';
$db = new EyeMySQLAdap();

session_start();
try{
	
	$Opcion				= $_POST['Opcion'];
	$userid  			= $_SESSION['UserID'];
	
	$lastCode=-1;
	switch ($Opcion) 
	{
		case 'GRABAR':{
				
				$ConceptID		= $_POST['ConceptID'];			
				$CategoryID		= $_POST['CategoryID'];
				$UnitID			= $_POST['UnitID'];
				$Description	= mysql_real_escape_string($_POST['Description']);
				$Cost			= ($_POST['Cost']!=''?$_POST['Cost']:0);
				$Status			= $_POST['Status'];
				$Source			= $_POST['Source'];
				
				if($ConceptID == '-1'){
					$array = array("CategoryID"=>$CategoryID,
						"UnitID"=>$UnitID,
						"Description"=>"'$Description'",
						"Cost"=>"$Cost",
						"Source"=>"'$Source'",
						"Status"=>($Status=="true"?"1":"0"),
						"CreateUserID"=>$userid,
						"CreateDate"=>"NOW()",
						"UpdateUserID"=>$userid,
						"UpdateDate"=>"NOW()");
					
					$result=$db->insert($array,"concept");
					$lastCode= mysql_insert_id();
				}
				else{
					$array = array("CategoryID"=>$CategoryID,
						"UnitID"=>$UnitID,
						"Description"=>"'$Description'",
						"Cost"=>"$Cost",
						"Source"=>"'$Source'",
						"Status"=>($Status=="true"?"1":"0"),						
						"UpdateUserID"=>$userid,
						"UpdateDate"=>"NOW()");
					
					$result=$db->update($array,"concept","ConceptID = " . $ConceptID);
					$lastCode = $ConceptID;
					}	
			}
			break;		
		case 'DELETECONCEPT':
			$ConceptID = $_POST['ConceptID'];
			$result=$db->delete("concept","ConceptID=".$ConceptID);
		break;		
	}
	if($result==false){
		//Echo '({success:true})';
		Echo '({success:false,Datos:'.json_encode("NOK").',LastCode:' .$lastCode . '})';	
	}
	else{
		//Echo '({success:true})';
		Echo '({success:true,Datos:'.json_encode("OK").',LastCode:' . $lastCode . '})';	
	}
}
catch(Exception $e)
{
	Echo '({success:false,Datos:'.json_encode($e->getMessage()).'})';		
}
?>



