2 * Copyright (C) 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jfinancials.beans.financial.model.receipt;
19 import java.text.MessageFormat;
20 import java.util.Date;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
29 import org.mxchange.jcontactsbusiness.model.employee.Employee;
30 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
31 import org.mxchange.jfinancials.events.receipt.added.ObservableReceiptAddedEvent;
32 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
33 import org.mxchange.jfinancials.exceptions.ReceiptAlreadyAddedException;
34 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
35 import org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote;
36 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
37 import org.mxchange.jproduct.model.payment.PaymentType;
38 import org.mxchange.jusercore.model.user.User;
41 * An administrative backing bean for receipts
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("adminReceiptController")
47 public class FinancialAdminReceiptWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptWebRequestController {
52 private static final long serialVersionUID = 595_754_280_374_171L;
55 * Event being fired when administrator has added a new receipt
59 private Event<ObservableReceiptAddedEvent> adminAddedReceiptEvent;
62 * EJB for general financial receipt purposes
64 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceipt!org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote")
65 private FinancialAdminReceiptSessionBeanRemote adminFinancialBean;
70 private Long receiptBarCodeNumber;
73 * Recipient issuing company (for example where the user went shopping to)
75 private BranchOffice receiptBranchOffice;
78 * General receipt controller
81 private FinancialsReceiptWebRequestController receiptController;
84 * Date/time the receipt has been issued
86 private Date receiptIssued;
89 * Receipt number (only numbers)
91 private Long receiptNumber;
94 * Payment type being used for this receipt
96 private PaymentType receiptPaymentType;
101 private Long receiptRegisterNumber;
106 private Employee receiptSellerEmployee;
109 * User who "owns" this receipt
111 private User receiptUser;
114 * Default constructor
116 public FinancialAdminReceiptWebRequestBean () {
117 // Call super constructor
122 * Adds the completed receipt to database by calling an EJB business method.
123 * If not all required fields are set, a proper exception is being thrown.
125 * @return Link outcome
127 public String addReceipt () {
128 // Are all required fields set?
129 if (this.getReceiptBranchOffice() == null) {
131 throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
132 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
133 // It must be an already peristed instance
134 throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
135 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
136 // It must be an already peristed instance
137 throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
138 } else if (this.getReceiptPaymentType() == null) {
140 throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
141 } else if (this.getReceiptIssued() == null) {
143 throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
146 // Prepare receipt instance
147 final BillableReceipt receipt = this.createReceiptInstance();
149 // Is the receipt already there?
150 if (this.receiptController.isReceiptAdded(receipt)) {
151 // Receipt has already been added
152 throw new FaceletException(MessageFormat.format("Receipt for receiptBranchOffice={0},receiptIssued={1},receiptNumber={2} has already been added.", this.getReceiptBranchOffice().getBranchId(), this.getReceiptIssued().toString(), this.getReceiptNumber())); //NOI18N
156 final BillableReceipt updatedReceipt;
158 // All is set, then try to call EJB
161 updatedReceipt = this.adminFinancialBean.addReceipt(receipt);
162 } catch (final ReceiptAlreadyAddedException ex) {
164 throw new FaceletException(ex);
167 // Fire event with updated instance
168 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
173 // Return redirect outcome
174 return "add_receipt_item?faces-redirect=true"; //NOI18N
178 * Getter for receipt's bar-code number
180 * @return Receipt's bar-code number
182 public Long getReceiptBarCodeNumber () {
183 return this.receiptBarCodeNumber;
187 * Setter for receipt's bar-code number
189 * @param receiptBarCodeNumber Receipt's bar-code number
191 public void setReceiptBarCodeNumber (final Long receiptBarCodeNumber) {
192 this.receiptBarCodeNumber = receiptBarCodeNumber;
196 * Getter for receipt issuing branch office
198 * @return Receipt issuing branch office
200 public BranchOffice getReceiptBranchOffice () {
201 return this.receiptBranchOffice;
205 * Setter for receipt issuing branch office
207 * @param receiptBranchOffice Receipt issuing branch office
209 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
210 this.receiptBranchOffice = receiptBranchOffice;
214 * Getter for receipt issue date and time
216 * @return Receipt issue date and time
218 @SuppressWarnings ("ReturnOfDateField")
219 public Date getReceiptIssued () {
220 return this.receiptIssued;
224 * Setter for receipt issue date and time
226 * @param receiptIssued Receipt issue date and time
228 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
229 public void setReceiptIssued (final Date receiptIssued) {
230 this.receiptIssued = receiptIssued;
234 * Getter for receipt number
236 * @return Receipt number
238 public Long getReceiptNumber () {
239 return this.receiptNumber;
243 * Setter for receipt number
245 * @param receiptNumber Receipt number
247 public void setReceiptNumber (final Long receiptNumber) {
248 this.receiptNumber = receiptNumber;
252 * Getter for payment type
254 * @return Payment type
256 public PaymentType getReceiptPaymentType () {
257 return this.receiptPaymentType;
261 * Setter for payment type
263 * @param receiptPaymentType Payment type
265 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
266 this.receiptPaymentType = receiptPaymentType;
270 * Getter for receipt register's number
272 * @return Receipt register's number
274 public Long getReceiptRegisterNumber () {
275 return this.receiptRegisterNumber;
279 * Setter for receipt register's number
281 * @param receiptRegisterNumber Receipt register's number
283 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
284 this.receiptRegisterNumber = receiptRegisterNumber;
288 * Getter for receipt seller employee
290 * @return Receipt seller employee
292 public Employee getReceiptSellerEmployee () {
293 return this.receiptSellerEmployee;
297 * Setter for receipt seller employee
299 * @param receiptSellerEmployee Receipt seller employee
301 public void setReceiptSellerEmployee (final Employee receiptSellerEmployee) {
302 this.receiptSellerEmployee = receiptSellerEmployee;
306 * Getter for user instance
308 * @return User instance
310 public User getReceiptUser () {
311 return this.receiptUser;
315 * Setter for user instance
317 * @param receiptUser User instance
319 public void setReceiptUser (final User receiptUser) {
320 this.receiptUser = receiptUser;
326 private void clear () {
328 this.setReceiptBarCodeNumber(null);
329 this.setReceiptBranchOffice(null);
330 this.setReceiptIssued(null);
331 this.setReceiptNumber(null);
332 this.setReceiptPaymentType(null);
333 this.setReceiptRegisterNumber(null);
334 this.setReceiptSellerEmployee(null);
335 this.setReceiptUser(null);
339 * Creates a new instance from all available data of this bean.
341 * @return Receipt instance
343 private BillableReceipt createReceiptInstance () {
344 // Create new instance with minimum required data
345 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
348 receipt.setReceiptUser(this.getReceiptUser());
349 receipt.setReceiptNumber(this.getReceiptNumber());
350 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
351 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
352 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
354 // Return prepared instance