Remove PDF generation
Revert PDF generation feature - will use external tool for HTML to PDF conversion.
This commit is contained in:
@@ -7,5 +7,4 @@ edition = "2021"
|
|||||||
askama = "0.15.5"
|
askama = "0.15.5"
|
||||||
chrono = "0.4.44"
|
chrono = "0.4.44"
|
||||||
csv = "1.4.0"
|
csv = "1.4.0"
|
||||||
printpdf = "0.9.1"
|
|
||||||
serde = "1.0.228"
|
serde = "1.0.228"
|
||||||
|
|||||||
62
src/main.rs
62
src/main.rs
@@ -6,7 +6,6 @@ use std::fs;
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
mod invoice_generator;
|
mod invoice_generator;
|
||||||
mod pdf;
|
|
||||||
|
|
||||||
use invoice_generator::{group_by_customer, read_csv_file, Customer};
|
use invoice_generator::{group_by_customer, read_csv_file, Customer};
|
||||||
|
|
||||||
@@ -15,43 +14,43 @@ fn fmt(v: f64) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ProductSummary {
|
struct ProductSummary {
|
||||||
pub name: String,
|
name: String,
|
||||||
pub volume: String,
|
volume: String,
|
||||||
pub amount: String,
|
amount: String,
|
||||||
pub avg_price: String,
|
avg_price: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Summary {
|
struct Summary {
|
||||||
pub total_volume: String,
|
total_volume: String,
|
||||||
pub grand_total: String,
|
grand_total: String,
|
||||||
pub products: Vec<ProductSummary>,
|
products: Vec<ProductSummary>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct CardData {
|
struct CardData {
|
||||||
pub card_number: String,
|
card_number: String,
|
||||||
pub transactions: Vec<FormattedTransaction>,
|
transactions: Vec<FormattedTransaction>,
|
||||||
pub total_amount: String,
|
total_amount: String,
|
||||||
pub total_volume: String,
|
total_volume: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct FormattedTransaction {
|
struct FormattedTransaction {
|
||||||
pub date: String,
|
date: String,
|
||||||
pub quality_name: String,
|
quality_name: String,
|
||||||
pub price: String,
|
price: String,
|
||||||
pub volume: String,
|
volume: String,
|
||||||
pub amount: String,
|
amount: String,
|
||||||
pub receipt: String,
|
receipt: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PreparedCustomer {
|
struct PreparedCustomer {
|
||||||
pub customer_number: String,
|
customer_number: String,
|
||||||
pub cards: Vec<CardData>,
|
cards: Vec<CardData>,
|
||||||
pub summary: Summary,
|
summary: Summary,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PreparedCustomer {
|
impl PreparedCustomer {
|
||||||
@@ -213,7 +212,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
for (customer_num, customer) in customers {
|
for (customer_num, customer) in customers {
|
||||||
let prepared = PreparedCustomer::from_customer(customer);
|
let prepared = PreparedCustomer::from_customer(customer);
|
||||||
let customer_html = CustomerTemplate {
|
let customer_html = CustomerTemplate {
|
||||||
customer: prepared.clone(),
|
customer: prepared,
|
||||||
period: period.clone(),
|
period: period.clone(),
|
||||||
generated_date: generated_date.clone(),
|
generated_date: generated_date.clone(),
|
||||||
}
|
}
|
||||||
@@ -222,15 +221,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let filename = format!("customer_{}.html", customer_num);
|
let filename = format!("customer_{}.html", customer_num);
|
||||||
fs::write(output_dir.join(&filename), customer_html)?;
|
fs::write(output_dir.join(&filename), customer_html)?;
|
||||||
println!("Genererade {}", filename);
|
println!("Genererade {}", filename);
|
||||||
|
|
||||||
let pdf_filename = format!("customer_{}.pdf", customer_num);
|
|
||||||
pdf::generate_customer_pdf(
|
|
||||||
&prepared,
|
|
||||||
&period,
|
|
||||||
&generated_date,
|
|
||||||
&output_dir.join(&pdf_filename),
|
|
||||||
)?;
|
|
||||||
println!("Genererade {}", pdf_filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
|
|||||||
244
src/pdf.rs
244
src/pdf.rs
@@ -1,244 +0,0 @@
|
|||||||
use printpdf::*;
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
use std::fs::File;
|
|
||||||
use std::io::BufWriter;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use crate::PreparedCustomer;
|
|
||||||
|
|
||||||
pub fn generate_customer_pdf(
|
|
||||||
customer: &PreparedCustomer,
|
|
||||||
period: &str,
|
|
||||||
generated_date: &str,
|
|
||||||
output_path: &Path,
|
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let html = format!(
|
|
||||||
r#"<!DOCTYPE html>
|
|
||||||
<html lang="sv">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Faktura - Kund {}</title>
|
|
||||||
<style>
|
|
||||||
@page {{
|
|
||||||
size: A4;
|
|
||||||
margin: 15mm;
|
|
||||||
}}
|
|
||||||
* {{ box-sizing: border-box; }}
|
|
||||||
body {{
|
|
||||||
font-family: Arial, sans-serif;
|
|
||||||
font-size: 12px;
|
|
||||||
margin: 0;
|
|
||||||
padding: 20px;
|
|
||||||
}}
|
|
||||||
.header {{
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
border-bottom: 2px solid #333;
|
|
||||||
padding-bottom: 10px;
|
|
||||||
margin-bottom: 15px;
|
|
||||||
}}
|
|
||||||
.header h1 {{
|
|
||||||
margin: 0;
|
|
||||||
font-size: 20px;
|
|
||||||
}}
|
|
||||||
.header .meta {{
|
|
||||||
text-align: right;
|
|
||||||
font-size: 11px;
|
|
||||||
color: #666;
|
|
||||||
}}
|
|
||||||
.summary {{
|
|
||||||
background: #f5f5f5;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
padding: 12px;
|
|
||||||
margin-bottom: 15px;
|
|
||||||
}}
|
|
||||||
.summary h2 {{
|
|
||||||
margin: 0 0 10px 0;
|
|
||||||
font-size: 14px;
|
|
||||||
}}
|
|
||||||
table {{
|
|
||||||
width: 100%;
|
|
||||||
border-collapse: collapse;
|
|
||||||
font-size: 11px;
|
|
||||||
}}
|
|
||||||
th, td {{
|
|
||||||
padding: 5px 8px;
|
|
||||||
text-align: left;
|
|
||||||
}}
|
|
||||||
th {{
|
|
||||||
background: #e0e0e0;
|
|
||||||
font-weight: bold;
|
|
||||||
}}
|
|
||||||
.grand-total-row td {{
|
|
||||||
font-weight: bold;
|
|
||||||
border-top: 2px solid #333;
|
|
||||||
background: #f0f0f0;
|
|
||||||
}}
|
|
||||||
.card-section {{
|
|
||||||
margin-bottom: 15px;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
}}
|
|
||||||
.card-header {{
|
|
||||||
background: #f0f0f0;
|
|
||||||
padding: 8px 12px;
|
|
||||||
font-weight: bold;
|
|
||||||
}}
|
|
||||||
.card-summary {{
|
|
||||||
padding: 6px 12px;
|
|
||||||
font-size: 11px;
|
|
||||||
background: #fafafa;
|
|
||||||
}}
|
|
||||||
.grand-total {{
|
|
||||||
background: #333;
|
|
||||||
color: white;
|
|
||||||
padding: 12px 15px;
|
|
||||||
font-size: 14px;
|
|
||||||
font-weight: bold;
|
|
||||||
text-align: right;
|
|
||||||
}}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="header">
|
|
||||||
<div>
|
|
||||||
<h1>Faktura - Kund {}</h1>
|
|
||||||
<div>Period: {}</div>
|
|
||||||
</div>
|
|
||||||
<div class="meta">
|
|
||||||
<div>Genererad: {}</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="summary">
|
|
||||||
<h2>Sammanfattning</h2>
|
|
||||||
<table>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Produkt</th>
|
|
||||||
<th style="text-align:right">Volym (L)</th>
|
|
||||||
<th style="text-align:right">Belopp</th>
|
|
||||||
<th style="text-align:right">Snittpris/L</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{}
|
|
||||||
<tr class="grand-total-row">
|
|
||||||
<td>Totalt</td>
|
|
||||||
<td style="text-align:right">{}</td>
|
|
||||||
<td style="text-align:right">{} Kr</td>
|
|
||||||
<td></td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{}
|
|
||||||
</body>
|
|
||||||
</html>"#,
|
|
||||||
customer.customer_number,
|
|
||||||
customer.customer_number,
|
|
||||||
period,
|
|
||||||
generated_date,
|
|
||||||
generate_product_rows(&customer.summary.products),
|
|
||||||
customer.summary.total_volume,
|
|
||||||
customer.summary.grand_total,
|
|
||||||
generate_card_sections(&customer.cards),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut warnings = Vec::new();
|
|
||||||
let doc = PdfDocument::from_html(
|
|
||||||
&html,
|
|
||||||
&BTreeMap::new(),
|
|
||||||
&BTreeMap::new(),
|
|
||||||
&GeneratePdfOptions::default(),
|
|
||||||
&mut warnings,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let file = File::create(output_path)?;
|
|
||||||
let mut writer = BufWriter::new(file);
|
|
||||||
let mut warnings = Vec::new();
|
|
||||||
doc.save_writer(&mut writer, &PdfSaveOptions::default(), &mut warnings);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_product_rows(products: &[crate::ProductSummary]) -> String {
|
|
||||||
products
|
|
||||||
.iter()
|
|
||||||
.map(|p| {
|
|
||||||
format!(
|
|
||||||
r#"<tr>
|
|
||||||
<td>{}</td>
|
|
||||||
<td style="text-align:right">{}</td>
|
|
||||||
<td style="text-align:right">{} Kr</td>
|
|
||||||
<td style="text-align:right">{} Kr</td>
|
|
||||||
</tr>"#,
|
|
||||||
p.name, p.volume, p.amount, p.avg_price
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n ")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_card_sections(cards: &[crate::CardData]) -> String {
|
|
||||||
cards
|
|
||||||
.iter()
|
|
||||||
.map(|card| {
|
|
||||||
format!(
|
|
||||||
r#"<div class="card-section">
|
|
||||||
<div class="card-header">Kort: {} | {} transaktioner</div>
|
|
||||||
<div class="card-summary">Summa: {} Kr | Volym: {} L</div>
|
|
||||||
<table>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Datum</th>
|
|
||||||
<th>Produkt</th>
|
|
||||||
<th style="text-align:right">Pris/L</th>
|
|
||||||
<th style="text-align:right">Volym</th>
|
|
||||||
<th style="text-align:right">Belopp</th>
|
|
||||||
<th>Kvitto</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{}
|
|
||||||
<tr style="font-weight:bold;background:#f9f9f9">
|
|
||||||
<td colspan="3">Kortsumma</td>
|
|
||||||
<td style="text-align:right">{}</td>
|
|
||||||
<td style="text-align:right">{}</td>
|
|
||||||
<td></td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>"#,
|
|
||||||
card.card_number,
|
|
||||||
card.transactions.len(),
|
|
||||||
card.total_amount,
|
|
||||||
card.total_volume,
|
|
||||||
generate_transaction_rows(&card.transactions),
|
|
||||||
card.total_volume,
|
|
||||||
card.total_amount,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n\n ")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_transaction_rows(transactions: &[crate::FormattedTransaction]) -> String {
|
|
||||||
transactions
|
|
||||||
.iter()
|
|
||||||
.map(|tx| {
|
|
||||||
format!(
|
|
||||||
r#"<tr>
|
|
||||||
<td>{}</td>
|
|
||||||
<td>{}</td>
|
|
||||||
<td style="text-align:right">{}</td>
|
|
||||||
<td style="text-align:right">{}</td>
|
|
||||||
<td style="text-align:right">{}</td>
|
|
||||||
<td>{}</td>
|
|
||||||
</tr>"#,
|
|
||||||
tx.date, tx.quality_name, tx.price, tx.volume, tx.amount, tx.receipt
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n ")
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user