BrightSide Workbench Full Report + Source Code
m303m/VatEntry.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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 
19 package org.turro.financials.treasury.m303m;
20 
21 import javax.persistence.NoResultException;
22 import org.turro.jpa.Dao;
23 
28 public class VatEntry {
29  private String account;
30  private int vat;
31  private Double amountVat, declaredVat, pendingVat;
32  private EntryMonth month;
33 
34  public VatEntry(int vat, EntryMonth month) {
35  this.vat = vat;
36  this.month = month;
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[] { month.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 
81  public EntryMonth getMonth() {
82  return month;
83  }
84 
85  public Dao getDao() {
86  return month.getDao();
87  }
88 
89  private void fillData() {
90  try {
91  // Current month 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[] { month.getFinalDate(), month.getMonthInitialDate(), month.getEntry().getType().getVatAccount() + "0" + vat });
103  if(objs != null) {
104  account = (String) objs[0];
105  if(month.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 month 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[] { month.getFinalDeclaredDate(), month.getMonthDeclaredDate(), month.getEntry().getType().getVatAccount() + "0" + vat });
127  if(objs != null) {
128  account = (String) objs[0];
129  if(month.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  }
140 
141 }
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380