BrightSide Workbench Full Report + Source Code
bsfinancials-www/src/main/java/org/turro/financials/document/logic/BatchOfWrapper.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.document.logic;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
24 import org.turro.command.Command;
25 import org.turro.command.Context;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.elephant.util.Messages;
28 import org.turro.financials.db.FinancialsPU;
29 import org.turro.financials.document.DocumentListbox;
30 import org.turro.financials.entity.*;
31 import org.turro.financials.model.document.BatchOfType;
32 import org.turro.financials.portfolio.PortfolioSet;
33 import org.turro.financials.portfolio.RelationMode;
34 import org.turro.i18n.I_;
35 import org.turro.jpa.Dao;
36 import org.turro.plugin.dossier.IIssue;
37 import org.turro.plugin.filter.IFilterValue;
38 import org.turro.zkoss.dialog.SelectionDialog;
39 import org.turro.zkoss.filter.FilterGrid;
40 import org.turro.zul.frame.Framework;
41 
46 public class BatchOfWrapper extends org.turro.financials.model.document.BatchOfWrapper {
47 
48  public Date relationDate = new java.util.Date(new java.util.Date().getTime() - (6L * 30L * 24L * 60L * 60L * 1000L));
49  public Date relationToDate = new Date();
50 
52  super(batchOf);
53  }
54 
55  public Date getRelationDate() {
56  return relationDate;
57  }
58 
59  public Date getRelationToDate() {
60  return relationToDate;
61  }
62 
63  public String createBatchString() {
64  String s = "";
65  if(batchOf.getType() != null) {
67  }
68  return s + I_.get("Create from") + "...";
69  }
70 
71  public String createRelationString() {
72  String s = "";
73  if(batchOf.getType() != null) {
75  }
76  return s + I_.get("Create from") + "...";
77  }
78 
79  public void setContract(Contract contract) {
80  batchOf.getBatch().setContract(contract);
81  for(Document doc : batchOf.getDocuments()) {
82  for(DocumentLine l : doc.getDocumentLines()) {
83  l.setStore(contract);
84  }
85  }
86  }
87 
88  public void setStore(Contract contract) {
89  batchOf.setStore(contract);
91  l.setStore(contract);
92  }
93  }
94 
95  public void loadBatch(BatchOfType type, final Command command) {
96  final DocumentListbox dl = new DocumentListbox(getPossibleBatch(type));
97  dl.setMultiple(false);
98  dl.setCheckmark(false);
99  dl.setShowContract(true);
100 
102  Framework.getCurrent().getPage(),
103  I_.get("Documents"),
104  dl, "80%", "80%", new Command() {
105  @Override
106  public Object execute(Context context) {
107  if(dl != null) {
109  if(batchOf.getBatch() != null) {
110  context.put("batch", batchOf.getBatch());
111  if(command != null) command.execute(context);
112  }
113  }
114  return null;
115  }
116  });
117  }
118 
119  public void loadRelation(final BatchOfType type, FilterGrid filterGrid, final Command command) {
120  final DocumentListbox dl = new DocumentListbox(getPossibleRelation(type, filterGrid));
121  dl.setMultiple(true);
122  dl.setCheckmark(true);
123  dl.setShowContract(true);
124  dl.setShowConcepts(true);
125  dl.setSelectFirst(false);
126 
128  Framework.getCurrent().getPage(),
129  I_.get("Documents"),
130  dl, "80%", "80%", new Command() {
131  @Override
132  public Object execute(Context context) {
133  if(dl != null) {
134  for(Document d : dl.getObjectValues()) {
135  if(d.getDocumentDefinition().getId() == type.getPortfolioId()) {
136  for(DocumentLine l : d.getDocumentLines()) {
137  l.setStore(batchOf.getBatch().getContract());
138  }
139  batchOf.getDocuments().add(d);
140  } else {
141  Collection<DocumentWorkflow> wf = d.getWorkflows();
142  for(final DocumentWorkflow dw : wf) {
143  if(d.getDocumentDefinition().getId() == dw.getAncestor().getId() &&
144  type.getPortfolioId() == dw.getDescendant().getId()) {
145  Document nd = new Document();
146  nd.flowFrom(d, new DocumentWrapper(d).getDocDescription(), dw);
148  nd.setDocumentDate(d.getDocumentDate());
149  nd.setReceiptDate(d.getReceiptDate());
150  } else {
153  }
154  nd.setDocumentNumber(d.getDocumentNumber() + "/1");
155  for(DocumentLine l : nd.getDocumentLines()) {
156  l.setStore(batchOf.getBatch().getContract());
157  }
158  batchOf.getDocuments().add(nd);
159  }
160  }
161  }
162  }
163  if(command != null) command.execute(context);
164  }
165  return null;
166  }
167  });
168  }
169 
170  public void delete(final boolean close) {
171  Messages.confirmDeletion().show(() -> {
172  if(BatchOfWrapper.super.delete()) {
173  if(close) Framework.getCurrent().closeSelected();
174  }
175  });
176  }
177 
178  public IIssue getIssue() {
179  return new DocumentWrapper(batchOf.getBatch()).getIssue();
180  }
181 
182  @Override
183  public boolean save(List<DocumentLine> lines) {
184  for(Document doc : batchOf.getDocuments()) {
185  for(DocumentLine l : doc.getDocumentLines()) {
186  l.setStore(batchOf.getBatch().getContract());
187  }
188  }
190  l.setStore(batchOf.getStore());
191  }
192  return super.save(lines);
193  }
194 
195  private Collection<Document> getPossibleBatch(BatchOfType type) {
196  Dao dao = new FinancialsPU();
197  return dao.getResultList(
198  "select doc from Document as doc " +
199  "where doc.documentDefinition.id = ? " +
200  "and not exists ("+
201  "select batch from BatchOf as batch where batch.batch = doc" +
202  ") " +
203  "order by doc.documentDate",
204  new Object[] { type.getBatchId() });
205  }
206 
207  private Collection<Document> getPossibleRelation(BatchOfType type, FilterGrid filterGrid) {
208  Dao dao = new FinancialsPU();
209  List<Document> relDocs = new ArrayList<Document>();
210  // Already exists the document
211  WhereClause wc = createCriteria(type, filterGrid.getValues());
212  relDocs.addAll(dao.getResultList(wc));
213  // iterate workflows to portfolio
214  PortfolioSet ps = new PortfolioSet();
215  ps.setDocumentDefinition(type.getPortfolioDefinition());
216  ps.load(RelationMode.RELATE_FORWARD, filterGrid.getValues());
217  relDocs.addAll(ps.getDocuments());
218  return relDocs;
219  }
220 
221  private WhereClause createCriteria(BatchOfType type, List<IFilterValue> values) {
222  WhereClause wc = new WhereClause();
223  wc.addClause("select doc from Document as doc");
224  wc.addClause("where doc.documentDefinition.id = :docDef");
225  wc.addNamedValue("docDef", type.getPortfolioId());
226  for(IFilterValue v : values) {
227  v.addConstraint(wc);
228  }
229  wc.addClause("and not exists (");
230  wc.addClause(" select batch from BatchOf as batch where doc member of batch.documents");
231  wc.addClause(") and exists (");
232  wc.addClause(" select line from doc.documentLines as line ");
233  wc.addClause(" where line.store = :store");
234  wc.addNamedValue("store", batchOf.getBatch().getContract());
235  wc.addClause(") ");
236  wc.addClause("order by doc.documentDate");
237  return wc;
238  }
239 
240 }
void addNamedValue(String name, Object value)
static Messages confirmDeletion()
Definition: Messages.java:87
void setStore(Contract store)
Definition: BatchOf.java:95
void setBatch(Document batch)
Definition: BatchOf.java:63
Set< Document > getDocuments()
Definition: BatchOf.java:67
void setDocumentNumber(String documentNumber)
Definition: Document.java:192
void setDocumentDate(Date documentDate)
Definition: Document.java:163
void setContract(Contract contract)
Definition: Document.java:123
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
void setReceiptDate(Date receiptDate)
Definition: Document.java:256
void flowFrom(Document doc, String header, DocumentWorkflow workflow)
Definition: Document.java:539
static String get(String msg)
Definition: I_.java:41
void setSelectFirst(boolean selectFirst)
static Framework getCurrent()
Definition: Framework.java:203