Commit df16c9aa by Tuukka Kivilahti

discount by role

1 parent bb1a5f46
......@@ -151,7 +151,7 @@ public class PlaceBean implements PlaceBeanLocal {
for (Entry<Product, Integer> entry : mockmap.entrySet()) {
logger.debug("Adding to price {} of {}", entry.getValue(), entry.getKey().getName());
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;
......
......@@ -285,12 +285,12 @@ public class ProductBean implements ProductBeanLocal {
}
@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) {
throw new RuntimeException("Some parameter is null!");
}
BigDecimal total = product.getPrice();
for (Discount d : product.getActiveDiscounts(quantity, date)) {
for (Discount d : product.getActiveDiscounts(quantity, date, user)) {
total = total.multiply(d.getPercentage());
}
return total.setScale(2, RoundingMode.HALF_UP).multiply(quantity);
......
......@@ -73,7 +73,7 @@ public class ProductPBean {
}
BigDecimal unitPrice = product.getPrice().negate();
List<Discount> discounts = product.getActiveDiscounts(quantity, date);
List<Discount> discounts = product.getActiveDiscounts(quantity, date, user);
for (Discount d : discounts) {
unitPrice = unitPrice.multiply(d.getPercentage());
}
......
......@@ -47,7 +47,7 @@ public interface ProductBeanLocal {
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);
......
......@@ -364,7 +364,7 @@ public class Bill extends GenericEntity {
}
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));
}
}
......
......@@ -163,15 +163,28 @@ public class Product extends GenericEntity {
}
// 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>();
for (Discount d : getDiscounts()) {
if (d.isActive() &&
(d.getValidTo() == null || d.getValidTo().after(time)) &&
(d.getValidFrom() == null || d.getValidFrom().before(time)) &&
quantity.compareTo(d.getAmountMax()) <= 0 &&
quantity.compareTo(d.getAmountMin()) >= 0) {
ret.add(d);
(d.getAmountMax().compareTo(BigDecimal.ZERO) == 0 || quantity.compareTo(d.getAmountMax()) <= 0) &&
(d.getAmountMin().compareTo(BigDecimal.ZERO) == 0 || quantity.compareTo(d.getAmountMin()) >= 0)) {
// 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;
......
......@@ -31,6 +31,12 @@
<h:inputText id="code" value="#{productView.discount.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:inputText id="amountMin" value="#{productView.discount.amountMin}" required="true" />
<h:message for="amountMin" />
......@@ -54,6 +60,8 @@
<h:outputLabel for="active" value="#{i18n['discount.active']}" />
<h:selectBooleanCheckbox id="active" value="#{productView.discount.active}" />
<h:message for="active" />
</h:panelGrid>
......
......@@ -66,7 +66,7 @@ public class FoodWaveFoodView extends GenericCDIView {
foodWave = foodWaveBean.findFoodwave(getFoodwaveid());
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();
}
......
......@@ -94,7 +94,7 @@ public class ProductShopView extends GenericCDIView {
public void initBillView() {
if (requirePermissions(ShopPermission.LIST_USERPRODUCTS)
&& shoppingcart == null) {
shoppingcart = new ListDataModel<ProductShopItem>(ProductShopItem.productList(productBean.listUserShoppableProducts()));
shoppingcart = new ListDataModel<ProductShopItem>(ProductShopItem.productList(productBean.listUserShoppableProducts(), user));
updateCartLimits(null);
logger.debug("Initialized billing shoppingcart to {}", shoppingcart);
this.beginConversation();
......@@ -137,7 +137,7 @@ public class ProductShopView extends GenericCDIView {
public void initShopView() {
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);
LanEventProperty cashdefault = eventbean.getProperty(LanEventPropertyKey.SHOP_DEFAULT_CASH);
......
......@@ -11,6 +11,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Product;
public class ProductShopItem {
......@@ -23,6 +24,8 @@ public class ProductShopItem {
private Map<Integer, BigDecimal> discountValues;
private BigDecimal price;
private BigDecimal limit;
private EventUser user;
public BigDecimal getCreditPrice()
{
......@@ -42,12 +45,12 @@ public class ProductShopItem {
return BigDecimal.ZERO;
}
public ProductShopItem(Product prod) {
public ProductShopItem(Product prod, EventUser user) {
super();
this.user = user;
this.product = prod;
id = this.product.getId();
setCount(BigDecimal.ZERO);
}
/**
......@@ -56,21 +59,21 @@ public class ProductShopItem {
* @param findForStaffshop
* @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>();
for (Product prod : products) {
if (prod.getPrice().compareTo(BigDecimal.ZERO) >= 0) {
ret.add(new ProductShopItem(prod));
ret.add(new ProductShopItem(prod, user));
}
}
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>();
for (Product prod : products) {
ret.add(new ProductShopItem(prod));
ret.add(new ProductShopItem(prod, user));
}
return ret;
......@@ -89,7 +92,7 @@ public class ProductShopItem {
this.count = 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>();
for (Discount d : discounts)
{
......@@ -153,4 +156,12 @@ public class ProductShopItem {
limit = limitValue;
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!