BrightSide Workbench Full Report + Source Code
EntryQuarter.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.m303;
19 
20 import java.util.Date;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.TreeMap;
24 import org.amic.util.date.CheckDate;
25 import org.turro.jpa.Dao;
26 
31 public class EntryQuarter {
32 
33  private ModelEntry entry;
34  private int quarter;
35  private Map<Integer, VatEntry> vatMap;
36  private Double amountOp, totalOp, toDeclareOp, toDeclareVat;
37 
38  public EntryQuarter(ModelEntry entry, int quarter) {
39  this.entry = entry;
40  this.quarter = quarter;
41  fillData();
42  }
43 
44  public double getAmountOp() {
45  return amountOp == null ? 0.0 : amountOp;
46  }
47 
48  public double getTotalOp() {
49  return totalOp == null ? 0.0 : totalOp;
50  }
51 
52  public void addToDeclareVat(Double toDeclareVat) {
53  this.toDeclareVat += toDeclareVat;
54  }
55 
56  public double getToDeclareTotalVat() {
57  return toDeclareVat == null ? 0.0 : toDeclareVat;
58  }
59 
60  public void addToDeclareOp(Double toDeclareOp) {
61  this.toDeclareOp += toDeclareOp;
62  }
63 
64  public double getToDeclareTotalOp() {
65  return toDeclareOp == null ? 0.0 : toDeclareOp;
66  }
67 
68  public int getQuarter() {
69  return quarter;
70  }
71 
72  public Map<Integer, VatEntry> getVatMap() {
73  return vatMap;
74  }
75 
76  public double getTotalInputVat() {
77  double result = 0.0;
78  for(VatEntry ve : vatMap.values()) {
79  if(getEntry().getType().getBookId() == 1 || getEntry().getType().getBookId() == 3) {
80  result += ve.getAmountVat();
81  }
82  }
83  return result;
84  }
85 
86  public double getTotalOutputVat() {
87  double result = 0.0;
88  for(VatEntry ve : vatMap.values()) {
89  if(getEntry().getType().getBookId() == 2) {
90  result += ve.getAmountVat();
91  }
92  }
93  return result;
94  }
95 
96  public Dao getDao() {
97  return entry.getDao();
98  }
99 
100  public ModelEntry getEntry() {
101  return entry;
102  }
103 
104  private void fillData() {
105  Set<Integer> vats = entry.getPossibleVats();
106  vatMap = new TreeMap<Integer, VatEntry>();
107  for(Integer vat : vats) {
108  vatMap.put(vat, new VatEntry(vat, this));
109  }
110  totalOp = 0.0;
111  toDeclareOp = 0.0;
112  toDeclareVat = 0.0;
113  for(VatEntry ve : vatMap.values()) {
114  totalOp += ve.getAmountOp();
115  }
116  amountOp = (Double) getDao().getSingleResult(
117  "select sum(e.debit - e.credit) from RegisterEntry e " +
118  "join e.register r " +
119  "join r.bookRegisters b with b.bookDefinition.id = ? " +
120  "where e.register.registerDate <= ? " +
121  "and e.register.registerDate >= ? " +
122  "and e.register.view.id = 1 " +
123  "and (e.register.exclude = FALSE and e.register.closing = FALSE and e.register.regularizeVAT = FALSE) " +
124  "and e.account.id like ?",
125  new Object[] { entry.getType().getBookId(), getFinalDate(), getQuarterInitialDate(), entry.getType().getOpAccount() });
126  if(amountOp != null && entry.getType().getOpAccount().startsWith("7")) {
127  amountOp = -amountOp;
128  }
129  }
130 
131  public Date getInitialDate() {
132  return new CheckDate(entry.getExercise(), 1, 1, 0, 0, 0).getDate();
133  }
134 
135  public Date getQuarterInitialDate() {
136  return new CheckDate(entry.getExercise(), ((quarter - 1) * 3) + 1, 1, 0, 0, 0).getDate();
137  }
138 
139  public Date getFinalDate() {
140  return new CheckDate(entry.getExercise(), ((quarter - 1) * 3) + 4, 1, 0, 0, 0).addDays(-1).getDate();
141  }
142 
143  public Date getQuarterDeclaredDate() {
144  return new CheckDate(entry.getExercise(), ((quarter - 1) * 3) + 1, 1, 0, 0, 0).addDays(26).getDate();
145  }
146 
147  public Date getFinalDeclaredDate() {
148  return new CheckDate(entry.getExercise(), ((quarter - 1) * 3) + 4, 1, 0, 0, 0).addDays(-1).addDays(25).getDate();
149  }
150 
151 }
EntryQuarter(ModelEntry entry, int quarter)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380