BrightSide Workbench Full Report + Source Code
DocumentComposer.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.document.logic;
20 
21 import java.util.Collection;
22 import java.util.List;
23 import org.turro.command.Command;
24 import org.turro.command.Context;
25 import org.turro.command.LabelControl;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.util.DecimalFormats;
28 import org.turro.elephant.util.Images;
29 import org.turro.elephant.util.Messages;
30 import org.turro.financials.batchof.BatchOfGrid;
31 import org.turro.financials.contract.ContractCombobox;
32 import org.turro.financials.contract.ContractInfo;
33 import org.turro.financials.db.FinancialsPU;
34 import org.turro.financials.document.DocumentDefinitionListbox;
35 import org.turro.financials.document.DocumentExpiriesGrid;
36 import org.turro.financials.document.DocumentLinesGrid;
37 import org.turro.financials.document.DocumentRegistersGrid;
38 import org.turro.financials.document.DocumentReport;
39 import org.turro.financials.document.DocumentSummaryGrid;
40 import org.turro.financials.document.contract.DocumentContractFlowListbox;
41 import org.turro.financials.document.process.DocumentProcessListbox;
42 import org.turro.financials.entity.Contract;
43 import org.turro.financials.entity.ContractFlow;
44 import org.turro.financials.entity.Document;
45 import org.turro.financials.entity.DocumentRelation;
46 import org.turro.financials.menu.FinancialsMenu;
47 import org.turro.financials.relation.RelationshipViewer;
48 import org.turro.i18n.I_;
49 import org.turro.jpa.composer.EntityComposer;
50 import org.turro.jpa.entity.DaoEntity;
51 import org.turro.math.Zero;
52 import org.turro.reflection.XMLSerializer;
53 import org.turro.util.PhraseBuilder;
54 import org.turro.zul.acceptance.AcceptanceButton;
55 import org.turro.zul.frame.Framework;
56 import org.zkoss.zk.ui.Component;
57 import org.zkoss.zk.ui.select.annotation.Listen;
58 import org.zkoss.zk.ui.select.annotation.Wire;
59 import org.zkoss.zul.Label;
60 import org.zkoss.zul.Row;
61 import org.zkoss.zul.Tab;
62 import org.zkoss.zul.Toolbarbutton;
63 
68 public class DocumentComposer extends EntityComposer<Document, Long> {
69 
70  @Wire("#contract")
71  private ContractCombobox contract;
72 
73  @Wire("#entInf")
74  private LabelControl entInf;
75 
76  @Wire("#ctcInf")
77  private ContractInfo ctcInf;
78 
79  @Wire("#properties")
80  private Toolbarbutton properties;
81 
82  @Wire("#lbCtcDef")
83  private DocumentDefinitionListbox lbCtcDef;
84 
85  @Wire("#docProcess")
86  private DocumentProcessListbox docProcess;
87 
88  @Wire("#tablines")
89  private Tab tablines;
90 
91  @Wire("#tabexpiries")
92  private Tab tabexpiries;
93 
94  @Wire("#tabrelations")
95  private Tab tabrelations;
96 
97  @Wire("#tabbatchof")
98  private Tab tabbatchof;
99 
100  @Wire("#tabregister")
101  private Tab tabregister;
102 
103  @Wire("#lines")
104  private DocumentLinesGrid lines;
105 
106  @Wire("#tflow")
107  private Label tflow;
108 
109  @Wire("#lbflow")
110  private DocumentContractFlowListbox lbflow;
111 
112  @Wire("#tadvance")
113  private Label tadvance;
114 
115  @Wire("#ladvance")
116  private Label ladvance;
117 
118  @Wire("#expiries")
119  private DocumentExpiriesGrid expiries;
120 
121  @Wire("#relations")
122  private RelationshipViewer relations;
123 
124  @Wire("#batchOf")
125  private BatchOfGrid batchOf;
126 
127  @Wire("#register")
128  private DocumentRegistersGrid register;
129 
130  @Wire("#acceptance")
131  private AcceptanceButton acceptance;
132 
133  @Wire("#report")
134  private DocumentReport report;
135 
136  @Wire("#reportnv")
137  private DocumentReport reportnv;
138 
139  @Wire("#reportsend")
140  private DocumentReport reportsend;
141 
142  @Wire("#summary")
143  private DocumentSummaryGrid summary;
144 
145  @Listen("onSelect = #tablines")
146  public void onSelectLines() {
147  lines.updateRows();
148  }
149 
150  @Listen("onSelect = #tabexpiries")
151  public void onSelectExpiries() {
152  if(expiries.getCollection() == null || expiries.getCollection().isEmpty()) {
153  expiries.notifyChange();
154  }
155  updateExpiriesOptions();
156  }
157 
158  @Listen("onSelect = #tabrelations")
159  public void onSelectRelations() {
160  relations.notifyChange();
161  }
162 
163  @Listen("onSelect = #tabbatchof")
164  public void onSelectBatchOf() {
165  batchOf.reload();
166  }
167 
168  @Listen("onSelect = #tabregister")
169  public void onSelectRegister() {
170  register.updateDocument(entity, expiries.getRelations());
171  }
172 
173  @Listen("onSelect = #lbflow")
174  public void onSelectFlow() {
175  ContractFlow cf = lbflow.getObjectValue();
176  if(cf != null) {
177  cf.generateFlows(entity);
178  expiries.notifyChange();
179  }
180  }
181 
182  @Listen("onChange = #contract")
183  public void onContractChange() {
184  if(contract.getObjectValue() != null) {
186  ctcInf.setContract(contract.getObjectValue());
187  entInf.setEntity(contract.getObjectValue());
188  entInf.generate();
189  entity.setContract(contract.getObjectValue());
190  lbCtcDef.updateContractDefinition(contract.getObjectValue().getContractDefinition());
191  lbCtcDef.setDisabled(false);
192  lines.updateDocument(entity);
193  docProcess.setDocument(entity);
194  updateExpiriesOptions();
195  } else {
196  lbCtcDef.setDisabled(true);
197  lines.clearTable();
198  expiries.clearTable();
199  }
200  }
201 
202  @Listen("onSelect = #lbCtcDef")
204  entity.setDocumentDefinition(lbCtcDef.getObjectValue());
205  lines.updateDocument(entity);
206  docProcess.setDocument(entity);
207  }
208 
209  @Listen("onSelect = #docProcess")
210  public void onDocProcess() {
211  docProcess.doProcess(entity);
212  lines.updateLines();
213  expiries.updateLines();
214  }
215 
216  @Listen("onClick = #properties")
217  public void onProperties() {
219  w.changeContract(new Command() {
220  @Override
221  public Object execute(Context context) {
222  contract.setObjectValue((Contract) context.get("contract"));
223  ctcInf.setContract(contract.getObjectValue());
224  entInf.setEntity(contract.getObjectValue());
225  entInf.generate();
226  entity.setContract(contract.getObjectValue());
227  updateExpiriesOptions();
228  return null;
229  }
230  });
231  }
232 
233  @Listen("onChange = #lines")
234  public void onLinesChange() {
235  summary.updateSummary();
236  }
237 
238  @Listen("onChange = #relations")
239  public void onRelationsChange() {
240  lines.updateLines();
241  summary.updateSummary();
242  }
243 
244  @Listen("onClick = #clone")
245  public void onDocumentClone() {
247  w.cloneDocument();
248  }
249 
250  @Listen("onClick = #copyLines")
251  public void onCopyLines() {
253  w.copyLines();
254  }
255 
256  @Listen("onClick = #pasteLines")
257  public void onPasteLines() {
259  w.pasteLines();
260  lines.updateLines();
261  summary.updateSummary();
262  }
263 
264  @Listen("onClick = #appendRow")
265  public void onAppendRow() {
266  lines.addNewRow();
267  }
268 
269  @Listen("onClick = #insertRow")
270  public void onInsertRow() {
271  Row row = lines.getLastRow();
272  if(row != null) {
273  lines.insertInSelectedRow(row);
274  }
275  }
276 
277  @Listen("onClick = #deleteRow")
278  public void onDeleteRow() {
279  Row row = lines.getLastRow();
280  if(row != null) {
281  lines.insertInSelectedRow(row);
282  }
283  }
284 
285  @Override
286  protected String getAttributeName() {
287  return "document";
288  }
289 
290  @Override
291  protected Document getEntityInstance(Long id) {
292  if(id == null) {
293  entity = new Document();
294  entity.setDocumentDate(new java.util.Date());
295  entity.setReceiptDate(entity.getDocumentDate());
296  } else {
297  entity = new FinancialsPU().find(Document.class, id);
298  }
299  return entity;
300  }
301 
302  @Override
303  protected DaoEntity getWrapperInstance(Component comp) {
307  return w;
308  }
309 
310  @Override
311  protected boolean beforeDelete() {
313  return super.beforeSave() && !acceptance.isBlocked();
314  }
315 
316  @Override
317  protected boolean beforeSave() {
319  w.setPriorSavingValues(lines.getValues(), (List<DocumentRelation>) expiries.getRelations());
320  return super.beforeSave() && (acceptance == null || !acceptance.isBlocked());
321  }
322 
323  @Override
324  protected void doOnDelete() {
326  PhraseBuilder pb = new PhraseBuilder();
327  boolean wrongDate = !w.checkSave(pb);
328  boolean batchs = !w.checkBatchs(pb);
329  if(wrongDate || batchs) {
330  if(batchs) {
331  Messages.info(I_.get("Can't delete")).add(pb.toString()).show();
332  // can't save
333  } else if(wrongDate && !Application.getApplication().isInRole("finan-model:edit")) {
334  // can't save
335  Messages.info(I_.get("Can't delete")).add(pb.toString()).show();
336  } else {
337  pb.addWord(I_.get("Proceed anyway"));
338  Messages.question().add(pb.toString()).show(() -> {
340  });
341  }
342  } else {
343  super.doOnDelete();
344  }
345  }
346 
347  @Override
348  protected void doOnSave() {
350  PhraseBuilder pb = new PhraseBuilder();
351  boolean wrongDate = !w.checkSave(pb);
352  boolean duplicates = !w.checkDuplicates(pb);
353  if(wrongDate || duplicates) {
354  if(wrongDate && !Application.getApplication().isInRole("finan-model:edit")) {
355  // can't save
356  Messages.info(I_.get("Can't delete")).add(pb.toString()).show();
357  } else {
358  pb.addWord(I_.get("Proceed anyway"));
359  Messages.question().add(pb.toString()).show(() -> {
360  DocumentComposer.super.doOnSave();
361  });
362  }
363  } else {
364  super.doOnSave();
365  }
366  }
367 
368 
369  @Override
370  public void afterSave() {
371  if(entity != null) {
373  }
374  }
375 
376  @Override
377  protected boolean shouldBeSaved() {
378  return !entity.isEmpty() && (entity.getId() == 0) || super.shouldBeSaved();
379  }
380 
381  @Override
382  public void doFinally() throws Exception {
383  super.doFinally();
384  if(entity.getId() == 0) {
385  acceptance.setVisible(false);
386  entInf.setVisible(false);
387  properties.setVisible(false);
388  } else {
389  contract.setVisible(false);
390  lbCtcDef.setContractDefinition(entity.getContract().getContractDefinition());
391  lbCtcDef.setObjectValue(entity.getDocumentDefinition());
392  }
393  lbCtcDef.setDisabled(true);
394  contract.setObjectValue(entity.getContract());
395  ctcInf.setContract(entity.getContract());
396  entInf.setEntity(entity.getContract());
397  entInf.generate();
398  docProcess.setDocument(entity);
399  updateExpiriesOptions();
400  report.setGrid(lines);
401  reportnv.setGrid(lines);
402  reportsend.setGrid(lines);
403  reportsend.setImage(Images.getImage("mail_send"));
404  reportsend.setTooltiptext(I_.get("Send documents"));
405  }
406 
407  @Override
408  protected boolean inSaveRole() {
409  return Application.getApplication().isInRole("finan-document:edit");
410  }
411 
412  @Override
413  protected boolean inDeleteRole() {
414  return Application.getApplication().isInRole("finan-document:delete");
415  }
416 
417  private String expiriesXML = "";
418 
419  @Override
420  protected void retrieveEntity() {
421  super.retrieveEntity();
422  for(DocumentRelation dr : entity.getDescendants()) {
423  if(dr.getDescendant().isValid()) {
424  expiriesXML += new XMLSerializer(dr.getDescendant()).data();
425  }
426  }
427  }
428 
429 // @Override
430 // protected boolean hasChanged() {
431 // boolean hasChanged = super.hasChanged();
432 // if(!hasChanged) {
433 // String newXML = "";
434 // for(DocumentRelation dr : entity.getDescendants()) {
435 // if(dr.getDescendant().isValid()) {
436 // newXML += new XMLSerializer(dr.getDescendant()).data();
437 // }
438 // }
439 // hasChanged = CompareUtil.compare(newXML, expiriesXML) != 0;
440 // }
441 // return hasChanged;
442 // }
443 
444  private void updateExpiriesOptions() {
446  double advance = w.getAdvance();
447  if(!Zero.near(advance, 2)) {
448  tadvance.setVisible(true);
449  ladvance.setVisible(true);
450  ladvance.setValue(DecimalFormats.format(advance));
451  } else {
452  tadvance.setVisible(false);
453  ladvance.setVisible(false);
454  }
455  Collection<ContractFlow> cfs = entity.getContractFlows();
456  if(cfs == null || cfs.isEmpty()) {
457  tflow.setVisible(false);
458  lbflow.setVisible(false);
459  } else {
460  lbflow.setAllowNull(true);
461  lbflow.setDocument(entity);
462  lbflow.setObjectValue(null);
463  tflow.setVisible(true);
464  lbflow.setVisible(true);
465  }
466  }
467 
468 }
void setEntity(Object entity)
static String format(Number value, String pattern)
static String getImage(String image)
Definition: Images.java:36
Messages add(String word)
Definition: Messages.java:50
void setContractDefinition(ContractDefinition contractDefinition)
void updateContractDefinition(ContractDefinition contractDefinition)
static String get(String msg)
Definition: I_.java:41
static boolean near(double value, int digits)
Definition: Zero.java:30
static Framework getCurrent()
Definition: Framework.java:203
void setSelectedLabel(String text)
Definition: Framework.java:103
void setSelectedTooltiptext(String text)
Definition: Framework.java:107