Commit 4ee37211 by Tuukka Kivilahti

#74

1 parent d2718a58
......@@ -53,6 +53,8 @@ public interface ProductBeanLocal {
AccountEvent createAccountEvent(Product product, BigDecimal quantity, EventUser user);
AccountEvent createAccountEvent(Product product, BigDecimal overriddenUnitPrice, BigDecimal quantity, EventUser user);
// List<Discount> getActiveDiscounts(Product product, BigDecimal quantity);
Product findById(int parseInt);
......
......@@ -72,12 +72,12 @@ public class BillBean implements BillBeanLocal {
private BillFacade billFacade;
@EJB
private EventBeanLocal eventbean;
private EventBean eventbean;
@EJB
private BillLineFacade billLineFacade;
@EJB
private PermissionBeanLocal permbean;
private PermissionBean permbean;
@EJB
private ProductBean productBean;
......@@ -308,7 +308,7 @@ public class BillBean implements BillBeanLocal {
logger.debug("Creating Bill prepaidInstant product {}, {}", prod.getName(), bl.getQuantity());
AccountEvent ac2 = productPBean.createAccountEvent(prod, bl.getQuantity(), user, bill.getSentDate(), bl.getFoodwave());
AccountEvent ac2 = productPBean.createAccountEvent(prod, bl.getQuantity(), null, user, bill.getSentDate(), bl.getFoodwave());
logger.info("Created ac from product. {}, userproducts {}", ac2, user.getAccountEvents().size());
ac2.setSeller(permbean.getCurrentUser());
......
......@@ -407,6 +407,15 @@ public class ProductBean implements ProductBeanLocal {
}
@Override
public AccountEvent createAccountEvent(Product product, BigDecimal overriddenUnitPrice, BigDecimal quantity, EventUser user) {
user = eventUserFacade.reload(user);
AccountEvent ret = productPBean.createAccountEvent(product, quantity, overriddenUnitPrice, user, Calendar.getInstance(), null);
cardTemplateBean.checkPrintedCard(user);
return ret;
}
@Override
// @RolesAllowed(ShopPermission.S_LIST_ALL_PRODUCTS)
public Product findByBarcode(String barcode) {
return productFacade.findProductByBarcode(barcode);
......
......@@ -23,10 +23,7 @@ import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -62,6 +59,10 @@ public class ProductPBean {
@EJB
private ProductFacade productFacade;
@EJB
private LoggingBean lbean;
private static final Logger logger = LoggerFactory
.getLogger(ProductPBean.class);
......@@ -72,9 +73,9 @@ public class ProductPBean {
// TODO Auto-generated constructor stub
}
public AccountEvent createAccountEvent(Product product,
BigDecimal quantity, EventUser user, Calendar date) {
return this.createAccountEvent(product, quantity, user, date, null);
public AccountEvent createAccountEvent(Product product, BigDecimal quantity, EventUser user, Calendar date) {
return this.createAccountEvent(product, null, quantity, user, date, null);
}
/**
......@@ -93,9 +94,7 @@ public class ProductPBean {
* AccountEvent creation time
* @return The created AccountEvent entity
*/
public AccountEvent createAccountEvent(Product product,
BigDecimal quantity, EventUser user, Calendar date,
FoodWave foodwave) {
public AccountEvent createAccountEvent(Product product, BigDecimal overriddenUnitPrice, BigDecimal quantity, EventUser user, Calendar date, FoodWave foodwave) {
if (!accounteventfacade.isAttached(product)) {
product = productFacade.reload(product);
......@@ -104,12 +103,25 @@ public class ProductPBean {
if (!product.getEvent().equals(user.getEvent())) {
throw new EJBException("Trying to create accountevent for different event in user and product");
}
BigDecimal unitPrice = product.getPrice().negate();
List<Discount> discounts = discountBean.getActiveDiscountsByProduct(product, quantity, date, user);
List<Discount> discounts = new ArrayList<>();
if(overriddenUnitPrice != null && BigDecimal.ZERO.compareTo(overriddenUnitPrice) < 0) {
unitPrice = overriddenUnitPrice;
lbean.logMessage(SecurityLogType.accountEvent, permbean.getCurrentUser(), "User creating accountevent with discount");
} else {
// no discounts if custom price
discounts = discountBean.getActiveDiscountsByProduct(product, quantity, date, user);
for (Discount d : discounts) {
unitPrice = unitPrice.multiply(d.getPercentage());
}
}
AccountEvent ret = new AccountEvent(user, product, unitPrice, quantity, Calendar.getInstance());
......@@ -124,17 +136,18 @@ public class ProductPBean {
foodwave.getAccountEvents().add(ret);
}
if (product.getProductFlags().contains(ProductFlag.RESERVE_PLACE_WHEN_BOUGHT)
|| product.getProductFlags().contains(ProductFlag.CREATE_NEW_PLACE_WHEN_BOUGHT)) {
if (product.getProductFlags().contains(ProductFlag.RESERVE_PLACE_WHEN_BOUGHT) || product.getProductFlags().contains(ProductFlag.CREATE_NEW_PLACE_WHEN_BOUGHT)) {
placebean.lockPlaceProduct(user, product, quantity);
}
List<DiscountInstance> accEventdiscounts = ret.getDiscountInstances();
for (Discount d : discounts) {
// discountsArray.add(discInst);
// discountinstancefacade.create(discInst);
accEventdiscounts.add(new DiscountInstance(ret, d));
}
if (product.getAccountEvents() == null) {
product.setAccountEvents(new ArrayList<AccountEvent>());
}
......@@ -145,6 +158,8 @@ public class ProductPBean {
logger.debug("create ac {} for user {}", ret, user.getUser());
// flush changes to db.
// userFacade.flush();
lbean.logMessage(SecurityLogType.accountEvent, permbean.getCurrentUser(), "User created accountevent: ", ret.getId());
return ret;
}
}
Manifest-Version: 1.0
Class-Path: lib/MoyaUtilities.jar
......@@ -36,10 +36,14 @@
<h:panelGroup >
<reader:codefield selectaction="#{productShopView.readCode}" selectvalue="#{i18n['shop.readBarcode']}" />
<!-- TODO: barcode does not work, fix and re-enable -->
<!-- reader:codefield selectaction="#{productShopView.readCode}" selectvalue="#{i18n['shop.readBarcode']}" / -->
<br />
<p:dataTable id="prods" value="#{productShopView.boughtItems}" var="prods" style="width:500px;">
<p:dataTable id="prods" value="#{productShopView.boughtItems}" editable="true" var="prods" style="width:600px;">
<p:ajax event="rowEdit" listener="#{productShopItemHelper.updateProductShopItemCount(prods)}" update="prods @(.inputval)" />
<p:column headerText="#{i18n['shop.count']}">
<p:inputText value="#{prods.count}" size="2">
<f:ajax render="@form" listener="#{productShopView.countBoughtChangeListener}" />
......@@ -55,19 +59,29 @@
<f:convertNumber maxFractionDigits="2" minFractionDigits="2" />
</h:outputText>
</p:column>
<p:column headerText="#{i18n['shop.sellPrice']}">
<h:outputText value="#{prods.creditPrice}">
<p:column headerText="#{i18n['shop.customPrice']}">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{prods.overriddenUnitPrice}" /></f:facet>
<f:facet name="input"><p:inputText value="#{prods.overriddenUnitPrice}" /></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="#{i18n['shop.unitdiscount']}">
<h:outputText value="#{prods.unitDiscount}">
<f:convertNumber maxFractionDigits="2" minFractionDigits="2" />
</h:outputText>
</p:column>
<p:column headerText="#{i18n['shop.totalPrice']}">
<h:outputText value="#{prods.price}">
<f:convertNumber maxFractionDigits="2" minFractionDigits="2" />
</h:outputText>
</p:column>
<p:column headerText="#{i18n['shop.actions']}">
<p:commandButton action="#{productShopView.removeBought()}" update="@form" onerror="location.reload(true);" value="Poista" />
<p:rowEditor />
<p:commandButton action="#{productShopView.removeBought()}" update="@form" onerror="location.reload(true);" icon="ui-icon-trash" />
</p:column>
</p:dataTable>
......@@ -120,7 +134,6 @@
$(function() {
$("#shoppingcartform\\:barcode").focus();
});
function blip() {
......
......@@ -163,7 +163,7 @@
}
</h:outputScript>
<h:form id="usermetaform" enctype="multipart/form-data">
<p:fieldset legend="#{i18n['user.meta.box.legend']}">
<p:fieldset legend="#{i18n['user.meta.box.title']}">
<p:panelGrid columns="1" cellpadding="1">
<div id="usermetaview">#{userView.meta}</div>
</p:panelGrid>
......
......@@ -19,9 +19,10 @@
</ui:define>
<ui:define name="edittab">
<users:usertabs tabId="shop" />
</ui:define>
<ui:define name="content">
<shop:shoppingcart />
</ui:define>
......
......@@ -124,8 +124,6 @@ public class FoodWaveFoodView extends GenericCDIView {
public String add(Integer count) {
ProductShopItem item = getShoppingcart().getRowData();
psiHelper.setProductShopItemCount(item, item.getCount().add(BigDecimal.valueOf(count)));
System.out.println("foobar" + item.getCount());
return null;
}
......
......@@ -177,6 +177,7 @@ public class ProductShopView extends GenericCDIView {
}
}
public void countBoughtChangeListener()
{
ProductShopItem item = boughtItems.getRowData();
......@@ -361,11 +362,13 @@ public class ProductShopView extends GenericCDIView {
public String commitShoppingCart() {
EventUser retuser = null;
for (ProductShopItem shopitem : shoppingcart) {
if (shopitem.getCount().compareTo(BigDecimal.ZERO) > 0) {
retuser = productBean.createAccountEvent(shopitem.getProduct(), shopitem.getCount(), userView.getSelectedUser()).getUser();
retuser = productBean.createAccountEvent(shopitem.getProduct(), shopitem.getCount(), shopitem.getOverriddenUnitPrice(), userView.getSelectedUser()).getUser();
}
}
if (cash != null && cash.compareTo(BigDecimal.ZERO) != 0) {
Product credProd = productBean.findCreditProduct();
retuser = productBean.createAccountEvent(credProd, cash, userView.getSelectedUser()).getUser();
......
......@@ -43,17 +43,15 @@ public class ProductShopItemHelper extends GenericCDIView {
@EJB
private DiscountBeanLocal discountBean;
public void setProductShopItemCount(ProductShopItem item, BigDecimal count) {
if (count == null || count.compareTo(BigDecimal.ZERO) < 0)
{
count = BigDecimal.ZERO;
}
item.setCount(count);
public void updateProductShopItemCount(ProductShopItem item) {
item.setInternalPrice(item.getProduct().getPrice().abs().multiply(count));
// Discounts or overridden price, you cannot get both
if(item.isPriceOverridden()) {
item.setInternalPrice(item.getOverriddenUnitPrice().multiply(item.getCount()));
} else {
item.setInternalPrice(item.getProduct().getPrice().abs().multiply(item.getCount()));
item.setInternalDiscounts(discountBean.getActiveDiscountsByProduct(item.getProduct(), count, Calendar.getInstance(), item.getUser()));
item.setInternalDiscounts(discountBean.getActiveDiscountsByProduct(item.getProduct(), item.getCount(), Calendar.getInstance(), item.getUser()));
item.setInternalDiscountValues(new HashMap<Integer, BigDecimal>());
......@@ -65,6 +63,20 @@ public class ProductShopItemHelper extends GenericCDIView {
}
}
}
public void setProductShopItemCount(ProductShopItem item, BigDecimal count) {
if (count == null || count.compareTo(BigDecimal.ZERO) < 0)
{
count = BigDecimal.ZERO;
}
item.setCount(count);
updateProductShopItemCount(item);
}
public boolean updateProductShopItemLimit(ProductShopItem item, BigDecimal limitValue) {
if (limitValue != null && limitValue.compareTo(BigDecimal.ZERO) < 0)
......
......@@ -19,6 +19,7 @@
package fi.codecrew.moya.web.helpers;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
......@@ -42,6 +43,12 @@ public class ProductShopItem {
private BigDecimal limit;
private EventUser user;
private BigDecimal overriddenUnitPrice = BigDecimal.ZERO;
public BigDecimal getCreditPrice()
{
if (BigDecimal.ZERO.compareTo(price) < 0)
......@@ -106,7 +113,7 @@ public class ProductShopItem {
*
* p.s. This still needs to exist because we want to set count from jsf, but
* this value needs to be updated with
* ProductShopIteHelper.setProductShopItemCount
* ProductShopIteHelper.updateProductShopItemCount
*
* @param count
*/
......@@ -114,6 +121,11 @@ public class ProductShopItem {
this.count = count;
}
/**
* Price and discounts are only stored so we can show them to user, changing there does not change end price of card
* @param discounts
*/
public void setInternalPrice(BigDecimal price) {
this.price = price;
}
......@@ -123,6 +135,10 @@ public class ProductShopItem {
return discounts;
}
/**
* Price and discounts are only stored so we can show them to user, changing there does not change end price of card
* @param discounts
*/
public void setInternalDiscounts(List<Discount> discounts) {
this.discounts = discounts;
}
......@@ -175,4 +191,34 @@ public class ProductShopItem {
public void setInternalDiscountValues(Map<Integer, BigDecimal> discountValues) {
this.discountValues = discountValues;
}
public BigDecimal getOverriddenUnitPrice() {
if(overriddenUnitPrice == null)
overriddenUnitPrice = BigDecimal.ZERO;
return overriddenUnitPrice;
}
/**
* Warning: this will not work if you do not run also: ProductShopIteHelper.setProductShopItemCount
*
* Overridden unit price will also override discounts
*
* @param overrideUnitPrice
*/
public void setOverriddenUnitPrice(BigDecimal overrideUnitPrice) {
this.overriddenUnitPrice = overrideUnitPrice;
}
public boolean isPriceOverridden() {
return (getOverriddenUnitPrice() != null && getOverriddenUnitPrice().compareTo(BigDecimal.ZERO) > 0);
}
public BigDecimal getUnitDiscount() {
return getPrice().divide(getCount()).subtract(getProduct().getPrice());
}
}
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
#Sat Mar 30 17:56:44 EET 2013
shop.unitdiscount = Alennus
shop.customPrice = Uusi hinta
accountEvent.commit = Tallenna
actionlog.create.header = Create new actionmessage
......
......@@ -1614,3 +1614,5 @@ voting.create.voteEnd = Voting close
voting.create.voteStart = Voting start
yes = Yes
shop.unitdiscount=Discount
shop.customPrice=new price
......@@ -1596,3 +1596,5 @@ voting.create.voteEnd = \u00C4\u00E4nestys kiinni
voting.create.voteStart = \u00C4\u00E4nestys auki
yes = Kyll\u00E4
shop.unitdiscount=Alennus
shop.customPrice=Uusi hinta
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!