<?php
include('../config/config.php');
include('../classes/classProduct.php');
include('../classes/classReceiveInvoice.php');
include('../classes/classSaleInvoice.php');
include('../classes/classPatient.php');
include('../classes/classTransaction.php');
include('../classes/classProductClosingReconcilation.php');

try
{	
	$retrieveBalanceTrxOb = new classTransaction;

	if(isset($_POST['search_trx_by_date'])&&$_POST['search_trx_by_date']!='')
	{
		$trx_array = $retrieveBalanceTrxOb->retrieveTransactionDetailByDateAndPurpose($_POST['search_trx_by_date'],'balance',$_SESSION['security']['location_id']);
	}
	else
	{
		$retrieveBalanceTrxOb = new classTransaction;
		$retrieveBalanceTrxOb->setTransactionPurpose('balance');
		$retrieveBalanceTrxOb->setLocationID($_SESSION['security']['location_id']);
		$trx_array = $retrieveBalanceTrxOb->retrieveTransactionByPurpose();
	}
	$total_trx = 0;
	$total_balance_received = 0;
	$total_balance_remaining = 0;
	
	if(isset($trx_array[0]['transact_id'])&&$trx_array[0]['transact_id']!='')
	{
	  
	  for($int=0;$int<count($trx_array);$int++)
	  {
		  $total_trx ++;
		  $total_balance_received += $trx_array[$int]['payment'];
		  $total_balance_remaining += $trx_array[$int]['balance'];
	  }
	}

}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}

/*echo '<pre>';
print_r($_POST);
echo '</pre>';
exit;
*/
?>
<!DOCTYPE html>
<html><!-- InstanceBegin template="/Templates/main.dwt.php" codeOutsideHTMLIsLocked="false" -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head> 
    <link rel="shortcut icon" href="../images/favicon.png">
    <!-- InstanceBeginEditable name="doctitle" -->
    <title>Med2TEC Hospital Management Information System</title>
    <!-- InstanceEndEditable -->
    <!--Core CSS -->
    <link href="../bs3/css/bootstrap.min.css" rel="stylesheet">
    <link href="../assets/jquery-ui/jquery-ui-1.10.1.custom.min.css" rel="stylesheet">
    <link href="../css/bootstrap-reset.css" rel="stylesheet"> 
    <link href="../css/css/font-awesome.css" rel="stylesheet">
    <link href="../css/css/font-awesome.min.css" rel="stylesheet">
    <link href="../assets/jvector-map/jquery-jvectormap-1.2.2.css" rel="stylesheet">
    <link href="../css/clndr.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../assets/jquery-multi-select/css/multi-select.css" />
    <!--clock css-->
    <link href="../assets/css3clock/css/style.css" rel="stylesheet"> 
    <!--Morris Chart CSS -->
    <link rel="stylesheet" href="../assets/morris-chart/morris.css">
    <!-- Custom styles for this template -->
    <link href="../css/style.css" rel="stylesheet">
    <link href="../css/style-responsive.css" rel="stylesheet"/> 
    <link rel="stylesheet" href="../popUp/popUp.css" />
    <link href="../css/custom.css" rel="stylesheet"/>
    <link href="../css/bootstrap-fullcalendar.css" rel="stylesheet"/> 
     <link rel="stylesheet" href="../datepicker/jquery-ui.css">   
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>
<body>

<section id="container">
    <!--header start-->
    <header class="header fixed-top clearfix">
        <!--logo start-->
        <div class="brand">

            <a href="db.php" class="logo">
                <img src="../images/logo.png" alt="">
            </a>
            <div class="sidebar-toggle-box">
                <div class="fa fa-bars"></div>  
            </div>
        </div> 
        <!--logo end-->

        <div class="nav notify-row" id="top_menu" style="float: right; margin-top: 16px; margin-left: 0px; margin-right: 10px;">
            <!--  notification start -->
             <img alt="" src="../images/org_logos/<?php echo $_SESSION['security']['org_id']?>.png">
            <!--  notification end -->
        </div>
        <div class="top-nav clearfix">
            <!--search & user info start-->
            <ul class="nav pull-right top-menu">
                <li>
                    <div class="toggle-right-box"><!-- InstanceBeginEditable name="print" -->
                    <a href="javascript:Popup('printmgmtbalance.php')"><i class="fa fa-print" style="margin-left: 11px; margin-top: 11px;"></i></a>
					<!-- InstanceEndEditable --></div>
                </li>
                <!-- user login dropdown start-->
                <li class="dropdown">
                    <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                        <img alt="" src="../images/avatar1_small.jpg"> 
                        <span class="username"><?php echo $username;?></span>
                        <b class="caret"></b>
                    </a>
                    <ul class="dropdown-menu extended logout">
                        <li><a href="profile.php"><i class=" fa fa-suitcase"></i>Profile</a></li>
                        <li><a href="changepwd.php"><i class="fa fa-cog"></i> Change Password</a></li>
                        <li><a href="../logout.php"><i class="fa fa-key"></i> Log Out</a></li>
                    </ul>
                </li>
                <!-- user login dropdown end -->
                <li>
                    <div class="toggle-right-box">
                        <div class="fa fa-bars"></div>
                    </div>
                </li>
            </ul>
            <!--search & user info end-->
        </div>
    </header>
    <!--header end-->
    <!--sidebar start-->
   <aside>
        <div id="sidebar" class="nav-collapse">
            <!-- sidebar menu start-->
            <ul class="sidebar-menu" id="nav-accordion">
					  	<li>
                    	<a class="active" href="db.php" style="background:#0072BC;">
                        <i class="fa fa-dashboard"></i>
                        <span>Dashboard</span>
                    	</a>
                		</li>
					  <?php
					  
$orgOb = new classUser;
$orgOb->retrieveOrgDataByOrgID($_SESSION['security']['org_id']);
           
		  
				if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==5||$_SESSION['security']['userrole_id']==9||$_SESSION['security']['userrole_id']==10)
				{
					?>
					    <li class="sub-menu">
                    	<a href="dbpatient.php" style="background:#BC1C4A;">
                        <i class="fa fa-user"></i>
                        <span>Patient Registration</span>
                    	</a>
                    	</li>
					  <?php
				}
		  
		  if($orgOb->getOPD())
		  {
				if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==4||$_SESSION['security']['userrole_id']==5||$_SESSION['security']['userrole_id']==9)
				{
					?>
						<li class="sub-menu">
						<a href="dbopd.php" style="background:#003366;">
						<i class="fa fa-th"></i>
						<span>Out-Patient (OPD)</span>
						</a>
						</li>
						<?php
				}
		  }
		  if($orgOb->getIPD())
		  {
			  	?>
						<li class="sub-menu">
						<a href="dbipd.php" style="background:#3AC7F9;">
						<i class="fa fa-th"></i>
						<span>In-Patient (IPD)</span>
						</a>
						</li>
						<?php
					
		  }
		  if($orgOb->getOT())
		  {
			   		?>
						<li class="sub-menu">
						<a href="dbot.php" style="background:#D6502A;">
						<i class="fa fa-th"></i>
						<span>Operation Theater</span>
						</a>
						</li>
                        <?php
		  }			
		  if($orgOb->getPharmacy())
		  {
				if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==6||$_SESSION['security']['userrole_id']==7||$_SESSION['security']['userrole_id']==8||$_SESSION['security']['userrole_id']==9)
				{
			  		?>
						<li class="sub-menu">
						<a href="dbpharmacy.php" style="background:#00869C;">
						<i class="fa fa-th"></i>
						<span>Pharmacy</span>
						</a>
						</li>
						<?php
				}
		  }
		  if($orgOb->getDiagnostic())
		  {
				if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==14||$_SESSION['security']['userrole_id']==9)
				{
			  	?>
						<li>
						<a href="dbdiagnostics.php" style="background:#380E4D;">
						<i class="fa fa-th"></i>
						<span>Diagnostics </span>
						</a>
						</li>
						<?php
				}
		  }
		  if($orgOb->getEmergency())
		  {
					  ?>
						<li class="sub-menu">
						<a href="dbemergency.php" style="background:#8EC63F;">
						<i class="fa fa-tasks"></i>
						<span>Emergency </span>
						</a>
						</li>
						<?php
		  }
          if($orgOb->getAdmin())
		  {
				if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==9)
				{
					?>
						<li class="sub-menu">
						<a href="dbadmin.php" style="background:#28282E;">
						<i class="fa fa-laptop"></i>
						<span>Admin Panel</span>
						</a>
						</li>
						<?php
				}
		  }
		  ?>
               
            </ul> 
            <div style="position: absolute; bottom: 2%; padding-left:10%;"><a href="http://www.softusion.com/" target="_blank"><img width="30" height="40" src="../images/SoftusionLOGO.png" alt=""><span style="font-size: 11px; float: right; margin-top: 15%;">Designed by Softusion Inc.</span></a></div><br>
            <!-- sidebar menu end-->
        </div>
    </aside>
    <!--sidebar end-->
    <!--main content start-->
	<!-- InstanceBeginEditable name="center" -->
  <section id="main-content">
    <section class="wrapper">
    <table cellspacing="0" cellpadding="0" width="100%" border="0" style="font-size:11px;">
  <tbody>
    <tr>
      <td width="12%" align="center"><a href="dbpharmacy.php"><img src="../images/icondb.png"></a></td>
      <td width="12%" align="center"><a href="mgmtsales.php"><img src="../images/iconcash.png"></a></td>
      <td width="12%" align="center"><a href="pharmpresc.php"><img src="../images/iconpresc.png"></a></td>
      <td width="12%" align="center">
      <?php
	  $checkReportOb = new classProductClosingReconcilation;
	  if($checkReportOb->checkClosingReportAlreadyInProgress('in_progress',$_SESSION['security']['location_id']))
	  {
		  	?>
		 	<a href="pharmsaleinv.php?ref=<?php echo base64_encode('new');?>"><img src="../images/iconsale.png"></a>
		  	<?php
	  }
	  else
	  {
			?>
			<a href="#" onClick="return inventoryclosing();"><img src="../images/iconsale.png" /></a>
			<?php
	  }
	  ?>
      	
      </td>
      <td width="12%" align="center"><a href="mgmtrefunds.php"><img src="../images/iconrefund.png"></a></td>
      <td width="12%" align="center"><a href="mgmtbalance.php"><img src="../images/iconbalance.png"></a></td>
      <td width="12%" align="center"></td>
      <td width="12%" align="center"></td>
      <td width="2%" align="center">&nbsp;</td>
    </tr>
    <tr>
      <td align="center"><a href="dbpharmacy.php">Pharmacy</a></td>
      <td align="center"><a href="mgmtsales.php">Point of Sale</a></td>
      <td align="center"><a href="pharmpresc.php">e-Prescriptions</a></td>
      <td align="center">
      <?php
	  $checkReportOb = new classProductClosingReconcilation;
	  if($checkReportOb->checkClosingReportAlreadyInProgress('in_progress',$_SESSION['security']['location_id']))
	  {
		  	?>
		 	<a href="pharmsaleinv.php?ref=<?php echo base64_encode('new');?>">Create Sale Invoice</a>
		  	<?php
	  }
	  else
	  {
			?>
			<a href="#" onClick="return inventoryclosing();">Create Sale Invoice</a>
			<?php
	  }
	  ?>
      	
      </td> 
      <td align="center"><a href="mgmtrefunds.php">Payments & Refunds</a></td>
      <td align="center"><a href="mgmtbalance.php">Balance Payments</a></td>
      <td align="center"></td>
      <td align="center"></td>
      <td height="24" align="center">&nbsp;</td>
    </tr>
  </tbody>
</table>
<br> 
      <!-- page start-->
      <div class="row">
        <div class="col-sm-12">
          <section class="panel">
           <header class="panel-heading">
              <h2>BALANCE PAYMENTS</h2>
            </header>
           
            <table width="100%" border="0" cellspacing="5" cellpadding="5">
              <tr>
              
                <td width="10%" align="right" valign="bottom">Search By Date:</td>
                
                <td width="20%" valign="bottom">
               <form action="" method="post" id="form_balance">
                <input type="text" name="search_trx_by_date" value="" id="datepicker" class="form-control" readonly style="display:inline; width:90%;">
                </form>
                 
                </td>
                
                <td width="20%" align="left" valign="bottom"><a href="mgmtbalance.php">View All</a>
                </td>
                
                <td width="70%" align="right">
                <div class="col-md-4" style="float: right; width:60%;">
                <div class="profile-nav alt">
                  <section class="panel">
                    <div class="user-heading alt clock-row terques-bg">
                      <h1><?php echo date('d F Y',time());?></h1>
                      <p class="text-left"><?php echo date('h:i A',time());?></p></br>
                      <p class="text-left"></p>
                      <span>Total Transactions: <div style="float:right;"><?php echo $total_trx;?></div></span></br>
                      <span>Balance Received: <div style="float:right;">Rs. <?php echo $total_balance_received;?></div></span></br>
                      <span>Balance Remaining: <div style="float:right;">Rs. <?php echo $total_balance_remaining;?></div></span>
                    </div>
                  </section>
                </div>
          		</div>
          		</td>
                
              </tr>
            </table>
            <br>
            <br>
            <div class="panel-body" style="padding-top: 0px; margin-top: -34px;">
              <div class="adv-table">
                <table  class="display table table-bordered table-striped" id="dynamic-table">
                  <thead>
                    <tr>
                     <th width="5%" align="left">&nbsp;</th>
           			 <th width="10%" align="center">Date</th>
            		 <th width="5%" align="center">ID</th>
            		 <th width="20%" align="left">Party Name</th>
           		 	 <th width="20%" align="left">Account</th>
            		 <th width="15%" align="center">Amount Payable</th>
            		 <th width="15%" align="center">Payment</th>
                     <th width="10%" align="center">Balance</th>
                    </tr>
                  </thead>
                  <tbody>
					<?php
                    if(isset($trx_array[0]['transact_id'])&&$trx_array[0]['transact_id']!='')
                    {
						for($trx=0;$trx<count($trx_array);$trx++)
						{
							if($trx%2==0){ $class = 'gradeX';}else{$class = 'gradeC';}
							 
							$tranOb = new classTransaction;
							$tranOb->setAccountID($trx_array[$trx]['account_id']);
							$account_data = $tranOb->retrieveAccountByID();	
							$acc_name = $account_data[0]['account_name'];
							
							if($trx_array[$trx]['supplier_id']!=0)
							{
								$supplier_id = $trx_array[$trx]['supplier_id'];
								
								$suppOb = new classProduct;
								$supp_info = $suppOb->retrieveSupplierBySupplierID($supplier_id);
								$name = $supp_info[0]['company_name'];
								
								$supplier_id = (base64_encode($supplier_id));
								
								$history = "<a href='patientsupplierbalance.php?supplier_id=$supplier_id'>Payment History</a>";
							}
							else
							{
							
								if($trx_array[$trx]['patient_id']!=0)
								{
									$patient_id = $trx_array[$trx]['patient_id'];
									$retrievePatientOb =  new classPatient;
									$retrievePatientOb->setPatientAutoID($patient_id);
									$patient_single_detail = $retrievePatientOb->retrievePatientByPatientID();
									
									$name = $patient_single_detail[0]['patient_fname'].' '.$patient_single_detail[0]['patient_lname'];
									
									$patient_id = (base64_encode($patient_id));
								
									$history = "<a href='patientsupplierbalance.php?patient_id=$patient_id'>Payment History</a>";
								}
								else
								{
									$name = 'Out Door';
									$history = '';
								}
							}
							
							?>
							<tr class="<?php echo $class;?>">
								<td align="center"><?php echo $history;?></td>
								<td align="center"><?php echo date('d M Y',$trx_array[$trx]['transact_time']);?></td>
								<td align="center"><?php echo $trx_array[$trx]['transact_id'];?></td>
								<td align="left"><?php echo $name;?></td>
								<td align="left"><?php echo $acc_name;?></td>
								<td align="right" >Rs. <?php echo $trx_array[$trx]['amount_payable'];?></td>
								<td align="right" >Rs. <?php echo $trx_array[$trx]['payment'];?></td>
								<td align="right">Rs. <?php echo $trx_array[$trx]['balance'];?></td>
							</tr>
							<?php
						}
                    }
					else
				  	{
					?>
					<tr>
                        <table>
                            <tr>
                                <td>No entries found!</td>
                            </tr>
                        </table>
					</tr>
					<?php

				  }
                    ?>
                  </tbody>
                  <tfoot>
                  </tfoot>
                </table>
                
              </div>
            </div>
          </section>
        </div>
      </div>
      <!-- page end-->
    </section>
  </section>
  <!-- InstanceEndEditable --><!--main content end-->
<!--right sidebar start-->
 
<!--right sidebar end-->
</section>
<!-- Placed js at the end of the document so the pages load faster -->
<!--Core js-->
<!--<script src="../js/jquery-1.10.2.min.js"></script>-->
  
 
<script src="../assets/jquery-ui/jquery-ui-1.10.1.custom.min.js"></script>
<script src="../bs3/js/bootstrap.min.js"></script>



<script src="../js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="../popUp/popUp.js"></script>
<script src="../js/ajax.js"></script>

<script src="../js/lib/jquery.js"></script>
<script src="../assets/fullcalendar/fullcalendar/fullcalendar.min.js"></script>
<script src="../js/calendar/external-dragging-calendar.js"></script>
<script src="../js/accordion-menu/jquery.dcjqaccordion.2.7.js"></script>
<script src="../js/scrollTo/jquery.scrollTo.min.js"></script>
<script src="../js/nicescroll/jquery.nicescroll.js" type="text/javascript"></script>
<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="js/flot-chart/excanvas.min.js"></script><![endif]-->
<script src="../assets/jQuery-slimScroll-1.3.0/jquery.slimscroll.js"></script>
<script src="../assets/skycons/skycons.js"></script>
<script src="../assets/jquery.scrollTo/jquery.scrollTo.js"></script>
<script src="../js/jquery.easing.min.js"></script>
<script src="../assets/calendar/clndr.js"></script>
<script src="../js/underscore-min.js"></script>
<script src="../assets/calendar/moment-2.2.1.js"></script>
<script src="../js/calendar/evnt.calendar.init.js"></script>
<script src="../assets/jvector-map/jquery-jvectormap-1.2.2.min.js"></script>
<script src="../assets/jvector-map/jquery-jvectormap-us-lcc-en.js"></script>
<script src="../assets/gauge/gauge.js"></script>



<!--clock init-->
<script src="../assets/css3clock/js/script.js"></script>
<!--Easy Pie Chart-->
<script src="../assets/easypiechart/jquery.easypiechart.js"></script>
<!--Sparkline Chart-->
<!--Morris Chart-->
<script src="../assets/morris-chart/morris.js"></script>
<script src="../assets/morris-chart/raphael-min.js"></script>
<!--jQuery Flot Chart-->
<script src="../assets/flot-chart/jquery.flot.animator.min.js"></script>
<script src="../assets/flot-chart/jquery.flot.growraf.js"></script>
<script src="../js/dashboard.js"></script>
<script src="../js/custom-select/jquery.customSelect.min.js" ></script>
<!--common script init for all pages-->
<script src="../js/scripts.js"></script>
<!--dynamic table initialization -->
<script src="../js/dynamic_table/dynamic_table_init.js"></script>



<!--dynamic table initialization -->
<script src="../js/dynamic_table/dynamic_table_init.js"></script>
<!--<start===================>-->
<script src="../js/lib/jquery.js"></script>


<script src="../bs3/js/bootstrap.min.js"></script>



<script class="include" type="text/javascript" src="../js/accordion-menu/jquery.dcjqaccordion.2.7.js"></script>

<script src="../js/scrollTo/jquery.scrollTo.min.js"></script>
<script src="../js/nicescroll/jquery.nicescroll.js" type="text/javascript"></script>
<!--Easy Pie Chart-->
<script src="../assets/easypiechart/jquery.easypiechart.js"></script>
<!--Sparkline Chart-->
<script src="../assets/sparkline/jquery.sparkline.js"></script>
<!--jQuery Flot Chart-->
<script src="../assets/flot-chart/jquery.flot.js"></script>
<script src="../assets/flot-chart/jquery.flot.tooltip.min.js"></script>
<script src="../assets/flot-chart/jquery.flot.resize.js"></script>
<script src="../assets/flot-chart/jquery.flot.pie.resize.js"></script>
<script type="text/javascript" src="../assets/bootstrap-daterangepicker/daterangepicker.js"></script>
<script type="text/javascript" src="../assets/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>

<!--dynamic table-->
<!--common script init for all pages-->
<script src="../js/scripts.js"></script>

<!--dynamic table initialization -->
<script src="../js/dynamic_table/dynamic_table_init.js"></script>

<script type="text/javascript" src="../datepicker/jquery-1.9.1.js"></script>
<script type="text/javascript" src="../datepicker/jquery-ui.js"></script>

<!--<script type="text/javascript" src="../popUp/jquery-1.9.0.js"></script>-->
<script type="text/javascript">

 function Popup(apri) { 
	window.open(apri,"","top=10,left=200,width=800,height=700,status=no,menubar=no,toolbar=no,scrollbars=yes"); 
 }
</script> 

</body>
<!-- InstanceEnd --></html><script>
$(function() {
	$("#datepicker").datepicker({
	showOn: "button",
	buttonImage: "../images/calendar.gif",
	buttonImageOnly: true,
	dateFormat: "yy-mm-dd",
	
	onClose: function(dateText, inst) {
	
    $("#form_balance").submit();
} 
	/*minDate: '-1'*/
	});
});
</script>