<?php

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

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

$memberpaymentdetail_obj	=	new Memberpaymentdetail();
$memberpaymentheader_obj	=	new Memberpaymentheader();
 
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'];
			
		$packagetotalcost = $_POST['final_total'];
		$packagetotalcostwithtax = $_POST['final_total_withtax'];
		
		$payment_count = $_POST['entered_payment_count'];
		
		$invoicearray['quotes_proforma_invoice']	=	2;
		$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'] 				= $_POST['txtstatus'];
		$invoicearray['entered_by']			  = $G_memberid;
		$invoicearray['entry_date']          = date('Y-m-d h:m:s');
		
	$sqltext_company_statecode = "Select * from user_company  where isdeleted = 0 and membercompanyid = $billing_membercompanyid ";
	$statecode = $company_obj-> getCompany('',$sqltext_company_statecode);
	$statecode = $statecode[0]['head_Stateid1'];
	
	$itemno   = $_POST['txtitemno_display1'];
	 
	if(is_array($itemno))
	{
		$rowcountitemno = count($itemno);
	}
	$cgstper 				= 0;
	$cgstamt 				= 0;
	$sgstper 				= 0;
	$sgstamt 				= 0;	

	for($j=0;$j<$rowcountitemno;$j++)
	{
			$itemno1	= $itemno[$j];
			$sqltext = "Select * from mstproducts  where isdeleted = 0 and  itemno = '".($itemno1)."' and membercompanyid = $billing_membercompanyid order by Product_ID desc";
	
			$row = $obj-> getProduct('',$sqltext);
			
		//	$quantity  = 0;
		//	$total	  = 0;
			$quantity  = $_POST['txtitemquantity_display1'][$j];
			$productid = $row[0]['Product_ID'];
			$total_data = $row[0]['price']*$quantity;
		//   $row[$j]['price'];	
	//	echo $j;
	//	print_r($row);
		if($quantity > 0)
		{
			$taxtype = CheckTypeOfGST($billed_membercompanyid,$statecode);
			$taxarray  = TaxCalculation($total_data,$productid,$taxtype);

		//	print_r($taxtype);
		//	print_r($taxarray);
		//	exit;
			switch($taxtype) 
				{
					case 'SC_GST':
						$cgstper 								+= $taxarray['cgstper'];
						$cgstamt 								+= $taxarray['cgstamt'];
						$sgstper 								+= $taxarray['sgstper'];
						$sgstamt 								+= $taxarray['sgstamt'];	

						$invoicearray['cgst_percentage'] = $cgstper;
						$invoicearray['cgst_amount'] 		= $cgstamt;
						$invoicearray['sgst_percentage'] = $sgstper;
						$invoicearray['sgst_amount'] 		= $sgstamt;
							
					break;
					
					case 'IGST':
						$igstper 								+= $taxarray['igstper'];
						$igstamt 								+= $taxarray['igstamt'];
						$invoicearray['igst_percentage'] = $igstper;
						$invoicearray['igst_amount'] 		= $igstamt;
						
					break;								
													
					case 'UGST':
						$ugstper 								+= $taxarray['ugstper'];
						$ugstamt 								+= $taxarray['ugstamt'];
						$invoicearray['ugst_percentage'] = $ugstper;
						$invoicearray['ugst_amount'] 		= $ugstamt;
					
					break;
									
				}
				$amount 										  += $taxarray['amount'];
				$total_amount 								  += $taxarray['total_amount_with_tax'];	
			
			}
		}
		if($_POST['discount'] > 0)
		{
			$invoicearray['discount_percentage'] 		= $_POST['discount'];
			$discount_amount 									= ($amount * $_POST['discount']) / 100;
			$invoicearray['discount_amount'] 			= $discount_amount ;
			$invoicearray['InvoiceBaseAmountBeforeDiscount'] 		= $total_amount;
			
			$invoicearray['InvoiceBaseAmount'] 			= $amount;
			$invoicearray['InvoiceTotalAmount'] 		= $total_amount - $discount_amount	;
			$invoicearray['SEZ_remark'] 	= "";
			
		}
		else 
		{
			$invoicearray['InvoiceBaseAmount'] 			= $amount;
			$invoicearray['InvoiceTotalAmount'] 		= $total_amount;
			$invoicearray['SEZ_remark'] 	= "";
		}
	//		exit;			
		$add_invoice = $invoice_obj -> AddInvoice($invoicearray);
		
		$sql ="select * from trninvoicetomember where billed_membercompanyid = $billed_membercompanyid order by memberinvoicesid desc limit 0,1";

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

		$memberinvoicesid	=	$get_invoicelist[0]['memberinvoicesid'];

		if($memberinvoicesid > '')
		{
			for($j=0;$j<$rowcountitemno;$j++)
			{
			$itemno1	= $itemno[$j];
			$sqltext = "Select * from mstproducts  where isdeleted = 0 and  itemno = '".($itemno1)."' and membercompanyid = $billing_membercompanyid order by Product_ID desc";
	
			$row = $obj-> getProduct('',$sqltext);
			$total = 0;
			$quantity  = $_POST['txtitemquantity_display1'][$j];
			$productid = $row[0]['Product_ID'];
			$total = $row[0]['price']*$quantity;
			
			if($quantity > 0)
			{
			$taxtype = CheckTypeOfGST($billed_membercompanyid,$statecode);
			$taxarray  = TaxCalculation($total,$productid,$taxtype);

			$memberinvoicedetail['memberinvoicesid'] 	= $memberinvoicesid;
			$memberinvoicedetail['itemno'] 				= $row[0]['itemno'];
			$memberinvoicedetail['productname'] 		= $row[0]['productname'];
			$memberinvoicedetail['product_description'] 	= $row[0]['product_description'];	
			$memberinvoicedetail['quantity '] 			= $quantity;	
			


			switch($taxtype) 
				{
					case 'SC_GST':
						$memberinvoicedetail['cgst_percentage_product']   = $taxarray['cgstper'];
						$memberinvoicedetail['cgst_amount_product'] 		= $taxarray['cgstamt'];
						$memberinvoicedetail['sgst_percentage_product']   = $taxarray['sgstper'];
						$memberinvoicedetail['sgst_amount_product'] 		= $taxarray['sgstamt'];	
							
					break;
					
					case 'IGST':
						$memberinvoicedetail['igst_percentage_product']  = $taxarray['igstper'];
						$memberinvoicedetail['igst_amount_product'] 	  = $taxarray['igstamt'];
						
					break;								
													
					case 'UGST':
						$memberinvoicedetail['ugst_percentage_product']  = $taxarray['ugstper'];
						$memberinvoicedetail['ugst_amount_product'] 	  = $taxarray['ugstamt'];
					
					break;
									
				}
				
			$memberinvoicedetail['ProductBaseAmount'] 		= $taxarray['amount'];
			$memberinvoicedetail['ProductTotalAmount'] 		= $taxarray['total_amount_with_tax'];	
			

				$memberinvoicedetail['billed_membercompanyid'] 				= $billed_membercompanyid;
				$add_memberinvoicedetail = $invoice_obj -> AddInvoiceDetails($memberinvoicedetail);
				}
			}	
		/*	
			$paymentheaderarray['billed_membercompanyid'] 				= $billed_membercompanyid;
			$paymentheaderarray['memberinvoicesid'] 	= $memberinvoicesid;
			$paymentheaderarray['paymentmode'] 			= $_POST['radiopaymentmode'];
			$paymentheaderarray['paymentdate'] 			= date("Y-m-d");	
			
			$header_total = 0;
			$tds_total 	  = 0;
			for($i=1;$i<=$payment_count;$i++) 
				{	
						
						$cheque_amt 				= intval($_POST['txtchequeamount'.$i]);
						$tds_amt					= intval($_POST['txttds'.$i]);
			   
					if($_POST['txtchequeamount'.$i] > '') 
					{						   
			   
			  			 $header_total = $header_total + $cheque_amt;
					
					}
					if($_POST['txttds'.$i] > '') 
					{						   
			   
			  			 $tds_total = $tds_total + $tds_amt;
					
					}
				}
			
			$paymentheaderarray['paymentamount'] 		= '';
			$paymentheaderarray['servicetaxamount'] 	= '';
			$paymentheaderarray['TotalAmount'] 			= $header_total;
			$paymentheaderarray['tds'] 					= $tds_total;
			$add_paymentheader = $memberpaymentheader_obj -> AddMemberpaymentheader($paymentheaderarray);
		
			$sql2 ="select * from user_company_payment_header where billed_membercompanyid = $billed_membercompanyid order by memberpaymentheaderid desc limit 0,1";
			$get_paymentheaderlist = $memberpaymentheader_obj -> getMemberpaymentheader('',$sql2,'','','','');
			$memberpaymentheaderid	= $get_paymentheaderlist[0]['memberpaymentheaderid'];
		//	  echo $memberpaymentheaderid;
			  
			if($memberpaymentheaderid > '')
			{
		//		echo "1";
				for($i=1;$i<=$payment_count;$i++) 
				{	
			//	echo "Hello";
						$paymentdetailarray['billed_membercompanyid'] 	= $billed_membercompanyid;
						$paymentdetailarray['memberpaymentheaderid'] 	= $memberpaymentheaderid;
						$paymentdetailarray['memberinvoicesid'] 			= $memberinvoicesid;
					 	
					 	$paymentdetailarray['chequeno'] 						= trim(sanitize($_POST['txtchequeno'.$i],SQL));
						$paymentdetailarray['chequedate'] 					= date("Y-m-d", strtotime($_POST['txtchequedate'.$i]));
						$paymentdetailarray['chequeamount'] 				= $_POST['txtchequeamount'.$i];
						$paymentdetailarray['bankname'] 						= trim(sanitize($_POST['txtbankname'.$i],SQL));
						$paymentdetailarray['branch'] 						= trim(sanitize($_POST['txtbranch'.$i],SQL));
						$paymentdetailarray['mode_of_payment'] 			= $_POST['radiopaymentmode'.$i];
			   
		//	   print_r($paymentdetailarray);
					if($_POST['txtchequeamount'.$i] > '' && $_POST['txtchequeno'.$i] > '') 
					{						   
			   //			print_r($paymentdetailarray);
			  			 $add_paymentdetail = $memberpaymentdetail_obj -> AddMemberpaymentdetail($paymentdetailarray);
					
					}
				
				}				 
			 	
			}
			
		*/
			
			
		}
		
		   	if($add_memberinvoicedetail)
		 		{
					$redirect_url = DOC_ROOT."admin/viewInvoice.php";
					header("Location: $redirect_url");
	    			exit;
				 }
		   	
		   }
?>