Commit 92d97874 by Tuukka Kivilahti

Merge branch 'billingRefactoring' into 'master'

Biling fixes

Refactor some billing stuff.
Mostly adding entity updating to all relevant reference entities.
Also fixes #67

kattonu: @tkfftk
2 parents 77257bf0 1ba81836
...@@ -72,7 +72,7 @@ public class BillBean implements BillBeanLocal { ...@@ -72,7 +72,7 @@ public class BillBean implements BillBeanLocal {
private EventUserFacade eventUserFacade; private EventUserFacade eventUserFacade;
@EJB @EJB
private ProductPBean productPBean; private ProductPBean productPBean;
@EJB @EJB
private DiscountBean discountBean; private DiscountBean discountBean;
...@@ -232,7 +232,7 @@ public class BillBean implements BillBeanLocal { ...@@ -232,7 +232,7 @@ public class BillBean implements BillBeanLocal {
ac.setEventTime(when); ac.setEventTime(when);
ac.setBill(bill); ac.setBill(bill);
ac.setSeller(permbean.getCurrentUser()); ac.setSeller(permbean.getCurrentUser());
bill.setAccountEvent(ac); bill.setAccountEvent(ac);
bill.setPaidDate(when.getTime()); bill.setPaidDate(when.getTime());
...@@ -244,10 +244,6 @@ public class BillBean implements BillBeanLocal { ...@@ -244,10 +244,6 @@ public class BillBean implements BillBeanLocal {
if (prod != null && prod.getProductFlags().contains(ProductFlag.PREPAID_INSTANT_CREATE)) { if (prod != null && prod.getProductFlags().contains(ProductFlag.PREPAID_INSTANT_CREATE)) {
logger.debug("Creating Bill prepaidInstant product {}, {}", prod.getName(), bl.getQuantity()); logger.debug("Creating Bill prepaidInstant product {}, {}", prod.getName(), bl.getQuantity());
if (prod.getProductFlags().contains(ProductFlag.RESERVE_PLACE_WHEN_BOUGHT)
|| prod.getProductFlags().contains(ProductFlag.CREATE_NEW_PLACE_WHEN_BOUGHT)) {
placebean.lockPlaceProduct(bill.getUser(), prod, bl.getQuantity());
}
AccountEvent ac2 = productPBean.createAccountEvent(prod, bl.getQuantity(), user, bill.getSentDate(), bl.getFoodwave()); AccountEvent ac2 = productPBean.createAccountEvent(prod, bl.getQuantity(), user, bill.getSentDate(), bl.getFoodwave());
logger.info("Created ac from product. {}, userproducts {}", ac2, user.getAccountEvents().size()); logger.info("Created ac from product. {}, userproducts {}", ac2, user.getAccountEvents().size());
...@@ -311,7 +307,7 @@ public class BillBean implements BillBeanLocal { ...@@ -311,7 +307,7 @@ public class BillBean implements BillBeanLocal {
bill.markExpired(); bill.markExpired();
return bill; return bill;
} }
@Override @Override
public Bill addProductToBill(Bill bill, Product product, BigDecimal count) { public Bill addProductToBill(Bill bill, Product product, BigDecimal count) {
return this.addProductToBill(bill, product, count, null); return this.addProductToBill(bill, product, count, null);
...@@ -319,30 +315,25 @@ public class BillBean implements BillBeanLocal { ...@@ -319,30 +315,25 @@ public class BillBean implements BillBeanLocal {
@Override @Override
public Bill addProductToBill(Bill bill, Product product, BigDecimal count, FoodWave foodwave) { public Bill addProductToBill(Bill bill, Product product, BigDecimal count, FoodWave foodwave) {
// If bill number > 0 bill has been sent and extra privileges are needed // If bill number > 0 bill has been sent and extra privileges are needed
// to modify. // to modify.
// if (!iscurrent || billnr != null) { // if (!iscurrent || billnr != null) {
// permbean.fatalPermission(BillPermission.WRITE_ALL, // permbean.fatalPermission(BillPermission.WRITE_ALL,
// "User tried to modify bill ", bill, // "User tried to modify bill ", bill,
// "without sufficient permissions"); // "without sufficient permissions");
// } // }
if (bill.getBillLines() == null) { if (bill.getBillLines() == null) {
bill.setBillLines(new ArrayList<BillLine>()); bill.setBillLines(new ArrayList<BillLine>());
} }
bill.getBillLines().add(new BillLine(bill, product, count, foodwave)); bill.getBillLines().add(new BillLine(bill, product, count, foodwave));
for (Discount disc : discountBean.getActiveDiscountsByProduct(product, count, bill.getSentDate(), bill.getUser())) { for (Discount disc : discountBean.getActiveDiscountsByProduct(product, count, bill.getSentDate(), bill.getUser())) {
bill.getBillLines().add(new BillLine(bill, product, disc, count)); bill.getBillLines().add(new BillLine(bill, product, disc, count));
} }
return bill; return bill;
} }
} }
...@@ -315,6 +315,10 @@ public class PlaceBean implements PlaceBeanLocal { ...@@ -315,6 +315,10 @@ public class PlaceBean implements PlaceBeanLocal {
logger.info("Associating buyer {} to place {}", user, gm); logger.info("Associating buyer {} to place {}", user, gm);
associatedToPlace = true; associatedToPlace = true;
gm.setUser(user); gm.setUser(user);
if (user.getGroupMemberships() == null) {
user.setGroupMemberships(new ArrayList<GroupMembership>());
}
user.getGroupMemberships().add(gm);
} }
} }
...@@ -346,7 +350,9 @@ public class PlaceBean implements PlaceBeanLocal { ...@@ -346,7 +350,9 @@ public class PlaceBean implements PlaceBeanLocal {
freePlace = new Place(); freePlace = new Place();
freePlace.setProduct(prod); freePlace.setProduct(prod);
freePlace.setProvidesRole(prod.getProvides()); freePlace.setProvidesRole(prod.getProvides());
freePlace.setName("-");
placeFacade.create(freePlace); placeFacade.create(freePlace);
} else if (prod.getPlaces() != null) { } else if (prod.getPlaces() != null) {
for (Place p : prod.getPlaces()) { for (Place p : prod.getPlaces()) {
if (!p.isTaken()) { if (!p.isTaken()) {
...@@ -359,11 +365,16 @@ public class PlaceBean implements PlaceBeanLocal { ...@@ -359,11 +365,16 @@ public class PlaceBean implements PlaceBeanLocal {
throw new EJBException("Could find a place to be reserved...."); throw new EJBException("Could find a place to be reserved....");
} }
GroupMembership gm = buy(freePlace, pg); GroupMembership gm = buy(freePlace, pg);
if (!associatedToPlace) if (!associatedToPlace)
{ {
logger.info("Associating buyer {} to place {}", user, gm); logger.info("Associating buyer {} to place {}", user, gm);
associatedToPlace = true; associatedToPlace = true;
gm.setUser(user); gm.setUser(user);
if (user.getGroupMemberships() == null) {
user.setGroupMemberships(new ArrayList<GroupMembership>());
}
user.getGroupMemberships().add(gm);
} }
} }
......
...@@ -14,12 +14,14 @@ import org.slf4j.Logger; ...@@ -14,12 +14,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fi.codecrew.moya.facade.AccountEventFacade; import fi.codecrew.moya.facade.AccountEventFacade;
import fi.codecrew.moya.facade.ProductFacade;
import fi.codecrew.moya.model.AccountEvent; import fi.codecrew.moya.model.AccountEvent;
import fi.codecrew.moya.model.Discount; import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.model.DiscountInstance; import fi.codecrew.moya.model.DiscountInstance;
import fi.codecrew.moya.model.EventUser; import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.FoodWave; import fi.codecrew.moya.model.FoodWave;
import fi.codecrew.moya.model.Product; import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.model.ProductFlag;
/** /**
* Session Bean implementation class ProductPBean * Session Bean implementation class ProductPBean
...@@ -35,6 +37,13 @@ public class ProductPBean { ...@@ -35,6 +37,13 @@ public class ProductPBean {
private DiscountBean discountBean; private DiscountBean discountBean;
@EJB @EJB
private AccountEventFacade accounteventfacade; private AccountEventFacade accounteventfacade;
@EJB
private PlaceBean placebean;
@EJB
private ProductFacade productFacade;
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(ProductPBean.class); .getLogger(ProductPBean.class);
...@@ -70,9 +79,12 @@ public class ProductPBean { ...@@ -70,9 +79,12 @@ public class ProductPBean {
BigDecimal quantity, EventUser user, Calendar date, BigDecimal quantity, EventUser user, Calendar date,
FoodWave foodwave) { FoodWave foodwave) {
if (!accounteventfacade.isAttached(product)) {
product = productFacade.reload(product);
}
if (!product.getEvent().equals(user.getEvent())) { if (!product.getEvent().equals(user.getEvent())) {
throw new EJBException( throw new EJBException("Trying to create accountevent for different event in user and product");
"Trying to create accountevent for different event in user and product");
} }
BigDecimal unitPrice = product.getPrice().negate(); BigDecimal unitPrice = product.getPrice().negate();
...@@ -94,12 +106,21 @@ public class ProductPBean { ...@@ -94,12 +106,21 @@ public class ProductPBean {
foodwave.getAccountEvents().add(ret); foodwave.getAccountEvents().add(ret);
} }
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(); List<DiscountInstance> accEventdiscounts = ret.getDiscountInstances();
for (Discount d : discounts) { for (Discount d : discounts) {
// discountsArray.add(discInst); // discountsArray.add(discInst);
// discountinstancefacade.create(discInst); // discountinstancefacade.create(discInst);
accEventdiscounts.add(new DiscountInstance(ret, d)); accEventdiscounts.add(new DiscountInstance(ret, d));
} }
if (product.getAccountEvents() == null) {
product.setAccountEvents(new ArrayList<AccountEvent>());
}
product.getAccountEvents().add(ret);
user.addAccountevent(ret); user.addAccountevent(ret);
accounteventfacade.create(ret); accounteventfacade.create(ret);
......
...@@ -40,7 +40,7 @@ public abstract class GenericFacade<C extends ModelInterface> { ...@@ -40,7 +40,7 @@ public abstract class GenericFacade<C extends ModelInterface> {
public C create(C entity) { public C create(C entity) {
getEm().persist(entity); getEm().persist(entity);
return entity; return entity;
} }
...@@ -289,4 +289,9 @@ public abstract class GenericFacade<C extends ModelInterface> { ...@@ -289,4 +289,9 @@ public abstract class GenericFacade<C extends ModelInterface> {
public void evict(C entity) { public void evict(C entity) {
getEm().getEntityManagerFactory().getCache().evict(getEntityClass(), entity.getId()); getEm().getEntityManagerFactory().getCache().evict(getEntityClass(), entity.getId());
} }
public boolean isAttached(ModelInterface entity) {
return getEm().contains(entity);
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!