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    // <<< CUSTOM METHODS START >>>
252    // <<< CUSTOM METHODS END >>>
253}
254
255impl super::Entity for Quant {
256    type Id = Uuid;
257
258    fn entity_id(&self) -> &Self::Id {
259        &self.id
260    }
261
262    fn entity_type() -> &'static str {
263        "Quant"
264    }
265}
266
267impl backbone_core::PersistentEntity for Quant {
268    fn entity_id(&self) -> String {
269        self.id.to_string()
270    }
271    fn set_entity_id(&mut self, id: String) {
272        if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
273            self.id = uuid;
274        }
275    }
276    fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
277        self.metadata.created_at
278    }
279    fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
280        self.metadata.created_at = Some(ts);
281    }
282    fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
283        self.metadata.updated_at
284    }
285    fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
286        self.metadata.updated_at = Some(ts);
287    }
288    fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
289        self.metadata.deleted_at
290    }
291    fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
292        self.metadata.deleted_at = ts;
293    }
294}
295
296impl backbone_orm::EntityRepoMeta for Quant {
297    fn column_types() -> std::collections::HashMap<String, String> {
298        let mut m = std::collections::HashMap::new();
299        m.insert("id".to_string(), "uuid".to_string());
300        m.insert("item_id".to_string(), "uuid".to_string());
301        m.insert("location_id".to_string(), "uuid".to_string());
302        m.insert("lot_id".to_string(), "uuid".to_string());
303        m.insert("package_id".to_string(), "uuid".to_string());
304        m.insert("owner_id".to_string(), "uuid".to_string());
305        m.insert("in_date".to_string(), "timestamptz".to_string());
306        m.insert("inventory_date".to_string(), "date".to_string());
307        m
308    }
309    fn search_fields() -> &'static [&'static str] {
310        &[]
311    }
312}
313
314/// Builder for Quant entity
315///
316/// Provides a fluent API for constructing Quant instances.
317/// System fields (id, metadata, timestamps) are auto-initialized.
318#[derive(Debug, Clone, Default)]
319pub struct QuantBuilder {
320    item_id: Option<Uuid>,
321    location_id: Option<Uuid>,
322    lot_id: Option<Uuid>,
323    package_id: Option<Uuid>,
324    owner_id: Option<Uuid>,
325    quantity: Option<Decimal>,
326    reserved_quantity: Option<Decimal>,
327    available_quantity: Option<Decimal>,
328    in_date: Option<DateTime<Utc>>,
329    inventory_quantity: Option<Decimal>,
330    inventory_diff_quantity: Option<Decimal>,
331    inventory_quantity_set: Option<bool>,
332    inventory_date: Option<NaiveDate>,
333    sn_duplicated: Option<bool>,
334}
335
336impl QuantBuilder {
337    /// Set the item_id field (required)
338    pub fn item_id(mut self, value: Uuid) -> Self {
339        self.item_id = Some(value);
340        self
341    }
342
343    /// Set the location_id field (required)
344    pub fn location_id(mut self, value: Uuid) -> Self {
345        self.location_id = Some(value);
346        self
347    }
348
349    /// Set the lot_id field (optional)
350    pub fn lot_id(mut self, value: Uuid) -> Self {
351        self.lot_id = Some(value);
352        self
353    }
354
355    /// Set the package_id field (optional)
356    pub fn package_id(mut self, value: Uuid) -> Self {
357        self.package_id = Some(value);
358        self
359    }
360
361    /// Set the owner_id field (optional)
362    pub fn owner_id(mut self, value: Uuid) -> Self {
363        self.owner_id = Some(value);
364        self
365    }
366
367    /// Set the quantity field (default: `Decimal::from(0)`)
368    pub fn quantity(mut self, value: Decimal) -> Self {
369        self.quantity = Some(value);
370        self
371    }
372
373    /// Set the reserved_quantity field (default: `Decimal::from(0)`)
374    pub fn reserved_quantity(mut self, value: Decimal) -> Self {
375        self.reserved_quantity = Some(value);
376        self
377    }
378
379    /// Set the available_quantity field (default: `Decimal::from(0)`)
380    pub fn available_quantity(mut self, value: Decimal) -> Self {
381        self.available_quantity = Some(value);
382        self
383    }
384
385    /// Set the in_date field (optional)
386    pub fn in_date(mut self, value: DateTime<Utc>) -> Self {
387        self.in_date = Some(value);
388        self
389    }
390
391    /// Set the inventory_quantity field (optional)
392    pub fn inventory_quantity(mut self, value: Decimal) -> Self {
393        self.inventory_quantity = Some(value);
394        self
395    }
396
397    /// Set the inventory_diff_quantity field (optional)
398    pub fn inventory_diff_quantity(mut self, value: Decimal) -> Self {
399        self.inventory_diff_quantity = Some(value);
400        self
401    }
402
403    /// Set the inventory_quantity_set field (default: `false`)
404    pub fn inventory_quantity_set(mut self, value: bool) -> Self {
405        self.inventory_quantity_set = Some(value);
406        self
407    }
408
409    /// Set the inventory_date field (optional)
410    pub fn inventory_date(mut self, value: NaiveDate) -> Self {
411        self.inventory_date = Some(value);
412        self
413    }
414
415    /// Set the sn_duplicated field (default: `false`)
416    pub fn sn_duplicated(mut self, value: bool) -> Self {
417        self.sn_duplicated = Some(value);
418        self
419    }
420
421    /// Build the Quant entity
422    ///
423    /// Returns Err if any required field without a default is missing.
424    pub fn build(self) -> Result<Quant, String> {
425        let item_id = self.item_id.ok_or_else(|| "item_id is required".to_string())?;
426        let location_id = self.location_id.ok_or_else(|| "location_id is required".to_string())?;
427
428        Ok(Quant {
429            id: Uuid::new_v4(),
430            item_id,
431            location_id,
432            lot_id: self.lot_id,
433            package_id: self.package_id,
434            owner_id: self.owner_id,
435            quantity: self.quantity.unwrap_or(Decimal::from(0)),
436            reserved_quantity: self.reserved_quantity.unwrap_or(Decimal::from(0)),
437            available_quantity: self.available_quantity.unwrap_or(Decimal::from(0)),
438            in_date: self.in_date,
439            inventory_quantity: self.inventory_quantity,
440            inventory_diff_quantity: self.inventory_diff_quantity,
441            inventory_quantity_set: self.inventory_quantity_set.unwrap_or(false),
442            inventory_date: self.inventory_date,
443            sn_duplicated: self.sn_duplicated.unwrap_or(false),
444            metadata: AuditMetadata::default(),
445        })
446    }
447}