<?php
  switch($Etape)
  {
    case 0:
	  echo "<script language=\"javascript\">\n";
	  echo "jQuery(document).ready(function($) {\n"; 
	  echo "  jQuery('.wbdatepicker').datepicker($.datepicker.regional[ \"fr-CH\" ]);\n";
	  echo "  jQuery('input').blur();\n";
	  echo "});\n";
	  echo "</script>\n";

      echo "<form name='info'>";
      echo "<TABLE border=1><TR><TH>Date</TH><TD>";
	  //.Cree_Entree_Type("intervalle",2,0,"",RECHERCHE).
      echo "<input type='text' class='wbdatepicker' name='DateDebut' size='10'> au <input type='text' class='wbdatepicker' name='DateFin' size='10'>";
	  echo "</TD></TR></TABLE>";
      echo "<input type='hidden' name='Type' value=$Type>";
      echo "<input type='hidden' name='Sous_Type' value=$Sous_Type>";
      echo "<input type='hidden' name='Etape' value='1'>";
      echo "<script language=JavaScript>\n";
      echo "force_submit=true;\n";
      echo "this.document.forms['info'].elements[0].focus();\n";
      echo "</script>\n";
      echo "<button type='button' onClick='submit_ok(this.form);'>Rechercher</button>";
      echo "</form>";
    break;
    case 1:
	  $intervalle[0]=substr($DateDebut,0,2);
      $intervalle[1]=substr($DateDebut,3,2);
      $intervalle[2]=substr($DateDebut,6,4);
      $intervalle[3]=substr($DateFin,0,2);
      $intervalle[4]=substr($DateFin,3,2);
      $intervalle[5]=substr($DateFin,6,4);

      $resultat = Employee::getMecano();
      $max_types_heures_atelier = 0;
      $rapport=array();
      foreach ($resultat as $ligne)
      {
        $nom_employe=$ligne["Nom"]." ".$ligne["Prenom"];
        $rapport[$nom_employe] = array();
        $types_heures_atelier = 0;
        $articles_heures_atelier = array_filter(explode(";",$ligne["ArticlesAtelier"]));
        $tot_heures_atelier = 0;
        foreach ($articles_heures_atelier as $article_heures_atelier)
        {
          $types_heures_atelier++;
          if ($types_heures_atelier > $max_types_heures_atelier)
          {
            $max_types_heures_atelier = $types_heures_atelier;
          }
		  $intervalleQString = "DateCommande >= '".substr($DateDebut,6,4)."-".substr($DateDebut,3,2)."-".substr($DateDebut,0,2)."' AND DateCommande <= '".substr($DateFin,6,4)."-".substr($DateFin,3,2)."-".substr($DateFin,0,2)."'";
          $requette2 = "SELECT SUM(commandearticles".ID_GARAGE.".Quantite) FROM commande".ID_GARAGE.", commandearticles".ID_GARAGE." WHERE commande".ID_GARAGE.".Genre IN ('F', 'G') AND ".$intervalleQString." AND commande".ID_GARAGE.".NoCommande = commandearticles".ID_GARAGE.".NoCommande AND commandearticles".ID_GARAGE.".TypeData = 0 AND commandearticles".ID_GARAGE.".NoArticle = '$article_heures_atelier'";
		  //echo $requette2."<br/>\n";
          $resultat2 = Connection::executeRawQueryWithfetchAll($requette2);
          if ($ligne2 = $resultat2[0])
          {
            $rapport[$nom_employe][$article_heures_atelier] = $ligne2[0];
            $tot_heures_atelier += $ligne2[0];
          }
        }
        if ($ligne["ArticleDepannage"] != "")
        {
		  $intervalleQString = "DateCommande >= '".substr($DateDebut,6,4)."-".substr($DateDebut,3,2)."-".substr($DateDebut,0,2)."' AND DateCommande <= '".substr($DateFin,6,4)."-".substr($DateFin,3,2)."-".substr($DateFin,0,2)."'";
          $requette2 = "SELECT SUM(commandearticles".ID_GARAGE.".Quantite) FROM commande".ID_GARAGE.", commandearticles".ID_GARAGE." WHERE commande".ID_GARAGE.".Genre IN ('F', 'G') AND ".$intervalleQString." AND commande".ID_GARAGE.".NoCommande = commandearticles".ID_GARAGE.".NoCommande AND commandearticles".ID_GARAGE.".TypeData = 0 AND commandearticles".ID_GARAGE.".NoArticle = '".$ligne["ArticleDepannage"]."'";
          $resultat2 = Connection::executeRawQueryWithfetchAll($requette2);
          if ($ligne2 = $resultat2[0])
          {
            $rapport[$nom_employe]["Depannage"] = $ligne2[0];
          }
        }
        $tmp1=$intervalle[2];
        $tmp2=$intervalle[1];
        $tmp3=$intervalle[0];
        if (strlen($tmp1)<4)
        {
          $tmp1 = substr("2000",0,4-strlen($tmp1)). $tmp1;
        }
        else if (strlen($tmp1)>4)
        {
          $erreur=1;
        }
        if (strlen($tmp2)<2)
        {
          $tmp2 = substr("01",0,2-strlen($tmp2)). $tmp2;
        }
        else if (strlen($tmp2)>2)
        {
          $erreur=1;
        }
        if (strlen($tmp3)<2)
        {
          $tmp3 = substr("01",0,2-strlen($tmp3)). $tmp3;
        }
        else if (strlen($tmp3)>2)
        {
          $erreur=1;
        }
        $start_jour = $tmp3;
        $start_mois = $tmp2;
        $start_annee = $tmp1;
        $start = $tmp1 . "-" . $tmp2 . "-" . $tmp3;
        $tmp1=$intervalle[5];
        $tmp2=$intervalle[4];
        $tmp3=$intervalle[3];
        if (strlen($tmp1)<4)
        {
          $tmp1 = substr("2000",0,4-strlen($tmp1)). $tmp1;
        }
        else if (strlen($tmp1)>4)
        {
          $erreur=1;
        }
        if (strlen($tmp2)<2)
        {
          $tmp2 = substr("01",0,2-strlen($tmp2)). $tmp2;
        }
        else if (strlen($tmp2)>2)
        {
          $erreur=1;
        }
        if (strlen($tmp3)<2)
        {
          $tmp3 = substr("01",0,2-strlen($tmp3)). $tmp3;
        }
        else if (strlen($tmp3)>2)
        {
          $erreur=1;
        }
        $end = $tmp1 . "-" . $tmp2 . "-" . $tmp3;
        $tab_absences = array();
        $requette2 = "SELECT * FROM absenceemploye".ID_GARAGE." WHERE NoClient IN (0, ".$ligne["NoClient"].") AND ((Fin>='$start' AND Fin<='$end') OR (Debut>='$start' AND Debut<='end')) ORDER BY Debut";
        $resultat2 = Connection::executeRawQueryWithfetchAll($requette2);
        foreach($resultat2 as $ligne2)
        {
          $jour=substr($ligne2["Debut"],8,2);
          $mois=substr($ligne2["Debut"],5,2);
          $annee=substr($ligne2["Debut"],0,4);
          if($ligne2["Debut"]==$ligne2["Fin"])
          {
            if(isset($tab_absences[$ligne2["Debut"]]))
            {
              switch($tab_absences[$ligne2["Debut"]])
              {
                case 1:
                break;
                case 2:
                  if($ligne2["FinMA"]==1)
                  {
                    $tab_absences[$ligne2["Debut"]]=1;
                  }
                break;
                case 3:
                  if($ligne2["DebutMA"]==0)
                  {
                    $tab_absences[$ligne2["Debut"]]=1;
                  }
                break;
              }
            }
            else
            {
              if($ligne2["DebutMA"]==0)
              {
                if($ligne2["FinMA"]==0)
                {
                  $tab_absences[$ligne2["Debut"]]=2;
                }
                else
                {
                  $tab_absences[$ligne2["Debut"]]=1;
                }
              }
              else
              {
                $tab_absences[$ligne2["Debut"]]=3;
              }
            }
          }
          else
          {
            if(isset($tab_absences[$ligne2["Debut"]]))
            {
              switch($tab_absences[$ligne2["Debut"]])
              {
                case 1:
                break;
                case 2:
                  $tab_absences[$ligne2["Debut"]]=1;
                break;
                case 3:
                  if($ligne2["DebutMA"]==0)
                  {
                    $tab_absences[$ligne2["Debut"]]=1;
                  }
                break;
              }
            }
            else
            {
              if($ligne2["DebutMA"]==0)
              {
                $tab_absences[$ligne2["Debut"]]=1;
              }
              else
              {
                $tab_absences[$ligne2["Debut"]]=3;
              }
            }
            $cpt=1;
            $date=mktime(0,0,0,$mois,$jour+$cpt,$annee);
            $date_str=date("Y-m-d",$date);
            while($date_str!=$ligne2["Fin"])
            {
              $tab_absences[$date_str]=1;
              $cpt++;
              $date=mktime(0,0,0,$mois,$jour+$cpt,$annee);
              $date_str=date("Y-m-d",$date);
            }
            if(isset($tab_absences[$ligne2["Fin"]]))
            {
              switch($tab_absences[$ligne2["Fin"]])
              {
                case 1:
                break;
                case 2:
                  if($ligne2["FinMA"]==1)
                  {
                    $tab_absences[$ligne2["Debut"]]=1;
                  }
                break;
                case 3:
                  $tab_absences[$ligne2["Debut"]]=1;
                break;
              }
            }
            else
            {
              if($ligne2["FinMA"]==0)
              {
                $tab_absences[$ligne2["Debut"]]=2;
              }
              else
              {
                $tab_absences[$ligne2["Debut"]]=1;
              }
            }
          }
        }
        //print_r($tab_absences);
        $cpt=0;
        $tot_pot=0;
        $continue=true;
        while($continue)
        {
          $date=mktime(0,0,0,$start_mois,$start_jour+$cpt,$start_annee);
          $date_str=date("Y-m-d",$date);
          if(jour_ferie(date("d",$date),date("m",$date),date("Y",$date))!=0)
          {
            if(isset($tab_absences[$date_str]))
            {
              if($tab_absences[$date_str]==1)
              {
              }
              else
              {
                $tot_pot+=0.5;
              }
            }
            else
            {
              $tot_pot+=1.0;
            }
          }
          if ($date_str==$end)
          {
            $continue=false;
          }
          $cpt++;
        }
        $rapport[$nom_employe]["Total"] = $tot_heures_atelier;
        $rapport[$nom_employe]["Potentiel"] = $tot_pot * $ligne["HeureAtelier"];
        $rapport[$nom_employe]["Pourcent"] = round($rapport[$nom_employe]["Total"] / $rapport[$nom_employe]["Potentiel"] * 100,2)."%";
      }
      echo "<table><tr><th>Nom</th>";
      for ($cpt=1;$cpt<=$max_types_heures_atelier;$cpt++)
      {
        echo "<th>Type $cpt</th><th>Total $cpt</th>";
      }
      echo "<th>Depannage</th><th>Total heures</th><th>Potentiel</th><th>Pourcent</th></tr>\n";
      foreach ($rapport as $nom => $employe)
      {
        echo "<tr><th>$nom</th>";
        $cpt = 0;
        foreach ($employe as $col => $valeur)
        {
          if ($col == "Depannage")
          {
            while ($cpt<$max_types_heures_atelier)
            {
              echo "<td>&nbsp;</td><td>&nbsp;</td>";
              $cpt++;
            }
          }
          if ($cpt < $max_types_heures_atelier)
          {
            echo "<td>$col</td>";
          }
          echo "<td>$valeur</td>";
          $cpt++;
        }
        echo "</tr>";
      }
      echo "</table>";
/*      echo "<pre>\n";
      print_r($rapport);
      echo "</pre>\n";*/
    break;
  }
?>
