Blame


1 6b2c26f4 2023-12-19 benni use std::{fmt::{self, Display, Formatter}, str::FromStr};
2 6b2c26f4 2023-12-19 benni use chrono::NaiveDate;
3 6b2c26f4 2023-12-19 benni use serde::{Serialize, Deserialize};
4 6b2c26f4 2023-12-19 benni
5 6b2c26f4 2023-12-19 benni #[derive(Debug, Default, Clone, Serialize, Deserialize)]
6 6b2c26f4 2023-12-19 benni pub struct Business {
7 6b2c26f4 2023-12-19 benni pub name: String,
8 6b2c26f4 2023-12-19 benni pub owner_name: String,
9 6b2c26f4 2023-12-19 benni pub address: String,
10 6b2c26f4 2023-12-19 benni pub zip_city: String,
11 6b2c26f4 2023-12-19 benni pub email: String,
12 6b2c26f4 2023-12-19 benni pub iban: String,
13 6b2c26f4 2023-12-19 benni pub bic: String,
14 6b2c26f4 2023-12-19 benni pub bank: String,
15 6b2c26f4 2023-12-19 benni pub ustid: String,
16 6b2c26f4 2023-12-19 benni }
17 6b2c26f4 2023-12-19 benni
18 6b2c26f4 2023-12-19 benni #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
19 6b2c26f4 2023-12-19 benni pub struct CustomerId(pub usize);
20 6b2c26f4 2023-12-19 benni
21 6b2c26f4 2023-12-19 benni #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
22 6b2c26f4 2023-12-19 benni pub struct Customer {
23 6b2c26f4 2023-12-19 benni pub id: CustomerId,
24 6b2c26f4 2023-12-19 benni pub name: String,
25 6b2c26f4 2023-12-19 benni pub address: String,
26 6b2c26f4 2023-12-19 benni pub zip_city: String,
27 6b2c26f4 2023-12-19 benni }
28 6b2c26f4 2023-12-19 benni
29 6b2c26f4 2023-12-19 benni #[derive(Debug, Clone, Serialize, Deserialize)]
30 6b2c26f4 2023-12-19 benni pub struct Record {
31 6b2c26f4 2023-12-19 benni pub description: String,
32 6b2c26f4 2023-12-19 benni pub date: NaiveDate,
33 6b2c26f4 2023-12-19 benni pub count: f32,
34 6b2c26f4 2023-12-19 benni pub price: f32,
35 6b2c26f4 2023-12-19 benni }
36 6b2c26f4 2023-12-19 benni
37 6b2c26f4 2023-12-19 benni #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
38 6b2c26f4 2023-12-19 benni pub struct InvoiceId {
39 6b2c26f4 2023-12-19 benni pub year: u16,
40 6b2c26f4 2023-12-19 benni pub month: u8,
41 6b2c26f4 2023-12-19 benni pub n: u32,
42 6b2c26f4 2023-12-19 benni }
43 6b2c26f4 2023-12-19 benni
44 6b2c26f4 2023-12-19 benni #[derive(Debug, Clone, Serialize, Deserialize)]
45 6b2c26f4 2023-12-19 benni pub struct Invoice {
46 6b2c26f4 2023-12-19 benni pub id: InvoiceId,
47 6b2c26f4 2023-12-19 benni pub date: NaiveDate,
48 6b2c26f4 2023-12-19 benni pub business: Business,
49 6b2c26f4 2023-12-19 benni pub customer: Customer,
50 6b2c26f4 2023-12-19 benni pub records: Vec<Record>,
51 6b2c26f4 2023-12-19 benni }
52 6b2c26f4 2023-12-19 benni
53 6b2c26f4 2023-12-19 benni impl Record {
54 6b2c26f4 2023-12-19 benni pub fn date(&self) -> String {
55 6b2c26f4 2023-12-19 benni self.date.format("%Y-%m-%d").to_string()
56 6b2c26f4 2023-12-19 benni }
57 6b2c26f4 2023-12-19 benni }
58 6b2c26f4 2023-12-19 benni
59 6b2c26f4 2023-12-19 benni impl Invoice {
60 6b2c26f4 2023-12-19 benni pub fn file_name(&self) -> String {
61 6b2c26f4 2023-12-19 benni format!("invoice-{}.pdf", self.id)
62 6b2c26f4 2023-12-19 benni }
63 6b2c26f4 2023-12-19 benni pub fn date(&self) -> String {
64 6b2c26f4 2023-12-19 benni self.date.format("%Y-%m-%d").to_string()
65 6b2c26f4 2023-12-19 benni }
66 6b2c26f4 2023-12-19 benni }
67 6b2c26f4 2023-12-19 benni
68 6b2c26f4 2023-12-19 benni impl Display for Customer {
69 6b2c26f4 2023-12-19 benni fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
70 6b2c26f4 2023-12-19 benni write!(f, "{} ({})", self.name, self.id)
71 6b2c26f4 2023-12-19 benni }
72 6b2c26f4 2023-12-19 benni }
73 6b2c26f4 2023-12-19 benni
74 6b2c26f4 2023-12-19 benni impl Display for CustomerId {
75 6b2c26f4 2023-12-19 benni fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
76 6b2c26f4 2023-12-19 benni write!(f, "{:03}", self.0)
77 6b2c26f4 2023-12-19 benni }
78 6b2c26f4 2023-12-19 benni }
79 6b2c26f4 2023-12-19 benni
80 6b2c26f4 2023-12-19 benni impl Display for InvoiceId {
81 6b2c26f4 2023-12-19 benni fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
82 6b2c26f4 2023-12-19 benni write!(f, "{:04}-{:02}-{:03}", self.year, self.month, self.n)
83 6b2c26f4 2023-12-19 benni }
84 6b2c26f4 2023-12-19 benni }
85 6b2c26f4 2023-12-19 benni
86 6b2c26f4 2023-12-19 benni impl FromStr for InvoiceId {
87 6b2c26f4 2023-12-19 benni type Err = ();
88 6b2c26f4 2023-12-19 benni fn from_str(s: &str) -> Result<Self, Self::Err> {
89 6b2c26f4 2023-12-19 benni fn parse(s: &str) -> Option<InvoiceId> {
90 6b2c26f4 2023-12-19 benni let mut iter = s.split('-');
91 6b2c26f4 2023-12-19 benni let year = iter.next()?.parse().ok()?;
92 6b2c26f4 2023-12-19 benni let month = iter.next()?.parse().ok()?;
93 6b2c26f4 2023-12-19 benni let n = iter.next()?.parse().ok()?;
94 6b2c26f4 2023-12-19 benni
95 6b2c26f4 2023-12-19 benni if year < 1000 || year > 9999 || month < 1 || month > 12 {
96 6b2c26f4 2023-12-19 benni return None;
97 6b2c26f4 2023-12-19 benni }
98 6b2c26f4 2023-12-19 benni
99 6b2c26f4 2023-12-19 benni Some(InvoiceId {
100 6b2c26f4 2023-12-19 benni year,
101 6b2c26f4 2023-12-19 benni month,
102 6b2c26f4 2023-12-19 benni n,
103 6b2c26f4 2023-12-19 benni })
104 6b2c26f4 2023-12-19 benni }
105 6b2c26f4 2023-12-19 benni parse(s).ok_or(())
106 6b2c26f4 2023-12-19 benni }
107 6b2c26f4 2023-12-19 benni }