Skip to main content

lago_types/requests/
credit_note.rs

1use serde::{Deserialize, Serialize};
2
3use crate::filters::common::ListFilters;
4use crate::filters::credit_note::CreditNoteFilter;
5use crate::models::{CreditNoteReason, CreditNoteRefundStatus, PaginationParams};
6
7/// Request parameters for listing credit notes.
8#[derive(Debug, Clone)]
9pub struct ListCreditNotesRequest {
10    pub pagination: PaginationParams,
11    pub filters: CreditNoteFilter,
12    /// Search by id, number, customer name, external_id or email.
13    pub search_term: Option<String>,
14}
15
16impl ListCreditNotesRequest {
17    /// Creates a new empty list credit notes request.
18    pub fn new() -> Self {
19        Self {
20            pagination: PaginationParams::default(),
21            filters: CreditNoteFilter::default(),
22            search_term: None,
23        }
24    }
25
26    /// Sets the pagination parameters for the request.
27    pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
28        self.pagination = pagination;
29        self
30    }
31
32    /// Sets the credit note filters for the request.
33    pub fn with_filters(mut self, filters: CreditNoteFilter) -> Self {
34        self.filters = filters;
35        self
36    }
37
38    /// Sets the search term for the request.
39    ///
40    /// Search by id, number, customer name, external_id or email.
41    pub fn with_search_term(mut self, term: String) -> Self {
42        self.search_term = Some(term);
43        self
44    }
45
46    /// Converts the request parameters into HTTP query parameters.
47    pub fn to_query_params(&self) -> Vec<(&str, String)> {
48        let mut params = self.pagination.to_query_params();
49        params.extend(self.filters.to_query_params());
50
51        if let Some(ref term) = self.search_term {
52            params.push(("search_term", term.clone()));
53        }
54
55        params
56    }
57}
58
59impl Default for ListCreditNotesRequest {
60    fn default() -> Self {
61        Self::new()
62    }
63}
64
65/// Request parameters for retrieving a specific credit note.
66#[derive(Debug, Clone)]
67pub struct GetCreditNoteRequest {
68    /// The Lago ID of the credit note to retrieve
69    pub lago_id: String,
70}
71
72impl GetCreditNoteRequest {
73    /// Creates a new get credit note request.
74    pub fn new(lago_id: String) -> Self {
75        Self { lago_id }
76    }
77}
78
79/// Item input for creating a credit note.
80#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct CreateCreditNoteItemInput {
82    /// The Lago ID of the fee to credit
83    pub fee_id: String,
84    /// The amount to credit for this fee in cents
85    pub amount_cents: i64,
86}
87
88impl CreateCreditNoteItemInput {
89    /// Creates a new credit note item input.
90    pub fn new(fee_id: String, amount_cents: i64) -> Self {
91        Self {
92            fee_id,
93            amount_cents,
94        }
95    }
96}
97
98/// Input parameters for creating a credit note.
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct CreateCreditNoteInput {
101    /// The Lago ID of the invoice to credit
102    pub invoice_id: String,
103    /// The reason for the credit note
104    pub reason: CreditNoteReason,
105    /// Optional description
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub description: Option<String>,
108    /// The amount to be credited in cents
109    pub credit_amount_cents: i64,
110    /// The amount to be refunded in cents
111    pub refund_amount_cents: i64,
112    /// The line items for the credit note
113    pub items: Vec<CreateCreditNoteItemInput>,
114}
115
116impl CreateCreditNoteInput {
117    /// Creates a new credit note input.
118    ///
119    /// # Arguments
120    /// * `invoice_id` - The Lago ID of the invoice to credit
121    /// * `reason` - The reason for the credit note
122    /// * `credit_amount_cents` - The amount to be credited in cents
123    /// * `refund_amount_cents` - The amount to be refunded in cents
124    /// * `items` - The line items for the credit note
125    pub fn new(
126        invoice_id: String,
127        reason: CreditNoteReason,
128        credit_amount_cents: i64,
129        refund_amount_cents: i64,
130        items: Vec<CreateCreditNoteItemInput>,
131    ) -> Self {
132        Self {
133            invoice_id,
134            reason,
135            description: None,
136            credit_amount_cents,
137            refund_amount_cents,
138            items,
139        }
140    }
141
142    /// Sets the description for the credit note.
143    pub fn with_description(mut self, description: String) -> Self {
144        self.description = Some(description);
145        self
146    }
147}
148
149/// Request wrapper for creating a credit note.
150#[derive(Debug, Clone, Serialize, Deserialize)]
151pub struct CreateCreditNoteRequest {
152    pub credit_note: CreateCreditNoteInput,
153}
154
155impl CreateCreditNoteRequest {
156    /// Creates a new create credit note request.
157    pub fn new(credit_note: CreateCreditNoteInput) -> Self {
158        Self { credit_note }
159    }
160}
161
162/// Input parameters for updating a credit note.
163#[derive(Debug, Clone, Serialize, Deserialize)]
164pub struct UpdateCreditNoteInput {
165    /// The refund status to update
166    #[serde(skip_serializing_if = "Option::is_none")]
167    pub refund_status: Option<CreditNoteRefundStatus>,
168}
169
170impl UpdateCreditNoteInput {
171    /// Creates a new empty update credit note input.
172    pub fn new() -> Self {
173        Self {
174            refund_status: None,
175        }
176    }
177
178    /// Sets the refund status.
179    pub fn with_refund_status(mut self, status: CreditNoteRefundStatus) -> Self {
180        self.refund_status = Some(status);
181        self
182    }
183}
184
185impl Default for UpdateCreditNoteInput {
186    fn default() -> Self {
187        Self::new()
188    }
189}
190
191/// Request wrapper for updating a credit note.
192#[derive(Debug, Clone, Serialize, Deserialize)]
193pub struct UpdateCreditNoteRequest {
194    /// The Lago ID of the credit note to update
195    #[serde(skip)]
196    pub lago_id: String,
197    /// The update input
198    pub credit_note: UpdateCreditNoteInput,
199}
200
201impl UpdateCreditNoteRequest {
202    /// Creates a new update credit note request.
203    pub fn new(lago_id: String, input: UpdateCreditNoteInput) -> Self {
204        Self {
205            lago_id,
206            credit_note: input,
207        }
208    }
209}