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

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


switch($act)
{
	case 'insert':
	
	if(!empty($txt_suppliername)) {
		$sv_gst_no='';
		if($sel_sup_type == "2"){
			$sv_gst_no=$txt_gst;
		}
		
		
		/*$strChk = "select count(sup_id) from m_supplier where sup_name = '".$txt_suppliername."' and IsActive != '2' ";		
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {	*/
			
			$sup_logo_path='';			
			if(isset($_FILES["supplier_logo"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["supplier_logo"]['tmp_name']);
				$file_mime = explode('/',$file_info['mime']);				
				if(!in_array($file_mime[1],array('jpg','jpeg','gif','png','bmp') ) ){
					echo json_encode(array("rslt"=>"7"));
					exit();
				}
				//image upload path - starts			
						
				$sup_logo_path=$obj->genThumbSupplier_logo('supplier_logo');							
				//image upload path - ends	
			}	
			
			$sup_img_path='';			
			if(isset($_FILES["supplier_img"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["supplier_img"]['tmp_name']);
				$file_mime = explode('/',$file_info['mime']);				
				if(!in_array($file_mime[1],array('jpg','jpeg','gif','png','bmp') ) ){
					echo json_encode(array("rslt"=>"7"));
					exit();
				}	
				$sup_img_path=$obj->genThumbSupplier_image('supplier_img');	
			}	
			
			$str="insert into m_supplier(code, sup_type, sup_payment_type, sup_product,sup_name, sup_address, sup_country, sup_state, sup_city, sup_postcode, sup_landline, sup_mobile, sup_email, sup_website_url, sup_image, sup_gstno, sup_company_logo, IsActive, UserId, createddate)values('".$txt_code."','".$sel_sup_type."', '".$rbtn_sup_pay_type."','".$supplier_type."', '".getRealescape($txt_suppliername)."', '".getRealescape($txt_address)."', '".getRealescape($txt_country)."', '".getRealescape($txt_state)."', '".getRealescape($txt_city)."', '".$txt_postcode."', '".$txt_landline."', '".$txt_mobile."', '".getRealescape($txt_email)."', '".getRealescape($txt_websiteurl)."', '".$sup_img_path."', '".$sv_gst_no."', '".$sup_logo_path."', '1', '".$_SESSION["UserId"]."',  NOW() )";
			
			$rslt = $db->insert($str);

			
			$log = $db->insert_log("insert","m_supplier","","Supplier Added Newly","supplier",$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($txt_suppliername)) {
		$sv_gst_no='';
		if($sel_sup_type == "2"){
			$sv_gst_no=$txt_gst;
		}
		
		/*$strChk = "select count(sup_id) from m_supplier where supplier_name = '".$txt_suppliername."' and IsActive != '2' and sup_id != '".$edit_id."' ";
 		$reslt = $db->get_a_line($strChk);
		
		if($reslt[0] == 0) {*/
			$str = "update m_supplier set  sup_type='".$sel_sup_type."',sup_product ='".$supplier_type."', sup_payment_type='".$rbtn_sup_pay_type."', sup_name='".getRealescape($txt_suppliername)."', sup_address='".getRealescape($txt_address)."', sup_country='".getRealescape($txt_country)."', sup_state='".getRealescape($txt_state)."', sup_city='".getRealescape($txt_city)."', sup_postcode='".$txt_postcode."', sup_landline='".$txt_landline."', sup_mobile='".$txt_mobile."', sup_email='".getRealescape($txt_email)."', sup_website_url='".getRealescape($txt_websiteurl)."', sup_gstno='".$sv_gst_no."',       ";
						
			
			if(isset($_FILES["supplier_logo"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["supplier_logo"]['tmp_name']);
				$file_mime = explode('/',$file_info['mime']);				
				if(!in_array($file_mime[1],array('jpg','jpeg','gif','png','bmp') ) ){
					echo json_encode(array("rslt"=>"7"));
					exit();
				}
				//image upload path - starts			
						
				$sup_logo_path=$obj->genThumbSupplier_logo('supplier_logo');							
				//image upload path - ends	
				$str .= " sup_company_logo='".$sup_logo_path."' , ";	
			}	
						
			if(isset($_FILES["supplier_img"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["supplier_img"]['tmp_name']);
				$file_mime = explode('/',$file_info['mime']);				
				if(!in_array($file_mime[1],array('jpg','jpeg','gif','png','bmp') ) ){
					echo json_encode(array("rslt"=>"7"));
					exit();
				}					
				$sup_img_path=$obj->genThumbSupplier_image('supplier_img');	
				$str .= " sup_image='".$sup_img_path."' , ";
			}						
			
			$str .= " UserId='".$_SESSION["UserId"]."'  where sup_id = '".$edit_id."' ";			
			$db->insert($str);
			$db->insert_log("update","m_supplier",$edit_id,"Supplier Details Update","supplier",$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_supplier set IsActive = '2', UserId='".$_SESSION["UserId"]."'  where sup_id = '".$edit_id."'";
		  $db->insert($str); 	 
		  
		  $db->insert_log("delete","m_supplier",$edit_id,"Supplier deleted","Supplier",$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_supplier set IsActive = '".$status."', UserId='".$_SESSION["UserId"]."'  where sup_id = '".$edit_id."'";
			$db->insert($str); 	 
		  
			$db->insert_log("changestatus","m_supplier",$edit_id,"Supplier status changed","Supplier",$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, $supplier_id){
	return 1;
}


?>