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 {
private EventUserFacade eventUserFacade;
@EJB
private ProductPBean productPBean;
@EJB
private DiscountBean discountBean;
......@@ -232,7 +232,7 @@ public class BillBean implements BillBeanLocal {
ac.setEventTime(when);
ac.setBill(bill);
ac.setSeller(permbean.getCurrentUser());
bill.setAccountEvent(ac);
bill.setPaidDate(when.getTime());
......@@ -244,10 +244,6 @@ public class BillBean implements BillBeanLocal {
if (prod != null && prod.getProductFlags().contains(ProductFlag.PREPAID_INSTANT_CREATE)) {
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());
logger.info("Created ac from product. {}, userproducts {}", ac2, user.getAccountEvents().size());
......@@ -311,7 +307,7 @@ 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);
......@@ -319,30 +315,25 @@ public class BillBean implements BillBeanLocal {
@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;
// 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;
}
}
......@@ -315,6 +315,10 @@ public class PlaceBean implements PlaceBeanLocal {
logger.info("Associating buyer {} to place {}", user, gm);
associatedToPlace = true;
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 {
freePlace = new Place();
freePlace.setProduct(prod);
freePlace.setProvidesRole(prod.getProvides());
freePlace.setName("-");
placeFacade.create(freePlace);
} else if (prod.getPlaces() != null) {
for (Place p : prod.getPlaces()) {
if (!p.isTaken()) {
......@@ -359,11 +365,16 @@ public class PlaceBean implements PlaceBeanLocal {
throw new EJBException("Could find a place to be reserved....");
}
GroupMembership gm = buy(freePlace, pg);
if (!associatedToPlace)
{
logger.info("Associating buyer {} to place {}", user, gm);
associatedToPlace = true;
gm.setUser(user);
if (user.getGroupMemberships() == null) {
user.setGroupMemberships(new ArrayList<GroupMembership>());
}
user.getGroupMemberships().add(gm);
}
}
......
......@@ -14,12 +14,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.facade.AccountEventFacade;
import fi.codecrew.moya.facade.ProductFacade;
import fi.codecrew.moya.model.AccountEvent;
import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.model.DiscountInstance;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.FoodWave;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.model.ProductFlag;
/**
* Session Bean implementation class ProductPBean
......@@ -35,6 +37,13 @@ public class ProductPBean {
private DiscountBean discountBean;
@EJB
private AccountEventFacade accounteventfacade;
@EJB
private PlaceBean placebean;
@EJB
private ProductFacade productFacade;
private static final Logger logger = LoggerFactory
.getLogger(ProductPBean.class);
......@@ -70,9 +79,12 @@ public class ProductPBean {
BigDecimal quantity, EventUser user, Calendar date,
FoodWave foodwave) {
if (!accounteventfacade.isAttached(product)) {
product = productFacade.reload(product);
}
if (!product.getEvent().equals(user.getEvent())) {
throw new EJBException(
"Trying to create accountevent for different event in user and product");
throw new EJBException("Trying to create accountevent for different event in user and product");
}
BigDecimal unitPrice = product.getPrice().negate();
......@@ -94,12 +106,21 @@ 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)) {
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>());
}
product.getAccountEvents().add(ret);
user.addAccountevent(ret);
accounteventfacade.create(ret);
......
......@@ -40,7 +40,7 @@ public abstract class GenericFacade<C extends ModelInterface> {
public C create(C entity) {
getEm().persist(entity);
return entity;
}
......@@ -289,4 +289,9 @@ public abstract class GenericFacade<C extends ModelInterface> {
public void evict(C entity) {
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!