BrightSide Workbench Full Report + Source Code
bsfinancials-www/src/main/java/org/turro/financials/document/logic/DocumentWrapper.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.Arrays;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.amic.util.date.CheckDate;
27 import org.turro.command.Command;
28 import org.turro.command.Context;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.util.DateFormats;
31 import org.turro.elephant.util.DecimalFormats;
32 import org.turro.elephant.zkoss.Modal;
33 import org.turro.elephant.zkoss.ModalWindow;
34 import org.turro.financials.contract.ContractCombobox;
35 import org.turro.financials.contract.logic.ContractWrapper;
36 import org.turro.financials.db.FinancialsPU;
37 import org.turro.financials.document.action.CloneDocumentAction;
38 import org.turro.financials.entity.*;
39 import org.turro.financials.model.business.CompanyWrapper;
40 import org.turro.financials.model.document.SimpleDocumentLineList;
41 import org.turro.i18n.I_;
42 import org.turro.jpa.Dao;
43 import org.turro.plugin.dossier.IDossier;
44 import org.turro.plugin.dossier.IIssue;
45 import org.turro.plugin.scheduler.ScheduledEdit;
46 import org.turro.util.PhraseBuilder;
47 import org.turro.zkoss.dialog.InputDialog;
48 import org.turro.zkoss.dialog.InputField;
49 import org.turro.zkoss.input.CollectionListbox;
50 import org.turro.zkoss.layout.GridLayout;
51 import org.turro.zul.frame.Framework;
52 import org.zkoss.zk.ui.HtmlBasedComponent;
53 import org.zkoss.zul.Checkbox;
54 
59 public class DocumentWrapper extends org.turro.financials.model.document.DocumentWrapper implements ScheduledEdit {
60 
61  public DocumentWrapper(Document document) {
62  super(document);
63  }
64 
65  public String getTabLabel() {
66  return getSelfLabel() + " #" + entity.getId();
67  }
68 
69  public String getSelfLabel() {
70  return I_.get("Document");
71  }
72 
73  public boolean checkSave(PhraseBuilder pb) {
74  if(!CompanyWrapper.isValidDate(entity.getReceiptDate())) {
75  pb.addWord(I_.get("Closing date"));
76  pb.addWord("\n");
78  pb.addWord("\n");
79  return false;
80  }
81  for(DocumentRelation dr : entity.getDescendants()) {
82  if(dr.getDescendant() != null && !CompanyWrapper.isValidDate(dr.getDescendant().getReceiptDate())) {
83  pb.addWord(I_.get("Closing date"));
84  pb.addWord("\n");
86  pb.addWord("\n");
87  return false;
88  }
89  }
90  return true;
91  }
92 
93  public boolean checkBatchs(PhraseBuilder pb) {
94  Collection<BatchOf> batchOfs = BatchOf.getFrom(entity);
95  if(!batchOfs.isEmpty()) {
96  pb.addWord(I_.get("Can't delete") + ":\n\n");
97  for(BatchOf b : batchOfs) {
98  pb.addWord(new DocumentWrapper(b.getBatch()).getFullDescription() + "\n");
99  }
100  return false;
101  }
102  return true;
103  }
104 
105  public String getDocDescription() {
106  return entity.getDocumentDefinition().getName() + ": " +
107  (entity.getForcedView() == null ? "" : "(" + entity.getForcedView().getName() + ") ") +
108  entity.getDocumentNumber() + " " +
109  DateFormats.format(entity.getDocumentDate(), true);
110  }
111 
112  public String getFullDescription() {
113  return getDocDescription() + " | " +
114  DecimalFormats.format(entity.getTotalAmount());
115  }
116 
117  public String getTooltiptext() {
118  try {
119  return entity.getContract().getFullDescription() + "\n" +
120  getDocDescription() + " | " +
121  DecimalFormats.format(entity.getTotalAmount());
122  } catch(Exception ex) {
123  return null;
124  }
125  }
126 
127  public void changeContract(final Command command) {
129  Framework.getCurrent().getPage(),
130  I_.get("Change"),
131  new InputField[] {
132  new InputField("Contract", null, null, 0) {
133  @Override
134  protected HtmlBasedComponent createEditor() {
135  ContractCombobox ccb = new ContractCombobox();
136  ccb.setOnlyActive(true);
137  ccb.setDocumentDefinition(entity.getDocumentDefinition());
138  ccb.setObjectValue(entity.getContract());
139  return ccb;
140  }
141  }
142  }, new Command() {
143  @Override
144  public Object execute(Context context) {
145  InputField[] fields = (InputField[]) context.get("fields");
146  if(fields.length > 0) {
147  for(InputField f : fields) {
148  if("Contract".equals(f.getLabel())) {
149  context.put("contract", f.getValue());
150  if(command != null) command.execute(context);
151  break;
152  }
153  }
154  }
155  return null;
156  }
157  });
158  }
159 
160  public void cloneDocument() {
161  try {
162  Context ctx = new Context();
163  ctx.put("entity", entity);
164  new CloneDocumentAction().execute(ctx);
165  } catch (Exception ex) {
166  Logger.getLogger(DocumentWrapper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
167  }
168  }
169 
170  public void copyLines() {
171  Framework.getClipboard().put("bbf_clip_doc_lin", new SimpleDocumentLineList(entity));
172  }
173 
174  public void pasteLines() {
175  SimpleDocumentLineList sdll = (SimpleDocumentLineList) Framework.getClipboard().get("bbf_clip_doc_lin");
176  if(sdll != null) {
177  sdll.addLines(entity);
178  }
179  }
180 
181  public boolean checkDuplicates(PhraseBuilder pb) {
182  Dao dao = new FinancialsPU();
183  List<Document> docs = dao.getResultList(
184  "select doc from Document as doc " +
185  "where doc.documentNumber = ? " +
186  "and doc.documentDefinition = ? " +
187  "and doc.contract = ? " +
188  "and year(doc.documentDate) = ? " +
189  "and doc.id <> ?",
190  new Object[] {
191  entity.getDocumentNumber(),
192  entity.getDocumentDefinition(),
193  entity.getContract(),
194  new CheckDate(entity.getDocumentDate()).getYear(),
195  entity.getId()
196  });
197  if(!docs.isEmpty()) {
198  pb.addWord(I_.get("Posible duplicates, save anyway?"));
199  pb.addWord("\n");
200  for(Document d : docs) {
201  pb.addWord("\n");
202  pb.addWord(new DocumentWrapper(d).getFullDescription());
203  }
204  pb.addWord("\n");
205  return false;
206  } else {
207  return true;
208  }
209  }
210 
211  public void relateAncestors(final DocumentDefinition documentDefinition, final Command command) {
212  HashMap args = new HashMap();
213  List<Document> list = getRelatedAncestor(documentDefinition);
214  args.put("relatedDoc", list);
215  args.put("mode", "descendant");
216  final ModalWindow rels = Modal.getModal("/WEB-INF/_zul/financials/document/relation.zul", null, args);
217  Modal.doModal(rels, new Command() {
218  @Override
219  public Object execute(Context context) {
220  if(rels.getResult() > 0) {
221  for(Document doc : (Collection<Document>) rels.getResultValue()) {
222  addRelation(doc, true, rels.getResult(), getSuitableWorkflow(documentDefinition));
223  }
224  if(command != null) command.execute(null);
225  }
226  return null;
227  }
228  });
229  }
230 
231  public void relateDescendants(final DocumentDefinition documentDefinition, final Command command) {
232  HashMap args = new HashMap();
233  List<Document> list = getRelatedDescendant(documentDefinition);
234  args.put("relatedDoc", list);
235  args.put("mode", "ancestor");
236  final ModalWindow rels = Modal.getModal("/WEB-INF/_zul/financials/document/relation.zul", null, args);
237  Modal.doModal(rels, new Command() {
238  @Override
239  public Object execute(Context context) {
240  if(rels.getResult() > 0) {
241  for(Document doc : (Collection<Document>) rels.getResultValue()) {
242  addRelation(doc, false, rels.getResult(), getSuitableWorkflow(documentDefinition));
243  }
244  if(command != null) command.execute(null);
245  }
246  return null;
247  }
248  });
249  }
250 
251  private void addRelation(Document relDoc, boolean ancestor, int mode, DocumentWorkflow dw) {
253  if(ancestor) {
254  rel.setDescendant(entity);
255  rel.setAncestor(relDoc);
256  entity.getAncestors().add(rel);
257  if(mode == 2) {
258  // lines
259  entity.appendLines(relDoc, new DocumentWrapper(relDoc).getDocDescription(), dw);
260  } else if(mode == 3) {
261  // amount
262  entity.appendAmounts(relDoc, new DocumentWrapper(relDoc).getDocDescription(), dw);
263  }
264  } else {
265  rel.setDescendant(relDoc);
266  rel.setAncestor(entity);
267  entity.getDescendants().add(rel);
268  }
269  }
270 
272  for(DocumentWorkflow dw : entity.getContract().getContractDefinition().getDocumentWorkflows()) {
273  if((dw.getAncestor().getId() == entity.getDocumentDefinition().getId() &&
274  dw.getDescendant().getId() == docDef.getId()) ||
275  (dw.getDescendant().getId() == entity.getDocumentDefinition().getId() &&
276  dw.getAncestor().getId() == docDef.getId())) {
277  return dw;
278  }
279  }
280  return null;
281  }
282 
283  public IIssue getIssue() {
284  if(entity.getId() > 0) {
285  IDossier dossier = new ContractWrapper(entity.getContract()).getDossier();
286  IIssue issue = dossier.addIssue();
287  issue.setDescription(getFullDescription());
288  issue.setPath(FinancialsPU.getObjectPath(entity));
289  return issue;
290  }
291  return null;
292  }
293 
294  /* ScheduledEdit */
295 
296  private CollectionListbox<String> actions = null;
297  private Checkbox draft = null;
298 
299  @Override
300  public Object getDataEditor(String data) {
301  GridLayout gl = new GridLayout("min,1");
302  actions = new CollectionListbox<String>() {
303  @Override
304  protected String convertToString(String v) {
305  return v;
306  }
307  };
308  actions.setMold("select");
309  actions.setCollection(Arrays.asList(new String[] { I_.get("Clone document") }));
310  if(data != null) {
311  if(data.contains(":clone")) {
312  actions.setObjectValue(I_.get("Clone document"));
313  }
314  }
315  gl.addCaption(I_.get("Action"));
316  gl.addComponent(actions);
317  gl.addRow();
318  draft = new Checkbox(I_.get("Draft"));
319  draft.setChecked(data == null || data.contains(":draft"));
320  gl.addSpace();
321  gl.addComponent(draft);
322  return gl;
323  }
324 
325  @Override
326  public String getDataEditorValue() {
327  String data = "action";
328  if(actions.getObjectValue().equals(I_.get("Clone document"))) {
329  data += ":clone";
330  }
331  if(draft.isChecked()) {
332  data += ":draft";
333  }
334  return data;
335  }
336 
337 }
static final String format(Date d, boolean dateOnly)
static String format(Number value, String pattern)
static int doModal(String file)
Definition: Modal.java:38
static ModalWindow getModal(String file)
Definition: Modal.java:109
static String getObjectPath(Object object)
static Collection< BatchOf > getFrom(Document document)
Definition: BatchOf.java:135
static String get(String msg)
Definition: I_.java:41
boolean equals(Object obj)
Definition: DaoEntity.java:154
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
void setCollection(Collection< V > collection)
GridLayout addComponent(HtmlBasedComponent comp)
GridLayout addCaption(String label)
static FrameClipboard getClipboard()
Definition: Framework.java:220
static Framework getCurrent()
Definition: Framework.java:203
void setDescription(String description)