18 package org.turro.financials.entity;
21 import javax.persistence.*;
22 import org.turro.string.Strings;
23 import org.turro.elephant.db.IdUtils;
24 import org.turro.elephant.db.WhereClause;
25 import org.turro.fieldit.FieldItSet;
26 import org.turro.fieldit.FieldItUtil;
27 import org.turro.fieldit.IValueItEntity;
28 import org.turro.fieldit.ValueItSet;
29 import org.turro.financials.db.FinancialsPU;
30 import org.turro.financials.product.IProduct;
31 import org.turro.financials.product.Movement;
32 import org.turro.groupit.GroupItUtil;
33 import org.turro.jpa.Dao;
34 import org.turro.jpa.entity.IDaoEntity;
35 import org.turro.math.Round;
36 import org.turro.reflection.MappingSet;
37 import org.turro.util.Chars;
47 @GeneratedValue(strategy=GenerationType.IDENTITY)
48 @Column(name=
"IDENTIFIER")
51 private String productCode, description, keywords;
53 private double price, tax;
55 private boolean publishable, service, promoted, delivery, concept, attachments;
57 @OneToMany(mappedBy =
"product", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=
true)
58 private Set<ProductByContractor> productByContractors =
new HashSet<>();
65 this.description = description;
81 this.keywords = keywords;
93 return productByContractors;
97 this.productByContractors = productByContractors;
105 this.productCode = productCode;
121 this.publishable = publishable;
129 this.service = service;
137 this.promoted = promoted;
145 this.delivery = delivery;
153 this.concept = concept;
161 this.attachments = attachments;
173 return Strings.isBlank(description);
178 if(Strings.isBlank(productCode)) {
187 (Strings.isBlank(keywords) ?
"" :
" " + keywords) +
193 return Strings.isBlank(extra) ?
"" :
" " + extra;
197 double taxable =
new Round(price).
decimals(fractions).value();
198 return new Round((taxable * (1.0 + tax / 100.0))).
decimals(fractions).value();
204 private transient Double lastCost =
null, lastCostTax =
null,
205 lastPrice =
null, lastPriceTax =
null;
209 return productByContractor;
214 this.productByContractor = productByContractor;
229 if(lastCost ==
null) {
230 Object d[] = loadData(1);
231 lastCost = (Double) d[0];
232 lastCostTax = (Double) d[1];
239 if(lastCost ==
null) {
240 Object d[] = loadData(1);
241 lastCost = (Double) d[0];
242 lastCostTax = (Double) d[1];
250 if(lastPrice ==
null) {
251 Object d[] = loadData(-1);
252 lastPrice = (Double) d[0];
253 lastPriceTax = (Double) d[1];
263 if(lastPrice ==
null) {
264 Object d[] = loadData(-1);
265 lastPrice = (Double) d[0];
266 lastPriceTax = (Double) d[1];
275 throw new UnsupportedOperationException(
"Not supported yet.");
307 if(productByContractor !=
null &&
312 return "#" + productCode + Chars.forward().spaced() +
getDescription(
true);
324 wc.
addClause(
"select new org.turro.financials.product.Movement");
325 wc.
addClause(
"(dl.document.contract, dl.document.documentDate,");
326 wc.
addClause(
"dl.lineType.stockCoefficient,dl.quantity,dl.price,dl.concept)");
328 wc.
addClause(
"where (dl.product = :product");
330 wc.
addClause(
"or dl.productByContractor = :pbc)");
333 wc.
addClause(
"order by dl.document.receiptDate desc, dl.lineOrder");
340 wc.
addClause(
"select new org.turro.financials.product.Movement");
341 wc.
addClause(
"('Accumulated',coalesce(sum(dl.quantity*dl.lineType.stockCoefficient),0.0),coalesce(sum(dl.quantity*dl.price*dl.lineType.stockCoefficient),0.0))");
343 wc.
addClause(
"where (dl.product = :product");
345 wc.
addClause(
"or dl.productByContractor = :pbc)");
352 wc.
addClause(
"and dl.document.receiptDate < :from2");
354 Collection<Movement> movs =
new FinancialsPU().getResultList(wc);
356 wc.
addClause(
"select new org.turro.financials.product.Movement");
357 wc.
addClause(
"(dl.document.contract, dl.document.documentDate,");
358 wc.
addClause(
"dl.lineType.stockCoefficient,dl.quantity,dl.price,dl.concept)");
360 wc.
addClause(
"where (dl.product = :product");
362 wc.
addClause(
"or dl.productByContractor = :pbc)");
369 wc.
addClause(
"and dl.document.receiptDate >= :from2");
372 wc.
addClause(
"and dl.document.receiptDate <= :to2");
375 wc.
addClause(
"order by dl.document.receiptDate asc, dl.lineOrder");
382 wc.
addClause(
"select pr from ProductRegulation pr");
383 wc.
addClause(
"where pr.product = :product");
389 wc.
addClause(
"and pr.regulationDate = ");
390 wc.
addClause(
"(select max(pr2.regulationDate) from ProductRegulation pr2");
391 wc.
addClause(
"where pr2.product = pr.product");
392 wc.
addClause(
"and pr2.regulationDate <= :date");
394 wc.
addClause(
"and pr2.store = pr.store");
402 double stock = pr ==
null ? 0.0 : pr.
getQuantity();
404 wc.
addClause(
"select coalesce(sum(dl.quantity*dl.lineType.stockCoefficient),0.0)");
406 wc.
addClause(
"where dl.product = :product");
411 wc.
addClause(
"and dl.document.receiptDate <= :date");
414 wc.
addClause(
"and dl.document.receiptDate >= :from");
418 stock += result ==
null ? 0.0 : result;
421 wc.
addClause(
"from ProductRegulation pr");
422 wc.
addClause(
"where pr.product = :product");
428 wc.
addClause(
"and pr.regulationDate <= :date");
431 wc.
addClause(
"and pr.regulationDate >= :from");
435 stock += result ==
null ? 0.0 : result;
445 wc.
addClause(
"select prod from Product prod where productCode = :code");
450 public static List<Product>
getProducts(String concept,
boolean equality) {
453 getDescriptionQuery(wc, concept, equality);
454 List<Product> list =
new FinancialsPU().getResultList(wc);
458 private static void getDescriptionQuery(
WhereClause wc, String description,
boolean equality) {
460 if(!Strings.isBlank(description)) {
463 wc.
addClause(
"and prod.description = :desc");
466 wc.
addLikeFields(
new String[] {
"prod.description" }, description);
471 wc.
addClause(
"order by prod.description");
474 private Object[] loadData(
double coefficient) {
475 WhereClause wc =
new WhereClause();
476 wc.addClause(
"select dl.price, dl.tax");
477 wc.addClause(
"from DocumentLine dl");
478 wc.addClause(
"where dl.product = :product");
479 wc.addNamedValue(
"product",
this);
480 wc.addClause(
"or dl.productByContractor = :pbc");
481 wc.addNamedValue(
"pbc", productByContractor);
482 wc.addClause(
"and dl.lineType.stockCoefficient = :coef");
483 wc.addNamedValue(
"coef", coefficient);
484 wc.addClause(
"and dl.quantity <> 0");
485 wc.addClause(
"order by dl.document.receiptDate desc, dl.lineOrder");
486 List l =
new FinancialsPU().getResultList(wc, 1);
489 d = (Object[]) l.get(0);
491 d =
new Double[] { 0.0, 0.0 };
516 return new ArrayList<>();
522 MappingSet
set =
new MappingSet();
523 set.addMapping(
Product.class, 1,
524 new String[] {
"id",
"productCode",
"description",
"keywords",
"price",
"tax",
525 "publishable",
"service",
"promoted",
"delivery",
"concept",
"attachments" },
526 new String[] {
"productByContractors" });
528 new String[] {
"contractorCode",
"price",
"tax" },
529 new String[] {
"contract" });
531 new String[] {
"id",
"name" },
static long getNewLongIdFromString(Dao dao, String table, String field)
void addClause(String clause)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)
static Collection< String > related(String path)
static Object getFieldValue(String label, String entityPath)
String getDescriptionFields()
static String getObjectPath(Object object)
boolean isUseContractorCode()
String getContractorCode()
void setConcept(boolean concept)
Collection< Movement > getMovements()
ProductRegulation getLastInventory(Dao dao, Contract store, Date date)
Collection< Movement > getMovements(Date from, Date to, Contract store)
static List< Product > getProducts(String concept, boolean equality)
double getProductStock(Date date, Contract store)
Set< ProductByContractor > getProductByContractors()
void setAttachments(boolean attachments)
Object getFieldValue(String label)
void setPromoted(boolean promoted)
double getProductPriceTax()
void setProductByContractor(ProductByContractor productByContractor)
String getDescription(boolean fields)
double getPriceWithTax(int fractions)
void setPublishable(boolean publishable)
MappingSet getSerializerMappings()
String getProductString()
void setService(boolean service)
ProductByContractor getProductByContractor()
double getProductCostTax()
String getProductCodeStr()
void setProductByContractors(Set< ProductByContractor > productByContractors)
void setProductCode(String productCode)
static List< Product > getProducts()
String getFieldDescription()
void setDescription(String description)
Collection< IValueItEntity > getRelated()
void setKeywords(String keywords)
void setDelivery(boolean delivery)
static Product getProductByCode(String code)
double getStock(Contract store, Date date)
void setPrice(double price)
static ValueItSet getValues(String entityPath)
static FieldItSet getFields(String entityPath)
Object getSingleResultOrNull(SqlClause sc)
Round decimals(int digits)