18 package org.turro.financials.model.document;
20 import java.util.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.TreeMap;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.Contract;
26 import org.turro.financials.entity.Document;
27 import org.turro.financials.entity.DocumentLine;
28 import org.turro.financials.entity.DocumentRelation;
29 import org.turro.financials.entity.DocumentWorkflow;
30 import org.turro.financials.model.business.CompanyWrapper;
31 import org.turro.jpa.Dao;
32 import org.turro.util.CompareUtil;
52 private void fillSet() {
55 List<Object[]> l = dao.getResultList(
56 "select dr, dw from DocumentRelation dr, DocumentWorkflow dw " +
57 "where dr.ancestor.documentDefinition.id = dw.ancestor.id " +
58 "and dr.descendant.documentDefinition.id = dw.descendant.id " +
59 "and dr.ancestor.contract.contractDefinition.id = dw.contractDefinition.id " +
60 "and dr.ancestor.documentDate >= ?",
61 new Object[] { closingDate }
68 private void fillSet(Contract contract) {
69 Date closingDate = CompanyWrapper.getCompanyClosingDate();
70 dao =
new FinancialsPU();
71 List<Object[]> l = dao.getResultList(
72 "select dr, dw from DocumentRelation dr, DocumentWorkflow dw " +
73 "where dr.ancestor.documentDefinition.id = dw.ancestor.id " +
74 "and dr.descendant.documentDefinition.id = dw.descendant.id " +
75 "and dr.ancestor.contract.contractDefinition.id = dw.contractDefinition.id " +
76 "and dr.ancestor.contract.id = ? " +
77 "and dr.ancestor.documentDate >= ?",
78 new Object[] { contract.getId(), closingDate }
81 checkRelation((DocumentRelation) o[0], (DocumentWorkflow) o[1]);
85 private void checkRelation(DocumentRelation dr, DocumentWorkflow dw) {
86 double descendantAmount = 0.0;
87 if(CompareUtil.compare(dr.getAncestor().isDraft(), dr.getDescendant().isDraft()) != 0) {
90 for(DocumentLine dl : dr.getDescendant().getDocumentLines()) {
91 if(dl.getLineType() !=
null && dl.getLineType().getId() == dw.getLineType().getId()) {
92 descendantAmount += dl.getAmount();
95 if(CompareUtil.compare(dr.getAncestor().getTotalAmount(), descendantAmount) != 0) {
96 PortfolioDisagreement pd =
get(dr.getAncestor().getId());
98 if(pd.fullfillsWith(descendantAmount)) {
99 remove(dr.getAncestor().getId());
101 pd.addAmount(descendantAmount);
104 put(dr.getAncestor().getId(),
new PortfolioDisagreement(dr.getAncestor(), descendantAmount));
109 private void checkBackwards() {
110 Iterator<PortfolioDisagreement> it = values().iterator();
111 while(it.hasNext()) {
112 if(checBackwardRelation(it.next())) {
118 private boolean checBackwardRelation(PortfolioDisagreement pd) {
119 double siblingsAmount = 0.0;
120 for(DocumentRelation dr : pd.getDocument().getDescendants()) {
122 for(DocumentRelation drdoc : dr.getDescendant().getAncestors()) {
123 Document doc = drdoc.getAncestor();
124 siblingsAmount += doc.getTotalAmount();
127 return pd.fullfillsBackwardWith(siblingsAmount);
static Date getCompanyClosingDate()
PortfolioDisagreementSet(Contract contract)
PortfolioDisagreementSet()