<?php
if(basename($_SERVER['PHP_SELF']) == 'prirepo.php') 
  $cm=" class='current'";
include'header.php';
include'connection.php';
$dato=$datos=$Date;
$custo='';
$conde='';
$condi='';
$t=$p=0;
$brc=$_SESSION['Branche'];
$brancho=$_SESSION['Branche'];

// search for an item to be added to the chart
if(isset($_POST['search']))
		{
			$custo=$_POST['custo'];
			$p=1;
		}
		
		// open a given customer account
if(isset($_POST['open']))
		{
			$brc=$_POST['brc'];
			$custo=$_POST['custo'];
			$code=$_POST['code'];
			$cuna=$_POST['cuna'];
			$p=$_POST['p'];
			$t=1;
		}
	
		if($dato==$datos)
			$mpri="ON $dato";
		else
			$mpri="FROM $dato TO $datos";

	if($brc=='0' OR $brc=='')
		$conde="";
	else
		$conde="AND `Branche`='$brc'";

	if($custo)
		$condi="AND `Customer`='$custo'";
	else
		$condi="";
?>
<div class="container-fluid main-content">
        <div class="page-title hidden-print">
          <h3 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
         Customers 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="prirepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Balance Report
                </p>
              </a></li>  		  

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

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

    <li class="list-group-item">
	  <a href="csrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Payment Report
                </p>
              </a></li>  
                         
            </ul>
  </div>
                    
           
           
       
         <div class="col-lg-10">
                  <div class="row">
         
           <div class="col-lg-4"> </div>
          
         
           

        <form action="" method="post" class="form-horizontal ">                  
                       <div class="col-lg-8 hidden-print"><div class="col-lg-4">
		
					   </div>
            <div class="col-lg-5"> 
          <input class="form-control" name="custo" type="text" id="searcho" autofocus="autofocus" placeholder="Customer" value="<?php echo $custo ?>"></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> 
             
               
            </div>
                <?php
			   if($t==0){
		$do=mysqli_query($cons, "SELECT *FROM `account` WHERE `Balance` > '1' $conde $condi ORDER BY `Customer` ASC LIMIT 1000");
				if($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"><?php echo $bra ?>&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 class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">

								 <form action="" method="post" class="form-horizontal "> 
				<table class="table table-striped table-hover">     
                                      <thead>
                    <tr role="row">
                     <th class="hidden-xs" width='5%'> &nbsp;&nbsp;No </th> 
                       <th> CUSTOMER </th><th> BRANCH </th>
					   <th class='text-center'> UPDATE </th>
						<th class='text-center'> TELEPHONE </th>
                       <th class='text-center'> ADDRESS </th>
                       <th class='text-right'> BALANCE &nbsp;&nbsp;&nbsp;</th>
                        <th class="hidden-xs hidden-print" style="width:40px; text-align:center;"> # </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					$n=1;				$tpa=$tto=0;
						while($ro=mysqli_fetch_assoc($do)){
							$code=$ro['Number'];
				$custo=$ro['Customer'];
				$dte=$ro['Cdate'];
				$tele=$ro['Telephone'];
				$adde=$ro['Address'];
				$bal=$ro['Balance'];
				$brc=$ro['Branche'];

				$tot=0;
	$cuse=mysql_query("SELECT `Destin`, SUM(`Price`*`Quantity`) AS 'TOT' FROM `stouse` WHERE `Client`='$code' AND `Action`='SALES' AND `Status`='0' AND `Upda`='1' ORDER BY `Number` ASC");
			$ruce=mysql_fetch_assoc($cuse);
					$tot=$ruce['TOT'];					

			$pay=0;
	$cusea=mysql_query("SELECT SUM(`Amount`) AS 'Amo' FROM `payment` WHERE `Client`='$code' AND `Pline`!='CREDIT' AND `Status`='0' AND (`Action`='SALES' OR `Action`='PAYMENT') ORDER BY `Number` ASC");
			$rpa=mysql_fetch_assoc($cusea);
					$pay=$rpa['Amo'];

					$bas=$tot-$pay;

$doix=mysql_query("SELECT `Name` FROM `branches` WHERE `Number`='$brc' ORDER BY `Number` ASC");
			$roix=mysql_fetch_assoc($doix);
				$fna=$roix['Name'];

								$balo=number_format($bal, 2);					$baso=number_format($bas, 2);

						$stn="padding:0px;";

						if($_SESSION['Cancel']=='0'){
						$dso='disabled';
						$btz='button';
					}
					else{
						$btz='submit';
					}

		print("<tr><td class=hidden-xs style='$stn'><div align='right'>&nbsp;&nbsp;$n&nbsp;&nbsp;&nbsp;&nbsp;</td>
		
		<td style='$stn'> $custo </td><td style='$stn'> $fna </td>
		<td style='$stn' class='text-center'> $dte </td>
		<td class='text-right' style='$stn'> $tele&nbsp;&nbsp;</td>
		<td style='$stn'> $adde </td><td style='$stn'><div align='right'> $balo&nbsp;&nbsp;</td>

						<form method=post action=''> <input type='hidden' name='brc' value='$brc'><input type='hidden' name='p' value='$p'>
						<input type='hidden' name='dte' value='$dte'><td class='hidden-xs hidden-print' align='right' style='width:40px; padding:0px;'>
       <input type='hidden' name='code' value='$code'><input type='hidden' name='cuna' value='$custo'>
	   <input type='hidden' name='p' value='$p'><input type='hidden' name='brc' value='$brc'> 

                          <button type='submit' class='btn btn-xs btn-success hidden-print' name='open' style='height:18px; padding:0px; margin:0px;'  title='Open' data-toggle='tooltip' data-placement='top'> &nbsp;&nbsp;<i class='lnr lnr-download'></i>&nbsp;&nbsp;</button></td></form></tr>");
						  $n++;									$tto+=$bal;
						}
							$tto=number_format($tto, 2);				
						?>
						
                     </tbody>

					 <thead>
					<tr><th class='hidden-xs'> </th><th colspan='5'> Total Amount </th>
					<th><div align='right'><?php echo $tto ?></th><th class="hidden-xs hidden-print"><div align='center'> -- </th></tr>
                  </table><br>

				  
				  <?php
				}
				 else{
						echo"<div class='row'>
            <div class='col-lg-12' style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b> 0 </b></span>
			 <span class='pull-right hidden-print'> $bra &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 class='widget-container fluid-height clearfix'>
            <div class='widget-content padded clearfix'>
			<br><br><br><br><br><br>
	<div style='text-align:center; font-size:24px; color:#ff3333'> Report not available on selected condition </div><br><br><br><br><br><br><br>";
					}  
			}
				   else{
			$dox=mysqli_query($cons, "SELECT *FROM (SELECT *FROM `payment` WHERE `Status`='0' AND `Voucher`!='0' AND `Action`='SALES' AND `Pline`='CREDIT' AND `Client`='$code' $conde GROUP BY `Voucher` ORDER BY `Voucher` DESC LIMIT 20) SUB ORDER BY `Voucher` ASC");
				if($fox=mysqli_num_rows($dox)){					
	
	 $top=mysqli_query($cons, "SELECT *FROM `account` WHERE `Number`='$code' GROUP BY `Customer` ORDER BY `Customer` ASC");
				$rop=mysqli_fetch_assoc($top);
					$sup=$rop['Customer'];
					$tin=$rop['Tin'];
					$per=$rop['Person'];

					$pa=0;
	$sepa=mysqli_query($cons, "SELECT SUM(`Amount`) AS 'Amo' FROM `payment` WHERE `Status`='0' AND `Client`='$code' AND `Action`='PAYMENT' AND `Pline`!='CREDIT'");
		$repa=mysqli_fetch_assoc($sepa);
			$pa=$repa['Amo'];									$pas=$pa;

					?>
					<div class="divFooter"><center><u><b>CREDIT 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" $fox " ?></b></span>
			 <span class="pull-right hidden-print"><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"><?php echo $sup ?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b> <?php echo $per ?>
			  &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; </b></span> 
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">

			<form action="" method="post">
				<table class="table table-striped table-hover">     
                       <thead><tr role="row">
                     <th width='3%' class="text-center">&nbsp;&nbsp;No&nbsp;&nbsp;</th>
                       <th class='text-center'> DUE&nbsp;DATE </th>
                       <th> CUSTOMER </th>
                       <th> ITEM&nbsp;NAME </th>
                       <th> QUANTITY </th>
                       <th class='text-right'> VALEU(RWF) </th>
						<th class='text-right'> PAYMENT </th>
						<th class='text-right'> CREDIT&nbsp;&nbsp;</th>
                     </tr>
                    </thead>
                                        <tbody>
			
			<?php
				$tba=$tpa=$tot=$tot=$tq=0;			$i=1;				$n=1;	
	while($rox=mysqli_fetch_assoc($dox)){
		$vous=$rox['Voucher'];
		$driver=$rox['Bname'];	

	$sepac=mysqli_query($cons, "SELECT SUM(`Amount`) AS 'Amo' FROM `payment` WHERE `Status`='0' AND `Client`='$code' AND `Action`='SALES' AND `Pline`!='CREDIT' AND `Voucher`='$vous'");
		$repac=mysqli_fetch_assoc($sepac);
			$pai=$repac['Amo'];
			$pa+=$pai;

		$i=1;							$ito='';				$amo=0;
	$do=mysqli_query($cons, "SELECT *FROM `stouse` WHERE `Status`='0' AND `Voucher`='$vous' AND `Action`='SALES' $conde ORDER BY `Number` ASC LIMIT 100");
				$fo=mysqli_num_rows($do);
						
			while($ro=mysqli_fetch_assoc($do)){
				$dte=$ro['Date'];
				$user=$ro['User'];
				$pri=$ro['Price'];
				$prio=number_format($pri, 2);
				$qty=$ro['Quantity'];
				$qto=number_format($qty, 2);
				$item=$ro['Item'];
				$custo=$ro['Destin'];
				$vehicule=$ro['Refer'];
				$voucher=$ro['Voucher'];
				$code=$ro['Client'];

				$amo=$qty*$pri;						

	$dop=mysqli_query($cons, "SELECT *FROM `items` WHERE `Number`='$item' ORDER BY `Number` DESC LIMIT 1");
				$rop=mysqli_fetch_assoc($dop);
					$iname=$rop['Iname'];
					$stn="padding:0px;";				
				
		
			if($pa>$amo)
				$pay=$amo;
			elseif($pa<$amo AND $pa>0)
				$pay=$pa;
			else
				$pay=0;

				$bal=$amo-$pay;
				$balo=number_format($bal, 2);
				$amoo=number_format($amo, 2);
				$payo=number_format($pay, 2);

		print("<tr>
		<td style='$stn' class='text-right'>$n&nbsp;&nbsp;</td><td style='$stn' class='text-center'>&nbsp;$dte&nbsp;</td>
		<td style='$stn'>&nbsp;$custo&nbsp;</td><td style='$stn'>&nbsp;$iname&nbsp;</td><td style='$stn' class='text-center'>&nbsp;$qto&nbsp;</td>
		<td style='$stn' class='text-right'> $amoo&nbsp;&nbsp;</td><td style='$stn' class='text-right'> $payo&nbsp;&nbsp;</td>
		<td style='$stn' class='text-right'> $balo&nbsp;&nbsp;</td></tr>");
	$pa-=$amo;					  $n++;					$tot+=$amo;					$tba+=$bal;									$tq+=$qty;
						}
						$tpa+=$pai;
	}
			$tba=number_format($tot-($pas+$tpa), 2);				$tq=number_format($tq, 2);			   $pao=number_format($pas+$tpa, 2);
																							$tot=number_format($tot, 2);	

				  print("<th class='text-center' colspan='4'>TOTAL AMOUNT</th>
				  <th class='text-left'> $tq </th><th class='text-right'> $tot </th>
				  <th class='text-right'> $pao </th>
				  <th class='text-right'> $tba </th></table><br><br>");
				}
					else{
						echo"<div class='row'>
            <div class='col-lg-12' style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b> 0 </b></span>
			 <span class='pull-right hidden-print'> $bra &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 class='widget-container fluid-height clearfix'>
            <div class='widget-content padded clearfix'>
			<br><br><br><br><br><br>
	<div style='text-align:center; font-size:24px; color:#ff9999'> Report not available on selected condition </div><br><br><br><br><br><br><br>";
					}
			}
					?>
                                      
                
              </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';
   ?>