<?php 
include 'dheader.php'; 
if (isset($_GET["delete"])) {
 		$delete = $db->query("DELETE FROM other_accounts WHERE id = '".$_GET["delete"]."'");
}
?>
	<div class="modal fade hidden-print" id="myModal" tabindex="-1" role="dialog" aria-hidden="true"></div>
	<div class="wrapper ">
		<?php include 'leftbar.php'; ?>
		<!-- left-bar -->
		<?php include 'topbar.php'; ?>
			<!-- top-bar -->
			<div class="main-content">

<form action="" method="get">
<div class="manage_buttons hidden-print">
	<div class="row">
		
		<div class="col-md-3">
			<div class="input-group input-daterange" id="reportrange2">
				
                <span class="input-group-addon bg">Bank</span>
                <select name="bank" class=" form-control" id="search-select">
                <option value="<?php echo $_GET["bank"]; ?>"><?php echo $_GET["bank"]; ?></option>
				  <option value="All">All</option>
				  <?php
					$listNames = $db->query("SELECT * FROM bank");
					while($rowNames = $listNames->fetchArray()){
				?>
					<option value="<?php echo $rowNames['name']; ?>"><?php echo $rowNames['name']; ?></option>
				<?php } ?>
				</select>
            </div>
		</div>

		<div class="col-md-3">
			<div class="input-group input-daterange" id="reportrange2">
				
                <span class="input-group-addon bg">By</span>
                <select name="group" class=" form-control" id="search-select">
                  <option value="2" <?php echo ($_GET['group'] == 2) ? 'selected':''; ?>>Group</option>
				  <option value="1" <?php echo ($_GET['group'] == 1) ? 'selected':''; ?>>Individual</option>
				</select>
            </div>
		</div>
		
		<div class="col-md-2">
			<div class="input-group input-daterange" id="reportrange">
                <span class="input-group-addon bg">
                   From					                       	</span>
                <input type="text" class="form-control start_date datepicker" value="<?php echo $_GET['sdate']; ?>" name="sdate" id="sdate">
            </div>
		</div>

		<div class="col-md-2">
			<div class="input-group input-daterange" id="reportrange1">
                <span class="input-group-addon bg">
                    To			                                </span>
                <input type="text" class="form-control end_date datepicker" value="<?php echo $_GET['edate']; ?>" name="edate" id="edate">
            </div>	
		</div>
		<div class="col-md-2">
			<div class="input-group input-daterange" id="reportrange1">
		<button type="submit" title="Submit" class="btn btn-success">Submit</button>
			</div>
		</div>	
	</div>
</div>

</form>
	<div class="container-fluid">
		<div class="row manage-table">

	
			<div class="panel panel-piluku">
				<div class="panel-heading">
				<h3 class="panel-title">
				<?php
				if (isset($_GET['sdate'])) {
				
					$sdate = $_GET['sdate'];
					$scus_date = explode('/', $sdate);
					$sget_sdate = $scus_date[1].'/'.$scus_date[0].'/'.$scus_date[2];

					// End date
	             	$edate = $_GET['edate'];
					$ecus_date = explode('/', $edate);
					$get_edate = $ecus_date[1].'/'.$ecus_date[0].'/'.$ecus_date[2];
				}
				?>
					Reports - Bank Accounts Report	<?php echo @$sget_sdate; ?>-<?php echo @$get_edate; ?>		
					<span class="panel-options custom">
											</span>
				</h3>
			</div>
				<div class="panel-body nopadding table_holder table-responsive" >
					<table class="tablesorter table  table-hover" id="sortable_table">
						<thead>
							<tr>
								<th>Date</th>
								<th>ID</th>
								<th>Bank</th>
								<th>Description</th>
								<th>Recieve</th>
								<th>Pay</th>
								<th>Total</th>
							</tr>
						</thead>
						<tbody>
						<?php 
						if (isset($_GET['sdate'])) {
							if($_GET['group'] == 1){
						?>
						<?php
							// Start date
							$date = "2015/12/30";
							
			                // End date
							$sedate = $_GET['sdate'];
							$secus_date = explode('/', $sedate);
							$seget_sdate = $secus_date[2].'/'.$secus_date[0].'/'.$secus_date[1];
			                $end_date = date ("Y/m/d", strtotime("-2 day", strtotime($seget_sdate)));
							
							$i = 0;
							$invoices = 0;
							$payments = 0;
							$receive = 0;
							$grandtotal = 0;
							$supertotal = 0;

							$other_title = $db->query("SELECT * FROM bank WHERE name = '".$_GET['bank']."'");
							$fother_title = $other_title->fetchArray();

							$grandtotal = $fother_title['balance'];

							while (strtotime($date) <= strtotime($end_date)) {
							//echo "$date\n";
							$date = date ("Y/m/d", strtotime("+1 day", strtotime($date)));

							if($_GET['bank'] == 'All'){
								// Run All Queries
								$var = '';
							}
							else{
								$var = "WHERE name = '".$_GET['bank']."'";
							}

								
								while ($fother_title = $other_title->fetchArray()) {
								$sql = "SELECT * FROM payments WHERE bank = '".$fother_title['name']."' AND bank != '' AND php_date = '$date' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['debcre'] == 1) {
		            					$grandtotal += $row['amount'];
		            				}else{
		            					$grandtotal -= $row['amount'];
		            				}
		            			}

		            			$sql = "SELECT * FROM payments2 WHERE bank = '".$fother_title['name']."' AND bank != '' AND php_date = '$date' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['debcre'] == 1) {
		            					$grandtotal -= $row['amount'];
		            				}else{
		            					$grandtotal += $row['amount'];
		            				}
		            			}

		            			$sql = "SELECT * FROM other_accounts WHERE bank = '".$fother_title['name']."' AND type = 2 AND php_date = '$date' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['receive'] > 0) {
		            					$grandtotal += $row['receive'];
		            				}else{
		            					$grandtotal -= $row['payment'];
		            				}
		            			}

		            			$sql = "SELECT * FROM expenses WHERE bank = '".$fother_title['name']."' AND type = 2 AND php_date = '$date' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['receive'] > 0) {
		            					$grandtotal += $row['receive'];
		            				}else{
		            					$grandtotal -= $row['payment'];
		            				}
		            			}

		            			$sql = "SELECT * FROM bank_accounts WHERE bank = '".$fother_title['name']."' AND php_date = '$date' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['receive'] > 0) {
		            					$grandtotal += $row['receive'];
		            				}else{
		            					$grandtotal -= $row['payment'];
		            				}
		            			}


							}
						}
							
							//$preBal = $remaining - $amount;
						?>
		                <tr>
		                	<td>&nbsp;</td>
		                    <td>&nbsp;</td>
		                    <td>&nbsp;</td>
		                    <td>BALANCE B/F</td>
		                    <td>0</td>
		                    <td>0</td>
		                    <td><?php echo $grandtotal;?></td>
		                </tr>

						<?php
							// Start date
							$sdate = $_GET['sdate'];
							$scus_date = explode('/', $sdate);
							$get_sdate = $scus_date[2].'/'.$scus_date[0].'/'.$scus_date[1];
							$get_sdate = date ("Y/m/d", strtotime("-1 day", strtotime($get_sdate)));
			                
			                // End date
			             	$edate = $_GET['edate'];
							$ecus_date = explode('/', $edate);
							$get_edate = $ecus_date[2].'/'.$ecus_date[0].'/'.$ecus_date[1];
			                
			                while (strtotime($get_sdate) <= strtotime($get_edate)) {
							//echo "$get_sdate\n";
			                $get_sdate = date ("Y/m/d", strtotime("+1 day", strtotime($get_sdate)));
			            ?>  
			            <?php
			           		$sql = "SELECT * FROM payments WHERE bank = '".$_GET['bank']."' AND bank != '' AND php_date = '$get_sdate' ";
							$run = $db->query($sql);

	            			while($row = $run->fetchArray()){
	            				if ($row['debcre'] == 1) {
	            					$grandtotal += $row['amount'];
	            				}else{
	            					$grandtotal -= $row['amount'];
	            				}
		            	?>			
			            	<tr>
			            		<td><?php echo $row['date']; ?></td>
			            		<td><?php echo $row['id']; ?></td>
			            		<td><?php echo $row['bank']; ?></td>
			            		<td><?php echo $row['description']; ?></td>
			            	<?php if ($row['debcre'] == 1) { ?>
			            		<td><?php echo $row['amount']; ?></td>
			            		<td><?php echo "-"; ?></td>
			            	<?php }else{ ?>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo $row['amount']; ?></td>
			            	<?php } ?>	
			            		<td><?php echo $grandtotal; ?></td>
			            	</tr>
			            <?php } ?>
			            <?php
			           		$sql = "SELECT * FROM payments2 WHERE bank = '".$_GET['bank']."' AND bank != '' AND php_date = '$get_sdate' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['debcre'] == 1) {
		            					$grandtotal -= $row['amount'];
		            				}else{
		            					$grandtotal += $row['amount'];
		            				}
		            	?>			
			            	<tr>
			            		<td><?php echo $row['date']; ?></td>
			            		<td><?php echo $row['id']; ?></td>
			            		<td><?php echo $row['bank']; ?></td>
			            		<td><?php echo $row['description']; ?></td>
			            	<?php if ($row['debcre'] == 1) { ?>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo $row['amount']; ?></td>
			            	<?php }else{ ?>
			            		<td><?php echo $row['amount']; ?></td>
			            		<td><?php echo "-"; ?></td>
			            	<?php } ?>	
			            		<td><?php echo $grandtotal; ?></td>
			            	</tr>
			            <?php } ?>

			            <?php
			           		$sql = "SELECT * FROM other_accounts WHERE bank = '".$_GET['bank']."' AND type = 2 AND php_date = '$get_sdate' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['receive'] > 0) {
		            					$grandtotal += $row['receive'];
		            				}else{
		            					$grandtotal -= $row['payment'];
		            				}	
		            	?>			
			            	<tr>
			            		<td><?php echo $row['date']; ?></td>
			            		<td><?php echo $row['id']; ?></td>
			            		<td><?php echo $row['bank']; ?></td>
			            		<td><?php echo $row['description']; ?></td>
			            	<?php if ($row['receive'] > 0) { ?>
			            		<td><?php echo $row['receive']; ?></td>
			            		<td><?php echo "-"; ?></td>
			            	<?php }else{ ?>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo $row['payment']; ?></td>
			            	<?php } ?>	
			            		<td><?php echo $grandtotal; ?></td>
			            	</tr>
			            <?php } ?>

			            <?php
			           		$sql = "SELECT * FROM expenses WHERE bank = '".$_GET['bank']."' AND type = 2 AND php_date = '$get_sdate' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['receive'] > 0) {
		            					$grandtotal += $row['receive'];
		            				}else{
		            					$grandtotal -= $row['payment'];
		            				}	
		            	?>			
			            	<tr>
			            		<td><?php echo $row['date']; ?></td>
			            		<td><?php echo $row['id']; ?></td>
			            		<td><?php echo $row['bank']; ?></td>
			            		<td><?php echo $row['description']; ?></td>
			            	<?php if ($row['receive'] > 0) { ?>
			            		<td><?php echo $row['receive']; ?></td>
			            		<td><?php echo "-"; ?></td>
			            	<?php }else{ ?>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo $row['payment']; ?></td>
			            	<?php } ?>	
			            		<td><?php echo $grandtotal; ?></td>
			            	</tr>
			            <?php } ?>

			            <?php
			           			$sql = "SELECT * FROM bank_accounts WHERE bank = '".$_GET['bank']."' AND php_date = '$get_sdate' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['receive'] > 0) {
		            					$grandtotal += $row['receive'];
		            				}else{
		            					$grandtotal -= $row['payment'];
		            				}	
		            	?>			
			            	<tr>
			            		<td><?php echo $row['date']; ?></td>
			            		<td><?php echo $row['id']; ?></td>
			            		<td><?php echo $row['bank']; ?></td>
			            		<td><?php echo $row['description']; ?></td>
			            	<?php if ($row['receive'] > 0) { ?>
			            		<td><?php echo $row['receive']; ?></td>
			            		<td><?php echo "-"; ?></td>
			            	<?php }else{ ?>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo $row['payment']; ?></td>
			            	<?php } ?>	
			            		<td><?php echo $grandtotal; ?></td>
			            	</tr>
			            <?php } ?>

			            <?php } } ?>


			            <?php
			            if($_GET['group'] == 2){
			            	if($_GET['bank'] == 'All'){
								// Run All Queries
								$var = '';
							}
							else{
								$var = "WHERE name = '".$_GET['bank']."'";
							}

							$i = 0;
							$invoices = 0;
							$payments = 0;
							$receive = 0;
							$grandtotal = 0;
							$supertotal = 0;

								$other_title = $db->query("SELECT * FROM bank $var");
								while ($fother_title = $other_title->fetchArray()) {

								$grandtotal = $fother_title['balance'];
								$sql = "SELECT * FROM payments WHERE bank = '".$fother_title['name']."' AND bank != '' ";
								$run = $db->query($sql);

		            			
		            			while($row = $run->fetchArray()){
		            				if ($row['debcre'] == 1) {
		            					$grandtotal += $row['amount'];
		            				}else{
		            					$grandtotal -= $row['amount'];
		            				}
		            			}

		            			$sql = "SELECT * FROM payments2 WHERE bank = '".$fother_title['name']."' AND bank != '' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['debcre'] == 1) {
		            					$grandtotal -= $row['amount'];
		            				}else{
		            					$grandtotal += $row['amount'];
		            				}
		            			}

		            			$sql = "SELECT * FROM other_accounts WHERE bank = '".$fother_title['name']."' AND type = 2 ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['receive'] > 0) {
		            					$grandtotal += $row['receive'];
		            				}else{
		            					$grandtotal -= $row['payment'];
		            				}
		            			}

		            			$sql = "SELECT * FROM expenses WHERE bank = '".$fother_title['name']."' AND type = 2 ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['receive'] > 0) {
		            					$grandtotal += $row['receive'];
		            				}else{
		            					$grandtotal -= $row['payment'];
		            				}
		            			}

		            			$sql = "SELECT * FROM bank_accounts WHERE bank = '".$fother_title['name']."' ";
								$run = $db->query($sql);

		            			while($row = $run->fetchArray()){
		            				if ($row['receive'] > 0) {
		            					$grandtotal += $row['receive'];
		            				}else{
		            					$grandtotal -= $row['payment'];
		            				}
		            			}

		            		$i++;
		            		$invoices = $i; 
		            		$supertotal += $grandtotal;
						?>
							<tr>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo $fother_title['name']; ?></td>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo $grandtotal; ?></td>
			            	</tr>
						<?php	
								}
							}
						?>

						<?php } ?>
			            	<tr>
								<td colspan="11" align="center" style="color: green;">کل بینکوں : <?php echo $invoices; ?> & کل رقم : <?php echo $supertotal; ?></td>
							</tr>
							</tbody>
						</table>			
				</div>	
		</div>	
				</div>
	</div>
</div>

			
<?php include 'dfooter.php'; ?>