Advertisement
julienpierre

SNCF Code acheteur final

Apr 27th, 2025 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 9.70 KB | Source Code | 0 0
  1. <div style="text-align: center; font-family: 'Courier New', monospace; margin: 20px;">
  2.     <h1 style="font-size: 20px; color: black; margin-bottom: 5px;">CARNET DE VOYAGE — BILLET SNCF</h1>
  3.     <div id="trip-type-banner" style="font-size: 16px; margin-bottom: 20px; color: #FF7900; font-weight: bold;"></div>
  4.     <div id="ticket-container" style="display: flex; justify-content: center; flex-wrap: wrap; gap: 20px; margin-bottom: 20px;"></div>
  5.     <button id="new-ticket-btn" style="background-color: #666; color: white; font-size: 16px; padding: 10px 25px; margin-right: 10px; border: none; cursor: pointer;">NOUVEAU BILLET</button>
  6.     <button id="validate-code-btn" style="background-color: #FF7900; color: white; font-size: 16px; padding: 10px 25px; border: none; cursor: pointer;">VALIDER</button>
  7. </div>
  8.  
  9. <script type="text/javascript">
  10.     // Inject after load to avoid inline JS blocking
  11.     setTimeout(function() {
  12.         const destinations = ["RENNES", "LILLE", "STRASBOURG", "MARSEILLE", "BIARRITZ", "TOULOUSE", "NICE", "BORDEAUX", "LYON", "NANTES"];
  13.         const classes = ["1", "2"];
  14.         const days = ["LUNDI", "MARDI", "MERCREDI", "JEUDI", "VENDREDI", "SAMEDI", "DIMANCHE"];
  15.         const timesOfDay = ["MATIN", "APRÈS-MIDI"];
  16.  
  17.         // Normalize any input into "16h00" format
  18.         function normalizeTime(input) {
  19.             let s = input.trim().toLowerCase().replace(":", "h");
  20.             const parts = s.split("h");
  21.             let h = parseInt(parts[0], 10).toString();
  22.             let m = parts[1] || "00";
  23.             if (m === "") m = "00";
  24.             m = m.padStart(2, "0");
  25.             return `${h}h${m}`;
  26.         }
  27.  
  28.         // Generate the same hash as seller side, including days
  29.         function genererCode(trajet, dayD, dayR, horaires, classe, prix, personnes) {
  30.             const parts = [
  31.                 trajet,
  32.                 dayD,
  33.                 ...(trajet === "ALLER-RETOUR" ? [dayR] : []),
  34.                 ...horaires.sort(),
  35.                 classe,
  36.                 prix,
  37.                 personnes
  38.             ];
  39.             const base = parts.join("|");
  40.             let hash = 0;
  41.             for (let i = 0; i < base.length; i++) {
  42.                hash = (hash * 31 + base.charCodeAt(i)) % 100000000;
  43.            }
  44.            return hash.toString(36).toUpperCase();
  45.        }
  46.  
  47.        function getNextDateFromDay(targetDay) {
  48.            const today = new Date();
  49.            const idxT = days.indexOf(targetDay);
  50.            const idxC = today.getDay() === 0 ? 6 : today.getDay() - 1;
  51.            let diff = (idxT - idxC + 7) % 7 || 7;
  52.            const d = new Date();
  53.            d.setDate(today.getDate() + diff);
  54.            return d;
  55.        }
  56.  
  57.        function formatDate(d) {
  58.            return `${d.getDate().toString().padStart(2,"0")}/${(d.getMonth()+1).toString().padStart(2,"0")}/${d.getFullYear()}`;
  59.        }
  60.  
  61.        let ticketData = {};
  62.  
  63.        function clearContainer(container) {
  64.            while (container.firstChild) container.removeChild(container.firstChild);
  65.        }
  66.  
  67.        function genererBillet() {
  68.            const type = Math.random() < 0.5 ? "ALLER SIMPLE" : "ALLER-RETOUR";
  69.            const arrival = destinations[Math.floor(Math.random() * destinations.length)];
  70.            const classe = classes[Math.floor(Math.random() * classes.length)];
  71.            const personnes = Math.floor(Math.random() * 4) + 1;
  72.            const dayD = days[Math.floor(Math.random() * days.length)];
  73.            const moment = timesOfDay[Math.floor(Math.random() * timesOfDay.length)];
  74.            const dateD = getNextDateFromDay(dayD);
  75.            const dayR = type === "ALLER-RETOUR" ? days[(days.indexOf(dayD) + 1) % 7] : "";
  76.            const dateR = new Date(dateD);
  77.            if (type === "ALLER-RETOUR") dateR.setDate(dateR.getDate() + 1);
  78.  
  79.            ticketData = {
  80.                type,
  81.                arrival,
  82.                classe,
  83.                personnes,
  84.                dayDepart: dayD,
  85.                dayRetour: dayR,
  86.                moment,
  87.                dateDepart: dateD,
  88.                billets: []
  89.            };
  90.  
  91.            document.getElementById("trip-type-banner").textContent =
  92.                `${type} — ${dayD} ${moment} — ${personnes} personne${personnes>1?"s":""}`;
  93.  
  94.             const container = document.getElementById("ticket-container");
  95.             clearContainer(container);
  96.  
  97.             container.appendChild(creerBillet("GARE MONTPARNASSE - PARIS", arrival, dayD, moment, dateD, true));
  98.             if (type === "ALLER-RETOUR") {
  99.                 container.appendChild(creerBillet(arrival, "GARE MONTPARNASSE - PARIS", dayR, moment, dateR, false));
  100.             }
  101.         }
  102.  
  103.         function creerBillet(dep, arr, day, moment, dateObj, withPrix) {
  104.             const div = document.createElement("div");
  105.             div.className = "billet";
  106.             div.style.cssText = `
  107.       width:520px; padding:20px;
  108.       background:repeating-linear-gradient(45deg,#fff,#fff 5px,#f9f4ee 5px,#f9f4ee 10px);
  109.       border:2px solid #333; font-family:'Courier New',monospace;
  110.       box-shadow:3px 3px 12px rgba(0,0,0,0.3);
  111.       text-transform:uppercase; margin-bottom:20px;
  112.     `;
  113.  
  114.             // En-tête
  115.             const h = document.createElement("div");
  116.             h.style.cssText = "text-align:center;font-weight:bold;border-bottom:1px solid #000;padding-bottom:5px;margin-bottom:10px;";
  117.             h.textContent = "BILLET À COMPOSTER AVANT ACCÈS AU TRAIN";
  118.             div.appendChild(h);
  119.  
  120.             // Dates + passagers
  121.             const u = document.createElement("div");
  122.             u.style.cssText = "display:flex;justify-content:space-between;margin-bottom:10px;";
  123.             const from = new Date(dateObj);
  124.             from.setDate(from.getDate() - 2);
  125.             [
  126.                 ["UTILISABLE DU :", formatDate(from)],
  127.                 ["AU :", formatDate(dateObj)],
  128.                 [`${ticketData.personnes} ADULTE${ticketData.personnes>1?"S":""}`, ""]
  129.             ]
  130.             .forEach(([lab, val]) => {
  131.                 const cell = document.createElement("div");
  132.                 const s = document.createElement("strong");
  133.                 s.textContent = lab;
  134.                 cell.appendChild(s);
  135.                 if (val) cell.appendChild(document.createTextNode(" " + val));
  136.                 u.appendChild(cell);
  137.             });
  138.             div.appendChild(u);
  139.  
  140.             // Départ / Arrivée / Classe
  141.             const info = document.createElement("div");
  142.             info.style.cssText = "display:flex;justify-content:space-between;border:1px solid #333;padding:10px;margin-bottom:10px;";
  143.             [
  144.                 ["DÉPART", dep],
  145.                 ["ARRIVÉE", arr],
  146.                 ["CLASSE", ticketData.classe]
  147.             ]
  148.             .forEach(([lab, val]) => {
  149.                 const blk = document.createElement("div");
  150.                 const s = document.createElement("strong");
  151.                 s.textContent = lab;
  152.                 blk.appendChild(s);
  153.                 blk.appendChild(document.createElement("br"));
  154.                 blk.appendChild(document.createTextNode(val));
  155.                 info.appendChild(blk);
  156.             });
  157.             div.appendChild(info);
  158.  
  159.             // Jour / Moment
  160.             const jm = document.createElement("div");
  161.             jm.style.marginBottom = "6px";
  162.             jm.innerHTML = `<strong>JOUR :</strong> ${day} - ${moment}`;
  163.             div.appendChild(jm);
  164.  
  165.             // Horaires
  166.             const hdiv = document.createElement("div");
  167.             hdiv.style.marginBottom = "6px";
  168.             hdiv.innerHTML = `<strong>HORAIRES :</strong> <input class="horaire" type="text" placeholder="8h30, 10h15" style="width:250px;font-family:'Courier New',monospace;">`;
  169.             div.appendChild(hdiv);
  170.  
  171.             // Prix
  172.             if (withPrix) {
  173.                 const pdiv = document.createElement("div");
  174.                 pdiv.style.marginBottom = "6px";
  175.                 pdiv.innerHTML = `<strong>PRIX :</strong> <input class="prix" type="number" min="0" style="width:100px;font-family:'Courier New',monospace;"> €`;
  176.                 div.appendChild(pdiv);
  177.             }
  178.  
  179.             // Code
  180.             const c = document.createElement("div");
  181.             c.className = "code-verif";
  182.             c.style.cssText = "text-align:right;margin-top:10px;font-weight:bold;color:orange;";
  183.             c.textContent = "--------";
  184.             div.appendChild(c);
  185.  
  186.             ticketData.billets.push(div);
  187.             return div;
  188.         }
  189.  
  190.         function validerCode() {
  191.             const {
  192.                 type,
  193.                 dayDepart,
  194.                 dayRetour,
  195.                 billets,
  196.                 classe,
  197.                 personnes
  198.             } = ticketData;
  199.             const horaires = [];
  200.             billets.forEach(b => {
  201.                 b.querySelector(".horaire").value
  202.                     .split(",")
  203.                     .map(normalizeTime)
  204.                     .filter(h => h)
  205.                     .forEach(h => horaires.push(h));
  206.             });
  207.             const prix = billets[0].querySelector(".prix").value.trim();
  208.             if (!prix || !horaires.length) {
  209.                 return alert("Merci de remplir les horaires et le prix du billet aller.");
  210.             }
  211.  
  212.             const code = genererCode(type, dayDepart, dayRetour, horaires, classe, prix, personnes);
  213.             billets.forEach(b => b.querySelector(".code-verif").textContent = code);
  214.         }
  215.  
  216.         document.getElementById("new-ticket-btn").addEventListener("click", genererBillet);
  217.         document.getElementById("validate-code-btn").addEventListener("click", validerCode);
  218.  
  219.         genererBillet();
  220.     }, 100);
  221. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement