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
//! Cost Accounting operations
//!
//! Comprehensive cost management supporting:
//! - Standard cost maintenance
//! - Average cost calculations
//! - FIFO/LIFO cost layers
//! - Cost variance tracking
//! - Cost adjustments with approval workflow
//! - BOM cost rollups
//!
//! # Example
//!
//! ```rust,no_run
//! use stateset_embedded::{Commerce, SetItemCost, CostMethod};
//! use rust_decimal_macros::dec;
//!
//! let commerce = Commerce::new("./store.db")?;
//!
//! // Set standard cost for an item
//! let cost = commerce.cost_accounting().set_item_cost(SetItemCost {
//! sku: "WIDGET-001".into(),
//! cost_method: Some(CostMethod::Average),
//! standard_cost: Some(dec!(10.00)),
//! material_cost: Some(dec!(6.00)),
//! labor_cost: Some(dec!(2.00)),
//! overhead_cost: Some(dec!(2.00)),
//! ..Default::default()
//! })?;
//!
//! println!("Standard cost: ${}", cost.standard_cost);
//! # Ok::<(), stateset_embedded::CommerceError>(())
//! ```
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use stateset_core::{
CostAdjustment, CostAdjustmentFilter, CostLayer, CostLayerFilter, CostMethod, CostRollup,
CostTransaction, CostTransactionFilter, CostTransactionType, CostVariance, CostVarianceFilter,
CreateCostAdjustment, CreateCostLayer, InventoryValuation, IssueCostLayers, ItemCost,
ItemCostFilter, RecordCostVariance, Result, SetItemCost, SkuCostSummary,
};
use stateset_db::Database;
use std::sync::Arc;
use uuid::Uuid;
/// Cost Accounting management interface.
pub struct CostAccounting {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for CostAccounting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CostAccounting").finish_non_exhaustive()
}
}
impl CostAccounting {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
// ========================================================================
// Item Cost Operations
// ========================================================================
/// Get the cost record for an item by SKU.
pub fn get_item_cost(&self, sku: &str) -> Result<Option<ItemCost>> {
self.db.cost_accounting().get_item_cost(sku)
}
/// Set or update the cost for an item.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, SetItemCost, CostMethod};
/// use rust_decimal_macros::dec;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let cost = commerce.cost_accounting().set_item_cost(SetItemCost {
/// sku: "RAW-001".into(),
/// cost_method: Some(CostMethod::Standard),
/// standard_cost: Some(dec!(15.00)),
/// material_cost: Some(dec!(15.00)),
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn set_item_cost(&self, input: SetItemCost) -> Result<ItemCost> {
self.db.cost_accounting().set_item_cost(input)
}
/// List item costs with optional filtering.
pub fn list_item_costs(&self, filter: ItemCostFilter) -> Result<Vec<ItemCost>> {
self.db.cost_accounting().list_item_costs(filter)
}
/// Update the average cost when receiving inventory.
///
/// This performs a weighted average calculation based on
/// existing quantity/cost and new receipt.
pub fn update_average_cost(
&self,
sku: &str,
quantity: Decimal,
unit_cost: Decimal,
) -> Result<ItemCost> {
self.db.cost_accounting().update_average_cost(sku, quantity, unit_cost)
}
/// Update the last purchase cost.
pub fn update_last_cost(&self, sku: &str, unit_cost: Decimal) -> Result<ItemCost> {
self.db.cost_accounting().update_last_cost(sku, unit_cost)
}
// ========================================================================
// Cost Layer Operations (FIFO/LIFO)
// ========================================================================
/// Create a cost layer for FIFO/LIFO costing.
///
/// Cost layers track the quantity and cost of each receipt,
/// enabling accurate FIFO/LIFO cost relief.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, CreateCostLayer, CostLayerSource};
/// use rust_decimal_macros::dec;
/// use uuid::Uuid;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let layer = commerce.cost_accounting().create_cost_layer(CreateCostLayer {
/// sku: "WIDGET-001".into(),
/// quantity: dec!(100),
/// unit_cost: dec!(10.50),
/// source_type: CostLayerSource::Purchase,
/// source_id: Some(Uuid::new_v4()), // PO receipt ID
/// lot_id: None,
/// location_id: Some(1),
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create_cost_layer(&self, input: CreateCostLayer) -> Result<CostLayer> {
self.db.cost_accounting().create_cost_layer(input)
}
/// Get a cost layer by ID.
pub fn get_cost_layer(&self, id: Uuid) -> Result<Option<CostLayer>> {
self.db.cost_accounting().get_cost_layer(id)
}
/// List cost layers with optional filtering.
pub fn list_cost_layers(&self, filter: CostLayerFilter) -> Result<Vec<CostLayer>> {
self.db.cost_accounting().list_cost_layers(filter)
}
/// Issue inventory using FIFO (First-In-First-Out) costing.
///
/// Consumes from oldest cost layers first.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, IssueCostLayers};
/// use rust_decimal_macros::dec;
/// use uuid::Uuid;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let transactions = commerce.cost_accounting().issue_fifo(IssueCostLayers {
/// sku: "WIDGET-001".into(),
/// quantity: dec!(25),
/// reference_type: Some("sales_order".into()),
/// reference_id: Some(Uuid::new_v4()),
/// notes: Some("Order fulfillment".into()),
/// })?;
///
/// for tx in &transactions {
/// println!("Issued {} @ ${} from layer", tx.quantity, tx.unit_cost);
/// }
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn issue_fifo(&self, input: IssueCostLayers) -> Result<Vec<CostTransaction>> {
self.db.cost_accounting().issue_fifo(input)
}
/// Issue inventory using LIFO (Last-In-First-Out) costing.
///
/// Consumes from newest cost layers first.
pub fn issue_lifo(&self, input: IssueCostLayers) -> Result<Vec<CostTransaction>> {
self.db.cost_accounting().issue_lifo(input)
}
/// Get total remaining quantity in cost layers for a SKU.
pub fn get_layers_remaining(&self, sku: &str) -> Result<Decimal> {
self.db.cost_accounting().get_layers_remaining(sku)
}
// ========================================================================
// Cost Transaction Operations
// ========================================================================
/// Record a cost transaction.
#[allow(clippy::too_many_arguments)]
pub fn record_cost_transaction(
&self,
sku: &str,
transaction_type: CostTransactionType,
quantity: Decimal,
unit_cost: Decimal,
layer_id: Option<Uuid>,
reference_type: Option<&str>,
reference_id: Option<Uuid>,
notes: Option<&str>,
) -> Result<CostTransaction> {
self.db.cost_accounting().record_cost_transaction(
sku,
transaction_type,
quantity,
unit_cost,
layer_id,
reference_type,
reference_id,
notes,
)
}
/// List cost transactions with optional filtering.
pub fn list_cost_transactions(
&self,
filter: CostTransactionFilter,
) -> Result<Vec<CostTransaction>> {
self.db.cost_accounting().list_cost_transactions(filter)
}
// ========================================================================
// Cost Variance Operations
// ========================================================================
/// Record a cost variance.
///
/// Captures the difference between standard and actual cost.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, RecordCostVariance, VarianceType};
/// use rust_decimal_macros::dec;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let variance = commerce.cost_accounting().record_variance(RecordCostVariance {
/// sku: "WIDGET-001".into(),
/// variance_type: VarianceType::Purchase,
/// standard_cost: dec!(10.00),
/// actual_cost: dec!(10.50),
/// quantity: dec!(100),
/// reference_type: Some("purchase_order".into()),
/// reference_id: None,
/// notes: Some("Price increase from supplier".into()),
/// })?;
///
/// println!("Variance: ${} ({}%)", variance.variance_amount, variance.variance_percent);
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn record_variance(&self, input: RecordCostVariance) -> Result<CostVariance> {
self.db.cost_accounting().record_variance(input)
}
/// List cost variances with optional filtering.
pub fn list_variances(&self, filter: CostVarianceFilter) -> Result<Vec<CostVariance>> {
self.db.cost_accounting().list_variances(filter)
}
/// Get total variance for a period.
pub fn get_variance_summary(&self, from: DateTime<Utc>, to: DateTime<Utc>) -> Result<Decimal> {
self.db.cost_accounting().get_variance_summary(from, to)
}
// ========================================================================
// Cost Adjustment Operations
// ========================================================================
/// Create a cost adjustment request.
///
/// Adjustments require approval before being applied.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, CreateCostAdjustment, CostAdjustmentType};
/// use rust_decimal_macros::dec;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let adjustment = commerce.cost_accounting().create_adjustment(CreateCostAdjustment {
/// sku: "WIDGET-001".into(),
/// adjustment_type: CostAdjustmentType::StandardCostUpdate,
/// new_cost: dec!(11.00),
/// reason: "Annual cost review - material price increase".into(),
/// created_by: Some("finance_user".into()),
/// })?;
///
/// println!("Created adjustment {}", adjustment.adjustment_number);
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create_adjustment(&self, input: CreateCostAdjustment) -> Result<CostAdjustment> {
self.db.cost_accounting().create_adjustment(input)
}
/// Get a cost adjustment by ID.
pub fn get_adjustment(&self, id: Uuid) -> Result<Option<CostAdjustment>> {
self.db.cost_accounting().get_adjustment(id)
}
/// List cost adjustments with optional filtering.
pub fn list_adjustments(&self, filter: CostAdjustmentFilter) -> Result<Vec<CostAdjustment>> {
self.db.cost_accounting().list_adjustments(filter)
}
/// Approve a cost adjustment.
pub fn approve_adjustment(&self, id: Uuid, approved_by: &str) -> Result<CostAdjustment> {
self.db.cost_accounting().approve_adjustment(id, approved_by)
}
/// Apply an approved cost adjustment.
///
/// Updates the item's standard cost.
pub fn apply_adjustment(&self, id: Uuid) -> Result<CostAdjustment> {
self.db.cost_accounting().apply_adjustment(id)
}
/// Reject a cost adjustment.
pub fn reject_adjustment(&self, id: Uuid) -> Result<CostAdjustment> {
self.db.cost_accounting().reject_adjustment(id)
}
// ========================================================================
// Rollup Operations
// ========================================================================
/// Calculate cost rollup for a manufactured item.
///
/// Sums component costs from the BOM to determine total cost.
pub fn calculate_rollup(&self, sku: &str, bom_id: Option<Uuid>) -> Result<CostRollup> {
self.db.cost_accounting().calculate_rollup(sku, bom_id)
}
/// Get the latest cost rollup for a SKU.
pub fn get_rollup(&self, sku: &str) -> Result<Option<CostRollup>> {
self.db.cost_accounting().get_rollup(sku)
}
// ========================================================================
// Valuation Operations
// ========================================================================
/// Get inventory valuation using specified costing method.
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, CostMethod};
///
/// let commerce = Commerce::new(":memory:")?;
///
/// let valuation = commerce.cost_accounting().get_inventory_valuation(CostMethod::Average)?;
/// println!("Total inventory value: ${}", valuation.total_value);
/// println!("Total quantity: {}", valuation.total_quantity);
/// println!("Average unit cost: ${}", valuation.average_unit_cost);
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn get_inventory_valuation(&self, cost_method: CostMethod) -> Result<InventoryValuation> {
self.db.cost_accounting().get_inventory_valuation(cost_method)
}
/// Get cost summary for a specific SKU.
pub fn get_sku_cost_summary(&self, sku: &str) -> Result<Option<SkuCostSummary>> {
self.db.cost_accounting().get_sku_cost_summary(sku)
}
/// Get total inventory value.
pub fn get_total_inventory_value(&self) -> Result<Decimal> {
self.db.cost_accounting().get_total_inventory_value()
}
}