BrightSide Workbench Full Report + Source Code
ContractPortfolio.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.command;
20 
21 import org.turro.financials.db.FinancialsPU;
22 import org.turro.financials.entity.Contract;
23 import org.turro.financials.model.AccountFormat;
24 import org.turro.jpa.Dao;
25 import org.turro.math.Zero;
26 
31 public class ContractPortfolio {
32 
33  private final Contract contract;
34  private double invoice, balance;
35  private boolean filled = false;
36 
37  public ContractPortfolio(Contract contract) {
38  this.contract = contract;
39  }
40 
41  public Contract getContract() {
42  if(!filled) fillData();
43  return contract;
44  }
45 
46  public double getInvoice() {
47  if(!filled) fillData();
48  return invoice;
49  }
50 
51  public double getBalance() {
52  if(!filled) fillData();
53  return Zero.near(balance, 2) ? 0.0 : balance;
54  }
55 
56  private void fillData() {
57  filled = true;
58  Dao dao = new FinancialsPU();
59  Object o;
60  // Provider
61  if(contract.getContractDefinition().getId() == 55) {
62  o = dao.getSingleResult(
63  "select sum(re.debit - re.credit) from RegisterEntry as re " +
64  "where(re.account.id like '" + AccountFormat.expand("400." + contract.getId()) + "' " +
65  "or re.account.id like '" + AccountFormat.expand("401." + contract.getId()) + "' " +
66  "or re.account.id like '" + AccountFormat.expand("410." + contract.getId()) + "' " +
67  "or re.account.id like '" + AccountFormat.expand("411." + contract.getId()) + "')");
68  if(o instanceof Number) {
69  balance = ((Number) o).doubleValue();
70  }
71  o = dao.getSingleResult(
72  "select sum(re.credit) from RegisterEntry as re " +
73  "where(re.account.id like '" + AccountFormat.expand("400." + contract.getId()) + "' " +
74  "or re.account.id like '" + AccountFormat.expand("410." + contract.getId()) + "')");
75  if(o instanceof Number) {
76  invoice = ((Number) o).doubleValue();
77  }
78  // Customer
79  } else if(contract.getContractDefinition().getId() == 48 || contract.getContractDefinition().getId() == 56) {
80  o = dao.getSingleResult(
81  "select sum(re.debit - re.credit) from RegisterEntry as re " +
82  "where (re.account.id like '" + AccountFormat.expand("430." + contract.getId()) + "' " +
83  "or re.account.id like '" + AccountFormat.expand("431." + contract.getId()) + "' " +
84  "or re.account.id like '" + AccountFormat.expand("440." + contract.getId()) + "' " +
85  "or re.account.id like '" + AccountFormat.expand("441." + contract.getId()) + "')");
86  if(o instanceof Number) {
87  balance = -((Number) o).doubleValue();
88  }
89  o = dao.getSingleResult(
90  "select sum(re.debit) from RegisterEntry as re " +
91  "where (re.account.id like '" + AccountFormat.expand("430." + contract.getId()) + "' " +
92  "or re.account.id like '" + AccountFormat.expand("440." + contract.getId()) + "')");
93  if(o instanceof Number) {
94  invoice = ((Number) o).doubleValue();
95  }
96  }
97  }
98 
99 }
ContractDefinition getContractDefinition()
Definition: Contract.java:125
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380
static boolean near(double value, int digits)
Definition: Zero.java:30