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
use super::{BillItem, BillProduct, Money};
use std::ops::Deref;
#[derive(Debug)]
#[cfg_attr(feature = "serialization", derive(Serialize))]
pub struct ItemList<P> {
items: Vec<BillItem<P>>
}
impl<P:BillProduct> ItemList<P> {
pub fn from_vec(list:Vec<BillItem<P>>) -> Self{
ItemList{ items: list }
}
pub fn new() -> Self{
ItemList{ items: Vec::new()}
}
pub fn gross_sum(&self) -> Money {
self.items.iter()
.map(|i|i.gross())
.fold(Money::default(), |acc, x| acc + x)
}
pub fn tax_sum(&self) -> Money{
if let Some(tax) = self.items.get(0).map(|i|i.product.tax()){
self.gross_sum() * **tax
} else {Money::default()}
}
pub fn net_sum(&self) -> Money{
self.gross_sum() + self.tax_sum()
}
pub fn push(&mut self, item:BillItem<P>){
self.items.push(item)
}
}
impl<P: BillProduct> Deref for ItemList<P> {
type Target = [BillItem<P>];
fn deref(&self) -> &[BillItem<P>]{
self.items.deref()
}
}