BrightSide Workbench Full Report + Source Code
RegisterWrapper.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.HashSet;
21 import java.util.Iterator;
22 import java.util.Set;
23 import org.turro.financials.db.FinancialsPU;
24 import org.turro.financials.entity.*;
25 import org.turro.financials.model.AccountsForDocumentLine;
26 import org.turro.financials.model.business.CompanyWrapper;
27 import org.turro.jpa.Dao;
28 import org.turro.jpa.entity.XMLSerializer;
29 import org.turro.log.SystemLogType;
30 import org.turro.log.SystemLogger;
31 
36 public class RegisterWrapper {
37 
38  protected Register register;
39  private Set<Long> bookDefs;
40  private boolean generate;
41 
42  public RegisterWrapper(Register register) {
43  this(register, true);
44  }
45 
46  public RegisterWrapper(Register register, boolean generate) {
47  this.register = register;
48  this.generate = generate;
49  bookDefs = new HashSet<Long>();
50  generateEntries();
51  }
52 
53  public Register getRegister() {
54  return register;
55  }
56 
57  public EntrySet getEntries() {
58  return generateEntries();
59  }
60 
61  private EntrySet generateEntries() {
62  EntrySet es = new EntrySet();
63  if(generate && register.getDocument() != null) {
64  register.setRegularizeVAT(register.getDocument().isRegularizeVAT());
65  register.setRegularizeIRPF(register.getDocument().isRegularizeIRPF());
66  RegisterView view = register.getView();
67  if(register.getDocument().getForcedView() != null) {
68  view = null;
69  if(register.getDocument().getForcedView().getId() != register.getView().getId()) {
70  register.reuseLines(es.getRegisterEntries());
71  return es;
72  }
73  }
74  bookDefs.clear();
75  for(DocumentLine dl : register.getDocument().getDocumentLines()) {
76  if(dl.getSubtotal() != 0) {
77  for(RegisterEntry re : new AccountsForDocumentLine(dl).getEntries(view, bookDefs)) {
78  es.addEntry(re);
79  }
80  }
81  }
82  es.roundEntries(register.getDocument().getCurrency().getDefaultFractionDigits());
83  new RegisterAssuredBalance(es, register.getDocument()).normalize();
84  es.calculateBalance();
85  register.reuseLines(es.getRegisterEntries());
86  register.killUnusedBooks(bookDefs);
87  } else {
88  bookDefs.clear();
89  for(RegisterEntry re : register.getRegisterEntries()) {
90  if(re.getDebit() != 0 || re.getCredit() != 0) {
91  MajorAccount ma = re.getAccount().getParent();
92  if(ma != null) {
94  if(bf != null) {
95  bookDefs.add(bf.getId());
96  }
97  }
98  es.addEntry(re);
99  }
100  }
101  es.calculateBalance();
102  }
103  getBookString();
104  return es;
105  }
106 
107  public String getBookString() {
108  Iterator<BookRegister> itbr = register.getBookRegisters().iterator();
109  // Clear temporary assigned books
110  while(itbr.hasNext()) {
111  BookRegister br = itbr.next();
112  boolean found = false;
113  for(Long idBd : bookDefs) {
114  if(br.getBookDefinition().getId() == idBd) {
115  found = true;
116  break;
117  }
118  }
119  if(!found && br.getId() == 0) {
120  itbr.remove();
121  }
122  }
123  // Generate new books
124  StringBuilder sb = new StringBuilder();
125  for(Long idBd : bookDefs) {
126  boolean found = false;
127  for(BookRegister br : register.getBookRegisters()) {
128  if(br.getBookDefinition().getId() == idBd) {
129  if(sb.length() > 0) {
130  sb.append(" | ");
131  }
132  sb.append(br.getBookDefinition().getDescription());
133  sb.append(" [" + (br.getBookOrder() == 0 ? "<auto>" : br.getBookOrder()) +"]");
134  found = true;
135  break;
136  }
137  }
138  if(!found) {
139  BookDefinition bd = new FinancialsPU().find(BookDefinition.class, idBd);
140  if(bd != null) {
141  BookRegister br = new BookRegister();
142  br.setRegister(register);
143  br.setBookDefinition(bd);
144  register.getBookRegisters().add(br);
145  if(sb.length() > 0) {
146  sb.append(" | ");
147  }
148  sb.append(bd.getDescription());
149  sb.append(" [<auto>]");
150  }
151  }
152  }
153  return sb.toString();
154  }
155 
156  public Register save() {
157  register.prepareSave();
158  register = new FinancialsPU().saveObject(register);
159  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, register, "saved", new XMLSerializer(register).data());
160  return register;
161  }
162 
163  public boolean delete() {
164  if(register.getDocument() == null) {
165  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, register, "deleted", new XMLSerializer(register).data());
166  new FinancialsPU().deleteObject(register);
167  return true;
168  }
169  return false;
170  }
171 
172  public static Register prepareBookRegister(Register register) {
173  Dao dao = new FinancialsPU();
174  Set<Long> bookDefs = new HashSet<Long>();
175  for(RegisterEntry re : register.getRegisterEntries()) {
176  if(re.getDebit() != 0 || re.getCredit() != 0) {
177  MajorAccount ma = re.getAccount().getParent();
178  if(ma != null) {
180  if(bf != null) {
181  bookDefs.add(bf.getId());
182  }
183  }
184  }
185  }
186  for(Long bookDef : bookDefs) {
187  BookRegister br = new BookRegister();
188  br.setRegister(register);
189  br.setBookDefinition(dao.find(BookDefinition.class, bookDef));
190  register.getBookRegisters().add(br);
191  }
192  register.prepareSave();
193  return register;
194  }
195 
196  public boolean checkSave() {
197  return CompanyWrapper.isValidDate(register.getRegisterDate(), register.isClosing() && register.isExclude());
198  }
199 
200 }
void setBookDefinition(BookDefinition bookDefinition)
void addEntry(RegisterEntry entry)
Definition: EntrySet.java:31
void roundEntries(int fractionDigits)
Definition: EntrySet.java:61
Collection< RegisterEntry > getRegisterEntries()
Definition: EntrySet.java:41
static Register prepareBookRegister(Register register)
RegisterWrapper(Register register, boolean generate)
void deleteObject(Object obj)
Definition: Dao.java:162
static ISystemLogger getInstance()
void doLog(SystemLogType type, Object entity, String comment, Serializable data)