BrightSide Workbench Full Report + Source Code
AccountCreator.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;
19 
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23 import org.turro.string.Strings;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.*;
26 
31 public class AccountCreator {
32 
33  private Contract contract, store;
34  private Document document;
35  private LineType lineType;
36 
37  public AccountCreator(Document document, LineType lineType, Contract store) {
38  this.contract = document.getContract();
39  this.store = store;
40  this.document = document;
41  this.lineType = lineType;
42  }
43 
44  public Set<Account> getAccounts() {
45  HashSet<Account> set = new HashSet<Account>();
46  for(LineTypeAccount lta : lineType.getLineTypeAccounts()) {
47  Account account = new Account();
48  String acc = lta.getPattern();
49 
50  if(acc.indexOf("%s") > -1) {
51  if(Strings.isBlank(account.getDescription())) {
52  account.setDescription(store.getFullDescription());
53  }
54  acc = convertPattern(acc, "\\%sctc", store.getContractDefinition().getAsContact());
55  acc = convertPattern(acc, "\\%scte", store.getContractDefinition().getDefaultContractPreference().getAsOperating(store));
56  acc = convertPattern(acc, "\\%sctd", store.getContractDefinition().getAsCash());
57  acc = convertPattern(acc, "\\%sctt", store.getContractDefinition().getDefaultContractPreference().getAsTax(store));
58  acc = convertPattern(acc, "\\%sctr", store.getContractDefinition().getDefaultContractPreference().getAsRetention(store));
59  acc = convertPattern(acc, "\\%sct", store.getId() + "");
60  acc = convertPattern(acc, "\\%s", store.getId() + "");
61  }
62  if(acc.indexOf("%ct") > -1) {
63  if(Strings.isBlank(account.getDescription())) {
64  account.setDescription(contract.getFullDescription());
65  }
66  acc = convertPattern(acc, "\\%ctc", contract.getContractDefinition().getAsContact());
67  acc = convertPattern(acc, "\\%cte", lineType.getContractPreference().getAsOperating(contract));
68  acc = convertPattern(acc, "\\%ctd", contract.getContractDefinition().getAsCash());
69  acc = convertPattern(acc, "\\%ctt", lineType.getContractPreference().getAsTax(contract));
70  acc = convertPattern(acc, "\\%ctr", lineType.getContractPreference().getAsRetention(contract));
71  acc = convertPattern(acc, "\\%ct", contract.getId() + "");
72  }
73  acc = convertPattern(acc, "\\%ltv", convertToString(lta.getTypeValue()));
74  // No dinamyc instance
75  //acc = convertPattern(acc, "\\%ldv", convertToString(documentLine.getTax()));
76  account.setId(AccountFormat.expand(acc));
77  if(Strings.isBlank(account.getDescription())) {
78  MajorAccount ma = account.getParent();
79  if(ma != null) {
80  account.setDescription(ma.getDescription() +
81  (lta.getTypeValue() != 0 ? (" (" + lta.getTypeValue() + ")") : ""));
82  }
83  }
84  set.add(account);
85  }
86  return set;
87  }
88 
89  public static String convertToString(double value) {
90  return Double.valueOf(value * 10).intValue() + "";
91  }
92 
93  private String convertPattern(String text, String pattern, String value) {
94  int l = value == null ? 0 : value.length();
95  if(text.contains(pattern.replaceAll("\\\\", "") + "%1") && l > 1) {
96  return text.replaceAll(pattern + "\\%1", value.substring(0, 1));
97  } else if(text.contains(pattern.replaceAll("\\\\", "") + "%2") && l > 2) {
98  return text.replaceAll(pattern + "\\%2", value.substring(0, 2));
99  }
100  return text.replaceAll(pattern, value);
101  }
102 
103  public static AccountSet getPosibleAccounts() {
104  AccountSet accs = new AccountSet();
105 
106  List<Contract> contracts = new FinancialsPU().getResultList(
107  "select ctc from Contract as ctc " +
108  "order by ctc.contractDefinition.name, ctc.contractDefinition.id, ctc.id"
109  );
110 
111  for(Contract contract : contracts) {
112  for(final RelatedDocument rd : contract.getContractDefinition().getRelatedDocuments()) {
114 
115  List<Contract> stores = new FinancialsPU().getResultList(
116  "select ctc from Contract as ctc " +
117  "where stock = TRUE " +
118  "and ctc.contractDefinition = ? " +
119  "order by ctc.contractDefinition.name, ctc.contractDefinition.id, ctc.id",
120  new Object[] { rsd.getRelatedStore() }
121  );
122 
123  for(Contract store : stores) {
124  Document document = new Document();
125  document.setDocumentDefinition(rd.getDocumentDefinition());
126  document.setContract(contract);
127 
129  for(ContractPreference cp : contract.getContractPreferences()) {
130  rlt.getLineType().setContractPreference(cp);
131  accs.addAll(new AccountCreator(document, rlt.getLineType(), store).getAccounts());
132  }
133  }
134  }
135  }
136  }
137  }
138 
139  return accs;
140  }
141 
142  public static AccountSet getOnUseAccounts() {
143  AccountSet accs = new AccountSet();
144 
145  List<Account> accounts = new FinancialsPU().getResultList(
146  "select distinct entry.account from RegisterEntry as entry"
147  );
148 
149  accs.addAll(accounts);
150 
151  return accs;
152  }
153 
154 }
void setDescription(String description)
Definition: Account.java:45
ContractDefinition getContractDefinition()
Definition: Contract.java:125
Set< ContractPreference > getContractPreferences()
Definition: Contract.java:145
void setContract(Contract contract)
Definition: Document.java:123
void setDocumentDefinition(DocumentDefinition documentDefinition)
Definition: Document.java:175
ContractPreference getContractPreference()
Definition: LineType.java:77
Set< LineTypeAccount > getLineTypeAccounts()
Definition: LineType.java:92
AccountCreator(Document document, LineType lineType, Contract store)
static String convertToString(double value)