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};
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,
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);
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,
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,
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,
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>,
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()
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)
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()
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)
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)
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)
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()?;
95 6b2c26f4 2023-12-19 benni if year < 1000 || year > 9999 || month < 1 || month > 12 {
96 6b2c26f4 2023-12-19 benni return None;
99 6b2c26f4 2023-12-19 benni Some(InvoiceId {
105 6b2c26f4 2023-12-19 benni parse(s).ok_or(())