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

<?php 
	if (isset($_GET["clear_customer"])) {
		$custo_name = urldecode($_GET['clear_customer']);
		$clear = $db->query("SELECT id FROM customer_setup WHERE customer_name = '$custo_name' ");
		$fetch_clear = $clear->fetchArray();
		// var_dump($fetch_clear);
		$date = date("m/d/Y");
		$ecus_date = explode('/', $date);
		$set_date = $ecus_date[2].'/'.$ecus_date[0].'/'.$ecus_date[1];

		$change_date = explode("/", $date);
		$show_date = $change_date[1]."/".$change_date[0]."/".$change_date[2];
		$php_date = $change_date[2]."/".$change_date[0]."/".$change_date[1];
		$pay = 'c'.$fetch_clear['id'];
		$type = 1;
		$amount = $_GET["clear_amount"];

		$clear_payment = $db->query("INSERT INTO payments (date, php_date, show_date, cs_id, type, amount, bank, description, action, debcre) 
		VALUES ('$date', '$php_date', '$show_date', '$pay', '$type', '$amount', '', 'Payment Clear', 1, 1)");

		header("LOCATION: all-customers-liabilities.php?name=&at=1&submit=Search");
	} 

	$at = $_GET['at'];
?>
	<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="row">
				<div class="col-sm-7">
			<div class="input-group input-daterange hidden-print" id="reportrange">
				
	                <span class="input-group-addon">
	                   Search By Name</span>
	                <input type="search" style="width: 420px;" class="form-control" list="searchlist" name="name" id="search">
	                <datalist id="searchlist">
	                <?php $query=$db->query("select * from customer_setup");
	                	while($rows=$query->fetchArray()){
	                 ?><option value="<?php echo $rows['customer_name'] ?>"></option>
	                	<?php } ?>
	                </datalist>
	               
	                
	            </div>
            </div> 

            				<div class="col-sm-5">
			<div class="input-group input-daterange hidden-print">
				
	                <span class="input-group-addon">
	                   Amount Type</span>
	                <select class="form-control" name="at" id="search">
	               		<option value="2">Without Zero Amount</option>
	               		<option value="1">With Zero Amount</option>
	                	
	                </select>

	                <input type="submit" class="btn btn-success" value="Search" style="height: 40px;" name="submit" id="submit">
	            </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">
				
					Reports - all customer liabilities
					<span class="panel-options custom">
											</span>
				</h3>
			</div>
			<?php if(isset($_GET['name'])){ ?>
				<div class="panel-body nopadding table_holder table-responsive" >
					<table class="tablesorter table  table-hover" id="sortable_table">
						<thead>
							<tr>
								<th>Customer Name</th>
								<th>Amount</th>
								<th>Action</th>
							</tr>
						</thead>
						<tbody>
						<?php
						$sname = ""; 
							if(@$_GET['name'] == ''){
								// $sname = "WHERE customer_name = * ";
							}else{
								$sname = "WHERE customer_name = '".$_GET['name']."' ";
							}

							$allS = "SELECT * FROM customer_setup $sname";
							$allSr = $db->query($allS);
							$allCustomerTotal = 0;
							$grand = 0;
							while($suppN = $allSr->fetchArray()){
							$preBal = 0;
							$open = "SELECT * FROM customer_setup ";
							$open .= "WHERE id = '".$suppN['id']."' ";
							$open_run = $db->query($open);
							while($open_row = $open_run->fetchArray()){
								$preBal += $open_row['open_balance'];
							}


							$sql = "SELECT DISTINCT invoiceNo FROM customerinvoice ";
							$sql .= "WHERE name = 'c".$suppN['id']."' ";
							//$sql .= "AND (date BETWEEN '".$_GET['sdate']."' AND '".$_GET['edate']."') ";
							$sql .= "AND payment_type != 'Opening Balance' ";
							$sql .= "ORDER BY id";
							
							$run = $db->query($sql);
							while($row = $run->fetchArray()){
								$rem = "SELECT * FROM customerinvoice ";
								$rem .= "WHERE invoiceNo = '".$row['invoiceNo']."' ";
								$rem_run = $db->query($rem);
								$rem_row = $rem_run->fetchArray();

								if ($rem_row['sale_type'] == 'Sale') {
									$preBal += $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'Purchase') {
									$preBal -= $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'Return') {
									$preBal -= $rem_row['remaining'];
								}
							}
							
						
							$sql2 = "SELECT * FROM payments ";
				            $sql2 .= "WHERE cs_id = 'c".$suppN['id']."' AND action = '1' ";
				            $sql2 .= "ORDER BY id";
				            $run2 = $db->query($sql2);
				            while($row2 = $run2->fetchArray()){
				            	if ($row2['debcre'] == 1) {
									$preBal -= $row2['amount'];
								}elseif ($row2['debcre'] == 2) {
									$preBal += $row2['amount'];
								}
							}
							
								$grand += $preBal;


								if($_GET['at'] == 2 && $preBal < 1)
								{
									continue;
								}
							?>
							<tr>
								<td><?php echo $suppN['customer_name']; ?></td>
								<td><?php echo $preBal; ?></td>
								<td style="color: red;">
									
									<a href="?name=&at=<?php echo $at; ?>&clear_amount=<?php echo $preBal; ?>&clear_customer=<?php echo urlencode($suppN['customer_name']); ?>" id="pay-person-btn" class="btn btn-success btn-lg" title="Add Payments"><span class="" onclick="return confirm('Are you sure you want to clear this customer payment?');">Clear Payments</span></a>

									<a href="paycustomer.php" target="_blank" id="pay-person-btn" class="btn btn-danger btn-lg" title="Add Payments"><span class="">Add Payment</span></a>
								</td>
							</tr>
							<?php } ?>
							<tr>
								<td colspan="3" align="center" style="color: green;">
Total customer liabilities : <?php echo $grand; ?></td>
							</tr>
						</tbody>
						</table>			
				</div>	
				<?php } ?>
		</div>	
				</div>
	</div>
</div>

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