2 * Copyright (C) 2017 Roland Häder
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.events.receipt.added.ObservableReceiptAddedEvent;
32 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
33 import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException;
34 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
35 import org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote;
36 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
37 import org.mxchange.jproduct.model.payment.PaymentType;
38 import org.mxchange.jusercore.model.user.User;
41 * An administrative backing bean for receipts
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("adminReceiptController")
47 public class FinancialAdminReceiptWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptWebRequestController {
52 private static final long serialVersionUID = 595_754_280_374_171L;
55 * Event being fired when administrator has added a new receipt
59 private Event<ObservableReceiptAddedEvent> adminAddedReceiptEvent;
62 * EJB for general financial receipt purposes
64 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceipt!org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote")
65 private FinancialAdminReceiptSessionBeanRemote adminReceiptBean;
70 private Long receiptBarCodeNumber;
73 * Recipient issuing company (for example where the user went shopping to)
75 private BranchOffice receiptBranchOffice;
78 * General receipt controller
81 private FinancialsReceiptWebRequestController receiptController;
84 * Date/time the receipt has been issued
86 private Date receiptIssued;
89 * Receipt number (only numbers)
91 private Long receiptNumber;
94 * Payment type being used for this receipt
96 private PaymentType receiptPaymentType;
101 private Long receiptRegisterNumber;
106 private Employable receiptSellerEmployee;
111 private Long receiptSequenceNumber;
114 * User who "owns" this receipt
116 private User receiptUser;
119 * Default constructor
121 public FinancialAdminReceiptWebRequestBean () {
122 // Call super constructor
127 * Adds the completed receipt to database by calling an EJB business method.
128 * If not all required fields are set, a proper exception is being thrown.
130 * @return Link outcome
132 public String addReceipt () {
133 // Are all required fields set?
134 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.receiptController.isReceiptAdded(receipt)) {
156 // Receipt has already been added
157 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
161 final BillableReceipt updatedReceipt;
163 // All is set, then try to call EJB
166 updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
167 } catch (final ReceiptAlreadyAddedException ex) {
169 throw new FaceletException(ex);
172 // Fire event with updated instance
173 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
178 // Return redirect outcome
179 return "add_receipt_item?faces-redirect=true"; //NOI18N
183 * Getter for receipt's bar-code number
185 * @return Receipt's bar-code number
187 public Long getReceiptBarCodeNumber () {
188 return this.receiptBarCodeNumber;
192 * Setter for receipt's bar-code number
194 * @param receiptBarCodeNumber Receipt's bar-code number
196 public void setReceiptBarCodeNumber (final Long receiptBarCodeNumber) {
197 this.receiptBarCodeNumber = receiptBarCodeNumber;
201 * Getter for receipt issuing branch office
203 * @return Receipt issuing branch office
205 public BranchOffice getReceiptBranchOffice () {
206 return this.receiptBranchOffice;
210 * Setter for receipt issuing branch office
212 * @param receiptBranchOffice Receipt issuing branch office
214 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
215 this.receiptBranchOffice = receiptBranchOffice;
219 * Getter for receipt issue date and time
221 * @return Receipt issue date and time
223 @SuppressWarnings ("ReturnOfDateField")
224 public Date getReceiptIssued () {
225 return this.receiptIssued;
229 * Setter for receipt issue date and time
231 * @param receiptIssued Receipt issue date and time
233 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
234 public void setReceiptIssued (final Date receiptIssued) {
235 this.receiptIssued = receiptIssued;
239 * Getter for receipt number
241 * @return Receipt number
243 public Long getReceiptNumber () {
244 return this.receiptNumber;
248 * Setter for receipt number
250 * @param receiptNumber Receipt number
252 public void setReceiptNumber (final Long receiptNumber) {
253 this.receiptNumber = receiptNumber;
257 * Getter for payment type
259 * @return Payment type
261 public PaymentType getReceiptPaymentType () {
262 return this.receiptPaymentType;
266 * Setter for payment type
268 * @param receiptPaymentType Payment type
270 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
271 this.receiptPaymentType = receiptPaymentType;
275 * Getter for receipt register's number
277 * @return Receipt register's number
279 public Long getReceiptRegisterNumber () {
280 return this.receiptRegisterNumber;
284 * Setter for receipt register's number
286 * @param receiptRegisterNumber Receipt register's number
288 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
289 this.receiptRegisterNumber = receiptRegisterNumber;
293 * Getter for receipt seller employee
295 * @return Receipt seller employee
297 public Employable getReceiptSellerEmployee () {
298 return this.receiptSellerEmployee;
302 * Setter for receipt seller employee
304 * @param receiptSellerEmployee Receipt seller employee
306 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
307 this.receiptSellerEmployee = receiptSellerEmployee;
311 * Getter for receipt sequence number
313 * @return Receipt sequence number
315 public Long getReceiptSequenceNumber () {
316 return this.receiptSequenceNumber;
320 * Setter for receipt sequence number
322 * @param receiptSequenceNumber Receipt sequence number
324 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
325 this.receiptSequenceNumber = receiptSequenceNumber;
329 * Getter for user instance
331 * @return User instance
333 public User getReceiptUser () {
334 return this.receiptUser;
338 * Setter for user instance
340 * @param receiptUser User instance
342 public void setReceiptUser (final User receiptUser) {
343 this.receiptUser = receiptUser;
349 private void clear () {
351 this.setReceiptBarCodeNumber(null);
352 this.setReceiptBranchOffice(null);
353 this.setReceiptIssued(null);
354 this.setReceiptNumber(null);
355 this.setReceiptPaymentType(null);
356 this.setReceiptRegisterNumber(null);
357 this.setReceiptSellerEmployee(null);
358 this.setReceiptSequenceNumber(null);
359 this.setReceiptUser(null);
363 * Creates a new instance from all available data of this bean.
365 * @return Receipt instance
367 private BillableReceipt createReceiptInstance () {
368 // Create new instance with minimum required data
369 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
372 receipt.setReceiptUser(this.getReceiptUser());
373 receipt.setReceiptNumber(this.getReceiptNumber());
374 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
375 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
376 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
377 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
379 // Return prepared instance