<?php
if(basename($_SERVER['PHP_SELF']) == 'purcharepo.php') 
$cm=" class='current'";
include'header.php';
include'connection.php';
$dato=$datos=$Date;
$custo=$conde='';
$k=$p=0;

$_SESSION['Base']="jblgroup_sales";
include'sales.php';
// search for an item to be added to the report
    if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$conde="AND `Date` BETWEEN '$dato' AND '$datos'";
			$p=1;

			if($custo)
				$condi="AND `Vendor`='$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">
              <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 active">
              <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-2" style="padding-left:40px;"> </div> 

				   <div class="col-lg-3">
			<select class="form-control" name="custo">
		<option value='' selected='selected'>SELECT A SUPPLIER</option>
			 <?php
$doi=mysqli_query($cons, "SELECT `Vendor` FROM `receive` WHERE `Delo`='0' AND `Vendor`!='' AND `Voucher`!='0' AND `Cnumber`!='' GROUP BY `Vendor` ORDER BY `Vendor` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
				$desto=$roi['Vendor'];
				if($desto==$custo)
					$sle="selected='selected'";
				else
					$sle='';
			echo"<option value='$desto' $sle> $desto </option>";
			}
			?>    
                            </select>
					   </div>      

                         
                       <div class="col-lg-7 hidden-print">
                
            <div class="col-lg-4"> 
           <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center" id="from" 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" id="to" 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($cons, "SELECT SUM(`Quantity`*`Ocost`) AS `Amo`, `Number`, `Vendor`, `Date`, `C02`, `V02`, `Cashier`, `Cnumber` FROM `receive` WHERE `Delo`='0' AND `Vendor`!='SHOP/GISOZI' AND `Vendor`!='MAIN STORE' AND `Vendor`!='SHOP/REMERA' AND `Cnumber`!='' $conde $condi GROUP BY `Cnumber` ORDER BY `Number` ASC");
	else
$do=mysqli_query($cons, "SELECT *FROM (SELECT SUM(`Quantity`*`Ocost`) AS `Amo`,  `Number`, `Vendor`, `Date`, `C02`, `V02`, `Cashier`, `Cnumber` FROM `receive` WHERE `Delo`='0' AND `Vendor`!='SHOP/GISOZI' AND `Vendor`!='MAIN STORE' AND `Vendor`!='SHOP/REMERA' AND `Cnumber`!='' GROUP BY `Cnumber` ORDER BY `Date` DESC LIMIT 15) SUB ORDER BY `Date` ASC");
$fo=mysqli_num_rows($do);

// SUM(`Quantity`*`Ocost`) AS `Amo`, `Number`, `Vendor`, `Date`, `C02`, `V02`, `Cashier`, `Cnumber`
?>


<div class="divFooter"><center><u><b>PURCHASE REPORT <?php echo"$mpri"; ?></b></u></center></div>
             <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b><?php echo" $fo " ?></b></span>
			 <span class="pull-right">&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('tblData')" 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="tblData">     
                                      <thead>
                    <tr role="row">
                     <th> No </th>
                        <th><div align='center'> DATE </th>
                        <th><div align='center'> SYSTEM&nbsp;USER </th>
                       <th><div align='center'> SUPPLIER </th>
                       <th><div align='center'> C&nbsp;NUMBER </th>
                       <th><div align='center'> TOTAL&nbsp;AMUNT </th>
					   <th><div align='center'> TAX&nbsp;BASE&nbsp;<font color='blue'>(C02)</FONT> </th>
					   <th><div align='center'> TAX&nbsp;BASE&nbsp;<font color='blue'>(V02)</font> </th>
                       <th><div align='center'> VAT&nbsp;AMOUNT </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					$n=1;				$tca=$tva=$tv=$tot=0;
						while($ro=mysqli_fetch_assoc($do)){
				$code=$ro['Number'];
				$dest=$ro['Vendor'];
				$dat=$ro['Date'];
			$c02=$ro['C02'];
			$v02=$ro['V02'];
			$val=$sa-$va;
			$user=$ro['Cashier'];
			$cnum=$ro['Cnumber'];
			$amo=$ro['Amo'];
			if($c02)
			$va=($v02*0.18);
			else
			$va=($v02-($v02/1.18));

			$c02o=number_format($c02);		
			$v02o=number_format($v02);			
			$vao=number_format($va, 2);			
			$amoo=number_format($amo, 2);
			$stl="style='padding:1px; font-size:12px;'";
		 
		 if($v02 OR $c02){
		print("<tr><td $stl><div align='right'>$n&nbsp;&nbsp;</td>
						<td class='text-center' $stl> $dat </td>
						<td $stl> $user </td><td $stl> $dest </td>
						<td $stl class='text-right'> $cnum </td>
						<td $stl class='text-right'> $amoo </td>
						<td $stl class='text-right'> $c02o </td>
						<td $stl class='text-right'> $v02o </td>
		                <td $stl class='text-right'> $vao </td></tr>");
							$n++;                  $tca+=$c02;           
				$tva+=$v02;              $tv+=$va;          $tot+=$amo;
						}
				}
						$tca=number_format($tca);
						$tva=number_format($tva);
						$tv=number_format($tv, 2);
						$tot=number_format($tot, 2);					
						?>
						
                    </tbody>
	<tr><th> </th><th colspan='4'>&nbsp;&nbsp;&nbsp;&nbsp; TOTAL VALUE </th>
					<th class='text-right'><?php echo $tot ?></th>
					<th class='text-right'><?php echo $tca ?></th>
					<th class='text-right'><?php echo $tva ?></th>
					<th class='text-right'><?php echo $tv ?></th></tr>
                  </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>
			 
			 <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="exportTableToExcel('tblData')" 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
   include'footer.php';
   ?>
