BrightSide Workbench Full Report + Source Code
bsfinancials-www/src/main/java/org/turro/financials/assets/FixedAssetWrapper.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.financials.assets;
19 
20 import java.util.*;
21 import org.turro.command.Command;
22 import org.turro.command.Context;
23 import org.turro.elephant.db.WhereClause;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.document.DocumentLinesListbox;
26 import org.turro.financials.entity.DocumentLine;
27 import org.turro.financials.entity.FixedAsset;
28 import org.turro.financials.entity.RegisterEntry;
29 import org.turro.financials.entity.RegisterView;
30 import org.turro.financials.menu.FinancialsMenu;
31 import static org.turro.financials.model.asset.FixedAssetWrapper.getAssets;
32 import org.turro.financials.model.document.DocumentWrapper;
33 import org.turro.financials.model.register.RegisterGenerator;
34 import org.turro.financials.model.register.ViewWrapper;
35 import org.turro.i18n.I_;
36 import org.turro.jpa.Dao;
37 import org.turro.math.Zero;
38 import org.turro.plugin.filter.IFilterValue;
39 import org.turro.zkoss.dialog.InputDialog;
40 import org.turro.zkoss.dialog.SelectionDialog;
41 import org.turro.zkoss.filter.FilterGrid;
42 import org.turro.zul.frame.Framework;
43 import org.zkoss.zk.ui.Component;
44 import org.zkoss.zk.ui.Page;
45 import org.zkoss.zul.Checkbox;
46 import org.zkoss.zul.Row;
47 
52 public class FixedAssetWrapper extends org.turro.financials.model.asset.FixedAssetWrapper {
53 
55  super(asset);
56  }
57 
59  if(asset.isEmpty()) return null;
60  for(Component row : lines.getRows().getChildren()) {
61  if(((Row)row).getValue() instanceof DocumentLine) {
62  Checkbox cb = (Checkbox) row.getAttribute("cb");
63  if(cb != null && cb.isChecked()) {
64  DocumentLine dl = (DocumentLine) ((Row)row).getValue();
65  Iterator<DocumentLine> it = asset.getDocumentLines().iterator();
66  while(it.hasNext()) {
67  if(it.next().getId() == dl.getId()) {
68  it.remove();
70  new DocumentWrapper(dl.getDocument()).save(null, null);
71  break;
72  }
73  }
74  }
75  }
76  }
77  return super.save();
78  }
79 
80  public void loadLines(FilterGrid filterGrid, final Command command) {
81  final DocumentLinesListbox docLines = new DocumentLinesListbox(getPossibleLines(filterGrid));
82  docLines.setMultiple(true);
83  docLines.setCheckmark(true);
84  docLines.setShowContract(true);
85  docLines.setSelectFirst(false);
86 
88  Framework.getCurrent().getPage(),
89  I_.get("Documents"),
90  docLines, "80%", "80%", new Command() {
91  @Override
92  public Object execute(Context context) {
93  if(docLines != null) {
94  for(DocumentLine dl : docLines.getObjectValues()) {
95  asset.getDocumentLines().add(dl);
96  }
97  if(command != null) command.execute(context);
98  }
99  return null;
100  }
101  });
102  }
103 
104  private Collection<DocumentLine> getPossibleLines(FilterGrid filterGrid) {
105  Dao dao = new FinancialsPU();
106  List<DocumentLine> relDocs = new ArrayList<DocumentLine>();
107  // Already exists the document
108  WhereClause wc = createCriteria(filterGrid.getValues());
109  relDocs.addAll(dao.getResultList(wc));
110  return relDocs;
111  }
112 
113  private WhereClause createCriteria(List<IFilterValue> values) {
114  WhereClause wc = new WhereClause();
115  wc.addClause("select dl from DocumentLine as dl");
116  wc.addClause("where dl.lineType.id = :lineType");
117  wc.addNamedValue("lineType", 4L);
118  for(IFilterValue v : values) {
119  v.addConstraint(wc);
120  }
121  wc.addClause("order by dl.document.documentDate");
122  return wc;
123  }
124 
125  public static void doDepreciations(Page page) {
126  InputDialog.getInput(page, I_.get("Depreciations"), "Date",
127  new Date(), null, 0, new Command() {
128  @Override
129  public Object execute(Context context) {
130  Date date = (Date) context.get("value");
131  if(date != null) {
132  for(RegisterView v : ViewWrapper.getViews()) {
134  for(FixedAsset asset : getAssets()) {
135  if(asset.getView().getId() == v.getId()) {
136  double toDepreciate = new FixedAssetWrapper(asset).getToDepreciate(date);
137  if(!Zero.near(toDepreciate, 2)) {
138  Set<RegisterEntry> sre = rg.addAmount(v, null, asset.getStore(), asset.getDepreciationLineType(),
139  asset.getId() + "-" + asset.getName(), toDepreciate);
140  for(RegisterEntry re : sre) {
141  re.setPath(FinancialsPU.getObjectPath(asset));
142  }
143  }
144  }
145  }
146  if(rg.getRegister() != null && !(rg.getRegister().getRegisterEntries().isEmpty())) {
147  rg.getRegister().setView(v);
148  rg.getRegister().setRegisterDate(date);
150  }
151  }
152  }
153  return null;
154  }
155  });
156  }
157 
158 }
static String getObjectPath(Object object)
void setContractPreference(ContractPreference contractPreference)
ContractPreference getDefaultContractPreference()
Definition: Document.java:429
void setView(RegisterView view)
Definition: Register.java:163
Set< RegisterEntry > getRegisterEntries()
Definition: Register.java:135
void setRegisterDate(Date registerDate)
Definition: Register.java:127
Set< RegisterEntry > addAmount(RegisterView view, Contract contract, Contract store, LineType lineType, String concept, double amount)
static List< RegisterView > getViews()
static String get(String msg)
Definition: I_.java:41
static boolean near(double value, int digits)
Definition: Zero.java:30
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
List< IFilterValue > getValues()
Rows getRows(boolean create)
void setSelectFirst(boolean selectFirst)
static Framework getCurrent()
Definition: Framework.java:203