BrightSide Workbench Full Report + Source Code
FinancialsSummary.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.action.IUserSummary;
22 import org.turro.action.UserSummaries;
23 import org.turro.annotation.UserSummary;
24 import org.turro.auth.Authentication;
25 import org.turro.elephant.context.IConstructor;
26 import org.turro.financials.cart.FollowUps;
27 import org.turro.financials.content.ContractContentIterator;
28 import org.turro.financials.contract.www.DocumentModel;
29 import org.turro.financials.entity.Contract;
30 import org.turro.financials.handshake.HandshakeList;
31 import org.turro.marker.ElephantMarker;
32 import org.turro.plugin.contacts.IContact;
33 import org.turro.util.CompareUtil;
34 
39 @UserSummary
40 public class FinancialsSummary implements IUserSummary, Comparable<IUserSummary> {
41 
42  @Override
43  public String render(IConstructor constructor) {
44  ElephantMarker marker = new ElephantMarker(constructor);
46 
47  if(UserSummaries.isNotZero("handshakeCount") ||
48  UserSummaries.isNotZero("contractCount") ||
49  UserSummaries.isNotZero("balanceAmount") ||
50  UserSummaries.isNotZero("notPreparedCount") ||
51  UserSummaries.isNotZero("notDeliveredCount")) {
52  return marker.parse("contract", "userSummary");
53  }
54 
55  return "";
56  }
57 
58  @Override
59  public void init(IConstructor constructor) {
60  Long handshakeCount = (Long) UserSummaries.getAttribute("handshakeCount");
61 
62  if(handshakeCount == null) {
63 
65 
66  UserSummaries.setAttribute("handshakeCount", Long.valueOf(HandshakeList.count()));
67 
68  if(!contact.isOutsider()) {
69  ContractContentIterator ci = new ContractContentIterator(constructor, null, contact, false, null);
70  DocumentModel dm = new DocumentModel();
71  UserSummaries.setAttribute("contractCount", Long.valueOf(ci.count()));
72  UserSummaries.setAttribute("documentCount", dm.getDocumentsCount(ci.items()));
73  UserSummaries.setAttribute("balanceAmount", Double.valueOf(contractsBalance(ci)));
74  UserSummaries.setAttribute("notPreparedCount", Long.valueOf(FollowUps.countNotPrepared(contact)));
75  UserSummaries.setAttribute("notDeliveredCount", Long.valueOf(FollowUps.countNotDelivered(contact)));
76  }
77 
78  }
79  }
80 
81  private double contractsBalance(ContractContentIterator ci) {
82  double amount = 0;
83  for(Contract c : ci.items()) {
84  amount += new ContractPortfolio(c).getBalance();
85  }
86  return amount;
87  }
88 
89  @Override
90  public boolean isRequired() {
91  return false;
92  }
93 
94  @Override
95  public int getOrder() {
96  return 1;
97  }
98 
99  @Override
100  public int compareTo(IUserSummary o) {
101  int result = CompareUtil.compare(getOrder(), o.getOrder());
102  if(result == 0) {
103  result = CompareUtil.compare(getClass().getName(), o.getClass().getName());
104  }
105  return result;
106  }
107 
108 }
static boolean isNotZero(String attribute)
static void setAttribute(String attribute, Object value)
static Map< String, Object > getAttributes()
static Object getAttribute(String attribute)
static long countNotDelivered(IContact contact)
Definition: FollowUps.java:71
static long countNotPrepared(IContact contact)
Definition: FollowUps.java:59
String parse(String rootTmpl, String tmpl)