<?php  
require "commonincludefiles.php";
if($_SESSION["cCompany_id"]!="" && isset($_SESSION["cCompany_id"]))
{

	$objProduct = new ProductMgmt();
	$objProductTaxMgmt = new ProductTaxMgmt();
	
	/*$taxid=json_decode(stripslashes($_REQUEST['taxid']));
    $taxmaount=json_decode(stripslashes($_REQUEST['taxamount']));*/
	
	$objProduct ->product_id              		= $_REQUEST["product_id"] ;
	$objProduct ->product_name					= $_REQUEST['product_name'] ;
	$objProduct ->product_measurement			= $_REQUEST['product_measurement'] ;
	$objProduct ->product_category				= $_REQUEST['product_category'] ;
	$objProduct ->product_type					= $_REQUEST['product_type'] ;
	$objProduct ->product_warranty				= $_REQUEST['product_warranty'] ;
	$objProduct ->product_warrantytype			= $_REQUEST['product_warrantytype'] ;
	$objProduct ->product_measurement			= $_REQUEST['product_measurement'] ;
	$objProduct ->product_reorder				= $_REQUEST['product_reorder'] ;
	$objProduct ->product_opnstock				= $_REQUEST['product_opnstock'] ;
	$objProduct ->product_description			= $_REQUEST['product_description'];
	$objProduct->company_id              		= $_SESSION["cCompany_id"] ;
	
	if($_REQUEST['product_active'] =="on"){
		$objProduct->product_active= 1;
	} else{
		$objProduct->product_active = 0;
	}
	
	if($objProduct ->product_id=="0" || $objProduct ->product_id==""){
		$objProduct ->add();	
		/*for($i=0;$i<count($taxid);$i++){
					 
					    $objProductTaxMgmt->product_id		= $objProduct ->product_id;
						$objProductTaxMgmt->tax_id			=$taxid[$i];
						$objProductTaxMgmt->tax_amount		=$taxmaount[$i];	
						$objProductTaxMgmt->add();	
					}	*/				
	}
	else{
		$objProduct ->edit();	
		$objProductTaxMgmt->product_id = $_REQUEST["product_id"];
		$objProductTaxMgmt->delete();	
		/*for($i=0;$i<count($taxid);$i++){
					 
					    $objProductTaxMgmt->product_id		= $objProduct ->product_id;
						$objProductTaxMgmt->tax_id			=$taxid[$i];
						$objProductTaxMgmt->tax_amount		=$taxmaount[$i];	
						$objProductTaxMgmt->add();	
					}		*/									
	}
	
	if ($objProduct->product_id!="0")
	{							
	
			$product_id = $objProduct->product_id;
			
			$FileName = $_REQUEST["product_photo"];
			
			if($FileName!="" && $FileName!="default.gif") {
							
			$Ext = substr($FileName, strrpos($FileName, ".")+1);
			$Ext = strtolower($Ext);
		
			$thumbimg = "product_" . $product_id . "." . $Ext;
			$thumbimg_path = $ProductImagePath . $thumbimg;																							
			
			if (file_exists($thumbimg_path)) {
				unlink($thumbimg_path);
			}
			
			copy($ProductImagePath.$FileName, $thumbimg_path);
			
			$file_path=realpath($thumbimg_path);
			chmod($file_path, 0777);			
			
			if (file_exists($ProductImagePath.$FileName)) {
				unlink($ProductImagePath.$FileName);
			}
			
			$objProduct->product_photo = $thumbimg;
			$objProduct->updatePhoto();			
		}
	}
	
	
	
	echo '{"success":"true","msg":""}';
}
else {
	echo '{"success":"false","msg":"session expiry"}';
}	         
?>
