<?php 
include 'session.php';
require_once('common/getname_functions.php');

include 'includes/image_thumb.php';
$obj=new Gthumb();	

extract($_REQUEST);
$act=$action;

$txt_inv_date_db = date('Y-m-d', strtotime($_POST['txt_inv_date']));

// echo $hdn_pdt_cnt;
// exit();


switch($act)
{
	case 'insert':
	
		$pdt_cnt = count($hdn_pdt_cnt);
		for($i=0; $i<$pdt_cnt; $i++){
			
			if(isset($txt_pdt_type[$i]) && !empty($txt_pdt_type[$i]))
			{	

				$str_insert_det="INSERT INTO `t_product_flow_details`( `p_date`, `sup_id`, `pdt_type`, `qty_grm`, `p_flow`, `financial_id`, `UserId`, `IsActive`, `createddate`) 
				values('".$txt_inv_date_db."','".$sel_supplier[$i]."', '".$txt_pdt_type[$i]."', '".$txt_pdt_grm[$i]."', '".$product_flow[$i]."', '".$_SESSION["Financial_id"]."', '".$_SESSION["UserId"]."','1',  NOW() )";
													
					$db->insert($str_insert_det);	
					
				}
				
			}		
	
		echo json_encode(array("rslt"=>"1")); //success
	
	break;
			
	
	
	case 'update':	 
		// $edit_id = base64_decode($Id);	
	// echo $edit_id	;
	// exit();	
	$str=" UPDATE `t_product_flow_details` SET  p_date = '".$txt_inv_date_db."', sup_id = '".$sel_supplier[0]."', pdt_type = '".$txt_pdt_type[0]."', qty_grm = '".$txt_pdt_grm[0]."', p_flow = '".$product_flow[0]."' WHERE p_id = '".$edit_id."'";
	$db->insert($str);

		
	echo json_encode(array("rslt"=>"2")); //update success
		
	break;
	
	case 'del':
		$edit_id = base64_decode($Id);
		
		
		$query1 = "DELETE FROM t_product_flow_details WHERE p_id = $edit_id";
		
	
		
	if ($db->insert($query1)  ) {
		echo json_encode(array("rslt" => "5", "msg" => "Product Flow Deleted Successfully"));
	} else {
		echo json_encode(array("rslt" => "-1", "err_msg" => "Error: " . $e->getMessage()));
	}

break;
	
case 'changestatus':
	$edit_id = base64_decode($Id);
	$status = $actval;
	
	$chk_ref = check_reference($db, $edit_id);

	if($chk_ref == 1){
		  $str="update t_product_flow_details set IsActive = '".$status."', UserId='".$_SESSION["UserId"]."'  where p_id = '".$edit_id."'";
		  $db->insert($str); 	 
		
		  // $db->insert_log("changestatus","m_customer",$edit_id,"Customer status changed","Customer",$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;
}


?>