backbone_inventory/domain/entity/
purchase_receipt_item.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5use rust_decimal::Decimal;
6use super::AuditMetadata;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(transparent)]
11pub struct PurchaseReceiptItemId(pub Uuid);
12
13impl PurchaseReceiptItemId {
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 PurchaseReceiptItemId {
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 PurchaseReceiptItemId {
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 PurchaseReceiptItemId {
33 fn from(id: Uuid) -> Self { Self(id) }
34}
35
36impl From<PurchaseReceiptItemId> for Uuid {
37 fn from(id: PurchaseReceiptItemId) -> Self { id.0 }
38}
39
40impl AsRef<Uuid> for PurchaseReceiptItemId {
41 fn as_ref(&self) -> &Uuid { &self.0 }
42}
43
44impl std::ops::Deref for PurchaseReceiptItemId {
45 type Target = Uuid;
46 fn deref(&self) -> &Self::Target { &self.0 }
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
50pub struct PurchaseReceiptItem {
51 pub id: Uuid,
52 pub receipt_id: Uuid,
53 pub item_id: Uuid,
54 pub quantity: Decimal,
55 pub rate: Decimal,
56 pub amount: Decimal,
57 pub is_landed_costs_line: bool,
58 #[serde(default)]
59 #[sqlx(json)]
60 pub metadata: AuditMetadata,
61}
62
63impl PurchaseReceiptItem {
64 pub fn builder() -> PurchaseReceiptItemBuilder {
66 <PurchaseReceiptItemBuilder as Default>::default()
67 }
68
69 pub fn new(receipt_id: Uuid, item_id: Uuid, quantity: Decimal, rate: Decimal, amount: Decimal, is_landed_costs_line: bool) -> Self {
71 Self {
72 id: Uuid::new_v4(),
73 receipt_id,
74 item_id,
75 quantity,
76 rate,
77 amount,
78 is_landed_costs_line,
79 metadata: AuditMetadata::default(),
80 }
81 }
82
83 pub fn id(&self) -> &Uuid {
85 &self.id
86 }
87
88 pub fn typed_id(&self) -> PurchaseReceiptItemId {
90 PurchaseReceiptItemId(self.id)
91 }
92
93 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
95 self.metadata.created_at.as_ref()
96 }
97
98 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
100 self.metadata.updated_at.as_ref()
101 }
102
103 pub fn is_deleted(&self) -> bool {
105 self.metadata.deleted_at.is_some()
106 }
107
108 pub fn is_active(&self) -> bool {
110 self.metadata.deleted_at.is_none()
111 }
112
113 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
115 self.metadata.deleted_at.as_ref()
116 }
117
118 pub fn created_by(&self) -> Option<&Uuid> {
120 self.metadata.created_by.as_ref()
121 }
122
123 pub fn updated_by(&self) -> Option<&Uuid> {
125 self.metadata.updated_by.as_ref()
126 }
127
128 pub fn deleted_by(&self) -> Option<&Uuid> {
130 self.metadata.deleted_by.as_ref()
131 }
132
133
134 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
140 for (key, value) in fields {
141 match key.as_str() {
142 "receipt_id" => {
143 if let Ok(v) = serde_json::from_value(value) { self.receipt_id = v; }
144 }
145 "item_id" => {
146 if let Ok(v) = serde_json::from_value(value) { self.item_id = v; }
147 }
148 "quantity" => {
149 if let Ok(v) = serde_json::from_value(value) { self.quantity = v; }
150 }
151 "rate" => {
152 if let Ok(v) = serde_json::from_value(value) { self.rate = v; }
153 }
154 "amount" => {
155 if let Ok(v) = serde_json::from_value(value) { self.amount = v; }
156 }
157 "is_landed_costs_line" => {
158 if let Ok(v) = serde_json::from_value(value) { self.is_landed_costs_line = v; }
159 }
160 _ => {} }
162 }
163 }
164
165}
166
167impl super::Entity for PurchaseReceiptItem {
168 type Id = Uuid;
169
170 fn entity_id(&self) -> &Self::Id {
171 &self.id
172 }
173
174 fn entity_type() -> &'static str {
175 "PurchaseReceiptItem"
176 }
177}
178
179impl backbone_core::PersistentEntity for PurchaseReceiptItem {
180 fn entity_id(&self) -> String {
181 self.id.to_string()
182 }
183 fn set_entity_id(&mut self, id: String) {
184 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
185 self.id = uuid;
186 }
187 }
188 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
189 self.metadata.created_at
190 }
191 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
192 self.metadata.created_at = Some(ts);
193 }
194 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
195 self.metadata.updated_at
196 }
197 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
198 self.metadata.updated_at = Some(ts);
199 }
200 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
201 self.metadata.deleted_at
202 }
203 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
204 self.metadata.deleted_at = ts;
205 }
206}
207
208impl backbone_orm::EntityRepoMeta for PurchaseReceiptItem {
209 fn column_types() -> std::collections::HashMap<String, String> {
210 let mut m = std::collections::HashMap::new();
211 m.insert("id".to_string(), "uuid".to_string());
212 m.insert("receipt_id".to_string(), "uuid".to_string());
213 m.insert("item_id".to_string(), "uuid".to_string());
214 m
215 }
216 fn search_fields() -> &'static [&'static str] {
217 &[]
218 }
219 fn relations() -> &'static [(&'static str, &'static str, &'static str)] {
220 &[("receipt", "purchase_receipts", "receiptId")]
221 }
222}
223
224#[derive(Debug, Clone, Default)]
229pub struct PurchaseReceiptItemBuilder {
230 receipt_id: Option<Uuid>,
231 item_id: Option<Uuid>,
232 quantity: Option<Decimal>,
233 rate: Option<Decimal>,
234 amount: Option<Decimal>,
235 is_landed_costs_line: Option<bool>,
236}
237
238impl PurchaseReceiptItemBuilder {
239 pub fn receipt_id(mut self, value: Uuid) -> Self {
241 self.receipt_id = Some(value);
242 self
243 }
244
245 pub fn item_id(mut self, value: Uuid) -> Self {
247 self.item_id = Some(value);
248 self
249 }
250
251 pub fn quantity(mut self, value: Decimal) -> Self {
253 self.quantity = Some(value);
254 self
255 }
256
257 pub fn rate(mut self, value: Decimal) -> Self {
259 self.rate = Some(value);
260 self
261 }
262
263 pub fn amount(mut self, value: Decimal) -> Self {
265 self.amount = Some(value);
266 self
267 }
268
269 pub fn is_landed_costs_line(mut self, value: bool) -> Self {
271 self.is_landed_costs_line = Some(value);
272 self
273 }
274
275 pub fn build(self) -> Result<PurchaseReceiptItem, String> {
279 let receipt_id = self.receipt_id.ok_or_else(|| "receipt_id is required".to_string())?;
280 let item_id = self.item_id.ok_or_else(|| "item_id is required".to_string())?;
281 let quantity = self.quantity.ok_or_else(|| "quantity is required".to_string())?;
282 let rate = self.rate.ok_or_else(|| "rate is required".to_string())?;
283
284 Ok(PurchaseReceiptItem {
285 id: Uuid::new_v4(),
286 receipt_id,
287 item_id,
288 quantity,
289 rate,
290 amount: self.amount.unwrap_or(Decimal::from(0)),
291 is_landed_costs_line: self.is_landed_costs_line.unwrap_or(false),
292 metadata: AuditMetadata::default(),
293 })
294 }
295}