<?php 
include 'session.php';
include 'includes/image_thumb.php';

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


switch($act)
{
	case 'insert':
	
	if(!empty($txt_companyname)) {
		$strChk = "select count(company_id) from m_company where company_name = '".$txt_companyname."' and IsActive != '2' ";		
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {	
			$path='';
			if(isset($_FILES["company_logo"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["company_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			
				$obj=new Gthumb();			
				$path=$obj->genThumbCompanyLogo('company_logo');							
				//image upload path - ends	
			}					

			$str="insert into m_company(company_name, proprietor_name, c_address, c_country, c_state, c_city, c_postcode, c_landline, c_mobile, gst_no, c_logo, c_email, c_website_url, IsActive, UserId, createddate)values('".getRealescape($txt_companyname)."', '".getRealescape($txt_proprietor)."', '".getRealescape($txt_address)."', '".getRealescape($txt_country)."', '".getRealescape($txt_state)."', '".getRealescape($txt_city)."', '".$txt_postcode."', '".$txt_landline."', '".$txt_mobile."', '".$txt_gst."', '".$path."', '".getRealescape($txt_email)."', '".getRealescape($txt_websiteurl)."', '1', '".$_SESSION["UserId"]."',  NOW() )";
			
			$rslt = $db->insert($str);
			
			$log = $db->insert_log("insert","m_company","","Company Added Newly","company",$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_companyname)) {
		$strChk = "select count(company_id) from m_company where company_name = '".$txt_companyname."' and IsActive != '2' and company_id != '".$edit_id."' ";
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {
			$str = "update m_company set company_name= '".getRealescape($txt_companyname)."', proprietor_name= '".getRealescape($txt_proprietor)."', c_address= '".getRealescape($txt_address)."', c_country= '".getRealescape($txt_country)."', c_state= '".getRealescape($txt_state)."', c_city= '".getRealescape($txt_city)."', c_postcode= '".$txt_postcode."', c_landline= '".$txt_landline."', c_mobile= '".$txt_mobile."', gst_no= '".$txt_gst."', c_email = '".getRealescape($txt_email)."', c_website_url='".getRealescape($txt_websiteurl)."',  ";
			
			if(isset($_FILES["company_logo"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["company_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			
				$obj=new Gthumb();			
				$path=$obj->genThumbCompanyLogo('company_logo');	
				//image upload path - ends
				$str .= " c_logo='".$path."' , ";				
			}
			
			$str .= " UserId='".$_SESSION["UserId"]."'  where company_id = '".$edit_id."' ";			
			$db->insert($str);
			$db->insert_log("update","m_company",$edit_id,"User Company Details","Company",$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_company set IsActive = '2', UserId='".$_SESSION["UserId"]."'  where company_id = '".$edit_id."'";
		  $db->insert($str); 	 
		  
		  $db->insert_log("delete","m_company",$edit_id,"Company deleted","Company",$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_company set IsActive = '".$status."', UserId='".$_SESSION["UserId"]."'  where company_id = '".$edit_id."'";
			$db->insert($str); 	 
		  
			$db->insert_log("changestatus","m_company",$edit_id,"Company status changed","Company",$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, $company_id){
	return 1;
}


?>