<?php 
include 'session.php';

extract($_REQUEST);
$act=$action;
if($chkstatus !=null)
	$status =1;
else
	$status =0;

switch($act)
{
	case 'insert':
	
	if(!empty($product_name) ) {
		$strChk = "select count(product_id) from m_product where product_name = '".$product_name."'  and IsActive != '2' ";		
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {			

			$str="INSERT into m_product(product_name, IsActive, createddate)
			values('".$product_name."',  '".$status."', NOW() )";
			
			$rslt = $db->insert($str);
			
			$log = $db->insert_log("insert","m_product","","Product Added Newly","Product",$str);
					
			
			echo json_encode(array("rslt"=>"1")); //success
		}
		else {
			 echo json_encode(array("rslt"=>"3")); //same exists
		}
	}
	else {
		echo json_encode(array("rslt"=>"4"));  //no values
	}
	
	break;
	
	
	case 'update':	 	
	//$edit_id		
	if(!empty($product_name) ) {
		$strChk = "select count(product_id) from m_product where product_name = '".$product_name."'  and IsActive != '2' AND product_id !='".$edit_id."' ";        	
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {
			$str = "update m_product set product_name= '".$product_name."' , IsActive = '".$status."' where product_id = '".$edit_id."'  ";
							
				
			$db->insert($str);
			$db->insert_log("update","m_product",$edit_id,"Product Details Update","Product",$str);

			echo json_encode(array("rslt"=>"2"));
		}
		else {
			echo json_encode(array("rslt"=>"3")); //same exists
		}
	}
	else {
		echo json_encode(array("rslt"=>"4"));  //no values
	}
		
	break;
	
	case 'del':
	  $edit_id = base64_decode($Id);
	  
	  $chk_ref = check_reference($db, $edit_id);
	  
	  if($chk_ref == 1){
		   $str="update m_product set IsActive = '2'  where product_id = '".$edit_id."'";
		  $db->insert($str); 	 
		  
		//   $db->insert_log("delete","m_product",$edit_id,"Product deleted","product",$str);
		  echo json_encode(array("rslt"=>"5")); //deletion
	  }
	  else{
		 echo json_encode(array("rslt"=>"-1", "err_msg"=>"Reference exists with other data. Cannot Perform the action.")); //deletion  
	  }	 		
	break;
	
	

	case 'changestatus':
		$edit_id = base64_decode($Id);
		$status = $actval;
	  
		$chk_ref = check_reference($db, $edit_id);
	  
		if($chk_ref == 1){
			$str="update m_product set IsActive = '".$status."'  where product_id = '".$edit_id."'";
			$db->insert($str); 	 
		  
			// $db->insert_log("changestatus","m_product",$edit_id,"Product status changed","financial",$str);
			echo json_encode(array("rslt"=>"6")); //status update
		}
		else{
			echo json_encode(array("rslt"=>"-1", "err_msg"=>"Reference exists with other data. Cannot Perform the action.")); //deletion  
		}		
	break;
	
	
}


function check_reference($db, $financial_id){
	return 1;
}


?>