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
//! Accounts Receivable operations
//!
//! Comprehensive accounts receivable management supporting:
//! - AR aging reports and analysis
//! - Collection activity tracking
//! - Credit memos and applications
//! - Write-offs
//! - Payment applications
//! - Customer statements
//!
//! # Example
//!
//! ```rust,ignore
//! use stateset_embedded::Commerce;
//!
//! let commerce = Commerce::new("./store.db")?;
//!
//! // Get AR aging summary
//! let aging = commerce.accounts_receivable().get_aging_summary()?;
//! println!("Total outstanding: ${}", aging.total);
//! println!("Current: ${}", aging.current);
//! println!("30+ days: ${}", aging.days_1_30);
//! # Ok::<(), stateset_embedded::CommerceError>(())
//! ```
use rust_decimal::Decimal;
use stateset_core::{
ApplyCreditMemo, ApplyPaymentToInvoices, ArAgingFilter, ArAgingSummary, ArPaymentApplication,
CollectionActivity, CollectionActivityFilter, CollectionStatus, CreateCollectionActivity,
CreateCreditMemo, CreateWriteOff, CreditMemo, CreditMemoFilter, CustomerArAging,
CustomerArSummary, CustomerStatement, DunningLetterType, GenerateStatementRequest, Invoice,
Result, WriteOff, WriteOffFilter,
};
use stateset_db::Database;
use std::sync::Arc;
use uuid::Uuid;
/// Accounts Receivable interface.
pub struct AccountsReceivable {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for AccountsReceivable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AccountsReceivable").finish_non_exhaustive()
}
}
impl AccountsReceivable {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
// ========================================================================
// Aging Reports
// ========================================================================
/// Get overall AR aging summary.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::Commerce;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let aging = commerce.accounts_receivable().get_aging_summary()?;
/// println!("Current: ${}", aging.current);
/// println!("1-30 days: ${}", aging.days_1_30);
/// println!("31-60 days: ${}", aging.days_31_60);
/// println!("61-90 days: ${}", aging.days_61_90);
/// println!("90+ days: ${}", aging.days_over_90);
/// println!("Total: ${}", aging.total);
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn get_aging_summary(&self) -> Result<ArAgingSummary> {
self.db.accounts_receivable().get_aging_summary()
}
/// Get AR aging for a specific customer.
pub fn get_customer_aging(&self, customer_id: Uuid) -> Result<Option<CustomerArAging>> {
self.db.accounts_receivable().get_customer_aging(customer_id)
}
/// Get detailed AR aging report with filtering.
pub fn get_aging_report(&self, filter: ArAgingFilter) -> Result<Vec<CustomerArAging>> {
self.db.accounts_receivable().get_aging_report(filter)
}
// ========================================================================
// Collection Activities
// ========================================================================
/// Log a collection activity (call, email, dunning letter, etc.).
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, CreateCollectionActivity, CollectionActivityType};
/// use uuid::Uuid;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let activity = commerce.accounts_receivable().log_collection_activity(
/// CreateCollectionActivity {
/// invoice_id: Uuid::new_v4(),
/// activity_type: CollectionActivityType::PhoneCall,
/// notes: Some("Spoke with customer, promised to pay by Friday".into()),
/// contact_method: Some("Phone".into()),
/// contact_result: Some("Promise to pay".into()),
/// performed_by: Some("John Collector".into()),
/// ..Default::default()
/// }
/// )?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn log_collection_activity(
&self,
input: CreateCollectionActivity,
) -> Result<CollectionActivity> {
self.db.accounts_receivable().log_collection_activity(input)
}
/// List collection activities with filtering.
pub fn list_collection_activities(
&self,
filter: CollectionActivityFilter,
) -> Result<Vec<CollectionActivity>> {
self.db.accounts_receivable().list_collection_activities(filter)
}
/// Update the collection status of an invoice.
pub fn update_collection_status(
&self,
invoice_id: Uuid,
status: CollectionStatus,
) -> Result<()> {
self.db.accounts_receivable().update_collection_status(invoice_id.into(), status)
}
// ========================================================================
// Dunning
// ========================================================================
/// Get invoices that are due for dunning letters.
pub fn get_invoices_due_for_dunning(&self) -> Result<Vec<Invoice>> {
self.db.accounts_receivable().get_invoices_due_for_dunning()
}
/// Send a dunning letter and log the activity.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, DunningLetterType};
/// use uuid::Uuid;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let activity = commerce.accounts_receivable().send_dunning_letter(
/// Uuid::new_v4(), // invoice_id
/// DunningLetterType::Reminder1,
/// Some("ar_system"),
/// )?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn send_dunning_letter(
&self,
invoice_id: Uuid,
letter_type: DunningLetterType,
sent_by: Option<&str>,
) -> Result<CollectionActivity> {
self.db.accounts_receivable().send_dunning_letter(invoice_id.into(), letter_type, sent_by)
}
// ========================================================================
// Write-offs
// ========================================================================
/// Create a write-off for an uncollectable invoice.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, CreateWriteOff, WriteOffReason};
/// use rust_decimal_macros::dec;
/// use uuid::Uuid;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let write_off = commerce.accounts_receivable().create_write_off(CreateWriteOff {
/// invoice_id: Uuid::new_v4(),
/// amount: dec!(500.00),
/// reason: WriteOffReason::Uncollectable,
/// notes: Some("Customer bankruptcy".into()),
/// approved_by: Some("CFO".into()),
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create_write_off(&self, input: CreateWriteOff) -> Result<WriteOff> {
self.db.accounts_receivable().create_write_off(input)
}
/// Get a write-off by ID.
pub fn get_write_off(&self, id: Uuid) -> Result<Option<WriteOff>> {
self.db.accounts_receivable().get_write_off(id)
}
/// List write-offs with filtering.
pub fn list_write_offs(&self, filter: WriteOffFilter) -> Result<Vec<WriteOff>> {
self.db.accounts_receivable().list_write_offs(filter)
}
/// Reverse a write-off (restore invoice to collections).
pub fn reverse_write_off(&self, id: Uuid) -> Result<WriteOff> {
self.db.accounts_receivable().reverse_write_off(id)
}
// ========================================================================
// Credit Memos
// ========================================================================
/// Create a credit memo for a customer.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, CreateCreditMemo, CreditMemoReason};
/// use rust_decimal_macros::dec;
/// use uuid::Uuid;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let memo = commerce.accounts_receivable().create_credit_memo(CreateCreditMemo {
/// customer_id: Uuid::new_v4(),
/// original_invoice_id: Some(Uuid::new_v4()),
/// reason: CreditMemoReason::ReturnCredit,
/// amount: dec!(150.00),
/// notes: Some("Credit for returned merchandise".into()),
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create_credit_memo(&self, input: CreateCreditMemo) -> Result<CreditMemo> {
self.db.accounts_receivable().create_credit_memo(input)
}
/// Get a credit memo by ID.
pub fn get_credit_memo(&self, id: Uuid) -> Result<Option<CreditMemo>> {
self.db.accounts_receivable().get_credit_memo(id)
}
/// Get a credit memo by number.
pub fn get_credit_memo_by_number(&self, number: &str) -> Result<Option<CreditMemo>> {
self.db.accounts_receivable().get_credit_memo_by_number(number)
}
/// List credit memos with filtering.
pub fn list_credit_memos(&self, filter: CreditMemoFilter) -> Result<Vec<CreditMemo>> {
self.db.accounts_receivable().list_credit_memos(filter)
}
/// Apply a credit memo to an invoice.
pub fn apply_credit_memo(&self, input: ApplyCreditMemo) -> Result<CreditMemo> {
self.db.accounts_receivable().apply_credit_memo(input)
}
/// Void a credit memo (only if not yet applied).
pub fn void_credit_memo(&self, id: Uuid) -> Result<CreditMemo> {
self.db.accounts_receivable().void_credit_memo(id)
}
/// Get unapplied credit memos for a customer.
pub fn get_unapplied_credits(&self, customer_id: Uuid) -> Result<Vec<CreditMemo>> {
self.db.accounts_receivable().get_unapplied_credits(customer_id)
}
// ========================================================================
// Payment Applications
// ========================================================================
/// Apply a payment to one or more invoices.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, ApplyPaymentToInvoices, InvoicePaymentApplication};
/// use rust_decimal_macros::dec;
/// use uuid::Uuid;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let applications = commerce.accounts_receivable().apply_payment_to_invoices(
/// ApplyPaymentToInvoices {
/// payment_id: Uuid::new_v4(),
/// applications: vec![
/// InvoicePaymentApplication {
/// invoice_id: Uuid::new_v4(),
/// amount: dec!(500.00),
/// },
/// InvoicePaymentApplication {
/// invoice_id: Uuid::new_v4(),
/// amount: dec!(250.00),
/// },
/// ],
/// }
/// )?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn apply_payment_to_invoices(
&self,
input: ApplyPaymentToInvoices,
) -> Result<Vec<ArPaymentApplication>> {
self.db.accounts_receivable().apply_payment_to_invoices(input)
}
/// Get all payment applications for a payment.
pub fn get_payment_applications(&self, payment_id: Uuid) -> Result<Vec<ArPaymentApplication>> {
self.db.accounts_receivable().get_payment_applications(payment_id)
}
/// Unapply a payment application (remove application, restore invoice balance).
pub fn unapply_payment(&self, application_id: Uuid) -> Result<()> {
self.db.accounts_receivable().unapply_payment(application_id)
}
// ========================================================================
// Customer Summary & Statements
// ========================================================================
/// Get AR summary for a customer.
pub fn get_customer_summary(&self, customer_id: Uuid) -> Result<Option<CustomerArSummary>> {
self.db.accounts_receivable().get_customer_summary(customer_id)
}
/// Generate a customer statement.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, GenerateStatementRequest};
/// use chrono::Utc;
/// use uuid::Uuid;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let statement = commerce.accounts_receivable().generate_statement(
/// GenerateStatementRequest {
/// customer_id: Uuid::new_v4(),
/// period_start: None, // defaults to 30 days ago
/// period_end: None, // defaults to now
/// include_paid_invoices: Some(false),
/// }
/// )?;
///
/// println!("Statement for: {}", statement.customer_name);
/// println!("Closing balance: ${}", statement.closing_balance);
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn generate_statement(
&self,
request: GenerateStatementRequest,
) -> Result<CustomerStatement> {
self.db.accounts_receivable().generate_statement(request)
}
// ========================================================================
// Analytics
// ========================================================================
/// Get total outstanding receivables.
pub fn get_total_outstanding(&self) -> Result<Decimal> {
self.db.accounts_receivable().get_total_outstanding()
}
/// Calculate Days Sales Outstanding (DSO).
pub fn get_dso(&self, days: i32) -> Result<Decimal> {
self.db.accounts_receivable().get_dso(days)
}
/// Get average days to pay for a customer.
pub fn get_average_days_to_pay(&self, customer_id: Uuid) -> Result<Option<i32>> {
self.db.accounts_receivable().get_average_days_to_pay(customer_id)
}
/// Get AR summary for multiple customers.
pub fn get_customers_batch(&self, ids: Vec<Uuid>) -> Result<Vec<CustomerArSummary>> {
self.db.accounts_receivable().get_customers_batch(ids)
}
}