BrightSide Workbench Full Report + Source Code
CompanyModelWrapper.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.financials.business;
20 
21 import java.util.List;
22 import org.turro.financials.entity.Contract;
23 import org.turro.financials.operating.ContractItem;
24 import org.turro.financials.operating.ContractItemSet;
25 import org.turro.financials.operating.MonthItem;
26 import org.turro.financials.operating.MonthItemSet;
27 import org.turro.financials.operating.ValueItem;
28 import org.turro.i18n.I_;
29 import org.zkoss.zul.CategoryModel;
30 import org.zkoss.zul.SimpleCategoryModel;
31 
36 public class CompanyModelWrapper {
37 
38  private final ContractItemSet items;
39 
41  this.items = items;
42  }
43 
44  public CategoryModel getSalesExpensesModel(List<Contract> contracts) {
45  SimpleCategoryModel cm = new SimpleCategoryModel();
46  // Ensure all months exist with zero value and ordered
47  MonthItemSet mis = new MonthItemSet();
48  for(ContractItem ci : items) {
49  if(exists(contracts, ci.getId())) {
50  for(MonthItem mi : ci.getMonths()) {
51  mis.getMonth(mi.getYear(), mi.getMonth());
52  }
53  }
54  }
55  for(MonthItem mi : mis) {
56  setModelValue(cm, I_.get("Sales"), mi.getStringId(), 0);
57  setModelValue(cm, I_.get("Expenses"), mi.getStringId(), 0);
58  setModelValue(cm, I_.get("Profit"), mi.getStringId(), 0);
59  }
60  // Add values
61  for(ContractItem ci : items) {
62  if(exists(contracts, ci.getId())) {
63  for(MonthItem mi : ci.getMonths()) {
64  double sales = mi.getMovements().getValues().getSalesAmount();
65  double expenses = mi.getMovements().getValues().getExpensesAmount();
66  setModelValue(cm, I_.get("Sales"), mi.getStringId(), sales);
67  setModelValue(cm, I_.get("Expenses"), mi.getStringId(), expenses);
68  setModelValue(cm, I_.get("Profit"), mi.getStringId(), sales - expenses);
69  }
70  }
71  }
72  return cm;
73  }
74 
75  public CategoryModel getStoresModel(List<Contract> contracts) {
76  SimpleCategoryModel cm = new SimpleCategoryModel();
77  for(ContractItem ci : items) {
78  if(exists(contracts, ci.getId())) {
79  for(ValueItem vi : ci.getValues()) {
80  setModelValue(cm, ci.getName(), vi.getName(), vi.getValue());
81  }
82  }
83  }
84  return cm;
85  }
86 
87  public CategoryModel getParticipantsModel(List<Contract> contracts) {
88  SimpleCategoryModel cm = new SimpleCategoryModel();
89  for(ContractItem ci : items) {
90  if(exists(contracts, ci.getId())) {
91  for(ValueItem vi : ci.getParticipants()) {
92  setModelValue(cm, ci.getName(), vi.getName(), vi.getValue());
93  }
94  }
95  }
96  return cm;
97  }
98 
99  public CategoryModel getProvidersModel(List<Contract> contracts) {
100  SimpleCategoryModel cm = new SimpleCategoryModel();
101  for(ContractItem ci : items) {
102  if(exists(contracts, ci.getId())) {
103  for(ValueItem vi : ci.getParticipants().getProviders()) {
104  setModelValue(cm, ci.getName(), vi.getName(), vi.getValue());
105  }
106  }
107  }
108  return cm;
109  }
110 
111  public CategoryModel getCustomersModel(List<Contract> contracts) {
112  SimpleCategoryModel cm = new SimpleCategoryModel();
113  for(ContractItem ci : items) {
114  if(exists(contracts, ci.getId())) {
115  for(ValueItem vi : ci.getParticipants().getCustomers()) {
116  setModelValue(cm, ci.getName(), vi.getName(), vi.getValue());
117  }
118  }
119  }
120  return cm;
121  }
122 
123  public CategoryModel getSaleStoresModel(List<Contract> contracts) {
124  SimpleCategoryModel cm = new SimpleCategoryModel();
125  for(ContractItem ci : items) {
126  if(exists(contracts, ci.getId())) {
127  for(ValueItem vi : ci.getValues().getSales()) {
128  setModelValue(cm, ci.getName(), vi.getName(), vi.getValue());
129  }
130  }
131  }
132  return cm;
133  }
134 
135  public CategoryModel getExpenseStoresModel(List<Contract> contracts) {
136  SimpleCategoryModel cm = new SimpleCategoryModel();
137  for(ContractItem ci : items) {
138  if(exists(contracts, ci.getId())) {
139  for(ValueItem vi : ci.getValues().getExpenses()) {
140  setModelValue(cm, ci.getName(), vi.getName(), vi.getValue());
141  }
142  }
143  }
144  return cm;
145  }
146 
147  private boolean exists(List<Contract> contracts, long id) {
148  for(Contract contract : contracts) {
149  if(contract.getId() == id) {
150  return true;
151  }
152  }
153  return false;
154  }
155 
156  private void setModelValue(SimpleCategoryModel cm, Comparable<?> series, Comparable<?> category, Number value) {
157  Double v = (Double) cm.getValue(series, category);
158  cm.setValue(series, category, (value == null ? 0.0 : value.doubleValue()) + (v == null ? 0.0 : v));
159  }
160 
161 }
CategoryModel getCustomersModel(List< Contract > contracts)
CategoryModel getSalesExpensesModel(List< Contract > contracts)
CategoryModel getProvidersModel(List< Contract > contracts)
CategoryModel getExpenseStoresModel(List< Contract > contracts)
CategoryModel getStoresModel(List< Contract > contracts)
CategoryModel getParticipantsModel(List< Contract > contracts)
CategoryModel getSaleStoresModel(List< Contract > contracts)
MonthItem getMonth(int year, int month)
static String get(String msg)
Definition: I_.java:41