Skip to content
Back to skills

Fifo Lot Cost Updater

ASecurity

Keep your DataDoe unit costs in step with your purchase lots, first in, first out. You give the lots (SKU, units, unit cost, in the order you sell them); the skill counts the units that left each lot from your orders and your FBA ledger, finds the day each lot ran out, and proposes the dated cost rows that give every lot its own cost - a blended cost on the changeover day, the next lot's cost from the day after. It shows the rows next to the ones already stored and writes them only after you ...

  • 8 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 3, 2026
ai-agentspythongogit

Works with

  • cli
  • mcp

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned December 3, 2026

npx -y skills add Deltologic/datadoe-ai-skills --skill fifo-lot-cost-updater --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Fifo Lot Cost Updater?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Fifo Lot Cost Updater
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/deltologic-fifo-lot-cost-updater/badge)](https://www.skillsdirectory.com/skills/deltologic-fifo-lot-cost-updater)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: fifo-lot-cost-updater
description: >-
  Keep your DataDoe unit costs in step with your purchase lots, first in, first out. You
  give the lots (SKU, units, unit cost, in the order you sell them); the skill counts the
  units that left each lot from your orders and your FBA ledger, finds the day each lot ran
  out, and proposes the dated cost rows that give every lot its own cost - a blended cost
  on the changeover day, the next lot's cost from the day after. It shows the rows next to
  the ones already stored and writes them only after you reply "apply". It never deletes a
  cost. For SKUs sold on one marketplace. Live from DataDoe. Use for "FIFO", "FIFO COGS",
  "lot cost", "batch cost", "my cost changed with the new shipment", "new batch, new cost",
  "update COGS", "landed cost changed", or "cost per lot like Sellerboard".
metadata:
  author: DataDoe
  check-more-skills-at: https://app.datadoe.com/hub/ai-agents-and-skills
  title: FIFO Lot Cost Updater
  access: write
  category: Profit & Finance
  interface: mcp
  output: action
---

# FIFO Lot Cost Updater

DataDoe stores a unit cost per SKU with a start date: the cost applies from that date until
the next dated row. Your real cost changes with every purchase lot, and the old lot does
not run out on a date you can know in advance. This skill works out that date. It counts
the units that left each lot, finds the day the lot ran out, and proposes the cost rows that
make DataDoe charge every unit the cost of the lot it came from. You see every row before
anything is written, and nothing is written until you reply `apply`.

It is not FIFO inside DataDoe: the lots stay in your sheet, and DataDoe keeps one cost per
SKU and day. The skill keeps those dated costs in step with your lots.

## When to use this

- A new shipment arrived at a different unit cost and you want the old cost to stay on the
  units that are still being sold.
- You moved from a tool with batch or FIFO costing and keep a lot list in a sheet.
- Weekly, next to your profit review: has a lot run out since last week?
- Trigger phrases: "FIFO", "FIFO COGS", "lot cost", "batch cost", "new batch, new cost", "my
  cost changed with the new shipment", "update COGS", "landed cost changed".

## The framework. Lots in, cost rows out

### 1. The lots come from you

One table, one row per lot, in the order the lots are sold:

| Column | Needed | Meaning |
| --- | --- | --- |
| `sku` | yes | The seller SKU, exactly as on Amazon. |
| `lot` | no | Your name for the lot (PO number, arrival month). |
| `units` | yes | Units of this lot you can sell. |
| `unit_cost` | yes | Item cost per unit, in the marketplace currency. |
| `unit_shipping` | no | Shipping or inbound cost per unit. Empty means 0. |
| `start_date` | first lot of each SKU | The first day to count from. The lot in this row is the one being sold on that day. |
| `units_left` | first lot of each SKU | Units of that lot not yet ordered at the start of `start_date`. Empty means the whole lot. |

The starting point matters more than anything else in the table: "on 1 October, 180 units
of lot L7 were left". Sales are counted by order date, so `units_left` is what had not been
ordered yet: stock on hand minus the units already reserved for customer orders. Everything
before `start_date` stays as it is. Amazon does not record which lot a unit came from, so
lot sizes and costs can only come from the seller. If a shipment arrived short, the seller
lowers `units`.

Accept the table pasted in chat, as an uploaded CSV or spreadsheet, or from a Google Sheet
when the client has a connector that can read it. Map differently named columns and show
the mapping before counting.

### 2. Count the units that left each lot

From `start_date` to the data day (workflow step 4), per SKU and day:

- **Sold:** units on order lines by order date, cancelled lines left out. Pending orders
  count: they will ship. DataDoe picks the cost of a sale by its order date, so the count
  uses the same date.
- **Left for another reason:** sellable units that were removed, lost or damaged, from the
  FBA ledger.
- **Came back:** customer returns that went back to sellable stock, and units Amazon found
  again, from the FBA ledger. They go to the lot in use.

Units leave the oldest lot that still has units. Within one day the non-sale movement is
applied first, then the sales. When the last lot in the list is used up, the count stops
there: the days after it have no lot, so the skill proposes nothing for them and leaves
their stored rows alone until the next lot is added.

### 3. One cost per day

DataDoe applies one cost to all sales of a SKU on the same day. So:

- A day whose sales come from one lot gets that lot's cost.
- The changeover day, when the old lot runs out in the middle of the day, gets the average
  cost of the units sold that day. 9 units of the old lot and 10 of the new one: (9 x old +
  10 x new) / 19, written with four decimals.
- The next day gets a dated row with the new lot's cost. Always, however small the
  difference: without it the blended cost would stay on every later sale.

With these rows the cost DataDoe charges for the units the skill counted matches FIFO, to
rounding. Without the blended row, the result is off by up to one day of sales times the
cost difference.

### 4. Write only the difference, never delete

The skill reads the cost rows already stored for the SKU and compares them with the cost
the lots give for each day. A stored row is compared to the cent, because DataDoe exports
show two decimals. It proposes:

- **add** - a dated row on a day on which the lots change the cost and no row exists yet;
- **overwrite** - a stored row inside the counted days that carries another cost than the
  lots give for its date, for example a change the seller planned for a date that turned
  out too early. The row keeps its date and its supplier name and gets the right cost.

Rows dated later than the day after the data day (in a normal run: after today) are the
seller's plan. They are shown and left alone. Rows before `start_date` are never touched.
No row is deleted, in any case.

### 5. Only when the lot belongs to this marketplace alone

A lot that is also sold on another marketplace of the same Amazon account runs out faster
than this marketplace's orders show. The skill looks for that in three ways (step 8) and
writes nothing for a SKU when any of them fails:

- orders of the SKU on the other marketplaces of the account that are connected to
  DataDoe;
- the FBA ledger, which covers the whole account: day by day, the units shipped may never
  run ahead of the units ordered on this marketplace;
- the seller's own answer that the SKU is sold here only. A marketplace that is not
  connected to DataDoe shows up in the ledger only, and a few units sold there can hide
  among the orders that have not shipped yet. Only the seller knows.

### 6. Preview, then `apply`

Show the lots, the days they ran out and every proposed row (output format below). Write
only after the user replies with the word `apply`, and only the SKUs that are ready: a SKU
that is blocked or has an open question is shown and not written.

## What it does not do

- **A lot shared by several marketplaces.** When units of the SKU also leave FBA for
  another marketplace of the same Amazon account (Pan-European sales, the European
  Fulfilment Network, the US and Canada from one stock), each marketplace needs its own
  cost row in its own currency and one marketplace's orders see only part of the lot. The
  skill says so and does not write for that SKU. There is no override.
- **Vendor Central.** Seller accounts only.
- **One lot list for several SKUs.** A lot belongs to one SKU.
- **Order inside a day.** The ledger and the orders are daily; the changeover day is
  blended.
- **Refunds at the lot's cost.** A refund uses the cost in effect on the day Amazon
  received the return, not the cost of the lot the unit came from.
- **Counting what never reached Amazon.** Inbound shortages are not in the count. The
  seller's `units` is the truth for the lot size.

Say these when they apply to the account in front of you, not as a list in every answer.

## Configuration

- MCP base: `https://mcp.datadoe.com/mcp/v1`
- **Export rules (every export in this skill):** each `exports_create` call needs `limit` and
  `outputType`. Aggregation aliases must differ from column names (`sum` of `quantity` ->
  alias `units`). Text columns go in `groupBy`, not in an aggregation. If `exports_create`
  already returns `COMPLETED`, skip polling; otherwise poll `exports_get` every ~5 s. For
  results you process in code, use `exports_raw_url_get` and save the file. The `in` filter
  takes a comma-separated value without spaces, so a SKU that contains a comma needs its
  own `=` filter. Do not page with `skip`.
- **An export that returns exactly `limit` rows is cut off.** Read it again in parts and
  check every part the same way: split the SKU list; for a single SKU split the dates into
  two ranges that do not overlap (for the cost table: one export with `from <` a date, one
  with `from >=` that date), and halve again while a part is cut off. A SKU whose data
  cannot be read in full is not written.
- **An identical export is reused for 15 minutes.** DataDoe returns the earlier file when
  the same export is asked for again within 15 minutes - also after a cost was written or
  edited in between. Every export of the cost table in this skill must therefore be a
  fresh one: give it a `limit` that no cost export of the last 15 minutes used. 5,000 minus
  the seconds that have passed in the current hour does it (a value from 1,401 to 5,000).
- **Exports show numbers with two decimals.** A stored cost of 6.3867 reads back as 6.39.
  Never compare a cost you wrote with the read-back value digit by digit; the calculator
  compares a stored row with the lots' cost to the cent.
- **Order Line Items** (`amazon_order_items_with_cogs`), default dataset, needs a date
  period: `date` (purchase date in the marketplace time zone - the date DataDoe uses to
  pick the cost), `sku`, `child_asin` (empty for the newest orders), `quantity`,
  `amazon_order_status` (Pending / Unshipped / Shipped / Canceled, some with a marketplace
  suffix), `item_status`, `fulfillment_channel` (`Amazon` or `Merchant`), `sales_channel`,
  `attribution_method`, `regional_account_id` (the Amazon seller account and its region;
  the same value on every marketplace of that account), `marketplace_country_code`,
  `seller_or_vendor_id`, `cogs_total_value`. Off-Amazon Multi-Channel Fulfillment lines
  have `attribution_method = regional` and a `sales_channel` that is not an Amazon domain;
  they use up the lot like any other sale.
- **FBA Ledger Details** (`amazon_fba_ledger_details`), **not in the default dataset**,
  needs a date period: `date`, `msku`, `event_type` (Adjustments / CustomerReturns /
  Receipts / Shipments / VendorReturns / WhseTransfers), `disposition`, `quantity`,
  `country` (the fulfillment center's country). `quantity` is signed: shipments, removals
  and losses are negative, returns and found units positive. The rows belong to the whole
  Amazon seller account in a region (North America, Europe or Far East), not to one
  marketplace: an export for one marketplace returns the movements of every country of
  that account. History is limited by the table's retention (`retentionDays` in
  `exports_sources_get`; the organization can change it in Settings > Data). Step 4 finds
  the first day that is really there.
- **Cost of Goods Sold** (`amazon_cogs`), no date period: `sku`, `child_asin`, `from`,
  `cost_item_value`, `cost_item_shipping_value`, `cost_currency`, `item_supplier_name`.
  DataDoe matches a cost to a sale on SKU and ASIN and takes the latest `from` on or before
  the order date. A row under another ASIN gives that sale no cost at all.
- **Write tool `cogs_upsert`:** `sellerOrVendorId`, `accountType: "SELLER"`, `cogsToUpsert`
  with at most **25** rows per call, each `{ sku, asin, fromDate, costItemValue,
  costItemShippingValue, costCurrency }`. One seller (one marketplace) per call.
  `costCurrency` must be the marketplace currency or the call is rejected. A row is
  identified by SKU, ASIN, currency and `fromDate`: the same key overwrites that row, any
  other row stays. One call is stored as a whole or not at all. There is **no dry run** on
  this tool - the preview is the skill's own table, so build it with care.
- **Supplier names belong to single rows.** An overwrite replaces the row's supplier name
  too: a row sent without `itemSupplierName` loses the name it had. When a row is
  overwritten, send the name stored on that very row (`supplier` in the calculator's
  result) and leave the field out when that row had none. A new row gets no supplier name,
  unless the user names one for it. Never copy a name from another date.
- **Never call `cogs_delete`.** With too few filters it removes every cost of a SKU or of
  the whole account. This skill corrects a row by overwriting it.
- **Calculator:** `scripts/fifo_lot_costs.py` in this skill's folder (Python 3, no
  packages) does step 9; `--selftest` runs its own checks. Its input is one JSON file:
  `{ "data_day": "YYYY-MM-DD", "evidence_read_at": "2026-03-28T08:05:00Z", "skus": [ {
  "sku", "anchor_date", "lots": [ { "lot", "units", "unit_cost", "unit_shipping" } ],
  "sold": { "YYYY-MM-DD": units }, "other_out": { "YYYY-MM-DD": units }, "stored": [ {
  "from", "unit_cost", "unit_shipping", "supplier" } ], "confirmed": [], "check": { "from",
  "orders_amazon": { "YYYY-MM-DD": units }, "ledger_shipments": { "YYYY-MM-DD": units },
  "other_marketplaces": { "DE": units }, "one_marketplace_confirmed": true } } ] }`. Use it
  whenever you can run code. If you cannot, follow the rules in "The framework" and in step
  9 by hand, one SKU at a time, and say that the numbers were worked out without the
  calculator.
- **Nothing is assumed from a missing key.** `sold`, `other_out`, `stored` and the three
  maps of `check` must all be passed. An empty map or list means a read that was completed
  and found nothing; a read that was not done is left out, and the calculator then refuses
  the SKU or blocks its write. Never fill a map with `{}` to get past a read you skipped.
- **Order, ledger and marketplace data is good for 15 minutes.** A pending order can be
  cancelled and a late shipment can land. `evidence_read_at` is the UTC time just before
  the first export of steps 6 to 8 was started; the calculator blocks every write once
  that is more than 15 minutes ago.
- **Access:** reading needs nothing special. `cogs_upsert` changes costs stored in DataDoe,
  not anything on Amazon. Backdated rows restate past profit: that is the point of the
  skill, and the preview says by about how much.

## Step-by-step workflow (MCP-native)

### Phase 1 - Read

1. `sellers_and_vendors_list` -> pick the seller (`accountType` SELLER), keep
   `sellerOrVendorId`, the marketplace and the marketplace's currency, and note the other
   seller accounts of the organization. A Vendor account: stop and say the skill is for
   seller accounts.
2. **Lots.** Read the table, map the columns, and check per SKU: at least one lot; whole
   units, not negative; a cost on every lot; a `start_date` on the first lot (ask for it if
   missing - never guess it); `units_left` above 0 and not above `units` (a first lot with
   nothing left is not the lot in use: start with the next one). Lots priced in another
   currency than the marketplace's: stop for that SKU and ask for the cost in the
   marketplace currency. Show the lots as understood. **Ask once:** is any of these SKUs
   also sold on another Amazon marketplace from the same stock, including a marketplace
   that is not connected to DataDoe? Only a clear "no" for a SKU counts as confirmed.
3. `exports_sources_get` (queries "ledger", "order line items", "cogs") -> source ids,
   `enabled`, and `retentionDays` of the ledger. If FBA Ledger Details is not enabled: stop
   and ask the user to turn it on in DataDoe (Settings > Data) and to come back when the
   first load has finished. Without the ledger, removed and lost units are missing and
   every lot looks fuller than it is.
4. **Ledger range, data day, check period.** One export on `amazon_fba_ledger_details`
   with no `groupBy`: `min` of `date` as `first_day`, `max` of `date` as `last_day`. Its
   `from` is 7 days before the earliest day you need - the earlier of the earliest
   `start_date` and 60 days before yesterday - so a long retention is used in full.
   - The **data day** is `last_day`, or yesterday if `last_day` is later. Say it in the
     answer; if it is more than 4 days back, say the ledger is behind.
   - A `start_date` earlier than `first_day`: the ledger does not reach that far (say its
     retention). Stop for that SKU and ask for the units left on `first_day` or later.
   - Per SKU the **check period** starts on the earlier of its `start_date` and 60 days
     before the data day, but not before `first_day`, and ends on the data day.
5. **Stored costs, read fresh.** Export `amazon_cogs` with `sku in (...)` and a `limit`
   chosen as the Configuration says: `sku`, `child_asin`, `from`, `cost_item_value`,
   `cost_item_shipping_value`, `cost_currency`, `item_supplier_name`. Keep the supplier name
   of every single row. Per SKU:
   - a row in another currency than the marketplace's: stop for that SKU and show the rows;
   - rows under one ASIN: that is the cost history to compare with (step 8 checks that the
     SKU is still sold under that ASIN);
   - rows under more than one ASIN: use the rows of the ASIN the SKU is sold under (step
     6) and say the other rows are left alone;
   - no rows at all: the first row is added on `start_date`, under the ASIN the SKU is sold
     under. Say that sales before `start_date` still have no cost.
6. **Sold units.** Export `amazon_order_items_with_cogs`, `from` = 2 days before the
   earliest check period start, `to` = data day, `sku in (...)`, `groupBy ["date", "sku",
   "child_asin", "fulfillment_channel", "amazon_order_status", "item_status",
   "sales_channel", "attribution_method"]`, `dateInterval: "DAY"`, `sum` of `quantity` as
   `units`, CSV, `limit` 5,000. Drop the rows where `amazon_order_status` or `item_status`
   is Canceled (or Cancelled). Per SKU:
   - `sold` = all remaining units per day from its `start_date` on, pending and unshipped
     lines included, `Amazon` and `Merchant` fulfilled alike;
   - `orders_amazon` = the `Amazon` fulfilled units per day from 2 days before its check
     period on (off-Amazon lines included);
   - keep the merchant-fulfilled and the off-Amazon units as separate totals for the
     report, and note the ASINs the SKU was sold under (ignore empty ones).
   Note the UTC time just before this export starts: it is `evidence_read_at`. A SKU
   without a row gets an empty map; a SKU whose export failed or was cut off gets none.
7. **Ledger movements.** Export `amazon_fba_ledger_details`, `from` = the earliest check
   period start, `to` = data day, `msku in (...)`, `disposition = SELLABLE`, `event_type
   in` the value `Shipments,CustomerReturns,VendorReturns,Adjustments`, `groupBy ["date",
   "msku", "event_type", "country"]`, `dateInterval: "DAY"`, `sum` of `quantity` as
   `units`, CSV, `limit` 5,000. Per SKU, all countries together:
   - `other_out` = per day from its `start_date` on, minus the sum of `units` over
     CustomerReturns, VendorReturns and Adjustments;
   - `ledger_shipments` = per day in its check period, minus the sum of `units` over
     Shipments (a positive number). Shipments are for the check only - sales are counted
     from the orders.
   Receipts and transfers between fulfillment centers are not movements out of a lot and
   are not read.
8. **Orders on other marketplaces.** When the organization has other seller accounts,
   export `amazon_order_items_with_cogs` for the selected seller and the others together
   (`sellerOrVendorIds` takes up to 5; more accounts need more exports, each with the
   selected seller in it), the whole check period, `sku in (...)`, `groupBy ["sku",
   "seller_or_vendor_id", "regional_account_id", "marketplace_country_code",
   "attribution_method", "amazon_order_status", "item_status"]`, `sum` of `quantity` as
   `units`. Drop cancelled rows and rows with `attribution_method = regional`. The selected
   seller's rows give its `regional_account_id`. Per SKU, `other_marketplaces` = the units
   on rows of another seller with the same `regional_account_id`, by marketplace. If the
   selected seller's rows have no `regional_account_id`, count every other seller's units
   of that SKU. `other_marketplaces` is an empty map only when this export ran and found
   nothing, or when step 1 showed that the organization has no other seller account. If
   the export failed, leave the map out: the SKU is then blocked.

### Phase 2 - Check, then calculate

9. **Calculate.** One more check first: the SKU was sold under exactly one ASIN in the read
   period, and the stored rows used are under that ASIN. Sold under several ASINs, or
   stored only under another ASIN: stop for that SKU and show both - a row under the wrong
   ASIN gives the sales no cost. No sale at all and no stored row: there is no ASIN to
   write under; stop for that SKU.
   Then build the calculator input: `data_day`; per SKU `anchor_date` = `start_date`,
   `lots` in order with the first lot's `units` = `units_left`, `sold`, `other_out`,
   `stored` = the SKU's rows from step 5 as `from`, `unit_cost`, `unit_shipping`,
   `supplier`, and `check` = `from` (the start of its check period), `orders_amazon`,
   `ledger_shipments`, `other_marketplaces`, `one_marketplace_confirmed` (true only after a
   clear "no" in step 2); at the top `evidence_read_at` from step 6. Run
   `scripts/fifo_lot_costs.py`. A SKU in `errors` has no result and is not written: fix its
   input, do not work around it. Before using the result, check
   that `units_sold` equals the units counted in step 6 from `start_date` to the data day;
   a flag `UNITS_OUTSIDE_COUNTED_DAYS` means dates outside that range were passed in.
   Read from the result: per lot the units left and the day it ran out; `rows_to_write`
   with `add` or `overwrite`, the supplier name to send back, the cost applied now and a
   note on blended days; `cogs_change_for_counted_days`; `stored_rows_left_alone`; the
   projection (show it only when the lot runs out within 90 days); `flags`; `questions`;
   `blockers`; `ready_to_write`.
   - **`blockers` keep a SKU out of the write, whatever the user says:**
     `SOLD_ON_OTHER_MARKETPLACE` (orders of the SKU on another marketplace of the same
     account - name the marketplace and the units), `SHIPMENTS_NOT_EXPLAINED` (on some day
     more units had left FBA than had been ordered on this marketplace up to that day,
     with 2 days allowed for orders placed just before the period - give the first such
     day, the largest gap and both totals; the lot is shared with a marketplace or channel
     DataDoe does not see, or the orders are incomplete), `NOT_CONFIRMED_ONE_MARKETPLACE`,
     `LEDGER_INCOMPLETE` (far fewer shipments in the ledger than orders: ledger days are
     missing), `CHECK_PERIOD_TOO_SHORT` (fewer than 14 days to compare; the ledger is still
     loading), `CHECK_STARTS_AFTER_ANCHOR`, `CHECK_NOT_RUN`, `CHECK_DATA_MISSING` (one of
     the three reads was not passed in - run it, do not pass an empty map instead),
     `SOLD_UNITS_INCOMPLETE` (on some day `sold` holds fewer units than the
     Amazon-fulfilled orders of the check - `sold` was cut short, build it again from step
     6), `EVIDENCE_EXPIRED` and `EVIDENCE_TIME_MISSING` (the orders, the ledger and the
     other marketplaces were read more than 15 minutes ago, or the time is unknown - repeat
     steps 4 and 6 to 8).
   - Flag `LOTS_EXHAUSTED`: the lots are used up - the list is missing the next lot. No
     row is proposed from that day on and the stored rows there are left alone. Show the
     day and ask for the next lot. Rows before that day are still valid.
   - Flag `NO_STORED_COST_BEFORE_ANCHOR`: no cost applied on `start_date`; the first row is
     added there.
   - `questions` keep a SKU out of the write until the user answers:
     `START_COST_DIFFERS` (the cost stored for `start_date` is not the first lot's cost -
     which one is right?), `MANY_OVERWRITES` (three or more stored rows would be
     overwritten - were the lots corrected since the last run, or does something else
     write costs for this SKU? Two writers undo each other every week), `ZERO_COST_LOT`.
     When the user confirms, run the calculator again with the code in `confirmed` for that
     SKU. When the user says the stored cost is right, correct the lot instead.

### Phase 3 - Preview

10. Show the preview (output format) in three groups: **ready to write**
    (`ready_to_write` true), **needs your answer** (open questions: show the rows and the
    question), **not written** (a blocker or a stop from an earlier step, with the reason
    and its numbers). Per SKU: the lots, what was counted, the proposed rows, and about how
    much the cost of goods changes for the counted days (when no cost applied before, say
    that those days had no cost at all, so the whole amount is new; pending and off-Amazon
    units are in the figure, DataDoe's profit tables add them when they ship or not at
    all). Mark a change dated in the last 3 days as provisional: a pending order can still
    be cancelled, and the next run then moves the date. Say when the data was read and that
    an `apply` later than 15 minutes after it reads everything again first. End with the
    line that asks for `apply`, counting only the rows of the ready SKUs. If nothing
    differs, say that the stored costs already match the lots and stop.

### Phase 4 - Apply (only after `apply`)

11. On the word `apply`:
    - **Read the stored costs again first, every time** (step 5, a fresh export).
    - **Read the orders, the ledger and the other marketplaces again when they are older
      than 15 minutes or the day has changed** (steps 4 and 6 to 8, with a new
      `evidence_read_at`). An approval given the next morning is an approval of
      yesterday's counts: a pending order may have been cancelled since, and a blended
      cost built on it would be wrong.
    - Run the calculator again and compare with the preview the user approved: the same
      SKUs ready, the same rows (date, costs, add or overwrite), the same values they
      replace and the same supplier names. Anything different - a row, a blocker, a
      question, the data day - means the plan the user approved no longer holds: show the
      new preview, say what changed, and wait for a new `apply`. If the lots changed since
      the preview, start again from step 2.
    - Only an unchanged plan is written: call `cogs_upsert` for the ready SKUs,
      `accountType: "SELLER"`, `asin` from step 9, `costCurrency` = the marketplace
      currency, `itemSupplierName` per row as the Configuration says.
    - **All rows of one SKU go in one call.** A blended row without the row of the next
      day would leave the blended cost on every later sale. Pack whole SKUs into calls of
      at most 25 rows. A SKU with more than 25 rows gets calls of its own, oldest dates
      first.
    - The user can leave out a whole SKU, never single rows of a SKU.
    - Send the calls one after another, never in parallel. On HTTP 429 nothing of that
      call was stored: wait for the `Retry-After` time and send the same call once more; a
      second 429 ends the run for the SKUs that are left. On a clear rejection (an error
      that names the problem) stop, show the message, and say which SKUs are stored and
      which are not.
    - **A call with no clear answer** (a timeout, a lost connection, an error that does not
      say the rows were rejected) may still be running. Never send it again on your own: a
      second request is a second write and is charged again, and a read made right after
      the timeout can come back empty while the first write is still on its way. Read the
      stored costs fresh and compare the rows of that call. All there: it was stored.
      Anything else - none, some, or a failed read: report the call's SKUs as **not
      verified**, say which rows are missing right now, and stop. No later call of those
      SKUs goes out. The seller asks for the lot costs again in a few minutes: the fresh
      preview then shows whatever is still missing, and a new `apply` writes it.
    - For a SKU that was only partly stored (more than 25 rows), say that its costs after
      the last stored date are wrong until the rest is written.
12. **Check what was stored.** Read the stored costs again as in step 5 (a fresh export
    with a new `limit`) and run the calculator again with them. It must propose no row for
    the SKUs just written - that includes the row of the day after every blended day. If
    it proposes one, show it and say the write is incomplete. Then offer one check in the
    data: Order Line Items for one changeover day, `item_status = Shipped`, grouped by
    `date` - the sum of `cogs_total_value` divided by the sum of `quantity` is the cost of
    that day (item plus shipping, to the cent). If it does not show the new cost yet, say
    so; do not write again.

## Output format

```
Lot costs - {seller} - {marketplace} ({currency}) - counted to {data day}

READY TO WRITE
{sku}  {product name if known}
Lots
  {lot}   {units at start} units   {cost} + {shipping}   ran out {date}
  {lot}   {units at start} units   {cost} + {shipping}   in use, {n} left (about {date} at {pace} a day)
Counted since {start_date}: {sold} sold{, of them {m} merchant-fulfilled}{, {o} off Amazon}; {x} removed, lost or damaged; {r} back to sellable stock
Checked since {check start}: {a} ordered here, {b} shipped by Amazon; no orders on other marketplaces; you confirmed it is sold here only
Cost rows
  {date}   {cost} + {shipping}   add         now {old cost}   blended: {a} from {lot}, {b} from {lot}
  {date}   {cost} + {shipping}   add         now {old cost}
  {date}   {cost} + {shipping}   overwrite   stored {old cost}
Left alone: your row dated {future date} ({cost})
Cost of goods for the counted days: about {+/-}{amount} {currency}

NEEDS YOUR ANSWER
{sku} - {question in one sentence}; {rows as above, not counted below}

NOT WRITTEN
{sku} - {reason in one sentence, with its numbers}

Reply "apply" to write {N} rows for {K} SKUs. Nothing is written until then, and no cost is deleted.
```

After `apply`:

```
Stored: {K} SKUs, {N} rows; a second run of the calculation finds nothing left to change.
Past profit for {first date} to {data day} now uses the lot costs.
Next: add a row to your lot list when the next shipment is ordered, and run this again in a week.
```

## Scheduled runs

A scheduled run reads and reports; it does not write, because nobody is there to reply
`apply`. Send the same preview, shortened to the SKUs with something to do: a lot that ran
out, a lot that runs out within 14 days at the current pace, a SKU with no next lot in the
list, a SKU with a question, a SKU that is blocked. End with: "Open a chat, ask for the lot
costs and reply apply to write these rows." Say nothing when nothing changed, if the user
asked for a quiet mode.

Move `start_date` forward every few months: the lot in use and the units left on a recent
day. The ledger keeps a limited number of days (step 4), and a shorter count is faster to
read and to check.

## Worked example (illustrative)

A headlamp, SKU `HL-400`, one marketplace, USD. The seller's list: lot `PO-17`, 500 units
at 6.20 + 0.80, with 120 left on 1 March; lot `PO-21`, 800 units at 6.90 + 0.80. The seller
confirms the SKU is sold on this marketplace only. Stored in DataDoe: 6.20 + 0.80 from last
year, and a planned row of 6.90 + 0.80 on 20 March.

Counted from 1 March to the data day, 27 March: 15 sold every day, 6 units removed on 3
March, 2 sellable returns on 5 March. After 7 March, 120 - 105 - 6 + 2 = 11 units of
`PO-17` are left. On 8 March 15 are sold: 11 from `PO-17` and 4 from `PO-21`.

- 8 March: add 6.3867 + 0.80 (blended: 11 x 6.20 and 4 x 6.90, divided by 15).
- 9 March: add 6.90 + 0.80.
- 20 March: the planned row already carries 6.90 + 0.80, the right cost for that day.
  Nothing to do.

The check: since late January this marketplace's orders and the ledger's shipments move
together, the shipments a day behind; no other marketplace has orders of the SKU.

Until now DataDoe charged 6.20 + 0.80 up to 19 March. Cost of goods for the counted days
goes up by about 118.30 USD: 15 x 0.1867 on 8 March, and 15 x 0.70 on each of the 11 days
from 9 to 19 March. On `apply` the stored costs are read once more and are unchanged, so
the two rows go out in one call. Then the costs are read again (the 8 March row now shows
as 6.39) and a second run of the calculation finds nothing left to change. The planned row
stays.

## Quality self-check

- Did every SKU get a `start_date` and the units left on that day from the user, not from
  a guess?
- Did the user say clearly that the SKU is sold on this marketplace only, and did I look
  for its orders on every other seller account of the organization?
- Did I pass the check data to the calculator, and keep every SKU with a blocker out of
  the write, whatever the user asked?
- Are sales counted by order date from Order Line Items, with cancelled lines out and
  pending lines in?
- Are the other movements taken from the ledger with `disposition = SELLABLE` only, and
  are receipts and transfers left out?
- Is the SKU sold under one ASIN, and do the rows go under that ASIN, in the marketplace
  currency?
- Does every overwritten row carry the supplier name of that same row, and no row a name
  copied from another date?
- Is `start_date` on or after the first day the ledger holds?
- Was every read of the cost table a fresh one (a `limit` not used in the last 15
  minutes), and was no export cut off at its `limit`?
- Does `units_sold` in the calculator's result equal my count from the orders?
- Is there a blended row on a day that used two lots, and a row with the new lot's cost on
  the next day, both in the same call?
- Did I leave the seller's future rows, the rows before `start_date` and the rows after
  the last lot ran out alone?
- Did I keep SKUs with an open question out of the write and out of the row count?
- Did I show every row before writing, and write only after the word `apply`?
- Right before writing, did I read the stored costs again - and the orders, the ledger and
  the other marketplaces too when they were older than 15 minutes - and find the plan
  unchanged?
- Did I pass every map the calculator needs, with an empty map only for a read that ran
  and found nothing?
- After writing, did a second run of the calculation with freshly read costs propose
  nothing?
- For a call without a clear answer, did I read back, report it as not verified unless
  every row was there, and send nothing again on my own?
- Did I avoid `cogs_delete` entirely?

## Common mistakes

- Counting sales by ship date. DataDoe picks the cost by order date; an order placed on
  the last day of a lot and shipped the next day belongs to the old lot.
- Counting only orders. Removed, lost and damaged units also use up a lot; without them
  the lot seems to last longer than it did.
- Adding ledger Shipments on top of the orders. That counts every sale twice. Shipments
  are for the check only.
- Reading every disposition. A customer return that came back damaged never returned to
  sellable stock; only `SELLABLE` rows move the lot.
- Writing the new lot's cost from the changeover day itself. The units of the old lot sold
  that morning then get the new cost. Blend that day.
- Writing the blended row and not the row of the next day - or skipping that row because
  the new cost is almost the same. Every later sale then gets the blended cost.
- Putting a cost on the days after the last lot in the list ran out. No lot says what
  those units cost; ask for the next lot.
- Comparing totals of orders and shipments with a tolerance and calling the lot unshared.
  A few units sold on another marketplace fit inside any tolerance; look for the other
  marketplace's orders and ask the seller.
- Deleting a planned row that turned out too early. Overwrite it with the cost that is
  right for its date; a delete with too few filters removes far more.
- Writing a row under a new ASIN or without one. DataDoe matches on SKU and ASIN; a row
  under another ASIN gives the sale no cost.
- Sending one supplier name for the whole SKU. An overwrite then puts the wrong supplier
  on a row from another shipment.
- Reading the cost table with the same export twice within 15 minutes. The second read
  returns the first file, also when a cost was written in between: a recalculation then
  works from costs that are no longer the stored ones.
- Writing the plan from a preview that is minutes old without reading the costs again.
  An edit made in DataDoe in between is overwritten unseen.
- Reporting a row as "not stored" because 6.3867 reads back as 6.39. Exports show two
  decimals.
- Sending a call again after a timeout, even after a read that came back empty. The first
  write may still land, and the second request is charged again. Report it as not
  verified and let the next preview pick up what is missing.
- Writing on an `apply` that comes hours after the preview without reading the orders
  again. A pending order that was cancelled in between changes the blended cost.
- Passing an empty map for a read that was skipped or failed. Empty means "looked and
  found nothing"; the calculator cannot tell the difference, so only you can.
- Running this next to another process that writes costs for the same SKU. Each run
  overwrites the other's rows. One writer per SKU.

## Notes

- Write skill. It changes the unit costs stored in DataDoe, after a preview and the word
  `apply`. It does not change anything on Amazon and it never deletes a cost.
- Running it again is safe: rows that are already right are not proposed again, and a
  date that moved is corrected by overwriting.
- A DataDoe skill, built on Order Line Items, FBA Ledger Details, the Cost of Goods Sold
  table and `cogs_upsert`. For the weekly profit view that uses these costs, see
  `weekly-business-review` and `net-profit-pl-analyzer`.

Files in this skill

  • SKILL.md37.9 KB
  • scripts/fifo_lot_costs.py44.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.