<?php
require_once("../include/config.php");
require_once("../include/startup.php");
	$invoice_obj					=	new Invoice_frquotation();

$obj  = new Company();
if($G_memberid > 0)
{
	
//$companyid= $G_companyid;
		$memberid = $G_memberid;
		$membercompanyid= $G_membercompanyid;
		$memberuserscompanyid = $G_memberuserscompanyid;
	//echo $companyid;
	//	echo $memberid;
		//echo $membercompanyid;
	

	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_invoice = "SELECT invoiceno,invoicedate
		FROM trninvoicetomember_frquotation a 
		inner join trnuser_company b on a.memberid = b.memberid
		where billing_membercompanyid=".$membercompanyid."	and memberuserscompanyid = $memberuserscompanyid and isdeleted = 0	  	  
		ORDER BY `memberinvoicesid` DESC limit 0,1";
	}
	else{
		$sql_invoice = "SELECT invoiceno,invoicedate
		FROM trninvoicetomember_frquotation a 
		
		where billing_membercompanyid=".$company_fromcompanyid." and isdeleted = 0	  
		ORDER BY `memberinvoicesid` DESC limit 0,1";
	}

		
		$sqllist_invoice = $invoice_obj->getInvoice('',$sql_invoice);
		if(is_array($sqllist_invoice))
		{
			 $rowcount_invoice = count($sqllist_invoice);
		}

	if($memberuserscompanyid > 0)
	{
		$sql_invoice_company = "SELECT invoiceno,invoicedate
		FROM trninvoicetomember_frquotation a 
		inner join trnuser_company b on a.memberid = b.memberid
		where billing_membercompanyid=".$membercompanyid." and b.memberuserscompanyid = $memberuserscompanyid and isdeleted = 0			  
		ORDER BY `memberinvoicesid` DESC,billing_membercompanyid ASC limit 0,1";
	}
	else{
		$sql_invoice_company = "SELECT invoiceno,invoicedate
		FROM trninvoicetomember_frquotation a 
		
		where billing_membercompanyid=".$membercompanyid." 	and isdeleted = 0	  
		ORDER BY `memberinvoicesid` DESC,billing_membercompanyid ASC limit 0,1";
	}


		
		$sqllist_invoice_company = $invoice_obj->getInvoice('',$sql_invoice_company);
		if(is_array($sqllist_invoice_company))
		{
			 $rowcount_invoice_company = count($sqllist_invoice_company);
		}
//echo $sql_invoice;
//print_r($sqllist_invoice);
//die();		
		
				 	
		if($membercompanyid > 0)
		{
			$invoiceno  				= $sqllist_invoice_company[0]['invoiceno'];
			$invoicedate  				= $sqllist_invoice_company[0]['invoicedate'];
		}
		else
		{
			$invoiceno  				= $sqllist_invoice[0]['invoiceno'];
			$invoicedate  				= $sqllist_invoice[0]['invoicedate'];
		}

			 	
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);
		}

				 	
		if($membercompanyid > 0)
		{
			$invoice_fromcompanyid  				= $sqllist_invoice1_company[0]['membercompanyid'];
			$invoice_fromcompanyname  				= $sqllist_invoice1_company[0]['companyname'];
		}
		else
		{
			$invoice_fromcompanyid  				= $sqllist_invoice1[0]['membercompanyid'];
			$invoice_fromcompanyname  				= $sqllist_invoice1[0]['companyname'];
		}
		//echo $invoice_fromcompanyid;
		//echo $invoice_fromcompanyname;
		//die();

		require_once("header1.html");
		require_once("leftnav.html");
		require_once("create_quotes1.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;
}   

?>
