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));
170 // Return redirect outcome
171 return "add_receipt_item?faces-redirect=true"; //NOI18N
175 * Getter for receipt's bar-code number
177 * @return Receipt's bar-code number
179 public Long getReceiptBarCodeNumber () {
180 return this.receiptBarCodeNumber;
184 * Setter for receipt's bar-code number
186 * @param receiptBarCodeNumber Receipt's bar-code number
188 public void setReceiptBarCodeNumber (final Long receiptBarCodeNumber) {
189 this.receiptBarCodeNumber = receiptBarCodeNumber;
193 * Getter for receipt issuing branch office
195 * @return Receipt issuing branch office
197 public BranchOffice getReceiptBranchOffice () {
198 return this.receiptBranchOffice;
202 * Setter for receipt issuing branch office
204 * @param receiptBranchOffice Receipt issuing branch office
206 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
207 this.receiptBranchOffice = receiptBranchOffice;
211 * Getter for receipt issue date and time
213 * @return Receipt issue date and time
215 @SuppressWarnings ("ReturnOfDateField")
216 public Date getReceiptIssued () {
217 return this.receiptIssued;
221 * Setter for receipt issue date and time
223 * @param receiptIssued Receipt issue date and time
225 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
226 public void setReceiptIssued (final Date receiptIssued) {
227 this.receiptIssued = receiptIssued;
231 * Getter for receipt number
233 * @return Receipt number
235 public Long getReceiptNumber () {
236 return this.receiptNumber;
240 * Setter for receipt number
242 * @param receiptNumber Receipt number
244 public void setReceiptNumber (final Long receiptNumber) {
245 this.receiptNumber = receiptNumber;
249 * Getter for payment type
251 * @return Payment type
253 public PaymentType getReceiptPaymentType () {
254 return this.receiptPaymentType;
258 * Setter for payment type
260 * @param receiptPaymentType Payment type
262 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
263 this.receiptPaymentType = receiptPaymentType;
267 * Getter for receipt register's number
269 * @return Receipt register's number
271 public Long getReceiptRegisterNumber () {
272 return this.receiptRegisterNumber;
276 * Setter for receipt register's number
278 * @param receiptRegisterNumber Receipt register's number
280 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
281 this.receiptRegisterNumber = receiptRegisterNumber;
285 * Getter for receipt seller employee
287 * @return Receipt seller employee
289 public Employee getReceiptSellerEmployee () {
290 return this.receiptSellerEmployee;
294 * Setter for receipt seller employee
296 * @param receiptSellerEmployee Receipt seller employee
298 public void setReceiptSellerEmployee (final Employee receiptSellerEmployee) {
299 this.receiptSellerEmployee = receiptSellerEmployee;
303 * Getter for user instance
305 * @return User instance
307 public User getReceiptUser () {
308 return this.receiptUser;
312 * Setter for user instance
314 * @param receiptUser User instance
316 public void setReceiptUser (final User receiptUser) {
317 this.receiptUser = receiptUser;
321 * Creates a new instance from all available data of this bean.
323 * @return Receipt instance
325 private BillableReceipt createReceiptInstance () {
326 // Create new instance with minimum required data
327 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
330 receipt.setReceiptUser(this.getReceiptUser());
331 receipt.setReceiptNumber(this.getReceiptNumber());
332 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
333 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
334 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
336 // Return prepared instance