BrightSide Workbench Full Report + Source Code
CompanyExercises.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.business;
20 
21 import java.util.Collection;
22 import java.util.Set;
23 import org.turro.elephant.util.DecimalFormats;
24 import org.turro.elephant.util.Images;
25 import org.turro.elephant.util.Messages;
26 import org.turro.financials.entity.Register;
27 import org.turro.financials.menu.FinancialsMenu;
28 import org.turro.financials.model.register.AccountItem;
29 import org.turro.financials.model.register.ClosingRegisters;
30 import org.turro.i18n.I_;
31 import org.turro.math.Zero;
32 import org.turro.zkoss.dialog.SelectionDialog;
33 import org.turro.zkoss.grid.PagingGrid;
34 import org.zkoss.zk.ui.event.Event;
35 import org.zkoss.zk.ui.event.EventListener;
36 import org.zkoss.zk.ui.event.Events;
37 import org.zkoss.zk.ui.ext.AfterCompose;
38 import org.zkoss.zul.Button;
39 import org.zkoss.zul.Checkbox;
40 import org.zkoss.zul.Column;
41 import org.zkoss.zul.Columns;
42 import org.zkoss.zul.Hlayout;
43 import org.zkoss.zul.Image;
44 import org.zkoss.zul.Label;
45 import org.zkoss.zul.Row;
46 import org.zkoss.zul.Rows;
47 
52 public class CompanyExercises extends PagingGrid implements AfterCompose {
53 
54  @Override
55  public void afterCompose() {
56  addColumns();
57  addRows();
58  }
59 
60  public void refresh() {
61  getRows(true).getChildren().clear();
62  addRows();
63  }
64 
65  private void addRows() {
66  final ClosingRegisters cr = new ClosingRegisters(
67  I_.get("Start exercise"),
68  I_.get("End exercise"),
69  I_.get("Operating closure"));
70 
71  Collection<Integer> exercises = cr.getExercises();
72 
73  Rows rows = getRows(true);
74  boolean removalShown = false;
75 
76  for(final Integer exercise : exercises) {
77  Row row = new Row();
78  rows.appendChild(row);
79  row.appendChild(new Label(exercise + ""));
80 
81  boolean closed = cr.existsClosingRegisters(exercise);
82  Double amount129 = null;
83 
84  Hlayout hbox = new Hlayout();
85  hbox.setSpacing("5px");
86  if(closed) {
87  hbox.appendChild(new Label(I_.get("Closed")));
88  final Set<AccountItem> items = cr.getNotClosed(exercise);
89  if(!items.isEmpty()) {
90  Image img = new Image(Images.getImage("warn"));
91  img.setTooltiptext(I_.get("Unbalanced"));
92  img.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
93  @Override
94  public void onEvent(Event event) throws Exception {
95  AccountItemListbox ail = new AccountItemListbox();
96  ail.setCollection(items);
97  SelectionDialog.showComponent(getPage(), I_.get("Unbalanced"),
98  ail, "80%", "400px", null);
99  }
100  });
101  hbox.appendChild(img);
102  }
103  } else {
104  hbox.appendChild(new Label(I_.get("Opened")));
105  amount129 = cr.get129(exercise);
106  if(amount129 != null && !Zero.near(amount129, 2)) {
107  Image img = new Image(Images.getImage("warn"));
108  img.setTooltiptext("129 = " + DecimalFormats.format(amount129));
109  hbox.appendChild(img);
110  }
111  }
112  row.appendChild(hbox);
113 
114  hbox = new Hlayout();
115  Button regen = new Button(I_.get("Generate registers"));
116  regen.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
117  @Override
118  public void onEvent(Event event) throws Exception {
119  Messages.confirmProcess().add(I_.get("Generate registers")).show(() -> {
120  cr.deleteClosingRegisters(exercise);
121  cr.createClosingRegisters(exercise);
122  refresh();
123  });
124  }
125  });
126  hbox.appendChild(regen);
127  if(closed) {
128  Button show = new Button(I_.get("Show"));
129  show.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
130  @Override
131  public void onEvent(Event event) throws Exception {
132  for(Register r : cr.getClosureRegisters(exercise)) {
133  FinancialsMenu.showRegister(r);
134  }
135  }
136  });
137  hbox.appendChild(show);
138  Button delete = new Button(I_.get("Delete"));
139  delete.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
140  @Override
141  public void onEvent(Event event) throws Exception {
142  Messages.confirmDeletion().show(() -> {
143  cr.deleteClosingRegisters(exercise);
144  refresh();
145  });
146  }
147  });
148  hbox.appendChild(delete);
149  }
150  row.appendChild(hbox);
151 
152  hbox = new Hlayout();
153  Button renum = new Button(I_.get("Renumerate registers"));
154  final Checkbox booksTo = new Checkbox(I_.get("Remun book registers"));
155  renum.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
156  @Override
157  public void onEvent(Event event) throws Exception {
158  Messages.confirmProcess().add(I_.get("Renumerate registers")).show(() -> {
159  CompanyWrapper.renumRegisters(exercise, booksTo.isChecked());
160  });
161  }
162  });
163  hbox.appendChild(renum);
164  hbox.appendChild(booksTo);
165  row.appendChild(hbox);
166 
167  hbox = new Hlayout();
168  if(!removalShown) {
169  removalShown = true;
170  Button removal = new Button(I_.get("Remove exercise"));
171  removal.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
172  @Override
173  public void onEvent(Event event) throws Exception {
174  Messages.confirmDeletion().add(I_.get("Remove exercise"))
175  .line().add(I_.get("Ensure the exercise is close."))
176  .line().add(I_.get("This process will remove movements and documents."))
177  .show(() -> {
178  CompanyWrapper.removeExercise(exercise);
179  refresh();
180  });
181  }
182  });
183  hbox.appendChild(removal);
184  }
185  row.appendChild(hbox);
186  }
187 
188  setRowCount(exercises.size());
189  }
190 
191  private void addColumns() {
192  Columns cols = new Columns();
193  appendChild(cols);
194 
195  Column col = new Column(I_.get("Exercise"));
196  col.setWidth("100px");
197  cols.appendChild(col);
198 
199  col = new Column(I_.get("Status"));
200  col.setWidth("150px");
201  cols.appendChild(col);
202 
203  col = new Column(I_.get("Closing"));
204  col.setHflex("1");
205  cols.appendChild(col);
206 
207  col = new Column(I_.get("Renum"));
208  col.setHflex("1");
209  cols.appendChild(col);
210 
211  col = new Column(I_.get("Management"));
212  col.setHflex("1");
213  cols.appendChild(col);
214  }
215 
216 }
static String getImage(String image)
Definition: Images.java:36
static String get(String msg)
Definition: I_.java:41
Rows getRows(boolean create)