Add invoice generator for fuel station transactions
- Read CSV files from input/ directory - Generate static HTML invoices grouped by customer and card - Filter transactions to only include fleet customers - Compact print-friendly layout with 2 decimal precision
This commit is contained in:
175
src/main.rs
Normal file
175
src/main.rs
Normal file
@@ -0,0 +1,175 @@
|
||||
use askama::Template;
|
||||
use chrono::Utc;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
mod invoice_generator;
|
||||
|
||||
use invoice_generator::{group_by_customer, read_csv_file, Batch, Customer};
|
||||
|
||||
fn fmt(v: f64) -> String {
|
||||
format!("{:.2}", v)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct CardData {
|
||||
card_number: String,
|
||||
transactions: Vec<FormattedTransaction>,
|
||||
total_amount: String,
|
||||
total_volume: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct FormattedTransaction {
|
||||
date: String,
|
||||
quality_name: String,
|
||||
price: String,
|
||||
volume: String,
|
||||
amount: String,
|
||||
receipt: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct PreparedCustomer {
|
||||
customer_number: String,
|
||||
cards: Vec<CardData>,
|
||||
grand_total: String,
|
||||
}
|
||||
|
||||
impl PreparedCustomer {
|
||||
fn from_customer(customer: Customer) -> Self {
|
||||
let cards: Vec<CardData> = customer
|
||||
.cards
|
||||
.into_iter()
|
||||
.map(|(card_number, transactions)| {
|
||||
let formatted_txs: Vec<FormattedTransaction> = transactions
|
||||
.into_iter()
|
||||
.map(|t| FormattedTransaction {
|
||||
date: t.date.format("%Y-%m-%d %H:%M").to_string(),
|
||||
quality_name: t.quality_name,
|
||||
price: fmt(t.price),
|
||||
volume: fmt(t.volume),
|
||||
amount: fmt(t.amount),
|
||||
receipt: t.receipt,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let total_amount: f64 = formatted_txs
|
||||
.iter()
|
||||
.map(|t| t.amount.parse::<f64>().unwrap())
|
||||
.sum();
|
||||
let total_volume: f64 = formatted_txs
|
||||
.iter()
|
||||
.map(|t| t.volume.parse::<f64>().unwrap())
|
||||
.sum();
|
||||
|
||||
CardData {
|
||||
card_number,
|
||||
transactions: formatted_txs,
|
||||
total_amount: fmt(total_amount),
|
||||
total_volume: fmt(total_volume),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let grand_total: f64 = cards
|
||||
.iter()
|
||||
.map(|c| c.total_amount.parse::<f64>().unwrap())
|
||||
.sum();
|
||||
|
||||
PreparedCustomer {
|
||||
customer_number: customer.customer_number,
|
||||
cards,
|
||||
grand_total: fmt(grand_total),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct IndexTemplate {
|
||||
customers: Vec<(String, usize)>,
|
||||
batches: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "customer.html")]
|
||||
struct CustomerTemplate {
|
||||
customer: PreparedCustomer,
|
||||
batches: Vec<String>,
|
||||
generated_date: String,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let data_dir = Path::new("input");
|
||||
let output_dir = Path::new("output");
|
||||
|
||||
if !output_dir.exists() {
|
||||
fs::create_dir_all(output_dir)?;
|
||||
}
|
||||
|
||||
let mut batches: Vec<Batch> = Vec::new();
|
||||
|
||||
for entry in fs::read_dir(data_dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.extension().and_then(|s| s.to_str()) == Some("csv") {
|
||||
match read_csv_file(&path) {
|
||||
Ok(batch) => {
|
||||
println!(
|
||||
"Loaded {} transactions from {}",
|
||||
batch.transactions.len(),
|
||||
batch.filename
|
||||
);
|
||||
batches.push(batch);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading {:?}: {}", path, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
batches.sort_by(|a, b| a.filename.cmp(&b.filename));
|
||||
|
||||
let batch_filenames: Vec<String> = batches.iter().map(|b| b.filename.clone()).collect();
|
||||
|
||||
let customers = group_by_customer(&batches);
|
||||
|
||||
let index_customers: Vec<(String, usize)> = customers
|
||||
.iter()
|
||||
.map(|(num, c)| (num.clone(), c.cards.len()))
|
||||
.collect();
|
||||
|
||||
let html = IndexTemplate {
|
||||
customers: index_customers,
|
||||
batches: batch_filenames.clone(),
|
||||
}
|
||||
.render()
|
||||
.unwrap();
|
||||
fs::write(output_dir.join("index.html"), html)?;
|
||||
|
||||
let generated_date = Utc::now().format("%Y-%m-%d %H:%M").to_string();
|
||||
|
||||
let customer_count = customers.len();
|
||||
for (customer_num, customer) in customers {
|
||||
let prepared = PreparedCustomer::from_customer(customer);
|
||||
let customer_html = CustomerTemplate {
|
||||
customer: prepared,
|
||||
batches: batch_filenames.clone(),
|
||||
generated_date: generated_date.clone(),
|
||||
}
|
||||
.render()
|
||||
.unwrap();
|
||||
let filename = format!("customer_{}.html", customer_num);
|
||||
fs::write(output_dir.join(&filename), customer_html)?;
|
||||
println!("Generated {}", filename);
|
||||
}
|
||||
|
||||
println!(
|
||||
"\nGenerated {} customer invoices in output/",
|
||||
customer_count
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user