<?php
require 'database.inc.php';
?>
<html>

<head>
  <meta http-equiv='content-type' content='text/html;charset=utf-8'>
  <SCRIPT language=JavaScript src='valideformulaire.js' type=text/javascript></SCRIPT>
  <title>WebBike V<?php echo VERSION; ?></title>
  <?php
  $size = 2;
  if ($ligne = Poste::getPostesByIP()) {
    $size = $ligne['Size'];
  }
  echo "<link href='css/style$size.css' rel='stylesheet' media='screen'>\n";

  echo "<link href='" . CSS_FOLDER . "style.css' rel='stylesheet' media='screen'>\n"
  ?>
</head>

<body>
  <script language=javascript>
    function go_facture(NoCommande, NoClient, SousType) {
      this.opener.location = "default.php?Type=4&Sous_Type=" + SousType + "&Etape=3&New=0&NoCommande=" + NoCommande + "&NoClient=" + NoClient;
      window.close();
    }

    function valide_suppresion() {
      if (confirm("Voulez-vous vraiment supprimer cette fiche atelier ?")) {
        this.document.infodel.submit();
      }
    }

    function imprimer_atelier() {
      if (this.document.info.Temps.value == 0) {
        alert("Vous devez rentrer un temps de travail");
        return;
      }
      if (this.document.info.Reception.value.length == 0) {
        alert("Vous devez rentrer un réceptionaire");
        return;
      }
      if (this.document.info.etape.value == 0) {
        this.document.info.etape.value = 1;
        this.document.info.pri.value = 1;
        this.document.info.submit();
      } else {
        this.document.info.pri.value = 1;
        this.document.info.submit();
      }
    }

    function valider_atelier() {
      if (this.document.info.Telephone.value.length == 0) {
        if (!confirm("Manque no de téléphone")) {
          return;
        }
      }
      if (this.document.info.Temps.value == 0) {
        alert("Vous devez rentrer un temps de travail");
        return;
      }
      this.document.info.submit();
    }

    function reception_atelier() {
      if (this.document.info.etape.value == 0) {
        this.document.info.etape.value = 1;
      }
      this.document.info.submit();
    }

    function facture(fact) {
      if (this.document.info.NoClient.value.length == 0) {
        alert('vous devez absolument avoir le NoClient pour créer un facture');
      } else {
        if (fact != 0) {
          this.document.info.fact.value = fact;
          this.document.info.etape.value = 2;
          if (fact == 6)
            this.document.info.NoCommande.value = this.document.info.NoCommande2.value;
          this.document.info.submit();
        } else {
          this.document.info.etape.value = 3;
          this.document.info.submit();
        }
      }
    }

    function recherche_client() {
      Nom = document.info.elements['Nom'].value;
      Prenom = document.info.elements['Prenom'].value;
      if (Nom.length == 0 && Prenom.length == 0) {
        alert("Vous devez rentrer un nom ou un prénom");
      } else {
        window.open('recherche_client_old.php?Nom=' + Nom + '&Prenom=' + Prenom + '&Mode=2', '', 'top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no');
      }
    }

    function recherche_vhc() {
      NoClient = document.info.elements['NoClient'].value;
      if (NoClient.length != 0) {
        window.open('recherche_vhc_old.php?NoClient=' + NoClient + '&Mode=0', '', 'top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no');
      } else {
        alert('Vous devez en premier trouver le no de client');
      }
    }

    function efface_vhc() {
      this.document.info.NoChasis.value = '';
      this.document.info.Vehicule.value = '';
      this.document.info.Km.value = '';
      this.document.info.vhc.value = '0';
      this.document.info.service.options.length = 1;
      this.document.info.service.selectedIndex = 0;
    }

    function duree_service() {
      current_value = document.forms['info'].service.options[document.forms['info'].service.selectedIndex].value;
      document.forms['info'].Temps.value = document.tab_duree[current_value];
    }

    function add_service_option(value, text, duree) {
      var o = new Option(text, value);
      document.forms['info'].service.options[document.forms['info'].service.options.length] = o;
      document.tab_duree[value] = duree;
    }
    document.tab_duree = new Array;
  </script>
  <?php
  if ((!empty($User)) && (!empty($Passwd))) {
    $lg_user = $User;
    $lg_password = $Passwd;
  }
  if (Test_User($lg_user, $lg_password) == TRUE) {
    //  $annee=date("Y");
    if ($new == 0) {
      if ($ligne = Atelier::getAtelierByNoAtelier($NoAtelier)) {
        echo "<form METHOD='GET' name='info' action='ok_atelier.php'>";
        echo "<TABLE border=1>\n";
        echo "<input type='hidden' name='pri' value='0'><BR>\n";
        echo "<TR><TH>No Client</TH><TD colspan='3'><Input type='text' name='NoClient' value='" . $ligne['NoClient'] . "' readonly><BUTTON type='button' onClick='recherche_client_ajax(2);'>Recherche</BUTTON></TD></TR>\n";
        echo "<TR><TH>Nom</TH><TD><Input type='text' name='Nom' value='" . htmlentities($ligne['Nom'], ENT_QUOTES, 'UTF-8') . "'></TD>\n";
        echo "<script language=JavaScript>\n";
        echo "this.document.forms['info'].elements['Nom'].focus();\n";
        echo "</script>\n";
        echo "<TH>Prenom</TH><TD><Input type='text' name='Prenom' value='" . htmlentities($ligne['Prenom'], ENT_QUOTES, 'UTF-8') . "' onchange='recherche_client_ajax(2);'></TD></TR>\n";
        echo "<TR><TH>Telephone</TH><TD colspan='3'><Input type='text' name='Telephone' value='" . $ligne['Telephone'] . "'></TD></TR>\n";
        echo "<TR><TH>No Chasis</TH><TD colspan='3'><Input type='text' name='NoChasis' value='" . $ligne['NoChasis'] . "' readonly><BUTTON type='button' onClick='recherche_vhc_ajax(0,1);'>Recherche</BUTTON><BUTTON type='button' onClick='efface_vhc();'>Effacer</BUTTON></TD></TR>\n";
        echo "<TR><TH>Vehicule</TH><TD><Input type='text' name='Vehicule' value='" . $ligne['Vehicule'] . "'></TD>\n";
        echo "<TH>Km</TH><TD><Input type='text' name='Km' value='" . $ligne['Km'] . "'></TD></TR>\n";
        echo "<TR><TH>Service</TH><TD colspan='3'><select name='service' onchange='duree_service(this)'><option value='0'>-</option>";
        if ($ligne['Id_Vhc'] != 0) {
          $resultat2 = ServiceVHC::getServiceVHCByVhc($ligne['Id_Vhc']);
          foreach ($resultat2 as $ligne2) {
            echo "<option value='" . $ligne2['Id'] . "'";
            if ($ligne2['Id'] == $ligne['Id_Service']) {
              echo " selected='1'";
            }
            echo ">Service " . $ligne2['Km'] . "</option>";
            echo "<script language=javascript>document.tab_duree[" . $ligne2["Id"] . "] = " . $ligne2["Duree"] . ";</script>";
          }
        }
        echo "</select></TD></TR>\n";
        echo "<TR><TH>Date</TH><TD colspan='3'>" . Input_Date("Date", substr($ligne['Date'], 0, 4), substr($ligne['Date'], 5, 2), substr($ligne['Date'], 8, 2)) . "</TD></TR>\n";
        echo "<TR><TH>Heure</TH><TD colspan='3'>" . Input_Heure_15("HD", substr($ligne['Heure'], 0, 2), substr($ligne['Heure'], 3, 2)) . "</TD></TR>\n";
        echo "<TR><TH>Durée prévu</TH><TD><Input type='text' name='Temps' value='" . $ligne['Temps'] . "'></TD>\n";
        echo "<TH>Mecano</TH><TD colspan='3'><select name='Mecano'><option value='0'>-</option>";
        $resultat2 = Employee::getMecano();
        foreach ($resultat2 as $ligne2) {
          echo "<option value='" . $ligne2["NoClient"] . "'";
          if ($ligne2["NoClient"] == $ligne["Mecano"]) {
            echo " selected";
          }
          echo ">" . $ligne2["Prenom"] . "</option>";
        }
        echo "</select></TD></TR>\n";
        echo "<TR><TH>Description</TH><TD colspan='3'><TextArea name='Description' Rows=4 Cols=51>" . htmlentities($ligne['Description'], ENT_COMPAT | ENT_HTML401, 'UTF-8') . "</TextArea></TD></TR>\n";
        echo "<TR><TH>Retour client</TH><TD colspan='3'>" . Input_Date_Heure("HF", substr($ligne['Restitution'], 0, 4), substr($ligne['Restitution'], 5, 2), substr($ligne['Restitution'], 8, 2), substr($ligne['Restitution'], 11, 2), substr($ligne['Restitution'], 14, 2)) . "</TD></TR>\n";
        echo "<TR><TH>Réceptionaire</TH><TD colspan='3'><Input type='text' name='Reception' value='" . $ligne['Reception'] . "'></TD></TR>\n";
        if ($ligne['Etape'] < 2) {
          if ($ligne['NoCommande'] != 0) {
            $ligne_acompte = DBCash::getByNoFacture($ligne['NoCommande']);
            echo "<TR><TH>Acompte</TH><TD colspan='3'>" . $ligne_acompte["Somme"];
            echo " <BUTTON TYPE='button' onClick='go_facture(" . $ligne['NoCommande'] . ", " . $ligne['NoClient'] . ", 1);'>Facture " . $ligne['NoCommande'] . "</button><BR>\n";
            echo "</TD></TR>";
          } else {
            echo "<TR><TH>Acompte</TH><TD colspan='3'><Input type='text' name='Acompte' value='0'>";
            if (As_Right_Groupe(6)) {
              $resultat2 = ModePayement::getVisibleModePayment();
            } else {
              $no_caisse = 1;
              if ($ligne1 = Poste::getPostesByIP()) {
                $no_caisse = $ligne1['NoCaisse'];
              }
              $resultat2 = ModePayement::getModePayementsByNoCaisse($no_caisse);
            }
            echo " <SELECT name='ModePayement' size='1'>\n";
            foreach ($resultat2 as $ligne2) {
              if (($ligne2['Type'] == 2) && (!Is_Banque())) {
              } else {
                if (As_Right_Groupe(6)) {
                  echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . " caisse " . $ligne2['NoCaisse'] . "</option>\n";
                } else {
                  echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . "</option>\n";
                }
              }
            }
            echo " </SELECT>\n";
            echo "</TD></TR>\n";
          }
        }
        if ($ligne['Etape'] == 2 && $lg_admin != 0) {
          echo "<TR><TH>NoFacture</TH><TD colspan='3'><Input type='text' name='NoCommande' value='" . $ligne['NoCommande'] . "'></TD></TR>\n";
        } else {
          echo "<input type='hidden' name='NoCommande' value='" . $ligne['NoCommande'] . "'>\n";
        }
        echo "</TABLE>\n";
        echo "<input type='hidden' name='NoAtelier' value='" . $ligne['NoAtelier'] . "'>\n";
        echo "<input type='hidden' name='etape' value='" . $ligne['Etape'] . "'>\n";
        echo "<input type='hidden' name='fact' value='0'>\n";
        echo "<input type='hidden' name='vhc' value='" . $ligne['Id_Vhc'] . "'>\n";
        echo "<button type='submit'>Valider</button><BR><BR>\n";
        if ($ligne['Etape'] == 0) {
          echo "<button type='button' onClick='reception_atelier();'>Récéption véhicule</button><BR>\n";
        }
        echo "<button type='button' onClick='imprimer_atelier();'>Fiche atelier</button><BR><BR>\n";
        if ($ligne['Etape'] == 1) {
          if ($ligne['NoCommande'] != 0) {
            echo "<button type='button' onClick='facture(1);'>Travaux terminés</button>\n";
          } else {
            echo "<button type='button' onClick='facture(1);'>Facture</button>\n";
            echo "<button type='button' onClick='facture(2);'>Garantie fourniseur</button>\n";
            echo "<button type='button' onClick='facture(3);'>Garantie garage</button>\n";
            echo "<button type='button' onClick='facture(4);'>Devis</button>\n";
            echo "<button type='button' onClick='facture(5);'>Facture interne</button>\n";
            echo "<button type='button' onClick='facture(0);'>Sans facture</button><BR>\n";
            if (Is_Admin())
              echo "<button type='button' onClick='facture(6);'>Facture existante</button><input type='text' name='NoCommande2'><BR>";
            echo "<BR>\n";
          }
        }
        echo "</form>";
        if ($ligne['Etape'] < 2) {
          echo "<form METHOD='GET' name='infodel' action='del_atelier.php'>";
          echo "<input type='hidden' name='NoAtelier' value='$NoAtelier'>\n";
          echo "<BUTTON TYPE='button' onClick='valide_suppresion();'>Supprimer</button><BR>\n";
          echo "</form>";
        }
        if ($ligne['NoCommande'] != 0 && $ligne['Etape'] != 1) {
          echo "<form>";
          $ligne2 = Commande::getCommandeByNoCommande($ligne['NoCommande']);
          $Sous_Type = 1;
          echo "<BUTTON TYPE='button' onClick='go_facture(" . $ligne['NoCommande'] . ", " . $ligne['NoClient'] . ", $Sous_Type)'>Facture</button><BR>\n";
          echo "</form>";
        }
        if ($ligne['Etape'] >= 1) {
          echo "<button id='SMS_button' type='button'";
          if ($ligne["Sms"] == 1) {
            echo " style='background-color:#FF0000;'";
          }
          echo " onClick='send_sms(" . $ligne['NoAtelier'] . ");'>Envoi SMS</button><BR>\n";
        }
      }
    } else {
      echo "<form METHOD='GET' name='info' action='ok_atelier.php'>";
      echo "<input type='hidden' name='pri' value='0'><BR>\n";
      echo "<TABLE border=1><input type='hidden' name='etape' value='0'><input type='hidden' name='new' value='1'>\n";
      echo "<TR><TH>No Client</TH><TD colspan='3'><Input type='text' name='NoClient' readonly><BUTTON type='button' onClick='recherche_client_ajax(2);'>Recherche</BUTTON></TD></TR>\n";
      echo "<TR><TH>Nom</TH><TD><Input type='text' name='Nom'></TD>\n";
      echo "<script language=JavaScript>\n";
      echo "this.document.forms['info'].elements['Nom'].focus();\n";
      echo "</script>\n";
      echo "<TH>Prenom</TH><TD><Input type='text' name='Prenom' onchange='recherche_client_ajax(2);'></TD></TR>\n";
      echo "<TR><TH>Telephone</TH><TD colspan='3'><Input type='text' name='Telephone'></TD></TR>\n";
      echo "<TR><TH>No Chasis</TH><TD colspan='3'><Input type='text' name='NoChasis' readonly><BUTTON type='button' onClick='recherche_vhc_ajax(0,1);'>Recherche</BUTTON><BUTTON type='button' onClick='efface_vhc();'>Effacer</BUTTON></TD></TR>\n";
      echo "<TR><TH>Vehicule</TH><TD><Input type='text' name='Vehicule'></TD>\n";
      echo "<TH>Km</TH><TD><Input type='text' name='Km' value='0'></TD></TR>\n";
      echo "<TR><TH>Service</TH><TD colspan='3'><select name='service' onchange='duree_service(this)'><option value='0'>-</option></select></TD></TR>\n";
      echo "<TR><TH>Date</TH><TD colspan='3'>" . Input_Date("Date", $annee, $mois, $jour) . "</TD></TR>\n";
      echo "<TR><TH>Heure</TH><TD colspan='3'>" . Input_Heure_15("HD", 8, 0) . "</TD></TR>\n";
      echo "<TR><TH>Mecano</TH><TD colspan='3'><select name='Mecano'><option value='0'>-</option>";
      $resultat = Employee::getMecano();
      foreach ($resultat as $ligne) {
        echo "<option value='" . $ligne["NoClient"] . "'";
        if ($mecano == $ligne["NoClient"]) {
          echo " selected";
        }
        echo ">" . $ligne["Prenom"] . "</option>";
      }
      echo "</select></TD></TR>\n";
      echo "<TR><TH>Durée prévu</TH><TD colspan='3'><Input type='text' name='Temps' value=''></TD></TR>\n";
      echo "<TR><TH>Description</TH><TD colspan='3'><TextArea name='Description' Rows=4 Cols=51></TextArea></TD></TR>\n";
      echo "<TR><TH>Retour client</TH><TD colspan='3'>" . Input_Date_Heure("HF", $annee, $mois, $jour, 17, 0) . "</TD></TR>\n";
      echo "<TR><TH>Réceptionaire</TH><TD colspan='3'><Input type='text' name='Reception' value=''></TD></TR>\n";
      echo "<TR><TH>Vehicule de prêt</TH><TD colspan='3'><select name='vhc_pret'><option value=''>-</option>";
      $resultat = CatLoc::getCatlocByPret();
      foreach ($resultat as $ligne) {
        $resultat2 = VehiculesLocation::getVehiculeLocationByCategorie($ligne['NoCatLoc']);
        foreach ($resultat2 as $ligne2) {
          $requette3 = "SELECT COUNT(*) FROM location" . ID_GARAGE . " WHERE NoChasis='" . addslashes($ligne2['NoChasis']) . "' AND((Debut>='$annee-$mois-$jour 00:00:00' AND Debut<='$annee-$mois-$jour 23:59:59') OR (Debut>='$annee-$mois-$jour 00:00:00' AND Debut<='$annee-$mois-$jour 23:59:59') OR (Debut<'$annee-$mois-$jour 00:00:00' AND Fin>'$annee-$mois-$jour 23:59:59'))";
          if ($resultat3 = Connection::executeRawQueryWithfetchAll($requette3))
            if ($ligne3 = $resultat3[0])
              $is_pret = ($ligne3[0] != 0);
          if (!$is_pret) {
            $requette3 = "SELECT Debut FROM location" . ID_GARAGE . " WHERE NoChasis='" . addslashes($ligne2['NoChasis']) . "' AND Debut>'$annee-$mois-$jour 23:59:59' ORDER BY Debut LIMIT 0, 1";
            if ($resultat3 = Connection::executeRawQueryWithfetchAll($requette3))
              if ($ligne3 = $resultat3[0]) {
                $prem = mktime(0, 0, 0, substr($ligne3[0], 5, 2), substr($ligne3[0], 8, 2), substr($ligne3[0], 0, 4));
                $actu = mktime(0, 0, 0, $mois, $jour, $annee);
                $j = (($prem - $actu) / (60 * 60 * 24)) . " jours";
              } else {
                $j = "";
              }
            $ligne3 = Vehicule::getVehiculeByNoChassis(addslashes($ligne2['NoChasis']));
            echo "<option value='" . $ligne2['NoChasis'] . "'>" . $ligne3['Model'] . " " . $ligne3['NoPlaque'] . " " . $j . "</option>";
          }
        }
      }
      echo "</select></TD>\n";
      echo "<TR><TH>Acompte</TH><TD colspan='3'><Input type='text' name='Acompte' value='0'>";
      if (As_Right_Groupe(6)) {
        $resultat2 = ModePayement::getVisibleModePayment();
      } else {
        $no_caisse = 1;
        if ($ligne1 = Poste::getPostesByIP()) {
          $no_caisse = $ligne1['NoCaisse'];
        }
        $resultat2 = ModePayement::getModePayementsByNoCaisse($no_caisse);
      }
      echo " <SELECT name='ModePayement' size='1'>\n";
      foreach ($resultat2 as $ligne2) {
        if (($ligne2['Type'] == 2) && (!Is_Banque())) {
        } else {
          if (As_Right_Groupe(6)) {
            echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . " caisse " . $ligne2['NoCaisse'] . "</option>\n";
          } else {
            echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . "</option>\n";
          }
        }
      }
      echo " </SELECT>\n";
      echo "</TD></TR>\n";
      echo "</TABLE>\n";
      echo "<input type='hidden' name='vhc' value='0'>\n";
      echo "<button type='button' onClick='valider_atelier();'>Valider</button><BR>\n";
      echo "<button type='button' onClick='imprimer_atelier();'>Fiche atelier</button><BR>\n";
      echo "</form>";
    }
  }
  ?>
</body>

</html>