Expiry-Aware Inventory
for Fresh Food
Fresh groceries spoil in days, and the same product sits in the warehouse in batches with different expiry dates. For a Market Kurly–style dawn-delivery service, we designed a system that tracks every batch, ships the soonest-to-expire stock first, discounts items as they near expiry, and records every disposal. We took it from requirements and data model through decision rules to working SQL and an admin dashboard.
Background & Problem
Background
Dawn and same-day delivery have moved fresh groceries online. Services like Market Kurly promise produce, dairy, and meat that are only days from expiry, so every day in the warehouse counts.
Problem
Short shelf lives cause both stockouts and waste. Counting units per product isn’t enough: 75 strawberries may be 30 that expire tomorrow and 45 that last three more days. Every unit thrown away adds disposal cost and environmental harm.
Key question: How can one system manage expiry dates, stock, shipments, and disposals together, so near-expiry stock leaves the warehouse before it spoils?
Requirements
We split the requirements by who acts: what the warehouse manager needs to see and record, and what the system must decide on its own.
- R1 · ManagerSee current stock and expiry dates per product, to spot shortages and items at risk.
- R2 · ManagerSee a list of near-expiry items, which the system flags once they fall within a set number of days.
- R3 · ManagerRecord and review disposals with quantity, reason, and date.
- R4 · SystemWhen a shipment request comes in, check whether enough usable stock exists.
- R5 · SystemShip from the batch closest to expiry first (FEFO), and log each shipment.
- R6 · SystemApply preset discounts to near-expiry items automatically, and show the manager which items are discounted.
Scope
The system does
- Track stock and expiry by batch
- Check shippable stock for each order
- Ship the soonest-to-expire batch first
- Set discounts by days left and stock
- Log disposals and surface items often wasted
The system does not
- Manage suppliers or create purchase orders
- Optimize delivery routes
- Give personalized coupons to customers
- Forecast demand with AI
System Design
How data moves
The data flow diagram links four processes (order, payment, inventory, and shipping) to the customer, payment gateway, and shipping provider. Inventory management sits at the center: it answers stock requests with pickable stock, reads expiry dates and stock levels, and writes disposal records.

How data is stored
Seven entities: Admin, Product, Discount policy, Inventory, Shipment request, Shipment, and Disposal.

Each Inventory row is one received batch with its own expiry date and remaining quantity. Without batches, FEFO and expiry-based discounts can’t be computed.
One request can be filled from several batches, so a Shipment request (with status Pending / Complete / Unavailable) links to one or more Shipment rows.
Normalization
We normalized the schema step by step to Third Normal Form so that a price or discount changes in one place, not in every table that copied it.
- 1NF · Non-atomic valueOne discount cell held two rules: “≤3 days → 30%, ≤1 day → 50%”One value per cellSplit into a column per threshold
- 2NF · Repeating columnsDiscount columns repeated for each threshold on the productDiscount policy tableOne row per product and threshold
- 3NF · Transitive dependencyProduct name and price copied into Inventory, Request, Shipment, and DisposalReference by keyThose tables keep only the product ID as a foreign key
Final relation schema 7 tables · PK, FK
| Table | Columns |
|---|---|
| Admin | admin_id, name, email, phone, registered_at |
| Product | product_id, product_name, category, unit_price |
| Discount policy | policy_id, product_id, days_left, discount_rate |
| Inventory | inventory_id, product_id, received_date, expiration_date, received_qty, current_quantity, unit_cost |
| Shipment request | request_id, admin_id, product_id, request_quantity, requested_at, request_status |
| Shipment | shipment_id, request_id, inventory_id, shipment_quantity, shipped_at |
| Disposal | disposal_id, inventory_id, admin_id, disposal_quantity, disposal_reason, disposal_date, note |
Rules & Logic
Three rules drive the system. We wrote each as a decision table first, so every combination of conditions had a defined outcome, including edge cases like a zero-quantity request or a product whose every batch has expired. Figure 1 shows all three on one sample order.
Decision tables 3 tables
| Conditions / Actions | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Expires today (0 days left) | N | N | N | Y | Y |
| ≤ 1 day left | N | N | Y | – | – |
| ≤ 3 days left | N | Y | Y | – | – |
| Stock > 0 | Y | Y | Y | Y | N |
| No discount | X | ||||
| 30% off | X | ||||
| 50% off | X | ||||
| Mark for disposal | X | X |
| Conditions / Actions | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Total stock ≥ requested | Y | N | Y | N | – |
| Unexpired stock exists | Y | Y | N | N | – |
| Requested quantity > 0 | Y | Y | Y | Y | N |
| Status → Complete | X | ||||
| Status → Unavailable (short stock) | X | X | |||
| Status → Unavailable (all expired) | X | ||||
| Status → Unavailable (invalid request) | X | ||||
| Run FEFO picking | X |
| Conditions / Actions | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| 2+ batches of the product | Y | Y | Y | N | N |
| First batch covers the remainder | Y | N | – | Y | N |
| First batch not expired | Y | Y | N | Y | Y |
| Ship all from first batch | X | X | |||
| Ship part, move to next batch | X | ||||
| Skip batch, flag for disposal | X | ||||
| Log shipment, deduct stock | X | X | X |
Y = yes · N = no · – = doesn’t matter · X = action runs
Pseudocode 3 procedures
Find near-expiry items and apply discountsDO
READ TABLE (inventory, product, discount_policy)
FIND inventory WHERE expiration_date >= TODAY
SET days_left = DATEDIFF(expiration_date, TODAY)
IF days_left = 0
SET action = 'dispose'
ELSEIF days_left <= 1
SET rate = discount_policy.rate WHERE days_left = 1 // 50%
ELSEIF days_left <= 3
SET rate = discount_policy.rate WHERE days_left = 3 // 30%
ELSE
SET rate = 0 // no discount
END IF
PRINT product_name, inventory_id, days_left, rate / action
UNTIL end of file
Decide whether a request can ship
DO
READ TABLE (shipment_request, inventory, product)
FIND shipment_request WHERE status = 'Pending'
// usable stock = unexpired batches with units left
SET available = SUM(inventory.current_quantity)
WHERE product_id = request.product_id
AND current_quantity > 0
AND expiration_date >= TODAY
IF request.quantity <= 0
UPDATE status = 'Unavailable' // invalid request
ELSEIF available >= request.quantity
UPDATE status = 'Complete'
CALL FEFO_ship(request_id, product_id, request.quantity)
ELSE
UPDATE status = 'Unavailable' // short or expired stock
END IF
UNTIL end of file
FEFO_ship: closest expiry first
// FEFO_ship(request_id, product_id, quantity)
SORT inventory BY expiration_date ASC
WHERE product_id = product_id
AND current_quantity > 0
AND expiration_date >= TODAY
SET remaining = quantity
FOR EACH batch IN sorted inventory
IF remaining <= 0 THEN BREAK
SET qty = MIN(batch.current_quantity, remaining)
INSERT INTO shipment (request_id, batch.inventory_id, qty, NOW)
UPDATE inventory SET current_quantity = current_quantity - qty
WHERE inventory_id = batch.inventory_id
SET remaining = remaining - qty
END FOR
Implementation
We had no access to Kurly’s internal data, so we built the database in MySQL with sample data designed to trigger every case: near-expiry items, both discount tiers, shippable and short-stock requests, a FEFO shipment split across batches, and one disposal for each reason. Six queries checked the rules against it.
- Stock by productTotals across batches for six products, e.g. strawberries 75, tofu 8.
- Near-expiry itemsFive batches within 3 days, soonest first. Tofu expires today.
- Automatic discounts50% at ≤1 day and 30% at ≤3 days. Strawberries expiring tomorrow drop from ₩5,000 to ₩2,500.
- Can it ship?Five requests checked. Milk: 90 requested, 80 in stock, so Unavailable.
- FEFO shipment logStrawberries 40 → 30 from the batch expiring 7 Jun, then 10 from 9 Jun. Salad 60 → 25 + 35.
- Disposal logThree records: expired, damaged packaging, and failed inspection.
SQL queries 6 queries
1 · Stock by productSELECT p.product_name, p.category,
SUM(i.current_quantity) AS total_stock
FROM product p
JOIN inventory i ON p.product_id = i.product_id
GROUP BY p.product_id, p.product_name, p.category;
2 · Near-expiry items (≤ 3 days)
SELECT p.product_name, i.inventory_id, i.expiration_date,
DATEDIFF(i.expiration_date, '2026-06-06') AS days_left,
i.current_quantity
FROM inventory i
JOIN product p ON i.product_id = p.product_id
WHERE DATEDIFF(i.expiration_date, '2026-06-06') <= 3
AND i.current_quantity > 0
ORDER BY i.expiration_date;
3 · Automatic discounts
SELECT p.product_name, i.inventory_id, i.expiration_date,
DATEDIFF(i.expiration_date, '2026-06-06') AS days_left,
CASE
WHEN DATEDIFF(i.expiration_date, '2026-06-06') <= 1 THEN 0.50
WHEN DATEDIFF(i.expiration_date, '2026-06-06') <= 3 THEN 0.30
ELSE 0.00
END AS discount_rate,
p.unit_price AS list_price,
p.unit_price * (1 - CASE
WHEN DATEDIFF(i.expiration_date, '2026-06-06') <= 1 THEN 0.50
WHEN DATEDIFF(i.expiration_date, '2026-06-06') <= 3 THEN 0.30
ELSE 0.00
END) AS sale_price
FROM inventory i
JOIN product p ON i.product_id = p.product_id
WHERE i.current_quantity > 0
ORDER BY i.expiration_date;
4 · Can each request ship?
SELECT sr.request_id, p.product_name, sr.request_quantity,
SUM(i.current_quantity) AS total_stock,
CASE WHEN SUM(i.current_quantity) >= sr.request_quantity
THEN 'Shippable' ELSE 'Short stock' END AS shippable,
sr.request_status
FROM shipmentrequest sr
JOIN product p ON sr.product_id = p.product_id
JOIN inventory i ON sr.product_id = i.product_id
GROUP BY sr.request_id, p.product_name,
sr.request_quantity, sr.request_status;
5 · FEFO shipment log
SELECT s.shipment_id, sr.request_id, p.product_name,
i.inventory_id, i.expiration_date,
s.shipment_quantity, s.shipped_at
FROM shipment s
JOIN shipmentrequest sr ON s.request_id = sr.request_id
JOIN inventory i ON s.inventory_id = i.inventory_id
JOIN product p ON i.product_id = p.product_id
ORDER BY sr.request_id, i.expiration_date;
6 · Disposal log
SELECT d.disposal_id, p.product_name, i.inventory_id,
d.disposal_quantity, d.disposal_reason,
d.disposal_date, d.note
FROM disposal d
JOIN inventory i ON d.inventory_id = i.inventory_id
JOIN product p ON i.product_id = p.product_id
ORDER BY d.disposal_date;
Admin dashboard
The dashboard turns the queries into one screen for the warehouse manager. Four counts sit on top (total stock, near-expiry stock, pending requests, today’s disposals), and each panel below answers one requirement.

- Stock by product (R1): totals per product and category
- Near-expiry & discounts (R2, R6): days left in red or amber, with the discount tier as a badge
- Shipment requests (R4, R5): each request’s status as Complete, Pending, or Unavailable
- Disposal log (R3): quantity, reason, and date
Limitations & Reflection
What I did
- [Your contribution 1, e.g. requirements and decision tables]
- [Your contribution 2, e.g. ER model and normalization]
- [Your contribution 3, e.g. dashboard design]
Limitations
The system covers stock, shipping, and disposal only. It doesn’t forecast demand, place orders, or plan delivery routes, and it was tested only on sample data. Discount thresholds are fixed by hand, though the disposal log could later be used to tune them per product.
Reflection
Designing the rules before the screens made the dashboard simple: every panel maps to a requirement, and every number is a query we had already tested. Decision tables did the same for edge cases. Listing every combination exposed cases that would otherwise hide behind a generic error, like a request for zero units or a product whose stock had all expired.
We designed for a manager we never met. Next, I would watch warehouse staff during an early-morning shift to learn which numbers they actually check first, and test whether the dashboard helps them act on near-expiry stock in time.