This file is the working execution plan for Phase 4: Inventory Architecture & Valuation.
It must be used together with:
Constitution_AMW_DJ_ERP.md- the current repository state
- Ahmad's latest instructions for the phase
- Phase Number:
Phase 4 - Phase Name:
Inventory Architecture & Valuation - Branch Name:
phase-4(merged tomaster) - Status: ✅ COMPLETE
- Version Tag:
v4.0-phase4-complete - Primary Goal:
Implement the product catalog, stock ledger, and Weighted Average Cost (WAC) engine. - Depends On:
Phase 3 - Manager Approval Required:
Yes - Completion Date: 2026-03-31
CategoryandProductmodels withSoftDeleteMixin(Constitution 8.4)StockAdjustmentmodel (Document-based adjustments with reason/actor)StockTransactionledger model with explicitchange_typeStockAdjustmentoperations (Add/Reduce/Transfer)- Weighted Average Cost (WAC) calculation logic with high precision
- Atomic stock updates with
select_for_update() - Unit of Measure (UoM) support for products
- SKU validation logic (Example:
WM-CR-159for "Washing Machine Crazy 159") - Integrated Audit Logging for non-quantity changes (Constitution 8.7)
- Sales integrations - Deferred to Phase 5
- Purchase integrations - Deferred to Phase 6
- Barcode generation - Optional/Deferred
- Functional product and category hierarchy
- Automated WAC recalculation on incoming stock
- Atomic and concurrency-safe stock updates
- Full audit history of stock movements
- Inventory logic lives in
inventory/operations/(Section 6.1) - Stock valuation uses Weighted Average Cost (Section 6.5)
- State changes must be atomic and use locking (Section 6.6)
- Business state changes must produce audit logs (Section 6.7)
Product.current_stockmust never be edited directly from a view. All changes must flow through theoperations/layer to ensure WAC and ledger consistency.
inventory(new app)core(forSoftDeleteMixinand base model support)iam(for policy enforcement)audit(for movement logs)
core/models.py(ImplementSoftDeleteMixinhere)inventory/models.py(Category, Product, StockAdjustment, StockTransaction)inventory/operations/stock.py(Business logic)inventory/logic/valuation.py(WAC calculations)
Product:sku(validated),name,category,uom(pcs, kg, etc.),current_stock,wac_price(19,4 precision),is_deleted(Soft Delete)StockAdjustment:product,type(ADD/REDUCE),quantity,reason,actor(Employee),timestamp(Executes immediately in Phase 4)StockTransaction:product,change_type(INTAKE, ADJUST_ADD, ADJUST_REDUCE, SALE, PURCHASE, TRANSFER, RETURN),quantity,unit_cost(19,4 precision),balance_after,location_note(simple text),timestamp,actor
adjust_stock(): Manual correction or initial intake (CreatesStockAdjustment)recalculate_wac():- ✅ Triggered on:
PURCHASE,INTAKE,ADJUST_ADD(cost-increasing) - ❌ NOT Triggered on:
SALE,ADJUST_REDUCE,TRANSFER(quantity-only changes)
- ✅ Triggered on:
SoftDelete:Product.delete()must use the mixin to setis_deletedrather than purging.
We will start with the product and category models. Then, we will build the StockTransaction ledger to ensure every movement is recorded. The core of this phase is the operations/stock.py module, which will handle all mutations using transaction.atomic and select_for_update. Finally, we will implement and unit test the WAC engine to ensure valuation accuracy.
- Product and Category models (with
SoftDeleteMixinanduom) - StockTransaction ledger model with mandatory
change_typecodes - SKU validation logic and non-quantity
AuditLogintegration - Stock adjustment operations (Add/Reduce) with atomic protection
- WAC valuation logic implementation (19,4 precision)
- Concurrency and stress testing for stock updates
- Goal:
Define the core products and categories with soft delete and SKU validation. - Owner:
Gem (Consultant AI) - Status:
Planned
- Task 4.1:
Implement Category and Product models- Output:
Catalog models with SKU validation (e.g. WM-CR-159), UoM, and SoftDeleteMixin - Verification:
Run unit tests for product creation and soft delete
- Output:
- Goal:
Implement atomic stock movements and the valuation engine. - Owner:
Gem (Consultant AI) - Status:
Planned
-
Task 4.2:
Implement StockTransaction model- Output:
Ledger model for tracking every unit movement - Verification:
Verify model records balance_after correctly
- Output:
-
Task 4.3:
Implement stock adjustment operation with WAC logic- Output:
atomic adjust_stock() method in inventory/operations/ - Verification:
Validate WAC recalculation formula through multiple test cases
- Output:
(Adhering to standard rules from template...)
WAC accuracy: (New Qty * New Cost + Old Qty * Old Cost) / (Total Qty)Concurrency check: Multiple simultaneous stock updates on the same SKUPolicy enforcement: Ensure only authorized employees can adjust stock
Confirm stock transactions appear in the ledger after each operation
Race conditions during high-volume stock updatesFloating point precision errors in WAC calculation
Mandatory use of select_for_update() on Product rowsUse DecimalField for all currency and cost-related fields
Should we support multiple warehouses/locations in Phase 4? (Deferred for simplicity)
- Product catalog architecture exists with Soft Delete and UoM
- SKU validation is implemented in the operation layer (Example:
WM-CR-159) - WAC recalculation (19,4 precision) is implemented and tested
- Stock updates are atomic and concurrency-safe
- Inventory history is visible in the ledger with explicit types
- Non-quantity product changes trigger standard AuditLogs
- IAM rules protect inventory operations
- StockAdjustment workflow with REJECTED status and required comment
- StockTransaction immutability enforced in admin (no edit/delete)
- 26 comprehensive tests passing (Category: 6, Product: 3, WAC: 6, StockTransaction: 2, StockOperations: 9)
2026-03-28-Planned-Initialized Phase 4 plan for inventory and WAC valuation.2026-03-31-Implementation-Completed inventory app with models, operations, WAC engine, and tests.2026-03-31-Review-Addressed Gem's review: added REJECTED status, verified immutability, confirmed UoM/SoftDelete.2026-03-31- ✅ COMPLETE -Phase 4 merged to master (v4.0-phase4-complete). 104 tests passing, 26 inventory-specific.
Phase 4 successfully delivered a production-ready inventory management system with:
- ✅ Product catalog with hierarchical categorization (Category, Product)
- ✅ Immutable stock transaction ledger (StockTransaction)
- ✅ Weighted Average Cost (WAC) valuation engine with 19,4 precision
- ✅ Atomic stock operations with concurrency safety (
select_for_update()) - ✅ Stock adjustment workflow with approval/rejection (StockAdjustment + rejection_comment)
- ✅ Comprehensive audit logging for all stock movements
- ✅ Full Django admin integration (all models registered)
- ✅ 26 unit and integration tests (all passing)
- ✅ Seed data with 5 products and initial stock levels ($44,625 total value)
All implementations follow Constitution Sections 6.1-6.7 (Operations-First, Atomic Safety, Audit Trail).
Next Phase: Phase 5 (Sales & CRM Workflows) - Customer management and atomic sales order workflows.
Version: v4.0-phase4-complete
Merged to Master: 2026-03-31
Total Commits: 9
Lines Added: +2,391 | Lines Removed: -35
(TBD)