BrightSide Workbench Full Report + Source Code
DocumentAmountsByBook.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.model.document;
20 
21 import java.util.Currency;
22 import org.turro.financials.entity.BookDefinition;
23 import org.turro.financials.entity.Document;
24 import org.turro.financials.entity.DocumentLine;
25 import org.turro.zkoss.locale.Currencies;
26 
31 public class DocumentAmountsByBook {
32 
33  private final Document document;
34  private final BookDefinition bookDefinition;
35  private AmountTaxableSet taxables;
36  private AmountRetainedSet retentions;
37 
38  public DocumentAmountsByBook(Document document, BookDefinition bookDefinition) {
39  this.document = document;
40  this.bookDefinition = bookDefinition;
41  createAmounts();
42  }
43 
45  return taxables;
46  }
47 
49  return retentions;
50  }
51 
52  public double getAmount() {
53  double amount = 0.0;
54  for(AmountTaxable at : taxables) {
55  amount += at.getAmount();
56  }
57  return amount;
58  }
59 
60  public double getDiscount() {
61  double amount = 0.0;
62  for(AmountTaxable at : taxables) {
63  amount += at.getDiscount();
64  }
65  return amount;
66  }
67 
68  public double getTotal() {
69  double amount = 0.0;
70  for(AmountTaxable at : taxables) {
71  if(at.isPerCent(0.0)) { //Contract operating modifier was set
72  amount += at.getTaxable();
73  } else {
74  amount += at.getTotal();
75  }
76  }
77  for(AmountRetained ar : retentions) {
78  amount -= ar.getRetained();
79  }
80  return amount;
81  }
82 
83  public boolean hasReq() {
84  return document.getContract() != null &&
86  }
87 
88  private void createAmounts() {
89  taxables = new AmountTaxableSet();
90  retentions = new AmountRetainedSet();
91  if(getCurrency() != null) {
92  int fractionDigits = getCurrency().getDefaultFractionDigits();
93  if(document.getContract() != null) {
94  int count = 0;
95  for(DocumentLine dl : document.getDocumentLines()) {
96  if(correspondsToBook(dl)) {
97  dl.setLineOrder(count++);
98  double tax = dl.getFullTax();
99  if(!document.getContract().getOperatingModifier().isSumTax(dl)) {
100  tax = 0.0; // Contract operating modifier was set
101  }
102  taxables.add(tax, dl.getTax(), dl.getEquivalenceSurcharge(),
103  dl.getTaxValue(), dl.getRealTaxValue(), dl.getEquivalenceSurchargeValue(),
104  dl.getSubtotal(), dl.getTaxable(), fractionDigits);
105  if(dl.getRetention() != 0.0) {
106  retentions.add(dl.getRetention(), dl.getTaxable(), fractionDigits);
107  }
108  }
109  }
110  taxables.clearEmpty();
111  retentions.clearEmpty();
112  }
113  }
114  }
115 
116  private Currency currency;
117 
118  public Currency getCurrency() {
119  if(currency == null && document.getContract() != null) {
120  currency = document.getContract().getCurrency();
121  }
122  return currency == null ? Currencies.getDefault() : currency;
123  }
124 
125  // TODO: Tipificar les inversions
126  private boolean correspondsToBook(DocumentLine dl) {
127  if(dl.getLineType() != null && dl.getLineType().getContractPreference().getName().startsWith("Inv.")) {
128  if(bookDefinition.getDescription().contains("inversions")) {
129  return true;
130  } else {
131  return false;
132  }
133  } else {
134  if(bookDefinition.getDescription().contains("inversions")) {
135  return false;
136  } else {
137  return true;
138  }
139  }
140  }
141 
142 }
OperatingModifier getOperatingModifier()
Definition: Contract.java:228
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
ContractPreference getContractPreference()
Definition: LineType.java:77
boolean add(Double perCent, Double taxable, int fractionDigits)
boolean add(Double perCent, Double tax, Double req, Double taxValue, Double taxRealValue, Double reqValue, Double subtotal, Double taxable, int fractionDigits)
DocumentAmountsByBook(Document document, BookDefinition bookDefinition)