BrightSide Workbench Full Report + Source Code
RegisterGenerator.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.model.register;
19 
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.Set;
23 import org.turro.string.Strings;
24 import org.turro.financials.entity.*;
25 import org.turro.financials.model.AccountFormat;
26 import org.turro.financials.model.business.CompanyWrapper;
27 import org.turro.math.Round;
28 import org.turro.math.Zero;
29 
34 public class RegisterGenerator {
35 
36  private Register register;
37  private int fractionDigits;
38 
39  public RegisterGenerator() {
40  fractionDigits = CompanyWrapper.getCompanyCurrency().getDefaultFractionDigits();
41  }
42 
43  public int getFractionDigits() {
44  return fractionDigits;
45  }
46 
47  public Register getRegister() {
48  if(register == null) {
49  register = new Register();
50  }
51  return register;
52  }
53 
54  public void setRegister(Register register) {
55  this.register = register;
56  }
57 
58  public void addAccount(Account account, String concept, double balance) {
59  if(!Zero.near(balance, fractionDigits)) {
60  if(balance < 0) {
61  addAccount(account, concept, 0.0, -balance);
62  } else {
63  addAccount(account, concept, balance, 0.0);
64  }
65  }
66  }
67 
68  public void addAccount(Contract store, String concept, double debit, double credit) {
69  String accStr = "%ctd.%ct",
71  accStr = accStr.replaceAll("\\%ctd", asCash)
72  .replaceAll("\\%ct", "" + store.getId());
73  Account account = new Account();
74  account.setId(AccountFormat.expand(accStr));
75  account.setDescription(store.getFullDescription());
76  addAccount(account, concept, debit, credit);
77  }
78 
79  public void addAccount(Account account, String concept, double debit, double credit) {
80  if(register == null) {
81  register = new Register();
82  }
83  if(Strings.isBlank(account.getDescription())) {
84  MajorAccount ma = account.getParent();
85  if(ma != null) {
86  account.setDescription(ma.getDescription());
87  }
88  }
89  if(!(Zero.near(debit, fractionDigits))) {
90  RegisterEntry re = new RegisterEntry();
91  re.setAccount(account);
92  re.setConcept(concept);
93  re.setCredit(0.0);
94  re.setDebit(new Round(debit).decimals(fractionDigits).value());
95  re.setRegister(register);
96  register.getRegisterEntries().add(re);
97  re.roundIt(fractionDigits);
98  }
99  if(!(Zero.near(credit, fractionDigits))) {
100  RegisterEntry re = new RegisterEntry();
101  re.setAccount(account);
102  re.setConcept(concept);
103  re.setCredit(new Round(credit).decimals(fractionDigits).value());
104  re.setDebit(0.0);
105  re.setRegister(register);
106  register.getRegisterEntries().add(re);
107  re.roundIt(fractionDigits);
108  }
109  }
110 
111  public Set<RegisterEntry> addAmount(RegisterView view, Contract contract, Contract store, LineType lineType, String concept, double amount) {
112  Set<RegisterEntry> set = addEntries(view, contract, store, lineType, concept, amount);
113  if(register == null) {
114  register = new Register();
115  }
116  for(RegisterEntry re : set) {
117  re.setRegister(register);
118  register.getRegisterEntries().add(re);
119  }
120  return set;
121  }
122 
123  private Set<RegisterEntry> addEntries(RegisterView view, Contract contract, Contract store, LineType lineType, String concept, double amount) {
124  if(store == null || lineType == null || amount == 0) return Collections.EMPTY_SET;
125  HashSet<RegisterEntry> set = new HashSet<RegisterEntry>();
126  for(LineTypeAccount lta : lineType.getLineTypeAccounts()) {
127  if(view == null || lta.getView() == null || lta.getView().getId() == view.getId()) {
128  Account account = new Account();
129  String acc = lta.getPattern();
130  if(acc.indexOf("%s") > -1) {
131  if(Strings.isBlank(account.getDescription())) {
132  account.setDescription(store.getFullDescription());
133  }
134  acc = convertPattern(acc, "\\%sctc", store.getContractDefinition().getAsContact());
135  acc = convertPattern(acc, "\\%scte", store.getContractDefinition().getDefaultContractPreference().getAsOperating(store));
136  acc = convertPattern(acc, "\\%sctd", store.getContractDefinition().getAsCash());
137  acc = convertPattern(acc, "\\%sctt", store.getContractDefinition().getDefaultContractPreference().getAsTax(store));
138  acc = convertPattern(acc, "\\%sctr", store.getContractDefinition().getDefaultContractPreference().getAsRetention(store));
139  acc = convertPattern(acc, "\\%sct", store.getId() + "");
140  acc = convertPattern(acc, "\\%s", store.getId() + "");
141  }
142  if(acc.indexOf("%ct") > -1) {
143  if(Strings.isBlank(account.getDescription())) {
144  account.setDescription(contract.getFullDescription());
145  }
146  acc = convertPattern(acc, "\\%ctc", contract.getContractDefinition().getAsContact());
147  acc = convertPattern(acc, "\\%cte", lineType.getContractPreference().getAsOperating(contract));
148  acc = convertPattern(acc, "\\%ctd", contract.getContractDefinition().getAsCash());
149  acc = convertPattern(acc, "\\%ctt", lineType.getContractPreference().getAsTax(contract));
150  acc = convertPattern(acc, "\\%ctr", lineType.getContractPreference().getAsRetention(contract));
151  acc = convertPattern(acc, "\\%ct", contract.getId() + "");
152  }
153  acc = convertPattern(acc, "\\%ltv", convertToString(lta.getTypeValue()));
154  account.setId(AccountFormat.expand(acc));
155  if(Strings.isBlank(account.getDescription())) {
156  MajorAccount ma = account.getParent();
157  if(ma != null) {
158  String extra = "";
159  if(lta.getType().equals(LineTypeAccountType.LTAT_TAX)) {
160  if(lta.getTypeValue() != 0) {
161  extra = " (" + lta.getTypeValue() + ")";
162  }
163  }
164  account.setDescription(ma.getDescription() + extra);
165  }
166  }
167  RegisterEntry re = new RegisterEntry();
168  re.setAccount(account);
169  re.setConcept(lineType.getName() + " - " + concept);
170  double _amount = 0;
171  if(lta.getType().equals(LineTypeAccountType.LTAT_TOTAL)) {
172  _amount = amount * lta.getCoefficient();
173  }
174  if(lta.getPosition().equals(AccountPosition.ACC_DEBIT)) {
175  re.setDebit(_amount);
176  } else {
177  re.setCredit(_amount);
178  }
179  if(re.getDebit() != 0 || re.getCredit() != 0) {
180  re.roundIt(fractionDigits);
181  set.add(re);
182  }
183  }
184  }
185  return set;
186  }
187 
188  private String convertToString(double value) {
189  return Double.valueOf(value).intValue() + "";
190  }
191 
192  private String convertPattern(String text, String pattern, String value) {
193  int l = value == null ? 0 : value.length();
194  if(text.contains(pattern.replaceAll("\\\\", "") + "%1") && l > 1) {
195  return text.replaceAll(pattern + "\\%1", value.substring(0, 1));
196  } else if(text.contains(pattern.replaceAll("\\\\", "") + "%2") && l > 2) {
197  return text.replaceAll(pattern + "\\%2", value.substring(0, 2));
198  }
199  return text.replaceAll(pattern, value);
200  }
201 
202 }
void setDescription(String description)
Definition: Account.java:45
ContractDefinition getContractDefinition()
Definition: Contract.java:125
ContractPreference getContractPreference()
Definition: LineType.java:77
Set< LineTypeAccount > getLineTypeAccounts()
Definition: LineType.java:92
void addAccount(Account account, String concept, double balance)
void addAccount(Account account, String concept, double debit, double credit)
void addAccount(Contract store, String concept, double debit, double credit)
Set< RegisterEntry > addAmount(RegisterView view, Contract contract, Contract store, LineType lineType, String concept, double amount)
static boolean near(double value, int digits)
Definition: Zero.java:30