<?php
   
  require_once("../config.php");
	require_once("../startup.php");

	 //echo"<pre>";
	// print_r($_POST);
	// die();


  $obj_proforma						= new Invoice_frproforma();
	$obj_invoice						= new Invoice();
	//$obj									= new AllTables();
	 

		$obj1 = new dbopenconn();
	$con = $obj1-> dbconnect();
$memberinvoicesid 			= intval($_POST['memberinvoicesid']);
//echo $memberinvoicesid;

$fieldnames .=  ($fieldnames=="(")?"$k":",$k";
					$fieldvalues .= ($fieldvalues=="(")?"'$v'":",'$v'";
$action    			= $_POST['opt'];
$memberid			= intval($_POST['memberid']);
$billing_membercompanyid			= intval($_POST['billing_membercompanyid']);
$companyid			= intval($_POST['companyid']);
	$wherecondition = "memberinvoicesid = $memberinvoicesid";
	$wherecondition1 = "and memberid = $memberid and billed_membercompanyid = $companyid and billing_membercompanyid= $billing_membercompanyid";
	//$wherecondition2 = "and billed_membercompanyid = $billed_membercompanyid";
	$sqltext ="insert into trninvoicetomember (`memberid`,`billing_membercompanyid`,`billed_membername`,`billed_membercompanyid`,`memberofficeid`,`quotes_proforma_invoice`,`inv_GST_no`,`inv_Add1`,`inv_Add2`,`inv_Add3`,`inv_Add4`,`inv_CityName`,`inv_Pin`,`inv_State`,`inv_StateName`,`inv_head_Stateid`,`inv_Country`,`apl_id`,`status_invoice`,`igst_percentage`,`igst_amount`,`cgst_percentage`,`cgst_amount`,`sgst_percentage`,`sgst_amount`,`ugst_percentage`,`ugst_amount`,`discount_percentage`,`discount_flat`,`discount_amount`,`InvoiceBaseAmountBeforeDiscount`,`InvoiceBaseAmount`,`InvoiceTotalAmount`,`paid_total_amount`,`InvoiceSubTotalAmount`) 
        SELECT  memberid,billing_membercompanyid,billed_membername,billed_membercompanyid,memberofficeid,quotes_proforma_invoice,inv_GST_no,inv_Add1,inv_Add2,inv_Add3,inv_Add4,inv_CityName,inv_Pin,inv_State,inv_StateName,inv_head_Stateid,inv_Country,apl_id,status_invoice,igst_percentage,igst_amount,cgst_percentage,cgst_amount,sgst_percentage,sgst_amount,ugst_percentage,ugst_amount,discount_percentage,discount_flat,discount_amount,InvoiceBaseAmountBeforeDiscount,InvoiceBaseAmount,InvoiceTotalAmount,paid_total_amount,InvoiceSubTotalAmount FROM trninvoicetomember where $wherecondition $wherecondition1";


	$applications = $obj1->insert_query($sqltext);

	
	
	$sqltext1 ="insert into trninvoicetomemberdetails (memberinvoicesid,billed_membercompanyid,panelid,categoryid,hsn_code,membershippackageid,itemno,productname,product_description,price,quantity,per,ProductBaseAmount,ProductTotalAmount) 
    SELECT (select memberinvoicesid from trninvoicetomember where billed_membercompanyid = $companyid order by memberinvoicesid desc limit 0,1)as memberinvoicesid,billed_membercompanyid,panelid,categoryid,hsn_code,membershippackageid,itemno,productname,product_description,price,quantity,per,ProductBaseAmount,ProductTotalAmount FROM trninvoicetomemberdetails where billed_membercompanyid = $companyid and memberinvoicesid = $memberinvoicesid";
//echo $sqltext;


	//echo $sqltext1;
	//	die();	
	
$applications1 = $obj1->insert_query($sqltext1);

$sql ="select * from trninvoicetomember 
				 where billed_membercompanyid = $companyid 
				 order by memberinvoicesid desc limit 0,1";
//echo $sql;
//die();
		$get_invoicelist = $obj_invoice -> getInvoice('',$sql,'','','');

		$memberinvoicesidd	=	$get_invoicelist[0]['memberinvoicesid'];
		
			$companyiddd	=	$get_invoicelist[0]['billed_membercompanyid'];
			$billing_membercompanyiddd	=	$get_invoicelist[0]['billing_membercompanyid'];
			//$memberinvoicesidd	=	$get_invoicelist[0]['memberinvoicesid'];*/
		
			if($applications)
			{
				//echo "success||".$memberinvoicesidd;
				echo"<script> alert('Invoice is completed');setTimeout(function() { window.location = 'edit_invoice.php?memberinvoicesid=$memberinvoicesidd&companyid=$companyiddd&billing_membercompanyid=$billing_membercompanyiddd'; } ); </script>";
				//	exit;*/
				
			}
			else 
			{
				echo "error||0";
			}
	/*}
	else 
	{
		echo "error||0";
	}*/
?>