<?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="">Customer Name</option>
				  <?php
					$listNames = $db->query("SELECT * FROM customer_setup ORDER BY UPPER(customer_name) ASC");
					while($rowNames = $listNames->fetchArray()){
						$selected = "";
						if (isset($_GET["name"])) {
						 	$cus_id = preg_replace('/[^0-9]/', "", strtolower($_GET["name"]));
						 	if ($cus_id == $rowNames['id']) {
						 		$selected = "selected";
						 	}
						}    
				?>
					<option value="c<?php echo $rowNames['id']; ?>" <?php echo $selected; ?>><?php echo $rowNames['customer_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" autocomplete="off" 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" autocomplete="off" 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>
	<br>
	<div class="container-fluid">
	
<?php 
		$storeTitle = $db->query("SELECT * FROM store");
		$storeName = $storeTitle->fetchArray();
	?>
	<center><u><h1><?php echo $storeName['name']; ?></h1></u></center>
		<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 customer_setup WHERE id = '$cus_id'");
				$rowNames = $listNames->fetchArray();  
				?>
					Customer Ledger - <?php echo $rowNames['customer_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 table-bordered" id="sortable_table">
						<thead>
							<tr>
								<th>Date</th>
								<th>Ref#</th>
								<th>Vou</th>
								<th>Particulars</th>
								<th>Debit</th>
								<th>Credit</th>
								<th>Balance</th><!-- 
								<th class="hidden-print">Edit</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;
							$cus_id = preg_replace('/[^0-9]/', "", strtolower($_GET["name"]));
							$open = "SELECT * FROM customer_setup ";
							$open .= "WHERE id = '".$cus_id."' ";
							$open_run = $db->query($open);//AND  payment_type = 'Opening Balance' AND php_date = '".$date."'
							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' ";
							$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'] == 'Purchase') {
									$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' ";
							$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'] == 'Sale') {
									$preBal += $rem_row['remaining'];
									//$totalDebit += $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'Purchase') {
									$preBal -= $rem_row['remaining'];
									//$totalDebit -= $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'Return') {
									$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 payments ";
				            $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'];
									//$totalCredit += $row2['amount'];
								}elseif ($row2['debcre'] == 2) {
									$preBal += $row2['amount'];
									//$totalCredit -= $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 customerinvoice ";
							$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'];
							?>
							<tr>
								<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." CR" : $preBal." DR"; ?></td>
			                    <td></td>
			                </tr>    
		                    <?php } ?>
							*/?>
							<?php
			 				$sql = "SELECT DISTINCT invoiceNo FROM customerinvoice ";
							$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' ";
							$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'] == 'Purchase') {
									$preBal -= $rem_row['remaining'];
									$totalDebit -= $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'Return') {
									$preBal -= $rem_row['remaining'];
									$totalDebit -= $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'PReturn') {
									$preBal += $rem_row['remaining'];
									$totalDebit += $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'] == 'Sale') {
		                    ?>
		                    	<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'] == 'Purchase') { ?>
		                    	<td><?php echo ($rem_row['description']) ? $rem_row['description'] : '-'; ?></td>
			                    <td>0</td>
			                    <td><?php echo $rem_row['remaining']; ?></td>
		                    <?php }elseif ($rem_row['sale_type'] == 'Return') { ?>
		                    	<td><?php echo ($rem_row['description']) ? $rem_row['description'] : '-'; ?></td>
		                    	<td>0</td>
			                    <td><?php echo $rem_row['remaining']; ?></td>
							<?php }elseif ($rem_rowpr['sale_type'] == 'PReturn') { ?>
		                    	<td><?php echo ($rem_rowpr['description']) ? $rem_rowpr['description'] : '-'; ?></td>
		                    	<td>0</td>
			                    <td><?php echo $rem_rowpr['remaining']; ?></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
			 				$sqlpr = "SELECT DISTINCT invoiceNo FROM purchases ";
							$sqlpr .= "WHERE name = '".$_GET['name']."' ";
							//$sqlpr .= "AND (date BETWEEN '".$_GET['sdate']."' AND '".$_GET['edate']."') ";
							$sqlpr .= "AND php_date = '".$get_sdate."' AND payment_type != 'Opening Balance' ";
							$sqlpr .= "ORDER BY date, id";
							
							$runpr = $db->query($sqlpr);
							while($rowpr = $runpr->fetchArray()){
								$rempr = "SELECT * FROM purchases ";
								$rempr .= "WHERE invoiceNo = '".$rowpr['invoiceNo']."' ";
								$rem_runpr = $db->query($rempr);
								$rem_rowpr = $rem_runpr->fetchArray();

								if ($rem_rowpr['sale_type'] == 'Sale') {
									$preBal += $rem_rowpr['remaining'];
									$totalDebit += $rem_rowpr['remaining'];
								}elseif ($rem_rowpr['sale_type'] == 'Purchase') {
									$preBal -= $rem_rowpr['remaining'];
									$totalDebit -= $rem_rowpr['remaining'];
								}elseif ($rem_rowpr['sale_type'] == 'Return') {
									$preBal -= $rem_rowpr['remaining'];
									$totalDebit -= $rem_rowpr['remaining'];
								}elseif ($rem_rowpr['sale_type'] == 'PReturn') {
									$preBal += $rem_rowpr['remaining'];
									$totalDebit += $rem_rowpr['remaining'];
								}
						?>	
		            	<tr>
		                	<td><?php echo $rem_rowpr['show_date']; ?></td>
		                	<td><?php echo ($rem_rowpr['invoiceNo']) ? $rem_rowpr['invoiceNo'] : '-'; ?></td>
		                    <td><?php echo ($rem_rowpr['sale_type']) ? $rem_rowpr['sale_type'] : 'Opening Balance'; ?></td>
		                    <?php
		                    	if ($rem_rowpr['sale_type'] == 'Sale') {
		                    ?>
		                    	<td><?php echo ($rem_rowpr['description']) ? $rem_rowpr['description'] : '-'; ?></td>
			                    <td><?php echo $rem_rowpr['remaining']; ?></td>
			                    <td>0</td>
		                    <?php }elseif ($rem_rowpr['sale_type'] == 'Purchase') { ?>
		                    	<td><?php echo ($rem_rowpr['description']) ? $rem_rowpr['description'] : '-'; ?></td>
			                    <td>0</td>
			                    <td><?php echo $rem_rowpr['remaining']; ?></td>
		                    <?php }elseif ($rem_rowpr['sale_type'] == 'Return') { ?>
		                    	<td><?php echo ($rem_rowpr['description']) ? $rem_rowpr['description'] : '-'; ?></td>
		                    	<td>0</td>
			                    <td><?php echo $rem_rowpr['remaining']; ?></td>
							<?php }elseif ($rem_rowpr['sale_type'] == 'PReturn') { ?>
		                    	<td><?php echo ($rem_rowpr['description']) ? $rem_rowpr['description'] : '-'; ?></td>
			                    <td><?php echo $rem_rowpr['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 payments ";
				            $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'];
									$totalCredit += $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 Debit <?php echo $totalDebit; ?> </p>	
								</td>
								<td>
										<p style="font-size: 12px; color: green;">Total Credit <?php echo $totalCredit; ?> </p>	
								</td>
								<td>
								</td>
							</tr>
						</table>			
				</div>	
		</div>	
				</div>
	</div>
</div>

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