2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.FacesException;
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.Employable;
30 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
31 import org.mxchange.jfinancials.beans.financial.model.receipt.list.FinancialsReceiptListWebViewController;
32 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
33 import org.mxchange.jfinancials.events.receipt.added.ObservableReceiptAddedEvent;
34 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
35 import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException;
36 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
37 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
38 import org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote;
39 import org.mxchange.jproduct.model.payment.PaymentType;
42 * A general financial receipt bean (controller)
44 * @author Roland Häder<roland@mxchange.org>
46 @Named ("receiptController")
48 public class FinancialsReceiptWebRequestBean extends BaseFinancialsBean implements FinancialsReceiptWebRequestController {
53 private static final long serialVersionUID = 56_189_028_928_371L;
56 * Event being fired when user has added a new receipt
60 private Event<ObservableReceiptAddedEvent> addedReceiptEvent;
65 private String receiptBarCodeNumber;
68 * EJB for general financial receipt purposes
70 @EJB (lookup = "java:global/jfinancials-ejb/financialReceipt!org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote")
71 private FinancialReceiptSessionBeanRemote receiptBean;
74 * Recipient issuing company (for example where the user went shopping to)
76 private BranchOffice receiptBranchOffice;
79 * Date/time the receipt has been issued
81 private Date receiptIssued;
84 * Receipt list controller
87 private FinancialsReceiptListWebViewController receiptListController;
90 * Receipt number (only numbers)
92 private String receiptNumber;
95 * Payment type being used for this receipt
97 private PaymentType receiptPaymentType;
102 private Long receiptRegisterNumber;
107 private Employable receiptSellerEmployee;
113 private FinancialsUserLoginWebSessionController userLoginController;
118 @SuppressWarnings ("CollectionWithoutInitialCapacity")
119 public FinancialsReceiptWebRequestBean () {
120 // Call super constructor
125 * Adds the completed receipt to database by calling an EJB business method.
126 * If not all required fields are set, a proper exception is being thrown.
129 public void addReceipt () {
130 // Are all required fields set?
131 if (!this.userLoginController.isUserLoggedIn()) {
133 throw new IllegalStateException("User is not logged-in"); //NOI18N
134 } else if (this.getReceiptBranchOffice() == null) {
136 throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
137 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
138 // It must be an already peristed instance
139 throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
140 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
141 // It must be an already peristed instance
142 throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
143 } else if (this.getReceiptPaymentType() == null) {
145 throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
146 } else if (this.getReceiptIssued() == null) {
148 throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
151 // Prepare receipt instance
152 final BillableReceipt receipt = this.createReceiptInstance();
154 // Is the receipt already there?
155 if (this.receiptListController.isReceiptAdded(receipt)) {
156 // Receipt has already been added
157 throw new FacesException(MessageFormat.format("Receipt for receiptBranchOffice={0},receiptIssued={1},receiptNumber={2} has already been added.", this.getReceiptBranchOffice().getBranchId(), this.getReceiptIssued().toString(), this.getReceiptNumber())); //NOI18N
161 final BillableReceipt updatedReceipt;
163 // All is set, then try to call EJB
166 updatedReceipt = this.receiptBean.addReceipt(receipt);
167 } catch (final ReceiptAlreadyAddedException ex) {
169 throw new FacesException(ex);
172 // Fire event with updated instance
173 this.addedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
180 * Getter for receipt's bar-code number
182 * @return Receipt's bar-code number
184 public String getReceiptBarCodeNumber () {
185 return this.receiptBarCodeNumber;
189 * Setter for receipt's bar-code number
191 * @param receiptBarCodeNumber Receipt's bar-code number
193 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
194 this.receiptBarCodeNumber = receiptBarCodeNumber;
198 * Getter for receipt issuing branch office
200 * @return Receipt issuing branch office
202 public BranchOffice getReceiptBranchOffice () {
203 return this.receiptBranchOffice;
207 * Setter for receipt issuing branch office
209 * @param receiptBranchOffice Receipt issuing branch office
211 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
212 this.receiptBranchOffice = receiptBranchOffice;
216 * Getter for receipt issue date and time
218 * @return Receipt issue date and time
220 @SuppressWarnings ("ReturnOfDateField")
221 public Date getReceiptIssued () {
222 return this.receiptIssued;
226 * Setter for receipt issue date and time
228 * @param receiptIssued Receipt issue date and time
230 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
231 public void setReceiptIssued (final Date receiptIssued) {
232 this.receiptIssued = receiptIssued;
236 * Getter for receipt number
238 * @return Receipt number
240 public String getReceiptNumber () {
241 return this.receiptNumber;
245 * Setter for receipt number
247 * @param receiptNumber Receipt number
249 public void setReceiptNumber (final String receiptNumber) {
250 this.receiptNumber = receiptNumber;
254 * Getter for payment type
256 * @return Payment type
258 public PaymentType getReceiptPaymentType () {
259 return this.receiptPaymentType;
263 * Setter for payment type
265 * @param receiptPaymentType Payment type
267 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
268 this.receiptPaymentType = receiptPaymentType;
272 * Getter for receipt register's number
274 * @return Receipt register's number
276 public Long getReceiptRegisterNumber () {
277 return this.receiptRegisterNumber;
281 * Setter for receipt register's number
283 * @param receiptRegisterNumber Receipt register's number
285 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
286 this.receiptRegisterNumber = receiptRegisterNumber;
290 * Getter for receipt seller employee
292 * @return Receipt seller employee
294 public Employable getReceiptSellerEmployee () {
295 return this.receiptSellerEmployee;
299 * Setter for receipt seller employee
301 * @param receiptSellerEmployee Receipt seller employee
303 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
304 this.receiptSellerEmployee = receiptSellerEmployee;
310 private void clear () {
312 this.setReceiptBarCodeNumber(null);
313 this.setReceiptBranchOffice(null);
314 this.setReceiptIssued(null);
315 this.setReceiptNumber(null);
316 this.setReceiptPaymentType(null);
317 this.setReceiptRegisterNumber(null);
318 this.setReceiptSellerEmployee(null);
322 * Returns a fully created receipt instance (except primary key, of course)
324 * @return Receipt instance
326 private BillableReceipt createReceiptInstance () {
327 // Init receipt instance
328 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.userLoginController.getLoggedInUser(), this.getReceiptIssued());
330 // Set optional fields
331 receipt.setReceiptNumber(this.getReceiptNumber());
332 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
333 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
334 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());