BrightSide Workbench Full Report + Source Code
ProductUncoded.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.product;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
24 import org.turro.string.Strings;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.financials.db.FinancialsPU;
27 import org.turro.financials.entity.Contract;
28 import org.turro.financials.entity.ProductByContractor;
29 
34 public class ProductUncoded implements IProduct {
35 
36  private String description = null;
37  private Double cost = null, costTax = null, price = null, priceTax = null;
38 
39  private transient ProductByContractor productByContractor;
40 
41  public ProductUncoded(String description) {
42  this.description = description;
43  }
44 
45  public ProductUncoded(String description, ProductByContractor productByContractor) {
46  this.description = description;
47  this.productByContractor = productByContractor;
48  }
49 
50  @Override
52  return productByContractor;
53  }
54 
55  @Override
56  public void setProductByContractor(ProductByContractor productByContractor) {
57  this.productByContractor = productByContractor;
58  }
59 
60  @Override
61  public double getProductCost() {
62  if(cost == null) {
63  loadCostData();
64  }
65  return cost;
66  }
67 
68  @Override
69  public double getProductCostTax() {
70  return costTax;
71  }
72 
73  @Override
74  public String getProductName() {
75  return description;
76  }
77 
78  @Override
79  public long getProductId() {
80  return -1;
81  }
82 
83  @Override
84  public String getProductCodeStr() {
85  return description;
86  }
87 
88  @Override
89  public double getProductPrice() {
90  if(price == null) {
91  loadPriceData();
92  }
93  return price;
94  }
95 
96  @Override
97  public double getProductPriceTax() {
98  return priceTax;
99  }
100 
101  @Override
102  public IProduct load(Object id) {
103  this.description = (String) id;
104  this.cost = null;
105  this.costTax = null;
106  this.price = null;
107  this.priceTax = null;
108  return this;
109  }
110 
111  @Override
112  public double getProductStock(Date date, Contract store) {
113  WhereClause wc = new WhereClause();
114  wc.addClause("select sum(quantity) ");
115  wc.addClause("from DocumentLine dl");
116  wc.addClause("where dl.document.receiptDate = :date");
117  wc.addNamedValue("date", date);
118  if(store != null) {
119  wc.addClause("and dl.store = :store");
120  wc.addNamedValue("store", store);
121  }
122  wc.addClause("and dl.product is null");
123  wc.addClause("and dl.productByContractor is null");
124  wc.addClause("and dl.lineType.stockCoefficient <> 0");
125  wc.addClause("and dl.quantity <> 0");
126  wc.addClause("and dl.concept = :concept");
127  wc.addNamedValue("concept", description);
128  return (Double) new FinancialsPU().getSingleResultOrNull(wc);
129  }
130 
131  @Override
132  public String getProductString() {
133  return description;
134  }
135 
136  @Override
137  public boolean isUncoded() {
138  return true;
139  }
140 
141  @Override
142  public Collection<Movement> getMovements() {
143  WhereClause wc = new WhereClause();
144  wc.addClause("select new org.turro.financials.product.Movement");
145  wc.addClause("(dl.document.contract, dl.document.documentDate,");
146  wc.addClause("dl.lineType.stockCoefficient,dl.quantity,dl.price,dl.concept)");
147  wc.addClause("from DocumentLine dl");
148  wc.addClause("where dl.product is null");
149  wc.addClause("and dl.productByContractor is null");
150  wc.addClause("and dl.concept = :concept");
151  wc.addNamedValue("concept", description);
152  wc.addClause("and dl.quantity <> 0");
153  wc.addClause("order by dl.document.receiptDate desc, dl.lineOrder");
154  return new FinancialsPU().getResultList(wc, 100);
155  }
156 
157  @Override
158  public Collection<Movement> getMovements(Date from, Date to, Contract store) {
159  WhereClause wc = new WhereClause();
160  wc.addClause("select new org.turro.financials.product.Movement");
161  wc.addClause("('Accumulated',coalesce(sum(dl.quantity*dl.lineType.stockCoefficient),0.0),coalesce(sum(dl.quantity*dl.price*dl.lineType.stockCoefficient),0.0))");
162  wc.addClause("from DocumentLine dl");
163  wc.addClause("where dl.product is null");
164  if(store != null) {
165  wc.addClause("and dl.store = :store");
166  wc.addNamedValue("store", store);
167  }
168  wc.addClause("and dl.productByContractor is null");
169  wc.addClause("and dl.concept = :concept");
170  wc.addNamedValue("concept", description);
171  wc.addClause("and dl.quantity <> 0");
172  wc.addClause("and dl.document.receiptDate < :from2");
173  wc.addNamedValue("from2", from);
174  Collection<Movement> movs = new FinancialsPU().getResultList(wc);
175  wc = new WhereClause();
176  wc.addClause("select new org.turro.financials.product.Movement");
177  wc.addClause("(dl.document.contract, dl.document.documentDate,");
178  wc.addClause("dl.lineType.stockCoefficient,dl.quantity,dl.price,dl.concept)");
179  wc.addClause("from DocumentLine dl");
180  wc.addClause("where dl.product is null");
181  if(store != null) {
182  wc.addClause("and dl.store = :store");
183  wc.addNamedValue("store", store);
184  }
185  wc.addClause("and dl.productByContractor is null");
186  wc.addClause("and dl.concept = :concept");
187  wc.addNamedValue("concept", description);
188  wc.addClause("and dl.quantity <> 0");
189  wc.addClause("and dl.document.receiptDate >= :from2");
190  wc.addNamedValue("from2", from);
191  if(to != null) {
192  wc.addClause("and dl.document.receiptDate <= :to2");
193  wc.addNamedValue("to2", to);
194  }
195  wc.addClause("order by dl.document.receiptDate asc, dl.lineOrder");
196  movs.addAll(new FinancialsPU().getResultList(wc));
197  return movs;
198  }
199 
200  private void loadCostData() {
201  Object d[] = loadData(1);
202  cost = (Double) d[0];
203  costTax = (Double) d[1];
204  }
205 
206  private void loadPriceData() {
207  Object d[] = loadData(-1);
208  price = (Double) d[0];
209  priceTax = (Double) d[1];
210  }
211 
212  private Object[] loadData(double coefficient) {
213  WhereClause wc = new WhereClause();
214  wc.addClause("select dl.price, dl.tax");
215  wc.addClause("from DocumentLine dl");
216  wc.addClause("where dl.product is null");
217  wc.addClause("and dl.productByContractor is null");
218  wc.addClause("and dl.lineType.stockCoefficient = :coef");
219  wc.addNamedValue("coef", coefficient);
220  wc.addClause("and dl.concept = :concept");
221  wc.addNamedValue("concept", description);
222  wc.addClause("and dl.quantity <> 0");
223  wc.addClause("order by dl.document.receiptDate desc, dl.lineOrder");
224  List l = new FinancialsPU().getResultList(wc, 1);
225  Object d[];
226  if(!l.isEmpty()) {
227  d = (Object[]) l.get(0);
228  } else {
229  d = new Double[] { 0.0, 0.0 };
230  }
231  return d;
232  }
233 
234  public static List<ProductUncoded> getProducts() {
235  return getProducts(null, false);
236  }
237 
238  public static List<ProductUncoded> getProducts(String concept, boolean equality) {
239  List<ProductUncoded> l = new ArrayList<ProductUncoded>();
240  for(String cp : getConcepts(concept, equality)) {
241  l.add(new ProductUncoded(cp));
242  }
243  return l;
244  }
245 
246  public static List<String> getConcepts(String concept, boolean equality) {
247  WhereClause wc = new WhereClause();
248  wc.addClause("select distinct dl.concept");
249  getConceptsQuery(wc, concept, equality);
250  List<String> list = new FinancialsPU().getResultList(wc);
251  return list;
252  }
253 
254  private static void getConceptsQuery(WhereClause wc, String concept, boolean equality) {
255  wc.addClause("from DocumentLine dl");
256  wc.addClause("where dl.product is null");
257  wc.addClause("and dl.productByContractor is null");
258  wc.addClause("and dl.lineType.stockCoefficient <> 0");
259  wc.addClause("and dl.price > 0");
260  if(!Strings.isBlank(concept)) {
261  if(equality) {
262  wc.addClause("and dl.concept = :concept");
263  wc.addNamedValue("concept", concept);
264  } else {
265  wc.addLikeFields(new String[] { "dl.concept" }, concept);
266  }
267  } else {
268  wc.addClause("and dl.concept is not null");
269  wc.addClause("and dl.concept <> ''");
270  }
271  wc.addClause("order by dl.concept");
272  }
273 
274 }
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)
static List< ProductUncoded > getProducts()
static List< String > getConcepts(String concept, boolean equality)
void setProductByContractor(ProductByContractor productByContractor)
ProductUncoded(String description, ProductByContractor productByContractor)
double getProductStock(Date date, Contract store)
Collection< Movement > getMovements(Date from, Date to, Contract store)
static List< ProductUncoded > getProducts(String concept, boolean equality)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419