BrightSide Workbench Full Report + Source Code
ClosingRegisters.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.register;
19 
20 import java.util.*;
21 import org.amic.util.date.CheckDate;
22 import org.turro.financials.db.FinancialsPU;
23 import org.turro.financials.entity.Account;
24 import org.turro.financials.entity.Register;
25 import org.turro.financials.entity.RegisterView;
26 import org.turro.financials.model.AccountFormat;
27 import org.turro.jpa.Dao;
28 import org.turro.math.Round;
29 
34 public class ClosingRegisters {
35 
36  private final String startString, endString, operatingString;
37  private final Collection<AccountItem> toInsert = new HashSet<>();
38 
39  public ClosingRegisters(String startString, String endString, String operatingString) {
40  this.startString = startString;
41  this.endString = endString;
42  this.operatingString = operatingString;
43  }
44 
45  public boolean existsClosingRegisters(int exercise) {
46  Collection<Register> list = getClosureRegisters(exercise);
47  return list != null && !(list.isEmpty());
48  }
49 
50  public void deleteClosingRegisters(int exercise) {
51  Collection<Register> list = getClosureRegisters(exercise);
52  for(Register r : list) {
53  new RegisterWrapper(r).delete();
54  }
55  }
56 
57  public void createClosingRegisters(int exercise) {
58  for(Register r : generateClosingRegisters(exercise)) {
59  if(!r.isEmpty()) {
60  new RegisterWrapper(r).save();
61  }
62  }
63  }
64 
65  public Collection<Register> generateClosingRegisters(int exercise) {
66  List<Register> list = new ArrayList<>();
67  Set amounts = getAmounts(exercise);
68  list.addAll(generateOperatingRegisters(exercise, amounts));
69  list.addAll(generateCloseAndStartRegisters(exercise, amounts));
70  return list;
71  }
72 
73  public Set<AccountItem> getNotClosed(int exercise) {
74  Dao dao = new FinancialsPU();
75  return new AccountSet(dao.getResultList(
76  "select new org.turro.financials.model.register.AccountItem(re.register.view.id, re.account.id, sum(round(re.debit - re.credit, 4) + 0.0)) " +
77  "from RegisterEntry as re " +
78  "where year(re.register.registerDate) = ? " +
79  "group by re.register.view.id, re.account.id " +
80  "having sum(round(re.debit - re.credit, 4) + 0.0) <> 0.0",
81  new Object[] { exercise}));
82  }
83 
84  public Double get129(int exercise) {
85  Dao dao = new FinancialsPU();
86  return (Double) dao.getSingleResultOrNull(
87  "select sum(round(re.debit - re.credit, 4) + 0.0) " +
88  "from RegisterEntry as re " +
89  "where year(re.register.registerDate) = ? " +
90  "and re.account.id = ?",
91  new Object[] { exercise, AccountFormat.expand("129") });
92  }
93 
94  public Collection<Integer> getExercises() {
95  Dao dao = new FinancialsPU();
96  return dao.getResultList(
97  "select distinct year(reg.registerDate) from Register reg");
98  }
99 
100  private Collection<Register> generateOperatingRegisters(int exercise, Set<AccountItem> amounts) {
101  List<Register> list = new ArrayList<>();
103  RegisterView current = null;
104  double g129 = 0.0;
105  for(AccountItem ai : amounts) {
106  if(current == null) {
107  current = ai.view;
108  reg.getRegister().setClosing(true);
109  reg.getRegister().setExclude(true);
110  reg.getRegister().setView(current);
111  reg.getRegister().setRegisterDate(new CheckDate(exercise, 12, 31, 0, 0, 0).getDate());
112  } else if(current.getId() != ai.view.getId()) {
113  if(!(reg.getRegister().isEmpty())) {
114  // add 129.0 account and sum current account balance
115  Account acc = new Account();
116  acc.setId("1290000000");
117  reg.addAccount(acc, operatingString, g129);
118  AccountItem ai129 = AccountItem.createAccountItem(current, acc, g129);
119  if(ai129 != null) toInsert.add(ai129);
120  list.add(reg.getRegister());
121  }
122  current = ai.view;
123  g129 = 0.0;
124  reg = new RegisterGenerator();
125  reg.getRegister().setClosing(true);
126  reg.getRegister().setExclude(true);
127  reg.getRegister().setView(current);
128  reg.getRegister().setRegisterDate(new CheckDate(exercise, 12, 31, 0, 0, 0).getDate());
129  }
130  if(ai.account.getId().startsWith("6") || ai.account.getId().startsWith("7")) {
131  g129 += new Round(ai.balance).decimals(reg.getFractionDigits()).value();
132  reg.addAccount(ai.account, operatingString, -ai.balance);
133  ai.balance = 0.0;
134  }
135  }
136  if(!(reg.getRegister().isEmpty())) {
137  // add 129.0 account and sum current account balance
138  Account acc = new Account();
139  acc.setId("1290000000");
140  reg.addAccount(acc, operatingString, g129);
141  AccountItem ai129 = AccountItem.createAccountItem(current, acc, g129);
142  if(ai129 != null) toInsert.add(ai129);
143  list.add(reg.getRegister());
144  }
145  amounts.addAll(toInsert);
146  return list;
147  }
148 
149  private Collection<Register> generateCloseAndStartRegisters(int exercise, Set<AccountItem> amounts) {
150  List<Register> list = new ArrayList<>();
151  RegisterGenerator close = new RegisterGenerator(), start = new RegisterGenerator();
152  RegisterView current = null;
153  for(AccountItem ai : amounts) {
154  if(current == null) {
155  current = ai.view;
156  close.getRegister().setClosing(true);
157  close.getRegister().setExclude(true);
158  close.getRegister().setView(current);
159  close.getRegister().setRegisterDate(new CheckDate(exercise, 12, 31, 0, 0, 0).getDate());
160  start.getRegister().setClosing(true);
161  start.getRegister().setExclude(false);
162  start.getRegister().setView(current);
163  start.getRegister().setRegisterDate(new CheckDate(exercise + 1, 1, 1, 0, 0, 0).getDate());
164  } else if(current.getId() != ai.view.getId()) {
165  list.add(close.getRegister());
166  list.add(start.getRegister());
167  current = ai.view;
168  close = new RegisterGenerator();
169  start = new RegisterGenerator();
170  close.getRegister().setClosing(true);
171  close.getRegister().setExclude(true);
172  close.getRegister().setView(current);
173  close.getRegister().setRegisterDate(new CheckDate(exercise, 12, 31, 0, 0, 0).getDate());
174  start.getRegister().setClosing(true);
175  start.getRegister().setExclude(false);
176  start.getRegister().setView(current);
177  start.getRegister().setRegisterDate(new CheckDate(exercise + 1, 1, 1, 0, 0, 0).getDate());
178  }
179  close.addAccount(ai.account, endString, -ai.balance);
180  start.addAccount(ai.account, startString, ai.balance);
181  }
182  if(!(close.getRegister().isEmpty())) {
183  list.add(close.getRegister());
184  list.add(start.getRegister());
185  }
186  return list;
187  }
188 
189  public Collection<Register> getClosureRegisters(int exercise) {
190  Dao dao = new FinancialsPU();
191  List<Register> list = dao.getResultList(
192  "select reg from Register reg " +
193  "where ( " +
194  "year(reg.registerDate) = ? " +
195  "and month(reg.registerDate) = 12 " +
196  "and reg.exclude = TRUE and reg.closing = TRUE " +
197  ") or ( " +
198  "year(reg.registerDate) = ? " +
199  "and month(reg.registerDate) = 1 " +
200  "and reg.exclude = FALSE and reg.closing = TRUE " +
201  ") and reg.document is null",
202  new Object[] { exercise, exercise + 1});
203  return list;
204  }
205 
206  private Set<AccountItem> getAmounts(int exercise) {
207  Dao dao = new FinancialsPU();
208  return new AccountSet(dao.getResultList(
209  "select new org.turro.financials.model.register.AccountItem(re.register.view.id, re.account.id, sum(round(re.debit - re.credit, 4) + 0.0)) " +
210  "from RegisterEntry as re " +
211  "where year(re.register.registerDate) = ? " +
212  "and re.register.exclude = FALSE " +
213  "group by re.register.view.id, re.account.id",
214  new Object[] { exercise}));
215  }
216 
217 }
void setExclude(boolean exclude)
Definition: Register.java:103
void setView(RegisterView view)
Definition: Register.java:163
void setClosing(boolean closing)
Definition: Register.java:87
void setRegisterDate(Date registerDate)
Definition: Register.java:127
ClosingRegisters(String startString, String endString, String operatingString)
Collection< Register > getClosureRegisters(int exercise)
Collection< Register > generateClosingRegisters(int exercise)
void addAccount(Account account, String concept, double balance)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419