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.
137 * @return Link outcome
139 public String addReceipt () {
140 // Are all required fields set?
141 if (this.getReceiptBranchOffice() == null) {
143 throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
144 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
145 // It must be an already peristed instance
146 throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
147 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
148 // It must be an already peristed instance
149 throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
150 } else if (this.getReceiptPaymentType() == null) {
152 throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
153 } else if (this.getReceiptIssued() == null) {
155 throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
158 // Prepare receipt instance
159 final BillableReceipt receipt = this.createReceiptInstance();
161 // Is the receipt already there?
162 if (this.receiptListController.isReceiptAdded(receipt)) {
163 // Receipt has already been added
164 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
168 final BillableReceipt updatedReceipt;
170 // All is set, then try to call EJB
173 updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
174 } catch (final ReceiptAlreadyAddedException ex) {
176 throw new FaceletException(ex);
179 // Fire event with updated instance
180 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
185 // Return redirect outcome
190 * Getter for receipt's bar-code number
192 * @return Receipt's bar-code number
194 public String getReceiptBarCodeNumber () {
195 return this.receiptBarCodeNumber;
199 * Setter for receipt's bar-code number
201 * @param receiptBarCodeNumber Receipt's bar-code number
203 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
204 this.receiptBarCodeNumber = receiptBarCodeNumber;
208 * Getter for receipt issuing branch office
210 * @return Receipt issuing branch office
212 public BranchOffice getReceiptBranchOffice () {
213 return this.receiptBranchOffice;
217 * Setter for receipt issuing branch office
219 * @param receiptBranchOffice Receipt issuing branch office
221 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
222 this.receiptBranchOffice = receiptBranchOffice;
226 * Getter for receipt issue date and time
228 * @return Receipt issue date and time
230 @SuppressWarnings ("ReturnOfDateField")
231 public Date getReceiptIssued () {
232 return this.receiptIssued;
236 * Setter for receipt issue date and time
238 * @param receiptIssued Receipt issue date and time
240 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
241 public void setReceiptIssued (final Date receiptIssued) {
242 this.receiptIssued = receiptIssued;
246 * Getter for receipt number
248 * @return Receipt number
250 public String getReceiptNumber () {
251 return this.receiptNumber;
255 * Setter for receipt number
257 * @param receiptNumber Receipt number
259 public void setReceiptNumber (final String receiptNumber) {
260 this.receiptNumber = receiptNumber;
264 * Getter for payment type
266 * @return Payment type
268 public PaymentType getReceiptPaymentType () {
269 return this.receiptPaymentType;
273 * Setter for payment type
275 * @param receiptPaymentType Payment type
277 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
278 this.receiptPaymentType = receiptPaymentType;
282 * Getter for receipt register's number
284 * @return Receipt register's number
286 public Long getReceiptRegisterNumber () {
287 return this.receiptRegisterNumber;
291 * Setter for receipt register's number
293 * @param receiptRegisterNumber Receipt register's number
295 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
296 this.receiptRegisterNumber = receiptRegisterNumber;
300 * Getter for receipt seller employee
302 * @return Receipt seller employee
304 public Employable getReceiptSellerEmployee () {
305 return this.receiptSellerEmployee;
309 * Setter for receipt seller employee
311 * @param receiptSellerEmployee Receipt seller employee
313 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
314 this.receiptSellerEmployee = receiptSellerEmployee;
318 * Getter for receipt sequence number
320 * @return Receipt sequence number
322 public Long getReceiptSequenceNumber () {
323 return this.receiptSequenceNumber;
327 * Setter for receipt sequence number
329 * @param receiptSequenceNumber Receipt sequence number
331 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
332 this.receiptSequenceNumber = receiptSequenceNumber;
336 * Getter for user instance
338 * @return User instance
340 public User getReceiptUser () {
341 return this.receiptUser;
345 * Setter for user instance
347 * @param receiptUser User instance
349 public void setReceiptUser (final User receiptUser) {
350 this.receiptUser = receiptUser;
356 private void clear () {
358 this.setReceiptBarCodeNumber(null);
359 this.setReceiptBranchOffice(null);
360 this.setReceiptIssued(null);
361 this.setReceiptNumber(null);
362 this.setReceiptPaymentType(null);
363 this.setReceiptRegisterNumber(null);
364 this.setReceiptSellerEmployee(null);
365 this.setReceiptSequenceNumber(null);
366 this.setReceiptUser(null);
370 * Creates a new instance from all available data of this bean.
372 * @return Receipt instance
374 private BillableReceipt createReceiptInstance () {
375 // Create new instance with minimum required data
376 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
379 receipt.setReceiptUser(this.getReceiptUser());
380 receipt.setReceiptNumber(this.getReceiptNumber());
381 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
382 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
383 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
384 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
386 // Return prepared instance