<?php
require_once("../include/config.php");
require_once("../include/startup.php");
$invoice_obj					=	new Invoice();
$obj  = new Company();
$membercompanyid= $G_membercompanyid;
$memberuserscompanyid = $G_memberuserscompanyid;
if($G_memberid > 0)
{
	
		$billed_membercompanyid	= $_GET['companyid'];
		$memberinvoicesid			= $_GET['memberinvoicesid'];
		$memberid 					= $G_memberid;
	
		
/*echo $sql_payment = "select * from mstmember x
				  inner join trninvoicetomember a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
				  inner join mstclients c on a.billed_membercompanyid = c.clientcompanyid
				  inner join user_company_payment_header d on a.memberinvoicesid = d.memberinvoicesid
				  inner join user_company_payment_detail e on d.memberpaymentheaderid = e.memberpaymentheaderid
				  where x.memberid = $memberid  and quotes_proforma_invoice = 2 and a.memberinvoicesid=$memberinvoicesid";*/
				  
	if($memberuserscompanyid > 0)
	{
		$sql_company = "SELECT *
		FROM user_company 
		where membercompanyid=".$membercompanyid."		  
		ORDER BY membercompanyid ASC limit 0,1";
	}
	else{
		$sql_company = "SELECT *
		FROM user_company 
		ORDER BY membercompanyid ASC limit 0,1";
	}
		$sqllist_company = $obj->getCompany('','',$sql_company);
		//echo $sql_company;
				//print_r($sqllist_company);

		if($membercompanyid > 0)
		{
			$company_fromcompanyid  				= $membercompanyid;
		}
		else
		{
			$company_fromcompanyid  				= $sqllist_company[0]['membercompanyid'];
		}


		if($memberuserscompanyid > 0)
	{
		

		$sql_payment = "SELECT head_receipt_no FROM `user_company_payment_header` a
		inner join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid
		 where billing_membercompanyid=".$membercompanyid."	and memberuserscompanyid = $memberuserscompanyid	 ORDER BY `memberpaymentheaderid` DESC limit 0,1";
	}
	else{


		$sql_payment = "SELECT head_receipt_no FROM `user_company_payment_header` a inner join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid  where billing_membercompanyid=".$company_fromcompanyid."	 ORDER BY `memberpaymentheaderid` DESC limit 0,1";
	}
		  
		
		$sqllist_payment = $invoice_obj->getInvoice('',$sql_payment);
		if(is_array($sqllist_payment))
		{
			 $rowcount_payment = count($sqllist_payment);
		}

	if($memberuserscompanyid > 0)
	{
		$sql_payment_company = "SELECT head_receipt_no
		FROM user_company_payment_header a 
		inner join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid
		inner join trnuser_company b on c.memberid = b.memberid
		where billing_membercompanyid=".$membercompanyid." and b.memberuserscompanyid = $memberuserscompanyid		  
		ORDER BY `memberpaymentheaderid` DESC,billing_membercompanyid ASC limit 0,1";

		
	}
	else{
		$sql_payment_company = "SELECT head_receipt_no
		FROM user_company_payment_header a 
		inner join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid
		where billing_membercompanyid=".$company_fromcompanyid." 	  
		ORDER BY `memberpaymentheaderid` DESC,billing_membercompanyid ASC limit 0,1";
	}


		
		$sqllist_payment_company = $invoice_obj->getInvoice('',$sql_payment_company);
		if(is_array($sqllist_payment_company))
		{
			 $rowcount_invoice_company = count($sqllist_payment_company);
		}
//echo $sql_payment_company;
//print_r($sqllist_payment_company);
//die();		
		
				 	
		if($membercompanyid > 0)
		{
			$head_receipt_no  				= $sqllist_payment_company[0]['head_receipt_no'];
			//$invoicedate  				= $sqllist_payment_company[0]['invoicedate'];
		}
		else
		{
			$head_receipt_no  				= $sqllist_payment[0]['head_receipt_no'];
			//$invoicedate  				= $sqllist_payment[0]['invoicedate'];
		}
		
				 //	$head_receipt_no  				= $sqllist_payment[0]['head_receipt_no'];
				 	
if($memberuserscompanyid > 0)
	{
		$sql_invoice1 = "SELECT a.companyname,a.membercompanyid
		FROM user_company  a 
		inner join trnuser_company b on a.membercompanyid = b.membercompanyid
		where a.membercompanyid=".$membercompanyid."	and b.memberuserscompanyid = $memberuserscompanyid		  	  
		ORDER BY a.membercompanyid ASC limit 0,1";
	}
	else{
		$sql_invoice1 = "SELECT a.companyname,a.membercompanyid
		FROM user_company  a 
		where a.membercompanyid=".$company_fromcompanyid."	  
		ORDER BY membercompanyid ASC limit 0,1";
	}
 

		$sqllist_invoice1 = $obj->getCompany('','',$sql_invoice1);
if(is_array($sqllist_invoice1))
		{
			 $rowcount_invoice1 = count($sqllist_invoice1);
		}

		if($memberuserscompanyid > 0)
	{
		$sql_invoice1_company = "SELECT a.companyname,a.membercompanyid
		FROM user_company  a 
		inner join trnuser_company b on a.membercompanyid = b.membercompanyid
		where a.membercompanyid=".$membercompanyid."	and memberuserscompanyid = $memberuserscompanyid			  
		ORDER BY a.membercompanyid ASC limit 0,1";
	}
	else{
		$sql_invoice1_company = "SELECT a.companyname,a.membercompanyid
		FROM user_company  a 
		where a.membercompanyid=".$membercompanyid."	  
		ORDER BY a.membercompanyid ASC limit 0,1";
	}
 
//echo $sql_invoice1_company;
//echo $sql_invoice1;
//die();
		$sqllist_invoice1_company = $obj->getCompany('','',$sql_invoice1_company);
if(is_array($sqllist_invoice1_company))
		{
			 $rowcount_invoice1_company = count($sqllist_invoice1_company);
		}
//echo $sql_invoice1_company;
//print_r($sqllist_invoice1);
//echo $sql_invoice1;
//print_r($sqllist_invoice1_company);
//die();
			$invoice_fromcompanyid1  				= $sqllist_invoice1[0]['membercompanyid'];
			$invoice_fromcompanyname1  				= $sqllist_invoice1[0]['companyname'];
			$invoice_fromcompanyid2  				= $sqllist_invoice1_company[0]['membercompanyid'];
			$invoice_fromcompanyname2  				= $sqllist_invoice1_company[0]['companyname'];
		if($membercompanyid > 0)
		{
			$invoice_fromcompanyid  				= $invoice_fromcompanyid2;
			$invoice_fromcompanyname  				= $invoice_fromcompanyname2;
		}
		else
		{
			$invoice_fromcompanyid  				= $invoice_fromcompanyid1;
			$invoice_fromcompanyname  				= $invoice_fromcompanyname1;
		}
			require_once("create_payment1.html");
		require_once("header1.html");
		require_once("leftnav.html");
		require_once("footer1.html");	
 
	
}
else if (!headers_sent($filename, $linenum)) 
{
				$redirect_url = DOC_ROOT."login.php";
				header("Location: $redirect_url");
    			exit;
					// You would most likely trigger an error here.
}
else 
{
				echo "Headers already sent in $filename on line $linenum\n" .
          			"Cannot redirect, for now please click this <a ";
    			exit;
}   

?>
