2 * Copyright (C) 2017, 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.events.receipt.added.ObservableReceiptAddedEvent;
33 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
34 import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException;
35 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
36 import org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote;
37 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
38 import org.mxchange.jproduct.model.payment.PaymentType;
39 import org.mxchange.jusercore.model.user.User;
42 * An administrative backing bean for receipts
44 * @author Roland Häder<roland@mxchange.org>
46 @Named ("adminReceiptController")
48 public class FinancialAdminReceiptWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptWebRequestController {
53 private static final long serialVersionUID = 595_754_280_374_171L;
56 * Event being fired when administrator has added a new receipt
60 private Event<ObservableReceiptAddedEvent> adminAddedReceiptEvent;
63 * EJB for general financial receipt purposes
65 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceipt!org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote")
66 private FinancialAdminReceiptSessionBeanRemote adminReceiptBean;
71 private String receiptBarCodeNumber;
74 * Recipient issuing company (for example where the user went shopping to)
76 private BranchOffice receiptBranchOffice;
79 * General receipt controller
82 private FinancialsReceiptWebRequestController receiptController;
85 * Date/time the receipt has been issued
87 private Date receiptIssued;
90 * Receipt list controller
93 private FinancialsReceiptListWebViewController receiptListController;
96 * Receipt number (only numbers)
98 private String receiptNumber;
101 * Payment type being used for this receipt
103 private PaymentType receiptPaymentType;
108 private Long receiptRegisterNumber;
113 private Employable receiptSellerEmployee;
118 private Long receiptSequenceNumber;
123 private Long receiptTransactionNumber;
126 * User who "owns" this receipt
128 private User receiptUser;
131 * Default constructor
133 public FinancialAdminReceiptWebRequestBean () {
134 // Call super constructor
139 * Adds the completed receipt to database by calling an EJB business method.
140 * If not all required fields are set, a proper exception is being thrown.
143 public void addReceipt () {
144 // Are all required fields set?
145 if (this.getReceiptBranchOffice() == null) {
147 throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
148 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
149 // It must be an already peristed instance
150 throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
151 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
152 // It must be an already peristed instance
153 throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
154 } else if (this.getReceiptPaymentType() == null) {
156 throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
157 } else if (this.getReceiptIssued() == null) {
159 throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
162 // Prepare receipt instance
163 final BillableReceipt receipt = this.createReceiptInstance();
165 // Is the receipt already there?
166 if (this.receiptListController.isReceiptAdded(receipt)) {
167 // Receipt has already been added
168 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
172 final BillableReceipt updatedReceipt;
174 // All is set, then try to call EJB
177 updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
178 } catch (final ReceiptAlreadyAddedException ex) {
180 throw new FaceletException(ex);
183 // Fire event with updated instance
184 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
191 * Getter for receipt's bar-code number
193 * @return Receipt's bar-code number
195 public String getReceiptBarCodeNumber () {
196 return this.receiptBarCodeNumber;
200 * Setter for receipt's bar-code number
202 * @param receiptBarCodeNumber Receipt's bar-code number
204 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
205 this.receiptBarCodeNumber = receiptBarCodeNumber;
209 * Getter for receipt issuing branch office
211 * @return Receipt issuing branch office
213 public BranchOffice getReceiptBranchOffice () {
214 return this.receiptBranchOffice;
218 * Setter for receipt issuing branch office
220 * @param receiptBranchOffice Receipt issuing branch office
222 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
223 this.receiptBranchOffice = receiptBranchOffice;
227 * Getter for receipt issue date and time
229 * @return Receipt issue date and time
231 @SuppressWarnings ("ReturnOfDateField")
232 public Date getReceiptIssued () {
233 return this.receiptIssued;
237 * Setter for receipt issue date and time
239 * @param receiptIssued Receipt issue date and time
241 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
242 public void setReceiptIssued (final Date receiptIssued) {
243 this.receiptIssued = receiptIssued;
247 * Getter for receipt number
249 * @return Receipt number
251 public String getReceiptNumber () {
252 return this.receiptNumber;
256 * Setter for receipt number
258 * @param receiptNumber Receipt number
260 public void setReceiptNumber (final String receiptNumber) {
261 this.receiptNumber = receiptNumber;
265 * Getter for payment type
267 * @return Payment type
269 public PaymentType getReceiptPaymentType () {
270 return this.receiptPaymentType;
274 * Setter for payment type
276 * @param receiptPaymentType Payment type
278 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
279 this.receiptPaymentType = receiptPaymentType;
283 * Getter for receipt register's number
285 * @return Receipt register's number
287 public Long getReceiptRegisterNumber () {
288 return this.receiptRegisterNumber;
292 * Setter for receipt register's number
294 * @param receiptRegisterNumber Receipt register's number
296 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
297 this.receiptRegisterNumber = receiptRegisterNumber;
301 * Getter for receipt seller employee
303 * @return Receipt seller employee
305 public Employable getReceiptSellerEmployee () {
306 return this.receiptSellerEmployee;
310 * Setter for receipt seller employee
312 * @param receiptSellerEmployee Receipt seller employee
314 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
315 this.receiptSellerEmployee = receiptSellerEmployee;
319 * Getter for receipt sequence number
321 * @return Receipt sequence number
323 public Long getReceiptSequenceNumber () {
324 return this.receiptSequenceNumber;
328 * Setter for receipt sequence number
330 * @param receiptSequenceNumber Receipt sequence number
332 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
333 this.receiptSequenceNumber = receiptSequenceNumber;
337 * Getter for receipt transaction number
339 * @return Receipt transaction number
341 public Long getReceiptTransactionNumber () {
342 return this.receiptTransactionNumber;
346 * Setter for receipt transaction number
348 * @param receiptTransactionNumber Receipt transaction number
350 public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
351 this.receiptTransactionNumber = receiptTransactionNumber;
355 * Getter for user instance
357 * @return User instance
359 public User getReceiptUser () {
360 return this.receiptUser;
364 * Setter for user instance
366 * @param receiptUser User instance
368 public void setReceiptUser (final User receiptUser) {
369 this.receiptUser = receiptUser;
375 private void clear () {
377 this.setReceiptBarCodeNumber(null);
378 this.setReceiptBranchOffice(null);
379 this.setReceiptIssued(null);
380 this.setReceiptNumber(null);
381 this.setReceiptPaymentType(null);
382 this.setReceiptRegisterNumber(null);
383 this.setReceiptSellerEmployee(null);
384 this.setReceiptSequenceNumber(null);
385 this.setReceiptTransactionNumber(null);
386 this.setReceiptUser(null);
390 * Creates a new instance from all available data of this bean.
392 * @return Receipt instance
394 private BillableReceipt createReceiptInstance () {
395 // Create new instance with minimum required data
396 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
399 receipt.setReceiptUser(this.getReceiptUser());
400 receipt.setReceiptNumber(this.getReceiptNumber());
401 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
402 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
403 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
404 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
405 receipt.setReceiptTransactionNumber(this.getReceiptTransactionNumber());
407 // Return prepared instance