backbone_inventory/domain/entity/
transfer.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5
6use super::Priority;
7use super::MoveType;
8use super::TransferState;
9use super::AuditMetadata;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
13#[serde(transparent)]
14pub struct TransferId(pub Uuid);
15
16impl TransferId {
17 pub fn new(id: Uuid) -> Self { Self(id) }
18 pub fn generate() -> Self { Self(Uuid::new_v4()) }
19 pub fn into_inner(self) -> Uuid { self.0 }
20}
21
22impl std::fmt::Display for TransferId {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 write!(f, "{}", self.0)
25 }
26}
27
28impl std::str::FromStr for TransferId {
29 type Err = uuid::Error;
30 fn from_str(s: &str) -> Result<Self, Self::Err> {
31 Ok(Self(Uuid::parse_str(s)?))
32 }
33}
34
35impl From<Uuid> for TransferId {
36 fn from(id: Uuid) -> Self { Self(id) }
37}
38
39impl From<TransferId> for Uuid {
40 fn from(id: TransferId) -> Self { id.0 }
41}
42
43impl AsRef<Uuid> for TransferId {
44 fn as_ref(&self) -> &Uuid { &self.0 }
45}
46
47impl std::ops::Deref for TransferId {
48 type Target = Uuid;
49 fn deref(&self) -> &Self::Target { &self.0 }
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
53pub struct Transfer {
54 pub id: Uuid,
55 pub name: String,
56 pub origin: Option<String>,
57 pub note: Option<String>,
58 pub priority: Priority,
59 pub picking_type_id: Uuid,
60 pub location_id: Uuid,
61 pub location_dest_id: Uuid,
62 pub partner_id: Option<Uuid>,
63 pub move_type: MoveType,
64 pub scheduled_date: DateTime<Utc>,
65 pub date_done: Option<DateTime<Utc>>,
66 pub state: TransferState,
67 pub is_locked: bool,
68 pub backorder_id: Option<Uuid>,
69 pub return_id: Option<Uuid>,
70 pub batch_id: Option<Uuid>,
71 #[serde(default)]
72 #[sqlx(json)]
73 pub metadata: AuditMetadata,
74}
75
76impl Transfer {
77 pub fn builder() -> TransferBuilder {
79 <TransferBuilder as Default>::default()
80 }
81
82 pub fn new(name: String, priority: Priority, picking_type_id: Uuid, location_id: Uuid, location_dest_id: Uuid, move_type: MoveType, scheduled_date: DateTime<Utc>, state: TransferState, is_locked: bool) -> Self {
84 Self {
85 id: Uuid::new_v4(),
86 name,
87 origin: None,
88 note: None,
89 priority,
90 picking_type_id,
91 location_id,
92 location_dest_id,
93 partner_id: None,
94 move_type,
95 scheduled_date,
96 date_done: None,
97 state,
98 is_locked,
99 backorder_id: None,
100 return_id: None,
101 batch_id: None,
102 metadata: AuditMetadata::default(),
103 }
104 }
105
106 pub fn id(&self) -> &Uuid {
108 &self.id
109 }
110
111 pub fn typed_id(&self) -> TransferId {
113 TransferId(self.id)
114 }
115
116 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
118 self.metadata.created_at.as_ref()
119 }
120
121 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
123 self.metadata.updated_at.as_ref()
124 }
125
126 pub fn is_deleted(&self) -> bool {
128 self.metadata.deleted_at.is_some()
129 }
130
131 pub fn is_active(&self) -> bool {
133 self.metadata.deleted_at.is_none()
134 }
135
136 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
138 self.metadata.deleted_at.as_ref()
139 }
140
141 pub fn created_by(&self) -> Option<&Uuid> {
143 self.metadata.created_by.as_ref()
144 }
145
146 pub fn updated_by(&self) -> Option<&Uuid> {
148 self.metadata.updated_by.as_ref()
149 }
150
151 pub fn deleted_by(&self) -> Option<&Uuid> {
153 self.metadata.deleted_by.as_ref()
154 }
155
156
157 pub fn with_origin(mut self, value: String) -> Self {
163 self.origin = Some(value);
164 self
165 }
166
167 pub fn with_note(mut self, value: String) -> Self {
169 self.note = Some(value);
170 self
171 }
172
173 pub fn with_partner_id(mut self, value: Uuid) -> Self {
175 self.partner_id = Some(value);
176 self
177 }
178
179 pub fn with_date_done(mut self, value: DateTime<Utc>) -> Self {
181 self.date_done = Some(value);
182 self
183 }
184
185 pub fn with_backorder_id(mut self, value: Uuid) -> Self {
187 self.backorder_id = Some(value);
188 self
189 }
190
191 pub fn with_return_id(mut self, value: Uuid) -> Self {
193 self.return_id = Some(value);
194 self
195 }
196
197 pub fn with_batch_id(mut self, value: Uuid) -> Self {
199 self.batch_id = Some(value);
200 self
201 }
202
203 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
209 for (key, value) in fields {
210 match key.as_str() {
211 "name" => {
212 if let Ok(v) = serde_json::from_value(value) { self.name = v; }
213 }
214 "origin" => {
215 if let Ok(v) = serde_json::from_value(value) { self.origin = v; }
216 }
217 "note" => {
218 if let Ok(v) = serde_json::from_value(value) { self.note = v; }
219 }
220 "priority" => {
221 if let Ok(v) = serde_json::from_value(value) { self.priority = v; }
222 }
223 "picking_type_id" => {
224 if let Ok(v) = serde_json::from_value(value) { self.picking_type_id = v; }
225 }
226 "location_id" => {
227 if let Ok(v) = serde_json::from_value(value) { self.location_id = v; }
228 }
229 "location_dest_id" => {
230 if let Ok(v) = serde_json::from_value(value) { self.location_dest_id = v; }
231 }
232 "partner_id" => {
233 if let Ok(v) = serde_json::from_value(value) { self.partner_id = v; }
234 }
235 "move_type" => {
236 if let Ok(v) = serde_json::from_value(value) { self.move_type = v; }
237 }
238 "scheduled_date" => {
239 if let Ok(v) = serde_json::from_value(value) { self.scheduled_date = v; }
240 }
241 "date_done" => {
242 if let Ok(v) = serde_json::from_value(value) { self.date_done = v; }
243 }
244 "state" => {
245 if let Ok(v) = serde_json::from_value(value) { self.state = v; }
246 }
247 "is_locked" => {
248 if let Ok(v) = serde_json::from_value(value) { self.is_locked = v; }
249 }
250 "backorder_id" => {
251 if let Ok(v) = serde_json::from_value(value) { self.backorder_id = v; }
252 }
253 "return_id" => {
254 if let Ok(v) = serde_json::from_value(value) { self.return_id = v; }
255 }
256 "batch_id" => {
257 if let Ok(v) = serde_json::from_value(value) { self.batch_id = v; }
258 }
259 _ => {} }
261 }
262 }
263
264 }
267
268impl super::Entity for Transfer {
269 type Id = Uuid;
270
271 fn entity_id(&self) -> &Self::Id {
272 &self.id
273 }
274
275 fn entity_type() -> &'static str {
276 "Transfer"
277 }
278}
279
280impl backbone_core::PersistentEntity for Transfer {
281 fn entity_id(&self) -> String {
282 self.id.to_string()
283 }
284 fn set_entity_id(&mut self, id: String) {
285 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
286 self.id = uuid;
287 }
288 }
289 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
290 self.metadata.created_at
291 }
292 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
293 self.metadata.created_at = Some(ts);
294 }
295 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
296 self.metadata.updated_at
297 }
298 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
299 self.metadata.updated_at = Some(ts);
300 }
301 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
302 self.metadata.deleted_at
303 }
304 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
305 self.metadata.deleted_at = ts;
306 }
307}
308
309impl backbone_orm::EntityRepoMeta for Transfer {
310 fn column_types() -> std::collections::HashMap<String, String> {
311 let mut m = std::collections::HashMap::new();
312 m.insert("id".to_string(), "uuid".to_string());
313 m.insert("picking_type_id".to_string(), "uuid".to_string());
314 m.insert("location_id".to_string(), "uuid".to_string());
315 m.insert("location_dest_id".to_string(), "uuid".to_string());
316 m.insert("partner_id".to_string(), "uuid".to_string());
317 m.insert("backorder_id".to_string(), "uuid".to_string());
318 m.insert("return_id".to_string(), "uuid".to_string());
319 m.insert("batch_id".to_string(), "uuid".to_string());
320 m.insert("priority".to_string(), "priority".to_string());
321 m.insert("move_type".to_string(), "move_type".to_string());
322 m.insert("state".to_string(), "transfer_state".to_string());
323 m.insert("scheduled_date".to_string(), "timestamptz".to_string());
324 m.insert("date_done".to_string(), "timestamptz".to_string());
325 m
326 }
327 fn search_fields() -> &'static [&'static str] {
328 &["name"]
329 }
330}
331
332#[derive(Debug, Clone, Default)]
337pub struct TransferBuilder {
338 name: Option<String>,
339 origin: Option<String>,
340 note: Option<String>,
341 priority: Option<Priority>,
342 picking_type_id: Option<Uuid>,
343 location_id: Option<Uuid>,
344 location_dest_id: Option<Uuid>,
345 partner_id: Option<Uuid>,
346 move_type: Option<MoveType>,
347 scheduled_date: Option<DateTime<Utc>>,
348 date_done: Option<DateTime<Utc>>,
349 state: Option<TransferState>,
350 is_locked: Option<bool>,
351 backorder_id: Option<Uuid>,
352 return_id: Option<Uuid>,
353 batch_id: Option<Uuid>,
354}
355
356impl TransferBuilder {
357 pub fn name(mut self, value: String) -> Self {
359 self.name = Some(value);
360 self
361 }
362
363 pub fn origin(mut self, value: String) -> Self {
365 self.origin = Some(value);
366 self
367 }
368
369 pub fn note(mut self, value: String) -> Self {
371 self.note = Some(value);
372 self
373 }
374
375 pub fn priority(mut self, value: Priority) -> Self {
377 self.priority = Some(value);
378 self
379 }
380
381 pub fn picking_type_id(mut self, value: Uuid) -> Self {
383 self.picking_type_id = Some(value);
384 self
385 }
386
387 pub fn location_id(mut self, value: Uuid) -> Self {
389 self.location_id = Some(value);
390 self
391 }
392
393 pub fn location_dest_id(mut self, value: Uuid) -> Self {
395 self.location_dest_id = Some(value);
396 self
397 }
398
399 pub fn partner_id(mut self, value: Uuid) -> Self {
401 self.partner_id = Some(value);
402 self
403 }
404
405 pub fn move_type(mut self, value: MoveType) -> Self {
407 self.move_type = Some(value);
408 self
409 }
410
411 pub fn scheduled_date(mut self, value: DateTime<Utc>) -> Self {
413 self.scheduled_date = Some(value);
414 self
415 }
416
417 pub fn date_done(mut self, value: DateTime<Utc>) -> Self {
419 self.date_done = Some(value);
420 self
421 }
422
423 pub fn state(mut self, value: TransferState) -> Self {
425 self.state = Some(value);
426 self
427 }
428
429 pub fn is_locked(mut self, value: bool) -> Self {
431 self.is_locked = Some(value);
432 self
433 }
434
435 pub fn backorder_id(mut self, value: Uuid) -> Self {
437 self.backorder_id = Some(value);
438 self
439 }
440
441 pub fn return_id(mut self, value: Uuid) -> Self {
443 self.return_id = Some(value);
444 self
445 }
446
447 pub fn batch_id(mut self, value: Uuid) -> Self {
449 self.batch_id = Some(value);
450 self
451 }
452
453 pub fn build(self) -> Result<Transfer, String> {
457 let picking_type_id = self.picking_type_id.ok_or_else(|| "picking_type_id is required".to_string())?;
458 let location_id = self.location_id.ok_or_else(|| "location_id is required".to_string())?;
459 let location_dest_id = self.location_dest_id.ok_or_else(|| "location_dest_id is required".to_string())?;
460
461 Ok(Transfer {
462 id: Uuid::new_v4(),
463 name: self.name.unwrap_or("/".to_string()),
464 origin: self.origin,
465 note: self.note,
466 priority: self.priority.unwrap_or_default(),
467 picking_type_id,
468 location_id,
469 location_dest_id,
470 partner_id: self.partner_id,
471 move_type: self.move_type.unwrap_or_default(),
472 scheduled_date: self.scheduled_date.unwrap_or(Utc::now()),
473 date_done: self.date_done,
474 state: self.state.unwrap_or_default(),
475 is_locked: self.is_locked.unwrap_or(true),
476 backorder_id: self.backorder_id,
477 return_id: self.return_id,
478 batch_id: self.batch_id,
479 metadata: AuditMetadata::default(),
480 })
481 }
482}