BrightSide Workbench Full Report + Source Code
AnnualTransactionsSet.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.document;
19 
20 import java.util.*;
21 import org.amic.util.date.CheckDate;
22 import org.turro.contacts.Address;
23 import org.turro.contacts.Contact;
24 import org.turro.elephant.db.WhereClause;
25 import org.turro.financials.db.FinancialsPU;
26 import org.turro.financials.entity.DocumentLine;
27 import org.turro.jpa.Dao;
28 
33 public abstract class AnnualTransactionsSet extends TreeSet<AnnualTransaction> {
34 
35  public Collection<String> getColumns() {
36  return Arrays.asList(new String[] {
37  getLabel("View"),
38  getLabel("Key"),
39  getLabel("Global identifier"),
40  getLabel("Name"),
41  getLabel("Address"),
42  "Q1",
43  "Q2",
44  "Q3",
45  "Q4",
46  getLabel("Amount")
47  });
48  }
49 
50  public Collection<Object[]> getRows() {
51  List<Object[]> l = new ArrayList<Object[]>();
52  for(AnnualTransaction at : this) {
53  Contact c = at.getContact();
54  Address a = c.getAddressMap().get("Fiscal");
55  l.add(new Object[] {
56  at.getView(),
57  at.getOpKey(),
59  c.getName(),
60  (a != null ? a.getAddressString() : ""),
61  at.getOperationsAmount(1),
62  at.getOperationsAmount(2),
63  at.getOperationsAmount(3),
64  at.getOperationsAmount(4),
66  });
67  }
68  return l;
69  }
70 
71  public abstract String getLabel(String key);
72 
73  public void fillSet(int year) {
74  //TODO: add to line type the transaction type, ex.: A, B
75  WhereClause wc = new WhereClause();
76  wc.addClause("select line from DocumentLine line join line.document doc");
77  wc.addClause("where year(doc.receiptDate) = :year");
78  wc.addNamedValue("year", year);
79  wc.addClause("and doc.documentDefinition.id in (12, 2, 1, 51, 49, 46, 5, 15, 18, 52)");
80  wc.addClause("and line.lineType.id in (12, 4, 97, 1, 3, 125, 150, 149, 151, 146, 147, 122, 123, 103, 126, 19, 135, 132, 169)");
81 
82  Dao dao = new FinancialsPU();
83 
84  for(DocumentLine line : (List<DocumentLine>) dao.getResultList(wc)) {
85  String opKey = "E";
86  switch((int) line.getLineType().getId()) {
87  case 12:
88  case 4:
89  case 97:
90  case 125:
91  case 150:
92  case 149:
93  case 151:
94  case 146:
95  case 147:
96  case 122:
97  case 123:
98  case 103:
99  case 126:
100  case 19:
101  case 135:
102  case 132:
103  opKey = "A";
104  break;
105  case 1:
106  case 3:
107  case 169:
108  opKey ="B";
109  break;
110  }
111  AnnualTransaction at = getAnnualTransaction(
112  (line.getDocument().getForcedView() == null ? "" : line.getDocument().getForcedView().getName()),
113  opKey,
114  line.getDocument().getContract().getContractor());
115  at.addOperationsAmount(new CheckDate(line.getDocument().getReceiptDate()).getQuarter(), line.getToDeclare());
116  }
117  }
118 
119  private AnnualTransaction getAnnualTransaction(String view, String opKey, String contractor) {
120  AnnualTransaction annualTrans = new AnnualTransaction(view, opKey, contractor);
121  for(AnnualTransaction at : this) {
122  if(at.compareTo(annualTrans) == 0) {
123  return at;
124  }
125  }
126  add(annualTrans);
127  return annualTrans;
128  }
129 
130 }
Map< String, Address > getAddressMap()
Definition: Contact.java:503
void addNamedValue(String name, Object value)
void addOperationsAmount(int quarter, double operationsAmount)