<script language="javascript">
jQuery(document).ready(function($) { jQuery('.wbdatepicker').datepicker($.datepicker.regional[ "fr-CH" ]); });
</script>
<?php //create_cache(); ?>
<div class="div_location">
<?php
  $max=8;
  if(isset($datestr))
  {
    $jour=substr($datestr,0,2);
    $mois=substr($datestr,3,2);
    $annee=substr($datestr,6,4);
  }
  else if(!isset($jour))
  {
    $jour=date("d");
    $mois=date("m");
    $annee=date("Y");
  }
  if(!isset($Categorie))
  {
    $Categorie=0;
  }
  echo "<TABLE cellspacing='0' class='tab_location'>\n";
  echo "<TR class='tr_jour'><TD class='td_label_vhc'>VHC</TD>";
  for($cpt=-1;$cpt<$max;$cpt++)
  {
    $date=mktime(0,0,0,$mois,$jour+$cpt,$annee);
    $a_jour = date("d",$date);
    $a_mois = date("m",$date);
    $a_annee = date("Y",$date);
    $a_semaine = date("w",$date);
    echo "<TD colspan=6 class='td_jour ";
    if (jour_ferie($a_jour,$a_mois,$a_annee,1)==0)
    {
      echo "jour_ferie";
    }
    else
    {
      echo "jour_ouvert";
    }
    echo "'>" . nom_jour($a_semaine) . " $a_jour " . nom_mois($a_mois) . " $a_annee</TD>";
  }
  echo "</TR>\n";
  $requette = "SELECT * FROM vehiculeslocation".ID_GARAGE."";
  if ($Categorie!=0)
  {
    $requette .= " WHERE CategorieMulti LIKE '%$Categorie%'";
  }
  else
  {
    $resultata = CatLoc::getCatlocByAncien();
    $first = true;
    foreach ($resultata as $lignea)
    {
      if($first) {
        $requette .= " WHERE CategorieMulti NOT LIKE '%".$lignea[0]."%'";
        $first = false;
      } else {
        $requette .= " AND CategorieMulti NOT LIKE '%".$lignea[0]."%'";
      }
    }
  }
  $requette .= " ORDER BY Position";
  if ($resultat = Connection::executeRawQueryWithfetchAll($requette))
  {
    foreach($resultat as $ligne)
    {
        if($ligne2 = Vehicule::getVehiculeByNoChassis($ligne['NoChasis']))
        {
          $nom_vhc = $ligne2['Model'] . " " . $ligne['NoPlaque'];
        }
      $date=mktime(0,0,0,$mois,$jour-1,$annee);
      $a_jour = date("d",$date);
      $a_mois = date("m",$date);
      $a_annee = date("Y",$date);
      $date_debut="$a_annee-$a_mois-$a_jour 00:00:00";
      $date=mktime(0,0,0,$mois,$jour+$max-1,$annee);
      $a_jour = date("d",$date);
      $a_mois = date("m",$date);
      $a_annee = date("Y",$date);
      $date_fin="$a_annee-$a_mois-$a_jour 23:59:59";
      $nb_loc=0;
      if ($resultat3 = Location::getByNoChassisAndDate($ligne['NoChasis'], $date_debut, $date_fin))
      {
        foreach($resultat3 as $ligne3)
        {
          $deb=$ligne3['Debut'];
          $fin=$ligne3['Fin'];
          $loc_deb[$nb_loc]=substr($deb,0,4).substr($deb,5,2).substr($deb,8,2);
          $loc_no_commande[$nb_loc]=$ligne3['NoCommande'];
          $t_deb=substr($deb,11,2);
          switch($t_deb)
          {
            case 0: case 1:
            case 2: case 3:
            case 4: case 5:
            case 6: case 7:
            case 8: case 9:
              $loc_deb[$nb_loc].="00";
            break;
            case 10: case 11:
              $loc_deb[$nb_loc].="01";
            break;
            case 12: case 13:
              $loc_deb[$nb_loc].="02";
            break;
            case 14: case 15:
              $loc_deb[$nb_loc].="03";
            break;
            case 16: case 17:
              $loc_deb[$nb_loc].="04";
            break;
            case 18: case 19:
            case 20: case 21:
            case 22: case 23:
              $loc_deb[$nb_loc].="05";
            break;
          }
          $loc_fin[$nb_loc]=substr($fin,0,4).substr($fin,5,2).substr($fin,8,2);
          $t_fin=substr($fin,11,2);
          switch($t_fin)
          {
            case 0: case 1: case 2:
            case 3: case 4:
            case 5: case 6:
            case 7: case 8:
            case 9: case 10:
              $loc_fin[$nb_loc].="00";
            break;
            case 11: case 12:
              $loc_fin[$nb_loc].="01";
            break;
            case 13: case 14:
              $loc_fin[$nb_loc].="02";
            break;
            case 15: case 16:
              $loc_fin[$nb_loc].="03";
            break;
            case 17: case 18:
              $loc_fin[$nb_loc].="04";
            break;
            case 19: case 20:
            case 21: case 22:
            case 23:
              $loc_fin[$nb_loc].="05";
            break;
          }
          $loc_etape[$nb_loc]=$ligne3['Etape'];
          $loc_nom[$nb_loc]= "<A TITLE='Debut ". Cree_Entree_Type("",18,0,$ligne3['Debut'],AFFICHAGE)." Fin ".Cree_Entree_Type("",18,0,$ligne3['Fin'],AFFICHAGE)." Total ". $ligne3['Total'] ."CHF Tel:".$ligne3['Telephone']."' HREF='location.php?new=0&Start=" . htmlspecialchars($ligne3['Debut']) . "&vhc=" . htmlspecialchars($ligne3['NoChasis']) . "' TARGET='_blank' rel='opener'>" . substr($ligne3['Nom'],0,10) . " " . substr($ligne3['Prenom'],0,1) . "&nbsp;</A>";
          $nb_loc++;
        }
      }
      echo "<TR class='tr_vhc'>\n <TD class='td_vhc'><A HREF='vhc_loc.php?NoChasis=".htmlspecialchars($ligne['NoChasis'])."' TARGET='_blank' rel='opener'>$nom_vhc</A></TD>\n";
      $cpt_rdv=0;
      $loc_start=0;
      $loc_stop=0;
      $loc_size=0;
      for($cpt=-1;$cpt<$max;$cpt++)
      {
        $date=mktime(0,0,0,$mois,$jour+$cpt,$annee);
        $a_jour = date("d",$date);
        $a_mois = date("m",$date);
        $a_annee = date("Y",$date);
        $a_semaine = date("w",$date);
        for($cpt2=0;$cpt2<6;$cpt2++)
        {
          $tmp_txt="<A TITLE='$nom_vhc $a_jour.$a_mois.$a_annee' HREF='location.php?new=1&jour=$a_jour&mois=$a_mois&anneed=$a_annee&periode=$cpt2&vhc=" . htmlspecialchars($ligne['NoChasis']) . "' TARGET='_blank' rel='opener'>".($cpt2*2+8)."h</A>";
          $loc_size++;
          if ($loc_start==0)
            echo " <TD class='td_loc";
          if ($cpt2<10)
            $tmp_date= $a_annee . $a_mois . $a_jour . "0" . $cpt2;
          else
            $tmp_date= $a_annee . $a_mois . $a_jour . $cpt2;
          if ($nb_loc>$cpt_rdv) /* location en cours */
          {
            if ($tmp_date>=$loc_deb[$cpt_rdv] && $loc_start==0) /* début location */
            {
              switch ($loc_etape[$cpt_rdv])
              {
                case 0:
                  echo " loc_reserve'";
                break;
                case 1:
                  echo " loc_en_cours'";
                break;
                case 2:
                  if($ligne4 = Commande::getCommandeByNoCommande($loc_no_commande[$cpt_rdv]))
                  {
                    if($ligne4["Paye"]=="O")
                    {
                      echo " loc_retour_facture_paye'";
                    }
                    else
                    {
                      echo " loc_retour_facture'";
                    }
                  }
                  else
                  {
                    echo " loc_retour_facture'";
                  }
                break;
                case 3:
                  echo " loc_retour_non_facture'";
                break;
              }
              $loc_start=1;
              $loc_size=1;
              if($tmp_date>$loc_deb[$cpt_rdv])
              {
                $loc_nom[$cpt_rdv]=htmlspecialchars("<-- ") . $loc_nom[$cpt_rdv];
              }
            }
            if ($tmp_date>=$loc_fin[$cpt_rdv])
            {
              $loc_start=0;
              $loc_stop=1;
              echo " COLSPAN=$loc_size";
              $tmp_txt=$loc_nom[$cpt_rdv];
              $cpt_rdv++;
              $loc_size=0;
            }
            else if (jour_ferie($a_jour,$a_mois,$a_annee,1)==0 && $loc_start==0 && $loc_stop==0)
            {
              echo " loc_ferie loc_pos_$cpt2'";
            }
            else if ($loc_start==0 && $loc_stop==0)
            {
              echo " loc_ouvert loc_pos_$cpt2'";
            }
            else
            {
            }
          }
          else if (jour_ferie($a_jour,$a_mois,$a_annee,1)==0)
          {
            echo " loc_ferie loc_pos_$cpt2'";
          }
          else
          {
            echo " loc_ouvert loc_pos_$cpt2'";
          }
          if ($loc_start==0)
            echo ">$tmp_txt</TD>\n";
          $loc_stop=0;
        }
      }
      if ($loc_start==1)
      {
        echo " COLSPAN=$loc_size";
        $tmp_txt="<B>".$loc_nom[$cpt_rdv]. htmlspecialchars(" -->") ."</B>";
        echo ">$tmp_txt</TD>\n";
      }
      echo "</TR>\n";
    }
  }
  echo "<TR class='tr_jour'><TD class='td_label_vhc'>VHC</TD>";
  for($cpt=-1;$cpt<$max;$cpt++)
  {
    $date=mktime(0,0,0,$mois,$jour+$cpt,$annee);
    $a_jour = date("d",$date);
    $a_mois = date("m",$date);
    $a_annee = date("Y",$date);
    $a_semaine = date("w",$date);
    echo "<TD colspan=6 class='td_jour ";
    if (jour_ferie($a_jour,$a_mois,$a_annee,1)==0)
    {
      echo "jour_ferie";
    }
    else
    {
      echo "jour_ouvert";
    }
    echo "'>" . nom_jour($a_semaine) . " $a_jour " . nom_mois($a_mois) . " $a_annee</TD>";
  }
  echo "</TR>\n";
  echo "</TABLE>\n";
  echo "<TABLE class='table_av_ap'><TR><TD><form><INPUT type='hidden' name='Type' value='5'><INPUT type='hidden' name='Sous_Type' value='1'>";
  $date=mktime(0,0,0,$mois,$jour-7,$annee);
  $a_jour = date("d",$date);
  $a_mois = date("m",$date);
  $a_annee = date("Y",$date);
  echo "<SCRIPT language=JavaScript>\ntype=$Type;\nsous_type=$Sous_Type;\nmemnoform=6;\nCategorie=$Categorie;\njour_down=$a_jour;\nmois_down=$a_mois;\nannee_down=$a_annee;\n</SCRIPT>\n";
  echo "<INPUT type='hidden' name='jour' value='$a_jour'><INPUT type='hidden' name='mois' value='$a_mois'><INPUT type='hidden' name='annee' value='$a_annee'><INPUT type='hidden' name='Categorie' value='$Categorie'>";
  echo "<INPUT type='submit' class='button' value='AV' /></form></TD>";
  echo "<TD><form><INPUT type='hidden' name='Type' value='5'><INPUT type='hidden' name='Sous_Type' value='1'>";
  $date=mktime(0,0,0,$mois,$jour+7,$annee);
  $a_jour = date("d",$date);
  $a_mois = date("m",$date);
  $a_annee = date("Y",$date);
  echo "<SCRIPT language=JavaScript>\njour_up=$a_jour;\nmois_up=$a_mois;\nannee_up=$a_annee;\n</SCRIPT>\n";
  echo "<INPUT type='hidden' name='jour' value='$a_jour'><INPUT type='hidden' name='mois' value='$a_mois'><INPUT type='hidden' name='annee' value='$a_annee'><INPUT type='hidden' name='Categorie' value='$Categorie'>";
  echo "<INPUT type='submit' class='button' value='AP' /></form></TD></TR></TABLE><BR>\n";
  echo "<form><INPUT type='hidden' name='Type' value='5'><INPUT type='hidden' name='Sous_Type' value='1'>";
  echo "<TABLE class='table_date'><TR><TD>";
  //echo "<TH>Jour</TH><INPUT type='text' name='jour' value='$jour' size=2></TD>";
  //echo "<TH>Mois</TH><TD><INPUT type='text' name='mois' value='$mois' size=2></TD>";
  //echo "<TH>Annee</TH><TD><INPUT type='text' name='annee' value='$annee' size=4>";
  $date=mktime(0,0,0,$mois,$jour,$annee);
  $date=date("d.m.Y",$date);
  echo "<input type='text' class='wbdatepicker' name='datestr' value='$date' size='10'>";
  echo "</TD><TD><INPUT type='submit' class='button' value='GO' /></TD></TR>";
  echo "</TABLE>";
  echo "</form>";
  echo "<form><INPUT type='hidden' name='Type' value='5'><INPUT type='hidden' name='Sous_Type' value='1'><INPUT type='hidden' name='jour' value='$jour'><INPUT type='hidden' name='mois' value='$mois'><INPUT type='hidden' name='annee' value='$annee'>";
  echo "<SELECT name='Categorie'>";
  $resultat = CatLoc::getCatloc();
  foreach($resultat as $ligne)
  {
    echo "<OPTION value='".$ligne['NoCatLoc']."'";
    if($Categorie==$ligne['NoCatLoc'])
      echo " selected";
    echo ">".$ligne['Description']."</OPTION>";
  }
  echo "</SELECT>";
  echo "<INPUT type='submit' class='button' value='GO' /></form>";
?>
</div>