<?php 
include 'dheader.php';
if (isset($_GET["delete"])) {
 	$remove = preg_replace('/[^a-z]/', "", strtolower($_GET["delete"]));
 	if ($remove == 's') {
 		$delete = $db->query("DELETE FROM payments2 WHERE cs_id = '".$_GET["delete"]."' AND id = '".$_GET["id"]."'");
 	}else{
 		$delete = $db->query("DELETE FROM payments WHERE cs_id = '".$_GET["delete"]."' AND id = '".$_GET["id"]."'");
	} 
}  
?>
	<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-2">
			<div class="input-group input-daterange" id="reportrange2">	
                <span class="input-group-addon bg">Only ?</span>
                <select name="only" class=" form-control" id="search-select">
                	<option value="" <?php echo ($_GET['only'] == "") ? 'selected':''; ?>>All</option>
                	<option value="1" <?php echo ($_GET['only'] == 1) ? 'selected':''; ?>>Suppliers</option>
                	<option value="2" <?php echo ($_GET['only'] == 2) ? 'selected':''; ?>>Customers</option>
                </select>
            </div>
		</div>
		<div class="col-md-2">
			<div class="input-group input-daterange" id="reportrange2">	
                <span class="input-group-addon bg">By</span>
                <select name="by" class=" form-control" id="search-select">
					<option value="All" <?php echo ($_GET['by'] == 'All') ? 'selected':''; ?>>All</option>
                	<option value="2" <?php echo ($_GET['by'] == 2) ? 'selected':''; ?>>Check</option>
                	<option value="1" <?php echo ($_GET['by'] == 1) ? 'selected':''; ?>>Cash</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'])) {
					?>
					<?php
						// Start date
						$sdate = $_GET['sdate'];
						$scus_date = explode('/', $sdate);
						$get_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 - Detailed Payments Report	<?php echo @$get_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>Ref#</th>
								<th>Name</th>
								<th>Vou</th>
								<th>Particulars</th>
								<th>Debit</th>
								<th>Credit</th>
								<th>Action</th>
							</tr>
						</thead>
						<tbody>
						<?php 
						if (isset($_GET['sdate'])) {
						?>
						<?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];

							$preBal = 0;
			                $receipts = 0;
			                $totalCustomerCredit = 0;
			                $totalCustomerDebit = 0;
			                $totalSupplierDebit = 0;
			                $totalSupplierCredit = 0;
			                while (strtotime($get_sdate) <= strtotime($get_edate)) {
							//echo "$get_sdate\n";
			                $get_sdate = date ("Y/m/d", strtotime("+1 day", strtotime($get_sdate)));

			                 
			                // Only Suppliers 
			                if($_GET['only'] == 1 || $_GET['only'] == ''){
			                //<!-- Supplier Payments -- >
				            $sql2 = "SELECT * FROM payments2 ";
				            $sql2 .= "WHERE php_date = '".$get_sdate."' ";
				           	if ($_GET['by'] == 'All') {
				           		# code...
				           	}else{
				           		$sql2 .= "AND type = '".$_GET['by']."' ";
				           	}
				            $sql2 .= "ORDER BY php_date, id";
				            $run2 = $db->query($sql2);
				            while($row2 = $run2->fetchArray()){
				            	if ($row2['debcre'] == 1) {
									$preBal -= $row2['amount'];
								}elseif ($row2['debcre'] == 2) {
									$preBal += $row2['amount'];
								}
							$receipts++;	
							?>	
								<tr>
									<td><?php echo $row2['show_date']; ?></td>
									<td><?php echo $row2['id']; ?></td>
									<?php
									$supID = preg_replace('/[^0-9]/', "", strtolower($row2['cs_id']));
									$selectCus = $db->query("SELECT * FROM supplier_setup WHERE id = '$supID'");
									$fCus = $selectCus->fetchArray();
									?>	
									<td><?php echo $fCus['supplier_name']; ?> - Supplier</td>
									<td><?php
										echo ($row2['type'] == '1') ? "Cash" : "Check";
										echo ($row2['bank'] != '') ? " on ".$row2['bank'] : " on hand";
									?></td>	
				                    <td><?php echo $row2['description']; ?></td>
				                <?php 
				                    if ($row2['debcre'] == 1) {
				                ?>    	
				                    <td>0</td>
				                    <td><?php echo $row2['amount']; ?></td>
				                    <?php $totalSupplierCredit += $row2['amount']; ?>
				                <?php 
				                	}elseif ($row2['debcre'] == 2) {   
				                ?>
				                    <td><?php echo $row2['amount']; ?></td>	
				                    <td>0</td>
				                <?php } ?>     
				                	<td><a href="editsupplierpay.php?edit=<?php echo $row2['id']; ?>&pay=<?php echo $row2['cs_id']; ?>" class="  update-person" title="Update" target="_blank">Edit</a> | <a href="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>&delete=<?php echo $row2['cs_id']; ?>&id=<?php echo $row2['id']; ?>" class="  delete-person" title="Delete" style="color: red;" onclick="return confirm('Are you sure you want to delete this supplier payment?');">Delete</a></td>
				                </tr>
				                <?php  } ?>
				            <?php } //End of only condition ?>
				                
				            <?php
				            // Only Customers
				            if($_GET['only'] == 2 || $_GET['only'] == ''){
			                // Customers Payments 
				            $sql2 = "SELECT * FROM payments ";
				            $sql2 .= "WHERE php_date = '".$get_sdate."' ";
				           	if ($_GET['by'] == 'All') {
				           		# code...
				           	}else{
				           		$sql2 .= "AND type = '".$_GET['by']."' ";
				           	}
				            $sql2 .= "ORDER BY php_date, id";
				            $run2 = $db->query($sql2);
				            while($row2 = $run2->fetchArray()){
				            	if ($row2['debcre'] == 1) {
									$preBal += $row2['amount'];
								}elseif ($row2['debcre'] == 2) {
									$preBal -= $row2['amount'];
								}
							$receipts++;	
							?>	
								<tr>
									<td><?php echo $row2['show_date']; ?></td>
									<td><?php echo $row2['id']; ?></td>
								<?php
									$cusID = preg_replace('/[^0-9]/', "", strtolower($row2['cs_id']));
									$selectCus = $db->query("SELECT * FROM customer_setup WHERE id = '$cusID'");
									$fCus = $selectCus->fetchArray();
								?>	
									<td><?php echo $fCus['customer_name']; ?> - Customer</td>
									<td><?php
										echo ($row2['type'] == '1') ? "Cash" : "Check";
										echo ($row2['bank'] != '') ? " on ".$row2['bank'] : " on hand";
									?></td>	
				                    <td><?php echo $row2['description']; ?></td>
				                <?php 
				                    if ($row2['debcre'] == 1) {
				                ?>    	
				                    <td>0</td>
				                    <td><?php echo $row2['amount']; ?></td>
				                    <?php $totalCustomerCredit += $row2['amount']; ?>
				                <?php 
				                	}elseif ($row2['debcre'] == 2) {   
				                ?>
				                    <td><?php echo $row2['amount']; ?></td>	
				                    <td>0</td>
				                <?php } ?>     
				                	<td><a href="editcustomerpay.php?edit=<?php echo $row2['id']; ?>&pay=<?php echo $row2['cs_id']; ?>" class="  update-person" title="Update" target="_blank">Edit</a> | <a href="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>&delete=<?php echo $row2['cs_id']; ?>&id=<?php echo $row2['id']; ?>" class="  delete-person" title="Delete" style="color: red;" onclick="return confirm('Are you sure you want to delete this customer payment?');">Delete</a></td>
				                </tr>
				            <?php  } ?>
				            <?php } //End of only condition ?>
				            


						<?php 
							 } // While Date ?>		
						<?php } // Date Set ?>		
							</tbody>
							<tr>
								<td colspan="8" align="center" style="color: green;">Total Receipts : <?php echo $receipts; ?> & Total amount : <?php echo $preBal; ?> <br>

									Total Payment received from customers : <?php echo $totalCustomerCredit; ?>
									<br>
									Total Payment Sended to suppliers : <?php echo $totalSupplierCredit; ?>
									</td>
							</tr>
						</table>			
				</div>	
		</div>	
				</div>
	</div>
</div>

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