BrightSide Workbench Full Report + Source Code
DocumentGenerator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.model.document;
20 
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.Set;
25 import org.turro.financials.db.FinancialsPU;
26 import org.turro.financials.entity.*;
27 import org.turro.financials.model.register.RegisterWrapper;
28 import org.turro.financials.model.register.ViewWrapper;
29 import org.turro.jpa.entity.XMLSerializer;
30 import org.turro.log.SystemLogType;
31 import org.turro.log.SystemLogger;
32 import org.turro.usual.UsualUsages;
33 
38 public class DocumentGenerator {
39 
40  private DocumentDefinition documentDefinition;
41  private Contract contract;
42  private String notes;
43  private Date receiptDate;
44  private Date documentDate;
45  private String documentNumber;
46  private boolean draft, noAncestors = false, noDescendants = false;
47 
48  private Document document;
49 
50  public DocumentGenerator(DocumentDefinition documentDefinition, Contract contract, String notes, Date receiptDate, Date documentDate, String documentNumber, boolean draft) {
51  this.documentDefinition = documentDefinition;
52  this.contract = contract;
53  this.notes = notes;
54  this.receiptDate = receiptDate;
55  this.documentDate = documentDate;
56  this.documentNumber = documentNumber;
57  this.draft = draft;
58  createDocument();
59  }
60 
61  public DocumentGenerator(Document document) {
62  this.document = document;
63  }
64 
65  public Document getDocument() {
66  return document;
67  }
68 
69  public DocumentLine addLine(Contract store, String concept,
70  double quantity, double price, double tax, double retention,
71  LineType lineType, ContractPreference contractPreference) {
72  DocumentLine dl = new DocumentLine();
73  dl.setDocument(document);
74  dl.setLineOrder(document.getMaxLineOrder() + 1);
75  dl.setStore(store);
76  dl.setConcept(concept);
77  dl.setQuantity(quantity);
78  dl.setPrice(price);
79  dl.setTax(tax);
80  dl.setRetention(retention);
81  dl.setLineType(lineType);
82  dl.setContractPreference(contractPreference);
83  dl = new FinancialsPU().saveObject(dl);
84  document.getDocumentLines().add(dl);
85  return dl;
86  }
87 
88  public DocumentLine addLine(Contract store, Product product, String concept,
89  double quantity, double price, double tax, double retention,
90  LineType lineType, ContractPreference contractPreference) {
91  DocumentLine dl = new DocumentLine();
92  dl.setDocument(document);
93  dl.setLineOrder(document.getMaxLineOrder() + 1);
94  dl.setStore(store);
95  dl.setProduct(product);
96  dl.setConcept(concept);
97  dl.setQuantity(quantity);
98  dl.setPrice(price);
99  dl.setTax(tax);
100  dl.setRetention(retention);
101  dl.setLineType(lineType);
102  dl.setContractPreference(contractPreference);
103  dl = new FinancialsPU().saveObject(dl);
104  document.getDocumentLines().add(dl);
105  return dl;
106  }
107 
108  public DocumentLine addLine(Contract store, ProductByContractor product, String concept,
109  double quantity, double price, double tax, double retention,
110  LineType lineType, ContractPreference contractPreference) {
111  DocumentLine dl = new DocumentLine();
112  dl.setDocument(document);
113  dl.setLineOrder(document.getMaxLineOrder() + 1);
114  dl.setStore(store);
115  dl.setProductByContractor(product);
116  dl.setProduct(product.getProduct());
117  dl.setConcept(concept);
118  dl.setQuantity(quantity);
119  dl.setPrice(price);
120  dl.setTax(tax);
121  dl.setRetention(retention);
122  dl.setLineType(lineType);
123  dl.setContractPreference(contractPreference);
124  dl = new FinancialsPU().saveObject(dl);
125  document.getDocumentLines().add(dl);
126  return dl;
127  }
128 
129  public Document save() {
130  document.clearEmpties();
131  for(DocumentLine l : document.getDocumentLines()) {
132  if(!l.isEmpty() && l.getStore() != null) {
133  UsualUsages.addUsage(document.getUsualPath() + "st:" + l.getStore().getId());
134  UsualUsages.addUsage(document.getUsualPath() + "cp:" + l.getConcept());
135  }
136  }
137  if(document.isDraft()) {
138  document.getRegisters().clear();
139  } else {
140  getRegisters();
141  Iterator<Register> ir = document.getRegisters().iterator();
142  while(ir.hasNext()) {
143  Register r = ir.next();
144  r.removeEmpties();
145  r.setRegisterDate(document.getReceiptDate());
146  if(r.isEmpty()) {
147  r.setDocument(null);
148  ir.remove();
149  } else if(document.getForcedView() != null) {
150  if(r.getView().getId() != document.getForcedView().getId()) {
151  r.setView(document.getForcedView());
152  r.setIdRegister(0);
153  }
154  }
155  }
156  }
157 
158  document.prepareSave();
159  document = new FinancialsPU().saveObject(document);
160  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, document, "saved", new XMLSerializer(document).data());
161 
162  return document;
163  }
164 
165  public Set<RegisterWrapper> getRegisters() {
166  Set<RegisterWrapper> s = new HashSet<RegisterWrapper>();
167  for(RegisterView rv : ViewWrapper.getViews()) {
168  boolean done = false;
169  for(Register r : document.getRegisters()) {
170  if(r.getView().getId() == rv.getId()) {
171  r.setRegisterDate(document.getReceiptDate());
172  s.add(new RegisterWrapper(r));
173  done = true;
174  break;
175  }
176  }
177  if(!done) {
178  Register r = new Register();
179  r.setView(rv);
180  r.setRegisterDate(document.getReceiptDate());
181  r.setDocument(document);
182  document.getRegisters().add(r);
183  s.add(new RegisterWrapper(r));
184  }
185  }
186  return s;
187  }
188 
189  private void createDocument() {
190  document = new Document();
191  document.setDocumentDefinition(documentDefinition);
192  document.setContract(contract);
193  document.setNotes(notes);
194  document.setReceiptDate(receiptDate == null ? documentDate : receiptDate);
195  document.setDocumentDate(documentDate);
196  document.setDocumentNumber(documentNumber);
197  document.setDraft(draft);
198  document.setCurrency(contract.getCurrency());
199  document = new FinancialsPU().saveObject(document);
200  }
201 
202 }
void setContractPreference(ContractPreference contractPreference)
void setProductByContractor(ProductByContractor productByContractor)
void setDocumentNumber(String documentNumber)
Definition: Document.java:192
void setDocumentDate(Date documentDate)
Definition: Document.java:163
void setContract(Contract contract)
Definition: Document.java:123
void setCurrency(Currency currency)
Definition: Document.java:135
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
void setDocumentDefinition(DocumentDefinition documentDefinition)
Definition: Document.java:175
void setReceiptDate(Date receiptDate)
Definition: Document.java:256
void setView(RegisterView view)
Definition: Register.java:163
void setIdRegister(long idRegister)
Definition: Register.java:119
void setDocument(Document document)
Definition: Register.java:95
void setRegisterDate(Date registerDate)
Definition: Register.java:127
DocumentLine addLine(Contract store, String concept, double quantity, double price, double tax, double retention, LineType lineType, ContractPreference contractPreference)
DocumentGenerator(DocumentDefinition documentDefinition, Contract contract, String notes, Date receiptDate, Date documentDate, String documentNumber, boolean draft)
DocumentLine addLine(Contract store, ProductByContractor product, String concept, double quantity, double price, double tax, double retention, LineType lineType, ContractPreference contractPreference)
DocumentLine addLine(Contract store, Product product, String concept, double quantity, double price, double tax, double retention, LineType lineType, ContractPreference contractPreference)
static List< RegisterView > getViews()
static ISystemLogger getInstance()
static Usually addUsage(String path)
void doLog(SystemLogType type, Object entity, String comment, Serializable data)