BrightSide Workbench Full Report + Source Code
AccountsForDocumentLine.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.Collections;
21 import java.util.HashSet;
22 import java.util.Set;
23 import org.turro.string.Strings;
24 import org.turro.financials.entity.*;
25 
31 
32  private final Contract contract, store;
33  private final DocumentLine documentLine;
34  private final LineType lineType;
35  private final ContractPreference contractPreference;
36 
37  public AccountsForDocumentLine(DocumentLine documentLine) {
38  this.documentLine = documentLine;
39  this.contract = documentLine.getDocument().getContract();
40  this.store = documentLine.getStore();
41  this.lineType = documentLine.getLineType();
42  this.contractPreference = documentLine.getContractPreference();
43  }
44 
45 
46  public Set<RegisterEntry> getEntries(RegisterView view, Set<Long> bookDefs) {
47  if(store == null || lineType == null || documentLine.getSubtotal() == 0) return Collections.EMPTY_SET;
48  //int fractionDigits = documentLine.getDocument().getCurrency().getDefaultFractionDigits();
49  HashSet<RegisterEntry> set = new HashSet<>();
50  for(LineTypeAccount lta : lineType.getLineTypeAccounts()) {
51  if((view == null || lta.getView() == null || lta.getView().getId() == view.getId()) &&
52  (lta.getOperatingModifier() == null || documentLine.getDocument().getContract().getOperatingModifier().equals(lta.getOperatingModifier()))) {
53  Account account = new Account();
54  String acc = lta.getPattern();
55  if(acc.contains("%s")) {
56  if(Strings.isBlank(account.getDescription()) &&
57  !acc.contains("%sctt") && !acc.contains("%sctr")) {
58  account.setDescription(store.getFullDescription());
59  }
60  acc = convertPattern(acc, "\\%sctc", store.getContractDefinition().getAsContact());
61  acc = convertPattern(acc, "\\%scte", store.getContractDefinition().getDefaultContractPreference().getAsOperating(store));
62  acc = convertPattern(acc, "\\%sctd", store.getContractDefinition().getAsCash());
63  acc = convertPattern(acc, "\\%sctt", store.getContractDefinition().getDefaultContractPreference().getAsTax(store));
64  acc = convertPattern(acc, "\\%sctr", store.getContractDefinition().getDefaultContractPreference().getAsRetention(store));
65  acc = convertPattern(acc, "\\%sct", store.getId() + "");
66  acc = convertPattern(acc, "\\%s", store.getId() + "");
67  }
68  if(acc.contains("%ct")) {
69  if(Strings.isBlank(account.getDescription()) &&
70  !acc.contains("%ctt") && !acc.contains("%ctr") && !acc.contains("%ctq")) {
71  account.setDescription(contract.getFullDescription());
72  }
73  acc = convertPattern(acc, "\\%ctc", contract.getContractDefinition().getAsContact());
74  acc = convertPattern(acc, "\\%cte", contractPreference.getAsOperating(contract));
75  acc = convertPattern(acc, "\\%ctd", contract.getContractDefinition().getAsCash());
76  acc = convertPattern(acc, "\\%ctt", contractPreference.getAsTax(contract));
77  acc = convertPattern(acc, "\\%ctr", contractPreference.getAsRetention(contract));
78  acc = convertPattern(acc, "\\%ct", contract.getId() + "");
79  }
80  acc = convertPattern(acc, "\\%ltv", convertToString(lta.getTypeValue()));
81  acc = convertPattern(acc, "\\%ldv", convertToString(documentLine.getTax()));
82  acc = convertPattern(acc, "\\%ldq", convertToString(documentLine.getEquivalenceSurcharge()));
83  acc = convertPattern(acc, "\\%ldr", convertToString(documentLine.getRetention()));
84  account.setId(AccountFormat.expand(acc));
85  if(Strings.isBlank(account.getDescription())) {
86  MajorAccount ma = account.getParent();
87  if(ma != null) {
88  String extra = "";
89  if(lta.getType().equals(LineTypeAccountType.LTAT_TAX)) {
90  if(lta.getTypeValue() != 0) {
91  extra = " (" + lta.getTypeValue() + ")";
92  } else if(documentLine.getTax() != 0) {
93  extra = " (" + documentLine.getTax() + ")";
94  }
95  } else if(lta.getType().equals(LineTypeAccountType.LTAT_REQ)) {
96  if(lta.getTypeValue() != 0) {
97  extra = " (" + lta.getTypeValue() + ")";
98  } else if(documentLine.getEquivalenceSurcharge()!= 0) {
99  extra = " (" + documentLine.getEquivalenceSurcharge()+ ")";
100  }
101  } else if(lta.getType().equals(LineTypeAccountType.LTAT_RETENTION)) {
102  if(lta.getTypeValue() != 0) {
103  extra = " (" + lta.getTypeValue() + ")";
104  } else if(documentLine.getRetention() != 0) {
105  extra = " (" + documentLine.getRetention() + ")";
106  }
107  }
108  account.setDescription(ma.getDescription() + extra);
109  }
110  }
111  RegisterEntry re = new RegisterEntry();
112  re.setAccount(account);
113  re.setConcept(lineType.getComposedName());
114  double amount = 0;
115  if(lta.getType().equals(LineTypeAccountType.LTAT_TOTAL)) {
116  //amount = new Round(documentLine.getAmount() * lta.getCoefficient()).decimals(fractionDigits).value();
117  amount = getAmount(documentLine) * lta.getCoefficient();
118  } else if(lta.getType().equals(LineTypeAccountType.LTAT_TAX_BASE)) {
119  //amount = new Round(documentLine.getTaxable() * lta.getCoefficient()).decimals(fractionDigits).value();
120  amount = getTaxable(documentLine) * lta.getCoefficient();
121  } else if(lta.getType().equals(LineTypeAccountType.LTAT_TAX)) {
122  if(lta.getTypeValue() == 0 || lta.getTypeValue() == documentLine.getTax()) {
123  //amount = new Round(documentLine.getTaxValue() * lta.getCoefficient()).decimals(fractionDigits).value();
124  amount = getTaxValue(documentLine) * lta.getCoefficient();
125  }
126  } else if(lta.getType().equals(LineTypeAccountType.LTAT_REQ)) {
127  if(lta.getTypeValue() == 0 || lta.getTypeValue() == documentLine.getEquivalenceSurcharge()) {
128  //amount = new Round(documentLine.getTaxValue() * lta.getCoefficient()).decimals(fractionDigits).value();
129  amount = getEquivalenceSurchargeValue(documentLine) * lta.getCoefficient();
130  }
131  } else if(lta.getType().equals(LineTypeAccountType.LTAT_RETENTION)) {
132  if(lta.getTypeValue() == 0 || lta.getTypeValue() == documentLine.getRetention()) {
133  //amount = new Round(documentLine.getTaxValue() * lta.getCoefficient()).decimals(fractionDigits).value();
134  amount = getRetained(documentLine) * lta.getCoefficient();
135  }
136  } else if(lta.getType().equals(LineTypeAccountType.LTAT_DISCOUNT)) {
137  //amount = new Round(documentLine.getDiscount() * lta.getCoefficient()).decimals(fractionDigits).value();
138  amount = getDiscount(documentLine) * lta.getCoefficient();
139  }
140  if(lta.getPosition().equals(AccountPosition.ACC_DEBIT)) {
141  re.setDebit(amount);
142  } else {
143  re.setCredit(amount);
144  }
145  if(re.getDebit() != 0 || re.getCredit() != 0) {
146  MajorAccount ma = account.getParent();
147  if(ma != null) {
149  if(bf != null) {
150  bookDefs.add(bf.getId());
151  }
152  }
153  set.add(re);
154  }
155  }
156  }
157  return set;
158  }
159 
160  private String convertToString(double value) {
161  return AccountCreator.convertToString(value);
162  }
163 
164  private String convertPattern(String text, String pattern, String value) {
165  int l = value == null ? 0 : value.length();
166  if(text.contains(pattern.replaceAll("\\\\", "") + "%1") && l > 1) {
167  return text.replaceAll(pattern + "\\%1", value.substring(0, 1));
168  } else if(text.contains(pattern.replaceAll("\\\\", "") + "%2") && l > 2) {
169  return text.replaceAll(pattern + "\\%2", value.substring(0, 2));
170  } else if(text.contains(pattern.replaceAll("\\\\", "") + "%3") && l > 3) {
171  return text.replaceAll(pattern + "\\%3", value.substring(0, 3));
172  }
173  if(text.contains(pattern.replaceAll("\\\\", "") + "%-1") && l > 1) {
174  return text.replaceAll(pattern + "\\%-1", value.substring(1));
175  } else if(text.contains(pattern.replaceAll("\\\\", "") + "%-2") && l > 2) {
176  return text.replaceAll(pattern + "\\%-2", value.substring(2));
177  } else if(text.contains(pattern.replaceAll("\\\\", "") + "%-3") && l > 3) {
178  return text.replaceAll(pattern + "\\%-3", value.substring(3));
179  }
180  return value == null ? text : text.replaceAll(pattern, value);
181  }
182 
183  public double getSubtotal(DocumentLine line) {
184  //return line.getQuantity() == 0 ? line.getPrice() : line.getPrice() * line.getQuantity();
185  return line.getSubtotal();
186  }
187 
188  public double getRetained(DocumentLine line) {
189  //return (line.getSubtotal() - line.getDiscount()) * (line.getRetention() / 100);
190  return line.getRetained();
191  }
192 
193  public double getDiscount(DocumentLine line) {
194  //return ((getSubtotal(line) - line.getDiscountMoney()) * (line.getDiscountPerCent() / 100)) + line.getDiscountMoney();
195  return line.getDiscount();
196  }
197 
198  public Double getTaxable(DocumentLine line) {
199  //return getSubtotal(line) - getDiscount(line);
200  return line.getTaxable();
201  }
202 
203  public Double getTaxValue(DocumentLine line) {
204 // if(line.getTax() > 0) {
205 // return getFullTaxValue(line) * line.getTax() / line.getFullTax();
206 // }
207 // return 0.0;
208 // //return (getRealAmount(line) + getRetained(line)) - getTaxable(line);
209  return line.getRealTaxValue();
210  }
211 
213 // if(line.getEquivalenceSurcharge() > 0) {
214 // return getFullTaxValue(line) * line.getEquivalenceSurcharge() / line.getFullTax();
215 // }
216 // return 0.0;
217  return line.getEquivalenceSurchargeValue();
218  }
219 
220 
221  public Double getAmount(DocumentLine line) {
222 // if(line.getDocument().getContract().getOperatingModifier().isSumTax(line)) {
223 // return (getTaxable(line) * (1 + line.getFullTax() / 100)) - getRetained(line);
224 // } else {
225 // return getTaxable(line) - getRetained(line);
226 // }
227  return line.getAmount();
228  }
229 
230 // public Double getRealAmount(DocumentLine line) {
231 // return (getTaxable(line) * (1 + line.getFullTax() / 100)) - getRetained(line);
232 // }
233 }
void setDescription(String description)
Definition: Account.java:45
ContractDefinition getContractDefinition()
Definition: Contract.java:125
OperatingModifier getOperatingModifier()
Definition: Contract.java:228
Set< LineTypeAccount > getLineTypeAccounts()
Definition: LineType.java:92
static String convertToString(double value)
Set< RegisterEntry > getEntries(RegisterView view, Set< Long > bookDefs)