BrightSide Workbench Full Report + Source Code
m303/ModelGrid.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.treasury.m303;
19 
20 import java.text.NumberFormat;
21 import org.turro.elephant.util.DecimalFormats;
22 import org.turro.elephant.util.Images;
23 import org.turro.financials.model.business.CompanyWrapper;
24 import org.turro.i18n.I_;
25 import org.turro.math.Zero;
26 import org.turro.util.CompareUtil;
27 import org.turro.zkoss.dialog.SelectionDialog;
28 import org.turro.zkoss.grid.GroupExtended;
29 import org.turro.zkoss.label.AlertLabel;
30 import org.turro.zkoss.label.LabelTypes;
31 import org.turro.zkoss.layout.AuxheaderAdapter;
32 import org.turro.zkoss.layout.GridLayout;
33 import org.zkoss.zk.ui.HtmlBasedComponent;
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.zul.*;
38 
43 public class ModelGrid extends GridLayout {
44 
45  private ModelSet modelSet;
46  private NumberFormat currencyFormatter;
47 
48  public ModelGrid() {
49  }
50 
51  public void setExercise(int exercise) {
52  modelSet = new ModelSet(exercise);
53  getChildren().clear();
54  Auxhead head = new Auxhead();
55  head.appendChild(new AuxheaderAdapter("Q1", 2));
56  head.appendChild(new AuxheaderAdapter("Q2", 2));
57  head.appendChild(new AuxheaderAdapter("Q3", 2));
58  head.appendChild(new AuxheaderAdapter("Q4", 2));
59  appendChild(head);
60  setColumns("min,right-300px,min,right-300px,min,right-300px,min,right-300px");
61  setColCaptions(new String[] {
62  I_.get("Tax"),
63  I_.get("Amount"),
64  I_.get("Tax"),
65  I_.get("Amount"),
66  I_.get("Tax"),
67  I_.get("Amount"),
68  I_.get("Tax"),
69  I_.get("Amount")
70  });
71  currencyFormatter = CompanyWrapper.getCurrencyFormatter();
72  compose();
73  }
74 
75  private void compose() {
76  getRows(true);
77  for(ModelEntry me : modelSet) {
78  GroupExtended ge = new GroupExtended(I_.byKey(me.getType().toString()));
79  getRows().appendChild(ge);
80  Row[] rows = new Row[] { new Row(), new Row(), new Row(), new Row(), new Row(), new Row(), new Row(),
81  new Row(), new Row(), new Row(), new Row(), new Row(), new Row(), new Row() };
82  for(int quarter = 0; quarter < 4; quarter++) {
83  composeVats(me.getQuarter(quarter), rows);
84  }
85  for(Row r : rows) {
86  if(r.getChildren().size() > 1) {
87  getRows().appendChild(r);
88  }
89  }
90  }
91  GroupExtended ge = new GroupExtended(I_.get("Total"));
92  getRows().appendChild(ge);
93  Row row = new Row();
94  for(int quarter = 0; quarter < 4; quarter++) {
95  row.appendChild(new Space());
96  row.appendChild(getTotalCell(quarter));
97  }
98  getRows().appendChild(row);
99  }
100 
101  private void composeVats(EntryQuarter eq, Row[] rows) {
102  int index = 0;
103  for(VatEntry ve : eq.getVatMap().values()) {
104  rows[index].appendChild(new Label(DecimalFormats.format(ve.getVat())));
105  rows[index].appendChild(getAmountsCell(ve));
106  index++;
107  }
108  if(!eq.getVatMap().values().isEmpty()) {
109  rows[index].appendChild(new Space());
110  rows[index].appendChild(getTaxableCell(eq));
111  }
112  }
113 
114  private HtmlBasedComponent getAmountsCell(final VatEntry ve) {
115  boolean notDeclared = Zero.near(modelSet.getDeclaredQuarter(ve.getQuarter().getQuarter() - 1).getAmountDeclared(), 2);
116  GridLayout gl = new GridLayout("100%,right-160px");
117  gl.addCaption(I_.get("Tax"));
118  gl.addComponent(new Label(currencyFormatter.format(ve.getAmountVat())));
119  gl.addRow();
120  gl.addCaption(I_.get("Declared"));
121  gl.addComponent(new AlertLabel(currencyFormatter.format(ve.getDeclaredVat())) {
122  @Override
123  protected int getType() {
124  if(!Zero.near(ve.getDeclaredVat(), 2)) {
125  if(CompareUtil.compare(ve.getAmountVat(), ve.getDeclaredVat()) == 0) {
126  return AlertLabel.ALERT_RIGHT;
127  } else {
128  return AlertLabel.ALERT_WRONG;
129  }
130  }
131  return 0;
132  }
133  });
134  gl.addRow();
135  gl.addCaption(I_.get("Pending"));
136  gl.addComponent(new Label(currencyFormatter.format(ve.getPendingVat())));
137  gl.addRow();
138  gl.addCaption(I_.get("Taxable"));
139  gl.addComponent(new Label(currencyFormatter.format(ve.getAmountOp())));
140  if(notDeclared) {
141  gl.addRow();
142  gl.addComponent(LabelTypes.getCaptionLabel(I_.get("To declare")));
143  gl.addSpace();
144  gl.addRow();
145  gl.addCaption(I_.get("Tax"));
146  gl.addComponent(LabelTypes.getCaptionLabel(currencyFormatter.format(ve.getToDeclareAmountVat())));
147  gl.addRow();
148  gl.addCaption(I_.get("Taxable"));
149  gl.addComponent(LabelTypes.getCaptionLabel(currencyFormatter.format(ve.getToDeclareAmountOp())));
150  }
151  return gl;
152  }
153 
154  private HtmlBasedComponent getBreakdownCell(final EntryQuarter eq) {
155  Toolbarbutton breakdown = new Toolbarbutton(I_.get("Breakdown"), Images.getImage("breakdown-open"));
156  breakdown.addEventListener(Events.ON_CLICK, new EventListener() {
157  @Override
158  public void onEvent(Event event) throws Exception {
159  SelectionDialog.showComponent(getPage(), I_.get("Breakdown"),
160  new Model349List(eq), "750px", "70%", null);
161  }
162  });
163  return breakdown;
164  }
165 
166  private HtmlBasedComponent getTaxableCell(final EntryQuarter eq) {
167  boolean notDeclared = Zero.near(modelSet.getDeclaredQuarter(eq.getQuarter() - 1).getAmountDeclared(), 2);
168  GridLayout gl = new GridLayout("100%,right-160px");
169  gl.addCaption(I_.get("Total"));
170  gl.addComponent(new Label(currencyFormatter.format(eq.getTotalOp())));
171  if(notDeclared) {
172  gl.addRow();
173  gl.addComponent(LabelTypes.getCaptionLabel(I_.get("To declare")));
174  gl.addSpace();
175  gl.addRow();
176  gl.addCaption(I_.get("Tax"));
177  gl.addComponent(LabelTypes.getCaptionLabel(currencyFormatter.format(eq.getToDeclareTotalVat())));
178  gl.addRow();
179  gl.addCaption(I_.get("Taxable"));
180  gl.addComponent(LabelTypes.getCaptionLabel(currencyFormatter.format(eq.getToDeclareTotalOp())));
181  }
182  gl.addRow();
183  gl.addComponent(getBreakdownCell(eq));
184  gl.addComponent(LabelTypes.getTinyLabel(currencyFormatter.format(eq.getAmountOp())));
185  return gl;
186  }
187 
188  private HtmlBasedComponent getTotalCell(final int quarter) {
189  final double inputVat = modelSet.getTotalInputVat(quarter),
190  outputVat = modelSet.getTotalOutputVat(quarter);
191  GridLayout gl = new GridLayout("100%,right-160px");
192  gl.addCaption(I_.get("Input VAT"));
193  gl.addComponent(new Label(currencyFormatter.format(inputVat)));
194  gl.addRow();
195  gl.addCaption(I_.get("Output VAT"));
196  gl.addComponent(new Label(currencyFormatter.format(outputVat)));
197  final double declared = modelSet.getDeclaredQuarter(quarter).getAmountDeclared();
198  gl.addRow();
199  gl.addCaption(I_.get("Declared"));
200  gl.addComponent(new AlertLabel(currencyFormatter.format(Math.abs(declared))) {
201  @Override
202  protected int getType() {
203  if(Zero.near((outputVat - inputVat) - declared, 2)) {
204  return AlertLabel.ALERT_RIGHT;
205  } else {
206  return AlertLabel.ALERT_WRONG;
207  }
208  }
209  });
210  if(Zero.near(declared, 2)) {
211  gl.addRow();
212  Button b = new Button(I_.get("Register"));
213  b.addEventListener(Events.ON_CLICK, new EventListener() {
214  @Override
215  public void onEvent(Event event) throws Exception {
216  modelSet.generateRegister(quarter);
217  }
218  });
219  gl.addComponent(b);
220  gl.addComponent(new Label(currencyFormatter.format(Math.abs(outputVat - inputVat))));
221  }
222  return gl;
223  }
224 
225 }
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)