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

$obj  							=  new Product();
$invoice_obj					=	new Invoice_frproforma();
$client_obj  					=	new Client();
$company_obj  					=	new Company();

 
if($_POST['SUBMIT'])
{ 
		$memberid 							= $_POST['memberid'];
		$billing_membercompanyid		= sanitize($_POST['billing_membercompanyid'],SQL);
		$billed_membercompanyid			= sanitize($_POST['billed_membercompanyid'],SQL);

	 
	 	$member_sql = "select billing_address from mstclients where clientcompanyid = $billed_membercompanyid ";
		$mem_info 	= $obj-> getProduct('',$member_sql);		
		$billing_address_val = $mem_info[0]['billing_address'];
	
		if($billing_address_val === '0') 
			{			
				$sql2 = "select companyname,Addline1 as Add1,Addline2 as Add2, head_CityName as CityName,head_Pin as Pin,head_Stateid1,head_StateidName as StateName,Country,gstno from mstclients where clientcompanyid =".$billed_membercompanyid;
			}
			else 
			{
				$billing_data = explode('-',$billing_address_val);
				$add_type = $billing_data[0];
				$add_id 	 = $billing_data[1];
				
					if($add_type == 'FACTORY') 
					{
						
							$sql2 = "select factoryaddressline1 as Add1,factoryaddressline2 as Add2,factoryaddressline3 as Add3,factoryaddressline4 as Add4,
										factorycity as CityName,factorypincode as Pin,factorystateid as head_Stateid,
										factorystate as StateName,factorygstno as GST_no 
										from client_factories where clientfactoryid = ".$add_id;
							
						
					}
					elseif($add_type == 'BRANCH') 
					{
							$sql2 = "select branchaddressline1 as Add1,branchaddressline2 as Add2,branchaddressline3 as Add3,branchaddressline4 as Add4,
										branchcity as CityName,branchpincode as Pin,branchstateid as head_Stateid,
										branchstate as StateName,branchgstno as GST_no
										from client_branches where clientbranchid = ".$add_id;				
									
						
					}				
				
			}
			
			$inv_add_data 	= $client_obj-> getClient('','',$sql2);		
//print_r($inv_add_data);
			$invoicearray['memberid'] 									= $memberid;
			$invoicearray['billing_membercompanyid'] 				= $billing_membercompanyid;
			$invoicearray['billed_membercompanyid'] 				= $billed_membercompanyid;
			$invoicearray['billed_membername'] 						= sanitize($inv_add_data[0]['companyname'],SQL);
			$invoicearray['inv_Add1'] 				= sanitize($inv_add_data[0]['Add1'],SQL);
			$invoicearray['inv_Add2'] 				= sanitize($inv_add_data[0]['Add2'],SQL);
			$invoicearray['inv_Add3'] 				= sanitize($inv_add_data[0]['Add3'],SQL);
			$invoicearray['inv_Add4'] 				= sanitize($inv_add_data[0]['Add4'],SQL);
			$invoicearray['inv_CityName'] 		= sanitize($inv_add_data[0]['CityName'],SQL);
			$invoicearray['inv_Pin'] 				= sanitize($inv_add_data[0]['Pin'],SQL);
			$invoicearray['inv_State'] 			= $inv_add_data[0]['State'];
			$invoicearray['inv_StateName'] 		= $inv_add_data[0]['StateName'];
			$invoicearray['inv_head_Stateid'] 	= $inv_add_data[0]['head_Stateid'];
			$invoicearray['inv_GST_no'] 			= $inv_add_data[0]['gstno'];
			$invoicearray['new_invoicesid'] 			= 0;
			 
	     //$invoicearray['tax'] 			= $inv_add_data[0]['gstno'];
	    // $invoicearray['inv_GST_no'] 			= $inv_add_data[0]['gstno'];
			

		
		$invoicearray['quotes_proforma_invoice']	=	1;
		$invoicearray['invoiceno'] 			= $_POST['txtinv_no'];
		$invoicearray['invoicedate'] 			= date("Y-m-d",strtotime($_POST['txtinv_date']));
		$invoicearray['duedate'] 				= date("Y-m-d",strtotime($_POST['txtinv_duedate']));
		$invoicearray['status_invoice'] 				= $_POST['txtgstapplicable'];
		$invoicearray['entered_by']			  = $G_memberid;
		$invoicearray['entry_date']          = date('Y-m-d h:m:s');
		

if( $_POST['txtgstapplicable'] == '1')
{
			// If Product Not Available Start Of product addition
			$itemname_display_array	= $_POST['txtitemname_asperinvoice_display1'];
			if(is_array($itemname_display_array))
			{
				$rowcountitemname = count($itemname_display_array);
			}
			
			for($j=0;$j<$rowcountitemname;$j++)
			{
				$itemname	= $itemname_display_array[$j];	
				$product_available_query = "Select * from mstproducts where productname = '$itemname' and membercompanyid = $billing_membercompanyid ";	
				$productrow = $obj-> getProduct('',$product_available_query);
				$productname = $productrow[0]['productname'];
				if($itemname != $productname)
				{
					$addproduct['itemno'] 			 = $_POST['txtitemno_asperinvoice_display1'][$j];
					$addproduct['productname'] 	 = $_POST['txtitemname_asperinvoice_display1'][$j];
					$addproduct['hsn_code'] 		 = $_POST['txthsncode_asperinvoice_display1'][$j];
					$addproduct['price'] 			 = $_POST['txtitemprice_asperinvoice_display1'][$j];
					$addproduct['membercompanyid'] = $billing_membercompanyid;
					$addproduct['memberid'] 		 = $memberid;
					//print_r($addproduct);
					$addproductrow = $obj-> AddProduct($addproduct);
					
				}				
			}	
			// End of product addition
				
			$cgstper 				= 0;
			$cgstamt 				= 0;
			$sgstper 				= 0;
			$sgstamt 				= 0;	

			if($billed_membercompanyid > 0)
			{
				$taxtype   = CheckTypeOfGST($billed_membercompanyid,$statecode);
			}
			else 
			{
				$taxtype   = CheckTypeOfGSTWithoutCustomerId($stateid,$statecode);
			}
				
				
			if($taxtype == 'IGST') 
			{		
				$tax_percent  = $_POST['tax'];
			 	$invoicearray['igst_percentage'] = $tax_percent	;
				$invoicearray['igst_amount'] 		= $_POST['gst_amt'];
					
			}
			else 	if($taxtype == 'SGST' || $taxtype == 'CGST')
			{
				
						$invoicearray['cgst_percentage'] = $tax_percent / 2;
						$cgst_amount 							= $_POST['gst_amt']/2;
						$invoicearray['cgst_amount'] 		= $cgst_amount;
						$invoicearray['sgst_percentage'] = $tax_percent / 2;
						$invoicearray['sgst_amount'] 		= $cgst_amount;
			}
		if($_POST['discounts'] > 0)
		{
			if($_POST['select_discounts_mode'] == '%') 
			{
				$invoicearray['discount_percentage'] 	= $_POST['discounts'];
			}
			else if($_POST['select_discounts_mode'] == '1')
			{
				$invoicearray['discount_flat'] 			= $_POST['discounts'];
			}
			$discount_amount 									= $_POST['storefinaldiscountamt'];
			$invoicearray['discount_amount'] 			= $discount_amount;			
				
			$invoicearray['InvoiceBaseAmount'] 			= $_POST['invoicebaseamount'];
			$invoicearray['InvoiceTotalAmount'] 		= $_POST['final_amt'];
			$invoicearray['SEZ_remark'] 	= "";
		}
		else 
		{
			$invoicearray['InvoiceBaseAmount'] 			= $_POST['invoicebaseamount'];
			$invoicearray['InvoiceTotalAmount'] 		= $_POST['final_amt'];
		
			$invoicearray['SEZ_remark'] 	= "";
		}



		 $add_invoice = $invoice_obj -> AddInvoice($invoicearray);
		
		
	 	$sql ="select * from trninvoicetomember_frproforma 
				 where billed_membercompanyid = $billed_membercompanyid 
				 order by memberinvoicesid desc limit 0,1";

		$get_invoicelist = $invoice_obj -> getInvoice('',$sql,'','','');

		$memberinvoicesid	=	$get_invoicelist[0]['memberinvoicesid'];
		
		$itemno   = $_POST['txtitemno_asperinvoice_display1'];
		
		if(is_array($itemno))
		{
			$rowcountitemno = count($itemno);
		}
		if($memberinvoicesid > '')
		{
			for($j=0;$j<$rowcountitemno;$j++)
			{
				 $itemno1	= $itemno[$j];
				 $itemname	= $itemname_display_array[$j];
				 $sqltext = "Select * from mstproducts  where isdeleted = 0 and  ( itemno = '".($itemno1)."' or productname  = '".($itemname)."') and membercompanyid = $billing_membercompanyid order by Product_ID desc";
		
				 $row = $obj-> getProduct('',$sqltext);
				 $total = 0;
				$quantity  = $_POST['txtitemquantity_asperinvoice_display1'][$j];
				$productid = $row[0]['Product_ID'];
				if($quantity > 0)
				{
						$memberinvoicedetail['memberinvoicesid'] 		= $memberinvoicesid;
						$memberinvoicedetail['itemno'] 					= $row[0]['itemno'];
						$memberinvoicedetail['productname'] 			= $row[0]['productname'];
						$memberinvoicedetail['product_description'] 	= $row[0]['product_description'];	
						$memberinvoicedetail['quantity '] 				= $quantity;	
						
							
						$memberinvoicedetail['ProductBaseAmount'] 		= $_POST['txtitemprice_asperinvoice_display1'][$j];
						$memberinvoicedetail['ProductTotalAmount'] 		= $_POST['txtitemprice_asperinvoice_display1'][$j]*$quantity;
					
						$memberinvoicedetail['new_invoicesid'] 						= 0;
						$memberinvoicedetail['billed_membercompanyid'] 				= $billed_membercompanyid;
						//print_r($memberinvoicedetail);
						//die();
						$add_memberinvoicedetail = $invoice_obj -> AddInvoiceDetails($memberinvoicedetail);
				}
			}	
			
			
		}
					
}

		  if($add_memberinvoicedetail)
		 		{
				//$redirect_url = DOC_ROOT."admin/viewProforma.php";
				//	header("Location: $redirect_url");
	    			//exit;
				 }
		   	
}
?>