BrightSide Workbench Full Report + Source Code
CashInvoiceAdvance.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.process;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
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.zkoss.Modal;
28 import org.turro.elephant.zkoss.ModalWindow;
29 import org.turro.financials.db.FinancialsPU;
30 import org.turro.financials.entity.Contract;
31 import org.turro.financials.entity.Document;
32 import org.turro.financials.entity.DocumentDefinition;
33 import org.turro.financials.entity.DocumentLine;
34 import org.turro.financials.entity.DocumentRelation;
35 import org.turro.financials.model.document.SingleLineDocumentWrapper;
36 import org.turro.jpa.Dao;
37 import org.turro.plugin.filter.IFilterValue;
38 import org.turro.zkoss.filter.FilterGrid;
39 import org.turro.zul.frame.Framework;
40 
45 public class CashInvoiceAdvance extends DocumentProcess {
46 
47  public static boolean isAccepted(Document document) {
48  return (document != null && document.getContract() != null &&
51  document.getDocumentDefinition() != null &&
53  }
54 
55  @Override
56  public String getLabel() {
57  return "Selection";
58  }
59 
60  @Override
61  public void doProcess(final Document document) {
62  HashMap args = new HashMap();
63  args.put("currentDocProcess", this);
64  args.put("currentStore", Framework.getAttribute(this, "store"));
65  final ModalWindow processForm = Modal.getModal("/WEB-INF/_zul/financials/document/process/cashInvoiceAdvance.zul", null, args);
66  Modal.doModal(processForm, new Command() {
67  @Override
68  public Object execute(Context context) {
69  if(processForm.getResult() == 1) {
70  Collection<Document> list = (Collection<Document>) ((Object[]) processForm.getResultValue())[0];
71  Contract store = (Contract) ((Object[]) processForm.getResultValue())[1];
72  Framework.setAttribute(this, "store", store);
73  int count = document.getMaxLineOrder() + 1;
74  Dao dao = new FinancialsPU();
75  for(Document d : list) {
76  DocumentLine dl = new DocumentLine();
78  document.getDocumentLines().add(dl);
79  dl.setConcept(d.getContract().getFullDescription() + "\n" + d.getDocumentString());
80  dl.setPrice(d.getTotalAmount());
81  dl.setLineOrder(count++);
83  dl.setStore(store);
84  for(DocumentRelation dr : d.getDescendants()) {
86  Document dd = dr.getDescendant();
88  sldw.setPrice(dd.getTotalAmount());
90  sldw.setStore(store);
91  }
92  }
93  }
94  return true;
95  }
96  });
97 // if(Modal.doModal(processForm) == 1) {
98 // Collection<Document> list = (Collection<Document>) ((Object[]) processForm.getResultValue())[0];
99 // Contract store = (Contract) ((Object[]) processForm.getResultValue())[1];
100 // Framework.setAttribute(this, "store", store);
101 // int count = document.getMaxLineOrder() + 1;
102 // Dao dao = new FinancialsPU();
103 // for(Document d : list) {
104 // DocumentLine dl = new DocumentLine();
105 // dl.setDocument(document);
106 // document.getDocumentLines().add(dl);
107 // dl.setConcept(d.getContract().getFullDescription() + "\n" + d.getDocumentString());
108 // dl.setPrice(d.getTotalAmount());
109 // dl.setLineOrder(count++);
110 // dl.setLineType(document.getDefaultLineType());
111 // dl.setStore(store);
112 // for(DocumentRelation dr : d.getDescendants()) {
113 // SingleLineDocumentWrapper sldw = new SingleLineDocumentWrapper(document, dao.find(DocumentDefinition.class, 61L));
114 // Document dd = dr.getDescendant();
115 // sldw.setConcept(dd.getContract().getFullDescription() + "\n" + dd.getDocumentString());
116 // sldw.setPrice(dd.getTotalAmount());
117 // sldw.setDocumentDate(dd.getDocumentDate());
118 // sldw.setStore(store);
119 // }
120 // }
121 // }
122  }
123 
124  public Collection<Document> getDocuments(FilterGrid filterGrid) {
125  Dao dao = new FinancialsPU();
126  List<Document> relDocs = new ArrayList<>();
127  WhereClause wc = createCriteria(filterGrid.getValues());
128  relDocs.addAll(dao.getResultList(wc));
129  return relDocs;
130  }
131 
132  private WhereClause createCriteria(List<IFilterValue> values) {
133  WhereClause wc = new WhereClause();
134  wc.addClause("select doc from Document as doc");
135  wc.addClause("where doc.documentDefinition.id = 1"); // Factura client
136  for(IFilterValue v : values) {
137  v.addConstraint(wc);
138  }
139  wc.addClause("order by doc.documentDate");
140  return wc;
141  }
142 
143 }
static int doModal(String file)
Definition: Modal.java:38
static ModalWindow getModal(String file)
Definition: Modal.java:109
Collection< Document > getDocuments(FilterGrid filterGrid)
ContractDefinition getContractDefinition()
Definition: Contract.java:125
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
List< IFilterValue > getValues()
static Object getAttribute(Object object, String key)
Definition: Framework.java:207
static void setAttribute(Object object, String key, Object value)
Definition: Framework.java:216