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


if($G_memberid > 0)
{
	

 $memberid = $G_memberid;
  $memberuserscompanyid = $G_memberuserscompanyid;
  $membercompanyid= $G_membercompanyid;
   // echo  $memberid;
  //echo $membercompanyidss;
  //die();

		$obj  = new Company();
		$obj_Invoice  =new Invoice();
		$obj_quotation = new Invoice_frquotation();
		$obj_proforma  =new Invoice_frproforma();
	   $obj_client  = new Client();
	   
	   //for recent quotes section 
	    $sql_fy = "select YEAR(CURDATE())-1 as last_fy_year,YEAR(CURDATE()) as This_fy_year,YEAR(CURDATE())+1 as next_fy_year
			 			    from mstmember x
					  		
					  		 where x.memberid = $memberid";
			  		 
					//echo $sql_fy;
					//die();
					$sqllist_fy = $obj->getCompany('','',$sql_fy);
					$last_fy_year						= $sqllist_fy[0]['last_fy_year'];
					$This_fy_year						= $sqllist_fy[0]['This_fy_year'];
					$next_fy_year						= $sqllist_fy[0]['next_fy_year']; 

	if($memberuserscompanyid > 0)
	{
	 $sql = "select * from mstmember x
			  inner join trninvoicetomember_frquotation a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails_frquotation b on a.memberinvoicesid = b.memberinvoicesid
				  inner join trnuser_company c on x.memberid = c.memberid
				  where x.memberid = $memberid and c.memberuserscompanyid = $memberuserscompanyid and a.billing_membercompanyid=$membercompanyid and quotes_proforma_invoice = 0  and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC limit 0,5";
	}
	else
	{
		$sql = "select * from mstmember x
			  inner join trninvoicetomember_frquotation a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails_frquotation b on a.memberinvoicesid = b.memberinvoicesid
				  where x.memberid = $memberid and quotes_proforma_invoice = 0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC limit 0,5";
	}
	//echo $sql;
	//die();
		$sqllist = $obj_quotation->getInvoice('',$sql);
		if(is_array($sqllist))
		{
			$rowcount = count($sqllist);
		}
	if($memberuserscompanyid > 0)
	{
		$sql2 = "select * from mstmember x
			  inner join trninvoicetomember_frquotation a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails_frquotation b on a.memberinvoicesid = b.memberinvoicesid
				    inner join trnuser_company c on x.memberid = c.memberid
				  where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid and c.memberuserscompanyid = $memberuserscompanyid and  quotes_proforma_invoice = 0 and a.isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC";
	}
	else
	{
		$sql2 = "select * from mstmember x
			  inner join trninvoicetomember_frquotation a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails_frquotation b on a.memberinvoicesid = b.memberinvoicesid
				  where x.memberid = $memberid and quotes_proforma_invoice = 0 and a.isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC";
	}			  
	
		$sqllist2 = $obj_quotation->getInvoice('',$sql2);
		if(is_array($sqllist2))
		{
			$rowcount2 = count($sqllist2);
		}
		
	if($memberuserscompanyid > 0)
	{	
		$sql_proforma = "select * from mstmember x
				  inner join trninvoicetomember_frproforma a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails_frproforma b on a.memberinvoicesid = b.memberinvoicesid
				    inner join trnuser_company c on x.memberid = c.memberid
				  where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid and c.memberuserscompanyid = $memberuserscompanyid and  quotes_proforma_invoice = 1 and a.isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC limit 0,5";
	}
	else
	{
		$sql_proforma = "select * from mstmember x
				  inner join trninvoicetomember_frproforma a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails_frproforma b on a.memberinvoicesid = b.memberinvoicesid
				  where x.memberid = $memberid and quotes_proforma_invoice = 1 and a.isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC limit 0,5";
	}
		$sqllist_proforma = $obj_Invoice->getInvoice('',$sql_proforma);
		if(is_array($sqllist_proforma))
		{
			$rowcount_proforma = count($sqllist_proforma);
		}
	if($memberuserscompanyid > 0)
	{		
		$sql_proforma2 = "select * from mstmember x
				  inner join trninvoicetomember_frproforma a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails_frproforma b on a.memberinvoicesid = b.memberinvoicesid
				    inner join trnuser_company c on x.memberid = c.memberid
				  where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid and c.memberuserscompanyid = $memberuserscompanyid and  quotes_proforma_invoice = 1 and a.isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC";
	}
	else
	{
		$sql_proforma2 = "select * from mstmember x
				  inner join trninvoicetomember_frproforma a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails_frproforma b on a.memberinvoicesid = b.memberinvoicesid
				  where x.memberid = $memberid and quotes_proforma_invoice = 1 and a.isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC";
	}	
		$sqllist_proforma2 = $obj_Invoice->getInvoice('',$sql_proforma2);
		if(is_array($sqllist_proforma2))
		{
			$rowcount_proforma2 = count($sqllist_proforma2);
		}
		
	if($memberuserscompanyid > 0)
	{		
		//for recent invoices section
		 $sql_invoice = "select a.memberinvoicesid,a.billed_membercompanyid,a.billing_membercompanyid,a.invoiceno,a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount from mstmember x
				  inner join trninvoicetomember a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
				  left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
				  left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
				    inner join trnuser_company e on x.memberid = e.memberid
				  where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid and e.memberuserscompanyid = $memberuserscompanyid and  a.quotes_proforma_invoice = 2 and a.isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC limit 0,5";
	}
	else
	{
		 $sql_invoice = "select a.memberinvoicesid,a.billed_membercompanyid,a.billing_membercompanyid,a.invoiceno,a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount from mstmember x
				  inner join trninvoicetomember a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
				  left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
				  left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
				  where x.memberid = $memberid and a.quotes_proforma_invoice = 2 and a.isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC limit 0,5";	
	}			  
		$sql_invoice_list = $obj_Invoice->getInvoice('',$sql_invoice);
		if(is_array($sql_invoice_list))
		{
			$rowcount_invoice = count($sql_invoice_list);
		}
	if($memberuserscompanyid > 0)
	{		
		$sql_invoice2 = "select a.memberinvoicesid,a.billed_membercompanyid,a.billing_membercompanyid,a.invoiceno,a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount from mstmember x
				  inner join trninvoicetomember a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
				  left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
				  left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
				    inner join trnuser_company e on x.memberid = e.memberid
				  where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid and e.memberuserscompanyid = $memberuserscompanyid and  a.quotes_proforma_invoice = 2 and a.isdeleted=0  and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC";
	}
	else
	{
		$sql_invoice2 = "select a.memberinvoicesid,a.billed_membercompanyid,a.billing_membercompanyid,a.invoiceno,a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount from mstmember x
				  inner join trninvoicetomember a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
				  left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
				  left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
				  where x.memberid = $memberid and a.quotes_proforma_invoice = 2 and a.isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by a.memberinvoicesid DESC";	
	}			  
	//echo $sql_invoice2;
		$sql_invoice_list2 = $obj_Invoice->getInvoice('',$sql_invoice2);
		if(is_array($sql_invoice_list2))
		{
			$rowcount_invoice2 = count($sql_invoice_list2);
		}
		
		$sql_client = "select * from mstclients where memberid = $memberid ";
	$sqllist_client = $obj_client->getClient('','',$sql_client);
	if(is_array($sqllist_client))
	{
		$rowcount_client = count($sqllist_client);
	}
	
	
	//for overdue payments graph
	$today_date = date('Y-m-d');
/*$sqlduequery="select count(a.billed_membercompanyid)as membercount,a.billed_membercompanyid,a.billing_membercompanyid,
						a.invoiceno,a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,
						SUM(c.TotalAmount) as paid_total_amount 
						from mstmember x 
						inner join trninvoicetomember a on x.memberid = a.memberid 
						inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid 
						left join user_company_payment_header c on a.memberinvoicesid = c.memberinvoicesid 
						left join user_company_payment_detail d on a.memberinvoicesid = d.memberinvoicesid 
						where x.memberid = $memberid and a.quotes_proforma_invoice = 2 
						and a.isdeleted=0 and a.duedate <= '$today_date'
						group by a.memberinvoicesid";*/
	/*$sqlduequery="SELECT SUM(a.TotalAmount) as paid_total_amount,c.billed_membername
					FROM user_company_payment_header a 
					INNER join mstclients b on a.billed_membercompanyid =b.clientcompanyid 
					INNER join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid
					INNER JOIN mstmember d on c.memberid =d.MemberID 
					WHERE d.MemberID = $memberid group by a.billed_membercompanyid";
*/	if($memberuserscompanyid > 0)
	{
		$sqlduequery="SELECT *,SUM(d.TotalAmount) as paid_total_amount,a.billed_membername
				  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
				    inner join trnuser_company f on x.memberid = f.memberid
					WHERE x.MemberID = $memberid and a.billing_membercompanyid=$membercompanyid and  f.memberuserscompanyid = $memberuserscompanyid and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by d.billed_membercompanyid DESC limit 0,5";
					//echo $sqlduequery;
					//die();
	}
	else
	{
		$sqlduequery="SELECT *,SUM(d.TotalAmount) as paid_total_amount,a.billed_membername
				  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 date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' group by d.billed_membercompanyid DESC limit 0,5";
					//echo $sqlduequery;
					//die();
	}				
	$sqlduequery_list = $obj_Invoice->getInvoice('',$sqlduequery);
	//$due_paymentcount = count($sqlduequery_list);
	
	//if ($sqlduequery_list)
			if(is_array($sqlduequery_list))
	{
		//$overdueinvoicesclient = ConvertToChartTable($sqlduequery_list,'billed_membername','paid_total_amount','received Amount','RegionBidsPie','recived payments');

         $due_paymentcount = count($sqlduequery_list);
	}
	//$paid_total_amount  				= $sqlduequery_list[0]['paid_total_amount'];
	

		if($memberuserscompanyid > 0)
	{
		$sql_invoice1 = "select SUM(InvoiceTotalAmount) as InvoiceTotalAmounts  from trninvoicetomember where memberid = $memberid  and billing_membercompanyid=$membercompanyid and isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' and  quotes_proforma_invoice = 2";
	}
	else
	{
		$sql_invoice1 = "select SUM(InvoiceTotalAmount) as InvoiceTotalAmounts  from trninvoicetomember where memberid = $memberid  and isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' and  quotes_proforma_invoice = 2";
	}
	//echo $sql_invoice1;
	//die();
	$sql_invoice_list1 = $obj_Invoice->getInvoice('',$sql_invoice1);
	
		
			
			$InvoiceTotalAmount1  				= $sql_invoice_list1[0]['InvoiceTotalAmounts'];
			//$InvoiceTotalAmount2  = format_number($InvoiceTotalAmount1);
	if($memberuserscompanyid > 0)
	{
		$sqlduequery1 = "select SUM(a.TotalAmount) as paid_total_amount  from user_company_payment_header a
		INNER join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid

		where memberid = $memberid and billing_membercompanyid=$membercompanyid and isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' and YEAR(a.paymentdate)  BETWEEN ('2020-01-01')  and ('2021-01-01')  and quotes_proforma_invoice = 2 ";
	}
	else
	{
		$sqlduequery1 = "select SUM(a.TotalAmount) as paid_total_amount  from user_company_payment_header a
		INNER join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid

		where memberid = $memberid and isdeleted=0 and date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' and YEAR(a.paymentdate)  BETWEEN ('2020-01-01')  and ('2021-01-01')  and quotes_proforma_invoice = 2 ";	
	}
		
	//echo $sqlduequery1;
	//die();
	$sqlduequery_list1 = $obj_Invoice->getInvoice('',$sqlduequery1);
	
		
			
			$paid_total_amount  				= $sqlduequery_list1[0]['paid_total_amount'];

         $balance1 							= $InvoiceTotalAmount1 - $paid_total_amount;

	/*
		echo $sql_count_status = "select a.status,COUNT(*)from mstmember x
				  inner join trninvoicetomember_frquotation a on x.memberid = a.memberid 
				  inner join trninvoicetomemberdetails_frquotation b on a.memberinvoicesid = b.memberinvoicesid
				  where x.memberid = $memberid and quotes_proforma_invoice = 0 group by a.memberinvoicesid";
	
		$sqllistcount_status = $obj_quotation->getInvoice('',$sql_count_status);
		if(is_array($sqllistcount_status))
		{
			$rowcount_status = count($sqllistcount_status);
		}
		for($i=0;$i<$rowcount_status;$i++){
			$statusdata = $sqllistcount_status[$i]['status'];
             if($statusdata == '0')
         		{
							$status1 = 'Draft';  
						 
							    		         		
         		}
         		elseif($statusdata == '1')
         		{
							$status = '<span class="badge badge-success  btn-block">Approved</span>';         		
         		}    		
         		elseif($statusdata == '2')
         		{
         				$status = '<span class="badge badge-warning  btn-block">Rejected</span>';         		
         		}
		
		}*/
	
	
		require_once("header1.html");
		require_once("leftnav.html");
		require_once("index1.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;
}  


	
	
?>
