BrightSide Workbench Full Report + Source Code
bsfinancials-core/src/main/java/org/turro/financials/model/business/CompanyWrapper.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.business;
19 
20 import java.text.DecimalFormat;
21 import java.text.NumberFormat;
22 import java.util.Currency;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Locale;
26 import org.amic.util.date.CheckDate;
27 import org.turro.contacts.Contact;
28 import org.turro.elephant.context.Application;
29 import org.turro.financials.db.FinancialsPU;
30 import org.turro.financials.entity.Company;
31 import org.turro.financials.entity.Contract;
32 import org.turro.financials.entity.Department;
33 import org.turro.financials.entity.Document;
34 import org.turro.financials.entity.DocumentLine;
35 import org.turro.financials.entity.Headquarters;
36 import org.turro.financials.entity.Register;
37 import org.turro.financials.entity.Service;
38 import org.turro.financials.model.document.DocumentWrapper;
39 import org.turro.financials.model.register.RegisterWrapper;
40 import org.turro.jpa.Dao;
41 import org.turro.jpa.DaoTransaction;
42 import org.turro.jpa.entity.DaoEntity;
43 import org.turro.jpa.entity.EntityCollections;
44 import org.turro.sql.SqlClause;
45 import org.turro.zkoss.locale.Currencies;
46 
51 public class CompanyWrapper extends DaoEntity<Company, Long> {
52 
53  public CompanyWrapper() {
54  super(getDefaultCompany(true));
55  }
56 
57  @Override
58  protected Dao createDao() {
59  return new FinancialsPU();
60  }
61 
62  @Override
63  public Company save() {
65  EntityCollections.entities(entity.getHeadquarters()).removeEmpties();
66  for(Headquarters hq : entity.getHeadquarters()) {
67  EntityCollections.entities(hq.getDepartments()).removeEmpties();
68  }
69 
70  _defaultCompany = super.save();
71  return _defaultCompany;
72  }
73 
74  @Override
75  public boolean delete() {
76  // do nothing
77  return false;
78  }
79 
80  @Override
81  protected boolean shouldLog() {
82  return false;
83  }
84 
85 // public static Collection<Register> save(Company company, String startString, String endString, String operatingString) {
86 // Collection<Register> list = setCompanyClosingDate(company.getClosingDate(), startString, endString, operatingString);
87 // Iterator<Service> its = company.getServices().iterator();
88 // while(its.hasNext()) {
89 // Service s = its.next();
90 // if(Strings.isBlank(s.getName())) {
91 // its.remove();
92 // }
93 // }
94 // Iterator<Headquarters> ith = company.getHeadquarters().iterator();
95 // while(ith.hasNext()) {
96 // Headquarters h = ith.next();
97 // if(Strings.isBlank(h.getIdContact())) {
98 // ith.remove();
99 // } else {
100 // Iterator<Department> itd = h.getDepartments().iterator();
101 // while(itd.hasNext()) {
102 // Department d = itd.next();
103 // if(Strings.isBlank(d.getName())) {
104 // itd.remove();
105 // }
106 // }
107 // }
108 // }
109 // company.setContactId(company.getContactId());
110 // _defaultCompany = new FinancialsPU().saveObject(company);
111 // return list;
112 // }
113 
114  public static boolean isValidDate(Date date) {
115  return isValidDate(date, false);
116  }
117 
118  public static boolean isValidDate(Date date, boolean closing) {
119  Date dateClosing = getCompanyClosingDate();
120  return (date == null || dateClosing == null || dateClosing.before(date) ||
121  (closing && new CheckDate(date).getYear() == getEndOfYearClosing(dateClosing)));
122  }
123 
124  public static Date getCompanyClosingDate() {
125  Company company = getDefaultCompany();
126  if(company != null) {
127  return company.getClosingDate();
128  }
129  return null;
130  }
131 
132  public static int getEndOfYearClosing() {
134  }
135 
136  public static int getEndOfYearClosing(Date date) {
137  CheckDate cd = new CheckDate(date);
138  if(cd.getDay() == 31 && cd.getMonth() == 12) {
139  return cd.getYear();
140  }
141  return 0;
142  }
143 
144 // public static Collection<Register> setCompanyClosingDate(Date date, String startString, String endString, String operatingString) {
145 // Collection<Register> list = null;
146 // Company company = getDefaultCompany();
147 // if(company != null) {
148 // Date previous = company.getClosingDate();
149 // int previousExercise = previous != null ? new CheckDate(previous).getYear() : 0,
150 // currentExercise = date != null ? new CheckDate(date).getYear() : 0;
151 // if(getEndOfYearClosing(date) > 0) {
152 // currentExercise++;
153 // }
154 // if(previousExercise < currentExercise) {
155 // ClosingRegisters cr = new ClosingRegisters(startString, endString, operatingString);
156 // if(previousExercise == 0) previousExercise = currentExercise - 1;
157 // for(int exercise = previousExercise; exercise < currentExercise; exercise++) {
158 // if(!cr.existsClosingRegisters(exercise)) {
159 // //cr.deleteClosingRegisters(exercise);
160 // list = cr.generateClosingRegisters(exercise);
161 // }
162 // }
163 // } else if(previousExercise > currentExercise) {
164 // //ClosingRegisters cr = new ClosingRegisters(startString, endString, operatingString);
165 // //for(int exercise = currentExercise + 1; exercise <= previousExercise; exercise++) {
166 // // cr.deleteClosingRegisters(exercise);
167 // //}
168 // }
169 // company.setClosingDate(date);
170 // new FinancialsPU().saveObject(company);
171 // }
172 // return list;
173 // }
174 
175  public static Currency getCompanyCurrency() {
176  Company company = getDefaultCompany();
177  if(company != null) {
178  Contact contact = (Contact) company.getIContact().getContact();
179  if(contact != null) {
180  return contact.getCurrency();
181  }
182  }
183  return Currencies.getDefault();
184  }
185 
186  public static Locale getCompanyLocale() {
187  Company company = getDefaultCompany();
188  if(company != null) {
189  Contact contact = (Contact) company.getIContact().getContact();
190  if(contact != null) {
191  return contact.getLocale();
192  }
193  }
194  return Application.getUsedLocale();
195  }
196 
197  public static NumberFormat getCurrencyFormatter() {
198  return DecimalFormat.getCurrencyInstance(getCompanyLocale());
199  }
200 
201  public static Company getCompanyFrom(DocumentLine line) {
202  if(line != null && line.getStore() != null) {
203  Contract store = line.getStore();
204  if(store.getDepartment() != null && store.getDepartment().getHeadquarters() != null) {
205  return store.getDepartment().getHeadquarters().getCompany();
206  }
207  if(store.getService() != null) {
208  return store.getService().getCompany();
209  }
210  }
211  return getDefaultCompany();
212  }
213 
215  if(line != null && line.getStore() != null) {
216  Contract store = line.getStore();
217  if(store.getDepartment() != null) {
218  return store.getDepartment().getHeadquarters();
219  }
220  }
221  return null;
222  }
223 
225  if(line != null && line.getStore() != null) {
226  Contract store = line.getStore();
227  return store.getDepartment();
228  }
229  return null;
230  }
231 
232  public static Service getServiceFrom(DocumentLine line) {
233  if(line != null && line.getStore() != null) {
234  Contract store = line.getStore();
235  return store.getService();
236  }
237  return null;
238  }
239 
240  private static Company _defaultCompany;
241 
242  public static Company getDefaultCompany() {
243  return getDefaultCompany(false);
244  }
245 
246  public static Company getDefaultCompany(boolean reload) {
247  if(reload || _defaultCompany == null) {
248  _defaultCompany = (Company) new FinancialsPU().getSingleResult("select company from Company as company");
249  }
250  return _defaultCompany;
251  }
252 
253  public static void renumRegisters(Integer year, boolean books) {
254  if(year == null || year == 0) return;
255 // Dao dao = new FinancialsPU();
256 // List<Register> l = dao.getResultList(
257 // "select r from Register r " +
258 // "where year(r.registerDate) = ? " +
259 // "and r.view = 1 " +
260 // "order by r.registerDate, r.closing desc",
261 // new Object[] {
262 // year
263 // });
264  int count = 1;
265  try(DaoTransaction transaction = new DaoTransaction(new FinancialsPU())) {
266  List<Register> l = transaction.getDao().getResultList(
267  "select r from Register r " +
268  "where year(r.registerDate) = ? " +
269  "and r.view = 1 " +
270  "order by r.registerDate, r.closing desc",
271  new Object[] {
272  year
273  });
274  for(Register r : l) {
275  r.setIdRegister(count++);
276  transaction.saveObject(r);
277  }
278  }
279  if(books) {
280  renumBookRegisters(year);
281  }
282  }
283 
284  public static void renumBookRegisters(Integer year) {
285  Dao dao = new FinancialsPU();
286  dao.executeUpdate(
287  "delete from BookRegister br " +
288  "where exists (" +
289  " select r from Register r " +
290  " where br.register.id = r.id " +
291  " and year(r.registerDate) = ? " +
292  " and r.view = 1 " +
293  ")",
294  new Object[] {
295  year
296  });
297  List<Register> l = dao.getResultList(
298  "select r from Register r " +
299  "where year(r.registerDate) = ? " +
300  "and r.view = 1 " +
301  "order by r.idRegister",
302  new Object[] {
303  year
304  });
305  for(Register r : l) {
307  if(!r.getBookRegisters().isEmpty()) {
308  dao.saveObject(r);
309  }
310  }
311  }
312 
313  public static Service getDefaultService() {
314  return (Service) new FinancialsPU().getSingleResult(
315  "select service from Service as service " +
316  "where service.id = (select min(id) from Service)");
317  }
318 
320  return (Contract) new FinancialsPU().getSingleResult(
321  "select ctc from Contract as ctc " +
322  "where ctc.id = (select min(id) from Contract where service is not null)");
323  }
324 
325  public static void removeExercise(Integer year) {
326  Dao dao = new FinancialsPU();
327  SqlClause.delete("Document")
328  .where().equal("year(receiptDate)", year)
329  .dao(dao)
330  .execute();
331  SqlClause.delete("DocumentLine")
332  .where("not exists").sub(
333  SqlClause.select("*").from("Document d")
334  .where("d.identifier = document_identifier"))
335  .dao(dao)
336  .executeNative();
337  SqlClause.delete("DocumentRelation")
338  .where("not exists").sub(
339  SqlClause.select("*").from("Document d")
340  .where("d.identifier = ancestor_identifier"))
341  .or("not exists").sub(
342  SqlClause.select("*").from("Document d")
343  .where("d.identifier = descendant_identifier"))
344  .dao(dao)
345  .executeNative();
346  SqlClause.delete("Register")
347  .where().equal("year(registerDate)", year)
348  .dao(dao)
349  .execute();
350  SqlClause.delete("RegisterEntry")
351  .where("not exists").sub(
352  SqlClause.select("*").from("Register r")
353  .where("r.identifier = register_identifier"))
354  .dao(dao)
355  .executeNative();
356  SqlClause.delete("BookRegister")
357  .where("not exists").sub(
358  SqlClause.select("*").from("Register r")
359  .where("r.identifier = register_identifier"))
360  .dao(dao)
361  .executeNative();
362  }
363 
364 }
static Register prepareBookRegister(Register register)
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380
static EntityCollections entities(Collection values)