BrightSide Workbench Full Report + Source Code
DocumentFilterGrid.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.filter;
19 
20 import org.turro.elephant.db.WhereClause;
21 import org.turro.financials.book.BookListbox;
22 import org.turro.financials.contract.ContractCombobox;
23 import org.turro.financials.contract.ContractDefinitionCombobox;
24 import org.turro.financials.document.DocumentDefinitionCombobox;
25 import org.turro.financials.view.ViewListbox;
26 import org.turro.zkoss.filter.FilterField;
27 import org.turro.zkoss.filter.FilterFieldOperator;
28 import org.turro.zkoss.filter.FilterGrid;
29 import org.zkoss.zk.ui.HtmlBasedComponent;
30 
35 public class DocumentFilterGrid extends FilterGrid {
36 
37  public DocumentFilterGrid() {
38  super();
40  @Override
41  public void addConstraint(WhereClause wc) {
42  doAddConstraint(wc, "doc.documentNumber");
43  }
44  });
45  addField(new FilterField("Internal ID", 0L, FilterFieldOperator.FILTER_EQUAL) {
46  @Override
47  public void addConstraint(WhereClause wc) {
48  doAddConstraint(wc, "doc.id");
49  }
50  });
51  addField(new FilterField("Date", new java.util.Date(new java.util.Date().getTime() - (1L * 30L * 24L * 60L * 60L * 1000L))) {
52  @Override
53  public void addConstraint(WhereClause wc) {
54  doAddConstraint(wc, "doc.documentDate");
55  }
56  });
57  addField(new FilterField("Receipt/Issue", new java.util.Date(new java.util.Date().getTime() - (1L * 30L * 24L * 60L * 60L * 1000L))) {
58  @Override
59  public void addConstraint(WhereClause wc) {
60  doAddConstraint(wc, "doc.receiptDate");
61  }
62  });
63  addField(new FilterField("Contract") {
64  @Override
65  protected HtmlBasedComponent createEditor() {
66  return new ContractCombobox();
67  }
68  @Override
69  public void addConstraint(WhereClause wc) {
70  doAddConstraint(wc, "doc.contract");
71  }
72  });
73  addField(new FilterField("View") {
74  @Override
75  protected HtmlBasedComponent createEditor() {
76  ViewListbox vl = new ViewListbox();
77  vl.setMold("select");
78  vl.setAllowNull(true);
79  return vl;
80  }
81  @Override
82  public void addConstraint(WhereClause wc) {
83  doAddConstraint(wc, "ifnull(doc.forcedView, 0)");
84  }
85  });
86  addField(new FilterField("Contract model") {
87  @Override
88  protected HtmlBasedComponent createEditor() {
89  return new ContractDefinitionCombobox();
90  }
91  @Override
92  public void addConstraint(WhereClause wc) {
93  doAddConstraint(wc, "doc.contract.contractDefinition");
94  }
95  });
96  addField(new FilterField("Document model") {
97  @Override
98  protected HtmlBasedComponent createEditor() {
99  return new DocumentDefinitionCombobox();
100  }
101  @Override
102  public void addConstraint(WhereClause wc) {
103  doAddConstraint(wc, "doc.documentDefinition");
104  }
105  });
106  addField(new FilterField("Draft", true) {
107  @Override
108  public void addConstraint(WhereClause wc) {
109  doAddConstraint(wc, "doc.draft");
110  }
111  });
112  addField(new FilterField("Amount", 0.0) {
113  @Override
114  public void addConstraint(WhereClause wc) {
115  //doAddSubqueryConstraint(wc, "doc.documentLines", "price");
116  if(FilterFieldOperator.FILTER_EQUAL.equals(getOperator())) {
117  wc.addClause(getChain().getOpSQL() + " (" + getObjectValue() + " <= ");
118  wc.addClause("(");
119  wc.addClause("select sum("+ getAmount() +") + 0.05 from doc.documentLines as dl");
120  wc.addClause(")");
121  wc.addClause("and " + getObjectValue() + " >= ");
122  wc.addClause("(");
123  wc.addClause("select sum(" + getAmount() + ") - 0.05 from doc.documentLines as dl");
124  wc.addClause("))");
125  } else if(FilterFieldOperator.FILTER_BIG.equals(getOperator())) {
126  wc.addClause(getChain().getOpSQL() + " (" + getObjectValue() + " < ");
127  wc.addClause("(");
128  wc.addClause("select sum("+ getAmount() +") from doc.documentLines as dl");
129  wc.addClause("))");
130  } else if(FilterFieldOperator.FILTER_BIG_OR_EQUAL.equals(getOperator())) {
131  wc.addClause(getChain().getOpSQL() + " (" + getObjectValue() + " <= ");
132  wc.addClause("(");
133  wc.addClause("select sum("+ getAmount() +") from doc.documentLines as dl");
134  wc.addClause("))");
135  } else if(FilterFieldOperator.FILTER_LESS.equals(getOperator())) {
136  wc.addClause(getChain().getOpSQL() + " (" + getObjectValue() + " > ");
137  wc.addClause("(");
138  wc.addClause("select sum("+ getAmount() +") from doc.documentLines as dl");
139  wc.addClause("))");
140  } else if(FilterFieldOperator.FILTER_LESS_OR_EQUAL.equals(getOperator())) {
141  wc.addClause(getChain().getOpSQL() + " (" + getObjectValue() + " >= ");
142  wc.addClause("(");
143  wc.addClause("select sum("+ getAmount() +") from doc.documentLines as dl");
144  wc.addClause("))");
145  } else if(FilterFieldOperator.FILTER_NOT_EQUAL.equals(getOperator())) {
146  wc.addClause(getChain().getOpSQL() + " (" + getObjectValue() + " >= ");
147  wc.addClause("(");
148  wc.addClause("select sum("+ getAmount() +") from doc.documentLines as dl");
149  wc.addClause(")");
150  wc.addClause("and " + getObjectValue() + " <= ");
151  wc.addClause("(");
152  wc.addClause("select sum(" + getAmount() + ") from doc.documentLines as dl");
153  wc.addClause("))");
154  }
155  }
156  });
157  addField(new FilterField("Quantity", 0.0) {
158  @Override
159  public void addConstraint(WhereClause wc) {
160  doAddSubqueryConstraint(wc, "doc.documentLines", "quantity");
161  }
162  });
163  addField(new FilterField("Store") {
164  @Override
165  protected HtmlBasedComponent createEditor() {
166  return new ContractCombobox();
167  }
168  @Override
169  public void addConstraint(WhereClause wc) {
170  doAddSubqueryConstraint(wc, "doc.documentLines", "store");
171  }
172  });
173  addField(new FilterField("Concept", "") {
174  @Override
175  public void addConstraint(WhereClause wc) {
176  doAddSubqueryConstraint(wc, "doc.documentLines", "concept");
177  }
178  });
179  addField(new FilterField("Book") {
180  @Override
181  protected HtmlBasedComponent createEditor() {
182  BookListbox bl = new BookListbox();
183  bl.setMold("select");
184  bl.setAllowNull(true);
185  return bl;
186  }
187  @Override
188  public void addConstraint(WhereClause wc) {
189  doAddSubqueryConstraint(wc, new String[] {"doc.registers", "sqx.bookRegisters"}, "bookDefinition");
190  }
191  });
192  addField(new FilterField("Book entry", 0L) {
193  @Override
194  public void addConstraint(WhereClause wc) {
195  doAddSubqueryConstraint(wc, new String[] {"doc.registers", "sqx.bookRegisters"}, "bookOrder");
196  }
197  });
198  if(!loadPreferences(this.getClass().getName()+"_filter")) {
199  addCurrentField("Number");
200  }
201  }
202 
203  public String getIfNull(String field, String value) {
204  return "case " + field + " when 0.0 then " + value + " else ifnull(" + field + "," + value + ") end";
205  }
206 
207  public String getSubtotal() {
208  return "(" + getIfNull("dl.quantity", "1.0") + " * dl.price)";
209  }
210 
211  public String getRetained() {
212  return "((" + getSubtotal() + " - " + getDiscount() + ") * (" + getIfNull("dl.retention", "0.0") + " / 100.0))";
213  }
214 
215  public String getDiscount() {
216  return "((" + getSubtotal() + " - dl.discountMoney) * (" + getIfNull("dl.discountPerCent", "0.0") + " / 100.0) + dl.discountMoney)";
217  }
218 
219  public String getTaxable() {
220  return "(" + getSubtotal() + " - " + getDiscount() + ")";
221  }
222 
223  public String getTaxValue() {
224  return "(" + getAmount() + " + " + getRetained() + ") - " + getTaxable();
225  }
226 
227  public String getAmount() {
228  return "(" + getTaxable() + "* (1.0 + (dl.tax / 100))) - " + getRetained();
229  }
230 
231 
232 }
FilterField addField(FilterField filterField)
Definition: FilterGrid.java:62