Commit 488fc14f by Tuomas Riihimäki

Add product dependecy possibility

This allows products do depend on other products, ie you must buy
computerplace to buy vip lounge entrances
1 parent b70320be
......@@ -424,20 +424,24 @@ public class BootstrapBean implements BootstrapBeanLocal {
dbUpdates.add(new String[] {
"ALTER TABLE discounts ADD COLUMN sort INTEGER NOT NULL default 10;"
});
dbUpdates.add(new String[] {
"CREATE TABLE map_queue_rules (id SERIAL NOT NULL, meta json, reserving_size INTEGER, default_timeout_min INTEGER, minium_slots_in_queue INTEGER, map_id INTEGER NOT NULL , PRIMARY KEY (id))",
"ALTER TABLE map_queue_rules ADD CONSTRAINT FK_map_queue_rules_map_id FOREIGN KEY (map_id) REFERENCES maps (id)",
});
dbUpdates.add(new String[] {
dbUpdates.add(new String[] {
"ALTER TABLE map_queue_rules ADD COLUMN biggest_first BOOLEAN default false",
});
dbUpdates.add(new String[]{
"CREATE TABLE product_dependencies (id SERIAL NOT NULL, dependency_type TEXT NOT NULL, meta json, multiplier DECIMAL(24,6) NOT NULL, priority INTEGER NOT NULL, dependant_id INTEGER NOT NULL, supporter_id INTEGER NOT NULL, PRIMARY KEY (id))",
"ALTER TABLE product_dependencies ADD CONSTRAINT FK_product_dependencies_dependant_id FOREIGN KEY (dependant_id) REFERENCES products (id)",
"ALTER TABLE product_dependencies ADD CONSTRAINT FK_product_dependencies_supporter_id FOREIGN KEY (supporter_id) REFERENCES products (id)",
});
}
}
public BootstrapBean() {
}
......
......@@ -23,6 +23,7 @@
package fi.codecrew.moya.model;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -48,7 +49,7 @@ import javax.persistence.UniqueConstraint;
import org.eclipse.persistence.annotations.PrivateOwned;
/**
*
*
*/
@Entity
@Table(name = "products")
......@@ -151,6 +152,9 @@ public class Product extends GenericEntity {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "product")
private List<ProductOptionGroup> productOptionGroups;
@OneToMany(cascade = CascadeType.ALL, mappedBy ="dependant")
@OrderBy("priority")
private List<ProductDependency> productDependencies = new ArrayList<>();
public Product() {
super();
......@@ -237,7 +241,7 @@ public class Product extends GenericEntity {
/**
* Get product price, includes vat
*
*
* @return
*/
public BigDecimal getPrice() {
......@@ -246,7 +250,7 @@ public class Product extends GenericEntity {
/**
* Set price, including vat
*
*
* @param price
*/
public void setPrice(BigDecimal price) {
......@@ -303,7 +307,7 @@ public class Product extends GenericEntity {
/**
* Set product vat-%, value between 0 and 1
*
*
* @param vat
*/
public void setVat(BigDecimal vat) {
......@@ -312,7 +316,7 @@ public class Product extends GenericEntity {
/**
* Get product vat-%, value between 0 and 1
*
*
* @return
*/
public BigDecimal getVat() {
......@@ -436,4 +440,12 @@ public class Product extends GenericEntity {
public void setMinBuyCount(int minBuyCount) {
this.minBuyCount = minBuyCount;
}
public List<ProductDependency> getProductDependencies() {
return productDependencies;
}
public void setProductDependencies(List<ProductDependency> productDependencies) {
this.productDependencies = productDependencies;
}
}
package fi.codecrew.moya.model;
import javax.persistence.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
@Entity
@Table(name = "product_dependencies")
public class ProductDependency extends GenericEntity {
public static final String DEPENDANT_COLUMN = "dependant_id" ;
private static final BigDecimal SCALE6_ONE = BigDecimal.ONE.setScale(6, RoundingMode.HALF_UP);
public static enum DependencyType {
// Dependant product can be bought maximum of supported product number of times
MAX_SUPPORTED_COUNT,
}
@Enumerated(EnumType.STRING)
@Column(nullable = false, name="dependency_type")
private DependencyType dependencyType;
// Order, in which dependencies will be parsed
@Column(nullable = false, name="priority")
private int priority = 1000;
@Column(name = "multiplier", nullable = false, precision = 24, scale = 6)
private BigDecimal multiplier = SCALE6_ONE;
@ManyToOne
@JoinColumn(nullable = false, name=DEPENDANT_COLUMN)
private Product dependant;
// product which is required when purhchasing the dependant product
@ManyToOne
@JoinColumn(nullable = false, name="supporter_id")
private Product supporter;
public DependencyType getDependencyType() {
return dependencyType;
}
public void setDependencyType(DependencyType dependencyType) {
this.dependencyType = dependencyType;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public Product getDependant() {
return dependant;
}
public void setDependant(Product dependant) {
this.dependant = dependant;
}
public Product getSupporter() {
return supporter;
}
public void setSupporter(Product supporter) {
this.supporter = supporter;
}
public BigDecimal getMultiplier() {
return multiplier;
}
public void setMultiplier(BigDecimal multiplier) {
this.multiplier = multiplier;
}
}
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:products="http://java.sun.com/jsf/composite/cditools/products" xmlns:p="http://primefaces.org/ui">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:products="http://java.sun.com/jsf/composite/cditools/products"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title></title>
......@@ -13,18 +15,62 @@
<f:viewParam name="productid" value="#{productView.productId}" />
<f:event type="preRenderView" listener="#{productView.initEditView}" />
</f:metadata>
<ui:param name="thispage" value="page.product.edit" />
<ui:define name="content">
<products:edit commitaction="#{productView.saveProduct()}" commitvalue="#{i18n['products.save']}" />
<hr/>
<h:form id="newdep">
<h:selectOneMenu id="supporter" layout="pageDirection" value="#{productView.dependency.supporter}"
converter="#{productConverter}">
<f:selectItem itemLabel="---" />
<f:selectItems var="prod" itemLabel="#{prod.name}" value="#{productView.products}"/>
</h:selectOneMenu>
<h:selectOneMenu id="type" layout="pageDirection" value="#{productView.dependency.dependencyType}">
<f:selectItems var="type" value="#{productView.dependencyTypes}" />
</h:selectOneMenu>
<p:commandButton action="#{productView.createProductDependency()}"
value="#{i18n['product.dependency.create']}" />
</h:form>
<h:form id="dependencies">
<h:dataTable border="1" var="dependency" value="#{productView.product.productDependencies}"
rendered="#{!empty productView.product.productDependencies}">
<h:column>
<f:facet name="header">
<h:outputText value="#{i18n['product.dependency.supporter']}"/>
</f:facet>
<h:outputText value="#{dependency.supporter.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{i18n['product.dependency.type']}"/>
</f:facet>
<h:outputText value="#{dependency.dependencyType}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{i18n['product.dependency.priority']}"/>
</f:facet>
<h:outputText value="#{dependency.priority}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{i18n['product.dependency.multiplier']}"/>
</f:facet>
<h:outputText value="#{dependency.multiplier}"/>
</h:column>
</h:dataTable>
</h:form>
<hr />
<h:form id="discounts">
<h:commandButton rendered="#{!empty productView.product.id}" action="#{productView.initCreateDiscount()}" value="#{i18n['product.createDiscount']}">
<h:commandButton rendered="#{!empty productView.product.id}"
action="#{productView.initCreateDiscount()}" value="#{i18n['product.createDiscount']}">
</h:commandButton>
<h:dataTable border="1" id="discount" value="#{productView.productDiscounts}" var="discount" rendered="#{!empty productView.product.id and !empty productView.product.discounts}">
<h:dataTable border="1" id="discount" value="#{productView.productDiscounts}" var="discount"
rendered="#{!empty productView.product.id and !empty productView.product.discounts}">
<h:column>
<f:facet name="header">
<h:outputText value="${i18n['discount.percentage']}" />
......@@ -79,11 +125,14 @@
</h:dataTable>
</h:form>
<hr />
<h:form id="limits">
<h:commandButton rendered="#{!empty productView.product.id}" action="#{productView.initCreateLimit()}" value="#{i18n['product.createLimit']}">
<h:commandButton rendered="#{!empty productView.product.id}" action="#{productView.initCreateLimit()}"
value="#{i18n['product.createLimit']}">
</h:commandButton>
<p:dataTable border="1" id="limit" value="#{productView.productLimits}" var="limit" rendered="#{!empty productView.product.id and !empty productView.product.productLimits}">
<p:dataTable border="1" id="limit" value="#{productView.productLimits}" var="limit"
rendered="#{!empty productView.product.id and !empty productView.product.productLimits}">
<p:column rowHeader="#{i18n['productLimit.name']}">
<h:outputText value="#{limit.name}" />
</p:column>
......@@ -94,7 +143,8 @@
<h:outputText value="#{limit.lowerLimit}" />
</p:column>
<p:column>
<p:commandButton ajax="false" action="#{productView.editLimit()}" value="#{i18n['productLimit.edit']}" />
<p:commandButton ajax="false" action="#{productView.editLimit()}"
value="#{i18n['productLimit.edit']}"/>
</p:column>
</p:dataTable>
......@@ -102,18 +152,23 @@
<hr />
<h:form id="options">
<h:commandButton rendered="#{!empty productView.product.id}" action="#{productView.initCreateProductOptionGroup()}" value="#{i18n['product.createProductOptionGroup']}"><br /><br />
<h:commandButton rendered="#{!empty productView.product.id}"
action="#{productView.initCreateProductOptionGroup()}"
value="#{i18n['product.createProductOptionGroup']}"><br/><br/>
</h:commandButton>
<p:dataTable border="1" id="optiongroups" value="#{productView.productOptionGroups}" var="group" rendered="#{!empty productView.product.id and !empty productView.product.productOptionGroups}">
<p:dataTable border="1" id="optiongroups" value="#{productView.productOptionGroups}" var="group"
rendered="#{!empty productView.product.id and !empty productView.product.productOptionGroups}">
<p:column rowHeader="#{i18n['name']}">
<h:outputText value="#{group.name}" />
</p:column>
<p:column>
<p:commandButton ajax="false" action="#{productView.editProductOptionGroup}" value="#{i18n['edit']}" />
<p:commandButton ajax="false" action="#{productView.editProductOptionGroup}"
value="#{i18n['edit']}"/>
</p:column>
<p:column>
<p:commandButton ajax="false" action="#{productView.deleteProductOptionGroup}" value="#{i18n['delete']}" />
<p:commandButton ajax="false" action="#{productView.deleteProductOptionGroup}"
value="#{i18n['delete']}"/>
</p:column>
</p:dataTable>
......
......@@ -23,7 +23,7 @@
<!-- <h:outputScript target="head" library="script" name="shopscript.js" /> -->
<h:outputScript library="primefaces" name="jquery/jquery.js" />
<p:dataTable columnClasses="nowrap,numalign,numalign,numalign,nowrap" id="billcart" value="#{cc.attrs.items}" var="cart" expandedRow="true">
<p:dataTable columnClasses="nowrap,numalign,numalign,numalign,nowrap" id="billcart" value="#{cc.attrs.items}" var="cart" expandedRow="#{cart.count > 0}">
<p:column style="width:16px">
<p:rowToggler rendered="#{cart.product.containsProductOptionGroups}" />
</p:column>
......
......@@ -274,6 +274,7 @@ public class ProductShopView extends GenericCDIView {
}
}
// Update all other items
for (ProductShopItem n : shoppingcart) {
BigDecimal l = limits.get(n.getProduct().getId());
if (l != null) {
......@@ -283,6 +284,11 @@ public class ProductShopView extends GenericCDIView {
psiHelper.updateProductShopItemLimit(n, l);
}
//Update product dependencies last...
for (ProductShopItem n : shoppingcart) {
n.updateProductDependencies(shoppingcart);
}
}
public String removeBought() {
......
......@@ -64,6 +64,7 @@ public class ProductView extends GenericCDIView {
private ProductOption productOption = null;
private ProductDependency dependency = new ProductDependency();
@SuppressWarnings("unused")
private static final Logger logger = LoggerFactory.getLogger(ProductView.class);
......@@ -76,6 +77,22 @@ public class ProductView extends GenericCDIView {
}
}
public List<Product> getProducts(){
return prodbean.getProducts();
}
public void createProductDependency(){
product.getProductDependencies().add(dependency);
dependency.setDependant(product);
dependency = new ProductDependency();
product = prodbean.mergeChanges(product);
}
public ProductDependency.DependencyType[] getDependencyTypes(){
return ProductDependency.DependencyType.values();
}
public void initCreateView() {
if (super.requirePermissions(ShopPermission.MANAGE_PRODUCTS)) {
setProduct(new Product(eventbean.getCurrentEvent()));
......@@ -270,4 +287,12 @@ public class ProductView extends GenericCDIView {
if(option != null)
productOptionGroup.setDefaultOption(option);
}
public ProductDependency getDependency() {
return dependency;
}
public void setDependency(ProductDependency dependency) {
this.dependency = dependency;
}
}
......@@ -125,13 +125,13 @@ public class ProductShopItem {
/**
* DO NOT USE THIS.
*
*
* Use ProductShopIteHelper.setProductShopItemCount instead.
*
*
* p.s. This still needs to exist because we want to set count from jsf, but
* this value needs to be updated with
* ProductShopIteHelper.updateProductShopItemCount
*
*
* @param count
*/
public void setCount(BigDecimal count) {
......@@ -315,4 +315,23 @@ public class ProductShopItem {
}
public void updateProductDependencies(ListDataModel<ProductShopItem> shoppingcart) {
if (product.getProductDependencies() == null || product.getProductDependencies().isEmpty()) {
return;
}
BigDecimal limitCnt = BigDecimal.ZERO;
for (ProductDependency dep : product.getProductDependencies()) {
for (ProductShopItem item : shoppingcart) {
if (dep.getSupporter().equals(item.getProduct())) {
limitCnt = limitCnt.add(item.getCount());
break;
}
}
}
if (limitCnt.compareTo(count) < 0) {
this.count = limitCnt;
}
setLimit(limitCnt);
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!