Skip to main content

backbone_inventory/domain/entity/
quant.rs

1use chrono::{DateTime, Utc, NaiveDate};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5use rust_decimal::Decimal;
6use super::AuditMetadata;
7
8/// Strongly-typed ID for Quant
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(transparent)]
11pub struct QuantId(pub Uuid);
12
13impl QuantId {
14    pub fn new(id: Uuid) -> Self { Self(id) }
15    pub fn generate() -> Self { Self(Uuid::new_v4()) }
16    pub fn into_inner(self) -> Uuid { self.0 }
17}
18
19impl std::fmt::Display for QuantId {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        write!(f, "{}", self.0)
22    }
23}
24
25impl std::str::FromStr for QuantId {
26    type Err = uuid::Error;
27    fn from_str(s: &str) -> Result<Self, Self::Err> {
28        Ok(Self(Uuid::parse_str(s)?))
29    }
30}
31
32impl From<Uuid> for QuantId {
33    fn from(id: Uuid) -> Self { Self(id) }
34}
35
36impl From<QuantId> for Uuid {
37    fn from(id: QuantId) -> Self { id.0 }
38}
39
40impl AsRef<Uuid> for QuantId {
41    fn as_ref(&self) -> &Uuid { &self.0 }
42}
43
44impl std::ops::Deref for QuantId {
45    type Target = Uuid;
46    fn deref(&self) -> &Self::Target { &self.0 }
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
50pub struct Quant {
51    pub id: Uuid,
52    pub item_id: Uuid,
53    pub location_id: Uuid,
54    pub lot_id: Option<Uuid>,
55    pub package_id: Option<Uuid>,
56    pub owner_id: Option<Uuid>,
57    pub quantity: Decimal,
58    pub reserved_quantity: Decimal,
59    pub available_quantity: Decimal,
60    pub in_date: Option<DateTime<Utc>>,
61    pub inventory_quantity: Option<Decimal>,
62    pub inventory_diff_quantity: Option<Decimal>,
63    pub inventory_quantity_set: bool,
64    pub inventory_date: Option<NaiveDate>,
65    pub sn_duplicated: bool,
66    #[serde(default)]
67    #[sqlx(json)]
68    pub metadata: AuditMetadata,
69}
70
71impl Quant {
72    /// Create a builder for Quant
73    pub fn builder() -> QuantBuilder {
74        <QuantBuilder as Default>::default()
75    }
76
77    /// Create a new Quant with required fields
78    pub fn new(item_id: Uuid, location_id: Uuid, quantity: Decimal, reserved_quantity: Decimal, available_quantity: Decimal, inventory_quantity_set: bool, sn_duplicated: bool) -> Self {
79        Self {
80            id: Uuid::new_v4(),
81            item_id,
82            location_id,
83            lot_id: None,
84            package_id: None,
85            owner_id: None,
86            quantity,
87            reserved_quantity,
88            available_quantity,
89            in_date: None,
90            inventory_quantity: None,
91            inventory_diff_quantity: None,
92            inventory_quantity_set,
93            inventory_date: None,
94            sn_duplicated,
95            metadata: AuditMetadata::default(),
96        }
97    }
98
99    /// Get the entity's unique identifier
100    pub fn id(&self) -> &Uuid {
101        &self.id
102    }
103
104    /// Get a strongly-typed ID for this entity
105    pub fn typed_id(&self) -> QuantId {
106        QuantId(self.id)
107    }
108
109    /// Get when this entity was created
110    pub fn created_at(&self) -> Option<&DateTime<Utc>> {
111        self.metadata.created_at.as_ref()
112    }
113
114    /// Get when this entity was last updated
115    pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
116        self.metadata.updated_at.as_ref()
117    }
118
119    /// Check if this entity is soft deleted
120    pub fn is_deleted(&self) -> bool {
121        self.metadata.deleted_at.is_some()
122    }
123
124    /// Check if this entity is active (not deleted)
125    pub fn is_active(&self) -> bool {
126        self.metadata.deleted_at.is_none()
127    }
128
129    /// Get when this entity was deleted
130    pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
131        self.metadata.deleted_at.as_ref()
132    }
133
134    /// Get who created this entity
135    pub fn created_by(&self) -> Option<&Uuid> {
136        self.metadata.created_by.as_ref()
137    }
138
139    /// Get who last updated this entity
140    pub fn updated_by(&self) -> Option<&Uuid> {
141        self.metadata.updated_by.as_ref()
142    }
143
144    /// Get who deleted this entity
145    pub fn deleted_by(&self) -> Option<&Uuid> {
146        self.metadata.deleted_by.as_ref()
147    }
148
149
150    // ==========================================================
151    // Fluent Setters (with_* for optional fields)
152    // ==========================================================
153
154    /// Set the lot_id field (chainable)
155    pub fn with_lot_id(mut self, value: Uuid) -> Self {
156        self.lot_id = Some(value);
157        self
158    }
159
160    /// Set the package_id field (chainable)
161    pub fn with_package_id(mut self, value: Uuid) -> Self {
162        self.package_id = Some(value);
163        self
164    }
165
166    /// Set the owner_id field (chainable)
167    pub fn with_owner_id(mut self, value: Uuid) -> Self {
168        self.owner_id = Some(value);
169        self
170    }
171
172    /// Set the in_date field (chainable)
173    pub fn with_in_date(mut self, value: DateTime<Utc>) -> Self {
174        self.in_date = Some(value);
175        self
176    }
177
178    /// Set the inventory_quantity field (chainable)
179    pub fn with_inventory_quantity(mut self, value: Decimal) -> Self {
180        self.inventory_quantity = Some(value);
181        self
182    }
183
184    /// Set the inventory_diff_quantity field (chainable)
185    pub fn with_inventory_diff_quantity(mut self, value: Decimal) -> Self {
186        self.inventory_diff_quantity = Some(value);
187        self
188    }
189
190    /// Set the inventory_date field (chainable)
191    pub fn with_inventory_date(mut self, value: NaiveDate) -> Self {
192        self.inventory_date = Some(value);
193        self
194    }
195
196    // ==========================================================
197    // Partial Update
198    // ==========================================================
199
200    /// Apply partial updates from a map of field name to JSON value
201    pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
202        for (key, value) in fields {
203            match key.as_str() {
204                "item_id" => {
205                    if let Ok(v) = serde_json::from_value(value) { self.item_id = v; }
206                }
207                "location_id" => {
208                    if let Ok(v) = serde_json::from_value(value) { self.location_id = v; }
209                }
210                "lot_id" => {
211                    if let Ok(v) = serde_json::from_value(value) { self.lot_id = v; }
212                }
213                "package_id" => {
214                    if let Ok(v) = serde_json::from_value(value) { self.package_id = v; }
215                }
216                "owner_id" => {
217                    if let Ok(v) = serde_json::from_value(value) { self.owner_id = v; }
218                }
219                "quantity" => {
220                    if let Ok(v) = serde_json::from_value(value) { self.quantity = v; }
221                }
222                "reserved_quantity" => {
223                    if let Ok(v) = serde_json::from_value(value) { self.reserved_quantity = v; }
224                }
225                "available_quantity" => {
226                    if let Ok(v) = serde_json::from_value(value) { self.available_quantity = v; }
227                }
228                "in_date" => {
229                    if let Ok(v) = serde_json::from_value(value) { self.in_date = v; }
230                }
231                "inventory_quantity" => {
232                    if let Ok(v) = serde_json::from_value(value) { self.inventory_quantity = v; }
233                }
234                "inventory_diff_quantity" => {
235                    if let Ok(v) = serde_json::from_value(value) { self.inventory_diff_quantity = v; }
236                }
237                "inventory_quantity_set" => {
238                    if let Ok(v) = serde_json::from_value(value) { self.inventory_quantity_set = v; }
239                }
240                "inventory_date" => {
241                    if let Ok(v) = serde_json::from_value(value) { self.inventory_date = v; }
242                }
243                "sn_duplicated" => {
244                    if let Ok(v) = serde_json::from_value(value) { self.sn_duplicated = v; }
245                }
246                _ => {} // ignore unknown fields
247            }
248        }
249    }
250
251}
252
253impl super::Entity for Quant {
254    type Id = Uuid;
255
256    fn entity_id(&self) -> &Self::Id {
257        &self.id
258    }
259
260    fn entity_type() -> &'static str {
261        "Quant"
262    }
263}
264
265impl backbone_core::PersistentEntity for Quant {
266    fn entity_id(&self) -> String {
267        self.id.to_string()
268    }
269    fn set_entity_id(&mut self, id: String) {
270        if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
271            self.id = uuid;
272        }
273    }
274    fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
275        self.metadata.created_at
276    }
277    fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
278        self.metadata.created_at = Some(ts);
279    }
280    fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
281        self.metadata.updated_at
282    }
283    fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
284        self.metadata.updated_at = Some(ts);
285    }
286    fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
287        self.metadata.deleted_at
288    }
289    fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
290        self.metadata.deleted_at = ts;
291    }
292}
293
294impl backbone_orm::EntityRepoMeta for Quant {
295    fn column_types() -> std::collections::HashMap<String, String> {
296        let mut m = std::collections::HashMap::new();
297        m.insert("id".to_string(), "uuid".to_string());
298        m.insert("item_id".to_string(), "uuid".to_string());
299        m.insert("location_id".to_string(), "uuid".to_string());
300        m.insert("lot_id".to_string(), "uuid".to_string());
301        m.insert("package_id".to_string(), "uuid".to_string());
302        m.insert("owner_id".to_string(), "uuid".to_string());
303        m.insert("in_date".to_string(), "timestamptz".to_string());
304        m.insert("inventory_date".to_string(), "date".to_string());
305        m
306    }
307    fn search_fields() -> &'static [&'static str] {
308        &[]
309    }
310}
311
312/// Builder for Quant entity
313///
314/// Provides a fluent API for constructing Quant instances.
315/// System fields (id, metadata, timestamps) are auto-initialized.
316#[derive(Debug, Clone, Default)]
317pub struct QuantBuilder {
318    item_id: Option<Uuid>,
319    location_id: Option<Uuid>,
320    lot_id: Option<Uuid>,
321    package_id: Option<Uuid>,
322    owner_id: Option<Uuid>,
323    quantity: Option<Decimal>,
324    reserved_quantity: Option<Decimal>,
325    available_quantity: Option<Decimal>,
326    in_date: Option<DateTime<Utc>>,
327    inventory_quantity: Option<Decimal>,
328    inventory_diff_quantity: Option<Decimal>,
329    inventory_quantity_set: Option<bool>,
330    inventory_date: Option<NaiveDate>,
331    sn_duplicated: Option<bool>,
332}
333
334impl QuantBuilder {
335    /// Set the item_id field (required)
336    pub fn item_id(mut self, value: Uuid) -> Self {
337        self.item_id = Some(value);
338        self
339    }
340
341    /// Set the location_id field (required)
342    pub fn location_id(mut self, value: Uuid) -> Self {
343        self.location_id = Some(value);
344        self
345    }
346
347    /// Set the lot_id field (optional)
348    pub fn lot_id(mut self, value: Uuid) -> Self {
349        self.lot_id = Some(value);
350        self
351    }
352
353    /// Set the package_id field (optional)
354    pub fn package_id(mut self, value: Uuid) -> Self {
355        self.package_id = Some(value);
356        self
357    }
358
359    /// Set the owner_id field (optional)
360    pub fn owner_id(mut self, value: Uuid) -> Self {
361        self.owner_id = Some(value);
362        self
363    }
364
365    /// Set the quantity field (default: `Decimal::from(0)`)
366    pub fn quantity(mut self, value: Decimal) -> Self {
367        self.quantity = Some(value);
368        self
369    }
370
371    /// Set the reserved_quantity field (default: `Decimal::from(0)`)
372    pub fn reserved_quantity(mut self, value: Decimal) -> Self {
373        self.reserved_quantity = Some(value);
374        self
375    }
376
377    /// Set the available_quantity field (default: `Decimal::from(0)`)
378    pub fn available_quantity(mut self, value: Decimal) -> Self {
379        self.available_quantity = Some(value);
380        self
381    }
382
383    /// Set the in_date field (optional)
384    pub fn in_date(mut self, value: DateTime<Utc>) -> Self {
385        self.in_date = Some(value);
386        self
387    }
388
389    /// Set the inventory_quantity field (optional)
390    pub fn inventory_quantity(mut self, value: Decimal) -> Self {
391        self.inventory_quantity = Some(value);
392        self
393    }
394
395    /// Set the inventory_diff_quantity field (optional)
396    pub fn inventory_diff_quantity(mut self, value: Decimal) -> Self {
397        self.inventory_diff_quantity = Some(value);
398        self
399    }
400
401    /// Set the inventory_quantity_set field (default: `false`)
402    pub fn inventory_quantity_set(mut self, value: bool) -> Self {
403        self.inventory_quantity_set = Some(value);
404        self
405    }
406
407    /// Set the inventory_date field (optional)
408    pub fn inventory_date(mut self, value: NaiveDate) -> Self {
409        self.inventory_date = Some(value);
410        self
411    }
412
413    /// Set the sn_duplicated field (default: `false`)
414    pub fn sn_duplicated(mut self, value: bool) -> Self {
415        self.sn_duplicated = Some(value);
416        self
417    }
418
419    /// Build the Quant entity
420    ///
421    /// Returns Err if any required field without a default is missing.
422    pub fn build(self) -> Result<Quant, String> {
423        let item_id = self.item_id.ok_or_else(|| "item_id is required".to_string())?;
424        let location_id = self.location_id.ok_or_else(|| "location_id is required".to_string())?;
425
426        Ok(Quant {
427            id: Uuid::new_v4(),
428            item_id,
429            location_id,
430            lot_id: self.lot_id,
431            package_id: self.package_id,
432            owner_id: self.owner_id,
433            quantity: self.quantity.unwrap_or(Decimal::from(0)),
434            reserved_quantity: self.reserved_quantity.unwrap_or(Decimal::from(0)),
435            available_quantity: self.available_quantity.unwrap_or(Decimal::from(0)),
436            in_date: self.in_date,
437            inventory_quantity: self.inventory_quantity,
438            inventory_diff_quantity: self.inventory_diff_quantity,
439            inventory_quantity_set: self.inventory_quantity_set.unwrap_or(false),
440            inventory_date: self.inventory_date,
441            sn_duplicated: self.sn_duplicated.unwrap_or(false),
442            metadata: AuditMetadata::default(),
443        })
444    }
445}