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;
121 * User who "owns" this receipt
123 private User receiptUser;
126 * Default constructor
128 public FinancialAdminReceiptWebRequestBean () {
129 // Call super constructor
134 * Adds the completed receipt to database by calling an EJB business method.
135 * If not all required fields are set, a proper exception is being thrown.
138 public void addReceipt () {
139 // Are all required fields set?
140 if (this.getReceiptBranchOffice() == null) {
142 throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
143 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
144 // It must be an already peristed instance
145 throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
146 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
147 // It must be an already peristed instance
148 throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
149 } else if (this.getReceiptPaymentType() == null) {
151 throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
152 } else if (this.getReceiptIssued() == null) {
154 throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
157 // Prepare receipt instance
158 final BillableReceipt receipt = this.createReceiptInstance();
160 // Is the receipt already there?
161 if (this.receiptListController.isReceiptAdded(receipt)) {
162 // Receipt has already been added
163 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
167 final BillableReceipt updatedReceipt;
169 // All is set, then try to call EJB
172 updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
173 } catch (final ReceiptAlreadyAddedException ex) {
175 throw new FaceletException(ex);
178 // Fire event with updated instance
179 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
186 * Getter for receipt's bar-code number
188 * @return Receipt's bar-code number
190 public String getReceiptBarCodeNumber () {
191 return this.receiptBarCodeNumber;
195 * Setter for receipt's bar-code number
197 * @param receiptBarCodeNumber Receipt's bar-code number
199 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
200 this.receiptBarCodeNumber = receiptBarCodeNumber;
204 * Getter for receipt issuing branch office
206 * @return Receipt issuing branch office
208 public BranchOffice getReceiptBranchOffice () {
209 return this.receiptBranchOffice;
213 * Setter for receipt issuing branch office
215 * @param receiptBranchOffice Receipt issuing branch office
217 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
218 this.receiptBranchOffice = receiptBranchOffice;
222 * Getter for receipt issue date and time
224 * @return Receipt issue date and time
226 @SuppressWarnings ("ReturnOfDateField")
227 public Date getReceiptIssued () {
228 return this.receiptIssued;
232 * Setter for receipt issue date and time
234 * @param receiptIssued Receipt issue date and time
236 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
237 public void setReceiptIssued (final Date receiptIssued) {
238 this.receiptIssued = receiptIssued;
242 * Getter for receipt number
244 * @return Receipt number
246 public String getReceiptNumber () {
247 return this.receiptNumber;
251 * Setter for receipt number
253 * @param receiptNumber Receipt number
255 public void setReceiptNumber (final String receiptNumber) {
256 this.receiptNumber = receiptNumber;
260 * Getter for payment type
262 * @return Payment type
264 public PaymentType getReceiptPaymentType () {
265 return this.receiptPaymentType;
269 * Setter for payment type
271 * @param receiptPaymentType Payment type
273 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
274 this.receiptPaymentType = receiptPaymentType;
278 * Getter for receipt register's number
280 * @return Receipt register's number
282 public Long getReceiptRegisterNumber () {
283 return this.receiptRegisterNumber;
287 * Setter for receipt register's number
289 * @param receiptRegisterNumber Receipt register's number
291 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
292 this.receiptRegisterNumber = receiptRegisterNumber;
296 * Getter for receipt seller employee
298 * @return Receipt seller employee
300 public Employable getReceiptSellerEmployee () {
301 return this.receiptSellerEmployee;
305 * Setter for receipt seller employee
307 * @param receiptSellerEmployee Receipt seller employee
309 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
310 this.receiptSellerEmployee = receiptSellerEmployee;
314 * Getter for receipt sequence number
316 * @return Receipt sequence number
318 public Long getReceiptSequenceNumber () {
319 return this.receiptSequenceNumber;
323 * Setter for receipt sequence number
325 * @param receiptSequenceNumber Receipt sequence number
327 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
328 this.receiptSequenceNumber = receiptSequenceNumber;
332 * Getter for user instance
334 * @return User instance
336 public User getReceiptUser () {
337 return this.receiptUser;
341 * Setter for user instance
343 * @param receiptUser User instance
345 public void setReceiptUser (final User receiptUser) {
346 this.receiptUser = receiptUser;
352 private void clear () {
354 this.setReceiptBarCodeNumber(null);
355 this.setReceiptBranchOffice(null);
356 this.setReceiptIssued(null);
357 this.setReceiptNumber(null);
358 this.setReceiptPaymentType(null);
359 this.setReceiptRegisterNumber(null);
360 this.setReceiptSellerEmployee(null);
361 this.setReceiptSequenceNumber(null);
362 this.setReceiptUser(null);
366 * Creates a new instance from all available data of this bean.
368 * @return Receipt instance
370 private BillableReceipt createReceiptInstance () {
371 // Create new instance with minimum required data
372 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
375 receipt.setReceiptUser(this.getReceiptUser());
376 receipt.setReceiptNumber(this.getReceiptNumber());
377 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
378 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
379 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
380 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
382 // Return prepared instance