<?php
	include "db_connection/db_connection.php";
	include "config/config.php";
	
	if($_SESSION['admin_login_id'] == '')
	{
		echo "<script>window.location='index.php';</script>";
	}
	
	$check_acc  = check_access('42');
?>
<!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=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title><?php echo $PROJECT_TITLE ?></title>

<link href="css/styles.css" rel="stylesheet" type="text/css" />
<link href="css/new_paging.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!--[if IE]> <link href="css/ie.css" rel="stylesheet" type="text/css"> <![endif]-->

<!--[if IE]> <link href="css/ie.css" rel="stylesheet" type="text/css"> <![endif]-->

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

<script type="text/javascript" src="js/plugins/forms/ui.spinner.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.mousewheel.js"></script>
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script type="text/javascript" src="js/plugins/charts/excanvas.min.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.orderBars.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.pie.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.resize.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.sparkline.min.js"></script>

<script type="text/javascript" src="js/plugins/tables/jquery.dataTables.js"></script>
<script type="text/javascript" src="js/plugins/tables/jquery.sortable.js"></script>
<script type="text/javascript" src="js/plugins/tables/jquery.resizable.js"></script>

<script type="text/javascript" src="js/plugins/forms/autogrowtextarea.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.uniform.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.inputlimiter.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.tagsinput.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.maskedinput.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.autotab.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.chosen.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.dualListBox.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.cleditor.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.ibutton.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.validationEngine-en.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.validationEngine.js"></script>

<script type="text/javascript" src="js/plugins/uploader/plupload.js"></script>
<script type="text/javascript" src="js/plugins/uploader/plupload.html4.js"></script>
<script type="text/javascript" src="js/plugins/uploader/plupload.html5.js"></script>
<script type="text/javascript" src="js/plugins/uploader/jquery.plupload.queue.js"></script>

<script type="text/javascript" src="js/plugins/wizards/jquery.form.wizard.js"></script>
<script type="text/javascript" src="js/plugins/wizards/jquery.validate.js"></script>
<script type="text/javascript" src="js/plugins/wizards/jquery.form.js"></script>

<script type="text/javascript" src="js/plugins/ui/jquery.collapsible.min.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.breadcrumbs.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.tipsy.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.progress.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.timeentry.min.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.colorpicker.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.jgrowl.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.fancybox.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.fileTree.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.sourcerer.js"></script>

<script type="text/javascript" src="js/plugins/others/jquery.fullcalendar.js"></script>
<script type="text/javascript" src="js/plugins/others/jquery.elfinder.js"></script>

<script type="text/javascript" src="js/plugins/ui/jquery.easytabs.min.js"></script>
<script type="text/javascript" src="js/files/bootstrap.js"></script>
<script type="text/javascript" src="js/files/functions.js"></script>

<script type="text/javascript" src="js/charts/chart.js"></script>
<script type="text/javascript" src="js/charts/hBar_side.js"></script>
<script>
	function reload_page(ids)
	{
		window.location='collection_list.php?coll_id='+ids;
	}
</script>

<!-- Shared on MafiaShare.net  --><!-- Shared on MafiaShare.net  --></head>

<body>

<!-- Top line begins -->
<?php include "header_footer/header.php"; ?>
<!-- Top line ends -->


<!-- Sidebar begins -->
<?php include "menus/left_menu.php"; ?>
<!-- Sidebar ends -->
<!-- Content begins -->
<div id="content">
<?php include "menus/top_menu.php"; ?>
    <!-- Breadcrumbs line -->
    <div class="breadLine">
        <div class="bc">
            <ul id="breadcrumbs" class="breadcrumbs">
                <li><a href="home.php">Dashboard</a></li>
                <li><a href="javascript:">Collections</a></li>
            </ul>
        </div>
	<?php include "menus/top_left_menu.php"; ?>
	<?php 
	$disp='none';
		if(isset($_POST['sub_search1'])){
			$disp='block';
		}
		$disp_r = '25';
		if(isset($_POST['disp_rec'])!=''){
			$disp_r = $_POST['disp_rec'];
		}
		?>
    </div>
    <!-- Main content -->
    <div class="wrapper">
	<?php if($check_acc==1){?>
        <ul class="middleNavR">
            <!--<li><a href="#" title="Add an article" class="tipN"><img src="images/icons/middlenav/create.png" alt="" /></a></li>
            <li><a href="#" title="Upload files" class="tipN"><img src="images/icons/middlenav/upload.png" alt="" /></a></li>--> 
			<?php
			$des_list42 = check_rights('42','add');
			if($des_list42==1){ ?>
          <li><a href="collection_add.php" title="Add Collections" class="tipN"><img src="images/icons/middlenav/add.png" alt="" /></a></li>
			<?php } ?>
            <!--<li><a href="#" title="Messages" class="tipN"><img src="images/icons/middlenav/dialogs.png" alt="" /></a><strong>8</strong></li>-->
            <li><a href="cheque_collection_list.php" title="Cheque Collection" class="tipN"><img src="images/icons/middlenav/stats.png" alt="" /></a></li>
    	</ul>
    	<?php
		if(isset($_GET['coll_id']))
		{
			$coll_id =	$_GET['coll_id'];	
			$log_chk = base64_decode(base64_decode(base64_decode($coll_id)));
		}
		else
		{
			$coll_id = base64_encode(base64_encode(base64_encode('cash')));
			$log_chk = 'cash';
		}
		?>
    	<!-- Chart --><!-- 6 + 6 -->
		 <div class="fluid">
		<div class="widget">
        <div class="formRow">
            <div class="grid3"><label>Collection For:<span class="req">*</span></label></div>
            <div class="grid6 searchDrop">
			<select data-placeholder="" class="select" style="width:350px;" name="login_for" id="login_for"  data-rel="chosen" onChange="reload_page(this.value);">
				<option value=""></option>
                <?php
				$SLCT_SER = "SELECT * FROM ac_received_det GROUP BY payment_type";
				$QURY_SER = mysqli_query($connect,$SLCT_SER);
				while($FTCH_SER = mysqli_fetch_object($QURY_SER))
				{
					$adm_name = base64_encode(base64_encode(base64_encode($FTCH_SER->payment_type)));
					if($FTCH_SER->payment_type=='Bill')
					{
						$pay_tser = "Advance";
					}
					else if($FTCH_SER->payment_type=='neft')
					{
						$pay_tser = "NEFT";
					}
					else 
					{
						$pay_tser = ucfirst($FTCH_SER->payment_type);
					}
				?>
					<option <?php if($adm_name == $coll_id){ ?> selected="selected" <?php }?> value="<?php echo $adm_name;?>"><?php echo $pay_tser;?></option>
                <?php
				}
				?>
			</select>
            </div>
            <div class="clear"></div>
        </div>
        </div>
        </div>
    	<div class="widget"> <!--style="width:600px; margin-left:150px;"-->
        <div class="whead"><h6>Collections List</h6><h6 style="float:right; margin-right:5%;"><?php echo $disp_r;?> Entries</h6><div class="clear"></div>
		<div class="tablePars" style="display:<?php echo $disp;?>; padding:0px; border-top:1px solid #cdcdcd;" id="disp_search">
		<div style="float: left; margin-top: 10px;">
		<form name="cli_form" id="cli_form" action="collection_list.php" method="post">
		<table class="table" style="font-size:12px; color:#909090" id="myTable">
            <thead>
            <tr>
            <td style="text-align:center; vertical-align:middle; width:5%;">&nbsp;</td>
            <td style="text-align:left; vertical-align:middle;width:5%; padding:0px; "><input type="text" name="party_code" id="party_code" value="<?php echo @$_POST['party_code'];?>" placeholder="Party Code"></td>
			<td style="text-align:left; vertical-align:middle;width:5%; padding:0px; "><input type="text" name="party_name" id="party_name" value="<?php echo @$_POST['party_name'];?>" placeholder="Party Name"></td>
			<td style="text-align:left; vertical-align:middle;width:5%; padding:0px; "><input type="text" name="payment_date" id="payment_date" value="<?php echo @$_POST['payment_date'];?>" placeholder="Payment Date" size="8"></td>
            <td style="text-align:left; vertical-align:middle;width:5%; padding:0px;"><input type="submit" value="Submit" class="buttonM bBlack formSubmit" style="float: right; margin-left:20px;"></td>
            <td style="text-align:right; vertical-align:middle;"><select style="opacity:none; margin-right:10px;" name="disp_rec" id="disp_rec" onchange="$('#cli_form').submit();" aria-controls="dynamic" style="opacity: 0;"><option value="25" <?php if(isset($_POST['disp_rec'])=='25'){ echo "selected"; }?>>25</option><option value="50" <?php if(isset($_POST['disp_rec'])=='50'){ echo "selected"; }?>>50</option><option value="75" <?php if(isset($_POST['disp_rec'])=='75'){ echo "selected"; }?>>75</option><option value="100" <?php if(isset($_POST['disp_rec'])=='100'){ echo "selected"; }?>>100</option></select></td>
            </tr>
			</table>
		</div>
			</form>
		</div>
		</div>
        <div id="dyn" class="hiddenpars">
        	<a href="javascript:0" id="searchlink" onfocus="foc(this.id)" onblur="stnon(this.id)" class="tOptions" title="Options" onclick="$('#disp_search').slideToggle();"><img src="images/icons/options.png" alt="" /></a>
            <table class="table table-hover table-bordered" style="width:100%; font-size:12px; color:#909090" id="myTable">
            <thead>
            <tr>
            <th width="26" style="text-align:center; vertical-align:middle;">S.No<span class="sorting" style="display: block;"></span></th>
            <th width="76" style="text-align:left; vertical-align:middle;">Payment Type </th>
            <th width="85" style="text-align:left; vertical-align:middle;">Party Code</th>
            <th width="85" style="text-align:left; vertical-align:middle;">Party Name</th>
            <th width="86" style="text-align:center; vertical-align:middle;">Payment Date </th>
            <th width="82" style="text-align:center; vertical-align:middle;">Receiver/Bank</th>
            <th width="74" style="text-align:right; vertical-align:middle;">Received Amt</th>
            <th width="61" style="text-align:center; vertical-align:middle;">View</th>
            </tr>
            </thead>
            <tbody>
            <?php
			$where ='';

			if(@$_POST['bill_no']!=''){
				$where .= " and bill_no like'%".trim($_POST['bill_no'])."%'";
			}

			if(isset($_POST['company_name'])!=''){
				$where .= " and cmp_name like '%".trim($_POST['company_name'])."%'";
			}
			$startrec = 1;

			$no_rec = $disp_r;

			if(isset($_GET['page'])!=''){
				$page=$_GET['page'];
			} else {
				$page=1;
			}
			$limX = ($page-1) * $no_rec;
			$page_id= base64_encode(base64_encode(base64_encode(2))); //THIS IS FOR PAGE IDENTIFICATION IN BL FILE for active inactive

			//$SLCT = "SELECT * FROM ac_received_det ORDER BY STR_TO_DATE(received_date,'%d/%m/%Y') DESC";
			$SLCT = "SELECT * FROM ac_received_det WHERE payment_type='$log_chk' ORDER BY STR_TO_DATE(received_date,'%d-%m-%Y') DESC Limit ".$limX.",".$no_rec."";
			//echo $SLCT;
			$QURY = mysqli_query($connect,$SLCT);
			if($limX==0){
				$loop =1;
				$st_loop =1;
			} else {
				$loop = $limX;
				$st_loop = $limX;
			}
			if(mysqli_num_rows($QURY)>0)
			{
			while($FTCH = mysqli_fetch_object($QURY))
			{
				$acc_re_id	=	base64_encode(base64_encode(base64_encode($FTCH->ac_received_det_id)));
				$SLCT_b = "SELECT SUM(bill_amt) AS bill_amt,SUM(received_amt) AS received_amt,SUM(pending_amt) AS pending_amt FROM ac_bills_det WHERE  ac_received_det_id= '$FTCH->ac_received_det_id'";
				//echo $SLCT_b;
				 $QURY_b = mysqli_query($connect,$SLCT_b);
				 $FTCH_b = mysqli_fetch_object($QURY_b);

				 $SLCT_E = "SELECT * 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'"; 
				 //echo $SLCT_E."<br/>";
				 $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;
				}

				$pa_id	=	base64_encode(base64_encode(base64_encode($FTCH_E->party_det_id)));

				$pay_typ = $FTCH->payment_type;

				if($FTCH->payment_type=='Bill')
				{
					$re_name = "----"; 
					$pay_tp = "Advance";
				}
				else
				{
					if($FTCH->payment_type=="cash")
					{
						$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->emp_det_id'";
						$QURY_e = mysqli_query($connect,$SLCT_e);
						$FTCH_e = mysqli_fetch_object($QURY_e);

						$re_name = $FTCH_e->name; 
						$pay_tp = "Cash";
					}
					else if($FTCH->payment_type=="cheque")
					{
						$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->emp_det_id'";
						$QURY_e = mysqli_query($connect,$SLCT_e);
						$FTCH_e = mysqli_fetch_object($QURY_e);
						$re_name = $FTCH_e->name; 
						$pay_tp = "Cheque";
					}
					else if($FTCH->payment_type=="neft")
					{ 
						$re_name = $FTCH->bank_name; 
						$pay_tp = "NEFT";
					}
				}

				if($loop%2 != "0")
					$bg = "fff";
				else
					$bg = "f2f2f2";
			?>
            <tr style="background-color:#<?php echo $bg; ?>">
            <td align="center" style="vertical-align:middle;"><?php echo $loop ?></td>
            <td align="left" style="vertical-align:middle;"><?php echo $pay_tp; ?></td>
            <td align="left" style="vertical-align:middle;"><?php echo $FTCH_E->party_code; ?></td>
            <td align="left" style="vertical-align:middle;">
			<?php //echo $cmp_name; ?>
			<?php 
				$name_count = strlen($cmp_name);
				$name='';
				if($name_count>11){
					$name = substr($cmp_name,0,11)."...";
				} else {
					$name = $cmp_name;
				} ?>
			<?php if($name_count>11){?>
			<div class="tooltip1">
			<?php echo $name;?>
				<span class="tooltiptext1"><?php echo $cmp_name;?></span>
			</div>
			<?php } else { ?>
			<?php echo $name;?>
			<?php } ?>
			</td>
            <td align="center"  style="vertical-align:middle;"><?php echo str_replace("-", "/",$FTCH->received_date); ?></td>
            <td align="left" style="vertical-align:middle;">
			<?php //echo $re_name; ?>
			<?php 
				$name_count = strlen($re_name);
				$name='';
				if($name_count>11){
					$name = substr($re_name,0,11)."...";
				} else {
					$name = $re_name;
				} ?>
			<?php if($name_count>11){?>
			<div class="tooltip1">
			<?php echo $name;?>
				<span class="tooltiptext1"><?php echo $re_name;?></span>
			</div>
			<?php } else { ?>
			<?php echo $name;?>
			<?php } ?>
			</td>
            <?php /*?><td align="center"><?php echo $FTCH_b->bill_amt; ?></td><?php */?>
            <td align="right" style="vertical-align:middle;"><?php echo number_format($FTCH->received_amt,2); ?></td>
            <?php /*?><td align="center"><?php echo $FTCH->cred_amt; ?></td>
            <td align="center"><?php echo $FTCH_b->pending_amt; ?></td>
            <td align="center"><?php echo $FTCH->dis_amt; ?></td><?php */?>

            <td class="center" align="center" style="vertical-align:middle;"><a href="collection_view.php?acc_re_id=<?php echo $acc_re_id ?>&pa_id=<?php echo $pa_id ?>" class="an_own">View</a></td>
            </tr>

            <?php
			$loop ++;

			 }
			 } else { ?>
			 <tr><td colspan="10">There are no records to display.</td></tr>
			<?php  }
			// To find pagination 

			$SLCT_page = "SELECT * FROM ac_received_det WHERE payment_type='$log_chk' ".$where." ORDER BY STR_TO_DATE(received_date,'%d-%m-%Y') DESC";
			$QURY_page = mysqli_query($connect,$SLCT_page);
			$total_pages= mysqli_num_rows($QURY_page);
			$num_results_on_page = '25';//$disp_r;

			$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
			 ?>
            </tbody>
            </table> 
			<?php if (ceil($total_pages / $num_results_on_page) > 0): ?>
			<span id="paging">
			<ul class="pagination" style="float:right;">
				<?php if ($page > 1): ?>
				<li class="prev"><a href="collection_list.php?ot=<?php echo $ot;?>&page=<?php echo $page-1 ?>">Prev</a></li>
				<?php endif; ?>

				<?php if($page > 3): ?>
				<li class="start"><a href="collection_list.php?ot=<?php echo $ot;?>&page=1">1</a></li>
				<li class="dots">...</li>
				<?php endif;?>

				<?php if ($page-2 > 0): ?><li class="page"><a href="collection_list.php?ot=<?php echo $ot;?>&page=<?php echo $page-2 ?>"><?php echo $page-2 ?></a></li><?php endif; ?>
				<?php if ($page-1 > 0): ?><li class="page"><a href="collection_list.php?ot=<?php echo $ot;?>&page=<?php echo $page-1 ?>"><?php echo $page-1 ?></a></li><?php endif; ?>

				<li class="currentpage"><a href="collection_list.php?ot=<?php echo $ot;?>&page=<?php echo $page ?>"><?php echo $page ?></a></li>

				<?php if ($page+1 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href="collection_list.php?ot=<?php echo $ot;?>&page=<?php echo $page+1 ?>"><?php echo $page+1 ?></a></li><?php endif; ?>
				<?php if ($page+2 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href="collection_list.php?ot=<?php echo $ot;?>&page=<?php echo $page+2 ?>"><?php echo $page+2 ?></a></li><?php endif; ?>

				<?php if ($page < ceil($total_pages / $num_results_on_page)-2): ?>
				<li class="dots">...</li>
				<li class="end"><a href="collection_list.php?ot=<?php echo $ot;?>&page=<?php echo ceil($total_pages / $num_results_on_page) ?>"><?php echo ceil($total_pages / $num_results_on_page) ?></a></li>
				<?php endif; ?>

				<?php if ($page < ceil($total_pages / $num_results_on_page)): ?>
				<li class="next"><a href="collection_list.php?ot=<?php echo $ot;?>&page=<?php echo $page+1 ?>">Next</a></li>
				<?php endif; ?>
			</ul>
			</span>
<?php endif; ?>
        </div>
        </div>
        <div class="fluid"><!-- Media table --></div>
<?php } else {  echo ACCESS_MSG;} ?>

    </div>
    <!-- Main content ends -->
    
</div>
<!-- Content ends -->
<?php /*?><?php include "header_footer/footer.php"; ?><?php */?>
</body>
</html>
