<?php
session_start();
if( $_SESSION["user"] == "" || $_SESSION["passwd"] == "" || $_SESSION["admin"] == false ) {
   mysqli_close($conn);
   header("Location: index.php");
}


include_once('db_config.php');

$Title  = "Retenciones.";
$Year   = isset($_POST['dyear']) ? $_POST["dyear"]  : "";
$Month  = isset($_POST['dmonth']) ? $_POST['dmonth'] : "";
$hYear  = isset($_POST['hyear']) ? $_POST["hyear"]  : "";
$hMonth = isset($_POST['hmonth']) ? $_POST['hmonth'] : "";
$ret = isset($_POST['ret']) ? $_POST['ret'] : "1";
$Fecha  = "";
$YS2011 = "";
$YS2012 = "";
$YS2013 = "";
$YS2014 = "";
$YS2015 = "";
$YS2016 = "";
$YS2017 = "";
$YS2018 = "";
$YS2019 = "";

$hYS2011 = "";
$hYS2012 = "";
$hYS2013 = "";
$hYS2014 = "";
$hYS2015 = "";
$hYS2016 = "";
$hYS2017 = "";
$hYS2018 = "";
$hYS2019 = "";

if( empty($Year) ) {        
   if( $_SESSION["dYear"] != "" ) $Year = $_SESSION["dYear"];
   else {
      $Year = date("Y");
   }
}

if( empty($Month) ) {       
   if( $_SESSION["dMonth"] != "" ) $Month = $_SESSION["dMonth"];
   else {
     $Month = date("n");
   }
}

if( empty($hYear) ) {        
   if( $_SESSION["hYear"] != "" ) $hYear = $_SESSION["hYear"];
   else {
      $hYear = date("Y");
   }
}

if( empty($hMonth) ) {       
//if( $hMonth == "" ) {       
   if( $_SESSION["hMonth"] != "" ) $hMonth = $_SESSION["hMonth"];
   else {
     $hMonth = date("n");
   }
}

//echo $hYear . "<br/>";
//echo $hMonth . "<br/>";

$_SESSION["Year"]  = $Year;
$_SESSION["Month"] = $Month;

$sm  = array("", "", "", "", "", "", "", "", "", "", "", "");
$sm [$Month-1]  = "selected";
$ss[$ret-1] = "selected";

if( $Year == "2011")  $YS2011 = "selected";
if( $Year == "2012")  $YS2012 = "selected";
if( $Year == "2013")  $YS2013 = "selected";
if( $Year == "2014")  $YS2014 = "selected";
if( $Year == "2015")  $YS2015 = "selected";
if( $Year == "2016")  $YS2016 = "selected";
if( $Year == "2017")  $YS2017 = "selected";
if( $Year == "2018")  $YS2018 = "selected";
if( $Year == "2019")  $YS2019 = "selected";

include 'header.php'; 
?>
<style>
.dropdown-content {
  overflow-x: hidden;
}

.dropdown-content li > a, .dropdown-content li > span {
/*   padding-left: 0;
   padding-right: 0;*/
}


#therow {
   background-color: white; 
}

#invoices:hover {
   background-color: #cfd8dc; 
}

.data {
   margin-top: 0; 
   margin-bottom: 0; 
/*   padding-left: 5%; */
}


.margin {
   margin-top: 0; 
   margin-bottom: 0; 
}

.margin-label {
   margin-top: 5px; 
}

</style>
  <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <div id="content">
  <div class="container" style="width:90%" >
    <div style="height: 15px;"></div>
    <div class="col l9 m12 s12">
      <div class="page-content">
        <div style="height:20px;"></div>
        <form id="iva" action="iibb.php" method="post"> 
        <div class="row">
         <div class="input-field col s2">
            <select name="dmonth">
 <?php
            echo "<option value='' disabled>Seleccione el Mes</option>";        
                echo "<option value='1' " . $sm[0] . ">Enero</option>";
                echo "<option value='2' " . $sm[1] . ">Febrero</option>";
                echo "<option value='3' " . $sm[2] . ">Marzo</option>";
                echo "<option value='4' " . $sm[3] . ">Abril</option>";
                echo "<option value='5' " . $sm[4] . ">Mayo</option>";
                echo "<option value='6' " . $sm[5] . ">Junio</option>";
                echo "<option value='7' " . $sm[6] . ">Julio</option>";
                echo "<option value='8' " . $sm[7] . ">Agosto</option>";
                echo "<option value='9' " . $sm[8] . ">Septiembre</option>";
                echo "<option value='10' " . $sm[9] . ">Octubre</option>";
                echo "<option value='11' " . $sm[10] . ">Noviembre</option>";
                echo "<option value='12' " . $sm[11] . ">Diciembre</option>";
?>                
            </select>
            <label>Mes</label>
         </div>      
         <div class="input-field col s2">
              <select  name="dyear">
              <option value="" disabled selected>Seleccione el A&ntilde;o</option>
<?php
                echo "<option value='2011' " . $YS2011 . ">2011</option>";
                echo "<option value='2012' " . $YS2012 . ">2012</option>";
                echo "<option value='2013' " . $YS2013 . ">2013</option>";
                echo "<option value='2014' " . $YS2014 . ">2014</option>";
                echo "<option value='2015' " . $YS2015 . ">2015</option>";
                echo "<option value='2016' " . $YS2016 . ">2016</option>";
                echo "<option value='2017' " . $YS2017 . ">2017</option>";
                echo "<option value='2018' " . $YS2018 . ">2018</option>";
                echo "<option value='2019' " . $YS2019 . ">2019</option>";
?>              
              </select>
              <label>A&ntilde;o</label>
         </div>    
         <div class="input-field col s2">
              <select name="ret">
              <option value="" disabled selected>Retenciones</option>
<?php
                echo "<option value='1'" . $ss[0] . ">Todas</option>";
                echo "<option value='2'" . $ss[1] . ">Ingresos Brutos</option>";
                echo "<option value='3'" . $ss[2] . ">Ganancias</option>";
                echo "<option value='4'" . $ss[3] . ">IVA</option>";
                echo "<option value='5'" . $ss[4] . ">Otras</option>";
?>              
              </select>
              <label>Retenciones</label>
         </div>    

         <button class="btn waves-effect waves-light btn col s1" style="margin-top:25px; margin-left: 30px;" type="submit" name="action" >Go!</button>
        </row>  
        </form>
        <div style="height:70px;"></div>
      </div>  <!-- page-content --> 
<style>
th {
 font-weight: normal;
 color: black;
}
</style>
<?php
   $xor = 0;
   echo "<table id='detail' class='highlight' style='color:grey; font-weight:normal;'>";
   echo "<thead>";
   echo "<tr>";
     echo "<div class='row data grey lighten-2' style='padding-top:10px; padding-bottom:10px;'>";
     echo "<div class='col s1' style='border-radius:0; text-decoration:underline; width:9%;'>Fecha</div>";
     echo "<div class='col s3' style='border-radius:0; text-decoration:underline; width:12%;'>N&uacute;mero</div>";
     echo "<div class='col s3' style='border-radius:0; text-decoration:underline; width:27%; overflow:hidden;'>Nombre</div>";
     echo "<div class='col s1' style='border-radius:0; text-decoration:underline; width:12%;'>CUIT</div>";
     echo "<div class='col s2 right-align' style='border-radius:0; text-decoration:underline; width:10%;'>Imponible</div>";
     echo "<div class='col s2 right-align' style='border-radius:0; text-decoration:underline; width:10%;'>IIBB</div>";
     echo "<div class='col s2 right-align' style='border-radius:0; text-decoration:underline; width:10%;'>Ganancias</div>";
     echo "<div class='col s2 right-align' style='border-radius:0; text-decoration:underline; width:10%;'>IVA</div>";
   echo "</div>";
   echo "</tr>"; 
   echo "</thead>"; 
   echo "<tbody />"; 
   echo "</table>"; 

   $sql = "select fecha, numero, empresas.nombre, cuit, (iva_105+iva_21+iva_27+iva_mt) as imponible, retenciones, Retenciones_Ganancias as ret_gan, retenciones_iva as ret_iva from comprobantes left join empresas on empresas.id = comprobantes.cliente where (operacion<>1 and operacion<>6) and year(fecha)='"  . $Year . "' and month(fecha)='" . $Month . "' and (retenciones > 0 or retenciones_ganancias > 0 or retenciones_iva > 0 ) and comprobantes.Borrado=0 order by fecha";
   $result = $conn->query($sql);
   if ($result->num_rows > 0) {

      $TI = $TIB = $TGAN = $TIVA = 0;
      while($row = $result->fetch_assoc()) {

         if( $row["retenciones"] > 0 && ( $ret == 1 || $ret == 2 ) ||
             $row["ret_gan"] > 0 && ( $ret == 1 || $ret == 3 ) ||
             $row["ret_iva"] > 0 && ( $ret == 1 || $ret == 4 )  ) {


            $date = date_create($row["fecha"]);
            $fecha = date_format($date, 'd-m-Y');
            
            $TI += $row["imponible"];   
            $TIB += $row["retenciones"];   
            $TGAN += $row["ret_gan"];   
            $TIVA += $row["ret_iva"];   
            if( $xor ^= 1 ) 
               echo "<div class='row data blue-grey lighten-5'>";
            else echo "<div class='row data'>";
            echo "<div class='input-field col s2' style='width:9%;'>" . $fecha . "</div>";
            echo "<div class='input-field col s1' style='width:12%;'>" . $row["numero"] . "</div>";
            echo "<div class='input-field col s3' style='width:27%;'>" . $row["nombre"] . "</div>";
            echo "<div class='input-field col s1' style='width:12%;'>" . $row["cuit"] . "</div>";
            
            echo "<div class='input-field col s2 right-align' style='width:10%;'>" . number_format( $row["imponible"], 2, ',' ,  '.' ) . "</div>";
            if( $row["retenciones"] > 0 && ( $ret == 1 || $ret == 2 ) ) 
               echo "<div class='input-field col s2 right-align' style='width:10%;'>" . number_format( $row["retenciones"], 2, ',' ,  '.' ) . "</div>";
            else echo "<div class='input-field col s2 right-align' style='width:10%;'></div>";
            
            if( $row["ret_gan"] > 0  && ( $ret == 1 || $ret == 3 ) ) 
               echo "<div class='input-field col s2 right-align' style='width:10%;'>" . number_format( $row["ret_gan"], 2, ',' ,  '.' ) . "</div>";
            else echo "<div class='input-field col s2 right-align' style='width:10%;'></div>";
            
            if( $row["ret_iva"] > 0  && ( $ret == 1 || $ret == 4 ) ) 
               echo "<div class='input-field col s2 right-align' style='width:10%;'>" . number_format( $row["ret_iva"], 2, ',' ,  '.' ) . "</div>";
            else echo "<div class='input-field col s2 right-align' style='width:10%;'></div>";
            echo "</div>";
         }
      }
   }

   echo "<div class='row data grey lighten-2'><b>";
   echo "<div class='input-field col s1 right-align' style='width:60%;'></div>";
   echo "<div class='input-field col s2 right-align' style='width:10%; margin-bottom:10px;'>" . number_format( $TI, 2, ',' ,  '.' ) . "</div>";
   echo "<div class='input-field col s2 right-align' style='width:10%;'>" . number_format( $TIB, 2, ',' ,  '.' ) . "</div>";
   echo "<div class='input-field col s2 right-align' style='width:10%;'>" . number_format( $TGAN, 2, ',' ,  '.' ) . "</div>";
   echo "<div class='input-field col s2 right-align' style='width:10%;'>" . number_format( $TIVA, 2, ',' ,  '.' ) . "</div>";
   echo "</b></div>";

    mysqli_close($conn);
?>        
   </div>   <!-- column --> 
   <div style="height:100px;"></div>
   </div>            <!-- container --> 
   </div>            <!-- content -->

   <script type="text/javascript" src="js/jquery.min.js"></script>
   <script type="text/javascript" src="js/jquery.form.js"></script> 
   <script type="text/javascript" src="js/materialize.js"></script>  
   <script src="js/numeral.min.js"></script>
   <script src="js/jquery.maskedinput.min.js" type="text/javascript"></script>
<script>

   $(document).ready(function() {
      $('select').material_select();

      $('#month').on('change', function() {
         Month = this.value;
         console.log( this.value )
      });   
      $('#year').on('change', function() {
         Year = this.value;
         console.log( this.value )
      });   
   });   

   // Post to the provided URL with the specified parameters.
   function post(path, parameters) {
      var form = $('<form></form>');

       form.attr("method", "post");
       form.attr("action", path);

       $.each(parameters, function(key, value) {
           var field = $('<input></input>');
           field.attr("type", "hidden");
           field.attr("name", key);
           field.attr("value", value);
           form.append(field);
       });

       // The form needs to be a part of the document in
       // order for us to be able to submit it.
       $(document.body).append(form);
      form.submit();
   };

</script>
</body>
</html>

