use crate::{
commands::{Cli, Config, Result, shorten_date},
jobber::JobberGet,
print_tip,
views::{InvoiceView, WorkView, create_work_table},
};
use clap::Parser;
use color_print::cprintln;
#[derive(Parser, Debug)]
pub(crate) struct ListInvoice {
invoice_no: usize,
}
impl ListInvoice {
pub(crate) fn run(&self, cli: &Cli) -> Result<()> {
cprintln!();
let config = Config::load()?;
let database = cli.open_database()?;
let invoice_no = self.invoice_no;
let invoice = database.get_invoice(invoice_no)?;
println!("{}", InvoiceView::new(invoice_no, &database, cli.ansi())?);
for bill in invoice.bills.iter() {
let scheme = &bill.scheme;
cprintln!(" Worker: <s>{}</>", bill.worker);
println!(" Subject: {}", bill.subject);
println!(" Revenue: {}", bill.revenue()?);
println!();
if !bill.work.is_empty() {
let mut rows: Vec<_> = bill
.work
.iter()
.enumerate()
.map(|(n, w)| {
WorkView::new(Some(n), w, database.get_tags(), scheme, cli.ansi())
})
.collect();
rows.sort_by_key(|r| r.order);
let rows: Vec<_> = rows
.iter_mut()
.scan(None, |prev: &mut Option<WorkView>, row: &mut WorkView| {
let p = row.clone();
if let Some(prev) = prev {
shorten_date(&mut row.start, &prev.start);
}
*prev = Some(p);
Some(row.clone())
})
.collect();
println!("{}", create_work_table(rows, &config));
}
}
print_tip!(cli);
print_tip!(
cli,
"Use <s>jobber export invoice {}</> to get a PDF.",
invoice_no
);
Ok(())
}
}