fix billing log

This commit is contained in:
Christoph Beckmann 2025-03-21 19:48:45 +01:00
parent 01b9145978
commit cd23f8ef19
3 changed files with 55 additions and 33 deletions

View File

@ -51,7 +51,10 @@ fn print_config() {
let slaves_by_master: &HashMap<_, _> = &SLAVES_BY_MASTER; let slaves_by_master: &HashMap<_, _> = &SLAVES_BY_MASTER;
let slave_properties: &HashMap<_, _> = &SLAVE_PROPERTIES; let slave_properties: &HashMap<_, _> = &SLAVE_PROPERTIES;
let machine_ids: &HashMap<_, _> = &MACHINE_IDS; let machine_ids: &HashMap<_, _> = &MACHINE_IDS;
dark_grey_ln!("{slaves_by_master:#?}{slave_properties:#?}{machine_ids:#?}");
let data_machine: &HashMap<_, _> = &utils::logs::billing::DATA_MACHINES;
let data_user: &HashMap<_, _> = &utils::logs::billing::DATA_USER;
dark_grey_ln!("{slaves_by_master:#?}{slave_properties:#?}{machine_ids:#?}{data_machine:#?}{data_user:#?}");
} }
async fn create_client() -> (AsyncClient, EventLoop) { async fn create_client() -> (AsyncClient, EventLoop) {

View File

@ -11,7 +11,7 @@ use crate::utils::booking::Booking;
use self::billing::billinglog; use self::billing::billinglog;
mod billing; pub mod billing;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct Record<'s> { struct Record<'s> {
@ -40,7 +40,7 @@ pub fn machinelog(machine: &str, booking: &Booking) -> io::Result<()> {
let file_writer = File::options() let file_writer = File::options()
.create(true) .create(true)
.append(true) .append(true)
.open("/root/machinelog.csv")?; .open("machinelog.csv")?;
WriterBuilder::new() WriterBuilder::new()
.has_headers(false) .has_headers(false)
@ -56,7 +56,7 @@ pub fn log_start() -> io::Result<()> {
File::options() File::options()
.create(true) .create(true)
.append(true) .append(true)
.open("/root/machinelog_debug.csv")? .open("machinelog_debug.csv")?
.write_all(format!("\n\n===== startup {} =====\n\n", Local::now()).as_bytes()) .write_all(format!("\n\n===== startup {} =====\n\n", Local::now()).as_bytes())
} }
@ -80,6 +80,6 @@ result: {result}",
File::options() File::options()
.append(true) .append(true)
.open("/root/machinelog_debug.csv")? .open("machinelog_debug.csv")?
.write_all(record.as_bytes()) .write_all(record.as_bytes())
} }

View File

@ -11,29 +11,38 @@ use serde::Serialize;
use crate::utils::booking::Booking; use crate::utils::booking::Booking;
struct MachineData { #[derive(Debug, Clone)]
id: i32, pub struct UserData {
id: Option<i32>,
to_be_used: bool
}
#[derive(Debug, Clone)]
pub struct MachineData {
id: Option<i32>,
to_be_used: bool, to_be_used: bool,
power_sense: bool, power_sense: bool,
divider: i32 divider: i32
} }
lazy_static! { lazy_static! {
static ref DATA_USER: HashMap<String, String> = fs::read_to_string("DataUser.csv") pub static ref DATA_USER: HashMap<String, UserData> = fs::read_to_string("DataUser.csv")
.expect("failed to open DataUser.csv") .expect("failed to open DataUser.csv")
.lines() .lines()
.map(|line| { .map(|line| {
let (name, id) = line let mut splits = line.split(',');
.split_once(',')
.unwrap();
( let name = splits.next().unwrap().to_string();
name.to_string(),
id.to_string() let ud = UserData {
) id : splits.next().unwrap().parse ().ok(),
to_be_used: splits.next().unwrap().parse::<i32>().unwrap_or(1) == 1,
};
(name, ud)
}) })
.collect(); .collect();
static ref DATA_MACHINES: HashMap<String, MachineData> = fs::read_to_string("DataMachines.csv") pub static ref DATA_MACHINES: HashMap<String, MachineData> = fs::read_to_string("DataMachines.csv")
.expect("failed to open DataMachines.csv") .expect("failed to open DataMachines.csv")
.lines() .lines()
.map(|line| { .map(|line| {
@ -41,9 +50,9 @@ lazy_static! {
let name = splits.next().unwrap().to_string(); let name = splits.next().unwrap().to_string();
let md = MachineData { let md = MachineData {
id : splits.next().unwrap().parse ().unwrap(), id : splits.next().unwrap().parse ().ok(),
to_be_used : splits.next().unwrap().parse::<i32>().unwrap() == 1, to_be_used : splits.next().unwrap().parse::<i32>().unwrap_or(1) == 1,
power_sense: splits.next().unwrap().parse::<i32>().unwrap() == 1, power_sense: splits.next().unwrap().parse::<i32>().unwrap() == 1,
divider : splits.next().unwrap().parse ().unwrap() divider : splits.next().unwrap().parse ().unwrap()
}; };
@ -64,23 +73,33 @@ struct BillingRecord {
} }
pub fn billinglog(machine: &str, booking: &Booking) -> io::Result<()> { pub fn billinglog(machine: &str, booking: &Booking) -> io::Result<()> {
let machine_data = &DATA_MACHINES let user_id =
.get(&machine.to_string()); if let Some(user_data) = &DATA_USER.get(&booking.user.to_string()) {
if !user_data.to_be_used { return Ok(()); }
if !machine_data.is_some_and(|md| !md.to_be_used) { user_data
return Ok(()); .id
} .map(|i| i.to_string())
.unwrap_or(booking.user.to_string())
} else {
booking.user.to_string()
};
let artikel_id =
if let Some(machine_data) = &DATA_MACHINES.get(&machine.to_string()) {
if !machine_data.to_be_used { return Ok(()); }
machine_data
.id
.map(|i| i.to_string())
.unwrap_or(machine.to_string())
} else {
machine.to_string()
};
let bill = BillingRecord { let bill = BillingRecord {
user_id: DATA_USER user_id,
.get(&booking.user)
.unwrap_or(&booking.user)
.clone(),
quelle: "allgemeiner Beleg", quelle: "allgemeiner Beleg",
brutto_netto: 2, brutto_netto: 2,
artikel_id: machine_data artikel_id,
.map(|md| md.id.to_string())
.unwrap_or(machine.to_string()),
positionsdetails: Local::now() positionsdetails: Local::now()
.format("%Y-%m-%d") .format("%Y-%m-%d")
.to_string(), .to_string(),
@ -96,7 +115,7 @@ pub fn billinglog(machine: &str, booking: &Booking) -> io::Result<()> {
let file_writer = File::options() let file_writer = File::options()
.create(true) .create(true)
.append(true) .append(true)
.open("/root/billinglog.csv")?; .open("billinglog.csv")?;
WriterBuilder::new() WriterBuilder::new()
.has_headers(false) .has_headers(false)