Skip to main content

stateset_embedded/
prepayments.rs

1//! Prepayment operations (advance payments to suppliers).
2
3use stateset_core::{
4    ApplyPrepayment, CreatePrepayment, Prepayment, PrepaymentApplication, PrepaymentApplicationId,
5    PrepaymentFilter, PrepaymentId, Result,
6};
7use stateset_db::{Database, DatabaseCapability};
8use std::sync::Arc;
9
10/// Prepayment operations.
11pub struct Prepayments {
12    db: Arc<dyn Database>,
13}
14
15impl std::fmt::Debug for Prepayments {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        f.debug_struct("Prepayments").finish_non_exhaustive()
18    }
19}
20
21impl Prepayments {
22    pub(crate) fn new(db: Arc<dyn Database>) -> Self {
23        Self { db }
24    }
25
26    /// Whether prepayments are supported by the active backend.
27    #[must_use]
28    pub fn is_supported(&self) -> bool {
29        self.db.supports_capability(DatabaseCapability::Prepayments)
30    }
31
32    fn ensure(&self) -> Result<()> {
33        self.db.ensure_capability(DatabaseCapability::Prepayments)
34    }
35
36    /// Create a new prepayment.
37    pub fn create(&self, input: CreatePrepayment) -> Result<Prepayment> {
38        self.ensure()?;
39        self.db.prepayments().create(input)
40    }
41
42    /// Get a prepayment by ID.
43    pub fn get(&self, id: PrepaymentId) -> Result<Option<Prepayment>> {
44        self.ensure()?;
45        self.db.prepayments().get(id)
46    }
47
48    /// List prepayments with optional filtering.
49    pub fn list(&self, filter: PrepaymentFilter) -> Result<Vec<Prepayment>> {
50        self.ensure()?;
51        self.db.prepayments().list(filter)
52    }
53
54    /// Apply a prepayment against a bill or payment obligation.
55    pub fn apply(&self, id: PrepaymentId, input: ApplyPrepayment) -> Result<Prepayment> {
56        self.ensure()?;
57        self.db.prepayments().apply(id, input)
58    }
59
60    /// List applications for a prepayment.
61    pub fn list_applications(&self, id: PrepaymentId) -> Result<Vec<PrepaymentApplication>> {
62        self.ensure()?;
63        self.db.prepayments().list_applications(id)
64    }
65
66    /// Reverse a previously-recorded application.
67    pub fn reverse_application(
68        &self,
69        id: PrepaymentId,
70        application_id: PrepaymentApplicationId,
71    ) -> Result<Prepayment> {
72        self.ensure()?;
73        self.db.prepayments().reverse_application(id, application_id)
74    }
75
76    /// Refund the remaining balance, closing the prepayment.
77    pub fn refund(&self, id: PrepaymentId) -> Result<Prepayment> {
78        self.ensure()?;
79        self.db.prepayments().refund(id)
80    }
81}