<?php
	include "db_connection/db_connection.php";
	include "config/config.php";
	
	if($_SESSION['admin_login_id'] == '')
	{
		echo "<script>window.location='index.php';</script>";
	}
	else // THIS IS FOR GETTING PARTY DET ID ROM LOAGIN DET AND PARTY DET TABLE
	{
		$admin_id = $_SESSION['admin_login_id'];
		
		$SLCT_log = "SELECT * FROM login_details,ac_party_det WHERE ac_party_det.party_det_id = login_details.party_det_id AND login_details.admin_id = '$admin_id'";
		//echo $SLCT_log;
		$QURY_log = mysqli_query($connect,$SLCT_log);
		$FTCH_log = mysqli_fetch_object($QURY_log);
		$party_id = '$FTCH_log->party_det_id';
	}
	
	$flag = 0;
   	$sta_iden = '0';
   	// THIS IS FOR CHECKING THE STAGE NAME
	if(isset($_GET['sta_name']) && $_GET['sta_name']!='' ) 
	{
		$mas_stage_id 	= 	$_GET['sta_name'];
		$mas_stage_id1	=	base64_decode(base64_decode(base64_decode(base64_decode($_GET['sta_name'])))); // this is for getting the value from url				
	}
	else
	{
		$mas_stage_id = '';
		$mas_stage_id1 = '';
	}
	
	if(isset($_GET['flg']) && $_GET['flg']!='' ) 
	{
		$flg_id 	= 	$_GET['flg'];
		$flg_id1	=	base64_decode(base64_decode(base64_decode(base64_decode($_GET['flg'])))); // this is for getting the value from url				
	}
	else
	{
		$flg_id = '';
		$flg_id1 = '';
	}
	
	if(isset($_GET['fdate']) && $_GET['fdate']!='' ){ $fdate = $_GET['fdate'];	}else{ $fdate = ''; }
	if(isset($_GET['todate']) && $_GET['todate']!='' ){ $todate = $_GET['todate'];	}else{ $todate = ''; }				
	if(isset($_GET['yea_r']) && $_GET['yea_r']!='' ){ $yea_r = 'checked="checked"'; $y_r = $_GET['yea_r'];}else{ $yea_r = ''; $y_r = '';}
	if(isset($_GET['mont_h']) && $_GET['mont_h']!='' ){ $mont_h = 'checked="checked"'; $m_h = $_GET['mont_h'];}else{ $mont_h = ''; $m_h = ''; }
	if(isset($_GET['da_y']) && $_GET['da_y']!='' ){ $da_y = 'checked="checked"'; $d_y = $_GET['da_y'];}else{ $da_y = ''; $d_y = ''; }
	
	if($flg_id1==2)
	{
		if(isset($_GET['mnthyr']) && $_GET['mnthyr']!='')
		{ 			
			$mnthyr = explode("-",base64_decode(base64_decode(base64_decode(base64_decode($_GET['mnthyr'])))));
			$mn_no = $mnthyr[0];
			$mn_nm = $mnthyr[1];
			$yr_nm = $mnthyr[2];
			$dat_rslt = "01-".$mn_no."-".$yr_nm; 
		}
		else
		{
			$mn_no = '';
			$mn_nm = '';
			$yr_nm = '';
			$dat_rslt = '';
		}
		$prnt_pag_flg = 1;
	}
	else
	{
		$mn_no = '';
		$mn_nm = '';
		$yr_nm = '';
		$dat_rslt = '';
		$prnt_pag_flg = 2;
	}
	
	if($mas_stage_id1 == '1') // THIS IS FOR BILLING CONDITION
	{ 
		$bil_tit1 = 'Total Net Amt';
		$bil_tit2 = 'Total Tax Amt';
		$bil_tit3 = 'Total Courier Amt';
		$bil_tit4 = 'Total Round Off';
		$bil_tit5 = 'Total Bill Amt';
		$lbl = "Billing";
	}
	else if($mas_stage_id1 == '2') // THIS IS FOR COLLECTION CONDITION
	{ 
		$bil_tit1 = 'Total Advance Amt';
		$bil_tit2 = 'Total Cash Amt';
		$bil_tit3 = 'Total Cheque Amt';
		$bil_tit4 = 'Total NEFT Amt';
		$bil_tit5 = 'Total Colletion';
		$lbl = "Collection";
	}
	$tot_net_amt = 0;
	$tot_tax_amt = 0;
	$tot_cor_amt = 0;
	$tot_rnd_amt = 0;
	$tot_bil_amt = 0;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Report</title>
		<script type="text/javascript" src="js/acc_analize_JS.js"></script>
		<style>
			.tet_bx{ border:none; background:#FFFFFF;}
		</style>
	</head>
	<body onload="tot_count();">
		<table width="960" border="0" align="center" style="outline:#000000 solid 1px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;">
			<?php 
			if($prnt_pag_flg==2)
			{
			?>
				<tr><th colspan="10" scope="col">Accounts Analize for <?php echo $lbl; ?> Report</th></tr>
				<tr><td colspan="10">&nbsp;</td></tr>
				<tr>
					<td colspan="2">&nbsp;From Date: <?php echo $fdate; ?></td>
					<td>&nbsp;</td>
					<td colspan="2">&nbsp;To Date: <?php echo $todate; ?></td>
					<td>&nbsp;Filter:</td>
					<td colspan="4">&nbsp;<?php if($yea_r!=''){ echo "Year"; } ?>&nbsp;-
						<?php if($mont_h!=''){ echo "Month"; } ?>&nbsp;-
						<?php if($da_y!=''){ echo "Day"; } ?>&nbsp;
					</td>
					<td>&nbsp;<!--Qty :&nbsp; <input type="text" id="tot_qty" name="tot_qty" value="" readonly="readonly" disabled="disabled" style="width:50px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" />--></td>
				</tr>
			<?php
			}
			else if($prnt_pag_flg==1)
			{
			?>
				<tr><th colspan="10" scope="col">Accounts Analize for <?php echo $lbl; ?> Report - &nbsp;<?php echo $mn_nm." - ".$yr_nm; ?></th></tr>
				<tr><td colspan="10">&nbsp;</td></tr>
			<?php
			}
			?>
			<tr><td colspan="10">&nbsp;</td></tr>
			<tr style="text-align:center">
				<td colspan="2"><?php echo $bil_tit1; ?></td>
				<td colspan="2"><?php echo $bil_tit2; ?></td>
				<td colspan="2"><?php echo $bil_tit3; ?></td>
				<td colspan="2"><?php echo $bil_tit4; ?></td>
				<td colspan="2"><?php echo $bil_tit5; ?></td>
			</tr>
			<tr style="text-align:center">
				<td colspan="2"><input type="text" class="tet_bx" id="stot_bl" name="tot_bl" value="" readonly="readonly" disabled="disabled" style="width:80px; font-size:12px; text-align:center" />&nbsp;</td>
				<td colspan="2"> <input type="text" class="tet_bx" id="stot_rec" name="stot_rec" value="" readonly="readonly" disabled="disabled" style="width:80px; font-size:12px; text-align:center" />      &nbsp;</td>
				<td colspan="2"><input type="text" class="tet_bx" id="stot_dis" name="stot_dis" value="" readonly="readonly" disabled="disabled" style="width:80px; font-size:12px; text-align:center" />&nbsp;</td>
				<td colspan="2"><input type="text" class="tet_bx" id="stot_pe" name="stot_pe" value="" readonly="readonly" disabled="disabled" style="width:80px; font-size:12px; text-align:center" />&nbsp;</td>
				<td colspan="2"><input type="text" class="tet_bx" id="stot_adv" name="stot_adv" value="" readonly="readonly" disabled="disabled" style="width:80px; font-size:12px; text-align:center" />&nbsp;</td>
			</tr>
			<tr>
			<td colspan="10">&nbsp;</td>
			</tr>
		</table>
		<?php
		if($flg_id1==1) // THIS IS PRINT SCREEN FLAGG VALUE FOR BILLING / COLLECTION MONTH WISE
		{
			$billing_date = "billing_det.bill_date"; 
			$collect_date = "AC.received_date"; 
			$dat_str_bill = ''; //THIS IS FOR QUERY STRING
			$dat_str_coll = '';	//THIS IS FOR QUERY STRING								
			if($fdate!='' && $todate!='')
			{
				$dat_str_bill = "WHERE STR_TO_DATE(".$billing_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; 
				$dat_str_coll = "WHERE STR_TO_DATE(".$collect_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; 
				$flgg=1;				
			}
			else if($fdate=='' && $todate!='')
			{
				if($yea_r!='' && $mont_h!='' && $da_y!='')
				{ 
					$dat_str_bill = "WHERE STR_TO_DATE(".$billing_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 
					$dat_str_coll = "WHERE STR_TO_DATE(".$collect_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')";
					$flgg = 1;				
				}									
				else if($yea_r!='' && $mont_h!='' && $da_y=='')
				{
					$vl = explode("/",$todate);
					$todat_vl = $vl[2]."-".$vl[1]."-".$vl[0];
				 	$dat_str_bill = "WHERE DATE_FORMAT(STR_TO_DATE(".$billing_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')";  
					$dat_str_coll = "WHERE DATE_FORMAT(STR_TO_DATE(".$collect_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')";  
					$flgg = 1;					
				}									
				else if($yea_r!='' && $mont_h=='' && $da_y=='')
				{ 
					$vl = explode("/",$todate);
					$todat_vl = ($vl[2]-1)."-12-31";
					$dat_str_bill = "WHERE YEAR(STR_TO_DATE(".$billing_date.",'%d-%m-%Y')) = YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; 				
					$dat_str_coll = "WHERE YEAR(STR_TO_DATE(".$collect_date.",'%d-%m-%Y')) = YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; 				
					$flgg = 1;
				}									
				else if($yea_r=='' && $mont_h=='' && $da_y=='')
				{ 
					$dat_str_bill = "WHERE STR_TO_DATE(".$billing_date.",'%d-%m-%Y') <= STR_TO_DATE('$todate','%d/%m/%Y')"; 				
					$dat_str_coll = "WHERE STR_TO_DATE(".$collect_date.",'%d-%m-%Y') <= STR_TO_DATE('$todate','%d/%m/%Y')";
					
					$flgg = 0;
				}									
				else if($yea_r=='' && $mont_h!='' && $da_y=='')
				{ 
					$vl = explode("/",$todate);
					$todat_vl = $vl[2]."-".$vl[1]."-".$vl[0];
					$dat_str_bill = "WHERE DATE_FORMAT(STR_TO_DATE(".$billing_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')";  
					$dat_str_coll = "WHERE DATE_FORMAT(STR_TO_DATE(".$collect_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')";  
					$flgg = 1;
				}									
				else if($yea_r=='' && $mont_h=='' && $da_y!='')
				{ 
					$dat_str_bill = "WHERE STR_TO_DATE(".$billing_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 
					$dat_str_coll = "WHERE STR_TO_DATE(".$collect_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 				
					$flgg = 1;
				}									
				else if($yea_r!='' && $mont_h=='' && $da_y!='')
				{ 
					$dat_str_bill = "WHERE STR_TO_DATE(".$billing_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 
					$dat_str_coll = "WHERE STR_TO_DATE(".$collect_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 					
					$flgg = 1;
				}									
				else if($yea_r=='' && $mont_h!='' && $da_y!='')
				{ 
					$dat_str_bill = "WHERE STR_TO_DATE(".$billing_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 				
					$dat_str_coll = "WHERE STR_TO_DATE(".$collect_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 					
					$flgg = 1;
				}
			}
		?>
			<table width="960" border="0" cellspacing="0" align="center" style="outline:#000000 solid 1px;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;">
				<thead>
					<tr>
							<?php 
								if($mas_stage_id1 == '1') // THIS IS FOR BILLING CONDITION
								{ 
							?>
									<th width="10%">S.No<span class="sorting" style="display: block;"></span></th>
									<th width="20%">Month</th>
									<th width="10%">Count</th>
									<th width="12%">Net Amount</th>
									<th width="12%">Tax Amount</th>
									<th width="12%">Courier Amount</th>
									<th width="12%">Round off</th>
									<th width="12%">Total Amount</th>
							<?php 
								} 
								else if($mas_stage_id1 == '2') // THIS IS FOR COLLECTION CONDITION
								{ 
							?>
									<th width="10%">S.No<span class="sorting" style="display: block;"></span></th>
									<th width="20%">Month</th>
									<th width="10%">Count</th>
									<th width="12%">Advance</th> 
									<th width="12%">Cash</th>
									<th width="12%">Cheque</th>
									<th width="12%">NEFT</th>
									<th width="12%">Total</th>
							<?php 
								} 
							?>
						</tr>
				</thead>
				<tbody>  
					<?php 
						$tot_net_amt = 0;
							$tot_tax_amt = 0;
							$tot_cor_amt = 0;
							$tot_rnd_amt = 0;
							$tot_bil_amt = 0;
							if($mas_stage_id1 == '1') // THIS IS FOR BILLING CONDITION
							{ 
								$SLCT = "SELECT COUNT(billing_det_id) As cont, bill_no,bill_type,party_det_id,bill_date,EXTRACT(MONTH FROM STR_TO_DATE(bill_date,'%d-%m-%Y')) AS mnth_no,MONTHNAME(STR_TO_DATE(bill_date,'%d-%m-%Y')) AS mnth_nm, EXTRACT( YEAR FROM STR_TO_DATE(bill_date,'%d-%m-%Y')) AS year_nm, SUM(bill_amt) AS bill_amt, SUM(bill_tot_tax_amt) AS bill_tot_tax_amt,SUM(replace(replace(round_off,')',''),'(','')) AS round_off,SUM(courier_amt) AS courier_amt FROM `billing_det` ".$dat_str_bill." GROUP BY EXTRACT(YEAR_MONTH FROM STR_TO_DATE(bill_date,'%d-%m-%Y')) ORDER BY EXTRACT(YEAR_MONTH FROM STR_TO_DATE(bill_date,'%d-%m-%Y')) ASC";
								$QURY = mysqli_query($connect,$SLCT);
								$loop=1;
								if(mysqli_num_rows($QURY)>0)
								{			
									while($FTCH = mysqli_fetch_object($QURY))
									{	
										$rnd_off = str_replace(array( '(', ')' ), '', $FTCH->round_off);		
										if($rnd_off!='')
										{
											$bl_amt = $FTCH->bill_amt-($FTCH->bill_tot_tax_amt+$rnd_off)-$FTCH->courier_amt;
											$rnd_lbl = $FTCH->round_off;
										}
										else if($rnd_off=='')
										{
											$bl_amt = $FTCH->bill_amt - $FTCH->bill_tot_tax_amt - $FTCH->courier_amt;
											$rnd_lbl = "0.00";				
										}														
						?>
										<tr>
											<td align="center" height="30" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $loop; ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $FTCH->mnth_nm." - ".$FTCH->year_nm; ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $FTCH->cont; ?></td>
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($bl_amt,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->bill_tot_tax_amt,2); ?></td>
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->courier_amt,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($rnd_lbl,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->bill_amt,2); ?></td>
										</tr>
						<?php 
										$tot_net_amt = $tot_net_amt + $bl_amt;
										$tot_tax_amt = $tot_tax_amt + $FTCH->bill_tot_tax_amt;
										$tot_cor_amt = $tot_cor_amt + $FTCH->courier_amt;
										$tot_rnd_amt = $tot_rnd_amt + $rnd_off;
										$tot_bil_amt = $tot_bil_amt + $FTCH->bill_amt;
										$loop++;
									}
								}
							} 
							else if($mas_stage_id1 == '2') // THIS IS FOR COLLECTION CONDITION
							{ 
								$SLCT = "SELECT COUNT(*) AS cont, AC.`ac_party_det_id`, EXTRACT(MONTH FROM STR_TO_DATE(AC.`received_date`,'%d-%m-%Y')) AS mnth_no, MONTHNAME(STR_TO_DATE(AC.`received_date`,'%d-%m-%Y')) AS mnth_nm, EXTRACT( YEAR FROM STR_TO_DATE(AC.`received_date`,'%d-%m-%Y')) AS year_nm, AC.`received_date`, IFNULL(SUM(AC1.`received_amt`),'0') AS advance, IFNULL(SUM(AC2.`received_amt`),'0') AS cash, IFNULL(SUM(AC3.`received_amt`),'0') AS cheque, IFNULL(SUM(AC4.`received_amt`),'0') AS neft,(IFNULL(SUM(AC1.`received_amt`),'0')+IFNULL(SUM(AC2.`received_amt`),'0')+IFNULL(SUM(AC3.`received_amt`),'0')+IFNULL(SUM(AC4.`received_amt`),'0')) AS tot_amt
										FROM `ac_received_det` AS AC
									    LEFT JOIN ac_received_det AC1 ON AC.ac_received_det_id=AC1.ac_received_det_id AND AC1.`payment_type`='Bill'
									    LEFT JOIN ac_received_det AC2 ON AC.ac_received_det_id=AC2.ac_received_det_id AND AC2.`payment_type`='cash'
									    LEFT JOIN ac_received_det AC3 ON AC.ac_received_det_id=AC3.ac_received_det_id AND AC3.`payment_type`='cheque'
									    LEFT JOIN ac_received_det AC4 ON AC.ac_received_det_id=AC4.ac_received_det_id AND AC4.`payment_type`='neft' ".$dat_str_coll." GROUP BY EXTRACT(YEAR_MONTH FROM STR_TO_DATE(AC.received_date,'%d-%m-%Y')) ORDER BY EXTRACT(YEAR_MONTH FROM STR_TO_DATE(AC.received_date,'%d-%m-%Y')) ASC";
								$QURY = mysqli_query($connect,$SLCT);
								$loop=1;
								if(mysqli_num_rows($QURY)>0)
								{			
									while($FTCH = mysqli_fetch_object($QURY))
									{				
										$SLCT_E = "SELECT party_det.party_det_id,party_det.party_code,party_det.cmp_name FROM ac_party_det,party_det WHERE party_det.party_det_id = ac_party_det.party_det_id AND ac_party_det.ac_party_det_id = '$FTCH->ac_party_det_id'"; 
										$QURY_E = mysqli_query($connect,$SLCT_E);
										$FTCH_E = mysqli_fetch_object($QURY_E);

										$SLCT_part = "SELECT cmp_name FROM party_det_log WHERE party_det_id = '$FTCH_E->party_det_id' AND STR_TO_DATE(REPLACE('$FTCH->received_date','/','-'),'%d-%m-%Y') BETWEEN STR_TO_DATE(`active_date`,'%Y-%m-%d') AND STR_TO_DATE(`inactive_date`,'%Y-%m-%d')";
										$QURY_part = mysqli_query($connect,$SLCT_part);
										if(mysqli_num_rows($QURY_part)>0)
										{
											$FTCH_part = mysqli_fetch_object($QURY_part);
											$cmp_name  = $FTCH_part->cmp_name;
										}
										else
										{
											$cmp_name  = $FTCH_E->cmp_name;
										}
						?>
										<tr>
											<td align="center"  height="30" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $loop; ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $FTCH->mnth_nm." - ".$FTCH->year_nm; ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $FTCH->cont; ?></td>
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->advance,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->cash,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->cheque,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->neft,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->tot_amt,2); ?></td> 											
										</tr>
						<?php 
										$tot_net_amt = $tot_net_amt + $FTCH->advance;
										$tot_tax_amt = $tot_tax_amt + $FTCH->cash;
										$tot_cor_amt = $tot_cor_amt + $FTCH->cheque;
										$tot_rnd_amt = $tot_rnd_amt + $FTCH->neft;
										$tot_bil_amt = $tot_bil_amt + $FTCH->tot_amt;
										$loop++;
									}
								}
							} 
						?>        
				</tbody>    
			</table>
		<?php		
		}
		else if($flg_id1==2) // THIS IS PRINT SCREEN FLAGG VALUE FOR BILLING / COLLECTION MONTH VIEW 
		{
			$billing_date = "billing_det.bill_date"; 
			$collect_date = "AC.received_date"; 
			$dat_str_bill = ''; //THIS IS FOR QUERY STRING
			$dat_str_coll = '';	//THIS IS FOR QUERY STRING	
			$dat_str_bill = "WHERE DATE_FORMAT(STR_TO_DATE(".$billing_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$dat_rslt','%d-%m-%Y'),'%m-%Y')";  
			$dat_str_coll = "WHERE DATE_FORMAT(STR_TO_DATE(".$collect_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$dat_rslt','%d-%m-%Y'),'%m-%Y')";  
				
		?>
			<table width="960" border="0" align="center" cellspacing="0" style="outline:#000000 solid 1px;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;">
				<thead>
					<tr>
						<?php 
							if($mas_stage_id1 == '1') // THIS IS FOR BILLING CONDITION
							{ 
						?>
								<th height="30" width="6%">S.No<span class="sorting" style="display: block;"></span></th>
								<th width="25%">Party Code/ Name</th>
								<th width="12%">Bill No</th> 
								<th width="9%">Bill Date</th>
								<th width="12%">Bill Amt</th>
								<th width="8%">Tax Amt</th>
								<th width="8%">Courier</th>
								<th width="7%">Round off</th>
								<th width="12%">Total Amt</th>
						<?php 
							} 
							else if($mas_stage_id1 == '2') // THIS IS FOR COLLECTION CONDITION
							{ 
						?>
								<th  height="30" width="10%">S.No<span class="sorting" style="display: block;"></span></th>
								<th width="30%">Party Code/Name</th>
								<th width="10%">Payment Dt</th>
								<th width="10%">Advance</th> 
								<th width="10%">Cash</th>
								<th width="10%">Cheque</th>
								<th width="10%">NEFT</th>
								<th width="10%">Total Amt</th>
						<?php 
							} 
						?>
					</tr>
					<tr><th colspan="9">&nbsp;</th></tr>
				</thead>
				<tbody>  
					<?php 
						$tot_net_amt = 0;
							$tot_tax_amt = 0;
							$tot_cor_amt = 0;
							$tot_rnd_amt = 0;
							$tot_bil_amt = 0;
							if($mas_stage_id1 == '1') // THIS IS FOR BILLING CONDITION
							{ 	
								$SLCT = "SELECT bill_no,bill_type,party_det_id,bill_date,bill_amt,bill_tot_tax_amt,round_off,courier_amt FROM `billing_det` ".$dat_str_bill." ORDER BY STR_TO_DATE(bill_date,'%d-%m-%Y') ASC,bill_no ASC, `party_det_id` ASC ";
								$QURY = mysqli_query($connect,$SLCT);
								$loop=1;
								if(mysqli_num_rows($QURY)>0)
								{			
									while($FTCH = mysqli_fetch_object($QURY))
									{
										$pa_id = $FTCH->party_det_id;
										
										$SLCT_PT = "SELECT party_code, cmp_name FROM party_det WHERE party_det_id = '$pa_id'";
										$QURY_PT = mysqli_query($connect,$SLCT_PT);
										$FTCH_PT = mysqli_fetch_object($QURY_PT);

										$SLCT_part = "SELECT cmp_name FROM party_det_log WHERE party_det_id = '$pa_id' AND STR_TO_DATE(REPLACE('$FTCH->bill_date','/','-'),'%d-%m-%Y') BETWEEN STR_TO_DATE(`active_date`,'%Y-%m-%d') AND STR_TO_DATE(`inactive_date`,'%Y-%m-%d')";
										$QURY_part = mysqli_query($connect,$SLCT_part);
										if(mysqli_num_rows($QURY_part)>0)
										{
											$FTCH_part = mysqli_fetch_object($QURY_part);
											$cmp_name  = $FTCH_part->cmp_name;
										}
										else
										{
											$cmp_name  = $FTCH_PT->cmp_name;
										}
										
										$rnd_off = str_replace(array( '(', ')' ), '', $FTCH->round_off);		
										//echo $rnd_off;
										if($rnd_off!='')
										{
											$bl_amt = $FTCH->bill_amt-($FTCH->bill_tot_tax_amt+$rnd_off)-$FTCH->courier_amt;
											$rnd_lbl = $FTCH->round_off;
										}
										else if($rnd_off=='')
										{
											$bl_amt = $FTCH->bill_amt - $FTCH->bill_tot_tax_amt - $FTCH->courier_amt;
											$rnd_lbl = "0.00";				
										}
						?>
										<tr>
											<td align="center"  height="30" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $loop; ?></td> 
											<td align="left" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $FTCH_PT->party_code." / ".$cmp_name; ?></td> 
											<td align="left" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $FTCH->bill_no; ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo str_replace("-", "/", $FTCH->bill_date); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($bl_amt,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->bill_tot_tax_amt,2); ?></td>
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->courier_amt,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $rnd_lbl; ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->bill_amt,2); ?></td>  
										</tr>
						<?php 
										$tot_net_amt = $tot_net_amt + $bl_amt;
										$tot_tax_amt = $tot_tax_amt + $FTCH->bill_tot_tax_amt;
										$tot_cor_amt = $tot_cor_amt + $FTCH->courier_amt;
										$tot_rnd_amt = $tot_rnd_amt + $rnd_off;
										$tot_bil_amt = $tot_bil_amt + $FTCH->bill_amt;
										$loop++;
									}
								}
							} 
							else if($mas_stage_id1 == '2') // THIS IS FOR COLLECTION CONDITION
							{ 
								$SLCT = "SELECT AC.`ac_party_det_id`, AC.`received_date`, IFNULL(AC1.`received_amt`,'0') AS advance, IFNULL(AC2.`received_amt`,'0') AS cash, IFNULL(AC3.`received_amt`,'0') AS cheque, IFNULL(AC4.`received_amt`,'0') AS neft,(IFNULL(AC1.`received_amt`,'0')+IFNULL(AC2.`received_amt`,'0')+IFNULL(AC3.`received_amt`,'0')+IFNULL(AC4.`received_amt`,'0')) AS tot_amt
										FROM `ac_received_det` AS AC
									    LEFT JOIN ac_received_det AC1 ON AC.ac_received_det_id=AC1.ac_received_det_id AND AC1.`payment_type`='Bill'
									    LEFT JOIN ac_received_det AC2 ON AC.ac_received_det_id=AC2.ac_received_det_id AND AC2.`payment_type`='cash'
									    LEFT JOIN ac_received_det AC3 ON AC.ac_received_det_id=AC3.ac_received_det_id AND AC3.`payment_type`='cheque'
									    LEFT JOIN ac_received_det AC4 ON AC.ac_received_det_id=AC4.ac_received_det_id AND AC4.`payment_type`='neft' ".$dat_str_coll." ORDER BY STR_TO_DATE(AC.`received_date`,'%d-%m-%Y') ASC";
								$QURY = mysqli_query($connect,$SLCT);
								$loop=1;
								if(mysqli_num_rows($QURY)>0)
								{			
									while($FTCH = mysqli_fetch_object($QURY))
									{				
										$SLCT_E = "SELECT party_det.party_det_id,party_det.party_code,party_det.cmp_name FROM ac_party_det,party_det WHERE party_det.party_det_id = ac_party_det.party_det_id AND ac_party_det.ac_party_det_id = '$FTCH->ac_party_det_id'"; 
										$QURY_E = mysqli_query($connect,$SLCT_E);
										$FTCH_E = mysqli_fetch_object($QURY_E);

										$SLCT_part = "SELECT cmp_name FROM party_det_log WHERE party_det_id = '$FTCH_E->party_det_id' AND STR_TO_DATE(REPLACE('$FTCH->received_date','/','-'),'%d-%m-%Y') BETWEEN STR_TO_DATE(`active_date`,'%Y-%m-%d') AND STR_TO_DATE(`inactive_date`,'%Y-%m-%d')";
										$QURY_part = mysqli_query($connect,$SLCT_part);
										if(mysqli_num_rows($QURY_part)>0)
										{
											$FTCH_part = mysqli_fetch_object($QURY_part);
											$cmp_name  = $FTCH_part->cmp_name;
										}
										else
										{
											$cmp_name  = $FTCH_E->cmp_name;
										}
						?>
										<tr>
											<td align="center" height="30" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $loop; ?></td> 
											<td align="left" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo $FTCH_E->party_code." / ".$cmp_name; ?></td>  
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo str_replace("-", "/", $FTCH->received_date); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->advance,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->cash,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->cheque,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->neft,2); ?></td> 
											<td align="center" style="border-bottom:0.1px solid gray;display:table-cell;vertical-align:middle"><?php echo number_format($FTCH->tot_amt,2); ?></td> 
										</tr>
						<?php 
										$tot_net_amt = $tot_net_amt + $FTCH->advance;
										$tot_tax_amt = $tot_tax_amt + $FTCH->cash;
										$tot_cor_amt = $tot_cor_amt + $FTCH->cheque;
										$tot_rnd_amt = $tot_rnd_amt + $FTCH->neft;
										$tot_bil_amt = $tot_bil_amt + $FTCH->tot_amt;
										$loop++;
									}
								}
							} 
						?>        
				</tbody>    
			</table>
		<?php	
		}
		?>
		<input name="tot_net_amt" id="tot_net_amt" type="hidden" value="<?php echo number_format($tot_net_amt,2); ?>" />
		<input name="tot_tax_amt" id="tot_tax_amt" type="hidden" value="<?php echo number_format($tot_tax_amt,2); ?>" />	 
		<input name="tot_cor_amt" id="tot_cor_amt" type="hidden" value="<?php echo number_format($tot_cor_amt,2); ?>" />	 
		<input name="tot_rnd_amt" id="tot_rnd_amt" type="hidden" value="<?php echo number_format($tot_rnd_amt,2); ?>" />	
		<input name="tot_bil_amt" id="tot_bil_amt" type="hidden" value="<?php echo number_format($tot_bil_amt,2); ?>" />
	</body>
</html>
