ツール管理へ戻る

v1 のソース

<!doctype html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>注文情報の記録</title>
  <style>
    :root {
      font-family: system-ui, -apple-system, "Noto Sans JP", sans-serif;
      color: #1f2937;
      background: #f3f6fa;
    }

    * { box-sizing: border-box; }

    body {
      margin: 0;
      padding: 28px 16px;
    }

    main {
      max-width: 1000px;
      margin: 0 auto;
      padding: 28px;
      background: #fff;
      border: 1px solid #dce3ec;
      border-radius: 14px;
      box-shadow: 0 8px 24px rgba(31, 41, 55, 0.07);
    }

    h1 { margin: 0 0 8px; font-size: 1.6rem; }
    h2 { margin: 0 0 14px; font-size: 1.2rem; }

    .description {
      margin: 0 0 24px;
      color: #526174;
      line-height: 1.6;
    }

    .form-grid {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 17px 20px;
    }

    .field label {
      display: block;
      margin-bottom: 7px;
      font-weight: 700;
    }

    input, select {
      width: 100%;
      min-height: 42px;
      padding: 9px 11px;
      border: 1px solid #bcc8d6;
      border-radius: 7px;
      background: #fff;
      color: inherit;
      font: inherit;
    }

    input:focus, select:focus {
      outline: 3px solid rgba(37, 99, 235, 0.18);
      border-color: #2563eb;
    }

    .example {
      margin: 5px 0 0;
      color: #64748b;
      font-size: 0.88rem;
    }

    .required { color: #b42318; }

    .error, .notice {
      display: none;
      margin: 18px 0 0;
      padding: 11px 13px;
      border-radius: 7px;
    }

    .error {
      color: #9b1c1c;
      background: #fff1f1;
      border: 1px solid #f0b8b8;
    }

    .notice {
      color: #14532d;
      background: #f0fdf4;
      border: 1px solid #bbf7d0;
    }

    .actions {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      margin-top: 22px;
    }

    button {
      padding: 10px 15px;
      border: 0;
      border-radius: 7px;
      font: inherit;
      font-weight: 700;
      cursor: pointer;
    }

    button:hover { filter: brightness(0.96); }
    button:disabled { opacity: 0.5; cursor: not-allowed; filter: none; }

    .primary { color: #fff; background: #2563eb; }
    .secondary { color: #263548; background: #e8edf3; }

    .records-section {
      margin-top: 30px;
      padding-top: 24px;
      border-top: 1px solid #dce3ec;
    }

    .record-count { margin: 0 0 12px; color: #526174; }

    .table-wrap {
      overflow-x: auto;
      border: 1px solid #dce3ec;
      border-radius: 8px;
    }

    table {
      width: 100%;
      min-width: 1050px;
      border-collapse: collapse;
    }

    th, td {
      padding: 9px 10px;
      text-align: left;
      vertical-align: top;
      border-bottom: 1px solid #dce3ec;
      overflow-wrap: anywhere;
    }

    th { background: #f3f6fa; white-space: nowrap; }
    tbody tr:last-child td { border-bottom: 0; }

    .empty-row td {
      color: #64748b;
      text-align: center;
    }

    .privacy-note {
      margin: 18px 0 0;
      color: #64748b;
      font-size: 0.9rem;
      line-height: 1.5;
    }

    @media (max-width: 650px) {
      body { padding: 16px 10px; }
      main { padding: 20px 16px; }
      .form-grid { grid-template-columns: 1fr; }
    }
  </style>
</head>
<body>
  <main>
    <h1>注文情報の記録</h1>
    <p class="description">
      注文情報を1件ずつ追加し、最後に全記録をCSVでダウンロードできます。
      「*」の項目は必須です。
    </p>

    <form id="orderForm" novalidate>
      <div class="form-grid">
        <div class="field">
          <label for="category">分類 <span class="required">*</span></label>
          <select id="category" name="category" required>
            <option value="">選択してください</option>
            <option value="量産">量産</option>
            <option value="試作">試作</option>
            <option value="その他">その他</option>
          </select>
          <p class="example">選択例:量産</p>
        </div>

        <div class="field">
          <label for="newItem">新規品</label>
          <select id="newItem" name="newItem">
            <option value="">未回答</option>
            <option value="○">○</option>
          </select>
          <p class="example">新規品の場合は「○」を選択します。</p>
        </div>

        <div class="field">
          <label for="orderDate">受注日</label>
          <input id="orderDate" name="orderDate" type="date">
          <p class="example">カレンダーから選択できます。未入力でも構いません。</p>
        </div>

        <div class="field">
          <label for="deliveryDate">納入日(到着日)</label>
          <input id="deliveryDate" name="deliveryDate" type="date">
          <p class="example">カレンダーから選択できます。未入力でも構いません。</p>
        </div>

        <div class="field">
          <label for="orderNumber">注文番号 <span class="required">*</span></label>
          <input id="orderNumber" name="orderNumber" type="text"
                 placeholder="例:ORD-001" required>
        </div>

        <div class="field">
          <label for="partNumber">品番 <span class="required">*</span></label>
          <input id="partNumber" name="partNumber" type="text"
                 placeholder="例:ABC-123" required>
        </div>

        <div class="field">
          <label for="itemName">品名 <span class="required">*</span></label>
          <input id="itemName" name="itemName" type="text"
                 placeholder="例:取付ブラケット" required>
        </div>

        <div class="field">
          <label for="quantity">数量 <span class="required">*</span></label>
          <input id="quantity" name="quantity" type="number"
                 min="0" step="1" inputmode="numeric"
                 placeholder="例:100" required>
          <p class="example">0以上の整数を入力してください。</p>
        </div>

        <div class="field">
          <label for="unitPrice">単価 <span class="required">*</span></label>
          <input id="unitPrice" name="unitPrice" type="number"
                 min="0" step="any" inputmode="decimal"
                 placeholder="例:125.5" required>
          <p class="example">0以上の数値を入力してください。小数も入力できます。</p>
        </div>
      </div>

      <p class="error" id="errorMessage" role="alert"></p>
      <p class="notice" id="noticeMessage" role="status"></p>

      <div class="actions">
        <button class="primary" type="submit">記録を追加</button>
        <button class="secondary" type="reset">入力欄を初期化</button>
      </div>
    </form>

    <section class="records-section" aria-labelledby="recordsHeading">
      <h2 id="recordsHeading">記録一覧</h2>
      <p class="record-count" id="recordCount" aria-live="polite">記録件数:0件</p>

      <div class="table-wrap">
        <table>
          <thead>
            <tr>
              <th>分類</th>
              <th>新規品</th>
              <th>受注日</th>
              <th>納入日(到着日)</th>
              <th>注文番号</th>
              <th>品番</th>
              <th>品名</th>
              <th>数量</th>
              <th>単価</th>
            </tr>
          </thead>
          <tbody id="recordsBody">
            <tr class="empty-row">
              <td colspan="9">まだ記録はありません。</td>
            </tr>
          </tbody>
        </table>
      </div>

      <div class="actions">
        <button class="primary" id="downloadButton" type="button" disabled>
          全記録をCSVでダウンロード
        </button>
        <button class="secondary" id="clearRecordsButton" type="button" disabled>
          記録一覧を初期化
        </button>
      </div>
    </section>

    <p class="privacy-note">
      記録はページを開いている間だけブラウザ内に保持されます。
      ページを閉じると消去され、外部への送信や自動保存は行いません。
    </p>
  </main>

  <script>
    "use strict";

    const form = document.getElementById("orderForm");
    const errorMessage = document.getElementById("errorMessage");
    const noticeMessage = document.getElementById("noticeMessage");
    const recordsBody = document.getElementById("recordsBody");
    const recordCount = document.getElementById("recordCount");
    const downloadButton = document.getElementById("downloadButton");
    const clearRecordsButton = document.getElementById("clearRecordsButton");

    const records = [];

    function getFormData() {
      return {
        category: document.getElementById("category").value,
        newItem: document.getElementById("newItem").value,
        orderDate: document.getElementById("orderDate").value,
        deliveryDate: document.getElementById("deliveryDate").value,
        orderNumber: document.getElementById("orderNumber").value.trim(),
        partNumber: document.getElementById("partNumber").value.trim(),
        itemName: document.getElementById("itemName").value.trim(),
        quantity: document.getElementById("quantity").value.trim(),
        unitPrice: document.getElementById("unitPrice").value.trim()
      };
    }

    function validateData(data) {
      if (!["量産", "試作", "その他"].includes(data.category)) {
        return "分類を選択してください。";
      }

      if (data.newItem !== "" && data.newItem !== "○") {
        return "新規品は「○」を選択するか、未回答のままにしてください。";
      }

      if (!data.orderNumber) return "注文番号を入力してください。";
      if (!data.partNumber) return "品番を入力してください。";
      if (!data.itemName) return "品名を入力してください。";

      if (data.quantity === "") {
        return "数量を入力してください。";
      }

      const quantity = Number(data.quantity);
      if (!Number.isFinite(quantity) || !Number.isInteger(quantity) || quantity < 0) {
        return "数量は0以上の整数で入力してください。";
      }

      if (data.unitPrice === "") {
        return "単価を入力してください。";
      }

      const unitPrice = Number(data.unitPrice);
      if (!Number.isFinite(unitPrice) || unitPrice < 0) {
        return "単価は0以上の数値で入力してください。";
      }

      return "";
    }

    function showError(message) {
      errorMessage.textContent = message;
      errorMessage.style.display = "block";
      noticeMessage.textContent = "";
      noticeMessage.style.display = "none";
    }

    function showNotice(message) {
      noticeMessage.textContent = message;
      noticeMessage.style.display = "block";
      errorMessage.textContent = "";
      errorMessage.style.display = "none";
    }

    function clearMessages() {
      errorMessage.textContent = "";
      errorMessage.style.display = "none";
      noticeMessage.textContent = "";
      noticeMessage.style.display = "none";
    }

    function createCell(value) {
      const cell = document.createElement("td");
      cell.textContent = value;
      return cell;
    }

    function renderRecords() {
      recordsBody.replaceChildren();

      if (records.length === 0) {
        const row = document.createElement("tr");
        row.className = "empty-row";

        const cell = document.createElement("td");
        cell.colSpan = 9;
        cell.textContent = "まだ記録はありません。";

        row.appendChild(cell);
        recordsBody.appendChild(row);
      } else {
        records.forEach(function (record) {
          const row = document.createElement("tr");
          [
            record.category,
            record.newItem,
            record.orderDate,
            record.deliveryDate,
            record.orderNumber,
            record.partNumber,
            record.itemName,
            record.quantity,
            record.unitPrice
          ].forEach(function (value) {
            row.appendChild(createCell(value));
          });
          recordsBody.appendChild(row);
        });
      }

      recordCount.textContent = "記録件数:" + records.length + "件";
      downloadButton.disabled = records.length === 0;
      clearRecordsButton.disabled = records.length === 0;
    }

    function escapeCsvCell(value) {
      let safeValue = String(value);

      // CSVを表計算ソフトで開いたときに、式として扱われるのを防ぎます。
      if (/^[\s]*[=+\-@]/.test(safeValue)) {
        safeValue = "'" + safeValue;
      }

      return '"' + safeValue.replace(/"/g, '""') + '"';
    }

    function createCsv() {
      const headers = [
        "分類",
        "新規品",
        "受注日",
        "納入日(到着日)",
        "注文番号",
        "品番",
        "品名",
        "数量",
        "単価"
      ];

      const lines = [headers.map(escapeCsvCell).join(",")];

      records.forEach(function (record) {
        const values = [
          record.category,
          record.newItem,
          record.orderDate,
          record.deliveryDate,
          record.orderNumber,
          record.partNumber,
          record.itemName,
          record.quantity,
          record.unitPrice
        ];
        lines.push(values.map(escapeCsvCell).join(","));
      });

      return lines.join("\r\n");
    }

    function downloadCsv() {
      if (records.length === 0) {
        showError("CSVに出力する記録がありません。");
        return;
      }

      const blob = new Blob(["\uFEFF" + createCsv()], {
        type: "text/csv;charset=utf-8"
      });
      const url = URL.createObjectURL(blob);
      const link = document.createElement("a");

      link.href = url;
      link.download = "注文情報の記録.csv";
      document.body.appendChild(link);
      link.click();
      link.remove();

      window.setTimeout(function () {
        URL.revokeObjectURL(url);
      }, 1000);

      showNotice(records.length + "件の記録をCSVでダウンロードしました。");
    }

    function clearRecords() {
      if (records.length === 0) return;

      if (!window.confirm("記録一覧をすべて消去します。よろしいですか?")) {
        return;
      }

      records.length = 0;
      renderRecords();
      clearMessages();
    }

    form.addEventListener("submit", function (event) {
      event.preventDefault();
      clearMessages();

      const data = getFormData();
      const validationError = validateData(data);

      if (validationError) {
        showError(validationError);
        return;
      }

      records.push(data);
      renderRecords();
      form.reset();
      document.getElementById("category").focus();
      showNotice("記録を追加しました。続けて入力するか、CSVをダウンロードしてください。");
    });

    form.addEventListener("reset", function () {
      window.setTimeout(clearMessages, 0);
    });

    downloadButton.addEventListener("click", downloadCsv);
    clearRecordsButton.addEventListener("click", clearRecords);

    renderRecords();
  </script>
</body>
</html>