<?php  
require_once 'inc/conx.php';
//echo $_POST['name'];
//die();
$ID = 0;
$countQuery = $db->query("SELECT count(*) totalCount FROM customer_setup WHERE customer_name = '".$_POST['name']."'");
$rowcount = $countQuery->fetchArray();
$totalCount = $rowcount['totalCount'];
if ($totalCount < 1) {

$countQuery = $db->query("SELECT count(*) totalCount FROM supplier_setup WHERE supplier_name = '".$_POST['name']."'");
$rowcount = $countQuery->fetchArray();
$totalCount = $rowcount['totalCount'];
	if ($totalCount < 1) {
		$ID = 0;
 	}else{
 		$supplier = $db->query("SELECT * FROM supplier_setup WHERE supplier_name = '".$_POST['name']."'");
 		$fetchdate = $supplier->fetchArray();
 		$ID = "s".$fetchdate['id'];
 		$payments = "payments2";
 	}
 }else{
 	$customer = $db->query("SELECT * FROM customer_setup WHERE customer_name = '".$_POST['name']."'");
 	$fetchdate = $customer->fetchArray();
 	$ID = "c".$fetchdate['id'];
 	$payments = "payments";
 }
 
//  echo $ID;
//  die();
 
    if($ID){
?>
 <?php
						$date = "2015/12/30";
						
		                // End date
						$sedate = date("m/d/Y");
						$secus_date = explode('/', $sedate);
						$seget_sdate = $secus_date[2].'/'.$secus_date[0].'/'.$secus_date[1];
		                $end_date = date ("Y/m/d", strtotime("-0 day", strtotime($seget_sdate)));
						
						$preBal = 0;
						while (strtotime($date) <= strtotime($end_date)) {
						//echo "$date\n";
						
                        
						$open = "SELECT * FROM customerinvoice ";
						$open .= "WHERE name = '".$ID."' AND  payment_type = 'Opening Balance' AND php_date = '".$date."'";
						$open_run = $db->query($open);
						while($open_row = $open_run->fetchArray()){
							$preBal += $open_row['remaining'];
						}


						$sql = "SELECT DISTINCT invoiceNo FROM customerinvoice ";
						$sql .= "WHERE name = '".$ID."' ";
						//$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($payments == "payments"){	
							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'];
							}
						}else{
						    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 = '".$ID."' 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'];
							}
						}
						
						$date = date ("Y/m/d", strtotime("+1 day", strtotime($date)));

					}
				
						echo $preBal;
    }
				
					?>

