<?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">
<style type="text/css">
	@media print {
		.table>thead>tr>td {
	    line-height: 1;
	    border: 1px solid #000;
			}

			.table>tbody>tr>td {
			    line-height: 1;
			    border: 1px solid #000;
			}
		}
	.table{
		font-size: 12px;
		padding: 0px;
	}
	.table>thead>tr>td {
	    line-height: 1;
	    border: 1px solid #000;
	}

	.table>tbody>tr>td {
	    line-height: 1;
	    border: 1px solid #000;
	}

	.manage-table tr td {
    height: 24px !important;
}
</style>
<form action="" method="get">
<div class="manage_buttons">
	<div class="row hidden-print">
	<div class="col-md-3">
			<div class="input-group input-daterange" id="reportrange2">
				
                <span class="input-group-addon bg">Name</span>
                <select name="name" class=" form-control" id="search-select">
				  <option value="">Supplier Name</option>
				  <?php
					$listNames = $db->query("SELECT * FROM supplier_setup ORDER BY supplier_name ASC");
					while($rowNames = $listNames->fetchArray()){
						$selected = "";
						if (isset($_GET["name"])) {
						 	$sup_id = preg_replace('/[^0-9]/', "", strtolower($_GET["name"]));
						 	if ($sup_id == $rowNames['id']) {
						 		$selected = "selected";
						 	}
						}    
				?>
					<option value="s<?php echo $rowNames['id']; ?>" <?php echo $selected; ?>><?php echo $rowNames['supplier_name']; ?></option>
				<?php } ?>
				</select>
            </div>
		</div>


		<div class="col-md-3">
			<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" name="sdate" id="sdate">
            </div>
		</div>
		<div class="col-md-3">
			<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" 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
				$cus_id = preg_replace('/[^0-9]/', "", strtolower(@$_GET["name"]));
				$listNames = $db->query("SELECT * FROM supplier_setup WHERE id = '$cus_id'");
				$rowNames = $listNames->fetchArray();  
				?>
					Supplier Ledger - <?php echo $rowNames['supplier_name']; ?>	
				<?php
					$sd = @$_GET['sdate'];
					$sd_date = explode('/', $sd);
					$sdget_date = @$sd_date[1].'/'.@$sd_date[0].'/'.@$sd_date[2];

					$eddate = @$_GET['edate'];
					$edcus_date = explode('/', $eddate);
					$edget_sdate = @$edcus_date[1].'/'.@$edcus_date[0].'/'.@$edcus_date[2];
				?>	
					<span style="float: right;">Date: <?php echo $sdget_date; ?> - <?php echo $edget_sdate; ?></span>		
					<span class="panel-options custom">
											</span>	
					
				</h3>

				<div class="row">
					<div class="col-md-4">
						Contact Person: <?php echo ($rowNames['email'])?$rowNames['email']:'-'; ?>	
					</div>
					<div class="col-md-4">
						Phone: <?php echo ($rowNames['phone'])?$rowNames['phone']:'-'; ?>	
					</div>
					<div class="col-md-4">
						Address/City: <?php echo $rowNames['address']; ?> <?php echo $rowNames['city']; ?>	
					</div>
					
				</div>
			</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>Vou</th>
								<th>Particulars</th>
								<th>Credit</th>
								<th>Debit</th>
								<th>Balance</th>
								<!-- <th>Action</th> -->
							</tr>
						</thead>
						<tbody>
						<?php 
						if (isset($_GET['sdate'])) {
						?>
						<?php
							// Start date
							$date = "2020/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)));
							
							//$endDate = strtotime($_GET['sdate'] . "-1 day");
							//$endDate = strftime("%Y/%m/%d",$endDate);
							//$amount = 0;
							//$remaining = 0;
							$preBal = 0;
							$totalDebit = 0;
							$totalCredit = 0;
							$open = "SELECT * FROM supplier_setup ";
							$open .= "WHERE id = '".$cus_id."' "; //AND  payment_type = 'Opening Balance' AND php_date = '".$date."'"
							$open_run = $db->query($open);
							while($open_row = $open_run->fetchArray()){
								$preBal += $open_row['open_balance'];
							}
							while (strtotime($date) <= strtotime($end_date)) {
							//echo "$date\n";
							$date = date ("Y/m/d", strtotime("+1 day", strtotime($date)));

							$sql = "SELECT DISTINCT invoiceNo FROM customerinvoice ";
							$sql .= "WHERE name = '".$_GET['name']."' ";
							//$sql .= "AND (date BETWEEN '".$_GET['sdate']."' AND '".$_GET['edate']."') ";
							$sql .= "AND php_date = '".$date."' 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') {
									$preBal -= $rem_row['remaining'];
									//$totalDebit += $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'Return') {
									$preBal += $rem_row['remaining'];
									//$totalDebit -= $rem_row['remaining'];
								}
							}
							
							$sqlpr = "SELECT DISTINCT invoiceNo FROM purchases ";
							$sqlpr .= "WHERE name = '".$_GET['name']."' ";
							//$sqlpr .= "AND (date BETWEEN '".$_GET['sdate']."' AND '".$_GET['edate']."') ";
							$sqlpr .= "AND php_date = '".$date."' 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') {
									$preBal += $rem_row['remaining'];
									//$totalDebit -= $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'PReturn') {
									$preBal -= $rem_row['remaining'];
									//$totalDebit -= $rem_row['remaining'];
								}
							}
							
								/*$q1 = "SELECT amount FROM payments ";
								$q1 .= "WHERE name = '".$_GET['name']."' AND action = 1 ";
								//$q1 .= "AND (date BETWEEN '".$strDate."' AND '".$endDate."') ";
								$q1 .= "AND php_date = '".$date."' ";
								$r1 = $db->query($q1);
								while($row1 = $r1->fetchArray()){
									 $amount += $row1['amount'];
								}*/

							$sql2 = "SELECT * FROM payments2 ";
				            $sql2 .= "WHERE cs_id = '".$_GET['name']."' AND action = '1' ";
				            $sql2 .= "AND php_date = '".$date."' ";
				            $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'];
								}
							}

						}
							
							//$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 $preBal;?></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)));

			                $open = "SELECT * FROM purchases ";
							$open .= "WHERE name = '".$_GET['name']."' AND  payment_type = 'Opening Balance' AND php_date = '".$get_sdate."'";
							$open_run = $db->query($open);
							while($open_row = $open_run->fetchArray()){
								$preBal += $open_row['remaining'];
							?>
							<td><?php echo $open_row['show_date']; ?></td>
		                	<td><?php echo '-'; ?></td>
		                	<td><?php echo '-'; ?></td>
		                    <td><?php echo 'Opening Balance'; ?></td>
		                    <td><?php echo $open_row['remaining']; ?></td>
		                    <td>0</td>
		                    <td><?php echo ($preBal < 1) ? $preBal." DR" : $preBal." CR"; ?></td>
		                   <!--  <td></td> -->
		                    <?php } ?>
							<?php
			 				$sql = "SELECT DISTINCT invoiceNo FROM purchases ";
							$sql .= "WHERE name = '".$_GET['name']."' ";
							//$sql .= "AND (date BETWEEN '".$_GET['sdate']."' AND '".$_GET['edate']."') ";
							$sql .= "AND php_date = '".$get_sdate."' AND payment_type != 'Opening Balance' AND sale_type IN ('Purchase', 'PReturn') ";
							$sql .= "ORDER BY date, id";
							
							$run = $db->query($sql);
							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') {
									$preBal += $rem_row['remaining'];
									$totalCredit += $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'PReturn') {
									$preBal -= $rem_row['remaining'];
									$totalCredit -= $rem_row['remaining'];
								}
								
							?>	
							<tr>
								<td><?php echo $rem_row['show_date']; ?></td>
								<td><?php echo ($rem_row['invoiceNo']) ? $rem_row['invoiceNo'] : '-'; ?></td>
								<td><?php echo ($rem_row['sale_type']) ? $rem_row['sale_type'] : 'Opening Balance'; ?></td>
								<?php
									if ($rem_row['sale_type'] == 'Purchase') { ?>
									
									<td><?php echo ($rem_row['description']) ? $rem_row['description'] : '-'; ?></td>
									<td><?php echo $rem_row['remaining']; ?></td>
									<td>0</td>
								<?php }elseif ($rem_row['sale_type'] == 'PReturn') { ?>
									<td><?php echo ($rem_row['description']) ? $rem_row['description'] : '-'; ?></td>
									<td>0</td>
									<td><?php echo $rem_row['remaining']; ?></td>
								<?php } ?>    

								<td><?php echo ($preBal < 1) ? $preBal." DR" : $preBal." CR"; ?></td>
								<!-- <td><?php if($rem_row['sale_type'] == 'Sale') { ?>
								<a href="sales.php?invoice=<?php echo $row['invoiceNo']; ?>" class="  update-person" title="Update" <?php echo (@$_SESSION['customeredit'] == '') ? 'style="display:none;"': ''; ?>>Edit</a>
								<?php } ?>
								<?php if($rem_row['sale_type'] == 'Purchase') { ?>
								<a href="purchase.php?invoice=<?php echo $row['invoiceNo']; ?>" class="  update-person" title="Update" <?php echo (@$_SESSION['customeredit'] == '') ? 'style="display:none;"': ''; ?>>Edit</a>
								<?php } ?>
								</td> -->
							</tr>    
							<?php } ?>
							<?php
			 				$sqlsl = "SELECT DISTINCT invoiceNo FROM customerinvoice ";
							$sqlsl .= "WHERE name = '".$_GET['name']."' ";
							//$sqlsl .= "AND (date BETWEEN '".$_GET['sdate']."' AND '".$_GET['edate']."') ";
							$sqlsl .= "AND php_date = '".$get_sdate."' AND payment_type != 'Opening Balance' AND sale_type IN ('Sale', 'Return') ";
							$sqlsl .= "ORDER BY date, id";
							
							$runsl = $db->query($sqlsl);
							while($rowsl = $runsl->fetchArray()){
								$remsl = "SELECT * FROM customerinvoice ";
								$remsl .= "WHERE invoiceNo = '".$rowsl['invoiceNo']."' ";
								$rem_runsl = $db->query($remsl);
								$rem_rowsl = $rem_runsl->fetchArray();

								if ($rem_rowsl['sale_type'] == 'Sale') {
									$preBal -= $rem_rowsl['remaining'];
									$totalDebit -= $rem_rowsl['remaining'];
								}elseif ($rem_rowsl['sale_type'] == 'Return') {
									$preBal += $rem_rowsl['remaining'];
									$totalDebit += $rem_rowsl['remaining'];
								}
								
						?>	
		            	<tr>
		                	<td><?php echo $rem_rowsl['show_date']; ?></td>
		                	<td><?php echo ($rem_rowsl['invoiceNo']) ? $rem_rowsl['invoiceNo'] : '-'; ?></td>
		                    <td><?php echo ($rem_rowsl['sale_type']) ? $rem_rowsl['sale_type'] : 'Opening Balance'; ?></td>
		                    <?php
		                    	if ($rem_rowsl['sale_type'] == 'Sale') {
		                    ?>
		                    	<td><?php echo ($rem_rowsl['description']) ? $rem_rowsl['description'] : '-'; ?></td>
			                    <td>0</td>
			                    <td><?php echo $rem_rowsl['remaining']; ?></td>
		                    <?php }elseif ($rem_rowsl['sale_type'] == 'Return') { ?>
		                    	<td><?php echo ($rem_rowsl['description']) ? $rem_rowsl['description'] : '-'; ?></td>
			                    <td><?php echo $rem_rowsl['remaining']; ?></td>
								<td>0</td>
			                <?php } ?>    

		                    <td><?php echo ($preBal < 1) ? $preBal." CR" : $preBal." DR"; ?></td>
		                    <?php /*	
		                    <td  class="hidden-print"><?php if($rem_row['sale_type'] == 'Sale') { ?>
		                    <a href="sales.php?invoice=<?php echo $row['invoiceNo']; ?>" class="  update-person" title="Update" <?php echo (@$_SESSION['customeredit'] == '') ? 'style="display:none;"': ''; ?>>Edit</a>
		                    <?php } ?>
		                    <?php if($rem_row['sale_type'] == 'Purchase') { ?>
		                    <a href="purchase.php?invoice=<?php echo $row['invoiceNo']; ?>" class="  update-person" title="Update" <?php echo (@$_SESSION['customeredit'] == '') ? 'style="display:none;"': ''; ?>>Edit</a>

		                    <?php } ?>
		                    */ 
		                    ?>
		                    </td>
		                </tr>    
						<?php } ?>
						
						<?php
				            $sql2 = "SELECT * FROM payments2 ";
				            $sql2 .= "WHERE cs_id = '".$_GET['name']."' AND action = '1' ";
				            $sql2 .= "AND php_date = '".$get_sdate."' ";
				            $sql2 .= "ORDER BY php_date, id";
				            $run2 = $db->query($sql2);
				            while($row2 = $run2->fetchArray()){
				            	if ($row2['debcre'] == 1) {
									$preBal -= $row2['amount'];
									$totalDebit += $row2['amount'];
								}elseif ($row2['debcre'] == 2) {
									$preBal += $row2['amount'];
									$totalCredit -= $row2['amount'];
								}
							?>	
								<tr>
									<td><?php echo $row2['show_date']; ?></td>
									<td><?php echo $row2['id']; ?></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 
				                	}elseif ($row2['debcre'] == 2) {   
				                ?>
				                    <td><?php echo $row2['amount']; ?></td>	
				                    <td>0</td>
				                <?php } ?>     
				                	<td><?php echo ($preBal < 1) ? $preBal." CR" : $preBal." DR"; ?></td>
				                </tr>
				            <?php  } ?>
						<?php 
							 } // While Date ?>		
						<?php } // Date Set ?>		
							</tbody>
							<tr>
								<td colspan="4">
								</td>
								
								<td>
										<p style="font-size: 12px; color: green;">Total Credit <?php echo $totalCredit; ?> </p>	
								</td>
								<td>
										<p style="font-size: 12px; color: green;">Total Debit <?php echo $totalDebit; ?> </p>	
								</td>
								<td>
								</td>
							</tr>
						</table>			
				</div>	
		</div>	
				</div>
	</div>
</div>

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