forked from Ruderverein-Donau-Linz/rowt
59 lines
1.2 KiB
Rust
59 lines
1.2 KiB
Rust
use super::User;
|
|
use serde::Serialize;
|
|
|
|
#[derive(Debug, Serialize)]
|
|
pub struct Fee {
|
|
pub sum_in_cents: i64,
|
|
pub parts: Vec<(String, i64)>,
|
|
pub name: String,
|
|
pub user_ids: String,
|
|
pub paid: bool,
|
|
pub users: Vec<User>,
|
|
}
|
|
|
|
impl Default for Fee {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl Fee {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
sum_in_cents: 0,
|
|
name: "".into(),
|
|
parts: Vec::new(),
|
|
user_ids: "".into(),
|
|
users: Vec::new(),
|
|
paid: false,
|
|
}
|
|
}
|
|
|
|
pub fn add(&mut self, desc: String, price_in_cents: i64) {
|
|
self.sum_in_cents += price_in_cents;
|
|
|
|
self.parts.push((desc, price_in_cents));
|
|
}
|
|
|
|
pub fn add_person(&mut self, user: &User) {
|
|
if !self.name.is_empty() {
|
|
self.name.push_str(" + ");
|
|
self.user_ids.push('&');
|
|
}
|
|
self.name.push_str(&user.name);
|
|
|
|
self.user_ids.push_str(&format!("user_ids[]={}", user.id));
|
|
self.users.push(user.clone());
|
|
}
|
|
|
|
pub fn paid(&mut self) {
|
|
self.paid = true;
|
|
}
|
|
|
|
pub fn merge(&mut self, fee: Fee) {
|
|
for (desc, price_in_cents) in fee.parts {
|
|
self.add(desc, price_in_cents);
|
|
}
|
|
}
|
|
}
|