{
  "name": "AIva Starter · Из заказов — в контроль маржи",
  "nodes": [
    {
      "parameters": {},
      "id": "order-margin-start",
      "name": "Запуск вручную",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        180
      ],
      "notes": "Нажмите Execute workflow в n8n. Внешние сервисы не вызываются."
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return [{\"json\":{\"id\":\"D-01\",\"price\":4500,\"cost\":2000,\"qty\":2,\"shipping\":400}},{\"json\":{\"id\":\"D-02\",\"price\":1000,\"cost\":850,\"qty\":1,\"shipping\":200}},{\"json\":{\"id\":\"D-03\",\"price\":2400,\"cost\":1600,\"qty\":3,\"shipping\":300}}];"
      },
      "id": "order-margin-0",
      "name": "Заказы",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        180
      ],
      "notes": "Пример из трёх заказов."
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return $input.all().map(({json:x})=>({json:{...x,revenue:x.price*x.qty,total_cost:x.cost*x.qty+x.shipping}}));"
      },
      "id": "order-margin-1",
      "name": "Стоимость",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        180
      ],
      "notes": "Считает выручку и себестоимость с доставкой."
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return $input.all().map(({json:x})=>({json:{...x,profit:x.revenue-x.total_cost,margin:x.revenue?Math.round((x.revenue-x.total_cost)/x.revenue*100):0}}));"
      },
      "id": "order-margin-2",
      "name": "Маржа",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        180
      ],
      "notes": "Вычисляет прибыль и процент маржи."
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return $input.all().map(({json:x})=>({json:{...x,review:x.margin<20}}));"
      },
      "id": "order-margin-3",
      "name": "Проверка порога",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        180
      ],
      "notes": "Отмечает маржу ниже 20%."
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return $input.all().map(({json:x})=>({json:{order:x.id,profit:x.profit,margin_percent:x.margin,review_required:x.review}}));"
      },
      "id": "order-margin-4",
      "name": "Отчёт заказов",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        70
      ],
      "notes": "Выдаёт отчёт для проверки человеком."
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const a=$input.all().map(x=>x.json);return [{json:{revenue:a.reduce((s,x)=>s+x.revenue,0),profit:a.reduce((s,x)=>s+x.profit,0),review:a.filter(x=>x.review).length}}];"
      },
      "id": "order-margin-5",
      "name": "Общая прибыль",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        290
      ],
      "notes": "Суммирует выручку и расчётную прибыль."
    }
  ],
  "connections": {
    "Запуск вручную": {
      "main": [
        [
          {
            "node": "Заказы",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Заказы": {
      "main": [
        [
          {
            "node": "Стоимость",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Стоимость": {
      "main": [
        [
          {
            "node": "Маржа",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Маржа": {
      "main": [
        [
          {
            "node": "Проверка порога",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Проверка порога": {
      "main": [
        [
          {
            "node": "Отчёт заказов",
            "type": "main",
            "index": 0
          },
          {
            "node": "Общая прибыль",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "tags": []
}
