2 * Copyright (C) 2016 - 2018 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.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.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.
128 * @return Link outcome
130 public String addReceipt () {
131 // Are all required fields set?
132 if (!this.userLoginController.isUserLoggedIn()) {
134 throw new IllegalStateException("User is not logged-in"); //NOI18N
135 } else if (this.getReceiptBranchOffice() == null) {
137 throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
138 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
139 // It must be an already peristed instance
140 throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
141 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
142 // It must be an already peristed instance
143 throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
144 } else if (this.getReceiptPaymentType() == null) {
146 throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
147 } else if (this.getReceiptIssued() == null) {
149 throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
152 // Prepare receipt instance
153 final BillableReceipt receipt = this.createReceiptInstance();
155 // Is the receipt already there?
156 if (this.receiptListController.isReceiptAdded(receipt)) {
157 // Receipt has already been added
158 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
162 final BillableReceipt updatedReceipt;
164 // All is set, then try to call EJB
167 updatedReceipt = this.receiptBean.addReceipt(receipt);
168 } catch (final ReceiptAlreadyAddedException ex) {
170 throw new FaceletException(ex);
173 // Fire event with updated instance
174 this.addedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
179 // Return redirect outcome
180 return "admin_list_receipts?faces-redirect=true"; //NOI18N
184 * Getter for receipt's bar-code number
186 * @return Receipt's bar-code number
188 public String getReceiptBarCodeNumber () {
189 return this.receiptBarCodeNumber;
193 * Setter for receipt's bar-code number
195 * @param receiptBarCodeNumber Receipt's bar-code number
197 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
198 this.receiptBarCodeNumber = receiptBarCodeNumber;
202 * Getter for receipt issuing branch office
204 * @return Receipt issuing branch office
206 public BranchOffice getReceiptBranchOffice () {
207 return this.receiptBranchOffice;
211 * Setter for receipt issuing branch office
213 * @param receiptBranchOffice Receipt issuing branch office
215 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
216 this.receiptBranchOffice = receiptBranchOffice;
220 * Getter for receipt issue date and time
222 * @return Receipt issue date and time
224 @SuppressWarnings ("ReturnOfDateField")
225 public Date getReceiptIssued () {
226 return this.receiptIssued;
230 * Setter for receipt issue date and time
232 * @param receiptIssued Receipt issue date and time
234 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
235 public void setReceiptIssued (final Date receiptIssued) {
236 this.receiptIssued = receiptIssued;
240 * Getter for receipt number
242 * @return Receipt number
244 public String getReceiptNumber () {
245 return this.receiptNumber;
249 * Setter for receipt number
251 * @param receiptNumber Receipt number
253 public void setReceiptNumber (final String receiptNumber) {
254 this.receiptNumber = receiptNumber;
258 * Getter for payment type
260 * @return Payment type
262 public PaymentType getReceiptPaymentType () {
263 return this.receiptPaymentType;
267 * Setter for payment type
269 * @param receiptPaymentType Payment type
271 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
272 this.receiptPaymentType = receiptPaymentType;
276 * Getter for receipt register's number
278 * @return Receipt register's number
280 public Long getReceiptRegisterNumber () {
281 return this.receiptRegisterNumber;
285 * Setter for receipt register's number
287 * @param receiptRegisterNumber Receipt register's number
289 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
290 this.receiptRegisterNumber = receiptRegisterNumber;
294 * Getter for receipt seller employee
296 * @return Receipt seller employee
298 public Employable getReceiptSellerEmployee () {
299 return this.receiptSellerEmployee;
303 * Setter for receipt seller employee
305 * @param receiptSellerEmployee Receipt seller employee
307 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
308 this.receiptSellerEmployee = receiptSellerEmployee;
314 private void clear () {
316 this.setReceiptBarCodeNumber(null);
317 this.setReceiptBranchOffice(null);
318 this.setReceiptIssued(null);
319 this.setReceiptNumber(null);
320 this.setReceiptPaymentType(null);
321 this.setReceiptRegisterNumber(null);
322 this.setReceiptSellerEmployee(null);
326 * Returns a fully created receipt instance (except primary key, of course)
328 * @return Receipt instance
330 private BillableReceipt createReceiptInstance () {
331 // Init receipt instance
332 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.userLoginController.getLoggedInUser(), this.getReceiptIssued());
334 // Set optional fields
335 receipt.setReceiptNumber(this.getReceiptNumber());
336 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
337 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
338 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());