Commit df16c9aa by Tuukka Kivilahti

discount by role

1 parent bb1a5f46
...@@ -151,7 +151,7 @@ public class PlaceBean implements PlaceBeanLocal { ...@@ -151,7 +151,7 @@ public class PlaceBean implements PlaceBeanLocal {
for (Entry<Product, Integer> entry : mockmap.entrySet()) { for (Entry<Product, Integer> entry : mockmap.entrySet()) {
logger.debug("Adding to price {} of {}", entry.getValue(), entry.getKey().getName()); logger.debug("Adding to price {} of {}", entry.getValue(), entry.getKey().getName());
if (entry.getKey() != null) { if (entry.getKey() != null) {
total = total.add(productBean.calculateTotal(entry.getKey(), new BigDecimal(entry.getValue()), now)); total = total.add(productBean.calculateTotal(entry.getKey(), new BigDecimal(entry.getValue()), now, user));
} }
} }
return total; return total;
......
...@@ -285,12 +285,12 @@ public class ProductBean implements ProductBeanLocal { ...@@ -285,12 +285,12 @@ public class ProductBean implements ProductBeanLocal {
} }
@Override @Override
public BigDecimal calculateTotal(Product product, BigDecimal quantity, Calendar date) { public BigDecimal calculateTotal(Product product, BigDecimal quantity, Calendar date, EventUser user) {
if (product == null || quantity == null) { if (product == null || quantity == null) {
throw new RuntimeException("Some parameter is null!"); throw new RuntimeException("Some parameter is null!");
} }
BigDecimal total = product.getPrice(); BigDecimal total = product.getPrice();
for (Discount d : product.getActiveDiscounts(quantity, date)) { for (Discount d : product.getActiveDiscounts(quantity, date, user)) {
total = total.multiply(d.getPercentage()); total = total.multiply(d.getPercentage());
} }
return total.setScale(2, RoundingMode.HALF_UP).multiply(quantity); return total.setScale(2, RoundingMode.HALF_UP).multiply(quantity);
......
...@@ -73,7 +73,7 @@ public class ProductPBean { ...@@ -73,7 +73,7 @@ public class ProductPBean {
} }
BigDecimal unitPrice = product.getPrice().negate(); BigDecimal unitPrice = product.getPrice().negate();
List<Discount> discounts = product.getActiveDiscounts(quantity, date); List<Discount> discounts = product.getActiveDiscounts(quantity, date, user);
for (Discount d : discounts) { for (Discount d : discounts) {
unitPrice = unitPrice.multiply(d.getPercentage()); unitPrice = unitPrice.multiply(d.getPercentage());
} }
......
...@@ -47,7 +47,7 @@ public interface ProductBeanLocal { ...@@ -47,7 +47,7 @@ public interface ProductBeanLocal {
Discount save(Discount discount); Discount save(Discount discount);
BigDecimal calculateTotal(Product product, BigDecimal quantity, Calendar date); BigDecimal calculateTotal(Product product, BigDecimal quantity, Calendar date, EventUser user);
HashMap<Integer, BigDecimal> getProductLimit(Map<Integer, BigDecimal> prodCounts, EventUser user); HashMap<Integer, BigDecimal> getProductLimit(Map<Integer, BigDecimal> prodCounts, EventUser user);
......
...@@ -364,7 +364,7 @@ public class Bill extends GenericEntity { ...@@ -364,7 +364,7 @@ public class Bill extends GenericEntity {
} }
this.getBillLines().add(new BillLine(this, product, count, foodwave)); this.getBillLines().add(new BillLine(this, product, count, foodwave));
for (Discount disc : product.getActiveDiscounts(count, sentDate)) { for (Discount disc : product.getActiveDiscounts(count, sentDate, this.getUser())) {
this.getBillLines().add(new BillLine(this, product, disc, count)); this.getBillLines().add(new BillLine(this, product, disc, count));
} }
} }
......
...@@ -163,15 +163,28 @@ public class Product extends GenericEntity { ...@@ -163,15 +163,28 @@ public class Product extends GenericEntity {
} }
// TODO: alennukset lasketaan täällä. HUOMHUOM!! // TODO: alennukset lasketaan täällä. HUOMHUOM!!
public List<Discount> getActiveDiscounts(BigDecimal quantity, Calendar time) { // Oikea paikka ois joku kaunis bean, mutta ehkä enskerralla voin
// refactoroida
// tai si sitäseuraavalla -TKjne
public List<Discount> getActiveDiscounts(BigDecimal quantity, Calendar time, EventUser user) {
ArrayList<Discount> ret = new ArrayList<Discount>(); ArrayList<Discount> ret = new ArrayList<Discount>();
for (Discount d : getDiscounts()) { for (Discount d : getDiscounts()) {
if (d.isActive() && if (d.isActive() &&
(d.getValidTo() == null || d.getValidTo().after(time)) && (d.getValidTo() == null || d.getValidTo().after(time)) &&
(d.getValidFrom() == null || d.getValidFrom().before(time)) && (d.getValidFrom() == null || d.getValidFrom().before(time)) &&
quantity.compareTo(d.getAmountMax()) <= 0 && (d.getAmountMax().compareTo(BigDecimal.ZERO) == 0 || quantity.compareTo(d.getAmountMax()) <= 0) &&
quantity.compareTo(d.getAmountMin()) >= 0) { (d.getAmountMin().compareTo(BigDecimal.ZERO) == 0 || quantity.compareTo(d.getAmountMin()) >= 0)) {
ret.add(d);
// plaah, there is role, must do magic
if (d.getRole() != null) {
for (Role role : user.getRoles()) {
if (d.getRole().equals(role)) {
ret.add(d);
}
}
} else {
ret.add(d);
}
} }
} }
return ret; return ret;
......
...@@ -31,6 +31,12 @@ ...@@ -31,6 +31,12 @@
<h:inputText id="code" value="#{productView.discount.code}" /> <h:inputText id="code" value="#{productView.discount.code}" />
<h:message for="code" /> <h:message for="code" />
<h:outputLabel for="role" value="#{i18n['discount.role']}:" />
<h:selectOneMenu id="role" value="#{productView.discount.role}" converter="#{roleConverter}">
<f:selectItems var="role" itemLabel="#{role.name}" value="#{roleDataView.rolesWithEmpty}" />
</h:selectOneMenu>
<h:message for="role" />
<h:outputLabel for="amountMin" value="#{i18n['discount.amountMin']}:" /> <h:outputLabel for="amountMin" value="#{i18n['discount.amountMin']}:" />
<h:inputText id="amountMin" value="#{productView.discount.amountMin}" required="true" /> <h:inputText id="amountMin" value="#{productView.discount.amountMin}" required="true" />
<h:message for="amountMin" /> <h:message for="amountMin" />
...@@ -54,6 +60,8 @@ ...@@ -54,6 +60,8 @@
<h:outputLabel for="active" value="#{i18n['discount.active']}" /> <h:outputLabel for="active" value="#{i18n['discount.active']}" />
<h:selectBooleanCheckbox id="active" value="#{productView.discount.active}" /> <h:selectBooleanCheckbox id="active" value="#{productView.discount.active}" />
<h:message for="active" /> <h:message for="active" />
</h:panelGrid> </h:panelGrid>
......
...@@ -66,7 +66,7 @@ public class FoodWaveFoodView extends GenericCDIView { ...@@ -66,7 +66,7 @@ public class FoodWaveFoodView extends GenericCDIView {
foodWave = foodWaveBean.findFoodwave(getFoodwaveid()); foodWave = foodWaveBean.findFoodwave(getFoodwaveid());
logger.debug("Foodwave {}", foodWave); logger.debug("Foodwave {}", foodWave);
shoppingcart = new ListDataModel<ProductShopItem>(ProductShopItem.productGTList(foodWave.getTemplate().getProducts())); shoppingcart = new ListDataModel<ProductShopItem>(ProductShopItem.productGTList(foodWave.getTemplate().getProducts(), userview.getUser()));
this.beginConversation(); this.beginConversation();
} }
......
...@@ -94,7 +94,7 @@ public class ProductShopView extends GenericCDIView { ...@@ -94,7 +94,7 @@ public class ProductShopView extends GenericCDIView {
public void initBillView() { public void initBillView() {
if (requirePermissions(ShopPermission.LIST_USERPRODUCTS) if (requirePermissions(ShopPermission.LIST_USERPRODUCTS)
&& shoppingcart == null) { && shoppingcart == null) {
shoppingcart = new ListDataModel<ProductShopItem>(ProductShopItem.productList(productBean.listUserShoppableProducts())); shoppingcart = new ListDataModel<ProductShopItem>(ProductShopItem.productList(productBean.listUserShoppableProducts(), user));
updateCartLimits(null); updateCartLimits(null);
logger.debug("Initialized billing shoppingcart to {}", shoppingcart); logger.debug("Initialized billing shoppingcart to {}", shoppingcart);
this.beginConversation(); this.beginConversation();
...@@ -137,7 +137,7 @@ public class ProductShopView extends GenericCDIView { ...@@ -137,7 +137,7 @@ public class ProductShopView extends GenericCDIView {
public void initShopView() { public void initShopView() {
if (requirePermissions(ShopPermission.SHOP_TO_OTHERS) && shoppingcart == null) { if (requirePermissions(ShopPermission.SHOP_TO_OTHERS) && shoppingcart == null) {
shoppingcart = new ListDataModel<ProductShopItem>(ProductShopItem.productGTList(productBean.findForStaffshop())); shoppingcart = new ListDataModel<ProductShopItem>(ProductShopItem.productGTList(productBean.findForStaffshop(), user));
updateCartLimits(null); updateCartLimits(null);
LanEventProperty cashdefault = eventbean.getProperty(LanEventPropertyKey.SHOP_DEFAULT_CASH); LanEventProperty cashdefault = eventbean.getProperty(LanEventPropertyKey.SHOP_DEFAULT_CASH);
......
...@@ -11,6 +11,7 @@ import org.slf4j.Logger; ...@@ -11,6 +11,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fi.codecrew.moya.model.Discount; import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Product; import fi.codecrew.moya.model.Product;
public class ProductShopItem { public class ProductShopItem {
...@@ -23,6 +24,8 @@ public class ProductShopItem { ...@@ -23,6 +24,8 @@ public class ProductShopItem {
private Map<Integer, BigDecimal> discountValues; private Map<Integer, BigDecimal> discountValues;
private BigDecimal price; private BigDecimal price;
private BigDecimal limit; private BigDecimal limit;
private EventUser user;
public BigDecimal getCreditPrice() public BigDecimal getCreditPrice()
{ {
...@@ -42,12 +45,12 @@ public class ProductShopItem { ...@@ -42,12 +45,12 @@ public class ProductShopItem {
return BigDecimal.ZERO; return BigDecimal.ZERO;
} }
public ProductShopItem(Product prod) { public ProductShopItem(Product prod, EventUser user) {
super(); super();
this.user = user;
this.product = prod; this.product = prod;
id = this.product.getId(); id = this.product.getId();
setCount(BigDecimal.ZERO); setCount(BigDecimal.ZERO);
} }
/** /**
...@@ -56,21 +59,21 @@ public class ProductShopItem { ...@@ -56,21 +59,21 @@ public class ProductShopItem {
* @param findForStaffshop * @param findForStaffshop
* @return * @return
*/ */
public static List<ProductShopItem> productGTList(List<Product> products) { public static List<ProductShopItem> productGTList(List<Product> products, EventUser user) {
List<ProductShopItem> ret = new ArrayList<ProductShopItem>(); List<ProductShopItem> ret = new ArrayList<ProductShopItem>();
for (Product prod : products) { for (Product prod : products) {
if (prod.getPrice().compareTo(BigDecimal.ZERO) >= 0) { if (prod.getPrice().compareTo(BigDecimal.ZERO) >= 0) {
ret.add(new ProductShopItem(prod)); ret.add(new ProductShopItem(prod, user));
} }
} }
return ret; return ret;
} }
public static List<ProductShopItem> productList(List<Product> products) { public static List<ProductShopItem> productList(List<Product> products, EventUser user) {
List<ProductShopItem> ret = new ArrayList<ProductShopItem>(); List<ProductShopItem> ret = new ArrayList<ProductShopItem>();
for (Product prod : products) { for (Product prod : products) {
ret.add(new ProductShopItem(prod)); ret.add(new ProductShopItem(prod, user));
} }
return ret; return ret;
...@@ -89,7 +92,7 @@ public class ProductShopItem { ...@@ -89,7 +92,7 @@ public class ProductShopItem {
this.count = count; this.count = count;
price = product.getPrice().abs().multiply(count); price = product.getPrice().abs().multiply(count);
discounts = product.getActiveDiscounts(count, Calendar.getInstance()); discounts = product.getActiveDiscounts(count, Calendar.getInstance(), user);
discountValues = new HashMap<Integer, BigDecimal>(); discountValues = new HashMap<Integer, BigDecimal>();
for (Discount d : discounts) for (Discount d : discounts)
{ {
...@@ -153,4 +156,12 @@ public class ProductShopItem { ...@@ -153,4 +156,12 @@ public class ProductShopItem {
limit = limitValue; limit = limitValue;
return false; return false;
} }
public EventUser getUser() {
return user;
}
public void setUser(EventUser user) {
this.user = user;
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!