BrightSide Workbench Full Report + Source Code
RegisterAssuredBalance.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.model.register;
20 
21 import org.turro.financials.entity.Document;
22 import org.turro.financials.entity.Register;
23 import org.turro.financials.entity.RegisterEntry;
24 import org.turro.financials.model.document.DocumentAmounts;
25 import org.turro.math.Round;
26 
31 public class RegisterAssuredBalance {
32 
33  private EntrySet entrySet;
34  private Document document;
35  private Register register;
36  private DocumentAmounts amounts;
37  private double balance;
38  private int digits;
39 
40  public RegisterAssuredBalance(EntrySet entrySet, Document document) {
41  this.entrySet = entrySet;
42  this.document = document;
43  this.digits = document.getCurrency().getDefaultFractionDigits();
44  amounts = document.getAmounts();
45  }
46 
47  public RegisterAssuredBalance(Register register) {
48  this.register = register;
49  this.document = register.getDocument();
50  this.digits = document.getCurrency().getDefaultFractionDigits();
51  amounts = document.getAmounts();
52  }
53 
54  public void normalize() {
55  checkTotal();
56  checkBalance();
57  }
58 
59  private void checkTotal() {
60  RegisterEntry registerEntry = getMaxEntry("^4[0134][01].*");
61  if(registerEntry != null && canRepairTotal(registerEntry)) {
62  setTotal(registerEntry, amounts.getTotal());
63  }
64  }
65 
66  private void checkBalance() {
67  balance = 0.0d;
68  if(entrySet != null) {
69  for(EntryWrapper ew : entrySet) {
70  balance += ew.getEntry().getDebit() - ew.getEntry().getCredit();
71  }
72  } else {
73  for(RegisterEntry re : register.getRegisterEntries()) {
74  balance += (re.getDebit() - re.getCredit());
75  }
76  }
77  if(new Round(Math.abs(balance)).decimals(digits).value() == 0.01d) {
78  if(!increaseAmount(getMaxEntry("^[76].*"), balance)) {
79  increaseAmount(getMaxEntry("^4[0134]09.*"), balance);
80  }
81  }
82  }
83 
84  private boolean canRepairTotal(RegisterEntry registerEntry) {
85  double total = amounts.getTotal(),
86  debit = registerEntry.getDebit(), credit = registerEntry.getCredit();
87  return (new Round(Math.abs(total - debit)).decimals(digits).value() == 0.01d ||
88  new Round(Math.abs(total - credit)).decimals(digits).value() == 0.01d);
89  }
90 
91  private double debit() {
92  double debit = 0.0d;
93  if(entrySet != null) {
94  for(EntryWrapper ew : entrySet) {
95  RegisterEntry re = ew.getEntry();
96  debit += re.getDebit();
97  }
98  } else {
99  for(RegisterEntry re : register.getRegisterEntries()) {
100  debit += re.getDebit();
101  }
102  }
103  return debit;
104  }
105 
106  private double credit() {
107  double credit = 0.0d;
108  if(entrySet != null) {
109  for(EntryWrapper ew : entrySet) {
110  RegisterEntry re = ew.getEntry();
111  credit += re.getCredit();
112  }
113  } else {
114  for(RegisterEntry re : register.getRegisterEntries()) {
115  credit += re.getCredit();
116  }
117  }
118  return credit;
119  }
120 
121  private RegisterEntry getMaxEntry(String accountExpr) {
122  double amount = 0.0d;
123  RegisterEntry result = null;
124  if(entrySet != null) {
125  for(EntryWrapper ew : entrySet) {
126  RegisterEntry re = ew.getEntry();
127  if(re.getAccount().getId().matches(accountExpr) && (Math.abs(re.getCredit() + re.getDebit())) > amount) {
128  result = re;
129  amount = Math.abs(re.getCredit() + re.getDebit());
130  }
131  }
132  } else {
133  for(RegisterEntry re : register.getRegisterEntries()) {
134  if(re.getAccount().getId().matches(accountExpr) && (Math.abs(re.getCredit() + re.getDebit())) > amount) {
135  result = re;
136  amount = Math.abs(re.getCredit() + re.getDebit());
137  }
138  }
139  }
140  return result;
141  }
142 
143  private void setTotal(RegisterEntry entry, double total) {
144  double debit = entry.getDebit(), credit = entry.getCredit();
145  if(new Round(Math.abs(total - debit)).decimals(digits).value() == 0.01d) {
146  entry.setDebit(total);
147  } else if(new Round(Math.abs(total - credit)).decimals(digits).value() == 0.01d) {
148  entry.setCredit(total);
149  }
150  }
151 
152  private boolean increaseAmount(RegisterEntry entry, double amount) {
153  boolean done = false;
154  if(entry != null) {
155  double debit = entry.getDebit(), credit = entry.getCredit();
156  if(new Round(Math.abs(debit)).decimals(digits).value() > 0.00d) {
157  entry.setDebit(entry.getDebit() - amount);
158  done = true;
159  } else if(new Round(Math.abs(credit)).decimals(digits).value() > 0.00d) {
160  entry.setCredit(entry.getCredit() + amount);
161  done = true;
162  }
163  }
164  return done;
165  }
166 
167 }