BrightSide Workbench Full Report + Source Code
bsfinancials-core/src/main/java/org/turro/financials/model/asset/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.model.asset;
19 
20 import java.util.Collection;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.Set;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.*;
26 import org.turro.financials.model.document.DocumentWrapper;
27 import org.turro.jpa.Dao;
28 import org.turro.jpa.entity.XMLSerializer;
29 import org.turro.log.SystemLogType;
30 import org.turro.log.SystemLogger;
31 
36 public class FixedAssetWrapper {
37 
38  protected FixedAsset asset;
39 
41  this.asset = asset;
42  }
43 
44  public FixedAsset save() {
45  Dao dao = new FinancialsPU();
46  Iterator<FixedAssetDepreciation> it = asset.getDepreciations().iterator();
47  while(it.hasNext()) {
48  if(!(it.next().isValid())) {
49  it.remove();
50  }
51  }
52  for(DocumentLine dl : asset.getDocumentLines()) {
53  dl.setLineType(getBuyLineType());
54  dl.setContractPreference(asset.getInvestmentPreference());
55  new DocumentWrapper(dl.getDocument()).save(null, null);
56  }
58  asset = dao.saveObject(asset);
60  return asset;
61  }
62 
63  public boolean delete() {
64  // Check if possible and notify
65  if(canDelete()) {
68  }
69  return true;
70  }
71 
72  public boolean canDelete() {
73  if(asset.getId() == 0) return false;
74  if(!(asset.getDocumentLines().isEmpty())) {
75  return false;
76  }
77  if(!(getEntries().isEmpty())) {
78  return false;
79  }
80  return true;
81  }
82 
83  public void keepRelations(Set<DocumentLine> docLines) {
84  asset.setDocumentLines(docLines);
85  }
86 
87  public double getInvestment() {
88  double investment = asset.getInvestment();
89  for(DocumentLine dl : asset.getDocumentLines()) {
90  investment += dl.getTaxable();
91  }
92  return investment;
93  }
94 
95  public double getDepreciated() {
96  double depreciated = asset.getDepreciated();
97  for(RegisterEntry re : getEntries()) {
98  depreciated += re.getDebit();
99  }
100  return depreciated;
101  }
102 
103  public double getToDepreciate(Date date) {
104  DepreciationMap dm = new DepreciationMap();
106  if(fad.isValid()) {
108  de.setPercent(fad.getPerCent());
109  dm.put(fad.getDepreciationDate(), de);
110  }
111  }
112  double toDepreciate = 0.0;
113  if(asset.getInvestment() > 0.00) {
114  toDepreciate = dm.getToDepreciate(asset.getInvestment(), null, date);
115  }
116  for(DocumentLine dl : asset.getDocumentLines()) {
117  toDepreciate += dm.getToDepreciate(dl.getTaxable(), dl.getDocument().getReceiptDate(), date);
118  }
119  double depreciated = asset.getDepreciated();
120  for(RegisterEntry re : getEntries()) {
121  Date regDate = re.getRegister().getRegisterDate();
122  if(regDate.before(date) || regDate.equals(date)) {
123  depreciated += re.getDebit();
124  }
125  }
126  return toDepreciate - depreciated;
127  }
128 
129  public Collection<RegisterEntry> getEntries() {
130  Dao dao = new FinancialsPU();
131  return dao.getResultList(
132  "select re from RegisterEntry as re " +
133  "where re.path = '" + FinancialsPU.getObjectPath(asset) + "' " +
134  "order by re.account, re.register.registerDate");
135  }
136 
137  public static LineType getBuyLineType() {
138  Dao dao = new FinancialsPU();
139  return dao.find(LineType.class, 4L);
140  }
141 
143  Dao dao = new FinancialsPU();
144  return dao.find(DocumentDefinition.class, 25L);
145  }
146 
147  public static Collection<FixedAsset> getAssets() {
148  Dao dao = new FinancialsPU();
149  return dao.getResultList("select fa from FixedAsset as fa where unsubscribed = FALSE order by fa.name");
150  }
151 
152 }
static String getObjectPath(Object object)
void setDocumentLines(Set< DocumentLine > documentLines)
Set< FixedAssetDepreciation > getDepreciations()
Definition: FixedAsset.java:88
Set< DocumentLine > getDocumentLines()
Definition: FixedAsset.java:96
ContractPreference getInvestmentPreference()
double getToDepreciate(double investment, Date initialDate, Date finalDate)
void deleteObject(Object obj)
Definition: Dao.java:162
static ISystemLogger getInstance()
void doLog(SystemLogType type, Object entity, String comment, Serializable data)