use rust_decimal::Decimal;
use stateset_core::{
CreateTransferOrder, Result, TransferOrder, TransferOrderFilter, TransferOrderId,
TransferOrderItemId,
};
use stateset_db::{Database, DatabaseCapability};
use std::sync::Arc;
pub struct TransferOrders {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for TransferOrders {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TransferOrders").finish_non_exhaustive()
}
}
impl TransferOrders {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
#[must_use]
pub fn is_supported(&self) -> bool {
self.db.supports_capability(DatabaseCapability::TransferOrders)
}
fn ensure(&self) -> Result<()> {
self.db.ensure_capability(DatabaseCapability::TransferOrders)
}
pub fn create(&self, input: CreateTransferOrder) -> Result<TransferOrder> {
self.ensure()?;
self.db.transfer_orders().create(input)
}
pub fn get(&self, id: TransferOrderId) -> Result<Option<TransferOrder>> {
self.ensure()?;
self.db.transfer_orders().get(id)
}
pub fn list(&self, filter: TransferOrderFilter) -> Result<Vec<TransferOrder>> {
self.ensure()?;
self.db.transfer_orders().list(filter)
}
pub fn ship(&self, id: TransferOrderId) -> Result<TransferOrder> {
self.ensure()?;
self.db.transfer_orders().ship(id)
}
pub fn receive_line(
&self,
id: TransferOrderId,
item_id: TransferOrderItemId,
quantity: Decimal,
) -> Result<TransferOrder> {
self.ensure()?;
self.db.transfer_orders().receive_line(id, item_id, quantity)
}
pub fn cancel(&self, id: TransferOrderId) -> Result<TransferOrder> {
self.ensure()?;
self.db.transfer_orders().cancel(id)
}
}