BrightSide Workbench Full Report + Source Code
m303m/ModelGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.treasury.m303m;
20 
21 import java.text.NumberFormat;
22 import org.turro.elephant.util.DecimalFormats;
23 import org.turro.elephant.util.Images;
24 import org.turro.financials.model.business.CompanyWrapper;
25 import org.turro.i18n.I_;
26 import org.turro.math.Zero;
27 import org.turro.util.CompareUtil;
28 import org.turro.zkoss.dialog.SelectionDialog;
29 import org.turro.zkoss.grid.GroupExtended;
30 import org.turro.zkoss.label.AlertLabel;
31 import org.turro.zkoss.label.LabelTypes;
32 import org.turro.zkoss.layout.AuxheaderAdapter;
33 import org.turro.zkoss.layout.GridLayout;
34 import org.zkoss.zk.ui.HtmlBasedComponent;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.event.Events;
38 import org.zkoss.zul.Auxhead;
39 import org.zkoss.zul.Button;
40 import org.zkoss.zul.Label;
41 import org.zkoss.zul.Row;
42 import org.zkoss.zul.Space;
43 import org.zkoss.zul.Toolbarbutton;
44 
49 public class ModelGrid extends GridLayout {
50 
51  private ModelSet modelSet;
52  private NumberFormat currencyFormatter;
53 
54  public ModelGrid() {
55  }
56 
57  public void setExercise(int exercise) {
58  modelSet = new ModelSet(exercise);
59  getChildren().clear();
60  Auxhead head = new Auxhead();
61  head.appendChild(new AuxheaderAdapter("M1", 2));
62  head.appendChild(new AuxheaderAdapter("M2", 2));
63  head.appendChild(new AuxheaderAdapter("M3", 2));
64  head.appendChild(new AuxheaderAdapter("M4", 2));
65  head.appendChild(new AuxheaderAdapter("M5", 2));
66  head.appendChild(new AuxheaderAdapter("M6", 2));
67  head.appendChild(new AuxheaderAdapter("M7", 2));
68  head.appendChild(new AuxheaderAdapter("M8", 2));
69  head.appendChild(new AuxheaderAdapter("M9", 2));
70  head.appendChild(new AuxheaderAdapter("M10", 2));
71  head.appendChild(new AuxheaderAdapter("M11", 2));
72  head.appendChild(new AuxheaderAdapter("M12", 2));
73  appendChild(head);
74  setColumns("min,right-300px,min,right-300px,min,right-300px,min,right-300px," +
75  "min,right-300px,min,right-300px,min,right-300px,min,right-300px," +
76  "min,right-300px,min,right-300px,min,right-300px,min,right-300px,");
77  setColCaptions(new String[] {
78  I_.get("Tax"),
79  I_.get("Amount"),
80  I_.get("Tax"),
81  I_.get("Amount"),
82  I_.get("Tax"),
83  I_.get("Amount"),
84  I_.get("Tax"),
85  I_.get("Amount"),
86  I_.get("Tax"),
87  I_.get("Amount"),
88  I_.get("Tax"),
89  I_.get("Amount"),
90  I_.get("Tax"),
91  I_.get("Amount"),
92  I_.get("Tax"),
93  I_.get("Amount"),
94  I_.get("Tax"),
95  I_.get("Amount"),
96  I_.get("Tax"),
97  I_.get("Amount"),
98  I_.get("Tax"),
99  I_.get("Amount"),
100  I_.get("Tax"),
101  I_.get("Amount")
102  });
103  currencyFormatter = CompanyWrapper.getCurrencyFormatter();
104  compose();
105  }
106 
107  private void compose() {
108  getRows(true);
109  for(ModelEntry me : modelSet) {
110  GroupExtended ge = new GroupExtended(I_.byKey(me.getType().toString()));
111  getRows().appendChild(ge);
112  Row[] rows = new Row[] { new Row(), new Row(), new Row(), new Row(), new Row(), new Row(), new Row(),
113  new Row(), new Row(), new Row(), new Row(), new Row(), new Row(), new Row() };
114  for(int month = 0; month < 12; month++) {
115  composeVats(me.getMonth(month), rows);
116  }
117  for(Row r : rows) {
118  if(r.getChildren().size() > 1) {
119  getRows().appendChild(r);
120  }
121  }
122  }
123  GroupExtended ge = new GroupExtended(I_.get("Total"));
124  getRows().appendChild(ge);
125  Row row = new Row();
126  for(int month = 0; month < 12; month++) {
127  row.appendChild(new Space());
128  row.appendChild(getTotalCell(month));
129  }
130  getRows().appendChild(row);
131  }
132 
133  private void composeVats(EntryMonth em, Row[] rows) {
134  int index = 0;
135  for(VatEntry ve : em.getVatMap().values()) {
136  rows[index].appendChild(new Label(DecimalFormats.format(ve.getVat())));
137  rows[index].appendChild(getAmountsCell(ve));
138  index++;
139  }
140  if(!em.getVatMap().values().isEmpty()) {
141  rows[index].appendChild(new Space());
142  rows[index].appendChild(getTaxableCell(em));
143  }
144  }
145 
146  private HtmlBasedComponent getAmountsCell(final VatEntry ve) {
147  boolean notDeclared = Zero.near(modelSet.getDeclaredMonth(ve.getMonth().getMonth() - 1).getAmountDeclared(), 2);
148  GridLayout gl = new GridLayout("100%,right-160px");
149  gl.addCaption(I_.get("Tax"));
150  gl.addComponent(new Label(currencyFormatter.format(ve.getAmountVat())));
151  gl.addRow();
152  gl.addCaption(I_.get("Declared"));
153  gl.addComponent(new AlertLabel(currencyFormatter.format(ve.getDeclaredVat())) {
154  @Override
155  protected int getType() {
156  if(!Zero.near(ve.getDeclaredVat(), 2)) {
157  if(CompareUtil.compare(ve.getAmountVat(), ve.getDeclaredVat()) == 0) {
158  return AlertLabel.ALERT_RIGHT;
159  } else {
160  return AlertLabel.ALERT_WRONG;
161  }
162  }
163  return 0;
164  }
165  });
166  gl.addRow();
167  gl.addCaption(I_.get("Pending"));
168  gl.addComponent(new Label(currencyFormatter.format(ve.getPendingVat())));
169  gl.addRow();
170  gl.addCaption(I_.get("Taxable"));
171  gl.addComponent(new Label(currencyFormatter.format(ve.getAmountOp())));
172  if(notDeclared) {
173  gl.addRow();
174  gl.addComponent(LabelTypes.getCaptionLabel(I_.get("To declare")));
175  gl.addSpace();
176  gl.addRow();
177  gl.addCaption(I_.get("Tax"));
178  gl.addComponent(LabelTypes.getCaptionLabel(currencyFormatter.format(ve.getToDeclareAmountVat())));
179  gl.addRow();
180  gl.addCaption(I_.get("Taxable"));
181  gl.addComponent(LabelTypes.getCaptionLabel(currencyFormatter.format(ve.getToDeclareAmountOp())));
182  }
183  return gl;
184  }
185 
186  private HtmlBasedComponent getBreakdownCell(final EntryMonth em) {
187  Toolbarbutton breakdown = new Toolbarbutton(I_.get("Breakdown"), Images.getImage("breakdown-open"));
188  breakdown.addEventListener(Events.ON_CLICK, new EventListener() {
189  @Override
190  public void onEvent(Event event) throws Exception {
191  SelectionDialog.showComponent(getPage(), I_.get("Breakdown"),
192  new Model349List(em), "750px", "70%", null);
193  }
194  });
195  return breakdown;
196  }
197 
198  private HtmlBasedComponent getTaxableCell(final EntryMonth em) {
199  boolean notDeclared = Zero.near(modelSet.getDeclaredMonth(em.getMonth()- 1).getAmountDeclared(), 2);
200  GridLayout gl = new GridLayout("100%,right-160px");
201  gl.addCaption(I_.get("Total"));
202  gl.addComponent(new Label(currencyFormatter.format(em.getTotalOp())));
203  if(notDeclared) {
204  gl.addRow();
205  gl.addComponent(LabelTypes.getCaptionLabel(I_.get("To declare")));
206  gl.addSpace();
207  gl.addRow();
208  gl.addCaption(I_.get("Tax"));
209  gl.addComponent(LabelTypes.getCaptionLabel(currencyFormatter.format(em.getToDeclareTotalVat())));
210  gl.addRow();
211  gl.addCaption(I_.get("Taxable"));
212  gl.addComponent(LabelTypes.getCaptionLabel(currencyFormatter.format(em.getToDeclareTotalOp())));
213  }
214  gl.addRow();
215  gl.addComponent(getBreakdownCell(em));
216  gl.addComponent(LabelTypes.getTinyLabel(currencyFormatter.format(em.getAmountOp())));
217  return gl;
218  }
219 
220  private HtmlBasedComponent getTotalCell(final int month) {
221  final double inputVat = modelSet.getTotalInputVat(month),
222  outputVat = modelSet.getTotalOutputVat(month);
223  GridLayout gl = new GridLayout("100%,right-160px");
224  gl.addCaption(I_.get("Input VAT"));
225  gl.addComponent(new Label(currencyFormatter.format(inputVat)));
226  gl.addRow();
227  gl.addCaption(I_.get("Output VAT"));
228  gl.addComponent(new Label(currencyFormatter.format(outputVat)));
229  final double declared = modelSet.getDeclaredMonth(month).getAmountDeclared();
230  gl.addRow();
231  gl.addCaption(I_.get("Declared"));
232  gl.addComponent(new AlertLabel(currencyFormatter.format(Math.abs(declared))) {
233  @Override
234  protected int getType() {
235  if(Zero.near((outputVat - inputVat) - declared, 2)) {
236  return AlertLabel.ALERT_RIGHT;
237  } else {
238  return AlertLabel.ALERT_WRONG;
239  }
240  }
241  });
242  if(Zero.near(declared, 2)) {
243  gl.addRow();
244  Button b = new Button(I_.get("Register"));
245  b.addEventListener(Events.ON_CLICK, new EventListener() {
246  @Override
247  public void onEvent(Event event) throws Exception {
248  modelSet.generateRegister(month);
249  }
250  });
251  gl.addComponent(b);
252  gl.addComponent(new Label(currencyFormatter.format(Math.abs(outputVat - inputVat))));
253  }
254  return gl;
255  }
256 
257 }
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41
void setColCaptions(String captions)
Definition: GridLayout.java:94
Rows getRows(boolean create)