<?php
if(basename($_SERVER['PHP_SELF']) == 'salesrepo.php') 
  $fn=" class='current'";
include'header.php';
include'connection.php';
$custo=$conde=$condi=$cond=''; 
$_SESSION['Base']="jblgroup_sales";
$datos=$Date;
$dato = strtotime("-9 days", strtotime("$Date"));
				$dato=date("Y-m-d", $dato);
$p=$k=0;

// search for an item to be added to the chart
if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
		}
		
	if(isset($_POST['addo']))
		{
			$dati=$_POST['dati'];
			$dato=$datos=$dati;
			$sales=str_replace(',', '', $_POST['sales']);
			$vat=str_replace(',', '', $_POST['vat']);
			$sp=$_POST['sp'];
			
		$sso=mysqli_query($conn, "DELETE FROM `z-report` WHERE `Date`='$dati' AND `Location`='$sp'");
			
	$so=mysqli_query($conn, "INSERT INTO `z-report` (`Number`, `Date`, `User`, `Location`, `Sales`, `Vat`) VALUES (NULL, '$dati', '$loge', '$sp', '$sales', '$vat')");
		}
	
	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
          </h3>
                 </div>
     
        <div class="row">
  <div class="col-lg-2 hidden-print">
   
  <ul class="list-group">   

    <li class="list-group-item active">
	  <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>
       
       
      <?php
        include'zmodal.php';
        ?>
           
    <form action="" method="post" class="form-horizontal "> 
        <div class="col-lg-10">
                  <div class="row">        

            <div class="col-lg-5 hidden-print">
                    <div class="col-lg-6" style="padding-left:40px;">
       <button class="btn  btn-warning btn-block" type="button" data-toggle='modal' data-target='#Modaldis' stle="font-size:13px;">Z-REPORT</button></div> 
       
                        <div class="col-lg-6" style="padding-right:40px;">
       <button class="btn  btn-success btn-block" type="button" onclick="location.href='custrepo.php'" stle="font-size:13px;">PAY ON ACCOUNT</button></div></div>
							
        <div class="col-lg-7 hidden-print"><div class="col-lg-2">
			
					   </div>
            <div class="col-lg-3"> 
           <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-3"> 
           <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
    $fo = (strtotime($datos) - strtotime($dato)) / (60 * 60 * 24) + 1;
        ?>
            
					<div class="divFooter"><center><u><b>SALES 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('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 table-bordered" id="tblData" style="font-size:11px;">     
                                      <thead>
                    <tr role="row"><th class="text-center"> No </th>
                       <th class="text-center"> DATE </th>
            <?php
        include'sales.php';
    $seta=mysqli_query($cons, "SELECT *FROM `datatable` WHERE `Status`='0' ORDER BY `Number` ASC");
                        $i=1;
                    $base=$bra=array();
                $feta=mysqli_num_rows($seta);
        while($reta=mysqli_fetch_assoc($seta)){
            $bran=$reta['Branch'];
            $base[$i]=$reta['Link_file'];
            $bra[$i]=$reta['Branch'];
            echo"<th colspan='2' class='text-center'> $bran </th>";
                $i++;
        }
        
        include'connection.php';
        ?>
        
        <th class="text-center" colspan="2"> TRANSPORT </th>
    <th class="text-center" colspan="4"> TOTAL AMOUNT </th></tr>
                        
<tr role="row"><th class="text-center" style="padding:0px;" colspan="2">  </th>            
              
              <?php          
           for($t=1; $t<=$feta; $t++)    
           echo"<th style='font-size:10px;padding:0px;' class='text-center'> SYSTEM SALES </th><th style='font-size:11px;padding:0px;' class='text-center text-primary'> EBM SALES </th>";
           ?>
           <th style='font-size:10px;padding:0px;' class='text-center'> SYSTEM&nbsp;SALES </th><th style='font-size:10px;padding:0px;' class="text-center text-primary"> EBM&nbsp;SALES </th>
           <th style='font-size:10px;padding:0px;' class='text-center'> SYSTEM&nbsp;SALES </th><th style='font-size:10px;padding:0px;' class="text-center text-primary"> EBM&nbsp;SALES </th>
                  <th class="text-center" style="padding:0px;font-size:10px;"> VAT </th>
                  <th class="text-center text-primary" style="padding:0px;font-size:10px;"> VALUE </th></tr>
                        </thead>
                            <tbody>
					<?php
		$n=1;					$date=$dato;            $tot=$tze=array();
		$tall=$tva=0;               
					
		while($date<=$datos){
            $all=$vat=$tsa=0;
				
                $stn="style='padding:1px; font-size:10px;'";
           
		echo"<tr><td $stn class='text-center'>$n</td>
                        <td $stn><center> $date </td>";
            for($i=1; $i<=$feta; $i++){
                $_SESSION['Base']=$base[$i];
                $bran=$bra[$i];
                include'sales.php';
                $amo=$sales=0;
    $sale=mysqli_query($cons, "SELECT SUM(`Quantity`*`Uprice`) AS 'Amount' FROM `sales` WHERE `Date`='$date' AND `Delo`='0' AND `Invoice`!='0'");
                $rale=mysqli_fetch_assoc($sale);
                    $amo=$rale['Amount'];
                    $amoo=number_format($amo, 2);
                      $tot[$i]+=$amo;
                      $all+=$amo;
        echo"<td class='text-right' $stn> $amoo </td>"; 
            include'connection.php';
            
    $zrep=mysqli_query($conn, "SELECT *FROM `z-report` WHERE `Date`='$date' AND `Location`='$bran' AND `Status`='0'");
                $zre=mysqli_fetch_assoc($zrep);
                    $sales=$zre['Sales'];
                    $vat+=$zre['Vat'];
                    $tze[$i]+=$sales;
             $tsa+=$sales;           $saleo=number_format($sales, 2);
        echo"<td class='text-right text-primary' $stn> $saleo </td>"; 
            } 
            
              $amo=$sales=0;
    $sale=mysqli_query($conn, "SELECT SUM(`Amount`*`Rate`) AS 'Amount' FROM `income` WHERE `Date`='$date' AND `Status`='0' AND `Vehicle`!='0' AND `External`='1' AND `Customer`!='2'");
                $rale=mysqli_fetch_assoc($sale);
                    $amo=$rale['Amount'];
                    $amoo=number_format($amo, 2);
                      $tot[1000]+=$amo;
                      $all+=$amo;
        echo"<td class='text-right' $stn> $amoo </td>"; 
            
    $zrep=mysqli_query($conn, "SELECT *FROM `z-report` WHERE `Date`='$date' AND `Location`='TRANSPORT' AND `Status`='0'");
                $zre=mysqli_fetch_assoc($zrep);
                    $sales=$zre['Sales'];
                    $vat+=$zre['Vat'];
                    $tze[1000]+=$sales;
             $tsa+=$sales;           $saleo=number_format($sales, 2);
        echo"<td class='text-right text-primary' $stn> $saleo </td>"; 
                $val=$tsa-$vat;
                
        $allo=number_format($all, 2);
        $valo=number_format($val, 2);
        $vato=number_format($vat, 2);
        $tsao=number_format($tsa, 2);
        
				echo"<td class='text-right' $stn> $allo </td>
				<td class='text-right text-primary' $stn> $tsao </td>
				<td class='text-right' $stn> $vato </td>
				<td class='text-right text-primary' $stn> $valo </td></tr>";
			$n++;           $tall+=$all;            $tva+=$vat;  
			$date = strtotime("+1 day", strtotime("$date"));
				$date=date("Y-m-d", $date);					
						}			

		?>
                    </tbody><thead> 
		<tr><th class="text-center" colspan="2" style="font-size:11px; font-weight:bold;">&nbsp;&nbsp;TOTAL&nbsp;SALES&nbsp;&nbsp;</th>
		<?php
		    $tall=$tell=0;
		for($i=1; $i<=$feta; $i++){
		    $to=$tot[$i];
		    $te=$tze[$i];
		$too=number_format($to, 2);	            $teo=number_format($te, 2);	
	echo"<th class='text-right' style='padding:0px; font-size:11px; font-weight:bold;'> $too </th><th class='text-right text-primary' style='padding:0px; font-size:11px; font-weight:bold;'> $teo </th>";
		    $tall+=$to;                     $tell+=$te;
		}
		
		    $to=$tot[1000];
		    $te=$tze[1000];
		$too=number_format($to, 2);	            $teo=number_format($te, 2);	
	echo"<th class='text-right' style='padding:0px; font-size:11px; font-weight:bold;'> $too </th><th class='text-right text-primary' style='padding:0px; font-size:11px; font-weight:bold;'> $teo </th>";
		    $tall+=$to;                     $tell+=$te;
		
		   $tval=$tell-$tva;					
		$tall=number_format($tall, 2);						
		$tell=number_format($tell, 2);						
		$tva=number_format($tva, 2);						
		$tval=number_format($tval, 2);	
		?>
			<th class='text-right' style="padding:0px; font-size:11px; font-weight:bold;"><?php echo $tall ?></th>
			<th class='text-right text-primary' style="padding:0px; font-size:11px; font-weight:bold;"><?php echo $tell ?></th>
			<th class='text-right' style="padding:0px; font-size:11px; font-weight:bold;"><?php echo $tva ?></th>
			<th class='text-right text-primary' style="padding:0px; font-size:11px; font-weight:bold;"><?php echo $tval ?></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>
			 
			 <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';
   ?>