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

   $obj_proforma						= new Invoice_frproforma();
	$obj_quotation						= new Invoice_frquotation();
	$obj									= new AllTables();
	$memberinvoicesid 			= intval($_POST['memberinvoicesid']);
	$action    						= $_POST['opt'];
 	$companyid						= intval($_POST['companyid']);
 	$billed_membercompanyid		= intval($_POST['companyid']);
 	$billing_membercompanyid 	= $_POST['billing_membercompanyid'];
 	$obj_Invoice							 = new Invoice();

   
if($action == "save_as_proforma")
{
		
			$wherecondition = "memberinvoicesid = $memberinvoicesid ";
		   $query = "SELECT * FROM trninvoicetomember_frquotation where $wherecondition"	;
		 
			$querylist = $obj_quotation ->getInvoice('',$query);
			$quotation_count =count($querylist);
			$quotationarray['memberid']			= 	$querylist[0]['memberid'];;
			//$quotationarray['memberinvoicesid']			= $querylist[0]['memberinvoicesid'];	
			$quotationarray['billed_membercompanyid']				=$querylist[0]['billed_membercompanyid'];
			$quotationarray['billing_membercompanyid']			= $billing_membercompanyid;
			$quotationarray['billed_membername'] 	= sanitize($querylist[0]['billed_membername'],SQL);
			$quotationarray['inv_Add1'] 				= sanitize($querylist[0]['inv_Add1'],SQL);
			$quotationarray['inv_Add2'] 				= sanitize($querylist[0]['inv_Add2'],SQL);
			$quotationarray['inv_Add3'] 				= sanitize($querylist[0]['inv_Add3'],SQL);
			$quotationarray['inv_Add4'] 				= sanitize($querylist[0]['inv_Add4'],SQL);
			$quotationarray['quotes_proforma_invoice']	=	1;
			$quotationarray['inv_CityName'] 			= sanitize($querylist[0]['inv_CityName'],SQL);
			$quotationarray['inv_Pin'] 				= sanitize($querylist[0]['inv_Pin'],SQL);
			$quotationarray['inv_StateName'] 		= sanitize($querylist[0]['inv_StateName'],SQL);
			$quotationarray['inv_head_Stateid'] 	= sanitize($querylist[0]['inv_head_Stateid'],SQL);
			$quotationarray['invoiceno'] 				= sanitize($querylist[0]['invoiceno'],SQL);
			$quotationarray['inv_GST_no'] 			= sanitize($querylist[0]['inv_GST_no'],SQL);
			$quotationarray['invoicedate'] 			= sanitize($querylist[0]['invoicedate'],SQL);
			$quotationarray['duedate'] 				= sanitize( $querylist[0]['duedate'],SQL);
			$quotationarray['status_invoice'] 		= sanitize($querylist[0]['status_invoice'],SQL);
			$quotationarray['igst_percentage']		= sanitize($querylist[0]['igst_percentage'],SQL);
			$quotationarray['igst_amount']			= sanitize($querylist[0]['igst_amount'],SQL);
			$quotationarray['cgst_percentage']		= sanitize($querylist[0]['cgst_percentage'],SQL);
			$quotationarray['cgst_amount']			= sanitize($querylist[0]['cgst_amount'],SQL);
			$quotationarray['sgst_percentage']		= sanitize($querylist[0]['sgst_percentage'],SQL);
			$quotationarray['sgst_amount']			= sanitize($querylist[0]['sgst_amount'],SQL);
			$quotationarray['ugst_percentage']		= sanitize($querylist[0]['ugst_percentage'],SQL);
			$quotationarray['ugst_amount']			= sanitize($querylist[0]['ugst_amount'],SQL);
			$quotationarray['discount_percentage']	= sanitize($querylist[0]['discount_percentage'],SQL);
			$quotationarray['discount_amount']		= sanitize($querylist[0]['discount_amount'],SQL);
			$quotationarray['InvoiceBaseAmountBeforeDiscount']			= $querylist[0]['InvoiceBaseAmountBeforeDiscount'];
			$quotationarray['InvoiceBaseAmount']							= $querylist[0]['InvoiceBaseAmount'];
			$quotationarray['InvoiceTotalAmount']							= $querylist[0]['InvoiceTotalAmount'];
			$quotationarray['InvoiceSubTotalAmount']							= $querylist[0]['InvoiceSubTotalAmount'];
			//print_r($quotationarray);
			 $add_invoice = $obj_proforma->AddInvoice($quotationarray);
			
			
			$querynew="SELECT memberinvoicesid FROM `trninvoicetomember_frproforma`  WHERE billing_membercompanyid=$billing_membercompanyid and
				 billed_membercompanyid=$billed_membercompanyid	
 				ORDER BY `memberinvoicesid` DESC limit 0,1";
 			
 			$querynewlist = $obj_proforma ->getInvoice('',$querynew);
 			 $memberinvoicesid_new			= 	$querynewlist[0]['memberinvoicesid'];
 			 

  			$query_proforma_detail ="SELECT * FROM trninvoicetomemberdetails_frquotation where memberinvoicesid = $memberinvoicesid";

			$proforma_detail_list = $obj_quotation->getInvoiceDetails('',$query_proforma_detail);
			$proforma_detail_count = count($proforma_detail_list);
			//echo "<br><pre>";
			//print_r($proforma_detail_list);
			//die();
			for($i=0;$i<$proforma_detail_count;$i++)
			{
				  $proformaarray['memberinvoicesid']			= 	$memberinvoicesid_new;
				   //$proformaarray['memberid']						= 	$proforma_detail_list[$i]['memberid'];
					$proformaarray['categoryid']					= $proforma_detail_list[$i]['categoryid'];
					$proformaarray['billed_membercompanyid']	= $proforma_detail_list[$i]['billed_membercompanyid'];
					//$proformaarray['billed_membername'] 		= sanitize($proforma_detail_list[$i]['billed_membername'],SQL);
					$proformaarray['apl_id'] 						= sanitize($proforma_detail_list[$i]['apl_id'],SQL);
					$proformaarray['hsn_code'] 						= sanitize($proforma_detail_list[$i]['hsn_code'],SQL);
				
					$proformaarray['membershippackageid'] 		= sanitize($proforma_detail_list[$i]['membershippackageid'],SQL);
					$proformaarray['itemno'] 						= sanitize($proforma_detail_list[$i]['itemno'],SQL);
					$proformaarray['productname'] 				= sanitize($proforma_detail_list[$i]['productname'],SQL);
					$proformaarray['product_description'] 				= sanitize($proforma_detail_list[$i]['product_description'],SQL);

					$proformaarray['price'] 						= sanitize($proforma_detail_list[$i]['price'],SQL);
					$proformaarray['quantity'] 					= sanitize($proforma_detail_list[$i]['quantity'],SQL);
					$proformaarray['igst_percentage_product'] = sanitize($proforma_detail_list[$i]['igst_percentage_product'],SQL);
					$proformaarray['igst_amount_product'] 				= sanitize($proforma_detail_list[$i]['igst_amount_product'],SQL);
					$proformaarray['sgst_percentage_product'] 			= sanitize($proforma_detail_list[$i]['sgst_percentage_product'],SQL);
					$proformaarray['sgst_amount_product'] 				=sanitize($proforma_detail_list[$i]['sgst_amount_product'],SQL);
					$proformaarray['cgst_percentage_product'] 					= sanitize($proforma_detail_list[$i]['cgst_percentage_product'],SQL);
					$proformaarray['cgst_amount_product']		= sanitize($proforma_detail_list[$i]['cgst_amount_product'],SQL);
					$proformaarray['ugst_percentage_product']			= sanitize($proforma_detail_list[$i]['ugst_percentage_product'],SQL);
					$proformaarray['ugst_amount_product']		= sanitize($proforma_detail_list[$i]['ugst_amount_product'],SQL);
					$proformaarray['ProductBaseAmount']			= sanitize($proforma_detail_list[$i]['ProductBaseAmount'],SQL);
					$proformaarray['ProductTotalAmount']		= sanitize($proforma_detail_list[$i]['ProductTotalAmount'],SQL);
					$proformaarray['isAdvance']			= sanitize($proforma_detail_list[$i]['isAdvance'],SQL);
					$proformaarray['year']		= sanitize($proforma_detail_list[$i]['year'],SQL);
					
					//echo "<pre>";
					//print_r($proformaarray);
	
 			
			//$wherecondition_proforma_detail = "memberinvoicesid = $memberinvoicesid_new ";
					$add_proforma_detail = $obj_proforma -> AddInvoiceDetails($proformaarray);
				
					//$add_proforma_detail = $obj->AddInfo($proformaarray,'trninvoicetomemberdetails_frproforma');
			}
			
			$sql ="select * from trninvoicetomember_frproforma 
				 where billed_membercompanyid = $companyid 
				 order by memberinvoicesid desc limit 0,1";
//echo $sql;
//die();
		$get_invoicelist = $obj_proforma -> 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'];*/
 
 

			$data['save_as_proforma']	= 1;
			$wherecondition1 = " memberinvoicesid = $memberinvoicesid ";
			$updtae_quotation = $obj_quotation->EditInvoice($data,$wherecondition1);
	
			if($updtae_quotation == true)
			{
				echo 'add||';
				echo"edit_proforma.php?memberinvoicesid=$memberinvoicesidd&companyid=$companyiddd&billing_membercompanyid=$billing_membercompanyiddd";
			}
			else 
			{
				echo 'not add';
			}
	}
	else 
	{
		echo false;
	}
?>