|
| 1 | +# Inventory Adjustment System with Item Master |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +We've implemented a comprehensive inventory adjustment system that integrates with the item master and handles various order types. The system automatically adjusts inventory levels based on: |
| 6 | + |
| 7 | +1. **Sales Orders** - Allocates, ships, cancels, and returns |
| 8 | +2. **Purchase Orders** - Receives inventory from suppliers |
| 9 | +3. **Manual Adjustments** - Cycle counts, damages, etc. |
| 10 | + |
| 11 | +## Key Components |
| 12 | + |
| 13 | +### 1. Item Master (`src/entities/item_master.rs`) |
| 14 | +The central repository for all inventory items with relationships to: |
| 15 | +- Inventory balances |
| 16 | +- Sales order lines |
| 17 | +- Purchase order lines |
| 18 | +- BOM (Bill of Materials) |
| 19 | +- Manufacturing work orders |
| 20 | + |
| 21 | +### 2. Inventory Adjustment Service (`src/services/inventory_adjustment_service.rs`) |
| 22 | +Core service that handles all inventory movements: |
| 23 | + |
| 24 | +```rust |
| 25 | +pub struct InventoryAdjustmentService { |
| 26 | + db_pool: Arc<DbPool>, |
| 27 | + event_sender: Arc<EventSender>, |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Key methods: |
| 32 | +- `adjust_for_sales_order()` - Handles SO allocations, shipments, cancellations, returns |
| 33 | +- `adjust_for_purchase_order_receipt()` - Handles PO receipts |
| 34 | + |
| 35 | +### 3. Event System |
| 36 | +Events are triggered for all inventory adjustments: |
| 37 | +- `InventoryAdjustedForOrder` - When SO affects inventory |
| 38 | +- `InventoryReceivedFromPO` - When PO receipt occurs |
| 39 | +- Full transaction history maintained |
| 40 | + |
| 41 | +## Inventory Adjustment Flow |
| 42 | + |
| 43 | +### Sales Order Processing |
| 44 | +``` |
| 45 | +1. Order Created → Allocate Inventory |
| 46 | + - Reduces available quantity |
| 47 | + - Increases allocated quantity |
| 48 | + - On-hand remains same |
| 49 | +
|
| 50 | +2. Order Shipped → Ship Inventory |
| 51 | + - Reduces on-hand quantity |
| 52 | + - Reduces allocated quantity |
| 53 | + - Available remains same |
| 54 | +
|
| 55 | +3. Order Cancelled → Deallocate Inventory |
| 56 | + - Increases available quantity |
| 57 | + - Reduces allocated quantity |
| 58 | + - On-hand remains same |
| 59 | +
|
| 60 | +4. Order Returned → Return Inventory |
| 61 | + - Increases on-hand quantity |
| 62 | + - Increases available quantity |
| 63 | + - Allocated remains same |
| 64 | +``` |
| 65 | + |
| 66 | +### Purchase Order Processing |
| 67 | +``` |
| 68 | +1. PO Receipt → Receive Inventory |
| 69 | + - Increases on-hand quantity |
| 70 | + - Increases available quantity |
| 71 | + - Creates/updates inventory balance |
| 72 | +``` |
| 73 | + |
| 74 | +## Database Schema |
| 75 | + |
| 76 | +### Key Tables |
| 77 | +- `item_master` - Item definitions |
| 78 | +- `inventory_balances` - Current inventory levels by location |
| 79 | +- `inventory_transactions` - Full audit trail |
| 80 | +- `sales_order_lines` - Sales order details |
| 81 | +- `purchase_order_lines` - Purchase order details |
| 82 | + |
| 83 | +### Inventory Balance Fields |
| 84 | +```sql |
| 85 | +quantity_on_hand -- Physical inventory |
| 86 | +quantity_allocated -- Reserved for orders |
| 87 | +quantity_available -- Available to promise (on_hand - allocated) |
| 88 | +``` |
| 89 | + |
| 90 | +## Transaction Types |
| 91 | +- `Receive` - From purchase orders |
| 92 | +- `Ship` - To customers |
| 93 | +- `Return` - From customers |
| 94 | +- `Adjust` - Manual adjustments |
| 95 | +- `Allocate` - Reserve for orders |
| 96 | +- `Deallocate` - Release reservations |
| 97 | +- `Transfer` - Between locations |
| 98 | + |
| 99 | +## Testing |
| 100 | + |
| 101 | +### Test Binary: `test_inventory_adjustments` |
| 102 | +Located at: `src/bin/test_inventory_adjustments.rs` |
| 103 | + |
| 104 | +Tests the full flow: |
| 105 | +1. Creates test items in item master |
| 106 | +2. Sets up warehouse locations |
| 107 | +3. Initializes inventory balances |
| 108 | +4. Tests sales order allocation/shipment |
| 109 | +5. Tests purchase order receipt |
| 110 | +6. Tests order cancellation/returns |
| 111 | +7. Displays transaction history |
| 112 | + |
| 113 | +### Running the Test |
| 114 | +```bash |
| 115 | +cargo build --bin test_inventory_adjustments |
| 116 | +./target/debug/test_inventory_adjustments |
| 117 | +``` |
| 118 | + |
| 119 | +## Example Usage |
| 120 | + |
| 121 | +### Adjusting for Sales Order |
| 122 | +```rust |
| 123 | +let service = InventoryAdjustmentService::new(db_pool, event_sender); |
| 124 | + |
| 125 | +// Allocate inventory when order is created |
| 126 | +let results = service |
| 127 | + .adjust_for_sales_order(order_id, SalesOrderAdjustmentType::Allocate) |
| 128 | + .await?; |
| 129 | + |
| 130 | +// Ship inventory when order ships |
| 131 | +let results = service |
| 132 | + .adjust_for_sales_order(order_id, SalesOrderAdjustmentType::Ship) |
| 133 | + .await?; |
| 134 | +``` |
| 135 | + |
| 136 | +### Receiving from Purchase Order |
| 137 | +```rust |
| 138 | +let receipt_lines = vec![ |
| 139 | + PurchaseOrderReceiptLine { |
| 140 | + po_line_id: 123, |
| 141 | + quantity_received: Decimal::from(50), |
| 142 | + location_id: 1, |
| 143 | + }, |
| 144 | +]; |
| 145 | + |
| 146 | +let results = service |
| 147 | + .adjust_for_purchase_order_receipt(po_id, receipt_lines) |
| 148 | + .await?; |
| 149 | +``` |
| 150 | + |
| 151 | +## Benefits |
| 152 | + |
| 153 | +1. **Real-time Inventory Tracking** - Always know current stock levels |
| 154 | +2. **Multi-location Support** - Track inventory across warehouses |
| 155 | +3. **Full Audit Trail** - Every transaction is logged |
| 156 | +4. **Event-Driven** - Integrates with other systems via events |
| 157 | +5. **Atomic Operations** - All adjustments in transactions |
| 158 | +6. **Type Safety** - Strongly typed with Rust |
| 159 | + |
| 160 | +## Future Enhancements |
| 161 | + |
| 162 | +1. **Lot/Serial Tracking** - Track specific batches |
| 163 | +2. **Expiration Management** - Handle perishable goods |
| 164 | +3. **Min/Max Alerts** - Automatic reorder points |
| 165 | +4. **Cycle Count Integration** - Scheduled counts |
| 166 | +5. **ABC Analysis** - Inventory classification |
| 167 | +6. **Forecasting** - Demand prediction |
0 commit comments