19 package org.turro.financials.model.register;
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;
37 private double balance;
41 this.entrySet = entrySet;
42 this.document = document;
43 this.digits = document.
getCurrency().getDefaultFractionDigits();
48 this.
register =
register;
49 this.document =
register.getDocument();
50 this.digits = document.
getCurrency().getDefaultFractionDigits();
59 private void checkTotal() {
61 if(registerEntry !=
null && canRepairTotal(registerEntry)) {
62 setTotal(registerEntry, amounts.
getTotal());
66 private void checkBalance() {
68 if(entrySet !=
null) {
69 for(EntryWrapper ew : entrySet) {
70 balance += ew.getEntry().getDebit() - ew.getEntry().getCredit();
73 for(RegisterEntry re :
register.getRegisterEntries()) {
74 balance += (re.getDebit() - re.getCredit());
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);
84 private boolean canRepairTotal(RegisterEntry registerEntry) {
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);
91 private double debit() {
93 if(entrySet !=
null) {
94 for(EntryWrapper ew : entrySet) {
95 RegisterEntry re = ew.getEntry();
96 debit += re.getDebit();
99 for(RegisterEntry re :
register.getRegisterEntries()) {
100 debit += re.getDebit();
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();
114 for(RegisterEntry re :
register.getRegisterEntries()) {
115 credit += re.getCredit();
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) {
129 amount = Math.abs(re.getCredit() + re.getDebit());
133 for(RegisterEntry re :
register.getRegisterEntries()) {
134 if(re.getAccount().getId().matches(accountExpr) && (Math.abs(re.getCredit() + re.getDebit())) > amount) {
136 amount = Math.abs(re.getCredit() + re.getDebit());
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);
152 private boolean increaseAmount(RegisterEntry entry,
double amount) {
153 boolean done =
false;
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);
159 }
else if(
new Round(Math.abs(credit)).decimals(digits).value() > 0.00d) {
160 entry.setCredit(entry.getCredit() + amount);
DocumentAmounts getAmounts()
RegisterAssuredBalance(Register register)
RegisterAssuredBalance(EntrySet entrySet, Document document)