BrightSide Workbench Full Report + Source Code
PayrollWrapper.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.document;
19 
20 import java.util.ArrayList;
21 import org.turro.financials.db.FinancialsPU;
22 import org.turro.financials.entity.Document;
23 import org.turro.financials.entity.DocumentLine;
24 import org.turro.financials.entity.LineType;
25 import org.turro.jpa.Dao;
26 
31 public class PayrollWrapper {
32 
33  private static final long
34  PAYROLL_TYPE = 8, RETENTION_TYPE = 10,
35  INSURANCE_TYPE = 9, SEIZURE_TYPE = 159,
36  PROTECTION_TYPE = 167, CONTINGENCIES_TYPE = 168;
37 
38  private Document payroll;
39  private DocumentLine payrollLine, retentionLine, insuranceLine, seizureLine,
40  protectionLine, contingenciesLine;
41 
42  public PayrollWrapper(Document payroll) {
43  this.payroll = payroll;
44  assignLines();
45  }
46 
47  public void save() {
48  //TODO: if id is assigned then remove document
49  if(!payroll.isEmpty()) {
50  new DocumentWrapper(payroll).save(
51  new ArrayList<>(payroll.getDocumentLines()),
52  new ArrayList<>(payroll.getDescendants()));
53  }
54  }
55 
57  return insuranceLine;
58  }
59 
60  public Document getPayroll() {
61  return payroll;
62  }
63 
65  return payrollLine;
66  }
67 
69  return retentionLine;
70  }
71 
73  return seizureLine;
74  }
75 
77  return protectionLine;
78  }
79 
81  return contingenciesLine;
82  }
83 
84  private void assignLines() {
85  if(payroll != null) {
86  Dao dao = new FinancialsPU();
87  payrollLine = retentionLine = insuranceLine = seizureLine = protectionLine = null;
88  for(DocumentLine dl : payroll.getDocumentLines()) {
89  if(dl.getLineType().getId() == PAYROLL_TYPE) {
90  payrollLine = dl;
91  } else if(dl.getLineType().getId() == RETENTION_TYPE) {
92  retentionLine = dl;
93  } else if(dl.getLineType().getId() == INSURANCE_TYPE) {
94  insuranceLine = dl;
95  } else if(dl.getLineType().getId() == SEIZURE_TYPE) {
96  seizureLine = dl;
97  } else if(dl.getLineType().getId() == PROTECTION_TYPE) {
98  protectionLine = dl;
99  } else if(dl.getLineType().getId() == CONTINGENCIES_TYPE) {
100  contingenciesLine = dl;
101  }
102  }
103  if(payrollLine == null) {
104  payrollLine = new DocumentLine();
105  payrollLine.setDocument(payroll);
106  payrollLine.setLineType(dao.find(LineType.class, PAYROLL_TYPE));
107  // avoid equalities
108  payrollLine.setLineOrder(payroll.getDocumentLines().size() + 90001);
109  payroll.getDocumentLines().add(payrollLine);
110  }
111  if(retentionLine == null) {
112  retentionLine = new DocumentLine();
113  retentionLine.setDocument(payroll);
114  retentionLine.setLineType(dao.find(LineType.class, RETENTION_TYPE));
115  // avoid equalities
116  retentionLine.setLineOrder(payroll.getDocumentLines().size() + 90001);
117  payroll.getDocumentLines().add(retentionLine);
118  }
119  if(insuranceLine == null) {
120  insuranceLine = new DocumentLine();
121  insuranceLine.setDocument(payroll);
122  insuranceLine.setLineType(dao.find(LineType.class, INSURANCE_TYPE));
123  // avoid equalities
124  insuranceLine.setLineOrder(payroll.getDocumentLines().size() + 90001);
125  payroll.getDocumentLines().add(insuranceLine);
126  }
127  if(seizureLine == null) {
128  seizureLine = new DocumentLine();
129  seizureLine.setDocument(payroll);
130  seizureLine.setLineType(dao.find(LineType.class, SEIZURE_TYPE));
131  // avoid equalities
132  seizureLine.setLineOrder(payroll.getDocumentLines().size() + 90001);
133  payroll.getDocumentLines().add(seizureLine);
134  }
135  if(protectionLine == null) {
136  protectionLine = new DocumentLine();
137  protectionLine.setDocument(payroll);
138  protectionLine.setLineType(dao.find(LineType.class, PROTECTION_TYPE));
139  // avoid equalities
140  protectionLine.setLineOrder(payroll.getDocumentLines().size() + 90001);
141  payroll.getDocumentLines().add(protectionLine);
142  }
143  if(contingenciesLine == null) {
144  contingenciesLine = new DocumentLine();
145  contingenciesLine.setDocument(payroll);
146  contingenciesLine.setLineType(dao.find(LineType.class, CONTINGENCIES_TYPE));
147  // avoid equalities
148  contingenciesLine.setLineOrder(payroll.getDocumentLines().size() + 90001);
149  payroll.getDocumentLines().add(contingenciesLine);
150  }
151  }
152  }
153 
154 }
Set< DocumentRelation > getDescendants()
Definition: Document.java:139
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180