group families in fee calc
All checks were successful
CI/CD Pipeline / test (push) Successful in 9m35s
CI/CD Pipeline / deploy-staging (push) Successful in 4m11s
CI/CD Pipeline / deploy-main (push) Has been skipped

This commit is contained in:
philipp 2024-01-19 08:02:09 +01:00
parent 519cd1985d
commit e498b4be3b
3 changed files with 27 additions and 7 deletions

View File

@ -142,10 +142,15 @@ impl User {
let mut fee = Fee::new(); let mut fee = Fee::new();
if let Some(family) = Family::find_by_opt_id(db, self.family_id).await { if let Some(family) = Family::find_by_opt_id(db, self.family_id).await {
fee.name(format!("{} + Familie", self.name)); let mut names = String::new();
for member in family.members(db).await { for member in family.members(db).await {
if !names.is_empty() {
names.push_str(" + ");
}
names.push_str(&member.name);
fee.merge(member.fee_without_families(db).await); fee.merge(member.fee_without_families(db).await);
} }
fee.name(names);
if family.amount_family_members(db).await > 2 { if family.amount_family_members(db).await > 2 {
fee.add("Familie 3+ Personen".into(), FAMILY_THREE_OR_MORE); fee.add("Familie 3+ Personen".into(), FAMILY_THREE_OR_MORE);
} else { } else {
@ -357,6 +362,26 @@ ORDER BY last_access DESC
.unwrap() .unwrap()
} }
pub async fn all_payer_groups(db: &SqlitePool) -> Vec<Self> {
sqlx::query_as!(
Self,
"
SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user
WHERE family_id IS NOT NULL
GROUP BY family_id
UNION
-- Select users with a null family_id, without grouping
SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user
WHERE family_id IS NULL;
"
)
.fetch_all(db)
.await
.unwrap()
}
pub async fn ergo(db: &SqlitePool) -> Vec<Self> { pub async fn ergo(db: &SqlitePool) -> Vec<Self> {
sqlx::query_as!( sqlx::query_as!(
Self, Self,

View File

@ -56,7 +56,7 @@ async fn fees(
) -> Template { ) -> Template {
let mut context = Context::new(); let mut context = Context::new();
let users = User::all(db).await; let users = User::all_payer_groups(db).await;
let mut fees = Vec::new(); let mut fees = Vec::new();
for user in users { for user in users {
if let Some(fee) = user.fee(db).await { if let Some(fee) = user.fee(db).await {

View File

@ -1,5 +0,0 @@
CREATE TABLE IF NOT EXISTS "family" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT
);
ALTER TABLE "user" ADD COLUMN "family_id" INTEGER REFERENCES family(id);