Skip to main content

stateset_embedded/
inbound_shipments.rs

1//! Inbound shipment operations (advance ship notices).
2
3use rust_decimal::Decimal;
4use stateset_core::{
5    CreateInboundShipment, InboundShipment, InboundShipmentFilter, InboundShipmentId,
6    InboundShipmentItemId, Result,
7};
8use stateset_db::{Database, DatabaseCapability};
9use std::sync::Arc;
10
11/// Inbound shipment operations.
12pub struct InboundShipments {
13    db: Arc<dyn Database>,
14}
15
16impl std::fmt::Debug for InboundShipments {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        f.debug_struct("InboundShipments").finish_non_exhaustive()
19    }
20}
21
22impl InboundShipments {
23    pub(crate) fn new(db: Arc<dyn Database>) -> Self {
24        Self { db }
25    }
26
27    /// Whether inbound shipments are supported by the active backend.
28    #[must_use]
29    pub fn is_supported(&self) -> bool {
30        self.db.supports_capability(DatabaseCapability::InboundShipments)
31    }
32
33    fn ensure(&self) -> Result<()> {
34        self.db.ensure_capability(DatabaseCapability::InboundShipments)
35    }
36
37    /// Create a new inbound shipment.
38    pub fn create(&self, input: CreateInboundShipment) -> Result<InboundShipment> {
39        self.ensure()?;
40        self.db.inbound_shipments().create(input)
41    }
42
43    /// Get an inbound shipment by ID.
44    pub fn get(&self, id: InboundShipmentId) -> Result<Option<InboundShipment>> {
45        self.ensure()?;
46        self.db.inbound_shipments().get(id)
47    }
48
49    /// List inbound shipments with optional filtering.
50    pub fn list(&self, filter: InboundShipmentFilter) -> Result<Vec<InboundShipment>> {
51        self.ensure()?;
52        self.db.inbound_shipments().list(filter)
53    }
54
55    /// Mark a shipment as in transit.
56    pub fn mark_in_transit(&self, id: InboundShipmentId) -> Result<InboundShipment> {
57        self.ensure()?;
58        self.db.inbound_shipments().mark_in_transit(id)
59    }
60
61    /// Mark a shipment as arrived.
62    pub fn mark_arrived(&self, id: InboundShipmentId) -> Result<InboundShipment> {
63        self.ensure()?;
64        self.db.inbound_shipments().mark_arrived(id)
65    }
66
67    /// Receive a quantity against a single line.
68    pub fn receive_line(
69        &self,
70        id: InboundShipmentId,
71        item_id: InboundShipmentItemId,
72        quantity: Decimal,
73    ) -> Result<InboundShipment> {
74        self.ensure()?;
75        self.db.inbound_shipments().receive_line(id, item_id, quantity)
76    }
77
78    /// Cancel an inbound shipment.
79    pub fn cancel(&self, id: InboundShipmentId) -> Result<InboundShipment> {
80        self.ensure()?;
81        self.db.inbound_shipments().cancel(id)
82    }
83}