1use crate::client::vat::VatClient;
2use crate::config::Config;
3use crate::error::YukiError;
4use crate::output::{OutputFormat, format_json, format_table, is_tty};
5
6pub async fn returns(
7 config: &Config,
8 admin: Option<&str>,
9 year: Option<&str>,
10 format: Option<&str>,
11) -> Result<(), YukiError> {
12 let entry = config.resolve_admin(admin)?;
13 let mut client = VatClient::new();
14 client.authenticate(&config.api_key).await?;
15 let all_returns = client.vat_return_list(&entry.admin_id).await?;
16
17 let filtered: Vec<_> = match year {
18 Some(y) => all_returns
19 .into_iter()
20 .filter(|r| r.period.starts_with(y))
21 .collect(),
22 None => all_returns,
23 };
24
25 let headers = vec![
26 "Period".into(),
27 "Status".into(),
28 "Start".into(),
29 "End".into(),
30 ];
31 let rows: Vec<Vec<String>> = filtered
32 .iter()
33 .map(|r| {
34 vec![
35 r.period.clone(),
36 r.status.clone(),
37 r.start_date.clone(),
38 r.end_date.clone(),
39 ]
40 })
41 .collect();
42
43 let fmt = OutputFormat::from_flag(format, is_tty());
44 match fmt {
45 OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
46 OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
47 }
48 Ok(())
49}
50
51pub async fn codes(
52 config: &Config,
53 admin: Option<&str>,
54 format: Option<&str>,
55) -> Result<(), YukiError> {
56 let entry = config.resolve_admin(admin)?;
57 let mut client = VatClient::new();
58 client.authenticate(&config.api_key).await?;
59 let vat_codes = client.active_vat_codes(&entry.admin_id).await?;
60
61 let headers = vec!["Code".into(), "Description".into()];
62 let rows: Vec<Vec<String>> = vat_codes
63 .iter()
64 .map(|c| vec![c.code.clone(), c.description.clone()])
65 .collect();
66
67 let fmt = OutputFormat::from_flag(format, is_tty());
68 match fmt {
69 OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
70 OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
71 }
72 Ok(())
73}