BillPBean.java 5.02 KB
package fi.codecrew.moya.beans;

import java.util.Calendar;

import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.codecrew.moya.enums.apps.ShopPermission;
import fi.codecrew.moya.exceptions.BillException;
import fi.codecrew.moya.exceptions.BillExceptionAlreadyPaid;
import fi.codecrew.moya.exceptions.BillExceptionNotEnoughtCredits;
import fi.codecrew.moya.facade.BillFacade;
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.EventUser;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.model.ProductFlag;
import fi.codecrew.moya.utilities.moyamessage.MoyaEventType;

/**
 * EJB private bean for bill functions Does not check allowed roles! All
 * rolechecks should be done outside this. class
 * 
 * @author tuomari
 *
 */

@Stateless
@LocalBean
public class BillPBean {
	@EJB
	private LoggingBeanLocal logbean;
	@EJB
	private BillFacade billFacade;
	@EJB
	private ProductBean productBean;
	@EJB
	private PermissionBean permbean;
	@EJB
	private ProductPBean productPBean;
	@EJB
	private EventUserFacade eventUserFacade;
	private static final Logger logger = LoggerFactory.getLogger(BillPBean.class);

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	private Bill markPaidSafeTransaction(Bill bill, Calendar when, boolean useCredits) throws BillException {
		bill = billFacade.reload(bill);

		if (bill.getAccountEvent() != null || bill.getPaidDate() != null) {
			logbean.sendMessage(MoyaEventType.BILL_ERROR, permbean.getCurrentUser(), "Tried to mark already paid bill as paid: " + bill.getId());
			// logbean.logMessage(SecurityLogType.bill, permbean.getCurrentUser(), "Trying to doublemark bill paid", bill.getId());
			throw new BillExceptionAlreadyPaid("Trying to mark bill paid, already paid, BillID: " + bill.getId());
		}

		Product creditproduct = productBean.findCreditProduct();

		EventUser user = bill.getUser();

		if (useCredits) {

			// check if there is enought credits
			if (bill.getUser().getAccountBalance().compareTo(bill.getTotalPrice()) < 1) {
				logbean.sendMessage(MoyaEventType.BILL_ERROR, permbean.getCurrentUser(), "Trying to pay bill with accountevents, and there is no saldo, billid: " + bill.getId());

				// logbean.logMessage(SecurityLogType.bill, permbean.getCurrentUser(), "Trying to pay bill with accountevents, and there is no saldo, billid: ", bill.getId());
				throw new BillExceptionNotEnoughtCredits("There is not enought credits to pay. , BillID: " + bill.getId());
			}
		} else {

			AccountEvent ac = productBean.createAccountEvent(creditproduct, bill.totalPrice(), user);

			logger.info("Created creditentry. {}, userproducts {}", ac, user.getAccountEvents().size());
			ac.setEventTime(when);
			ac.setBill(bill);
			ac.setSeller(permbean.getCurrentUser());

			bill.setAccountEvent(ac);
		}

		bill.setPaidDate(when.getTime());

		return bill;
	}

	public Bill markPaid(Bill bill, Calendar when, boolean useCredits) throws BillException {

		bill = markPaidSafeTransaction(bill, when, useCredits);

		EventUser user = bill.getUser();

		if (bill.isFoowavePaymentOver() && !permbean.hasPermission(ShopPermission.MANAGE_FOODWAVES))
		{
			logbean.sendMessage(MoyaEventType.BILL_ERROR, permbean.getCurrentUser(), "FoodwaveClosed and marking bill for it paid. Billid: " + bill.getId());
			throw new EJBException("Trying to mark paid a closed or left foodwave");
		}

		// bill = billFacade.merge(bill);

		for (BillLine bl : bill.getBillLines()) {

			Product prod = bl.getLineProduct();
			if (prod != null && !prod.getProductFlags().contains(ProductFlag.PREPAID_CREDIT)) {

				logger.debug("Creating Bill prepaidInstant product {}, {}", prod.getName(), bl.getQuantity());

				AccountEvent ac2 = productPBean.createAccountEvent(prod, null,bl.getQuantity(), user, bill.getSentDate(), bl.getFoodwave());
				logger.info("Created ac from product. {}, userproducts {}", ac2, user.getAccountEvents().size());

				ac2.setSeller(permbean.getCurrentUser());

			}
		}

		billFacade.flush();

		/*
		MailMessage msg = new MailMessage();

		String subject = MessageFormat.format(eventbean.getPropertyString(LanEventPropertyKey.BILL_PAID_MAIL_SUBJECT), user.getEvent().getName());
		String content = MessageFormat.format(eventbean.getPropertyString(LanEventPropertyKey.BILL_PAID_MAIL_CONTENT), (bill.getBillNumber() == null) ? "----" : bill.getBillNumber().toString());
		logger.info("Bill mail subject: {}, content {}", subject, content);
		msg.setSubject(subject);
		msg.setMessage(content);
		msg.setTo(bill.getUser().getUser());
		utilbean.sendMail(msg);
		*/

		eventUserFacade.flush();
		logbean.sendMessage(MoyaEventType.BILL_PAID, permbean.getCurrentUser(), "Marking bill paid, for user: ", bill.getUser().getId(), "BillId: ", bill.getId());
		eventUserFacade.evict(bill.getUser());

		return bill;
	}
}