1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Warehouse and Location management operations
//!
//! Comprehensive warehouse management system supporting:
//! - Warehouse definitions and configuration
//! - Location hierarchy (zones, aisles, racks, bins)
//! - Location inventory tracking
//! - Inventory movements and transfers
//!
//! # Example
//!
//! ```rust,no_run
//! use stateset_embedded::{Commerce, CreateWarehouse, CreateLocation, WarehouseType, LocationType};
//!
//! let commerce = Commerce::new("./store.db")?;
//!
//! // Create a warehouse
//! let warehouse = commerce.warehouse().create_warehouse(CreateWarehouse {
//! code: "WH-001".into(),
//! name: "Main Distribution Center".into(),
//! warehouse_type: WarehouseType::Distribution,
//! ..Default::default()
//! })?;
//!
//! // Create a location within the warehouse
//! let location = commerce.warehouse().create_location(CreateLocation {
//! warehouse_id: warehouse.id,
//! location_type: LocationType::Pick,
//! zone: Some("A".into()),
//! aisle: Some("01".into()),
//! rack: Some("02".into()),
//! bin: Some("03".into()),
//! ..Default::default()
//! })?;
//!
//! println!("Created location {} in warehouse {}", location.code, warehouse.name);
//! # Ok::<(), stateset_embedded::CommerceError>(())
//! ```
use rust_decimal::Decimal;
use stateset_core::{
AdjustLocationInventory, BatchResult, CreateLocation, CreateWarehouse, CreateZone, Location,
LocationFilter, LocationInventory, LocationInventoryFilter, LocationMovement, MoveInventory,
MovementFilter, Result, UpdateLocation, UpdateWarehouse, UpdateZone, Warehouse,
WarehouseFilter, Zone,
};
use stateset_db::Database;
use std::sync::Arc;
/// Warehouse and Location management interface.
pub struct WarehouseOps {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for WarehouseOps {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WarehouseOps").finish_non_exhaustive()
}
}
impl WarehouseOps {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
// ========================================================================
// Warehouse Operations
// ========================================================================
/// Create a new warehouse.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, CreateWarehouse, WarehouseType, WarehouseAddress};
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let warehouse = commerce.warehouse().create_warehouse(CreateWarehouse {
/// code: "DC-EAST".into(),
/// name: "East Coast Distribution Center".into(),
/// warehouse_type: WarehouseType::Distribution,
/// address: WarehouseAddress {
/// street1: "123 Logistics Way".into(),
/// city: "Newark".into(),
/// state: "NJ".into(),
/// postal_code: "07102".into(),
/// country: "US".into(),
/// ..Default::default()
/// },
/// timezone: Some("America/New_York".into()),
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create_warehouse(&self, input: CreateWarehouse) -> Result<Warehouse> {
self.db.warehouse().create_warehouse(input)
}
/// Get a warehouse by ID.
pub fn get_warehouse(&self, id: i32) -> Result<Option<Warehouse>> {
self.db.warehouse().get_warehouse(id)
}
/// Get a warehouse by code.
pub fn get_warehouse_by_code(&self, code: &str) -> Result<Option<Warehouse>> {
self.db.warehouse().get_warehouse_by_code(code)
}
/// Update a warehouse.
pub fn update_warehouse(&self, id: i32, input: UpdateWarehouse) -> Result<Warehouse> {
self.db.warehouse().update_warehouse(id, input)
}
/// List warehouses with optional filtering.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, WarehouseFilter, WarehouseType};
///
/// let commerce = Commerce::new(":memory:")?;
///
/// // Get all active distribution centers
/// let warehouses = commerce.warehouse().list_warehouses(WarehouseFilter {
/// warehouse_type: Some(WarehouseType::Distribution),
/// is_active: Some(true),
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn list_warehouses(&self, filter: WarehouseFilter) -> Result<Vec<Warehouse>> {
self.db.warehouse().list_warehouses(filter)
}
/// Delete a warehouse. Only warehouses without locations can be deleted.
pub fn delete_warehouse(&self, id: i32) -> Result<()> {
self.db.warehouse().delete_warehouse(id)
}
/// Count warehouses matching the filter.
pub fn count_warehouses(&self, filter: WarehouseFilter) -> Result<u64> {
self.db.warehouse().count_warehouses(filter)
}
// ========================================================================
// Zone Operations
// ========================================================================
/// Create a new zone within a warehouse.
///
/// Zones are logical groupings of locations (e.g., "Bulk Storage", "Pick Area", "Returns").
pub fn create_zone(&self, input: CreateZone) -> Result<Zone> {
self.db.warehouse().create_zone(input)
}
/// Get a zone by ID.
pub fn get_zone(&self, id: i32) -> Result<Option<Zone>> {
self.db.warehouse().get_zone(id)
}
/// Get all zones in a warehouse.
pub fn get_zones(&self, warehouse_id: i32) -> Result<Vec<Zone>> {
self.db.warehouse().get_zones(warehouse_id)
}
/// Update a zone.
pub fn update_zone(&self, id: i32, input: UpdateZone) -> Result<Zone> {
self.db.warehouse().update_zone(id, input)
}
/// Delete a zone.
pub fn delete_zone(&self, id: i32) -> Result<()> {
self.db.warehouse().delete_zone(id)
}
// ========================================================================
// Location Operations
// ========================================================================
/// Create a new location within a warehouse.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, CreateLocation, LocationType};
/// use rust_decimal_macros::dec;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// // Create a pick location with capacity constraints
/// let location = commerce.warehouse().create_location(CreateLocation {
/// warehouse_id: 1,
/// location_type: LocationType::Pick,
/// zone: Some("A".into()),
/// aisle: Some("01".into()),
/// rack: Some("05".into()),
/// level: Some("2".into()),
/// bin: Some("03".into()),
/// max_weight_kg: Some(dec!(100)),
/// max_volume_m3: Some(dec!(0.5)),
/// is_pickable: Some(true),
/// is_receivable: Some(false),
/// ..Default::default()
/// })?;
///
/// println!("Created location: {}", location.code); // e.g., "A-01-05-2-03"
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create_location(&self, input: CreateLocation) -> Result<Location> {
self.db.warehouse().create_location(input)
}
/// Get a location by ID.
pub fn get_location(&self, id: i32) -> Result<Option<Location>> {
self.db.warehouse().get_location(id)
}
/// Get a location by code within a warehouse.
pub fn get_location_by_code(&self, warehouse_id: i32, code: &str) -> Result<Option<Location>> {
self.db.warehouse().get_location_by_code(warehouse_id, code)
}
/// Update a location.
pub fn update_location(&self, id: i32, input: UpdateLocation) -> Result<Location> {
self.db.warehouse().update_location(id, input)
}
/// List locations with optional filtering.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, LocationFilter, LocationType};
///
/// let commerce = Commerce::new(":memory:")?;
///
/// // Get all pickable locations in zone A
/// let locations = commerce.warehouse().list_locations(LocationFilter {
/// warehouse_id: Some(1),
/// zone: Some("A".into()),
/// is_pickable: Some(true),
/// is_active: Some(true),
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn list_locations(&self, filter: LocationFilter) -> Result<Vec<Location>> {
self.db.warehouse().list_locations(filter)
}
/// Delete a location. Only locations without inventory can be deleted.
pub fn delete_location(&self, id: i32) -> Result<()> {
self.db.warehouse().delete_location(id)
}
/// Count locations matching the filter.
pub fn count_locations(&self, filter: LocationFilter) -> Result<u64> {
self.db.warehouse().count_locations(filter)
}
/// Get all active locations for a warehouse.
pub fn get_locations_for_warehouse(&self, warehouse_id: i32) -> Result<Vec<Location>> {
self.db.warehouse().get_locations_for_warehouse(warehouse_id)
}
/// Get pickable locations with available inventory for a SKU.
///
/// Returns locations that are marked as pickable and have available
/// (non-reserved) inventory for the specified SKU.
pub fn get_pickable_locations(&self, warehouse_id: i32, sku: &str) -> Result<Vec<Location>> {
self.db.warehouse().get_pickable_locations(warehouse_id, sku)
}
/// Get receivable locations for a warehouse.
///
/// Returns locations where inventory can be received (e.g., receiving docks, staging areas).
pub fn get_receivable_locations(&self, warehouse_id: i32) -> Result<Vec<Location>> {
self.db.warehouse().get_receivable_locations(warehouse_id)
}
/// Create multiple locations in a batch.
///
/// Returns a `BatchResult` with succeeded and failed operations.
pub fn create_locations_batch(
&self,
inputs: Vec<CreateLocation>,
) -> Result<BatchResult<Location>> {
self.db.warehouse().create_locations_batch(inputs)
}
/// Get multiple locations by ID.
pub fn get_locations_batch(&self, ids: Vec<i32>) -> Result<Vec<Location>> {
self.db.warehouse().get_locations_batch(ids)
}
// ========================================================================
// Location Inventory Operations
// ========================================================================
/// Get all inventory at a specific location.
pub fn get_location_inventory(&self, location_id: i32) -> Result<Vec<LocationInventory>> {
self.db.warehouse().get_location_inventory(location_id)
}
/// Get all locations with inventory for a SKU within a warehouse.
pub fn get_inventory_for_sku(
&self,
warehouse_id: i32,
sku: &str,
) -> Result<Vec<LocationInventory>> {
self.db.warehouse().get_inventory_for_sku(warehouse_id, sku)
}
/// Adjust inventory at a location.
///
/// Use positive quantities to add inventory, negative to remove.
/// Creates a movement record for audit trail.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, AdjustLocationInventory};
/// use rust_decimal_macros::dec;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// // Add inventory from receiving
/// let inventory = commerce.warehouse().adjust_inventory(AdjustLocationInventory {
/// location_id: 1,
/// sku: "PROD-001".into(),
/// lot_id: None,
/// quantity: dec!(100),
/// reason: "Receipt from PO-12345".into(),
/// reference_type: Some("purchase_order".into()),
/// reference_id: None,
/// performed_by: Some("warehouse_user".into()),
/// })?;
///
/// println!("New quantity on hand: {}", inventory.quantity_on_hand);
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn adjust_inventory(&self, input: AdjustLocationInventory) -> Result<LocationInventory> {
self.db.warehouse().adjust_inventory(input)
}
/// Move inventory between locations.
///
/// Validates that sufficient available (non-reserved) quantity exists
/// at the source location. Creates movement records for both locations.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, MoveInventory};
/// use rust_decimal_macros::dec;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// // Move inventory from bulk to pick location
/// let movement = commerce.warehouse().move_inventory(MoveInventory {
/// from_location_id: 1, // Bulk storage
/// to_location_id: 2, // Pick location
/// sku: "PROD-001".into(),
/// lot_id: None,
/// quantity: dec!(50),
/// reason: Some("Replenish pick location".into()),
/// performed_by: Some("forklift_operator".into()),
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn move_inventory(&self, input: MoveInventory) -> Result<LocationMovement> {
self.db.warehouse().move_inventory(input)
}
/// List location inventory with optional filtering.
pub fn list_location_inventory(
&self,
filter: LocationInventoryFilter,
) -> Result<Vec<LocationInventory>> {
self.db.warehouse().list_location_inventory(filter)
}
/// Get total available quantity for a SKU across a warehouse.
///
/// Sums available quantity (`on_hand` - reserved) across all locations.
pub fn get_total_available(&self, warehouse_id: i32, sku: &str) -> Result<Decimal> {
let inventory = self.db.warehouse().get_inventory_for_sku(warehouse_id, sku)?;
Ok(inventory.iter().map(|i| i.quantity_available).sum())
}
/// Get total on-hand quantity for a SKU across a warehouse.
pub fn get_total_on_hand(&self, warehouse_id: i32, sku: &str) -> Result<Decimal> {
let inventory = self.db.warehouse().get_inventory_for_sku(warehouse_id, sku)?;
Ok(inventory.iter().map(|i| i.quantity_on_hand).sum())
}
// ========================================================================
// Movement Operations
// ========================================================================
/// Get inventory movements with optional filtering.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, MovementFilter, MovementType};
/// use chrono::{Utc, Duration};
///
/// let commerce = Commerce::new(":memory:")?;
///
/// // Get all transfers in the last 7 days
/// let movements = commerce.warehouse().get_movements(MovementFilter {
/// warehouse_id: Some(1),
/// movement_type: Some(MovementType::Transfer),
/// from_date: Some(Utc::now() - Duration::days(7)),
/// limit: Some(100),
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn get_movements(&self, filter: MovementFilter) -> Result<Vec<LocationMovement>> {
self.db.warehouse().get_movements(filter)
}
/// Count movements matching the filter.
pub fn count_movements(&self, filter: MovementFilter) -> Result<u64> {
self.db.warehouse().count_movements(filter)
}
}