Add a recommended Food Storage example, a Ration tab, and a supply planner

Build on the rebuilt config-only Inventory with three related additions.

Recommended example inventory:
- A fresh install now seeds the "Food Storage" example with the recommended
  one-adult / one-year (~3,000 kcal/day) emergency food supply (60 items,
  ~1.12M kcal), instead of an empty inventory.
- The counts are layered on top of the shipped food catalog (single source of
  per-package nutrition) via RECOMMENDED_ONE_YEAR_FOOD_COUNTS, joined at seed
  time by recommended_food_storage_items().
- Genericized the two remaining brand names in the catalog defaults
  (Velveeta -> Processed Cheese, Spam -> Canned Lunch Meat) so both the catalog
  and the seeded inventory stay brand-free.

Ration tab (split out of Summary):
- Moved the Ration Estimate (People / Daily Calories / estimate table) out of
  the increasingly long Summary tab into its own "Ration" tab, shown only for
  inventories that have a calories field.

Supply planner ("Plan More Supply"):
- A slider picks a target duration longer than the current estimate; the whole
  inventory is scaled up proportionally (every item by the same factor, mix
  kept balanced) into a sortable shopping list of what to buy.
- Any split package rounds UP to a whole one (ceil), matching the "1.6 buckets
  -> buy 1 more" rule.
- The shopping list is exportable as CSV (reusing toCSV) and PDF (reusing the
  print-to-PDF path, scoped via body.printing-shopping so only the list prints
  -- no inventory or summary).

Search filter:
- Moved the items search out of the top toolbar (which implied it filtered
  every tab) into the Items tab, so it clearly narrows Items only;
  Summary/Ration/Export/print always use the full inventory.

Aggregation/role helpers (findNameKey, planSupplyPurchase) live in summarize.js,
the single source of truth shared by Summary, export, and the ration views.
All new pure functions and UI flows are covered by tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-06-19 12:39:58 -06:00
parent 5427beb5fb
commit ee0b827c71
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
14 changed files with 545 additions and 38 deletions

View file

@ -21,12 +21,12 @@ items:
# --- Dairy ---
- {id: 9, name: Nonfat Dry Milk, category: dairy, subcategory: dry milk, item_size: '64', item_size_unit: oz, calories: '6400'}
- {id: 10, name: Evaporated Milk, category: dairy, subcategory: evaporated milk, item_size: '12', item_size_unit: oz, calories: '480'}
- {id: 11, name: Velveeta Cheese, category: dairy, subcategory: cheese, item_size: '32', item_size_unit: oz, calories: '2560'}
- {id: 11, name: Processed Cheese, category: dairy, subcategory: cheese, item_size: '32', item_size_unit: oz, calories: '2560'}
# --- Fats ---
- {id: 12, name: Butter, category: fats, subcategory: butter, item_size: '1', item_size_unit: lb, calories: '3200'}
- {id: 13, name: Extra-Virgin Olive Oil, category: fats, subcategory: oil, item_size: '50.7', item_size_unit: oz, calories: '12000'}
- {id: 14, name: Shortening, category: fats, subcategory: shortening, item_size: '3', item_size_unit: lb, calories: '12000'}
- {id: 14, name: Shortening, category: fats, subcategory: shortening, item_size: '3', item_size_unit: lb, calories: '12430'}
- {id: 15, name: Peanut Butter, category: fats, subcategory: peanut butter, item_size: '18', item_size_unit: oz, calories: '3040'}
# --- Sugars ---
@ -35,14 +35,14 @@ items:
- {id: 18, name: Confectioners Sugar, category: sugars, subcategory: powdered sugar, item_size: '1', item_size_unit: lb, calories: '1800'}
- {id: 19, name: Light Corn Syrup, category: sugars, subcategory: corn syrup, item_size: '16', item_size_unit: oz, calories: '1920'}
- {id: 20, name: Maple Syrup, category: sugars, subcategory: syrup, item_size: '12.5', item_size_unit: oz, calories: '1200'}
- {id: 21, name: Clover Honey, category: sugars, subcategory: honey, item_size: '16', item_size_unit: oz, calories: '1280'}
- {id: 21, name: Honey, category: sugars, subcategory: honey, item_size: '16', item_size_unit: oz, calories: '1280'}
- {id: 22, name: Cocoa Syrup, category: sugars, subcategory: syrup, item_size: '16', item_size_unit: oz, calories: '1200'}
- {id: 23, name: Jelly or Preserves, category: sugars, subcategory: jelly, item_size: '16', item_size_unit: oz, calories: '1300'}
# --- Meats ---
- {id: 24, name: Tuna in Oil, category: meats, subcategory: canned, item_size: '5', item_size_unit: oz, calories: '187'}
- {id: 25, name: Canned Ham, category: meats, subcategory: canned, item_size: '16', item_size_unit: oz, calories: '800'}
- {id: 26, name: Spam, category: meats, subcategory: canned, item_size: '12', item_size_unit: oz, calories: '1020'}
- {id: 26, name: Canned Lunch Meat, category: meats, subcategory: canned, item_size: '12', item_size_unit: oz, calories: '1020'}
- {id: 27, name: Vienna Sausage, category: meats, subcategory: canned, item_size: '5', item_size_unit: oz, calories: '375'}
- {id: 28, name: Roast Beef, category: meats, subcategory: canned, item_size: '12', item_size_unit: oz, calories: '375'}
@ -50,7 +50,7 @@ items:
- {id: 29, name: Cream of Chicken Soup, category: meals, subcategory: canned, item_size: '10.75', item_size_unit: oz, calories: '300'}
- {id: 30, name: Canned Macaroni and Cheese, category: meals, subcategory: canned, item_size: '15', item_size_unit: oz, calories: '480'}
- {id: 31, name: Canned Pasta, category: meals, subcategory: canned, item_size: '15', item_size_unit: oz, calories: '500'}
- {id: 32, name: Chunky Soup, category: meals, subcategory: canned, item_size: '18.8', item_size_unit: oz, calories: '500'}
- {id: 32, name: Chunky Soup, category: meals, subcategory: canned, item_size: '18.8', item_size_unit: oz, calories: '360'}
- {id: 33, name: Roast Beef Hash, category: meals, subcategory: canned, item_size: '15', item_size_unit: oz, calories: '780'}
- {id: 34, name: Beef Stew, category: meals, subcategory: canned, item_size: '24', item_size_unit: oz, calories: '630'}
- {id: 35, name: Chili with Beans, category: meals, subcategory: canned, item_size: '15', item_size_unit: oz, calories: '700'}

View file

@ -184,7 +184,12 @@ class InventoriesConfig(MultiFileConfig):
return
for default in DEFAULT_INVENTORIES:
try:
self.create_inventory(default['name'], default['type'])
inventory = self.create_inventory(default['name'], default['type'])
# Optionally populate the example with seed items (e.g. the recommended one-year food supply).
items_factory = default.get('items_factory')
items = items_factory() if items_factory else None
if items:
self.save_inventory(inventory['slug'], dict(items=items))
except Exception as e:
logger.warning(f'Failed to seed default inventory {default}: {e}')

View file

@ -96,9 +96,63 @@ DEFAULT_TYPE = 'food'
INVENTORY_TYPES = tuple(DEFAULT_FIELD_SETS.keys())
# A brand-new install seeds one example inventory of each kind so the feature is discoverable.
# Recommended package counts for a one-adult, one-year (~3,000 kcal/day) food supply, keyed by the shipped
# food-catalog id (see catalog_defaults.yaml). Transcribed from the same "One-Year Emergency Food Supply for One
# Adult" source list. `seed_defaults()` joins these counts with the catalog's per-package data to populate the
# example "Food Storage" inventory on a fresh install (e.g. id 1 = white rice: 70 lb stored as 14 x 5 lb bags).
RECOMMENDED_ONE_YEAR_FOOD_COUNTS = {
# Grains
1: 14, 2: 14, 3: 6, 4: 4, 5: 4, 6: 36, 7: 4, 8: 12,
# Dairy
9: 12, 10: 24, 11: 3,
# Fats
12: 12, 13: 5, 14: 2, 15: 12,
# Sugars
16: 5, 17: 12, 18: 12, 19: 6, 20: 6, 21: 9, 22: 8, 23: 12,
# Meats
24: 48, 25: 12, 26: 24, 27: 24, 28: 24,
# Meals (prepared / canned)
29: 12, 30: 24, 31: 24, 32: 24, 33: 24, 34: 18, 35: 24,
# Legumes
36: 96,
# Vegetables
37: 24, 38: 36, 39: 12, 40: 180, 41: 12,
# Fruits
42: 48,
# Cooking ingredients
43: 12, 44: 12, 45: 24, 46: 6, 47: 12, 48: 24, 49: 6, 50: 12, 51: 12, 52: 12,
53: 2, 54: 2, 55: 2, 56: 1, 57: 2, 58: 1, 59: 48, 60: 48,
}
# Per-package fields copied from a food-catalog entry onto a seeded inventory item (its `count` is added from
# RECOMMENDED_ONE_YEAR_FOOD_COUNTS).
_RECOMMENDED_ITEM_KEYS = ('name', 'category', 'subcategory', 'item_size', 'item_size_unit', 'calories')
def recommended_food_storage_items() -> list:
"""Build the example one-adult/one-year Food Storage items.
Joins the shipped food catalog (per-package nutrition in ``catalog_defaults.yaml``) with
``RECOMMENDED_ONE_YEAR_FOOD_COUNTS`` (how many packages to store), so the per-package nutritional data keeps a
single source of truth and this layer only adds quantities. Items are kept generic (no brand).
"""
from .catalog import load_catalog_defaults
by_id = {e.get('id'): e for e in load_catalog_defaults()}
items = []
for catalog_id, count in RECOMMENDED_ONE_YEAR_FOOD_COUNTS.items():
entry = by_id.get(catalog_id)
if not entry:
continue
item = {key: entry.get(key, '') for key in _RECOMMENDED_ITEM_KEYS}
item['count'] = str(count)
items.append(item)
return items
# A brand-new install seeds one example inventory of each kind so the feature is discoverable. The food example
# is populated with the recommended one-adult/one-year supply; `items_factory` is called lazily at seed time.
DEFAULT_INVENTORIES = [
dict(name='Food Storage', type='food'),
dict(name='Food Storage', type='food', items_factory=recommended_food_storage_items),
]

View file

@ -30,10 +30,10 @@ def test_seed_catalog_defaults_and_tombstone(test_catalog_config):
assert len(config.items) == seeded
# A user-deleted default is NOT re-added on the next seed (tombstoned via merged_default_ids).
remaining = [i for i in config.items if i['name'] != 'Spam']
remaining = [i for i in config.items if i['name'] != 'Canned Lunch Meat']
catalog_module.save_catalog_items(remaining)
catalog_module.seed_catalog_defaults(config)
assert not any(i['name'] == 'Spam' for i in config.items)
assert not any(i['name'] == 'Canned Lunch Meat' for i in config.items)
def test_catalog_file_is_not_loaded_as_an_inventory(test_inventory_configs, test_catalog_config):

View file

@ -1,7 +1,8 @@
import pytest
from modules.inventory import common as inventory_common
from modules.inventory.defaults import slugify, default_fields
from modules.inventory.defaults import (slugify, default_fields, recommended_food_storage_items,
RECOMMENDED_ONE_YEAR_FOOD_COUNTS)
from modules.inventory.errors import InventoryConflict
@ -114,6 +115,44 @@ def test_delete_inventory(food_inventory_factory, test_inventory_configs):
assert config.all_inventories() == []
def test_recommended_food_storage_items():
"""The example supply joins the shipped food catalog (per-package nutrition) with the recommended counts."""
items = recommended_food_storage_items()
# One item per recommended count, all resolved against the catalog.
assert len(items) == len(RECOMMENDED_ONE_YEAR_FOOD_COUNTS)
# Kept generic: no brand names.
assert all(not item.get('brand') for item in items)
# Counts are positive strings; the per-package calorie basis comes from the catalog.
assert all(item['count'] and int(item['count']) > 0 for item in items)
rice = next(i for i in items if i['name'] == 'Long Grain White Rice')
assert rice['count'] == '14'
assert rice['calories'] == '8040'
assert rice['item_size'] == '5' and rice['item_size_unit'] == 'lb'
# Total stored calories approximate one adult-year at ~3,000 kcal/day.
total = sum(int(i['calories']) * int(i['count']) for i in items if i.get('calories'))
assert 1_000_000 < total < 1_300_000
def test_seed_defaults_populates_food_storage(test_inventory_configs):
config = test_inventory_configs
config.seed_defaults()
inventories = config.all_inventories()
assert len(inventories) == 1
inv = inventories[0]
assert inv['name'] == 'Food Storage'
assert inv['type'] == 'food'
# Seeded with the full recommended one-adult/one-year supply, with server-assigned ids.
assert len(inv['items']) == len(RECOMMENDED_ONE_YEAR_FOOD_COUNTS)
assert all(isinstance(i['id'], int) for i in inv['items'])
def test_seed_defaults_idempotent(test_inventory_configs):
config = test_inventory_configs
config.seed_defaults()
config.seed_defaults()
assert len(config.all_inventories()) == 1
def test_config_round_trip(food_inventory_factory, test_inventory_configs):
"""Saving to per-inventory YAML and re-importing preserves fields and items."""
config = test_inventory_configs