<?php
if(basename($_SERVER['PHP_SELF']) == 'custrepo.php') 
  $fn=" class='current'";
include'header.php';
include'connection.php';
$custo=$conde=$condi=$cond=$conds='';
$dato=$datos=$Date;
$gara=$pall='';
$p=$j=0;

// call to pay all by vehicle selection
if(isset($_POST['payall']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$gara=$_POST['gara'];
			$p=$_POST['p'];
			$n=$_POST['n'];
	$den=mysqli_query($conn, "UPDATE `income` SET `Checked`='0' WHERE `Checked`!='0' ORDER BY `Number` ASC LIMIT 10");
	
	while($n>0){
	    if($_POST["sel$n"]){
	$den=mysqli_query($conn, "UPDATE `income` SET `Checked`='1' WHERE `Number`='".$_POST["pay$n"]."' ORDER BY `Number` ASC LIMIT 1");
	            $j++;
	    }
	
	        $n--;
	        }
		}
		
		
			// Delete a single payment record
if(isset($_POST['pdelex']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$code=$_POST['code'];
			$gara=$_POST['gara'];
			$p=$_POST['p'];
			$num=$_POST['num'];
			$link=$_POST['link'];
			$pri=$_POST['pri']*$_POST['rti'];
				$p=$_POST['p'];
				$file=$_POST['file'];
		      unlink("files/$file");
		            
	$deles=mysqli_query($conn, "DELETE FROM `payment` WHERE `Number`='$num' AND `Action`='TRANSPORT' ORDER BY `Number` DESC LIMIT 1");
						
	$deles=mysqli_query($conn, "DELETE FROM `deposit` WHERE `Item`='TRANSPORT' AND `Operation`='DEPOSIT' AND `Refer`='x1' AND `Number`='$link' ORDER BY `Number` DESC LIMIT 1");
		}
		

	       // Add a payment
        if(isset($_POST['addpa']))
		    {
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$gara=$_POST['gara'];
			$supplier=$_POST['supplier'];
			$refo=str_replace("'", "`", $_POST['refo']);
			$amo=str_replace(',', '', $_POST['amo']);
			$balo=str_replace(',', '', $_POST['balo']);
			$p=$_POST['p'];
			$date=$_POST['datis'];
			$currency=$_POST['currency'];
			$pieces = explode("-", $_POST['pline']);
			$pline = $pieces[0]; 
			$count=$_POST['count'];
			$code=$_POST['code'];
			$pall=$_POST['pall'];
			$caso=$_POST['caso'];
			$cust=$_POST['cust'];
			$naso=$_POST['naso'];
			
	$doi=mysqli_query($conn, "SELECT `Rate` FROM `currency` WHERE `Code`='$currency' ORDER BY `Number` DESC");
			$roi=mysqli_fetch_assoc($doi);
				$rate=$roi['Rate'];
				$cheno=$bank='';
				$pdate=$date;
			
			 if($pline=='CHEQUE'){
			$cheno=$_POST['cheno'];
			$bank=$_POST['bank'];
			$pdate=$_POST['cheda'];
		        }
		     
		     if($pline=='DEPOSIT'){
			$cheno=$_POST['slino'];
			$bank=$_POST['acco'];
			$pdate=$_POST['cheda'];
		        }
		        
		     if($pline=='TRANSFER'){
			$cheno=$_POST['tno'];
			$bank=$_POST['tacco'];
			$pdate=$_POST['tda'];
		     }
		     
		     if($pline=='TRANSFER' OR $pline=='DEPOSIT'){
	$doi=mysqli_query($conn, "SELECT `Number`, `Rate` FROM `baccount` WHERE `Account`='$bank' ORDER BY `Number` ASC");
				$roi=mysqli_fetch_assoc($doi);
					$acco=$roi['Number'];
					$dra=$roi['Rate'];
                        $ope="DEPOSIT";
                            $bra=0;
                        $source=$pline;
                $purpo="Transport income payment";
                        $depo=$amo;
					
    $and=mysqli_query($conn, "INSERT INTO `deposit` (`Number`, `Date`, `Time`, `User`, `Item`, `Refer`, `Amount`, `Customer`, `Operation`, `Status`, `Valid`, `Account`, `Descri`, `Client`, `Voucher`, `Source`, `Record`, `Branche`, `Bank`, `Rate`) VALUES (NULL, '$pdate', '$Time', '$loge', 'TRANSPORT', 'x1', '$depo', '$cust', '$ope', '0', '0', '$acco', '$purpo', '0', '$naso', '$source', '0', '$bra', '$bank', '$dra')");
    $last_id = mysqli_insert_id($conn);
		        }

	    $temp1 = explode(".", $_FILES["file1"]["name"]);
    $newfilename1 = round(microtime(true)) . '.' . end($temp1);
    move_uploaded_file($_FILES["file1"]["tmp_name"], "files/" . $newfilename1);
	    if(!end($temp1))
	    $newfilename1='';
	    
	    if($pall=='ALL'){
			if($custo)
		$condi="AND `income`.`Vehicle`='$custo'";

			if($gara)
		$cond="AND `income`.`Customer`='$gara'";

				if($p)
			$conde="AND `income`.`Date` BETWEEN '$dato' AND '$datos'";
			
$do=mysqli_query($conn, "SELECT `income`.`Number`, `income`.`Customer`, `income`.`Balance` FROM `income` INNER JOIN `vehicles` ON `vehicles`.`Number` = `income`.`Vehicle` WHERE `income`.`Status`='0' AND `vehicles`.`Trip`='1' AND `income`.`Checked`='1' $conde $condi $cond ORDER BY `income`.`Date` ASC, `Number` ASC");
    while($ro=mysqli_fetch_assoc($do)){
        $inco=$ro['Balance'];
        $naso=$ro['Number'];
        $caso=$ro['Customer'];
        if($inco<($amo*$rate))
        $ami=($inco/$rate);
        else
        $ami=$amo;
        
    $setri=mysqli_query($conn, "SELECT `Customer` FROM `account` WHERE `Number`='$caso'");
        $file=mysqli_fetch_assoc($setri);
            $cust=$file['Customer'];
    
	$so=mysqli_query($conn, "INSERT INTO `payment` (`Date`, `Time`, `User`, `Amount`, `Pline`, `Invoice`, `Status`, `Customer`, `Action`, `Descri`, `Destin`, `Cheno`, `Bname`, `Pdate`, `Container`, `Currency`, `Rate`, `Files`, `Linked`, `Account`) VALUES ('$date', '$Time', '$loge', '$ami', '$pline', '$naso', '0', '$cust', 'TRANSPORT', '$refo', '', '$cheno', '$bank', '$pdate', '', '$currency', '$rate', '$newfilename1', '$last_id', '$caso')");
	                if($inco>($amo*$rate))
	                   break;
	                   
	        $amo-=($inco/$rate);
            }
	$den=mysqli_query($conn, "UPDATE `income` SET `Checked`='0' WHERE `Checked`!='0' ORDER BY `Number` ASC LIMIT 10");
	    }
	    else{
	$so=mysqli_query($conn, "INSERT INTO `payment` (`Date`, `Time`, `User`, `Amount`, `Pline`, `Invoice`, `Status`, `Customer`, `Action`, `Descri`, `Destin`, `Cheno`, `Bname`, `Pdate`, `Container`, `Currency`, `Rate`, `Files`, `Linked`, `Account`) VALUES ('$date', '$Time', '$loge', '$amo', '$pline', '$naso', '0', '$cust', 'TRANSPORT', '$refo', '', '$cheno', '$bank', '$pdate', '', '$currency', '$rate', '$newfilename1', '$last_id', '$caso')");
		}
		    }
		    
		    

// search for an item to be added to the chart
if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$gara=$_POST['gara'];
			    $p=1;
			    
	$den=mysqli_query($conn, "UPDATE `income` SET `Checked`='0' WHERE `Checked`!='0' ORDER BY `Number` ASC LIMIT 100");
		}
		
		
	
	    if($dato==$datos)
			$mpri="ON $dato";
		else
			$mpri="FROM $dato TO $datos";

			if($custo)
		$condi="AND `income`.`Vehicle`='$custo'";

			if($gara){
			    if($gara=='EXTERNAL')
		$cond="AND `income`.`Customer`!='2'";
		        else
		$cond="AND `income`.`Customer`='$gara'";
			}
		
		    
		    if($j)
		$conds="AND `income`.`Checked`='1'";
		

				if($p)
			$conde="AND `income`.`Date` BETWEEN '$dato' AND '$datos'";
?>

<div class="container-fluid main-content">
        <div class="page-title hidden-print">
          <h3 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
         Finance
          </h3>
                 </div>
     
        <div class="row">
  <div class="col-lg-2 hidden-print">
   
  <ul class="list-group">   

    <li class="list-group-item active">
	  <a href="custrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Pay on Account
                </p>
              </a></li> 

    <li class="list-group-item">
	  <a href="salesrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Sales Report
                </p>
              </a></li>   

    <li class="list-group-item">
	  <a href="purchase.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Purchase Orders
                </p>
              </a></li>   

    <li class="list-group-item">
	  <a href="trans.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Service Providers
                </p>
              </a></li> 

    <li class="list-group-item">
	  <a href="experepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Payout/Expenses
                </p>
              </a></li>  

    <li class="list-group-item">
	  <a href="zreport.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Daily Z-Report
                </p>
              </a></li>  

    <li class="list-group-item">
	  <a href="taxrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;VAT/TAX Report
                </p>
              </a></li>    

    <li class="list-group-item">
	  <a href="assets.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Assets Report
                </p>
              </a></li>  
                         
            </ul>
  </div>
                    
           
           
    <form action="" method="post" class="form-horizontal "> 
        <div class="col-lg-10">
                  <div class="row">

				  <div class="col-lg-1"> </div>         
                <?php
			    if($gara=='EXTERNAL')
					$sci="selected='selected'";
				else
					$sci='';
				?>
            <div class="col-lg-3 hidden-print" style="padding-left:50px;">
        <select class="form-control" name="gara">
				<option value='' selected='selected'>Select Customer</option>
				<option value='EXTERNAL' <?php echo $sci ?> style="color:blue;"> EXTERNAL TRANSPORT </option>
			 <?php
			$dois=mysqli_query($conn, "SELECT `account`.`Customer`, `account`.`Number` FROM `account` INNER JOIN `income` ON `account`.`Number` = `income`.`Customer` WHERE `income`.`Status`='0' AND `income`.`External`='1' GROUP BY `account`.`Customer` ORDER BY `account`.`Customer` ASC");
			while($rois=mysqli_fetch_assoc($dois)){
				$acco=$rois['Number'];
				$gar=$rois['Customer'];
				if($acco==$gara)
					$sli="selected='selected'";
				else
					$sli='';
			echo"<option value='$acco' $sli> $gar </option>";
			}
			?>    
        </select></div> 
							
                       <div class="col-lg-8 hidden-print"><div class="col-lg-3">
			<select class="form-control" name="custo">
				<option value='' selected='selected'>Select Vehicle</option>
			 <?php
			$doi=mysqli_query($conn, "SELECT `vehicles`.`Plate`, `income`.`Vehicle` FROM `income` INNER JOIN `vehicles` ON `vehicles`.`Number` = `income`.`Vehicle` WHERE `vehicles`.`Trip`='1' GROUP BY `income`.`Vehicle` ORDER BY `income`.`Vehicle` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
				$code=$roi['Vehicle'];
				$fna=$roi['Plate'];
				if($code==$custo)
					$sle="selected='selected'";
				else
					$sle='';
			echo"<option value='$code' $sle> $fna </option>";
			}
			?>    
                            </select>
					   </div>
            <div class="col-lg-3"> 
           <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center"  name="dato" type="text" value="<?php echo $dato ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>


		  <div class="col-lg-3"> 
           <div class="input-group date" data-provide="datepicker">	
      <input class="form-control form-center" name="datos" type="text" value="<?php echo $datos ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
              </div></div>                      
                       
                       <div class="col-lg-2">
                        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         </div></form> 
                         
    <script type="text/javascript">
var boxcounter;
$(function() {
    boxcounter = 0;
    $(".checkbox").click(function() {
        if(this.checked) {
            counter++;
            if(counter == 1){
                $(".delete").attr("disabled", "");
            }
        } else {
            counter--;
            if(counter == 0){
                $(".delete").attr("disabled", "disabled");
            }
        }
    }
}
        </script>
               
            </div>
             <?php
				if($p)
$do=mysqli_query($conn, "SELECT `income`.* FROM `income` INNER JOIN `vehicles` ON `vehicles`.`Number` = `income`.`Vehicle` WHERE `income`.`Status`='0' AND `vehicles`.`Trip`='1' $conde $condi $cond $conds ORDER BY `income`.`Date` ASC, `Number` ASC");
	else
$do=mysqli_query($conn, "SELECT *FROM (SELECT `income`.* FROM `income` INNER JOIN `vehicles` ON `vehicles`.`Number` = `income`.`Vehicle` WHERE `income`.`Status`='0' AND `vehicles`.`Trip`='1' ORDER BY `income`.`Date` DESC LIMIT 15) SUB ORDER BY `Date` ASC, `Number` ASC");
$fo=mysqli_num_rows($do);
					?>
					<div class="divFooter"><center><u><b>CUSTOMERS REPORT <?php echo"$mpri"; ?></b></u></center></div>
		 <div class="row">
            <div class="col-lg-12">
             <span> &nbsp;&nbsp; Total Records Found : <b><?php echo" $fo " ?></b></span>
			 <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="return window.print()" class="hidden-print" title='Click to print' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-printer"></i></a></span>
			 
			  <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="exportTableToExcel('inco_Data')" class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
    
 <script type="text/javascript">
function UpdateCost() {
  var sum = 0;
  var gn, elem;
  for (i=1; i<=<?php echo $fo ?>; i++) {
    gn = 'sum_m_'+i;
    elem = document.getElementById(gn);
    if (elem.checked == true) { sum += Number(elem.value); }
  }
  document.getElementById('totalcost' ).value = sum.toFixed(0);
}
window.onload=UpdateCost
</script>

			 <table class="table table-striped table-hover" id="inco_Data">     
                                      <thead>
                    <tr role="row">
                     <th class="text-center"> No </th>
                        <th class="text-center" width="8%"> Date </th> 
                       <th class="text-center"> Vehicle </th> 
                        <th class="text-center"> Customer </th>
                        <th class="text-center" colspan='2'> Destination </th>
						 <th class="text-center"> Amount </th>
						 <th class="text-center"> Rate </th>
						 <th class="text-center"> Value&nbsp;(RWF) </th> 
                        <th class="text-center"> Paid </th>  
                        <th class="text-center"> Balance </th>
						 <th class="hidden-print text-center" width="1%"> # </th></tr></thead>
                                        <tbody>
					<?php
					if($gara)
					    echo"<form action='' method='post'>";
			$i=$t=1;					$tam=$tdi=$twe=$tva=$tpa=0;
	while($ro=mysqli_fetch_assoc($do)){
$code=$ro['Trip'];
$naso=$ro['Number'];
$emplo=$ro['Vehicle'];
$amo=$ro['Amount'];
$amoo=number_format($amo, 2);
$dte=$ro['Date'];
$vou=$ro['Voucher'];
$caso=$ro['Customer'];
$garag=$ro['District'];
$pur=$ro['Location'];
$rate=$ro['Rate'];
$val=$amo*$rate;
$count="TRANSPORT";
if($ro['Checked'])
$chk="checked";
else
$chk="";
$pay=0;
						
	$seepai=mysqli_query($conn, "SELECT SUM(`Amount`*`Rate`) AS 'Pay' FROM `payment` WHERE `Invoice`='$naso' AND `Action`='$count' AND `Status`='0'");
		$reepai=mysqli_fetch_assoc($seepai);
		    $pay=$reepai['Pay'];
		    if($gara)
		        $pall="ALL";
		        
		        $bal=$val-$pay;	
		        
		       	if($bal>10 AND $rate>1000){
                $val=$amo*$rase;
		        $bal=$val-$pay;	
	$then=mysqli_query($conn, "UPDATE `income` SET `Rate`='$rase' WHERE `Number`='$naso' AND `Rate`>'1000' ORDER BY `Number` ASC LIMIT 1");
		            }
		    
 	$twe+=$wei;	                  $tva+=$val;                   $tpa+=$pay;  

if($amo>'10000' AND $rate=='1')
$tam+=($amo/$rase);	
else
$tam+=$amo;	


    $seepai=mysqli_query($conn, "UPDATE `payment` SET `Account`='$caso' WHERE `Invoice`='$naso' AND `Action`='$count' AND `Status`='0' AND `Account`='0'");
					
	$payo=number_format($pay, 2);
	$valo=number_format($val, 2);
	$rato=number_format($rate, 2);
	$balo=number_format($bal, 2);
	
	$setri=mysqli_query($conn, "SELECT `Customer` FROM `account` WHERE `Number`='$caso'");
        $file=mysqli_fetch_assoc($setri);
            $cust=$file['Customer'];

$doi=mysqli_query($conn, "SELECT *FROM `vehicles` WHERE `Number`='$emplo' AND `Trip`='1'");
			$roi=mysqli_fetch_assoc($doi);
				$fna=$roi['Plate'];
				
	    if($pay)
	$lin="<a href='#' data-toggle='modal' data-target='#exampleModalx$i'>";
		else
	$lin="";
	
            $stn="style='padding:1px; font-size:13px;'";
           
	 print("<tr><td $stn class='text-center'>$i</td>
	 <td class='text-center' $stn> $dte </td><td $stn>&nbsp;$fna&nbsp;</td>
    <td $stn> $cust </td><td $stn> $garag </td><td $stn> $pur </td>
			<td class='text-right' $stn> $amoo&nbsp;</td>
			<td class='text-right' $stn> $rato&nbsp;</td>
    <td class='text-right' $stn> $valo&nbsp;</td><td class='text-right' $stn>");
	
	            $dst="TRANSPORT AMOUNT";
	
	// ************************************* Open payment modal ******************************************
		echo"<div class='modal fade' id='exampleModalx$i' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true' style='top:220px;'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content'>
      <div class='modal-header text-left'>
        <h5 class='modal-title' id='exampleModalLabel'>PAYMENT REPORT 
&nbsp;&nbsp;&nbsp;&nbsp;<label class='pull-right'><b>$ $payo</b></label></h5>

      </div>
      <div class='modal-body' style='height:auto;'>
	  <table class='table table-striped table-hover '><thead><tr>
	  <th width='14%' class='text-center'> Date </th>
	  <th class='text-center'> User </th>
	  <th width='12%' class='text-center'> Mode </th>
	  <th class='text-center'> Reference </th>
	  <th class='text-center'> Rate </th>
	  <th class='text-center'> Amount </th><th width='1%'> # </th>
	  </tr></thead></tbody>";
		$k=9000000000;				$sts="padding:1px; font-size:12px; background-color:transparent;";           $img=array();   
		$kt=1;
		
$spai=mysqli_query($conn, "SELECT *FROM `payment` WHERE `Invoice`='$naso' AND `Action`='TRANSPORT' AND `Status`='0' ORDER BY `Number` ASC");
				while($rpai=mysqli_fetch_assoc($spai)){
					$prs=number_format($rpai['Amount'], 2);
					$mod=$rpai['Pline'];
					$cur=$rpai['Rate'];
					$rat=number_format($rpai['Rate'], 2);
					$refe=$rpai['Cheno'];
					$bank=$rpai['Bname'];
					$desc=$rpai['Descri'];
					$dti=$rpai['Date'];
					$num=$rpai['Number'];
					$user=$rpai['User'];
					$file=$rpai['Files'];
					$link=$rpai['Linked'];
					$img[$kt]=$file;
					if($mod=='CASH')
					    $refe=$desc;
					else
					    $refe="$refe/$bank";

		echo"<tr style='background-color:transparent;'>
		<td class='text-center' style='$sts'> $dti </td>
		<td style='text-align:left; $sts'> $user </td><td style='$sts'> $mod </td>
<td style='$sts'> $refe </td><td style='$sts' class='text-center'> $cur </td>
		
		<td style='text-align:right; $sts'>
		<div title='Rate: $rat' data-toggle='tooltip' 
		data-placement='top'> $prs </div></td><td style='$sts'>
	
		<div title='Delete' data-toggle='tooltip' data-placement='top'>
		<input type='hidden' name='dato' value='$dato'>
	  <input type='hidden' name='datos' value='$datos'>
	  <input type='hidden' name='custo' value='$custo'>
	  <input type='hidden' name='gara' value='$gara'>
	  <input type='hidden' name='num' value='$num'>
	  <input type='hidden' name='p' value='$p'>
	  <input type='hidden' name='file' value='$file'>
	  <input type='hidden' name='link' value='$link'>
		
		<button type='button' class='btn btn-xs btn-link text-danger hidden-print' style='height:16px; padding:0px; margin:0px; width:auto; margin-top:-5px;' name='pdelex'>&nbsp;&nbsp;<i class='lnr lnr-trash'></i>&nbsp;&nbsp;</button></div></td></tr>";
			$k++;                    $kt++;
				}        

      echo"</tbody></table><center>";
      
      for($ln=1; $ln<=$fl; $ln++){
          $ims=$img[$ln];
          
          echo"<style>
          .hover_img a { position:relative; }
.hover_img a span { position:absolute; display:none; z-index:99; font-size:14px; }
.hover_img a:hover span { display:block; }
    </style>";
                if($ims)
    echo"<label class='hover_img'>
     <a href='files/$ims'>$ims<span><img src='files/$ims' alt='image' height='500' width='500' /></span></a></label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
      }
      
      
      echo"</div><div class='modal-header text-right' 
	  style='margin-top:-10px; height:40px; padding-top:10px; border:0px solid blue;'>
  <button type='button' class='btn btn-xs btn-warning' data-dismiss='modal'>&nbsp;&nbsp;&nbsp;CLOSE&nbsp;&nbsp;&nbsp;</button>
      </div>
    </div>
  </div>
</div>";
	// ****************************************** End of modal ****************************************	


print("$lin $payo </a></td><td $stn><div align='right'> $balo&nbsp;&nbsp;</td>");
	
	$upds=mysqli_query($conn, "UPDATE `income` SET `Balance`='$bal' WHERE `Number`='$naso' ORDER BY `Number` ASC LIMIT 1");
		
		if($gara)
		    echo"<td align='right' style='width:20px; padding:0px;'><input class='form-control' name='sel$i' type='checkbox' value='$bal' $chk style='margin:1px; width:30px; height:18px;' class='checkbox'  onclick='UpdateCost()' id='sum_m_$i'><input type='hidden' value='$naso' name='pay$i'></td>";
		else{
$line="<div class='modal fade' id='exampleModalo$i' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true' style='top:20px;'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content'>
      <div class='modal-header'>
        <h5 class='modal-title' id='exampleModalLabel'>ADD A PAYMENT
    <label class='pull-right'><b>$dst : &nbsp;&nbsp; $balo</b>
            </label></h5>

      </div>
      <div class='modal-body' style='text-align:center; height:auto;'>
      <form action='' method='post' enctype='multipart/form-data'>";

	include'addpait.php';
			echo"<td align='right' style='width:20px; padding:0px;'>
	<div title='Pay' data-toggle='tooltip' data-placement='top' align='center'>
	<button type='button' class='btn btn-xs btn-success hidden-print' style='height:20px; padding:0px; margin:2px;' data-toggle='modal' data-target='#exampleModalo$i'> &nbsp;&nbsp;<i class='lnr lnr-briefcase'></i>&nbsp;&nbsp; </button></td>";
		}
	
	            print"</tr>";
						$i++;							
						}
			
			$tba=$tva-$tpa;						
		$tam=number_format($tam, 2);				
		$tpa=number_format($tpa, 2);				
		$tba=number_format($tba, 2);				
		$tva=number_format($tva, 2);					

		?>
                    </tbody><thead> 
<tr><th> </th><th colspan='5' style='padding:2px'>&nbsp;&nbsp;Total Amount </th>
	<th class='text-right' style='padding:2px'><?php echo $tam ?></th><th> </th>
			<th class='text-right' style='padding:2px'><?php echo $tva ?></th>
			<th class='text-right' style='padding:2px'><?php echo $tpa ?></th>
	<th class='text-right' style='padding:2px'><?php echo $tba ?>&nbsp;</th>
	    <th class='hidden-print' style='padding:2px'><center> -- </th></tr>
	        </thead></table>
	
	<?php
	    if($gara AND $j==0){
	 echo"<input type='hidden' name='dato' value='$dato'>
	  <input type='hidden' name='datos' value='$datos'>
	  <input type='hidden' name='custo' value='$custo'>
	  <input type='hidden' name='gara' value='$gara'>
	  <input type='hidden' name='n' value='$i'>
	  <input type='hidden' name='p' value='$p'>
 <button type='submit' class='btn btn-sm btn-info pull-right' style='width:180px; height:28px; margin-right:10px; margin-left:10px;' name='payall' class='delete'><i class='lnr lnr-briefcase'></i> &nbsp;&nbsp; PAY&nbsp;SELECTED </button> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input class='form-control text-right pull-right' type='text' name='sen' id='totalcost' value='' onchange='format(this);' style='width:160px; border:0px; font-size:24px; margin-right:10px; margin-left:10px; height:28px;'></form>";
	 
	 /*
	         $i=0;
					    $balo=$tba;
					include'addpait.php';
    echo"<button type='button' class='btn btn-sm btn-default pull-right' style='width:180px; height:28px; margin-right:5px;' data-toggle='modal' data-target='#exampleModalo$i'><i class='lnr lnr-briefcase'></i> &nbsp;&nbsp; PAY&nbsp;ALL </button>";
            */
	    }
	    if($j){
        	   echo"<script>
    $('#myModal').modal({ show : true });
                </script>";
                
	   $line="<div><div><div><div style='padding:0px; 
	   margin-top:30px; border:1px solid #D8D7D6; border-radius:5px;'>
      <div class='modal-header' style='margin-bottom:40px; margin-top:0px;'>
        <h5 class='modal-title' id='exampleModalLabel'>ADD A PAYMENT 
    <label class='pull-right'><b>$dst : &nbsp;$tba</b>
            </label></h5>

      </div>
	   
      <div style='text-align:center; height:auto; width:80%;'>
      <form action='' method='post' enctype='multipart/form-data'>";
					include'addpait.php';
		echo"</div>";
	    }
	    ?>
                  
                                      
                
              </div>
            </div></div>
                  </div>                  
                <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="return window.print()" class="hidden-print" title='Click to print' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-printer"></i></a></span>
			 <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="exportTableToExcel('inco_Data')" class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
      
   </div></div></div>  
   <?php
    $setri=mysqli_query($conn, "DELETE FROM `income` WHERE `Amount`='0' ORDER BY `Number` ASC LIMIT 2");
    $setri=mysqli_query($conn, "DELETE FROM `payment` WHERE `Amount`='0' ORDER BY `Number` ASC LIMIT 2");
   include'footer.php';
   ?>