{
  "name": "【freee】注文メールから請求書の下書きを作る（送信前の確認版）",
  "nodes": [
    {
      "parameters": {},
      "name": "手動で開始",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "f1"
    },
    {
      "parameters": {
        "jsCode": "// 実際の運用では、Gmailトリガーで受け取ったメール本文がここに入る\nreturn [{ json: { 本文: \"件名：【ご注文】11月分のご依頼\\n差出人：経理担当（サンプル商事）\\n\\nいつもお世話になっております。下記のとおり注文いたします。\\n\\n取引先コード：C001\\n品名：ホームページ保守（11月分） 数量：1 単価：33000\\n品名：ブログ記事の作成 数量：4 単価：15000\\n品名：画像素材の調整 数量：2.5 単価：4000\\n\\nよろしくお願いいたします。\" } }];"
      },
      "name": "注文メール（サンプル）",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        190,
        0
      ],
      "id": "f2"
    },
    {
      "parameters": {
        "jsCode": "// 注文メールの本文から、取引先コードと明細（品名・数量・単価）を取り出す\nconst text = $input.first().json.本文;\nconst partner = (text.match(/取引先コード[：:]\\s*(\\S+)/) || [])[1];\nconst lines = [...text.matchAll(/品名[：:]\\s*(.+?)\\s+数量[：:]\\s*([\\d.]+)\\s+単価[：:]\\s*([\\d,]+)/g)]\n  .map(m => ({ 品名: m[1], 数量: Number(m[2]), 単価: Number(m[3].replace(/,/g, '')) }));\nif (!partner || lines.length === 0) throw new Error('取引先コードか明細が読み取れませんでした。メールの書式を確認してください');\nreturn [{ json: { 取引先コード: partner, 明細: lines } }];"
      },
      "name": "注文内容を読み取る",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        380,
        0
      ],
      "id": "f3"
    },
    {
      "parameters": {
        "jsCode": "// freee請求書API（POST /iv/invoices）に送る内容を組み立てる\n// 事業所IDなどは自社の値に置き換える\nconst COMPANY_ID = 1234567; // ← freeeの事業所ID\nconst o = $input.first().json;\n// 日付は日本時間で作る（toISOString() はUTCになり、日本時間の朝9時前は前日の日付になる）\nconst now = $now.setZone('Asia/Tokyo');\nconst billingDate = now.toISODate();\n// 支払期日：翌月末\nconst dueDate = now.plus({ months: 1 }).endOf('month').toISODate();\nreturn [{ json: {\n  company_id: COMPANY_ID,\n  billing_date: billingDate,\n  payment_date: dueDate,\n  payment_type: 'transfer',\n  tax_entry_method: 'out',\n  tax_fraction: 'omit',\n  withholding_tax_entry_method: 'out',\n  partner_code: o.取引先コード,\n  partner_title: '御中',\n  subject: '注文メールからの自動作成（確認待ち）',\n  memo: 'n8nで自動作成。送付前に内容を確認すること',\n  lines: o.明細.map(l => ({ type: 'item', description: l.品名, quantity: l.数量, unit_price: String(l.単価), tax_rate: 10 })),\n} }];"
      },
      "name": "freeeに送る内容を作る",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        570,
        0
      ],
      "id": "f4"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.freee.co.jp/iv/invoices",
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}",
        "options": {}
      },
      "name": "freee請求書に作成（要：freee連携）",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        760,
        0
      ],
      "id": "f5",
      "disabled": true
    }
  ],
  "connections": {
    "手動で開始": {
      "main": [
        [
          {
            "node": "注文メール（サンプル）",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "注文メール（サンプル）": {
      "main": [
        [
          {
            "node": "注文内容を読み取る",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "注文内容を読み取る": {
      "main": [
        [
          {
            "node": "freeeに送る内容を作る",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "freeeに送る内容を作る": {
      "main": [
        [
          {
            "node": "freee請求書に作成（要：freee連携）",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}