BrightSide Workbench Full Report + Source Code
Register.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.entity;
19 
20 import java.util.*;
21 import javax.persistence.*;
22 import org.amic.util.date.CheckDate;
23 import org.turro.elephant.db.IdUtils;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.model.Amount;
26 import org.turro.financials.model.AmountSet;
27 import org.turro.financials.model.document.DocumentAmounts;
28 import org.turro.jpa.entity.IDaoEntity;
29 import org.turro.reflection.MappingSet;
30 
35 @Entity
36 @Table(indexes={
37  @Index(name="IndexRegister", columnList="idRegister"),
38  @Index(name="IndexDate", columnList="registerDate")
39 })
40 public class Register implements java.io.Serializable, IDaoEntity {
41 
42  @Id
43  @GeneratedValue(strategy=GenerationType.IDENTITY)
44  @Column(name="IDENTIFIER")
45  private long id;
46 
47  @Column(nullable=false)
48  @Temporal(value = TemporalType.DATE)
49  private Date registerDate;
50 
51  @Column(nullable=false)
52  private long idRegister;
53 
54  @ManyToOne
55  private RegisterView view;
56 
57  private boolean exclude;
58 
59  private boolean closing;
60 
61  private boolean regularizeVAT;
62 
63  private boolean regularizeIRPF;
64 
65  @ManyToOne
66  private Document document;
67 
68  @OneToMany(mappedBy = "register", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
69  private Set<BookRegister> bookRegisters = new HashSet<BookRegister>();
70 
71  @OneToMany(mappedBy = "register", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
72  //@OrderBy(value="entryOrder")
73  private Set<RegisterEntry> registerEntries = new HashSet<RegisterEntry>();
74 
75  public Set<BookRegister> getBookRegisters() {
76  return bookRegisters;
77  }
78 
79  public void setBookRegisters(Set<BookRegister> bookRegisters) {
80  this.bookRegisters = bookRegisters;
81  }
82 
83  public boolean isClosing() {
84  return closing;
85  }
86 
87  public void setClosing(boolean closing) {
88  this.closing = closing;
89  }
90 
91  public Document getDocument() {
92  return document;
93  }
94 
95  public void setDocument(Document document) {
96  this.document = document;
97  }
98 
99  public boolean isExclude() {
100  return exclude;
101  }
102 
103  public void setExclude(boolean exclude) {
104  this.exclude = exclude;
105  }
106 
107  public long getId() {
108  return id;
109  }
110 
111  public void setId(long id) {
112  this.id = id;
113  }
114 
115  public long getIdRegister() {
116  return idRegister;
117  }
118 
119  public void setIdRegister(long idRegister) {
120  this.idRegister = idRegister;
121  }
122 
123  public Date getRegisterDate() {
124  return registerDate;
125  }
126 
127  public void setRegisterDate(Date registerDate) {
128  if(this.registerDate != null && !new CheckDate(this.registerDate).sameYear(registerDate)) {
129  bookRegisters.clear();
130  idRegister = 0;
131  }
132  this.registerDate = registerDate;
133  }
134 
135  public Set<RegisterEntry> getRegisterEntries() {
136  return registerEntries;
137  }
138 
139  public void setRegisterEntries(Set<RegisterEntry> registerEntries) {
140  this.registerEntries = registerEntries;
141  }
142 
143  public boolean isRegularizeIRPF() {
144  return regularizeIRPF;
145  }
146 
147  public void setRegularizeIRPF(boolean regularizeIRPF) {
148  this.regularizeIRPF = regularizeIRPF;
149  }
150 
151  public boolean isRegularizeVAT() {
152  return regularizeVAT;
153  }
154 
155  public void setRegularizeVAT(boolean regularizeVAT) {
156  this.regularizeVAT = regularizeVAT;
157  }
158 
160  return view;
161  }
162 
163  public void setView(RegisterView view) {
164  this.view = view;
165  }
166 
167  /* IDaoEntity */
168 
169  @Override
170  public Object entityId() {
171  return id;
172  }
173 
174  @Override
175  public boolean isEmpty() {
176  removeEmpties();
177  return registerEntries.isEmpty() ||
178  view == null ||
179  registerDate == null;
180  }
181 
182  @Override
183  public void prepareSave() {
184  IDaoEntity.super.prepareSave();
185  if(idRegister == 0) {
186  idRegister = IdUtils.getMaxLongIdFromLong(
187  new FinancialsPU(), "Register", "idRegister",
188  new String[] {
189  "year(registerDate) = " + new CheckDate(registerDate).getYear(),
190  "view.id = " + view.getId()
191  });
192  }
193  }
194 
195  @Override
196  public Collection<Collection> collections() {
197  return List.of(getBookRegisters(), getRegisterEntries());
198  }
199 
200  /* Helpers */
201 
202  public double getDebit() {
203  double r = 0;
204  for(RegisterEntry re : registerEntries) {
205  r += re.getDebit();
206  }
207  return r;
208  }
209 
210  public double getCredit() {
211  double r = 0;
212  for(RegisterEntry re : registerEntries) {
213  r += re.getCredit();
214  }
215  return r;
216  }
217 
218  public void reuseLines(Collection<RegisterEntry> entries) {
219  Iterator<RegisterEntry> it1 = getRegisterEntries().iterator();
220  Iterator<RegisterEntry> it2 = entries.iterator();
221  RegisterEntry re1, re2 = null;
222  while(it1.hasNext()) {
223  re1 = it1.next();
224  if(it2.hasNext()) {
225  re2 = it2.next();
226  } else {
227  re2 = null;
228  }
229  // new lines is finished
230  if(re2 == null) {
231  re1.clearLine();
232  } else {
233  re1.assignValuesFrom(re2);
234  re1.setRegister(this);
235  }
236  re2 = null;
237  }
238  // there are still new lines
239  if(re2 != null) {
240  re1 = new RegisterEntry();
241  re1.assignValuesFrom(re2);
242  re1.setRegister(this);
243  registerEntries.add(re1);
244  }
245  while(it2.hasNext()) {
246  re2 = it2.next();
247  re1 = new RegisterEntry();
248  re1.assignValuesFrom(re2);
249  re1.setRegister(this);
250  registerEntries.add(re1);
251  }
252  }
253 
254  /* Books */
255 
256  public void killUnusedBooks(Set<Long> bookDefs) {
257  Iterator<BookRegister> it = getBookRegisters().iterator();
258  while(it.hasNext()) {
259  boolean used = false;
260  BookRegister br = it.next();
261  for(long bd : bookDefs) {
262  if(br.getBookDefinition().getId() == bd) {
263  used = true;
264  break;
265  }
266  }
267  if(!used) {
268  it.remove();
269  }
270  }
271  }
272 
273  public String getBookString() {
274  StringBuilder sb = new StringBuilder();
275  for(BookRegister br : getBookRegisters()) {
276  if(sb.length() > 0) {
277  sb.append(" | ");
278  }
279  sb.append(br.getBookDefinition().getDescription());
280  sb.append(" [" + (br.getBookOrder() == 0 ? "<auto>" : br.getBookOrder()) +"]");
281  }
282  return sb.toString();
283  }
284 
285  private transient AmountSet as;
286 
287  public AmountSet getAmounts(Collection<String> majors) {
288  if(as == null) {
289  as = new AmountSet();
290  if(document != null) {
291  for(RegisterEntry re : registerEntries) {
292  Account acc = re.getAccount();
293  for(String major : majors) {
294  if(acc.getId().startsWith(major)) {
295  double tax = Integer.valueOf(acc.getId().substring(5)) / 10.0;
296  if(tax > 0) {
297  double amount = re.getDebit() + re.getCredit();
298  as.addAmount(new Amount(tax, amount / (tax / 100.0), 0.0, 0.0, 2));
299  }
300  break;
301  }
302  }
303  }
304  }
305  }
306  return as;
307  }
308 
309  private transient DocumentAmounts da;
310 
312  if(da == null) {
313  if(document != null) {
314  da = document.getAmounts();
315  }
316  }
317  return da;
318  }
319 
320  /* XML Serializer */
321 
322  public MappingSet getSerializerMappings() {
323  MappingSet set = new MappingSet();
324  set.addMapping(Register.class, 1,
325  new String[] { "idRegister", "registerDate" },
326  new String[] { "view", "registerEntries" });
327  set.addMapping(RegisterView.class, 2,
328  new String[] { "id", "name" },
329  null);
330  set.addMapping(RegisterEntry.class, 2,
331  new String[] { "debit", "credit", "approved", "conciliated" },
332  new String[] { "account" } );
333  set.addMapping(Account.class, 3,
334  new String[] { "id", "description" },
335  null);
336  return set;
337  }
338 
339 }
static long getMaxLongIdFromLong(Dao dao, String table, String field)
Definition: IdUtils.java:91
void assignValuesFrom(RegisterEntry registerEntry)
void reuseLines(Collection< RegisterEntry > entries)
Definition: Register.java:218
void setRegularizeIRPF(boolean regularizeIRPF)
Definition: Register.java:147
Collection< Collection > collections()
Definition: Register.java:196
void setExclude(boolean exclude)
Definition: Register.java:103
AmountSet getAmounts(Collection< String > majors)
Definition: Register.java:287
void setView(RegisterView view)
Definition: Register.java:163
void setRegularizeVAT(boolean regularizeVAT)
Definition: Register.java:155
void setRegisterEntries(Set< RegisterEntry > registerEntries)
Definition: Register.java:139
Set< RegisterEntry > getRegisterEntries()
Definition: Register.java:135
void setIdRegister(long idRegister)
Definition: Register.java:119
void killUnusedBooks(Set< Long > bookDefs)
Definition: Register.java:256
void setClosing(boolean closing)
Definition: Register.java:87
void setBookRegisters(Set< BookRegister > bookRegisters)
Definition: Register.java:79
void setDocument(Document document)
Definition: Register.java:95
DocumentAmounts getDocumentAmounts()
Definition: Register.java:311
void setRegisterDate(Date registerDate)
Definition: Register.java:127