<?php 
include 'dheader.php'; 
?>
	<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" value="">
	                <datalist id="searchlist">
	                <?php $query=$db->query("select * from supplier_setup");
	                	while($rows=$query->fetchArray()){
	                 ?><option value="<?php echo $rows['supplier_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 supplier bills
		
					<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>Supplier Name</th>
								<th>Amount</th>
							</tr>
						</thead>
						<tbody>
						<?php
						$sname = ""; 
							if(@$_GET['name'] == ''){
								// $sname = "WHERE customer_name = * ";
							}else{
								$sname = "WHERE supplier_name = '".$_GET['name']."' ";
							}

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


							$sql = "SELECT DISTINCT invoiceNo FROM customerinvoice ";
							$sql .= "WHERE name = 's".$suppN['id']."' ";
							//$sql .= "AND (date BETWEEN '".$_GET['sdate']."' AND '".$_GET['edate']."') ";
							$sql .= " AND payment_type != 'Opening Balance' AND sale_type IN ('Sale', 'Return') ";
							$sql .= "ORDER BY date, 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') {
									// echo 'sl'.
									$preBal -= $rem_row['remaining'];
									//$totalDebit += $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'Return') {
									// echo 'sr'.
									$preBal += $rem_row['remaining'];
									//$totalDebit -= $rem_row['remaining'];
								}
							}
							
							$sqlpr = "SELECT DISTINCT invoiceNo FROM purchases ";
							$sqlpr .= "WHERE name = 's".$suppN['id']."' ";
							//$sqlpr .= "AND (date BETWEEN '".$_GET['sdate']."' AND '".$_GET['edate']."') ";
							$sqlpr .= " AND payment_type != 'Opening Balance' AND sale_type IN ('Purchase', 'PReturn') ";
							$sqlpr .= "ORDER BY date, id";
							
							$run = $db->query($sqlpr);
							while($row = $run->fetchArray()){
								$rem = "SELECT * FROM purchases ";
								$rem .= "WHERE invoiceNo = '".$row['invoiceNo']."' ";
								$rem_run = $db->query($rem);
								$rem_row = $rem_run->fetchArray();

								if ($rem_row['sale_type'] == 'Purchase') {
									// echo 'pr'.
									$preBal += $rem_row['remaining'];
									//$totalDebit -= $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'PReturn') {
									// echo 'rp'.
									$preBal -= $rem_row['remaining'];
									//$totalDebit -= $rem_row['remaining'];
								}
							}
							
							$sql2 = "SELECT * FROM payments2 ";
				            $sql2 .= "WHERE cs_id = 's".$suppN['id']."' AND action = '1' ";
				            $sql2 .= "ORDER BY id";
				            $run2 = $db->query($sql2);
				            while($row2 = $run2->fetchArray()){
				            	if ($row2['debcre'] == 1) {
									// echo 'pd'.
									$preBal -= $row2['amount'];
								}elseif ($row2['debcre'] == 2) {
									// echo 'pc'.
									$preBal += $row2['amount'];
								}
							}
							
								$grand += $preBal;

								if($_GET['at'] == 2 && $preBal < 1)
								{
									continue;
								}
							?>
							<tr>
								<td><?php echo $suppN['supplier_name']; ?></td>
								<td><?php echo $preBal; ?></td>
							</tr>
							<?php } ?>
							<tr>
								<td colspan="3" align="center" style="color: green;">Total liabilities Supplier :
 <?php echo $grand; ?></td>
							</tr>
						</tbody>
						</table>			
				</div>
				<?php } ?>	
		</div>	
				</div>
	</div>
</div>

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