quickbooks_types/reports/
params.rs

1use std::borrow::Cow;
2
3use chrono::NaiveDate;
4use serde::{Deserialize, Serialize};
5
6macro_rules! impl_display_enum {
7    (
8      $($doc:literal)?
9      $name:ident => {
10        $(
11            $variant_display:ident;
12        )* -
13        $(
14            $variant:ident => $display:expr
15        ),* $(,)?
16      }
17    ) => {
18        $(#[doc = $doc])?
19        #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
20        pub enum $name {
21            $(
22                $variant_display,
23            )*
24            $(
25                #[serde(rename = $display)]
26                $variant,
27            )*
28        }
29
30        impl $name {
31            /// Returns the string representation of the enum variant.
32            pub fn as_str(&self) -> &str {
33                match self {
34                    $(
35                        $name::$variant_display => stringify!($variant_display),
36                    )*
37                    $(
38                        $name::$variant => $display,
39                    )*
40                }
41            }
42        }
43
44        impl std::fmt::Display for $name {
45            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46                write!(f, "{}", self.as_str())
47            }
48        }
49
50        impl HasValue for $name {
51            fn value(&self) -> Cow<'_, str> {
52                self.as_str().into()
53            }
54        }
55    };
56}
57
58impl_display_enum!(
59  "Represents various predefined date ranges for reports."
60  DateMacro => {
61  All;
62  Today;
63  Yesterday;
64  -
65  ThisWeek => "This Week",
66  ThisWeekToDate => "This Week-to-date",
67  ThisMonth => "This Month",
68  ThisMonthToDate => "This Month-to-date",
69  ThisFiscalQuarter => "This Fiscal Quarter",
70  ThisFiscalQuarterToDate => "This Fiscal Quarter-to-date",
71  ThisFiscalYear => "This Fiscal Year",
72  ThisFiscalYearToDate => "This Fiscal Year-to-date",
73  ThisCalendarQuarter => "This Calendar Quarter",
74  ThisCalendarQuarterToDate => "This Calendar Quarter-to-date",
75  ThisCalendarYear => "This Calendar Year",
76  ThisCalendarYearToDate => "This Calendar Year-to-date",
77  LastWeek => "Last Week",
78  LastWeekToDate => "Last Week-to-date",
79  LastMonth => "Last Month",
80  LastMonthToDate => "Last Month-to-date",
81  LastFiscalQuarter => "Last Fiscal Quarter",
82  LastFiscalQuarterToDate => "Last Fiscal Quarter-to-date",
83  LastFiscalYear => "Last Fiscal Year",
84  LastFiscalYearToDate => "Last Fiscal Year-to-date",
85  LastCalendarQuarter => "Last Calendar Quarter",
86  LastCalendarQuarterToDate => "Last Calendar Quarter-to-date",
87  LastCalendarYear => "Last Calendar Year",
88  LastCalendarYearToDate => "Last Calendar Year-to-date",
89  NextWeek => "Next Week",
90  Next4Weeks => "Next 4 Weeks",
91  NextMonth => "Next Month",
92  NextFiscalQuarter => "Next Fiscal Quarter",
93  NextFiscalYear => "Next Fiscal Year",
94  NextCalendarQuarter => "Next Calendar Quarter",
95  NextCalendarYear => "Next Calendar Year"
96});
97
98impl_display_enum!(
99  "Represents how columns are summarized in reports."
100  SummarizeColumnBy => {
101    Total;
102    Year;
103    Quarter;
104    FiscalYear;
105    FiscalQuarter;
106    Month;
107    Week;
108    Days;
109    Customers;
110    Vendors;
111    Employees;
112    Departments;
113    Classes;
114    ProductsAndServices;
115    -
116});
117impl_display_enum!(
118  "Represents different aging methods for reports."
119  AgingMethod => {
120    Current;
121    -
122    ReportDate => "Report_Date",
123});
124
125impl_display_enum!(
126  "Represents different accounting methods for reports."
127  AccountingMethod => {
128    Cash;
129    Accrual;
130    -
131});
132
133impl_display_enum!(
134  "Represents different sort orders for reports."
135  SortOrder => {
136    Ascending;
137    Descending;
138    -
139});
140
141impl_display_enum!(
142  "Represents Paid status for AR reports."
143  ArPaid => {
144    All;
145    Paid;
146    Unpaid;
147    -
148});
149
150impl_display_enum!(
151  "Represents different attachment types for reports."
152  AttachmentType => {
153    -
154    TemporaryLinks => "TEMPORARY_LINKS",
155    None => "NONE",
156});
157
158impl_display_enum!(
159  "Filters report contents to include information for specified check status."
160  Cleared => {
161    Cleared;
162    Uncleared;
163    Reconciled;
164    Deposited;
165    -
166});
167
168impl_display_enum!(
169  "Represents whether a report was printed."
170  Printed => {
171    Printed;
172    -
173    ToBePrinted => "To_be_printed",
174});
175
176macro_rules! impl_id_param {
177    ($($name:ident),*) => {
178
179      $(
180          paste::paste! {
181            #[derive(Debug, Clone, Copy, PartialEq, Eq)]
182            #[doc = "Represents the ID for a " $name " in QuickBooks reports."]
183            pub struct [<$name Id>](pub u32);
184            impl HasValue for [<$name Id>] {
185                fn value(&self) -> Cow<'_, str> {
186                    self.to_string().into()
187                }
188            }
189
190            impl std::fmt::Display for [<$name Id>] {
191                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
192                    write!(f, "{}", self.0)
193                }
194            }
195          }
196        )*
197    };
198}
199
200impl_id_param!(Customer, Vendor, Employee, Item, Class, Department, Account, Term);
201
202pub trait HasValue {
203    // fn name() -> &'static str;
204    fn value(&self) -> Cow<'_, str>;
205}
206
207// Implement QBReportParam directly for common types
208impl HasValue for String {
209    fn value(&self) -> Cow<'_, str> {
210        self.into()
211    }
212}
213
214impl HasValue for &str {
215    fn value(&self) -> Cow<'_, str> {
216        (*self).into()
217    }
218}
219
220impl HasValue for u32 {
221    fn value(&self) -> Cow<'_, str> {
222        self.to_string().into()
223    }
224}
225
226impl HasValue for NaiveDate {
227    fn value(&self) -> Cow<'_, str> {
228        self.format("%Y-%m-%d").to_string().into()
229    }
230}
231
232impl HasValue for bool {
233    fn value(&self) -> Cow<'_, str> {
234        if *self { "true" } else { "false" }.into()
235    }
236}
237
238impl<V: HasValue> HasValue for Vec<V> {
239    fn value(&self) -> Cow<'_, str> {
240        self.iter()
241            .map(|v| v.value())
242            .collect::<Vec<_>>()
243            .join(",")
244            .into()
245    }
246}