BrightSide Workbench Full Report + Source Code
m303/VatEntry.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 javax.persistence.NoResultException;
21 import org.turro.jpa.Dao;
22 
27 public class VatEntry {
28 
29  private String account;
30  private int vat;
31  private Double amountVat, declaredVat, pendingVat;
32  private EntryQuarter quarter;
33 
34  public VatEntry(int vat, EntryQuarter quarter) {
35  this.vat = vat;
36  this.quarter = quarter;
37  fillData();
38  }
39 
40  public void setPendingVat(double value) {
41  pendingVat = value;
42  }
43 
44  public Double getPendingVat() {
45  return pendingVat;
46  }
47 
48  public String getAccount() {
49  if(account == null) {
50  account = (String) getDao().getSingleResult(
51  "select a.id from Account as a where a.id like ?",
52  new Object[] { quarter.getEntry().getType().getVatAccount() + "00" + vat });
53  }
54  return account;
55  }
56 
57  public double getAmountVat() {
58  return amountVat;
59  }
60 
61  public double getToDeclareAmountVat() {
62  return amountVat - (declaredVat + pendingVat);
63  }
64 
65  public double getAmountOp() {
66  return getAmountVat() / (getVat() / 100);
67  }
68 
69  public double getToDeclareAmountOp() {
70  return getToDeclareAmountVat() / (getVat() / 100);
71  }
72 
73  public double getDeclaredVat() {
74  return declaredVat;
75  }
76 
77  public double getVat() {
78  return vat / 10.0d;
79  }
80 
82  return quarter;
83  }
84 
85  public Dao getDao() {
86  return quarter.getDao();
87  }
88 
89  private void fillData() {
90  try {
91  // Current quarter amount
92  Object[] objs = (Object[]) getDao().getSingleResult(
93  "select a.id, sum(e.debit), sum(e.credit) from RegisterEntry e " +
94  "join e.register r " +
95  "join e.account a " +
96  "where r.registerDate <= ? " +
97  "and r.registerDate >= ? " +
98  "and r.view.id = 1 " +
99  "and (r.exclude = FALSE and r.closing = FALSE and r.regularizeVAT = FALSE) " +
100  "and a.id like ? " +
101  "group by a.id",
102  new Object[] { quarter.getFinalDate(), quarter.getQuarterInitialDate(), quarter.getEntry().getType().getVatAccount() + "0" + vat });
103  if(objs != null) {
104  account = (String) objs[0];
105  if(quarter.getEntry().getType().getVatAccount().startsWith("472")) {
106  amountVat = (Double) (objs[1] == null ? 0.0 : objs[1]);
107  } else {
108  amountVat = (Double) (objs[2] == null ? 0.0 : objs[2]);
109  }
110  }
111  } catch(NoResultException ex) {
112  amountVat = 0.0;
113  }
114  try {
115  // Current quarter declared
116  Object[] objs = (Object[]) getDao().getSingleResult(
117  "select a.id, sum(e.debit), sum(e.credit) from RegisterEntry e " +
118  "join e.register r " +
119  "join e.account a " +
120  "where r.registerDate <= ? " +
121  "and r.registerDate >= ? " +
122  "and r.view.id = 1 " +
123  "and (r.exclude = FALSE and r.closing = FALSE and r.regularizeVAT = FALSE) " +
124  "and a.id like ? " +
125  "group by a.id",
126  new Object[] { quarter.getFinalDeclaredDate(), quarter.getQuarterDeclaredDate(), quarter.getEntry().getType().getVatAccount() + "0" + vat });
127  if(objs != null) {
128  account = (String) objs[0];
129  if(quarter.getEntry().getType().getVatAccount().startsWith("472")) {
130  declaredVat = (Double) (objs[2] == null ? 0.0 : objs[2]);
131  } else {
132  declaredVat = (Double) (objs[1] == null ? 0.0 : objs[1]);
133  }
134  }
135  } catch(NoResultException ex) {
136  declaredVat = 0.0;
137  }
138  pendingVat = 0.0;
139 // if(declaredVat == 0) {
140 // try {
141 // // Corrections to previous quarters
142 // Object[] objs = (Object[]) getDao().getSingleResult(
143 // "select a.id, sum(e.debit), sum(e.credit) from RegisterEntry e " +
144 // "join e.register r " +
145 // "join e.account a " +
146 // "where r.registerDate < ? " +
147 // "and r.registerDate >= ? " +
148 // "and r.view.id = 1 " +
149 // "and r.exclude = FALSE and r.closing = FALSE " +
150 // "and a.id like ? " +
151 // "group by a.id",
152 // new Object[] { quarter.getQuarterInitialDate(), quarter.getInitialDate(), quarter.getEntry().getType().getVatAccount() + "00" + vat });
153 // Double pastAmountVat = 0.0, pastDeclaredVat = 0.0;
154 // if(objs != null) {
155 // account = (String) objs[0];
156 // if(quarter.getEntry().getType().getVatAccount().startsWith("472")) {
157 // pastAmountVat = (Double) (objs[1] == null ? 0.0 : objs[1]);
158 // pastDeclaredVat = (Double) (objs[2] == null ? 0.0 : objs[2]);
159 // } else {
160 // pastAmountVat = (Double) (objs[2] == null ? 0.0 : objs[2]);
161 // pastDeclaredVat = (Double) (objs[1] == null ? 0.0 : objs[1]);
162 // }
163 // declaredVat += pastDeclaredVat - pastAmountVat;
164 // }
165 // } catch(NoResultException ex) {}
166 // }
167  }
168 
169 }
VatEntry(int vat, EntryQuarter quarter)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380