backbone_inventory/application/service/
inventory_intake.rs1use rust_decimal::Decimal;
16use serde::{Deserialize, Serialize};
17use sqlx::PgPool;
18use uuid::Uuid;
19
20use super::inventory_write_service::{
21 DeliveryLine, InventoryError, InventoryWriteService, NewDelivery, NewReceipt, ReceiptLine,
22};
23
24fn default_currency() -> String { "IDR".into() }
27
28#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
29pub struct DeliveryRequestLine {
30 pub item_id: Uuid,
31 pub quantity: Decimal,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
36pub struct DeliveryRequested {
37 pub delivery_number: String,
38 pub branch_id: Option<Uuid>,
39 pub customer_id: Uuid,
40 pub source_so_id: Option<Uuid>,
41 pub warehouse_id: Uuid,
42 pub posting_date: chrono::NaiveDate,
43 #[serde(default = "default_currency")]
45 pub currency: String,
46 pub cogs_account_id: Uuid,
48 pub inventory_account_id: Uuid,
49 pub lines: Vec<DeliveryRequestLine>,
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
54pub struct ReceiptRequestLine {
55 pub item_id: Uuid,
56 pub quantity: Decimal,
57 pub rate: Decimal,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
63pub struct ReceiptExpected {
64 pub receipt_number: String,
65 pub branch_id: Option<Uuid>,
66 pub supplier_id: Uuid,
67 pub source_po_id: Option<Uuid>,
68 pub warehouse_id: Uuid,
69 pub posting_date: chrono::NaiveDate,
70 #[serde(default = "default_currency")]
72 pub currency: String,
73 pub inventory_account_id: Uuid,
74 pub grir_account_id: Uuid,
75 pub lines: Vec<ReceiptRequestLine>,
76}
77
78#[derive(Clone)]
80pub struct DeliveryIntake {
81 write: InventoryWriteService,
82}
83
84impl DeliveryIntake {
85 pub fn new(pool: PgPool) -> Self {
86 Self { write: InventoryWriteService::new(pool) }
87 }
88
89 pub async fn on_delivery_requested(&self, req: DeliveryRequested) -> Result<Uuid, InventoryError> {
92 self.write.create_delivery_note(NewDelivery {
93 delivery_number: req.delivery_number,
94 branch_id: req.branch_id,
95 customer_id: req.customer_id,
96 source_so_id: req.source_so_id,
97 warehouse_id: req.warehouse_id,
98 posting_date: req.posting_date,
99 currency: req.currency,
100 cogs_account_id: req.cogs_account_id,
101 inventory_account_id: req.inventory_account_id,
102 lines: req.lines.into_iter().map(|l| DeliveryLine { item_id: l.item_id, quantity: l.quantity }).collect(),
103 }).await
104 }
105
106 pub async fn on_receipt_expected(&self, req: ReceiptExpected) -> Result<Uuid, InventoryError> {
110 self.write.create_purchase_receipt(NewReceipt {
111 receipt_number: req.receipt_number,
112 branch_id: req.branch_id,
113 supplier_id: req.supplier_id,
114 source_po_id: req.source_po_id,
115 warehouse_id: req.warehouse_id,
116 posting_date: req.posting_date,
117 currency: req.currency,
118 inventory_account_id: req.inventory_account_id,
119 grir_account_id: req.grir_account_id,
120 lines: req.lines.into_iter().map(|l| ReceiptLine { item_id: l.item_id, quantity: l.quantity, rate: l.rate, is_landed_costs_line: false }).collect(),
123 }).await
124 }
125}