backbone_pos/domain/entity/
pos_opening_entry.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5
6use super::PosSessionStatus;
7use super::AuditMetadata;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct PosOpeningEntryId(pub Uuid);
13
14impl PosOpeningEntryId {
15 pub fn new(id: Uuid) -> Self { Self(id) }
16 pub fn generate() -> Self { Self(Uuid::new_v4()) }
17 pub fn into_inner(self) -> Uuid { self.0 }
18}
19
20impl std::fmt::Display for PosOpeningEntryId {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 write!(f, "{}", self.0)
23 }
24}
25
26impl std::str::FromStr for PosOpeningEntryId {
27 type Err = uuid::Error;
28 fn from_str(s: &str) -> Result<Self, Self::Err> {
29 Ok(Self(Uuid::parse_str(s)?))
30 }
31}
32
33impl From<Uuid> for PosOpeningEntryId {
34 fn from(id: Uuid) -> Self { Self(id) }
35}
36
37impl From<PosOpeningEntryId> for Uuid {
38 fn from(id: PosOpeningEntryId) -> Self { id.0 }
39}
40
41impl AsRef<Uuid> for PosOpeningEntryId {
42 fn as_ref(&self) -> &Uuid { &self.0 }
43}
44
45impl std::ops::Deref for PosOpeningEntryId {
46 type Target = Uuid;
47 fn deref(&self) -> &Self::Target { &self.0 }
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
51pub struct PosOpeningEntry {
52 pub id: Uuid,
53 pub pos_profile_id: Uuid,
54 pub branch_id: Option<Uuid>,
55 pub cashier_party_id: Uuid,
56 pub opened_at: DateTime<Utc>,
57 pub opening_balances: Option<serde_json::Value>,
58 pub status: PosSessionStatus,
59 #[serde(default)]
60 #[sqlx(json)]
61 pub metadata: AuditMetadata,
62}
63
64impl PosOpeningEntry {
65 pub fn builder() -> PosOpeningEntryBuilder {
67 <PosOpeningEntryBuilder as Default>::default()
68 }
69
70 pub fn new(pos_profile_id: Uuid, cashier_party_id: Uuid, opened_at: DateTime<Utc>, status: PosSessionStatus) -> Self {
72 Self {
73 id: Uuid::new_v4(),
74 pos_profile_id,
75 branch_id: None,
76 cashier_party_id,
77 opened_at,
78 opening_balances: None,
79 status,
80 metadata: AuditMetadata::default(),
81 }
82 }
83
84 pub fn id(&self) -> &Uuid {
86 &self.id
87 }
88
89 pub fn typed_id(&self) -> PosOpeningEntryId {
91 PosOpeningEntryId(self.id)
92 }
93
94 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
96 self.metadata.created_at.as_ref()
97 }
98
99 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
101 self.metadata.updated_at.as_ref()
102 }
103
104 pub fn is_deleted(&self) -> bool {
106 self.metadata.deleted_at.is_some()
107 }
108
109 pub fn is_active(&self) -> bool {
111 self.metadata.deleted_at.is_none()
112 }
113
114 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
116 self.metadata.deleted_at.as_ref()
117 }
118
119 pub fn created_by(&self) -> Option<&Uuid> {
121 self.metadata.created_by.as_ref()
122 }
123
124 pub fn updated_by(&self) -> Option<&Uuid> {
126 self.metadata.updated_by.as_ref()
127 }
128
129 pub fn deleted_by(&self) -> Option<&Uuid> {
131 self.metadata.deleted_by.as_ref()
132 }
133
134 pub fn status(&self) -> &PosSessionStatus {
136 &self.status
137 }
138
139
140 pub fn with_branch_id(mut self, value: Uuid) -> Self {
146 self.branch_id = Some(value);
147 self
148 }
149
150 pub fn with_opening_balances(mut self, value: serde_json::Value) -> Self {
152 self.opening_balances = Some(value);
153 self
154 }
155
156 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
162 for (key, value) in fields {
163 match key.as_str() {
164 "pos_profile_id" => {
165 if let Ok(v) = serde_json::from_value(value) { self.pos_profile_id = v; }
166 }
167 "branch_id" => {
168 if let Ok(v) = serde_json::from_value(value) { self.branch_id = v; }
169 }
170 "cashier_party_id" => {
171 if let Ok(v) = serde_json::from_value(value) { self.cashier_party_id = v; }
172 }
173 "opened_at" => {
174 if let Ok(v) = serde_json::from_value(value) { self.opened_at = v; }
175 }
176 "opening_balances" => {
177 if let Ok(v) = serde_json::from_value(value) { self.opening_balances = v; }
178 }
179 "status" => {
180 if let Ok(v) = serde_json::from_value(value) { self.status = v; }
181 }
182 _ => {} }
184 }
185 }
186
187}
188
189impl super::Entity for PosOpeningEntry {
190 type Id = Uuid;
191
192 fn entity_id(&self) -> &Self::Id {
193 &self.id
194 }
195
196 fn entity_type() -> &'static str {
197 "PosOpeningEntry"
198 }
199}
200
201impl backbone_core::PersistentEntity for PosOpeningEntry {
202 fn entity_id(&self) -> String {
203 self.id.to_string()
204 }
205 fn set_entity_id(&mut self, id: String) {
206 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
207 self.id = uuid;
208 }
209 }
210 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
211 self.metadata.created_at
212 }
213 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
214 self.metadata.created_at = Some(ts);
215 }
216 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
217 self.metadata.updated_at
218 }
219 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
220 self.metadata.updated_at = Some(ts);
221 }
222 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
223 self.metadata.deleted_at
224 }
225 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
226 self.metadata.deleted_at = ts;
227 }
228}
229
230impl backbone_orm::EntityRepoMeta for PosOpeningEntry {
231 fn column_types() -> std::collections::HashMap<String, String> {
232 let mut m = std::collections::HashMap::new();
233 m.insert("id".to_string(), "uuid".to_string());
234 m.insert("pos_profile_id".to_string(), "uuid".to_string());
235 m.insert("branch_id".to_string(), "uuid".to_string());
236 m.insert("cashier_party_id".to_string(), "uuid".to_string());
237 m.insert("status".to_string(), "pos_session_status".to_string());
238 m.insert("opened_at".to_string(), "timestamptz".to_string());
239 m
240 }
241 fn search_fields() -> &'static [&'static str] {
242 &[]
243 }
244}
245
246#[derive(Debug, Clone, Default)]
251pub struct PosOpeningEntryBuilder {
252 pos_profile_id: Option<Uuid>,
253 branch_id: Option<Uuid>,
254 cashier_party_id: Option<Uuid>,
255 opened_at: Option<DateTime<Utc>>,
256 opening_balances: Option<serde_json::Value>,
257 status: Option<PosSessionStatus>,
258}
259
260impl PosOpeningEntryBuilder {
261 pub fn pos_profile_id(mut self, value: Uuid) -> Self {
263 self.pos_profile_id = Some(value);
264 self
265 }
266
267 pub fn branch_id(mut self, value: Uuid) -> Self {
269 self.branch_id = Some(value);
270 self
271 }
272
273 pub fn cashier_party_id(mut self, value: Uuid) -> Self {
275 self.cashier_party_id = Some(value);
276 self
277 }
278
279 pub fn opened_at(mut self, value: DateTime<Utc>) -> Self {
281 self.opened_at = Some(value);
282 self
283 }
284
285 pub fn opening_balances(mut self, value: serde_json::Value) -> Self {
287 self.opening_balances = Some(value);
288 self
289 }
290
291 pub fn status(mut self, value: PosSessionStatus) -> Self {
293 self.status = Some(value);
294 self
295 }
296
297 pub fn build(self) -> Result<PosOpeningEntry, String> {
301 let pos_profile_id = self.pos_profile_id.ok_or_else(|| "pos_profile_id is required".to_string())?;
302 let cashier_party_id = self.cashier_party_id.ok_or_else(|| "cashier_party_id is required".to_string())?;
303 let opened_at = self.opened_at.ok_or_else(|| "opened_at is required".to_string())?;
304
305 Ok(PosOpeningEntry {
306 id: Uuid::new_v4(),
307 pos_profile_id,
308 branch_id: self.branch_id,
309 cashier_party_id,
310 opened_at,
311 opening_balances: self.opening_balances,
312 status: self.status.unwrap_or_default(),
313 metadata: AuditMetadata::default(),
314 })
315 }
316}