<?php
  switch($Etape)
  {
    case 0:
      echo "<form name='info'>";
      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 "Acompte <input type='text' id='Acompte' name='Acompte'>";
      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";
      if ($resultat2)
      {
        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 "<br>";
      echo "<script language=JavaScript>\n";
      echo "this.document.forms['info'].elements['Acompte'].focus();\n";
      echo "</script>\n";
      echo "<button type='button' onClick='submit_ok(this.form);'>Valider</button>";
      echo "</form>";
    break;
    case 1:
      if ($Acompte > 0)
      {
        $NoCommande=Trouve_No_Commande_Libre("FD");
        $date = date("Y-m-d");
        $nc = "0";
        $requette="INSERT INTO commande".ID_GARAGE." (NoCommande,Type,Genre,PersonneModif,NoClient,DateCommande,DateModification,MontantTotal,APayer,DateDelaisPayement) VALUES " .
                  "($NoCommande, 'FD', 'F', '$lg_user', $nc, '$date', '$date', 0, -$Acompte, '".date("Y-m-d",mktime(0,0,0,date("m"),date("d")+30,date("Y")))."')";
        if ($resultat = Connection::executeRawQuery($requette))
        {
          $requette="INSERT INTO commandearticles".ID_GARAGE." (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
                    "$NoCommande, 1, 1, '', 'Acompte pièce', 0, 0, 0, 0, 0, 0)";
          Connection::executeRawQuery($requette);
          $no_caisse=1;
          $mode_payement=1;
          $TabModePayement=explode(" ", $ModePayement);
          if(count($TabModePayement)==2)
          {
            $mode_payement = $TabModePayement[0];
            $no_caisse = $TabModePayement[1];
          }
          $requette="INSERT INTO dbcash".ID_GARAGE." (NoFacture, PersonneEncaissement, Date, Somme, TVA, ModePayement, NoCaisse) VALUES".
                  " ($NoCommande, '$lg_user', '$date ".date("H:i").":00', $Acompte, -".get_config('TVA').", $mode_payement, $no_caisse)";
          //echo $requette . "<BR />\n";
          Connection::executeRawQuery($requette);
          $no_cash = Connection::getConnection()->lastInsertId();
          print_quitance($no_cash);
          print_quitance($no_cash);
          header("Location: default.php?Type=4&Sous_Type=1&Etape=3&NoCommande=$NoCommande");
        }
      }
    break;
  }
?>
