1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use chrono::prelude::*;
use bill::Currency;
use icalendar::Calendar;
use std::fmt::Write;
use std::path::PathBuf;
use std::collections::HashMap;
use util;
use storage::{self, StorageDir, Storable};
use project::Project;
use project::spec::*;
pub mod error;
use self::error::*;
pub fn with_projects<F>(dir:StorageDir, search_terms:&[&str], f:F) -> Result<()>
where F:Fn(&Project)->Result<()>
{
trace!("with_projects({:?})", search_terms);
let projects = storage::setup::<Project>()?.search_projects_any(dir, search_terms)?;
if projects.is_empty() {
return Err(format!("Nothing found for {:?}", search_terms).into())
}
for project in projects {
f(&project)?;
}
Ok(())
}
pub fn csv(year:i32) -> Result<String> {
let mut projects = storage::setup::<Project>()?.open_projects(StorageDir::Year(year))?;
projects.sort_by(|pa,pb| pa.index().unwrap_or_else(||"zzzz".to_owned()).cmp( &pb.index().unwrap_or_else(||"zzzz".to_owned())));
projects_to_csv(&projects)
}
pub fn projects_to_csv(projects:&[Project]) -> Result<String>{
let mut string = String::new();
let splitter = ";";
writeln!(&mut string, "{}",
[
lformat!("INum"),
lformat!("Designation"),
lformat!("Date"),
lformat!("InvoiceDate"),
lformat!("Caterer"),
lformat!("Responsible"),
lformat!("Payed on"),
lformat!("Amount"),
lformat!("Canceled")
]
.join(splitter))?;
for project in projects {
writeln!(&mut string, "{}", [
project.field("InvoiceNumber") .unwrap_or_else(|| String::from(r#""""#)),
project.field("Name") .unwrap_or_else(|| String::from(r#""""#)),
project.field("event/dates/0/begin") .unwrap_or_else(|| String::from(r#""""#)),
project.field("invoice/date") .unwrap_or_else(|| String::from(r#""""#)),
project.field("Employees") .unwrap_or_else(|| String::from(r#""""#)),
project.field("Responsible") .unwrap_or_else(|| String::from(r#""""#)),
project.field("invoice/payed_date") .unwrap_or_else(|| String::from(r#""""#)),
project.sum_sold().map(|c|c.value().to_string()).unwrap_or_else(|_| String::from(r#""""#)),
String::from(if project.canceled(){"canceled"} else {""})
].join(splitter))?;
}
Ok(string)
}
fn open_payments(projects: &[Project]) -> Currency {
projects.iter()
.filter(|&p| !p.canceled() && !p.is_payed() && p.age().unwrap_or(0) > 0)
.filter_map(|p| p.sum_sold().ok())
.fold(Currency::default(), |acc, x| acc + x)
}
fn open_wages(projects: &[Project]) -> Currency {
projects.iter()
.filter(|p| !p.canceled() && p.age().unwrap_or(0) > 0)
.filter_map(|p| p.hours().net_wages())
.fold(Currency::default(), |acc, x| acc + x)
}
fn unpayed_employees(projects: &[Project]) -> HashMap<String, Currency> {
let mut buckets = HashMap::new();
let employees = projects.iter()
.filter(|p| !p.canceled() && p.age().unwrap_or(0) > 0)
.filter_map(|p| p.hours().employees())
.flat_map(|e| e.into_iter());
for employee in employees {
let bucket = buckets.entry(employee.name.clone()).or_insert_with(Currency::new);
*bucket = *bucket + employee.salary;
}
buckets
}
#[derive(Debug)]
pub struct Dues {
pub acc_sum_sold: Currency,
pub acc_wages: Currency,
pub unpayed_employees: HashMap<String, Currency>,
}
pub fn dues() -> Result<Dues> {
let projects = storage::setup::<Project>()?.open_projects(StorageDir::Working)?;
let acc_sum_sold: Currency = open_payments(&projects);
let acc_wages = open_wages(&projects);
let unpayed_employees = unpayed_employees(&projects);
Ok(Dues{ acc_sum_sold, acc_wages, unpayed_employees})
}
pub fn spec() -> Result<()> {
use project::spec::*;
let projects = storage::setup::<Project>()?.open_projects(StorageDir::Working)?;
for project in projects {
info!("{}", project.dir().display());
project.client().validate().map_err(|errors| println!("{}", errors)).unwrap();
project.client().full_name();
project.client().first_name();
project.client().title();
project.client().email();
project.hours().employees_string();
project.invoice().number_long_str();
project.invoice().number_str();
project.offer().number();
project.age().map(|a|format!("{} days", a)).unwrap();
project.modified_date().map(|d|d.year().to_string()).unwrap();
project.sum_sold().map(|c|util::currency_to_string(&c)).unwrap();
project.responsible().map(|s|s.to_owned()).unwrap();
project.name().map(|s|s.to_owned()).unwrap();
}
Ok(())
}
pub fn delete_project_confirmation(dir: StorageDir, search_terms:&[&str]) -> Result<()> {
let storage = storage::setup_with_git::<Project>()?;
for project in storage.search_projects_any(dir, search_terms)? {
storage.delete_project_if(&project,
|| util::really(&format!("you want me to delete {:?} [y/N]", project.dir())) && util::really("really? [y/N]")
)?
}
Ok(())
}
pub fn archive_projects(search_terms:&[&str], manual_year:Option<i32>, force:bool) -> Result<Vec<PathBuf>>{
trace!("archive_projects matching ({:?},{:?},{:?})", search_terms, manual_year,force);
Ok( storage::setup_with_git::<Project>()?.archive_projects_if(search_terms, manual_year, || force) ?)
}
pub fn archive_all_projects() -> Result<Vec<PathBuf>> {
let storage = storage::setup_with_git::<Project>()?;
let mut moved_files = Vec::new();
for project in storage.open_projects(StorageDir::Working)?
.iter()
.filter(|p| p.is_ready_for_archive().is_ok()) {
println!(" we could get rid of: {}", project.name().unwrap_or(""));
moved_files.push(project.dir());
moved_files.append(&mut storage.archive_project(&project, project.year().unwrap())?);
}
Ok(moved_files)
}
pub fn unarchive_projects(year:i32, search_terms:&[&str]) -> Result<Vec<PathBuf>> {
Ok( storage::setup_with_git::<Project>()?.unarchive_projects(year, search_terms) ?)
}
pub fn calendar(dir: StorageDir) -> Result<String> {
calendar_with_tasks(dir, true)
}
pub fn calendar_and_tasks(dir: StorageDir) -> Result<String> {
calendar_with_tasks(dir, false)
}
pub fn calendar_with_tasks(dir: StorageDir, show_tasks:bool) -> Result<String> {
let storage = storage::setup::<Project>()?;
let mut cal = Calendar::new();
if show_tasks {
for project in storage.open_projects(StorageDir::Working)? {
cal.append(&mut project.to_tasks())
}
}
for project in storage.open_projects(dir)?{
cal.append(&mut project.to_ical())
}
Ok(cal.to_string())
}