Commit 44b92701 by Tuukka Kivilahti

user.getRoles() -> UserBean.findUsersRoles(user)

1 parent df16c9aa
package fi.codecrew.moya.beans;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
......@@ -27,7 +29,9 @@ import fi.codecrew.moya.facade.EventUserFacade;
import fi.codecrew.moya.model.AccountEvent;
import fi.codecrew.moya.model.Bill;
import fi.codecrew.moya.model.BillLine;
import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.FoodWave;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.LanEventPropertyKey;
import fi.codecrew.moya.model.Product;
......@@ -68,6 +72,9 @@ public class BillBean implements BillBeanLocal {
private EventUserFacade eventUserFacade;
@EJB
private ProductPBean productPBean;
@EJB
private DiscountBean discountBean;
/**
* Default constructor.
......@@ -304,5 +311,38 @@ public class BillBean implements BillBeanLocal {
bill.markExpired();
return bill;
}
@Override
public Bill addProductToBill(Bill bill, Product product, BigDecimal count) {
return this.addProductToBill(bill, product, count, null);
}
@Override
public Bill addProductToBill(Bill bill, Product product, BigDecimal count, FoodWave foodwave) {
// If bill number > 0 bill has been sent and extra privileges are needed
// to modify.
// if (!iscurrent || billnr != null) {
// permbean.fatalPermission(BillPermission.WRITE_ALL,
// "User tried to modify bill ", bill,
// "without sufficient permissions");
// }
if (bill.getBillLines() == null) {
bill.setBillLines(new ArrayList<BillLine>());
}
bill.getBillLines().add(new BillLine(bill, product, count, foodwave));
for (Discount disc : discountBean.getActiveDiscountsByProduct(product, count, bill.getSentDate(), bill.getUser())) {
bill.getBillLines().add(new BillLine(bill, product, disc, count));
}
return bill;
}
}
package fi.codecrew.moya.beans;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import fi.codecrew.moya.facade.DiscountFacade;
import fi.codecrew.moya.facade.EventFacade;
import fi.codecrew.moya.facade.ProductFacade;
import fi.codecrew.moya.beans.DiscountBeanLocal;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.model.Role;
/**
* Session Bean implementation class DiscountBean
*/
@Stateless
@LocalBean
public class DiscountBean implements DiscountBeanLocal {
@EJB
private DiscountFacade discountfacade;
@EJB
private EventBeanLocal eventbean;
@EJB
private ProductFacade productfacade;
@EJB
private EventFacade eventfacade;
private UserBean userBean;
public DiscountBean() {
}
......@@ -36,6 +39,35 @@ public class DiscountBean implements DiscountBeanLocal {
return ret;
}
@Override
public List<Discount> getActiveDiscountsByProduct(Product product, BigDecimal quantity, Calendar time, EventUser user) {
ArrayList<Discount> ret = new ArrayList<Discount>();
for (Discount d : product.getDiscounts()) {
if (d.isActive() &&
(d.getValidTo() == null || d.getValidTo().after(time)) &&
(d.getValidFrom() == null || d.getValidFrom().before(time)) &&
(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 stuff
if (d.getRole() != null) {
for (Role role : userBean.localFindUsersRoles(user)) {
if (d.getRole().equals(role)) {
ret.add(d);
}
}
} else {
ret.add(d);
}
}
}
return ret;
}
// @Override
// public Discount create(String discountdesc) {
// LanEvent ev = eventbean.getCurrentEvent();
......
......@@ -91,6 +91,7 @@ public class ProductBean implements ProductBeanLocal {
private EventBeanLocal eventbean;
@EJB
private BillLineFacade billLineFacade;
@EJB
private ProductPBean productPBean;
......@@ -99,6 +100,9 @@ public class ProductBean implements ProductBeanLocal {
@EJB
private PlaceBean placebean;
@EJB
private DiscountBean discountBean;
private static final Logger logger = LoggerFactory.getLogger(ProductBean.class);
......@@ -290,7 +294,7 @@ public class ProductBean implements ProductBeanLocal {
throw new RuntimeException("Some parameter is null!");
}
BigDecimal total = product.getPrice();
for (Discount d : product.getActiveDiscounts(quantity, date, user)) {
for (Discount d : discountBean.getActiveDiscountsByProduct(product, quantity, date, user)) {
total = total.multiply(d.getPercentage());
}
return total.setScale(2, RoundingMode.HALF_UP).multiply(quantity);
......
......@@ -30,6 +30,9 @@ public class ProductPBean {
@EJB
private PermissionBean permbean;
@EJB
private DiscountBean discountBean;
@EJB
private AccountEventFacade accounteventfacade;
private static final Logger logger = LoggerFactory
......@@ -73,7 +76,7 @@ public class ProductPBean {
}
BigDecimal unitPrice = product.getPrice().negate();
List<Discount> discounts = product.getActiveDiscounts(quantity, date, user);
List<Discount> discounts = discountBean.getActiveDiscountsByProduct(product, quantity, date, user);
for (Discount d : discounts) {
unitPrice = unitPrice.multiply(d.getPercentage());
}
......
package fi.codecrew.moya.beans;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
......@@ -10,6 +11,8 @@ import javax.ejb.Local;
import fi.codecrew.moya.bortal.views.BillSummary;
import fi.codecrew.moya.model.Bill;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.FoodWave;
import fi.codecrew.moya.model.Product;
@Local
public interface BillBeanLocal {
......@@ -37,5 +40,9 @@ public interface BillBeanLocal {
List<Bill> find(EventUser user);
Bill expireBill(Bill bill);
Bill addProductToBill(Bill bill, Product product, BigDecimal count, FoodWave foodwave);
Bill addProductToBill(Bill bill, Product product, BigDecimal count);
}
package fi.codecrew.moya.beans;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Local;
import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Product;
@Local
public interface DiscountBeanLocal {
Discount save(Discount discount);
public List<Discount> getActiveDiscountsByProduct(Product product, BigDecimal quantity, Calendar time, EventUser user);
}
......@@ -347,28 +347,6 @@ public class Bill extends GenericEntity {
return delayIntrest;
}
public void addProduct(Product product, BigDecimal count) {
this.addProduct(product, count, null);
}
public void addProduct(Product product, BigDecimal count, FoodWave foodwave) {
// If bill number > 0 bill has been sent and extra privileges are needed
// to modify.
// if (!iscurrent || billnr != null) {
// permbean.fatalPermission(BillPermission.WRITE_ALL,
// "User tried to modify bill ", bill,
// "without sufficient permissions");
// }
if (this.billLines == null) {
billLines = new ArrayList<BillLine>();
}
this.getBillLines().add(new BillLine(this, product, count, foodwave));
for (Discount disc : product.getActiveDiscounts(count, sentDate, this.getUser())) {
this.getBillLines().add(new BillLine(this, product, disc, count));
}
}
public void setBillNumber(Integer billNumber) {
this.billNumber = billNumber;
}
......
......@@ -162,34 +162,6 @@ public class Product extends GenericEntity {
return tot;
}
// TODO: alennukset lasketaan täällä. HUOMHUOM!!
// 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)) &&
(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;
}
public BigDecimal getInventoryCount() {
BigDecimal ret = new BigDecimal(0);
......
......@@ -40,10 +40,9 @@
<h:commandButton action="#{productShopView.addMinusOne}" value="#{i18n['productshop.minusOne']}">
<f:ajax render="@form" />
</h:commandButton>
<h:inputText size="4" id="cartcount" value="#{cart.count}">
<h:outputText id="cartcount" escape="false" value="&nbsp;&nbsp;#{cart.count}&nbsp;&nbsp;">
<f:convertNumber maxIntegerDigits="2" minFractionDigits="0" />
<f:ajax render="@form" event="valueChange" listener="#{productShopView.updateAllCartLimits()}" />
</h:inputText>
</h:outputText>
<h:commandButton action="#{productShopView.addOne}" value="#{i18n['productshop.plusOne']}">
<f:ajax render="@form" />
</h:commandButton>
......
......@@ -23,6 +23,7 @@ import fi.codecrew.moya.model.FoodWave;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
import fi.codecrew.moya.web.cdiview.user.UserView;
import fi.codecrew.moya.web.helper.ProductShopItemHelper;
import fi.codecrew.moya.web.helpers.ProductShopItem;
@Named
......@@ -36,6 +37,9 @@ public class FoodWaveFoodView extends GenericCDIView {
@EJB
private FoodWaveBeanLocal foodWaveBean;
@Inject
private ProductShopItemHelper psiHelper;
@EJB
EventBeanLocal eventBean;
......@@ -99,7 +103,8 @@ public class FoodWaveFoodView extends GenericCDIView {
public String add(Integer count) {
ProductShopItem item = getShoppingcart().getRowData();
item.setCount(item.getCount().add(BigDecimal.valueOf(count)));
psiHelper.setProductShopItemCount(item, item.getCount().add(BigDecimal.valueOf(count)));
System.out.println("foobar" + item.getCount());
return null;
}
......@@ -133,7 +138,7 @@ public class FoodWaveFoodView extends GenericCDIView {
for (ProductShopItem shopitem : shoppingcart) {
if (shopitem.getCount().compareTo(BigDecimal.ZERO) > 0) {
bill.addProduct(shopitem.getProduct(), shopitem.getCount(), getFoodWave());
billBean.addProductToBill(bill, shopitem.getProduct(), shopitem.getCount(), getFoodWave());
}
}
logger.warn("Committing shoppingcart for user {}. Cart prize: {}", userview.getSelectedUser().getWholeName(), bill.getTotalPrice());
......@@ -198,4 +203,12 @@ public class FoodWaveFoodView extends GenericCDIView {
this.billEditView = billEditView;
}
public ProductShopItemHelper getPsiHelper() {
return psiHelper;
}
public void setPsiHelper(ProductShopItemHelper psiHelper) {
this.psiHelper = psiHelper;
}
}
......@@ -31,6 +31,7 @@ import fi.codecrew.moya.web.annotations.SelectedUser;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
import fi.codecrew.moya.web.cdiview.reader.ReaderView;
import fi.codecrew.moya.web.cdiview.user.UserView;
import fi.codecrew.moya.web.helper.ProductShopItemHelper;
import fi.codecrew.moya.web.helpers.ProductShopItem;
@Named
......@@ -53,6 +54,8 @@ public class ProductShopView extends GenericCDIView {
@EJB
private transient EventBeanLocal eventbean;
public void cashChanged()
{
payInstant = false;
......@@ -78,6 +81,18 @@ public class ProductShopView extends GenericCDIView {
@Inject
private BillEditView billEditView;
@Inject
private ProductShopItemHelper psiHelper;
public ProductShopItemHelper getPsiHelper() {
return psiHelper;
}
public void setPsiHelper(ProductShopItemHelper psiHelper) {
this.psiHelper = psiHelper;
}
private boolean hasLimits = false;
private boolean blip = false;
private ListDataModel<ProductShopItem> boughtItems;
......@@ -151,7 +166,8 @@ public class ProductShopView extends GenericCDIView {
public String add(Integer count) {
ProductShopItem item = shoppingcart.getRowData();
item.setCount(item.getCount().add(BigDecimal.valueOf(count)));
psiHelper.setProductShopItemCount(item, item.getCount().add(BigDecimal.valueOf(count)));
updateCartLimits(item);
return null;
......@@ -196,7 +212,7 @@ public class ProductShopView extends GenericCDIView {
// Update the updated cart first
if (item != null) {
BigDecimal l = limits.get(item.getProduct().getId());
if (item.updateLimit(l)) {
if (psiHelper.updateProductShopItemLimit(item, l)) {
updateCartLimits(null);
return;
}
......@@ -207,14 +223,16 @@ public class ProductShopView extends GenericCDIView {
if (l != null) {
hasLimits = true;
}
n.updateLimit(l);
psiHelper.updateProductShopItemLimit(n,l);
}
}
public String removeBought() {
ProductShopItem row = boughtItems.getRowData();
row.setCount(row.getCount().subtract(BigDecimal.ONE));
psiHelper.setProductShopItemCount(row, row.getCount().subtract(BigDecimal.ONE));
updateCartLimits(row);
return null;
......@@ -284,7 +302,7 @@ public class ProductShopView extends GenericCDIView {
for (ProductShopItem shopitem : shoppingcart) {
if (shopitem.getCount().compareTo(BigDecimal.ZERO) > 0) {
bill.addProduct(shopitem.getProduct(), shopitem.getCount());
billbean.addProductToBill(bill, shopitem.getProduct(), shopitem.getCount());
}
}
billbean.createBill(bill);
......
package fi.codecrew.moya.web.helper;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Named;
import fi.codecrew.moya.beans.DiscountBeanLocal;
import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
import fi.codecrew.moya.web.helpers.ProductShopItem;
@Named
@ConversationScoped
public class ProductShopItemHelper extends GenericCDIView {
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB
private DiscountBeanLocal discountBean;
public void setProductShopItemCount(ProductShopItem item, BigDecimal count) {
if (count == null || count.compareTo(BigDecimal.ZERO) < 0)
{
count = BigDecimal.ZERO;
}
item.setInternalCount(count);
item.setInternalPrice(item.getProduct().getPrice().abs().multiply(count));
item.setInternalDiscounts(discountBean.getActiveDiscountsByProduct(item.getProduct(),count, Calendar.getInstance(), item.getUser()) );
item.setInternalDiscountValues(new HashMap<Integer, BigDecimal>());
for (Discount d : item.getDiscounts())
{
BigDecimal newprice = item.getPrice().multiply(d.getPercentage());
item.getInternalDiscountValues().put(d.getId(), item.getPrice().subtract(newprice));
item.setInternalPrice(newprice);
}
}
public boolean updateProductShopItemLimit(ProductShopItem item, BigDecimal limitValue) {
if (limitValue != null && limitValue.compareTo(BigDecimal.ZERO) < 0)
{
this.setProductShopItemCount(item, item.getCount().add(limitValue));
if (item.getCount().compareTo(BigDecimal.ZERO) < 0) {
this.setProductShopItemCount(item,BigDecimal.ZERO);
}
item.setLimit(BigDecimal.ZERO);
return true;
}
item.setLimit(limitValue);
return false;
}
}
......@@ -2,8 +2,6 @@ package fi.codecrew.moya.web.helpers;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -25,7 +23,6 @@ public class ProductShopItem {
private BigDecimal price;
private BigDecimal limit;
private EventUser user;
public BigDecimal getCreditPrice()
{
......@@ -50,7 +47,8 @@ public class ProductShopItem {
this.user = user;
this.product = prod;
id = this.product.getId();
setCount(BigDecimal.ZERO);
setInternalCount(BigDecimal.ZERO);
setInternalPrice(BigDecimal.ZERO);
}
/**
......@@ -83,36 +81,35 @@ public class ProductShopItem {
return this.product;
}
public void setCount(BigDecimal count) {
if (count == null || count.compareTo(BigDecimal.ZERO) < 0)
{
count = BigDecimal.ZERO;
}
/**
* DO NOT USE THIS.
*
* Use ProductShopIteHelper.setProductShopItemCount instead.
* @param count
*/
public void setInternalCount(BigDecimal count) {
this.count = count;
price = product.getPrice().abs().multiply(count);
discounts = product.getActiveDiscounts(count, Calendar.getInstance(), user);
discountValues = new HashMap<Integer, BigDecimal>();
for (Discount d : discounts)
{
BigDecimal newprice = price.multiply(d.getPercentage());
discountValues.put(d.getId(), price.subtract(newprice));
price = newprice;
}
}
public void setInternalPrice(BigDecimal price) {
this.price = price;
}
public List<Discount> getDiscounts()
{
return discounts;
}
public void setInternalDiscounts(List<Discount> discounts) {
this.discounts = discounts;
}
public BigDecimal getDiscount(Integer discId)
{
return discountValues.get(discId);
}
public BigDecimal getPrice()
{
return price;
......@@ -141,22 +138,6 @@ public class ProductShopItem {
return limit;
}
public boolean updateLimit(BigDecimal limitValue) {
if (limitValue != null && limitValue.compareTo(BigDecimal.ZERO) < 0)
{
logger.info("product limit {}, count {}", limitValue, count);
setCount(getCount().add(limitValue));
if (count.compareTo(BigDecimal.ZERO) < 0) {
setCount(BigDecimal.ZERO);
}
limit = BigDecimal.ZERO;
return true;
}
limit = limitValue;
return false;
}
public EventUser getUser() {
return user;
}
......@@ -164,4 +145,12 @@ public class ProductShopItem {
public void setUser(EventUser user) {
this.user = user;
}
public Map<Integer, BigDecimal> getInternalDiscountValues() {
return discountValues;
}
public void setInternalDiscountValues(Map<Integer, BigDecimal> discountValues) {
this.discountValues = discountValues;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!