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

	$objInvoiceMgmt = new InvoiceMgmt();
	$objInvoiceProductMgmt = new InvoiceProductMgmt();
	$objInvoiceTaxMgmt = new InvoiceTaxMgmt();

	$productid=json_decode(stripslashes($_REQUEST['productid']));
    $productqty=json_decode(stripslashes($_REQUEST['productqty']));
	$productrate=json_decode(stripslashes($_REQUEST['productrate']));
	
	$taxid=json_decode(stripslashes($_REQUEST['taxid']));
    $taxpercentade=json_decode(stripslashes($_REQUEST['taxpercentade']));
	    	
 	$objInvoiceMgmt ->invoice_id             = $_REQUEST["invoice_id"] ;
	$objInvoiceMgmt ->vendor_id             = $_REQUEST["vendor_id"] ;
	$objInvoiceMgmt ->invoice_no			= $_REQUEST['invoice_no'] ;
	$objInvoiceMgmt ->invoice_date			= $_REQUEST['invoice_date'] ;
	$objInvoiceMgmt ->invoice_paymenttype	= $_REQUEST['invoice_paymenttype'] ;

	if($objInvoiceMgmt ->invoice_id=="0" || $objInvoiceMgmt ->invoice_id==""){
		$objInvoiceMgmt ->add();	
		for($i=0;$i<count($productid);$i++){
					 
					    $objInvoiceProductMgmt->invoice_id		= $objInvoiceMgmt ->invoice_id;
						$objInvoiceProductMgmt->product_id		=$productid[$i];
						$objInvoiceProductMgmt->product_qty		=$productqty[$i];	
						$objInvoiceProductMgmt->product_rate	=$productrate[$i];	
						$objInvoiceProductMgmt->add();	
					}	
		for($i=0;$i<count($taxid);$i++){
		 
			$objInvoiceTaxMgmt->invoice_id		= $objInvoiceMgmt ->invoice_id;
			$objInvoiceTaxMgmt->tax_id			=$taxid[$i];
			$objInvoiceTaxMgmt->tax_percentage	=$taxpercentade[$i];	
			$objInvoiceTaxMgmt->add();	
		}					
	}
	else{
		$objInvoiceMgmt ->edit();	
		$objInvoiceProductMgmt->invoice_id = $_REQUEST["invoice_id"];
		$objInvoiceProductMgmt->delete();	
		for($i=0;$i<count($productid);$i++){
					 
					    $objInvoiceProductMgmt->invoice_id		= $objInvoiceMgmt ->invoice_id;
						$objInvoiceProductMgmt->product_id		=$productid[$i];
						$objInvoiceProductMgmt->product_qty		=$productqty[$i];	
						$objInvoiceProductMgmt->product_rate	=$productrate[$i];	
						$objInvoiceProductMgmt->add();	
					}	
		$objInvoiceTaxMgmt->invoice_id = $_REQUEST["invoice_id"];
		$objInvoiceTaxMgmt->delete();	
		for($i=0;$i<count($taxid);$i++){
		 
			$objInvoiceTaxMgmt->invoice_id		= $objInvoiceMgmt ->invoice_id;
			$objInvoiceTaxMgmt->tax_id			=$taxid[$i];
			$objInvoiceTaxMgmt->tax_percentage	=$taxpercentade[$i];	
			$objInvoiceTaxMgmt->add();	
		}											
	}
	
	
	
	echo '{"success":"true","msg":""}';
}
else {
	echo '{"success":"false","msg":"session expiry"}';
}	         
?>
