2 * Copyright (C) 2017 - 2020 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 Long receiptRegisterNumber;
134 * Current receipt is a resumption of this receipt
136 private BillableReceipt receiptResumptionOf;
141 private Employable receiptSellerEmployee;
146 private Long receiptSequenceNumber;
151 private Long receiptTransactionNumber;
154 * User who "owns" this receipt
156 private User receiptUser;
159 * Default constructor
161 public FinancialAdminReceiptActionWebViewBean () {
162 // Call super constructor
167 * Adds the completed receipt to database by calling an EJB business method.
168 * If not all required fields are set, a proper exception is being thrown.
171 public void addReceipt () {
172 // Are all required fields set?
173 if (this.getReceiptBranchOffice() == null) {
175 throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
176 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
177 // It must be an already peristed instance
178 throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
179 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
180 // It must be an already peristed instance
181 throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
182 } else if (this.getReceiptPaymentType() == null) {
184 throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
185 } else if (this.getReceiptIssued() == null) {
187 throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
190 // Prepare receipt instance
191 final BillableReceipt receipt = this.createReceiptInstance();
193 // Is the receipt already there?
194 if (this.receiptListController.isReceiptAdded(receipt)) {
195 // Receipt has already been added
196 throw new FacesException(MessageFormat.format(
197 "Receipt for receiptBranchOffice={0},receiptIssued={1},receiptNumber={2} has already been added.", //NOI18N
198 this.getReceiptBranchOffice().getBranchId(),
199 this.getReceiptIssued().toString(),
200 this.getReceiptNumber()
205 final BillableReceipt updatedReceipt;
207 // All is set, then try to call EJB
210 updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
211 } catch (final ReceiptAlreadyAddedException ex) {
213 throw new FacesException(ex);
216 // Fire event with updated instance
217 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
224 * Copies all fields from current receipt instance to local bean properties.
226 public void copyAllReceiptProperties () {
227 // Is current receipt instance valid?
228 if (this.getCurrentReceipt() == null) {
230 throw new NullPointerException("this.currentReceipt is null"); //NOI18N
234 this.setReceiptBarCodeNumber(this.getCurrentReceipt().getReceiptBarCodeNumber());
235 this.setReceiptBonusCard(this.getCurrentReceipt().getReceiptBonusCard());
236 this.setReceiptBranchOffice(this.getCurrentReceipt().getReceiptBranchOffice());
237 this.setReceiptId(this.getCurrentReceipt().getReceiptId());
238 this.setReceiptIssued(this.getCurrentReceipt().getReceiptIssued());
239 this.setReceiptNumber(this.getCurrentReceipt().getReceiptNumber());
240 this.setReceiptPaymentType(this.getCurrentReceipt().getReceiptPaymentType());
241 this.setReceiptRegisterNumber(this.getCurrentReceipt().getReceiptRegisterNumber());
242 this.setReceiptSellerEmployee(this.getCurrentReceipt().getReceiptSellerEmployee());
243 this.setReceiptSequenceNumber(this.getCurrentReceipt().getReceiptSequenceNumber());
244 this.setReceiptTransactionNumber(this.getCurrentReceipt().getReceiptTransactionNumber());
245 this.setReceiptUser(this.getCurrentReceipt().getReceiptUser());
249 * Getter for current receipt
251 * @return Current receipt
253 public BillableReceipt getCurrentReceipt () {
254 return this.currentReceipt;
258 * Setter for current receipt
260 * @param currentReceipt Current receipt
262 public void setCurrentReceipt (final BillableReceipt currentReceipt) {
263 this.currentReceipt = currentReceipt;
267 * Getter for receipt's bar-code number
269 * @return Receipt's bar-code number
271 public String getReceiptBarCodeNumber () {
272 return this.receiptBarCodeNumber;
276 * Setter for receipt's bar-code number
278 * @param receiptBarCodeNumber Receipt's bar-code number
280 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
281 this.receiptBarCodeNumber = receiptBarCodeNumber;
285 * Getter for bonus card
289 public BonusCard getReceiptBonusCard () {
290 return this.receiptBonusCard;
294 * Setter for bonus card
296 * @param receiptBonusCard Bonus card
298 public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
299 this.receiptBonusCard = receiptBonusCard;
303 * Getter for receipt issuing branch office
305 * @return Receipt issuing branch office
307 public BranchOffice getReceiptBranchOffice () {
308 return this.receiptBranchOffice;
312 * Setter for receipt issuing branch office
314 * @param receiptBranchOffice Receipt issuing branch office
316 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
317 this.receiptBranchOffice = receiptBranchOffice;
321 * Getter for receipt id
325 public Long getReceiptId () {
326 return this.receiptId;
330 * Setter for receipt id
332 * @param receiptId Receipt id
334 public void setReceiptId (final Long receiptId) {
335 this.receiptId = receiptId;
339 * Getter for receipt issue date and time
341 * @return Receipt issue date and time
343 @SuppressWarnings ("ReturnOfDateField")
344 public Date getReceiptIssued () {
345 return this.receiptIssued;
349 * Setter for receipt issue date and time
351 * @param receiptIssued Receipt issue date and time
353 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
354 public void setReceiptIssued (final Date receiptIssued) {
355 this.receiptIssued = receiptIssued;
359 * Getter for receipt number
361 * @return Receipt number
363 public String getReceiptNumber () {
364 return this.receiptNumber;
368 * Setter for receipt number
370 * @param receiptNumber Receipt number
372 public void setReceiptNumber (final String receiptNumber) {
373 this.receiptNumber = receiptNumber;
377 * Getter for payment type
379 * @return Payment type
381 public PaymentType getReceiptPaymentType () {
382 return this.receiptPaymentType;
386 * Setter for payment type
388 * @param receiptPaymentType Payment type
390 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
391 this.receiptPaymentType = receiptPaymentType;
395 * Getter for receipt register's number
397 * @return Receipt register's number
399 public Long getReceiptRegisterNumber () {
400 return this.receiptRegisterNumber;
404 * Setter for receipt register's number
406 * @param receiptRegisterNumber Receipt register's number
408 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
409 this.receiptRegisterNumber = receiptRegisterNumber;
413 * Getter for receipt's resumption of an other receipt
415 * @return Current receipt is based on this receipt
417 public BillableReceipt getReceiptResumptionOf () {
418 return this.receiptResumptionOf;
422 * Setter for receipt's resumption of an other receipt
424 * @param receiptResumptionOf Current receipt is based on this receipt
426 public void setReceiptResumptionOf (final BillableReceipt receiptResumptionOf) {
427 this.receiptResumptionOf = receiptResumptionOf;
431 * Getter for receipt seller employee
433 * @return Receipt seller employee
435 public Employable getReceiptSellerEmployee () {
436 return this.receiptSellerEmployee;
440 * Setter for receipt seller employee
442 * @param receiptSellerEmployee Receipt seller employee
444 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
445 this.receiptSellerEmployee = receiptSellerEmployee;
449 * Getter for receipt sequence number
451 * @return Receipt sequence number
453 public Long getReceiptSequenceNumber () {
454 return this.receiptSequenceNumber;
458 * Setter for receipt sequence number
460 * @param receiptSequenceNumber Receipt sequence number
462 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
463 this.receiptSequenceNumber = receiptSequenceNumber;
467 * Getter for receipt transaction number
469 * @return Receipt transaction number
471 public Long getReceiptTransactionNumber () {
472 return this.receiptTransactionNumber;
476 * Setter for receipt transaction number
478 * @param receiptTransactionNumber Receipt transaction number
480 public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
481 this.receiptTransactionNumber = receiptTransactionNumber;
485 * Getter for user instance
487 * @return User instance
489 public User getReceiptUser () {
490 return this.receiptUser;
494 * Setter for user instance
496 * @param receiptUser User instance
498 public void setReceiptUser (final User receiptUser) {
499 this.receiptUser = receiptUser;
503 * Updates receipt instance with new data. This copies all fields from this
504 * backing bean into currentReceipt instance which should be still there.
506 public String updateReceipt () {
507 // Is current instance still there?
508 if (this.getCurrentReceipt() == null) {
510 throw new NullPointerException("this.currentReceipt is null"); //NOI18N
511 } else if (this.getCurrentReceipt().getReceiptId() == null) {
513 throw new NullPointerException("this.currentReceipt.receiptId is null"); //NOI18N
516 // Create new instance
517 final BillableReceipt receipt = this.createReceiptInstance();
520 if (Objects.equals(receipt, this.getCurrentReceipt())) {
521 // Yes, then abort here with message to user
522 this.showFacesMessage("form-edit-receipt", "ADMIN_ERROR_FINANCIAL_RECEIPT_NOT_CHANGED", FacesMessage.SEVERITY_WARN); //NOI18N
527 Receipts.copyReceiptData(receipt, this.getCurrentReceipt());
529 // Create updated receipt instance
530 final BillableReceipt updatedReceipt;
534 updatedReceipt = this.adminReceiptBean.updateReceipt(this.getCurrentReceipt());
535 } catch (final ReceiptNotFoundException ex) {
537 throw new FacesException(ex);
541 this.adminUpdatedReceiptEvent.fire(new ReceiptUpdatedEvent(updatedReceipt));
546 // Redirect to list view
547 return "admin_list_receipts"; //NOI18N
553 private void clear () {
555 this.setReceiptBarCodeNumber(null);
556 this.setReceiptBonusCard(null);
557 this.setReceiptBranchOffice(null);
558 this.setReceiptId(null);
559 this.setReceiptIssued(null);
560 this.setReceiptNumber(null);
561 this.setReceiptPaymentType(null);
562 this.setReceiptRegisterNumber(null);
563 this.setReceiptResumptionOf(null);
564 this.setReceiptSellerEmployee(null);
565 this.setReceiptSequenceNumber(null);
566 this.setReceiptTransactionNumber(null);
567 this.setReceiptUser(null);
571 * Creates a new instance from all available data of this bean.
573 * @return Receipt instance
575 private BillableReceipt createReceiptInstance () {
576 // Create new instance with minimum required data
577 final BillableReceipt receipt = new FinancialReceipt(
578 this.getReceiptPaymentType(),
579 this.getReceiptBranchOffice(),
580 this.getReceiptIssued()
584 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
585 receipt.setReceiptBonusCard(this.getReceiptBonusCard());
586 receipt.setReceiptId(this.getReceiptId());
587 receipt.setReceiptNumber(this.getReceiptNumber());
588 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
589 receipt.setReceiptResumptionOf(this.getReceiptResumptionOf());
590 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
591 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
592 receipt.setReceiptTransactionNumber(this.getReceiptTransactionNumber());
593 receipt.setReceiptUser(this.getReceiptUser());
595 // Return prepared instance