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.application.FacesMessage;
26 import javax.faces.view.ViewScoped;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
31 import org.mxchange.jcontactsbusiness.model.employee.Employable;
32 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
33 import org.mxchange.jfinancials.beans.financial.model.receipt.list.FinancialsReceiptListWebViewController;
34 import org.mxchange.jfinancials.events.receipt.added.ObservableReceiptAddedEvent;
35 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
36 import org.mxchange.jfinancials.events.receipt.updated.ObservableReceiptUpdatedEvent;
37 import org.mxchange.jfinancials.events.receipt.updated.ReceiptUpdatedEvent;
38 import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException;
39 import org.mxchange.jfinancials.exceptions.receipt.ReceiptNotFoundException;
40 import org.mxchange.jfinancials.model.bonus_card.BonusCard;
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;
136 private Employable receiptSellerEmployee;
141 private Long receiptSequenceNumber;
146 private Long receiptTransactionNumber;
149 * User who "owns" this receipt
151 private User receiptUser;
154 * Default constructor
156 public FinancialAdminReceiptActionWebViewBean () {
157 // Call super constructor
162 * Adds the completed receipt to database by calling an EJB business method.
163 * If not all required fields are set, a proper exception is being thrown.
166 public void addReceipt () {
167 // Are all required fields set?
168 if (this.getReceiptBranchOffice() == null) {
170 throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
171 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
172 // It must be an already peristed instance
173 throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
174 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
175 // It must be an already peristed instance
176 throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
177 } else if (this.getReceiptPaymentType() == null) {
179 throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
180 } else if (this.getReceiptIssued() == null) {
182 throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
185 // Prepare receipt instance
186 final BillableReceipt receipt = this.createReceiptInstance();
188 // Is the receipt already there?
189 if (this.receiptListController.isReceiptAdded(receipt)) {
190 // Receipt has already been added
191 throw new FaceletException(MessageFormat.format(
192 "Receipt for receiptBranchOffice={0},receiptIssued={1},receiptNumber={2} has already been added.", //NOI18N
193 this.getReceiptBranchOffice().getBranchId(),
194 this.getReceiptIssued().toString(),
195 this.getReceiptNumber()
200 final BillableReceipt updatedReceipt;
202 // All is set, then try to call EJB
205 updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
206 } catch (final ReceiptAlreadyAddedException ex) {
208 throw new FaceletException(ex);
211 // Fire event with updated instance
212 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
219 * Copies all fields from current receipt instance to local bean properties.
221 public void copyAllReceiptProperties () {
222 // Is current receipt instance valid?
223 if (this.getCurrentReceipt() == null) {
225 throw new NullPointerException("this.currentReceipt is null");
229 this.setReceiptBarCodeNumber(this.getCurrentReceipt().getReceiptBarCodeNumber());
230 this.setReceiptBonusCard(this.getCurrentReceipt().getReceiptBonusCard());
231 this.setReceiptBranchOffice(this.getCurrentReceipt().getReceiptBranchOffice());
232 this.setReceiptId(this.getCurrentReceipt().getReceiptId());
233 this.setReceiptIssued(this.getCurrentReceipt().getReceiptIssued());
234 this.setReceiptNumber(this.getCurrentReceipt().getReceiptNumber());
235 this.setReceiptPaymentType(this.getCurrentReceipt().getReceiptPaymentType());
236 this.setReceiptRegisterNumber(this.getCurrentReceipt().getReceiptRegisterNumber());
237 this.setReceiptSellerEmployee(this.getCurrentReceipt().getReceiptSellerEmployee());
238 this.setReceiptSequenceNumber(this.getCurrentReceipt().getReceiptSequenceNumber());
239 this.setReceiptTransactionNumber(this.getCurrentReceipt().getReceiptTransactionNumber());
240 this.setReceiptUser(this.getCurrentReceipt().getReceiptUser());
244 * Getter for current receipt
246 * @return Current receipt
248 public BillableReceipt getCurrentReceipt () {
249 return this.currentReceipt;
253 * Setter for current receipt
255 * @param currentReceipt Current receipt
257 public void setCurrentReceipt (final BillableReceipt currentReceipt) {
258 this.currentReceipt = currentReceipt;
262 * Getter for receipt's bar-code number
264 * @return Receipt's bar-code number
266 public String getReceiptBarCodeNumber () {
267 return this.receiptBarCodeNumber;
271 * Setter for receipt's bar-code number
273 * @param receiptBarCodeNumber Receipt's bar-code number
275 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
276 this.receiptBarCodeNumber = receiptBarCodeNumber;
280 * Getter for bonus card
284 public BonusCard getReceiptBonusCard () {
285 return this.receiptBonusCard;
289 * Setter for bonus card
291 * @param receiptBonusCard Bonus card
293 public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
294 this.receiptBonusCard = receiptBonusCard;
298 * Getter for receipt issuing branch office
300 * @return Receipt issuing branch office
302 public BranchOffice getReceiptBranchOffice () {
303 return this.receiptBranchOffice;
307 * Setter for receipt issuing branch office
309 * @param receiptBranchOffice Receipt issuing branch office
311 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
312 this.receiptBranchOffice = receiptBranchOffice;
316 * Getter for receipt id
320 public Long getReceiptId () {
321 return this.receiptId;
325 * Setter for receipt id
327 * @param receiptId Receipt id
329 public void setReceiptId (final Long receiptId) {
330 this.receiptId = receiptId;
334 * Getter for receipt issue date and time
336 * @return Receipt issue date and time
338 @SuppressWarnings ("ReturnOfDateField")
339 public Date getReceiptIssued () {
340 return this.receiptIssued;
344 * Setter for receipt issue date and time
346 * @param receiptIssued Receipt issue date and time
348 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
349 public void setReceiptIssued (final Date receiptIssued) {
350 this.receiptIssued = receiptIssued;
354 * Getter for receipt number
356 * @return Receipt number
358 public String getReceiptNumber () {
359 return this.receiptNumber;
363 * Setter for receipt number
365 * @param receiptNumber Receipt number
367 public void setReceiptNumber (final String receiptNumber) {
368 this.receiptNumber = receiptNumber;
372 * Getter for payment type
374 * @return Payment type
376 public PaymentType getReceiptPaymentType () {
377 return this.receiptPaymentType;
381 * Setter for payment type
383 * @param receiptPaymentType Payment type
385 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
386 this.receiptPaymentType = receiptPaymentType;
390 * Getter for receipt register's number
392 * @return Receipt register's number
394 public Long getReceiptRegisterNumber () {
395 return this.receiptRegisterNumber;
399 * Setter for receipt register's number
401 * @param receiptRegisterNumber Receipt register's number
403 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
404 this.receiptRegisterNumber = receiptRegisterNumber;
408 * Getter for receipt seller employee
410 * @return Receipt seller employee
412 public Employable getReceiptSellerEmployee () {
413 return this.receiptSellerEmployee;
417 * Setter for receipt seller employee
419 * @param receiptSellerEmployee Receipt seller employee
421 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
422 this.receiptSellerEmployee = receiptSellerEmployee;
426 * Getter for receipt sequence number
428 * @return Receipt sequence number
430 public Long getReceiptSequenceNumber () {
431 return this.receiptSequenceNumber;
435 * Setter for receipt sequence number
437 * @param receiptSequenceNumber Receipt sequence number
439 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
440 this.receiptSequenceNumber = receiptSequenceNumber;
444 * Getter for receipt transaction number
446 * @return Receipt transaction number
448 public Long getReceiptTransactionNumber () {
449 return this.receiptTransactionNumber;
453 * Setter for receipt transaction number
455 * @param receiptTransactionNumber Receipt transaction number
457 public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
458 this.receiptTransactionNumber = receiptTransactionNumber;
462 * Getter for user instance
464 * @return User instance
466 public User getReceiptUser () {
467 return this.receiptUser;
471 * Setter for user instance
473 * @param receiptUser User instance
475 public void setReceiptUser (final User receiptUser) {
476 this.receiptUser = receiptUser;
480 * Updates receipt instance with new data. This copies all fields from this
481 * backing bean into currentReceipt instance which should be still there.
483 public void updateReceipt () {
484 // Is current instance still there?
485 if (this.getCurrentReceipt() == null) {
487 throw new NullPointerException("this.currentReceipt is null");
488 } else if (this.getCurrentReceipt().getReceiptId() == null) {
490 throw new NullPointerException("this.currentReceipt.receiptId is null");
493 // Create new instance
494 final BillableReceipt receipt = this.createReceiptInstance();
497 if (Objects.equals(receipt, this.getCurrentReceipt())) {
498 // Yes, then abort here with message to user
499 this.showFacesMessage("form-edit-receipt", "ADMIN_ERROR_FINANCIAL_RECEIPT_NOT_CHANGED", FacesMessage.SEVERITY_ERROR);
504 Receipts.copyAll(receipt, this.getCurrentReceipt());
506 // Create updated receipt instance
507 final BillableReceipt updatedReceipt;
511 updatedReceipt = this.adminReceiptBean.updateReceipt(this.getCurrentReceipt());
512 } catch (final ReceiptNotFoundException ex) {
514 throw new FaceletException(ex);
518 this.adminUpdatedReceiptEvent.fire(new ReceiptUpdatedEvent(updatedReceipt));
527 private void clear () {
529 this.setReceiptBarCodeNumber(null);
530 this.setReceiptBonusCard(null);
531 this.setReceiptBranchOffice(null);
532 this.setReceiptId(null);
533 this.setReceiptIssued(null);
534 this.setReceiptNumber(null);
535 this.setReceiptPaymentType(null);
536 this.setReceiptRegisterNumber(null);
537 this.setReceiptSellerEmployee(null);
538 this.setReceiptSequenceNumber(null);
539 this.setReceiptTransactionNumber(null);
540 this.setReceiptUser(null);
544 * Creates a new instance from all available data of this bean.
546 * @return Receipt instance
548 private BillableReceipt createReceiptInstance () {
549 // Create new instance with minimum required data
550 final BillableReceipt receipt = new FinancialReceipt(
551 this.getReceiptPaymentType(),
552 this.getReceiptBranchOffice(),
553 this.getReceiptIssued()
557 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
558 receipt.setReceiptBonusCard(this.getReceiptBonusCard());
559 receipt.setReceiptId(this.getReceiptId());
560 receipt.setReceiptNumber(this.getReceiptNumber());
561 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
562 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
563 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
564 receipt.setReceiptTransactionNumber(this.getReceiptTransactionNumber());
565 receipt.setReceiptUser(this.getReceiptUser());
567 // Return prepared instance