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

// search for an item to be added to the chart
if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$p=1;
		}
		
// delete a given payment
if(isset($_POST['delos']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$p=$_POST['p'];
			$rowid=$_POST['rowid'];
	
	$des=mysqli_query($conn, "UPDATE `payment` SET `Status`='1' WHERE `Number` = '$rowid' ORDER BY `Number` LIMIT 1");
		}
		
		if($custo)
		    $conde="AND `Customer`='$custo'";
	
	    if($dato==$datos)
			$mpri="ON $dato";
		else
			$mpri="FROM $dato TO $datos";
                ?>
                
<div class="container-fluid main-content">
        <div class="page-title hidden-print">
          <h3 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
         Finance Report
          </h3>
  
    </div>
   <div class="row">
  <div class="col-lg-2 hidden-print">
   
  <ul class="list-group">
                  
			  <li class="list-group-item active">
              <a href="paidreport.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Payment Report 1
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="paireport.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Payment Report 2
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="expenserepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Expenses Report 1
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="expereport.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Expenses Report 2
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="purcharepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Purchase Report
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="bankreport.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Bank Report
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="assetreport.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Asset Report
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="fsalerepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Finacial Statement
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="finrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Finacial Position
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="holdrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Withholding Report
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="cashrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Cashbox 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 text-center"><font color="#ffcc33" size="4">OUT</font></div>         

				  <div class="col-lg-2"> </div>         

            <div class="col-lg-3 hidden-print" style="padding-left:50px;">
        <select class="form-control" name="custo">
				<option value='' selected='selected'>Select Customer</option>
			 <?php
			$dois=mysqli_query($conn, "SELECT `Customer` FROM `payment` WHERE `Status`='0' AND `Action`='SUPPLIER' GROUP BY `Customer` ORDER BY `Customer` ASC");
			while($rois=mysqli_fetch_assoc($dois)){
				$caso=$rois['Customer'];
				if($caso==$custo)
					$sli="selected='selected'";
				else
					$sli='';
			echo"<option value='$caso' $sli> $caso </option>";
			}
			?>    
                            </select></div> 
							
                       <div class="col-lg-6 hidden-print">
                       
            <div class="col-lg-4"> 
           <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-4"> 
           <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-3">
                        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         </div> 
                      
                     
                  
            </form> 
             
               
            </div>
             <?php
				if($p)
$do=mysqli_query($conn, "SELECT *FROM `payment` WHERE `Status`='0' AND `Action`='SUPPLIER' AND `Date` BETWEEN '$dato' AND '$datos' $conde ORDER BY `Date` ASC, `Number` ASC");
	else
$do=mysqli_query($conn, "SELECT *FROM (SELECT *FROM `payment` WHERE `Status`='0' AND `Action`='SUPPLIER' ORDER BY `Date` DESC LIMIT 15) SUB ORDER BY `Date` ASC, `Number` ASC");
        $fo=mysqli_num_rows($do);
					?>
					<div class="divFooter"><center><u><b>PAYMENT 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;<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="#" id="exporttable" 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">

			 <table class="table table-striped table-hover" id="htmltable">     
                                      <thead>
                    <tr role="row">
                     <th class="text-center"> No </th>
                        <th class="text-center" width="7%"> Date </th> 
                        <th class="text-center"> Customer </th>
                        <th class="text-center"> Destination </th>
						 <th class="text-center"> Amount </th> 
						 <th class="text-center"> Cash </th> 
                        <th class="text-center"> Momo </th>  
                        <th class="text-center"> Cheque </th>  
                        <th class="text-center"> Deposit </th> 
                        <th class="text-center"> Transfer </th>  
						 <th class="text-center"> Rate </th>
						 <th class="text-center"> Value&nbsp;(RWF) </th>
						 <?php
						 if($_SESSION['Cfr'])
						    echo"<th class='text-center'> # </th>";
						    ?>
						 </tr></thead><tbody>
					<?php
	    $i=1;					$tinco=$tca=$tmo=$tde=$ttra=$tche=$tval=0;
	while($ro=mysqli_fetch_assoc($do)){
	    $ca=$mo=$de=$tra=$che=0;
        $code=$ro['Number'];
        $acco=$ro['Account'];
        $cust=$ro['Customer'];
        $vou=$ro['Invoice'];
        $user=$ro['User'];
        $rate=$ro['Rate'];
        $dte=$ro['Date'];
        $amo=$ro['Amount'];
        $val=$amo*$rate;
        
        if($ro['Pline']=='CASH')
            $ca=$amo;
        if($ro['Pline']=='MOMO')
            $mo=$amo;
        if($ro['Pline']=='DEPOSIT')
            $de=$amo;
        if($ro['Pline']=='TRANSFER')
            $tra=$amo;
        if($ro['Pline']=='CHEQUE')
            $che=$amo;

$seepai=mysqli_query($conn, "SELECT `Supplier`, `Purpose`, `Amount` FROM `services` WHERE `Number`='$vou' AND `Supplier`='$cust' AND `Status`='0'");
		$reepai=mysqli_fetch_assoc($seepai);
		    $garag=$reepai['Supplier'];
		    $pur=$reepai['Purpose'];
		     $inco=$reepai['Amount'];
					
	$cao=number_format($ca, 2);
	$moo=number_format($mo, 2);
	$deo=number_format($de, 2);
	$trao=number_format($tra, 2);
	$cheo=number_format($che, 2);
	$rato=number_format($rate, 2);
	$valo=number_format($val, 2);
	$incoo=number_format($inco, 2);
	$stn="style='padding:1px; font-size:12px;'";
           
	 print("<tr><td $stn class='text-center'>$i</td>
	 <td class='text-center' $stn> $dte </td>
    <td $stn> $cust </td><td $stn> $pur </td>
			<td class='text-right' $stn> $incoo&nbsp;</td>
			<td class='text-right' $stn> $cao&nbsp;</td>
			<td class='text-right' $stn> $moo&nbsp;</td>
			<td class='text-right' $stn> $cheo&nbsp;</td>
			<td class='text-right' $stn> $deo&nbsp;</td>
			<td class='text-right' $stn> $trao&nbsp;</td>
			<td class='text-right' $stn> $rato&nbsp;</td>
    <td class='text-right' $stn> $valo&nbsp;</td>");
    if($_SESSION['Cfr']){
    echo"<form method=post action=''><td class='hidden-xs hidden-print' align='right' style='width:40px; padding:0px;'>";
    
    	echo"<div class='modal fade' id='exampleModal$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'>
        <h5 class='modal-title' id='exampleModalLabel'>DELETE CONFIRMATION 
        <label class='text-right pull-right'> $cust: $toto </label></h5>

      </div>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to delete this requisition?</h5>
      </div><form method='post' action=''>
            <input type='hidden' name='rowid' value='$code'>
            <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='p' value='$p'>
      <div class='modal-footer' style='margin-top:-10px; height:50px; padding-top:5px; border:0px solid blue;'>
        <button type='button' class='btn btn-sm btn-success' data-dismiss='modal'>&nbsp;NO&nbsp;</button>
        <button type='submit' name='delos' class='btn btn-sm btn-danger'>YES</button>
      </div></form>
    </div>
  </div>
</div>";

            
        echo"<button type='submit' class='btn btn-xs btn-danger hidden-print' style='height:20px; padding:0px; margin:2px;' data-toggle='modal' data-target='#exampleModal$i'> &nbsp;&nbsp;<i class='lnr lnr-trash'></i>&nbsp;&nbsp;</button></td></form>";
    }
    
    print("</tr>");
						$i++;
						
$tca+=$ca*$rate;                $tmo+=$mo*$rate;            $tde+=$de*$rate;
	$ttra+=$tra*$rate;            $tche+=$che*$rate;            $tval+=$val;
		$tinco+=$inco*$rate;
						}
						
		$tca=number_format($tca, 2);				
		$tmo=number_format($tmo, 2);				
		$tde=number_format($tde, 2);				
		$tche=number_format($tche, 2);				
		$ttra=number_format($ttra, 2);				
		$tval=number_format($tval, 2);				
		$tinco=number_format($tinco, 2);					

		?>
                    </tbody><thead> 
<tr><th> </th><th colspan='3' style='padding:1px; font-size:12px;'>&nbsp;&nbsp;Total Amount </th>
	<th style='padding:1px; font-size:12px; text-align:center;'>&nbsp;--&nbsp;</th>
	<th class='text-right' style='padding:1px; font-size:12px;'><?php echo $tca ?>&nbsp;</th>
	<th class='text-right' style='padding:1px; font-size:12px;'><?php echo $tmo ?>&nbsp;</th>
	<th class='text-right' style='padding:1px; font-size:12px;'><?php echo $tche ?>&nbsp;</th>
	<th class='text-right' style='padding:1px; font-size:12px;'><?php echo $tde ?>&nbsp;</th>
	<th class='text-right' style='padding:1px; font-size:12px;'><?php echo $ttra ?>&nbsp;</th>
	<th class='text-right' style='padding:1px; font-size:12px; text-align:center;'>&nbsp;--&nbsp;</th>
	<th class='text-right' style='padding:1px; font-size:12px;'><?php echo $tval ?>&nbsp;</th>
	
						 <?php
						 if($_SESSION['Cfr'])
						    echo"<th class='text-center'> </th>";
						    ?>
	</tr>
	        </thead></table>
                  
                                      
                
              </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>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>