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

$year=date("Y");
$month=date("F");

// search for an item to be added to the chart
if(isset($_POST['search']))
		{
			$month=$_POST['month'];
			$year=$_POST['year'];
		}
		
            $mon = date('m',strtotime($month));
            $last = date('t',strtotime($month));
            $dato="$year-$mon-01";
            $datos="$year-$mon-$last";

		if($dato==$datos)
			$mpri="ON $dato";
		else
			$mpri="FROM $dato TO $datos";
?>

<script src="style/tableExport.js"></script>
<script type="text/javascript" src="style/jquery.base64.js"></script>
<script src="style/export.js"></script>


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

    <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 active">
	  <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><br><br><br>
  </div>
       
        <form action="" method="post" class="form-horizontal "> 
        <div class="col-lg-10">
                  <div class="row hidden-print">

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

 <div class="col-lg-2 text-right" style="height:34px; padding:8px;"> 
 Month / Year</div><div class="col-lg-2">
 <select class="form-control" name="month" required>
	<?php
    for ($i = 1; $i < 13;   $i++) {
     $date_str = date("F", mktime(0, 0, 0, $i, 10));
	if($date_str==$month)
		$st='selected';
	else
		$st='';
    echo "<option value='$date_str' $st>".$date_str ."</option>";
    } 
	?>
              </select>  
	 </div>
            <div class="col-lg-2">
<select class="form-control" name="year" style="text-align:center;" required>
	<?php
				 $l=date("Y")+1;
				 $e=$l-4;
    for ($i = $l; $i > $e;   $i--) {
		if($i==$year)
			$si='selected';
		else
			$si='';
    echo "<option value='$i' $si>".$i."</option>";
    } 
	?>
              </select>
	</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>
            </form> 

			</div>
<?php
$fo = ((strtotime($datos) - strtotime($dato)) / 86400) + 1;
?>


<div class="divFooter"><center><u><b>TAX REPORT <?php echo"$mpri"; ?></b></u></center></div>
             <div class="row"> </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><th><div align='center'> DATE </th>
                        
            <?php
                $point=$tot=array();                 $n=1;              $tp=0;
            $selo=mysqli_query($conn, "SELECT `Location` FROM `z-report` WHERE `Location`!='' GROUP BY `Location` ORDER BY `Location` DESC");
            $feco=mysqli_num_rows($selo);
            while($relo=mysqli_fetch_assoc($selo)){
                $loca=$relo['Location'];
                $point[$n]=$loca;
            echo"<th><div align='center'> $loca </th>";
                    $n++;
                }
                ?>
                        <th><div align='center'> TOTAL&nbsp;VAT </th>
                       <th><div align='center'> PURCHASE </th>
                       <th><div align='center'> BALANCE </th></tr>
                    </thead>
                                        <tbody>
					<?php
		$date=$dato;
		$stl="style='padding:0px;'";
		while($date<=$datos){
print("<tr><td class='text-center' $stl> $date </td>");
    $tor=0;
    for($i=1; $i<=$feco; $i++){
        $loce=$point[$i];
		    
    $do=mysqli_query($conn, "SELECT SUM(`Vat`) AS 'Sat' FROM `z-report` WHERE `Status`='0' AND `Date`='$date' AND `Location`='$loce' ORDER BY `Number` ASC");
             $ro=mysqli_fetch_assoc($do);
             $vat=$ro['Sat'];
             $tor+=$vat;
             $tot[$i]+=$vat;
             $vato=number_format($vat, 2);
    echo"<td class='text-right' $stl> $vato </td>"; 
            }
		        $toro=number_format($tor, 2);
		print("<td $stl class='text-right'> $toro </td>");
		
		 $pur=0;
    $sese=mysqli_query($conn, "SELECT SUM(`Vat`) AS `Purc` FROM `services` WHERE `Date`='$date' AND `Vat`>'0' AND `External`='0' AND `Status`='0'");
            $rese=mysqli_fetch_assoc($sese);
                $pur+=$rese['Purc'];
            
		 $_SESSION['Base']="jblgroup_sales";
            include'sales.php';
            
    $doi=mysqli_query($cons, "SELECT `C02`, `V02` FROM `receive` WHERE `Delo`='0' AND `V02`!='0' AND `Cnumber`!='' AND `Date`='$date' AND `Voucher`!='0' AND `Vendor`!='SHOP/GISOZI' AND `Vendor`!='MAIN STORE' AND `Vendor`!='SHOP/REMERA' GROUP BY `Cnumber` ORDER BY `Number` ASC");
			while($roi=mysqli_fetch_assoc($doi))
				if($roi['C02'])
				    $pur+=$roi['V02']*0.18;
				else
				    $pur+=$roi['V02']-($roi['V02']/1.18);
				$puro=number_format($pur, 2);
				$bal=$tor-$pur;
				$balo=number_format($bal, 2);
				$tp+=$pur;
		
			print("<td $stl class='text-right'> $puro </td>
			<td $stl class='text-right'> $balo </td></tr>");
			
			$date = strtotime("+1 day", strtotime($date));
            $date = date("Y-m-d", $date);
						}					
						?>
						
                    </tbody>
	        <tr><th> TOTAL AMOUNT </th>
	        <?php
	                $tor=0;
	            for($i=1; $i<=$feco; $i++){
	                $to=$tot[$i];
	                $tor+=$to;
	       $too=number_format($to, 2);
					echo"<th class='text-right'> $too </th>";
					} 
		   $toro=number_format($tor, 2);
		   $bal=$tor-$tp;
				$tpo=number_format($tp, 2);
				$balo=number_format($bal, 2);
				?>
		<th class='text-right'><?php echo $toro ?></th>
		<th class='text-right'><?php echo $tpo ?></th>
		<th class='text-right'><?php echo $balo ?></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';
   ?>
