BillBean.java
7.03 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package fi.insomnia.bortal.beans;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.insomnia.bortal.beanutil.PdfPrinter;
import fi.insomnia.bortal.bortal.views.BillSummary;
import fi.insomnia.bortal.enums.apps.BillPermission;
import fi.insomnia.bortal.facade.BillFacade;
import fi.insomnia.bortal.facade.BillLineFacade;
import fi.insomnia.bortal.facade.UserFacade;
import fi.insomnia.bortal.model.AccountEvent;
import fi.insomnia.bortal.model.Bill;
import fi.insomnia.bortal.model.BillLine;
import fi.insomnia.bortal.model.LanEvent;
import fi.insomnia.bortal.model.Product;
import fi.insomnia.bortal.model.User;
import fi.insomnia.bortal.util.MailMessage;
import fi.insomnia.bortal.utilities.I18n;
/**
* Session Bean implementation class BillBean
*/
@Stateless
@LocalBean
@DeclareRoles({ "USER", "BILL/WRITE_ALL", "BILL/READ_ALL", "BILL/CREATE_BILL" })
public class BillBean implements BillBeanLocal {
private static final Logger logger = LoggerFactory.getLogger(BillBean.class);
@EJB
private BillFacade billFacade;
@EJB
private EventBeanLocal eventbean;
@EJB
private BillLineFacade billLineFacade;
@PersistenceContext
private EntityManager em;
@EJB
private PermissionBeanLocal permbean;
@EJB
private ProductBeanLocal productBean;
@EJB
private PlaceBean placebean;
@EJB
private PermissionBeanLocal permissionbean;
@EJB
private UserFacade userfacade;
@EJB
private UtilBean utilbean;
/**
* Default constructor.
*/
public BillBean() {
// TODO Auto-generated constructor stub
}
@Override
@RolesAllowed("USER")
public Bill findById(int id) {
LanEvent event = eventbean.getCurrentEvent();
if (id <= 0) {
return null;
}
Bill bill = billFacade.find(event, id);
User currentuser = permbean.getCurrentUser();
logger.debug("bill {} user {}", bill, currentuser);
if (bill != null && !currentuser.equals(bill.getUser()) && !permbean.hasPermission(BillPermission.READ_ALL)) {
bill = null;
}
return bill;
}
@Override
public void getPdfBillStream(Bill bill, OutputStream ostream) {
if (bill == null) {
return;
}
if (bill.getBillNumber() == null || bill.getBillNumber() <= 0) {
generateBillNumber(bill);
}
new PdfPrinter(bill).output(ostream);
}
private void generateBillNumber(Bill bill) {
if (bill.getBillNumber() == null || bill.getBillNumber() == 0) {
LanEvent currEvent = eventbean.getCurrentEvent();
Integer billnr = billFacade.getBiggestBillNumber();
if (billnr == null || billnr < currEvent.getNextBillNumber()) {
billnr = currEvent.getNextBillNumber();
} else {
++billnr;
}
bill.setBillNumber(billnr);
billFacade.merge(bill);
}
}
// @Override
// public Bill createEmptyBill(User shoppingUser) throws
// PermissionDeniedException {
// if (permbean.isCurrentUser(shoppingUser)) {
// permbean.fatalPermission(BillPermission.CREATE_BILL,
// "No permission to create empty bill for self");
// } else {
// permbean.fatalPermission(BillPermission.WRITE_ALL,
// "Trying to create bill to someone else without sufficient permission");
// }
//
// LanEvent event = eventbean.getCurrentEvent();
// Bill ret = new Bill(event, shoppingUser);
// billFacade.create(ret);
// ret.setUser(shoppingUser);
// em.flush();
// logger.debug("Created bill with id {} and user {}", ret.getId(),
// ret.getUser());
// return ret;
// }
// @Override
// @RolesAllowed("SHOP/EXECUTE")
// public BillLine addProductToBill(Bill bill, Product product, BigDecimal
// count) throws PermissionDeniedException {
//
// // If bill number > 0 bill has been sent and extra privileges are needed
// // to modify.
// boolean iscurrent = permissionbean.isCurrentUser(bill.getUser());
// Integer billnr = bill.getBillNumber();
// if (!iscurrent || billnr != null) {
// permbean.fatalPermission(BillPermission.WRITE_ALL,
// "User tried to modify bill ", bill, "without sufficient permissions");
// }
// BillLine line = new BillLine(bill, product.getName(),
// product.getUnitName(), count, product.getPrice(), product.getVat());
// line.setLineProduct(product);
// billLineFacade.create(line);
//
// List<Discount> discounts = productBean.getActiveDiscounts(product,
// count);
//
// for (Discount disc : discounts) {
// BigDecimal unitPrice =
// product.getPrice().subtract(product.getPrice().multiply(disc.getPercentage())).negate().setScale(2,
// RoundingMode.HALF_UP);
// BigDecimal vatPrice =
// product.getVat().subtract(product.getVat().multiply(disc.getPercentage())).negate().setScale(2,
// RoundingMode.HALF_UP);
//
// BillLine discountLine = new BillLine(bill, disc.getShortdesc(),
// product.getUnitName(), count, unitPrice, vatPrice);
// billLineFacade.create(discountLine);
//
// }
//
// em.flush();
// return line;
// }
@Override
@RolesAllowed("BILL/READ_ALL")
public List<Bill> findAll() {
return billFacade.findAll(eventbean.getCurrentEvent());
}
@Override
@RolesAllowed("BILL/READ_ALL")
public Collection<BillSummary> getBillLineSummary() {
Collection<BillSummary> ret = billLineFacade.getLineSummary(eventbean.getCurrentEvent());
return ret;
}
@Override
@RolesAllowed("BILL/WRITE_ALL")
public void markPaid(Bill bill, Calendar when) {
Product creditproduct = productBean.findCreditProduct();
AccountEvent ac = productBean.createAccountEvent(creditproduct, bill.totalPrice(), bill.getUser());
ac.setDelivered(when);
ac.setEventTime(when);
ac.setBill(bill);
ac.setSeller(permbean.getCurrentUser());
bill.setAccountEvent(ac);
bill.setPaidDate(when);
bill = billFacade.merge(bill);
for (BillLine bl : bill.getBillLines()) {
Product prod = bl.getLineProduct();
if (prod != null) {
if (prod.isPrepaidInstant()) {
logger.debug("Creating Bill prepaidInstant product {}, {}", prod.getName(), bl.getQuantity());
if (prod.getPlaces().size() > 0) {
placebean.lockPlaceProduct(bill.getUser(), prod, bl.getQuantity());
}
productBean.createAccountEvent(prod, bl.getQuantity(), bill.getUser());
}
}
}
MailMessage msg = new MailMessage();
msg.setSubject(I18n.get("bill.billMarkedPaidMail.subject"));
msg.setMessage(I18n.get("bill.billMarkedPaidMail.message", (bill.getBillNumber() == null) ? "----" : bill.getBillNumber().toString()));
msg.setTo(bill.getUser());
utilbean.sendMail(msg);
}
@Override
@RolesAllowed("BILL/CREATE_BILL")
public void createBill(Bill bill) throws PermissionDeniedException {
if (!permbean.isCurrentUser(bill.getUser())) {
permbean.fatalPermission(BillPermission.WRITE_ALL, "Not enought rights to create bill for user ", bill.getUser());
}
User user = userfacade.find(bill.getUser().getId());
if (user.getBills() == null) {
user.setBills(new ArrayList<Bill>());
}
user.getBills().add(bill);
}
}