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

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


switch($act)
{
	case 'insert':
	
	if(!empty($txt_customername)) {
		$sv_gst_no='';
		if($sel_cus_type == "2"){
			$sv_gst_no=$txt_gst;
		}
		
		/*$strChk = "select count(cus_id) from m_customer where cus_name = '".$txt_customername."' and IsActive != '2' ";		
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {	*/
			
			$cus_logo_path='';			
			if(isset($_FILES["customer_logo"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["customer_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			
						
				$cus_logo_path=$obj->genThumbCustomer_logo('customer_logo');							
				//image upload path - ends	
			}	
			
			$cus_img_path='';			
			if(isset($_FILES["customer_img"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["customer_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();
				}	
				$cus_img_path=$obj->genThumbCustomer_image('customer_img');	
			}	
			

			$str="insert into m_customer(code, cus_type, cus_name, cus_address, cus_country, cus_state, cus_city, cus_postcode, cus_landline, cus_mobile, cus_email, cus_website_url, cus_image, cus_gstno, cus_company_logo, IsActive, UserId, createddate)values('".$txt_code."','".$sel_cus_type."', '".getRealescape($txt_customername)."', '".getRealescape($txt_address)."', '".getRealescape($txt_country)."', '".getRealescape($txt_state)."', '".getRealescape($txt_city)."', '".$txt_postcode."', '".$txt_landline."', '".$txt_mobile."', '".getRealescape($txt_email)."', '".getRealescape($txt_websiteurl)."', '".$cus_img_path."', '".$sv_gst_no."', '".$cus_logo_path."', '1', '".$_SESSION["UserId"]."',  NOW() )";
			
			$rslt = $db->insert($str);
			
			$log = $db->insert_log("insert","m_customer","","Customer Added Newly","customer",$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_customername)) {
		$sv_gst_no='';
		if($sel_cus_type == "2"){
			$sv_gst_no=$txt_gst;
		}
		
		$strChk = "select count(cus_id) from m_customer where customer_name = '".$txt_customername."' and IsActive != '2' and cus_id != '".$edit_id."' ";
 		$reslt = $db->get_a_line($strChk);
		
		//if($reslt[0] == 0) {
			$str = "update m_customer set  cus_type='".$sel_cus_type."', cus_name='".getRealescape($txt_customername)."', cus_address='".getRealescape($txt_address)."', cus_country='".getRealescape($txt_country)."', cus_state='".getRealescape($txt_state)."', cus_city='".getRealescape($txt_city)."', cus_postcode='".$txt_postcode."', cus_landline='".$txt_landline."', cus_mobile='".$txt_mobile."', cus_email='".getRealescape($txt_email)."', cus_website_url='".getRealescape($txt_websiteurl)."', cus_gstno='".$sv_gst_no."',       ";
						
			
			if(isset($_FILES["customer_logo"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["customer_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			
						
				$cus_logo_path=$obj->genThumbCustomer_logo('customer_logo');							
				//image upload path - ends	
				$str .= " cus_company_logo='".$cus_logo_path."' , ";	
			}	
						
			if(isset($_FILES["customer_img"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["customer_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();
				}	
				$cus_img_path=$obj->genThumbCustomer_image('customer_img');	
				$str .= " cus_image='".$cus_img_path."' , ";
			}
			
			
			$str .= " UserId='".$_SESSION["UserId"]."'  where cus_id = '".$edit_id."' ";			
			$db->insert($str);
			$db->insert_log("update","m_customer",$edit_id,"Customer Details Update","customer",$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_customer set IsActive = '2', UserId='".$_SESSION["UserId"]."'  where cus_id = '".$edit_id."'";
		  $db->insert($str); 	 
		  
		  $db->insert_log("delete","m_customer",$edit_id,"Customer deleted","Customer",$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_customer set IsActive = '".$status."', UserId='".$_SESSION["UserId"]."'  where cus_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, $customer_id){
	return 1;
}


?>