1use crate::cli::setup_domain;
2use crate::client::accounting::AccountingClient;
3use crate::client::accounting_info::AccountingInfoClient;
4use crate::config::Config;
5use crate::error::YukiError;
6use crate::output::{
7 ListOptions, OutputFormat, apply_pagination, format_json, format_table, is_tty,
8};
9use crate::period::parse_period;
10
11pub async fn balance(
12 config: &Config,
13 admin: Option<&str>,
14 account: Option<&str>,
15 period: Option<&str>,
16 format: Option<&str>,
17) -> Result<(), YukiError> {
18 let (start, _end) = resolve_period(period)?;
19 let (client, entry) = setup_domain(config, admin).await?;
20 let mut balances = client.gl_account_balances(&entry.admin_id, &start).await?;
21 if let Some(code) = account {
23 balances.retain(|b| b.code == code);
24 }
25
26 let headers = vec!["Account".into(), "Description".into(), "Balance".into()];
27 let rows: Vec<Vec<String>> = balances
28 .iter()
29 .map(|b| vec![b.code.clone(), b.description.clone(), b.amount.clone()])
30 .collect();
31
32 let fmt = OutputFormat::from_flag(format, is_tty());
33 match fmt {
34 OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
35 OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
36 }
37 Ok(())
38}
39
40pub async fn transactions(
41 config: &Config,
42 admin: Option<&str>,
43 account: Option<&str>,
44 period: Option<&str>,
45 format: Option<&str>,
46 opts: ListOptions<'_>,
47) -> Result<(), YukiError> {
48 let (start, end) = resolve_period(period)?;
49 let gl_code = account.unwrap_or("");
50 let (client, entry) = setup_domain(config, admin).await?;
51 let xml = client
52 .gl_account_transactions(&entry.admin_id, gl_code, &start, &end)
53 .await?;
54 let transactions = AccountingClient::parse_gl_transactions(&xml)?;
55
56 let headers = vec![
57 "ID".into(),
58 "Date".into(),
59 "Amount".into(),
60 "Description".into(),
61 ];
62 let mut rows: Vec<Vec<String>> = transactions
63 .iter()
64 .map(|t| {
65 vec![
66 t.id.clone(),
67 t.date.clone(),
68 t.amount.clone(),
69 t.description.clone(),
70 ]
71 })
72 .collect();
73 apply_pagination(&mut rows, &opts);
74
75 let fmt = OutputFormat::from_flag(format, is_tty());
76 match fmt {
77 OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
78 OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
79 }
80 Ok(())
81}
82
83pub async fn scheme(
84 config: &Config,
85 admin: Option<&str>,
86 format: Option<&str>,
87) -> Result<(), YukiError> {
88 let entry = config.resolve_admin(admin)?;
89 let mut client = AccountingInfoClient::new();
90 client.authenticate(&config.api_key).await?;
91 let accounts = client.get_gl_account_scheme(&entry.admin_id).await?;
92
93 let headers = vec!["Code".into(), "Description".into(), "Type".into()];
94 let rows: Vec<Vec<String>> = accounts
95 .into_iter()
96 .map(|a| vec![a.code, a.description, a.account_type])
97 .collect();
98
99 let fmt = OutputFormat::from_flag(format, is_tty());
100 match fmt {
101 OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
102 OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
103 }
104 Ok(())
105}
106
107pub async fn start_balance(
108 config: &Config,
109 admin: Option<&str>,
110 year: Option<&str>,
111 format: Option<&str>,
112) -> Result<(), YukiError> {
113 let year_str;
114 let bookyear = match year {
115 Some(y) => y,
116 None => {
117 year_str = current_year().to_string();
118 &year_str
119 }
120 };
121 let entry = config.resolve_admin(admin)?;
122 let mut client = AccountingInfoClient::new();
123 client.authenticate(&config.api_key).await?;
124 let balances = client
125 .get_start_balance_by_gl_account(&entry.admin_id, bookyear)
126 .await?;
127
128 let headers = vec!["GL Account".into(), "Description".into(), "Balance".into()];
129 let rows: Vec<Vec<String>> = balances
130 .into_iter()
131 .map(|b| vec![b.gl_account_code, b.description, b.balance])
132 .collect();
133
134 let fmt = OutputFormat::from_flag(format, is_tty());
135 match fmt {
136 OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
137 OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
138 }
139 Ok(())
140}
141
142pub async fn revenue(
143 config: &Config,
144 admin: Option<&str>,
145 period: Option<&str>,
146 format: Option<&str>,
147) -> Result<(), YukiError> {
148 let (start, end) = resolve_period(period)?;
149 let (client, entry) = setup_domain(config, admin).await?;
150 let amount = client.net_revenue(&entry.admin_id, &start, &end).await?;
151
152 let headers = vec!["Period".into(), "Net Revenue".into()];
153 let rows = vec![vec![format!("{start} to {end}"), amount]];
154
155 let fmt = OutputFormat::from_flag(format, is_tty());
156 match fmt {
157 OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
158 OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
159 }
160 Ok(())
161}
162
163fn resolve_period(period: Option<&str>) -> Result<(String, String), YukiError> {
167 match period {
168 Some(p) => parse_period(p),
169 None => {
170 let year = current_year();
171 Ok((format!("{year}-01-01"), format!("{year}-12-31")))
172 }
173 }
174}
175
176fn current_year() -> u32 {
177 use std::time::{SystemTime, UNIX_EPOCH};
178 let secs = SystemTime::now()
179 .duration_since(UNIX_EPOCH)
180 .unwrap_or_default()
181 .as_secs();
182 1970 + (secs / 31_557_600) as u32
184}