BillPBean.java
5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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;
}
}