2 * Copyright (C) 2017 - 2022 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.action;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.FacesException;
26 import javax.faces.application.FacesMessage;
27 import javax.faces.view.ViewScoped;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import org.mxchange.jbonuscard.model.bonus_card.BonusCard;
31 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
32 import org.mxchange.jcontactsbusiness.model.employee.Employable;
33 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
34 import org.mxchange.jfinancials.beans.financial.model.receipt.list.FinancialsReceiptListWebViewController;
35 import org.mxchange.jfinancials.events.receipt.added.ObservableReceiptAddedEvent;
36 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
37 import org.mxchange.jfinancials.events.receipt.updated.ObservableReceiptUpdatedEvent;
38 import org.mxchange.jfinancials.events.receipt.updated.ReceiptUpdatedEvent;
39 import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException;
40 import org.mxchange.jfinancials.exceptions.receipt.ReceiptNotFoundException;
41 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
42 import org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote;
43 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
44 import org.mxchange.jfinancials.model.receipt.Receipts;
45 import org.mxchange.jproduct.model.payment.PaymentType;
46 import org.mxchange.jusercore.model.user.User;
49 * An administrative backing bean for receipts
51 * @author Roland Häder<roland@mxchange.org>
53 @Named ("adminReceiptActionController")
55 public class FinancialAdminReceiptActionWebViewBean extends BaseFinancialsBean implements FinancialAdminReceiptActionWebViewController {
60 private static final long serialVersionUID = 595_754_280_374_172L;
63 * Event being fired when administrator has added a new receipt
67 private Event<ObservableReceiptAddedEvent> adminAddedReceiptEvent;
70 * EJB for general financial receipt purposes
72 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceipt!org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote")
73 private FinancialAdminReceiptSessionBeanRemote adminReceiptBean;
76 * Event being fired when administrator has added a new receipt
80 private Event<ObservableReceiptUpdatedEvent> adminUpdatedReceiptEvent;
85 private BillableReceipt currentReceipt;
90 private String receiptBarCodeNumber;
93 * Bonus card assigned with receipt
95 private BonusCard receiptBonusCard;
98 * Recipient issuing company (for example where the user went shopping to)
100 private BranchOffice receiptBranchOffice;
105 private Long receiptId;
108 * Date/time the receipt has been issued
110 private Date receiptIssued;
113 * Receipt list controller
116 private FinancialsReceiptListWebViewController receiptListController;
119 * Receipt number (only numbers)
121 private String receiptNumber;
124 * Payment type being used for this receipt
126 private PaymentType receiptPaymentType;
131 private String receiptQrCode;
136 private Long receiptRegisterNumber;
139 * Current receipt is a resumption of this receipt
141 private BillableReceipt receiptResumptionOf;
146 private Employable receiptSellerEmployee;
151 private Long receiptSequenceNumber;
156 private Long receiptTransactionNumber;
159 * User who "owns" this receipt
161 private User receiptUser;
164 * Default constructor
166 public FinancialAdminReceiptActionWebViewBean () {
167 // Call super constructor
172 * Adds the completed receipt to database by calling an EJB business method.
173 * If not all required fields are set, a proper exception is being thrown.
176 public void addReceipt () {
177 // Are all required fields set?
178 if (this.getReceiptBranchOffice() == null) {
180 throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
181 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
182 // It must be an already peristed instance
183 throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
184 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
185 // It must be an already peristed instance
186 throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
187 } else if (this.getReceiptPaymentType() == null) {
189 throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
190 } else if (this.getReceiptIssued() == null) {
192 throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
195 // Prepare receipt instance
196 final BillableReceipt receipt = this.createReceiptInstance();
198 // Is the receipt already there?
199 if (this.receiptListController.isReceiptAdded(receipt)) {
200 // Receipt has already been added
201 throw new FacesException(MessageFormat.format(
202 "Receipt for receiptBranchOffice={0},receiptIssued={1},receiptNumber={2} has already been added.", //NOI18N
203 this.getReceiptBranchOffice().getBranchId(),
204 this.getReceiptIssued().toString(),
205 this.getReceiptNumber()
210 final BillableReceipt updatedReceipt;
212 // All is set, then try to call EJB
215 updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
216 } catch (final ReceiptAlreadyAddedException ex) {
218 throw new FacesException(ex);
221 // Fire event with updated instance
222 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
229 * Copies all fields from current receipt instance to local bean properties.
231 public void copyAllReceiptProperties () {
232 // Is current receipt instance valid?
233 if (this.getCurrentReceipt() == null) {
235 throw new NullPointerException("this.currentReceipt is null"); //NOI18N
239 this.setReceiptBarCodeNumber(this.getCurrentReceipt().getReceiptBarCodeNumber());
240 this.setReceiptBonusCard(this.getCurrentReceipt().getReceiptBonusCard());
241 this.setReceiptBranchOffice(this.getCurrentReceipt().getReceiptBranchOffice());
242 this.setReceiptId(this.getCurrentReceipt().getReceiptId());
243 this.setReceiptIssued(this.getCurrentReceipt().getReceiptIssued());
244 this.setReceiptNumber(this.getCurrentReceipt().getReceiptNumber());
245 this.setReceiptPaymentType(this.getCurrentReceipt().getReceiptPaymentType());
246 this.setReceiptQrCode(this.getCurrentReceipt().getReceiptQrCode());
247 this.setReceiptRegisterNumber(this.getCurrentReceipt().getReceiptRegisterNumber());
248 this.setReceiptSellerEmployee(this.getCurrentReceipt().getReceiptSellerEmployee());
249 this.setReceiptSequenceNumber(this.getCurrentReceipt().getReceiptSequenceNumber());
250 this.setReceiptTransactionNumber(this.getCurrentReceipt().getReceiptTransactionNumber());
251 this.setReceiptUser(this.getCurrentReceipt().getReceiptUser());
255 * Getter for current receipt
257 * @return Current receipt
259 public BillableReceipt getCurrentReceipt () {
260 return this.currentReceipt;
264 * Setter for current receipt
266 * @param currentReceipt Current receipt
268 public void setCurrentReceipt (final BillableReceipt currentReceipt) {
269 this.currentReceipt = currentReceipt;
273 * Getter for receipt's bar-code number
275 * @return Receipt's bar-code number
277 public String getReceiptBarCodeNumber () {
278 return this.receiptBarCodeNumber;
282 * Setter for receipt's bar-code number
284 * @param receiptBarCodeNumber Receipt's bar-code number
286 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
287 this.receiptBarCodeNumber = receiptBarCodeNumber;
291 * Getter for bonus card
295 public BonusCard getReceiptBonusCard () {
296 return this.receiptBonusCard;
300 * Setter for bonus card
302 * @param receiptBonusCard Bonus card
304 public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
305 this.receiptBonusCard = receiptBonusCard;
309 * Getter for receipt issuing branch office
311 * @return Receipt issuing branch office
313 public BranchOffice getReceiptBranchOffice () {
314 return this.receiptBranchOffice;
318 * Setter for receipt issuing branch office
320 * @param receiptBranchOffice Receipt issuing branch office
322 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
323 this.receiptBranchOffice = receiptBranchOffice;
327 * Getter for receipt id
331 public Long getReceiptId () {
332 return this.receiptId;
336 * Setter for receipt id
338 * @param receiptId Receipt id
340 public void setReceiptId (final Long receiptId) {
341 this.receiptId = receiptId;
345 * Getter for receipt issue date and time
347 * @return Receipt issue date and time
349 @SuppressWarnings ("ReturnOfDateField")
350 public Date getReceiptIssued () {
351 return this.receiptIssued;
355 * Setter for receipt issue date and time
357 * @param receiptIssued Receipt issue date and time
359 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
360 public void setReceiptIssued (final Date receiptIssued) {
361 this.receiptIssued = receiptIssued;
365 * Getter for receipt number
367 * @return Receipt number
369 public String getReceiptNumber () {
370 return this.receiptNumber;
374 * Setter for receipt number
376 * @param receiptNumber Receipt number
378 public void setReceiptNumber (final String receiptNumber) {
379 this.receiptNumber = receiptNumber;
383 * Getter for payment type
385 * @return Payment type
387 public PaymentType getReceiptPaymentType () {
388 return this.receiptPaymentType;
392 * Setter for payment type
394 * @param receiptPaymentType Payment type
396 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
397 this.receiptPaymentType = receiptPaymentType;
401 * Getter for receipt QR code
403 * @return Receipt QR code
405 public String getReceiptQrCode () {
406 return this.receiptQrCode;
410 * Setter for receipt QR code
412 * @param receiptQrCode Receipt QR code
414 public void setReceiptQrCode (final String receiptQrCode) {
415 this.receiptQrCode = receiptQrCode;
419 * Getter for receipt register's number
421 * @return Receipt register's number
423 public Long getReceiptRegisterNumber () {
424 return this.receiptRegisterNumber;
428 * Setter for receipt register's number
430 * @param receiptRegisterNumber Receipt register's number
432 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
433 this.receiptRegisterNumber = receiptRegisterNumber;
437 * Getter for receipt's resumption of an other receipt
439 * @return Current receipt is based on this receipt
441 public BillableReceipt getReceiptResumptionOf () {
442 return this.receiptResumptionOf;
446 * Setter for receipt's resumption of an other receipt
448 * @param receiptResumptionOf Current receipt is based on this receipt
450 public void setReceiptResumptionOf (final BillableReceipt receiptResumptionOf) {
451 this.receiptResumptionOf = receiptResumptionOf;
455 * Getter for receipt seller employee
457 * @return Receipt seller employee
459 public Employable getReceiptSellerEmployee () {
460 return this.receiptSellerEmployee;
464 * Setter for receipt seller employee
466 * @param receiptSellerEmployee Receipt seller employee
468 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
469 this.receiptSellerEmployee = receiptSellerEmployee;
473 * Getter for receipt sequence number
475 * @return Receipt sequence number
477 public Long getReceiptSequenceNumber () {
478 return this.receiptSequenceNumber;
482 * Setter for receipt sequence number
484 * @param receiptSequenceNumber Receipt sequence number
486 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
487 this.receiptSequenceNumber = receiptSequenceNumber;
491 * Getter for receipt transaction number
493 * @return Receipt transaction number
495 public Long getReceiptTransactionNumber () {
496 return this.receiptTransactionNumber;
500 * Setter for receipt transaction number
502 * @param receiptTransactionNumber Receipt transaction number
504 public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
505 this.receiptTransactionNumber = receiptTransactionNumber;
509 * Getter for user instance
511 * @return User instance
513 public User getReceiptUser () {
514 return this.receiptUser;
518 * Setter for user instance
520 * @param receiptUser User instance
522 public void setReceiptUser (final User receiptUser) {
523 this.receiptUser = receiptUser;
527 * Updates receipt instance with new data. This copies all fields from this
528 * backing bean into currentReceipt instance which should be still there.
530 * @return Redirect target
532 public String updateReceipt () {
533 // Is current instance still there?
534 if (this.getCurrentReceipt() == null) {
536 throw new NullPointerException("this.currentReceipt is null"); //NOI18N
537 } else if (this.getCurrentReceipt().getReceiptId() == null) {
539 throw new NullPointerException("this.currentReceipt.receiptId is null"); //NOI18N
542 // Create new instance
543 final BillableReceipt receipt = this.createReceiptInstance();
546 if (Objects.equals(receipt, this.getCurrentReceipt())) {
547 // Yes, then abort here with message to user
548 this.showFacesMessage("form-edit-receipt", "ADMIN_ERROR_FINANCIAL_RECEIPT_NOT_CHANGED", FacesMessage.SEVERITY_WARN); //NOI18N
553 Receipts.copyReceiptData(receipt, this.getCurrentReceipt());
555 // Create updated receipt instance
556 final BillableReceipt updatedReceipt;
560 updatedReceipt = this.adminReceiptBean.updateReceipt(this.getCurrentReceipt());
561 } catch (final ReceiptNotFoundException ex) {
563 throw new FacesException(ex);
567 this.adminUpdatedReceiptEvent.fire(new ReceiptUpdatedEvent(updatedReceipt));
572 // Redirect to list view
573 return "admin_list_receipts"; //NOI18N
579 private void clear () {
581 this.setReceiptBarCodeNumber(null);
582 this.setReceiptBonusCard(null);
583 this.setReceiptBranchOffice(null);
584 this.setReceiptId(null);
585 this.setReceiptIssued(null);
586 this.setReceiptNumber(null);
587 this.setReceiptPaymentType(null);
588 this.setReceiptQrCode(null);
589 this.setReceiptRegisterNumber(null);
590 this.setReceiptResumptionOf(null);
591 this.setReceiptSellerEmployee(null);
592 this.setReceiptSequenceNumber(null);
593 this.setReceiptTransactionNumber(null);
594 this.setReceiptUser(null);
598 * Creates a new instance from all available data of this bean.
600 * @return Receipt instance
602 private BillableReceipt createReceiptInstance () {
603 // Create new instance with minimum required data
604 final BillableReceipt receipt = new FinancialReceipt(
605 this.getReceiptPaymentType(),
606 this.getReceiptBranchOffice(),
607 this.getReceiptIssued()
611 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
612 receipt.setReceiptBonusCard(this.getReceiptBonusCard());
613 receipt.setReceiptId(this.getReceiptId());
614 receipt.setReceiptNumber(this.getReceiptNumber());
615 receipt.setReceiptQrCode(this.getReceiptQrCode());
616 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
617 receipt.setReceiptResumptionOf(this.getReceiptResumptionOf());
618 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
619 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
620 receipt.setReceiptTransactionNumber(this.getReceiptTransactionNumber());
621 receipt.setReceiptUser(this.getReceiptUser());
623 // Return prepared instance