<?php
require_once("../include/config.php");
require_once("../include/startup.php");
$invoice_obj					=	new Invoice();
$obj  = new Company();
$testclass    			= 	new AllTables();
if($G_memberid > 0)
{
		//$membercompanyid						= intval($_GET['membercompanyid']);
$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=".$companyid."		  
		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($companyid > 0)
		{
			$company_fromcompanyid  				= $companyid;
		}
		else
		{
			$company_fromcompanyid  				= $sqllist_company[0]['membercompanyid'];
		}
	

				 	
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=".$companyid."	  
		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($companyid > 0)
		{
			$invoice_fromcompanyid  				= $invoice_fromcompanyid2;
			$invoice_fromcompanyname  				= $invoice_fromcompanyname2;
		}
		else
		{
			$invoice_fromcompanyid  				= $invoice_fromcompanyid1;
			$invoice_fromcompanyname  				= $invoice_fromcompanyname1;
		}


		$AllStatesList 	= getAllStates();
$stetelist = '<option value="">Select State</option>'.getAllStateListForCombo('','','',$AllStatesList);


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

?>
