BrightSide Workbench Full Report + Source Code
AccountReportFormula.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.financials.report;
19 
20 import java.util.List;
21 import org.turro.string.Strings;
22 import org.turro.script.Script;
23 
28 public class AccountReportFormula {
29 
30  private AccountReport report;
31  private String label, formula;
32  private boolean percent = false;
33  private final PeriodSet periods = new PeriodSet();
34 
35  public AccountReportFormula(String label) {
36  this.label = label;
37  }
38 
39  public String getFormula() {
40  return formula;
41  }
42 
43  public void setFormula(String formula) {
44  this.formula = formula;
45  }
46 
47  public String getLabel() {
48  return label;
49  }
50 
51  public void setLabel(String label) {
52  this.label = label;
53  }
54 
55  public boolean isPercent() {
56  return percent;
57  }
58 
59  public void setPercent(boolean percent) {
60  this.percent = percent;
61  }
62 
64  return report;
65  }
66 
67  public void setReport(AccountReport report) {
68  this.report = report;
69  }
70 
71  public double getValue(AccountReportPeriod period) {
72  AccountReportPeriod p = periods.getPeriod(period);
73  if(p == null) {
74  p = new AccountReportPeriod(period.getYear(), period.getMonth());
75  p.setValue(new Script().evalToDoubleOrZero(parseFormula(period)));
76  periods.add(p);
77  }
78  return p.getValue();
79  }
80 
81  private String parseFormula(AccountReportPeriod period) {
82  String result = formula;
83  List<String> items = Strings.extractAll(result, "\\@\\{([a-zA-Z0-9]+)\\}");
84  for(String item : items) {
85  result = result.replaceAll("\\@\\{" + item + "\\}", report.getItem(item).getAccumulatedFor(period) + "");
86  result = result.replaceAll("--", "");
87  }
88  List<String> formulas = Strings.extractAll(result, "\\@F\\{([a-zA-Z0-9]+)\\}");
89  for(String form : formulas) {
90  result = result.replaceAll("\\@F\\{" + form + "\\}", report.getFormula(form).getValue(period) + "");
91  result = result.replaceAll("--", "");
92  }
93  return result;
94  }
95 
96 }
double getAccumulatedFor(AccountReportPeriod period)
AccountReportItem getItem(String label)
AccountReportFormula getFormula(String label)
AccountReportPeriod getPeriod(AccountReportPeriod period)
Definition: PeriodSet.java:36