<?php

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

$obj  = new Client();
$obj1  = new Company();

	if($G_memberid > 0)
{
	$memberid = $G_memberid;
	 $memberuserscompanyid = $G_memberuserscompanyid;
	$membercompanyid= $G_membercompanyid;

	$montharray = sanitize($_POST['cmbmonth'],SQL);
				$montharray_count =  count($_POST['cmbmonth']);
				if($montharray_count > 0) 
				{
					$month_id = '(';
					for($i=0;$i<$montharray_count;$i++) 
					{
							$month =  explode('-',$montharray[$i]);
						if($i == 0) 
						{
								$month_id .= $month[0];
							$monthvar .= $month[1];
						}
						else 
						{
							$month_id .=",".$month[0];
							$monthvar .= ",".$month[1];
						}
								 
					}
					$month_id .= ')';
					
					$where_cond_month = "MONTH(a.invoicedate) in $month_id and";
				}		
 $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;
					//die();
					$sqllist_fy = $obj1->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'];
	$display1 = sanitize($_POST['txtfromdate'],SQL);
	$display2 = sanitize($_POST['txttodate'],SQL);
	
	$fromdate	= date("Y-m-d", strtotime($_POST['txtfromdate']));
	$todate		= date("Y-m-d", strtotime($_POST['txttodate']));
		if ($fromdate=='1970-01-01')
	{
		//$where_date ="YEAR (invoicedate) = YEAR (CURDATE()) and ";
		$where_date ="date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' and ";
		
	}
	else
	{
		$where_date ="date(invoicedate) BETWEEN '$fromdate' and '$todate'and";
	}





 
$day = sanitize($_POST['opt'],SQL);
$todayedate = sanitize($_POST['todayedate'],SQL);
$yesterdaydate = sanitize($_POST['yesterdaydate'],SQL);
$this_month = sanitize($_POST['this_month'],SQL);
$last_month = sanitize($_POST['last_month'],SQL);

$this_year = sanitize($_POST['this_year'],SQL);
	$this_years =  explode('-',$this_year);
	$this_year1 .= $this_years[0];
	$Next_year2 .= $this_years[1];

$last_year = sanitize($_POST['last_years'],SQL);
	$last_years =  explode('-',$last_year);
	$last_year1 .= $last_years[0];
	$this_year3 .= $last_years[1];

$this_quarter = sanitize($_POST['this_quarter'],SQL);
	$this_quarter_months =  explode('-',$this_quarter);
		$this_quarter_month1 .= $this_quarter_months[0];
		$this_quarter_month2 .= $this_quarter_months[1];
		$this_quarter_month3 .= $this_quarter_months[2];
$last_quarter = sanitize($_POST['last_quarter'],SQL);
	$last_quarter_months =  explode('-',$last_quarter);
		$last_quarter_month1 .= $last_quarter_months[0];
		$last_quarter_month2 .= $last_quarter_months[1];
		$last_quarter_month3 .= $last_quarter_months[2];		
$today=date('Y-m-d', strtotime($todayedate));
$yesterday=date('Y-m-d', strtotime($yesterdaydate));




switch ($day) {
    case "today":
    
        $where_day .= "and invoicedate = '$today'  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "yesterday":
        $where_day .= "and invoicedate = '$yesterday'  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "Last_7_days":
     	$where_day .= "and invoicedate >= CURRENT_DATE()-7  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "last_30_days":
   		//$where_day .= "and invoicedate >= CURRENT_DATE()-30";
   		$where_day .= "and MONTH(invoicedate) in ('$this_month')  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "This_month":
       // $where_day .= "and invoicedate >= CURRENT_DATE()-30";
        $where_day .= "and MONTH(invoicedate) in ('$this_month')  and YEAR (invoicedate) = YEAR (CURDATE())";
        
        break;
    case "Last_month":
       // $where_day .= "and invoicedate >= CURRENT_DATE()-60";
         $where_day .= "and MONTH(invoicedate) in ('$last_month')  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "This_quarter":
     // $where_day .= "and invoicedate >= QUARTER('$today')";
		//$where_day = "and MONTH(invoicedate) in ('$this_quarter_month1,$this_quarter_month2,$this_quarter_month3')  and YEAR (invoicedate) = YEAR (CURDATE())";
    	if($this_quarter=='1')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='2')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($this_quarter=='3')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($this_quarter=='4')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($this_quarter=='5')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
    	if($this_quarter=='6')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='7')
    	{

		$where_day .= "and QUARTER(invoicedate)='3' AND and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='8')
    	{

		$where_day .= "and QUARTER(invoicedate)='3' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='9')
    	{

		$where_day .= "and QUARTER(invoicedate)='3'and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='10')
    	{

		$where_day .= "and QUARTER(invoicedate)='4' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='11')
    	{

		$where_day .= "and QUARTER(invoicedate)='4' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='12')
    	{

		$where_day .= "and QUARTER(invoicedate)='4'  and YEAR (invoicedate) = YEAR (CURDATE())";
		}
        break;
    	case "Last_quarter":
    // $where_day .= "and invoicedate >= QUARTER(CURDATE())-1";
		//$where_day = "and MONTH(invoicedate) in ('$last_quarter_month1,$last_quarter_month2,$last_quarter_month3')  and YEAR (invoicedate) = YEAR (CURDATE())";
    	if($last_quarter=='1')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='2')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($last_quarter=='3')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($last_quarter=='4')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($last_quarter=='5')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
    	if($last_quarter=='6')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='7')
    	{

		$where_day .= "and QUARTER(invoicedate)='3' AND and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='8')
    	{

		$where_day .= "and QUARTER(invoicedate)='3' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='9')
    	{

		$where_day .= "and QUARTER(invoicedate)='3'and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='10')
    	{

		$where_day .= "and QUARTER(invoicedate)='4' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='11')
    	{

		$where_day .= "and QUARTER(invoicedate)='4' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='12')
    	{

		$where_day .= "and QUARTER(invoicedate)='4'  and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		
		//$where_day .= "";
        break;
         case "This_year":
    // $where_day .= "and invoicedate >= QUARTER(CURDATE())-1";
		//$where_day = "and YEAR(invoicedate) in('$this_year')";

       //  $sql="SELECT *FROM mytable where date BETWEEN CAST('$pst-04-01' AS DATE) AND CAST('$pt-03-31' AS DATE)";

		$where_day ="and date(invoicedate) BETWEEN '$this_year1-04-01' and '$Next_year2-03-31'";
		//$where_date .= "";
        break;
         case "Last_year":
    // $where_day .= "and invoicedate >= QUARTER(CURDATE())-1";
		//$where_day = "and YEAR(invoicedate) in('$last_year')";
		$where_day ="and date(invoicedate) BETWEEN '$last_year1-04-01' and '$this_year3-03-31'";

		
		//$where_day .= "";
        break;
    case "All_time":
    	$where_day .= "";
       // $where_day .= "and invoicedate >= CURRENT_DATE()-30";
        break;
    default:
        $where_day .= "";
}

$radiopaymentmode = sanitize($_POST['radiopaymentmode1'],SQL);
switch ($radiopaymentmode) {
    case "Partially_Paid":
    
        $where_paymentmode .= "and InvoiceTotalAmount-TotalAmount !='0'";
        // $where_paymentmode1 .= "and InvoiceTotalAmount !=paid_total_amount";
        $where_paymentmode1 .= "and InvoiceTotalAmount-paid_total_amount >'0'";
        break;
    case "Not_Paid":
        $where_paymentmode .= "and paid_total_amount = ' '";
        $where_paymentmode1 .= "and paid_total_amount = ' '";
        break;
    case "Full_Paid":
     	 $where_paymentmode1 .= "and InvoiceTotalAmount =paid_total_amount";
     	$where_paymentmode .= "and InvoiceTotalAmount-TotalAmount ='0'";
        break;
    case "All":
   		//$where_day .= "and invoicedate >= CURRENT_DATE()-30";
   		$where_paymentmode .= "";
   		$where_paymentmode1 .= "";
        break;
    default:
        $where_paymentmode .= "";
        $where_paymentmode1 .= "";
}



	if($memberuserscompanyid > 0)
	{	
		if($day)
		{

				$sql2 = "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
				   inner join trnuser_company f on x.memberid = f.memberid
				  where x.memberid = $memberid   $where_day and  f.memberuserscompanyid = $memberuserscompanyid and a.billing_membercompanyid=$membercompanyid and quotes_proforma_invoice = 2  GROUP by memberpaymentdetailID order by a.invoicedate,d.head_receipt_date,CAST(a.invoiceno AS unsigned),CAST(d.head_receipt_no AS unsigned) ";
		}
		else
		{
				$sql2 = "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
				   inner join trnuser_company f on x.memberid = f.memberid
				  where x.memberid = $memberid  and $where_date f.memberuserscompanyid = $memberuserscompanyid and a.billing_membercompanyid=$membercompanyid and quotes_proforma_invoice = 2  GROUP by memberpaymentdetailID order by a.invoicedate,d.head_receipt_date,CAST(a.invoiceno AS unsigned),CAST(d.head_receipt_no AS unsigned) ";
		}		  
	}
	else
	{
		if($day)
		{
				$sql2 = "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 $where_day  and quotes_proforma_invoice = 2  GROUP by memberpaymentdetailID order by a.invoicedate,d.head_receipt_date,CAST(a.invoiceno AS unsigned),CAST(d.head_receipt_no AS unsigned)";
		}
		else
		{
			$sql2 = "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 $where_date quotes_proforma_invoice = 2  GROUP by memberpaymentdetailID order by a.invoicedate,d.head_receipt_date,CAST(a.invoiceno AS unsigned),CAST(d.head_receipt_no AS unsigned)";
		}		  
	}			  
		/*$sql2 ="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 f on x.memberid = f.memberid
				  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  GROUP by memberpaymentdetailID";	*/	  
		$sqllist2 = $obj1->getCompany('','',$sql2);
		//print_r($sqllist2);
		if(is_array($sqllist2))
		{
			$rowcount2 = count($sqllist2);
		}


if($memberuserscompanyid > 0)
				{
					  $sql1 = "select MONTHNAME(CURRENT_DATE()) as This_monthname,MONTHNAME(CURRENT_DATE())-1 as Last_monthname,MONTH(CURRENT_DATE()) as This_month,MONTH(CURRENT_DATE())-1 as Last_month,CURRENT_DATE() as today_date,CURRENT_DATE() -1 as Yesterday_date,MONTH(CURRENT_DATE())+1 as This_month_quter2,MONTH(CURRENT_DATE())+2 as This_month_quter3,MONTH(CURRENT_DATE())-2 as Last_month_quter2,MONTH(CURRENT_DATE())-3 as Last_month_quter3,MONTH(CURRENT_DATE())-4 as Last_month_quter4,YEAR(CURDATE())-1 as last_year,YEAR(CURDATE()) as This_year,YEAR(CURDATE())+1 as next_year
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		  inner join trnuser_company c on x.memberid = c.memberid
					  		 where x.memberid = $memberid  and a.billing_membercompanyid=$membercompanyid and c.memberuserscompanyid = $memberuserscompanyid and a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid";

				}
				else
				{
					  $sql1 = "select MONTHNAME(CURRENT_DATE()) as This_monthname,MONTHNAME(CURRENT_DATE())-1 as Last_monthname,MONTH(CURRENT_DATE()) as This_month,MONTH(CURRENT_DATE())-1 as Last_month,CURRENT_DATE() as today_date,CURRENT_DATE() -1 as Yesterday_date,MONTH(CURRENT_DATE())+1 as This_month_quter2,MONTH(CURRENT_DATE())+2 as This_month_quter3,MONTH(CURRENT_DATE())-2 as Last_month_quter2,MONTH(CURRENT_DATE())-3 as Last_month_quter3,MONTH(CURRENT_DATE())-4 as Last_month_quter4,YEAR(CURDATE())-1 as last_year,YEAR(CURDATE()) as This_year,YEAR(CURDATE())+1 as next_year
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 where x.memberid = $memberid and a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid";
				}	  		 
					//echo $sql;
					//die();
					$sqllist1 = $obj1->getCompany('','',$sql1);


					$today_date						= date("d-M",strtotime($sqllist1[0]['today_date']));
					$Yesterday_date						= date("d-M",strtotime($sqllist1[0]['Yesterday_date']));
					$This_month						= $sqllist1[0]['This_month'];
					 $This_monthname = date("M", mktime(0, 0, 0, $This_month, 10));
					$Last_month						= $sqllist1[0]['Last_month'];
					$Last_month						= $sqllist1[0]['Last_month'];
					 $Last_monthname = date("M", mktime(0, 0, 0, $Last_month, 10));
					//$This_monthname						= $sqllist1[0]['This_monthname'];
					 $last_year						= $sqllist1[0]['last_year'];
					$This_year						= $sqllist1[0]['This_year'];
					$next_year						= $sqllist1[0]['next_year'];
					$This_month_quter2						= $sqllist1[0]['This_month_quter2'];
					$This_quter_monthname2 = date("M", mktime(0, 0, 0, $This_month_quter2, 10));
					$This_month_quter3						= $sqllist1[0]['This_month_quter3'];
					$This_quter_monthname3 = date("M", mktime(0, 0, 0, $This_month_quter3, 10));
					$Last_month_quter2						= $sqllist1[0]['Last_month_quter2'];
					$Last_quter_monthname2 = date("M", mktime(0, 0, 0, $Last_month_quter2, 10));
					$Last_month_quter3						= $sqllist1[0]['Last_month_quter3'];
					$Last_quter_monthname3 = date("M", mktime(0, 0, 0, $Last_month_quter3, 10));
					$Last_month_quter1						= $sqllist1[0]['Last_month_quter4'];
					$Last_quter_monthname1 = date("M", mktime(0, 0, 0, $Last_month_quter1, 10));

					if($This_month == '1')
					{
						$contain="Jan-Feb-Mar";
					}
					if($This_month == '2')
					{
						$contain="Jan-Feb-Mar";
					}
					if($This_month == '3')
					{
						$contain="Jan-Feb-Mar";
					}
					if($This_month == '4')
					{
						$contain="Apr-May-Jun";
					}
					if($This_month == '5')
					{
						$contain="Apr-May-Jun";
					}
					if($This_month == '6')
					{
						$contain="Apr-May-Jun";
					}
					if($This_month == '7')
					{
						$contain="Jul-Aug-Sept";
					}
					if($This_month == '8')
					{
						$contain="Jul-Aug-Sept";
					}
					if($This_month == '9')
					{
						$contain="Jul-Aug-Sept";
					}
					if($This_month == '10')
					{
						$contain="Oct-Nov-Dec";
					}
					if($This_month == '11')
					{
						$contain="Oct-Nov-Dec";
					}
					if($This_month == '12')
					{
						$contain="Oct-Nov-Dec";
					}
					if($Last_month_quter3 == '1')
					{
						$contain1="Jan-Feb-Mar";
					}
					if($Last_month_quter3 == '2')
					{
						$contain1="Jan-Feb-Mar";
					}
					if($Last_month_quter3 == '3')
					{
						$contain1="Jan-Feb-Mar";
					}
					if($Last_month_quter3 == '4')
					{
						$contain1="Apr-May-Jun";
					}
					if($Last_month_quter3 == '5')
					{
						$contain1="Apr-May-Jun";
					}
					if($Last_month_quter3 == '6')
					{
						$contain1="Apr-May-Jun";
					}
					if($Last_month_quter3 == '7')
					{
						$contain1="Jul-Aug-Sept";
					}
					if($Last_month_quter3 == '8')
					{
						$contain1="Jul-Aug-Sept";
					}
					if($Last_month_quter3 == '9')
					{
						$contain1="Jul-Aug-Sept";
					}
					if($Last_month_quter3 == '10')
					{
						$contain1="Oct-Nov-Dec";
					}
					if($Last_month_quter3 == '11')
					{
						$contain1="Oct-Nov-Dec";
					}
					if($Last_month_quter3 == '12')
					{
						$contain1="Oct-Nov-Dec";
					}


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