111 lines
3.8 KiB
Rust
111 lines
3.8 KiB
Rust
use std::error::Error;
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
use rust_xlsxwriter::*;
|
|
use crate::template::antares::Antares;
|
|
|
|
fn resolve_excel_path(path_str: &str) -> Result<PathBuf, Box<dyn Error>> {
|
|
let path = Path::new(path_str);
|
|
|
|
// If path ends with .xlsx, treat it as a file path
|
|
if path_str.ends_with(".xlsx") {
|
|
// Create parent directories if needed
|
|
if let Some(parent) = path.parent() {
|
|
if !parent.as_os_str().is_empty() {
|
|
fs::create_dir_all(parent)?;
|
|
}
|
|
}
|
|
return Ok(path.to_path_buf());
|
|
}
|
|
|
|
// If path has an extension but it's not .xlsx, error out
|
|
if path.extension().is_some() {
|
|
return Err(format!(
|
|
"Invalid file extension. Expected .xlsx, got: {}",
|
|
path_str
|
|
)
|
|
.into());
|
|
}
|
|
|
|
// Otherwise, treat as directory and create antares.xlsx inside
|
|
fs::create_dir_all(path)?;
|
|
Ok(path.join("antares.xlsx"))
|
|
}
|
|
|
|
pub fn export_to_excel(items: &Antares, path_str: &str) -> Result<(), Box<dyn Error>> {
|
|
let output_path = resolve_excel_path(path_str)?;
|
|
|
|
let mut workbook = Workbook::new();
|
|
let sheet = workbook.add_worksheet();
|
|
|
|
let header_format = Format::new().set_bold();
|
|
|
|
// Write headers
|
|
sheet.write_string_with_format(0, 0, "BESZCIKKSZAM", &header_format)?;
|
|
sheet.write_string_with_format(0, 1, "GYCIKKSZAM", &header_format)?;
|
|
sheet.write_string_with_format(0, 2, "BESZCIKKNEV", &header_format)?;
|
|
sheet.write_string_with_format(0, 3, "GYARTO", &header_format)?;
|
|
sheet.write_string_with_format(0, 4, "CIKKAZON", &header_format)?;
|
|
sheet.write_string_with_format(0, 5, "KESZLET", &header_format)?;
|
|
sheet.write_string_with_format(0, 6, "ME", &header_format)?;
|
|
sheet.write_string_with_format(0, 7, "EGYSEGAR", &header_format)?;
|
|
|
|
let mut row = 1u32;
|
|
for item in items {
|
|
// Skip if BESZCIKKSZAM is empty
|
|
let cikkszam = match &item.cikkszam {
|
|
Some(cs) if !cs.is_empty() => cs.clone(),
|
|
_ => continue,
|
|
};
|
|
|
|
// BESZCIKKNEV: cikk_megnevezes_rovid or cikk_megnevezes
|
|
let megnevezes = item
|
|
.cikk_megnevezes_rovid
|
|
.clone()
|
|
.or_else(|| item.cikk_megnevezes.clone())
|
|
.unwrap_or_default();
|
|
|
|
// Skip if BESZCIKKNEV is empty
|
|
if megnevezes.is_empty() {
|
|
continue;
|
|
}
|
|
|
|
// EGYSEGAR: netto_kisker_ar or find in cikk_jellemzok where jellemzo_nev = "Alap ár"
|
|
let mut unit_price = item.netto_kisker_ar.unwrap_or(0.0);
|
|
if unit_price == 0.0 {
|
|
if let Some(ref jellemzok) = item.cikk_jellemzok {
|
|
for jellemzo in jellemzok {
|
|
if let Some(ref nev) = jellemzo.jellemzo_nev {
|
|
if nev == "Alap ár" {
|
|
if let Some(ref ertek) = jellemzo.jellemzo_ertek {
|
|
unit_price = ertek.parse().unwrap_or(0.0);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Write row data
|
|
if unit_price == 0.0 {
|
|
continue;
|
|
}
|
|
sheet.write_string(row, 0, &cikkszam)?;
|
|
sheet.write_string(row, 1, &cikkszam)?;
|
|
sheet.write_string(row, 2, &megnevezes)?;
|
|
sheet.write_string(row, 3, "EGYEB")?;
|
|
sheet.write_string(row, 4, item.vonalkod.as_deref().unwrap_or(""))?;
|
|
sheet.write_number(row, 5, item.szabad_keszlet.unwrap_or(0) as f64)?;
|
|
sheet.write_string(row, 6, item.mennyisegi_egyseg_kod.as_deref().unwrap_or(""))?;
|
|
sheet.write_number(row, 7, unit_price)?;
|
|
|
|
row += 1;
|
|
}
|
|
|
|
workbook.save(&output_path)?;
|
|
let display_path = output_path.display();
|
|
println!("Excel file '{}' created with {} rows", display_path, row - 1);
|
|
Ok(())
|
|
}
|