BrightSide Workbench Full Report + Source Code
PortfolioStatus.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.contract;
19 
20 import java.util.Date;
21 import org.turro.financials.db.FinancialsPU;
22 import org.turro.financials.entity.Contract;
23 import org.turro.financials.entity.MajorAccount;
24 import org.turro.financials.model.AccountFormat;
25 import org.turro.financials.model.business.CompanyWrapper;
26 import org.turro.jpa.Dao;
27 import org.turro.util.CompareUtil;
28 
33 public class PortfolioStatus implements Comparable<PortfolioStatus> {
34 
35  private Contract contract;
36  private Double balance, effects;
37 
38  public PortfolioStatus(Contract contract) {
39  this.contract = contract;
40  loadStatus();
41  }
42 
43  public Double getBalance() {
44  return balance;
45  }
46 
47  public Contract getContract() {
48  return contract;
49  }
50 
51  public Double getEffects() {
52  return effects;
53  }
54 
55  public String getMajor() {
56  return contract.getContractDefinition().getAsContact().substring(0, 2);
57  }
58 
59  public String getMajorString() {
61  }
62 
63  public boolean isEmpty() {
64  return (balance == null || balance == 0.0) &&
65  (effects == null || effects == 0.0);
66  }
67 
68  private void loadStatus() {
69  Date closingDate = CompanyWrapper.getCompanyClosingDate();
70  Dao dao = new FinancialsPU();
71  balance = (Double) dao.getSingleResult(
72  "select sum(re.debit - re.credit) from RegisterEntry re " +
73  "join re.register r " +
74  "where re.account.id = ? " +
75  "and r.registerDate >= ?",
76  new Object[] {
77  AccountFormat.expand(contract.getContractDefinition().getAsContact() + "." + contract.getId()),
78  closingDate
79  });
80  effects = (Double) dao.getSingleResult(
81  "select sum(re.debit - re.credit) from RegisterEntry re " +
82  "join re.register r " +
83  "where re.account.id = ? " +
84  "and r.registerDate >= ?",
85  new Object[] {
86  AccountFormat.expand(contract.getContractDefinition().getAsContact().substring(0, 2) + "1." + contract.getId()),
87  closingDate
88  });
89  if("40".equals(getMajor()) || "41".equals(getMajor())) {
90  balance = balance == null ? null : -balance;
91  effects = effects == null ? null : -effects;
92  }
93  }
94 
95  @Override
96  public int compareTo(PortfolioStatus o) {
97  int result = CompareUtil.compare(getMajor(), o.getMajor());
98  if(result == 0) {
99  result = CompareUtil.compare(contract.getName(), o.contract.getName());
100  }
101  if(result == 0) {
102  result = CompareUtil.compare(contract.getId(), o.contract.getId());
103  }
104  return result;
105  }
106 
107 }
ContractDefinition getContractDefinition()
Definition: Contract.java:125
static MajorAccount getMajor(String account)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380