BrightSide Workbench Full Report + Source Code
SettlementSet.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.treasury.settlement;
19 
20 import java.util.Date;
21 import java.util.List;
22 import java.util.TreeSet;
23 import org.amic.util.date.CheckDate;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.Account;
26 import org.turro.financials.entity.Contract;
27 import org.turro.financials.entity.RegisterView;
28 import org.turro.financials.menu.FinancialsMenu;
29 import org.turro.financials.model.register.RegisterGenerator;
30 import org.turro.financials.model.register.ViewWrapper;
31 import org.turro.i18n.I_;
32 import org.turro.jpa.Dao;
33 
38 public class SettlementSet extends TreeSet<SettlementEntry> {
39 
40  static final String
41  CREDITOR_EXP = "475%",
42  DEBTOR_EXP = "470%",
43  ON_ACCOUNT_EXP = "473%";
44 
45  private Dao dao;
46  private Date date, settlementDate;
47 
48  public Date getDate() {
49  return date;
50  }
51 
52  public void setDate(Date date) {
53  this.date = date;
54  fillData();
55  }
56 
57  public Date getSettlementDate() {
58  return settlementDate;
59  }
60 
61  public void setSettlementDate(Date settlementDate) {
62  this.settlementDate = settlementDate;
63  }
64 
65  public Dao getDao() {
66  if(dao == null) {
67  dao = new FinancialsPU();
68  }
69  return dao;
70  }
71 
72  private void fillData() {
73  clear();
74  List<Object[]> list = (List<Object[]>) getDao().getResultList(
75  "select a.id, a.description, sum(e.debit), sum(e.credit) from RegisterEntry e " +
76  "join e.register r " +
77  "join e.account a " +
78  "where r.registerDate <= ? " +
79  //"and r.registerDate >= ? " +
80  "and r.view.id = 1 " +
81  //"and (r.exclude = FALSE and r.closing = FALSE) " +
82  "and a.id like ? " +
83 // "and r.document is not null " +
84 // "and not exists (" +
85 // " select re from RegisterEntry re where re.account.id like '5%' " +
86 // " and re.register.id = r.id " +
87 // ") " +
88  "group by a.id, a.description",
89  new Object[] { date, /*getInitialDate(),*/ CREDITOR_EXP });
90  for(Object[] objs : list) {
91  SettlementEntry se = new SettlementEntry((String) objs[0], (String) objs[1]);
92  double debit = (Double) (objs[2] == null ? 0.0 : objs[2]),
93  credit = (Double) (objs[3] == null ? 0.0 : objs[3]);
94  se.setAmount(-(debit - credit));
95  add(se);
96  }
97  list = (List<Object[]>) getDao().getResultList(
98  "select a.id, a.description, sum(e.debit), sum(e.credit) from RegisterEntry e " +
99  "join e.register r " +
100  "join e.account a " +
101  "where r.registerDate <= ? " +
102  //"and r.registerDate >= ? " +
103  "and r.view.id = 1 " +
104  //"and (r.exclude = FALSE and r.closing = FALSE) " +
105  "and (a.id like ? or a.id like ?)" +
106 // "and r.document is not null " +
107 // "and not exists (" +
108 // " select re from RegisterEntry re where re.account.id like '5%' " +
109 // " and re.register.id = r.id " +
110 // ") " +
111  "group by a.id, a.description",
112  new Object[] { date, /*getInitialDate(),*/ DEBTOR_EXP, ON_ACCOUNT_EXP });
113  for(Object[] objs : list) {
114  SettlementEntry se = new SettlementEntry((String) objs[0], (String) objs[1]);
115  double debit = (Double) (objs[2] == null ? 0.0 : objs[2]),
116  credit = (Double) (objs[3] == null ? 0.0 : objs[3]);
117  se.setAmount((debit - credit));
118  add(se);
119  }
120  }
121 
122  public Date getInitialDate() {
123  return new CheckDate(date).setMonth(1).setDay(1).getDate();
124  }
125 
126  public double getDebtorTotal() {
127  double total = 0.0;
128  for(SettlementEntry se : this) {
129  if(se.isChecked() && se.isDebtor()) {
130  total += se.getAmount();
131  }
132  }
133  return total;
134  }
135 
136  public double getCreditorTotal() {
137  double total = 0.0;
138  for(SettlementEntry se : this) {
139  if(se.isChecked() && (se.isCreditorVAT() || se.isCreditorIRPF())) {
140  total += se.getAmount();
141  }
142  }
143  return total;
144  }
145 
146  public void generateRegister(Contract store) {
148  RegisterView formalView = ViewWrapper.getFormalView();
149  reg.getRegister().setView(formalView);
150  reg.getRegister().setRegisterDate(date);
151 
152  double total = 0.0;
153 
154  for(SettlementEntry se : this) {
155  Account acc = new Account();
156  acc.setId(se.getAccount());
157  if(se.isChecked() && se.isCreditorIRPF()) {
158  reg.addAccount(acc, I_.get("Settlement") + " " + I_.get("Treasury"), se.getAmount(), 0);
159  total -= se.getAmount();
160  }
161  }
162 
163  if(total < 0.0) {
164  Account acc = new Account();
165  acc.setId("4751000000");
166  reg.addAccount(acc, I_.get("Settlement") + " " + I_.get("Treasury"), 0.0, -total);
167  } else if(total > 0.0) {
168  Account acc = new Account();
169  acc.setId("4730000000");
170  reg.addAccount(acc, I_.get("Settlement") + " " + I_.get("Treasury"), total, 0.0);
171  }
172 
174 
176  reg2.getRegister().setView(formalView);
177  reg2.getRegister().setRegisterDate(settlementDate);
178 
179  if(total < 0.0) {
180  Account acc = new Account();
181  acc.setId("4751000000");
182  reg2.addAccount(acc, I_.get("Settlement") + " " + I_.get("Treasury"), -total, 0.0);
183  } else if(total > 0.0) {
184  Account acc = new Account();
185  acc.setId("4730000000");
186  reg2.addAccount(acc, I_.get("Settlement") + " " + I_.get("Treasury"), 0.0, total);
187  }
188 
189  for(SettlementEntry se : this) {
190  Account acc = new Account();
191  acc.setId(se.getAccount());
192  if(se.isChecked() && se.isCreditorVAT()) {
193  reg2.addAccount(acc, I_.get("Settlement") + " " + I_.get("Treasury"), se.getAmount(), 0);
194  total -= se.getAmount();
195  } else if(se.isChecked() && se.isDebtor()) {
196  reg2.addAccount(acc, I_.get("Settlement") + " " + I_.get("Treasury"), 0, se.getAmount());
197  total += se.getAmount();
198  }
199  }
200 
201  if(total < 0.0) {
202  reg2.addAccount(store, I_.get("Settlement") + " " + I_.get("Treasury"), 0.0, -total);
203  } else if(total > 0.0) {
204  reg2.addAccount(store, I_.get("Settlement") + " " + I_.get("Treasury"), total, 0.0);
205  }
206 
208  }
209 
210 }
void setView(RegisterView view)
Definition: Register.java:163
void setRegisterDate(Date registerDate)
Definition: Register.java:127
void addAccount(Account account, String concept, double balance)
static String get(String msg)
Definition: I_.java:41