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_item.action;
19 import java.math.BigDecimal;
20 import java.text.MessageFormat;
21 import java.util.Objects;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.inject.Any;
26 import javax.faces.FacesException;
27 import javax.faces.application.FacesMessage;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
31 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
32 import org.mxchange.jfinancials.beans.financial.model.receipt_item.list.FinancialsReceiptItemListWebViewController;
33 import org.mxchange.jfinancials.events.receipt_item.added.AdminReceiptItemAddedEvent;
34 import org.mxchange.jfinancials.events.receipt_item.added.ObservableAdminReceiptItemAddedEvent;
35 import org.mxchange.jfinancials.events.receipt_item.updated.AdminReceiptItemUpdatedEvent;
36 import org.mxchange.jfinancials.events.receipt_item.updated.ObservableAdminReceiptItemUpdatedEvent;
37 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException;
38 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemNotFoundException;
39 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
40 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
41 import org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote;
42 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItem;
43 import org.mxchange.jfinancials.model.utils.ReceiptItemUtils;
44 import org.mxchange.jproduct.model.product.Product;
47 * An administrative action backing bean for receipt items
49 * @author Roland Häder<roland@mxchange.org>
51 @Named ("adminReceiptItemActionController")
53 public class FinancialAdminReceiptItemActionWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemActionWebRequestController {
58 private static final long serialVersionUID = 595_754_280_374_173L;
61 * Event being fired when administrator has added a new receipt item
65 private Event<ObservableAdminReceiptItemAddedEvent> adminAddedReceiptItemEvent;
68 * EJB for general financial receipt item purposes
70 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
71 private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
74 * Event being fired when an administrator has updated a receipt item
79 private Event<ObservableAdminReceiptItemUpdatedEvent> adminUpdatedReceiptItemEvent;
82 * Currently worked on receipt item
84 private BillableReceiptItem currentReceiptItem;
89 private String itemBrandName;
92 * Coupon number assigned with item
94 private String itemCouponNumber;
97 * Product's gross price
99 private BigDecimal itemGrossPrice;
107 * Whether the item is a discount on whole receipt or item
109 private Boolean itemIsDiscount;
112 * Whether the item is a refund
114 private Boolean itemIsRefund;
117 * Item manufacturer/producer
119 private BasicData itemManufacturer;
122 * Product's net price
124 private BigDecimal itemNetPrice;
129 private Long itemNumber;
134 private Product itemProduct;
139 private BigDecimal itemProductQuantity;
144 private BillableReceipt itemReceipt;
147 * Tax rate for this item
149 private BigDecimal itemTaxRate;
152 * Receipt item list controller
155 private FinancialsReceiptItemListWebViewController receiptItemListController;
158 * Default constructor
160 public FinancialAdminReceiptItemActionWebRequestBean () {
161 // Call super constructor
166 * Adds the completed receipt item to database by calling an EJB business
167 * method. If not all required fields are set, a proper exception is being
171 public void addReceiptItem () {
172 // Are all required fields set?
174 // Prepare receipt item instance
175 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
177 // Is the receipt already there?
178 if (this.receiptItemListController.isReceiptItemAdded(receiptItem)) {
179 // Receipt has already been added
180 throw new FacesException(MessageFormat.format("Receipt for itemReceipt.receiptId={0},itemProduct.productId={1},itemProductQuantity={2} has already been added.", receiptItem.getItemReceipt().getReceiptId(), receiptItem.getItemProduct().getProductId(), receiptItem.getItemProductQuantity())); //NOI18N
184 final BillableReceiptItem updatedReceiptItem;
186 // All is set, then try to call EJB
189 updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
190 } catch (final ReceiptItemAlreadyAddedException ex) {
192 throw new FacesException(ex);
195 // Fire event with updated instance
196 this.adminAddedReceiptItemEvent.fire(new AdminReceiptItemAddedEvent(updatedReceiptItem));
203 * Copies all receipt item properties from currentReceiptItem instance
205 public void copyAllReceiptItemProperties () {
206 // Is the instance set?
207 if (this.getCurrentReceiptItem() == null) {
209 throw new NullPointerException("this.currrentReceiptItem is null"); //NOI18N
210 } else if (this.getCurrentReceiptItem().getItemId() == null) {
212 throw new NullPointerException("this.currrentReceiptItem.itemId is null"); //NOI18N
213 } else if (this.getCurrentReceiptItem().getItemId() < 1) {
215 throw new IllegalArgumentException(MessageFormat.format("this.currrentReceiptItem.itemId={0} is invalid", this.getCurrentReceiptItem().getItemId())); //NOI18N
218 // Copy all item fields
219 this.setItemBrandName(this.getCurrentReceiptItem().getItemBrandName());
220 this.setItemCouponNumber(this.getCurrentReceiptItem().getItemCouponNumber());
221 this.setItemGrossPrice(this.getCurrentReceiptItem().getItemGrossPrice());
222 this.setItemId(this.getCurrentReceiptItem().getItemId());
223 this.setItemIsDiscount(this.getCurrentReceiptItem().getItemIsDiscount());
224 this.setItemIsRefund(this.getCurrentReceiptItem().getItemIsRefund());
225 this.setItemManufacturer(this.getCurrentReceiptItem().getItemManufacturer());
226 this.setItemNetPrice(this.getCurrentReceiptItem().getItemNetPrice());
227 this.setItemNumber(this.getCurrentReceiptItem().getItemNumber());
228 this.setItemProduct(this.getCurrentReceiptItem().getItemProduct());
229 this.setItemProductQuantity(this.getCurrentReceiptItem().getItemProductQuantity());
230 this.setItemReceipt(this.getCurrentReceiptItem().getItemReceipt());
231 this.setItemTaxRate(this.getCurrentReceiptItem().getItemTaxRate());
235 * Getter for current receipt item
237 * @return Current receipt item
239 public BillableReceiptItem getCurrentReceiptItem () {
240 return this.currentReceiptItem;
244 * Setter for current receipt item
246 * @param currentReceiptItem Current receipt item
248 public void setCurrentReceiptItem (final BillableReceiptItem currentReceiptItem) {
249 this.currentReceiptItem = currentReceiptItem;
253 * Getter for item brand name
255 * @return Item brand name
257 public String getItemBrandName () {
258 return this.itemBrandName;
262 * Setter for item brand name
264 * @param itemBrandName Item brand name
266 public void setItemBrandName (final String itemBrandName) {
267 this.itemBrandName = itemBrandName;
271 * Getter for item's coupon number
273 * @return Item's coupon number
275 public String getItemCouponNumber () {
276 return this.itemCouponNumber;
280 * Setter for item's coupon number
282 * @param itemCouponNumber Item's coupon number
284 public void setItemCouponNumber (final String itemCouponNumber) {
285 this.itemCouponNumber = itemCouponNumber;
289 * Getter for product's gross price
291 * @return Product's gross price
293 public BigDecimal getItemGrossPrice () {
294 return this.itemGrossPrice;
298 * Setter for product's gross price
300 * @param itemGrossPrice Product's gross price
302 public void setItemGrossPrice (final BigDecimal itemGrossPrice) {
303 this.itemGrossPrice = itemGrossPrice;
307 * Getter for item's primary key
309 * @return Item's primary key
311 public Long getItemId () {
316 * Setter for item's primary key
318 * @param itemId Item's primary key
320 public void setItemId (final Long itemId) {
321 this.itemId = itemId;
325 * Getter for whether the item is a discount on whole receipt or item
327 * @return Whether the item is a discount on whole receipt or item
329 public Boolean getItemIsDiscount () {
330 return this.itemIsDiscount;
334 * Setter for whether the item is a discount on whole receipt or item
336 * @param itemIsDiscount Whether the item is a discount on whole receipt or
339 public void setItemIsDiscount (final Boolean itemIsDiscount) {
340 this.itemIsDiscount = itemIsDiscount;
344 * Getter for whether the item is a refund
346 * @return Whether the item is a refund
348 public Boolean getItemIsRefund () {
349 return this.itemIsRefund;
353 * Setter for whether the item is a refund
355 * @param itemIsRefund Whether the item is a refund
357 public void setItemIsRefund (final Boolean itemIsRefund) {
358 this.itemIsRefund = itemIsRefund;
362 * Getter for item manufacturer/producer
364 * @return Item manufacturer/producer
366 public BasicData getItemManufacturer () {
367 return this.itemManufacturer;
371 * Setter for item manufacturer/producer
373 * @param itemManufacturer Item manufacturer/producer
375 public void setItemManufacturer (final BasicData itemManufacturer) {
376 this.itemManufacturer = itemManufacturer;
380 * Getter for product's net price
382 * @return Product's net price
384 public BigDecimal getItemNetPrice () {
385 return this.itemNetPrice;
389 * Setter for product's net price
391 * @param itemNetPrice Product's net price
393 public void setItemNetPrice (final BigDecimal itemNetPrice) {
394 this.itemNetPrice = itemNetPrice;
398 * Getter for item's number
400 * @return Item's number
402 public Long getItemNumber () {
403 return this.itemNumber;
407 * Setter for item's number
409 * @param itemNumber Item's number
411 public void setItemNumber (final Long itemNumber) {
412 this.itemNumber = itemNumber;
416 * Getter for assigned product
418 * @return Assigned product
420 public Product getItemProduct () {
421 return this.itemProduct;
425 * Setter for assigned product
427 * @param itemProduct Assigned product
429 public void setItemProduct (final Product itemProduct) {
430 this.itemProduct = itemProduct;
434 * Getter for item product quantity
436 * @return Item product quantity
438 public BigDecimal getItemProductQuantity () {
439 return this.itemProductQuantity;
443 * Setter for item product quantity
445 * @param itemProductQuantity Item product quantity
447 public void setItemProductQuantity (final BigDecimal itemProductQuantity) {
448 this.itemProductQuantity = itemProductQuantity;
452 * Getter for assigned receipt
454 * @return Assigned receipt
456 public BillableReceipt getItemReceipt () {
457 return this.itemReceipt;
461 * Setter for assigned receipt
463 * @param itemReceipt Assigned receipt
465 public void setItemReceipt (final BillableReceipt itemReceipt) {
466 this.itemReceipt = itemReceipt;
470 * Getter for product's tax rate
472 * @return Product's tax rate
474 public BigDecimal getItemTaxRate () { //NOI18N
475 return this.itemTaxRate;
479 * Setter for product's tax rate
481 * @param itemTaxRate Product's tax rate
483 public void setItemTaxRate (final BigDecimal itemTaxRate) { //NOI18N
484 this.itemTaxRate = itemTaxRate;
488 * Updates currently worked on receipt item instance.
490 * @return Redirection outcome
492 public String updateReceiptItem () {
493 // Is current instance set?
494 if (this.getCurrentReceiptItem() == null) {
496 throw new NullPointerException("this.currentReceiptItem is null"); //NOI18N
497 } else if (this.getCurrentReceiptItem().getItemId() == null) {
499 throw new NullPointerException("this.currentReceiptItem.itemId is null"); //NOI18N
500 } else if (this.getCurrentReceiptItem().getItemId() < 1) {
502 throw new IllegalArgumentException(MessageFormat.format("this.currentReceiptItem.itemId={0} is null", this.getCurrentReceiptItem().getItemId())); //NOI18N
506 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
509 if (Objects.equals(receiptItem, this.getCurrentReceiptItem())) {
510 // Yes, then abort here with message to user
511 this.showFacesMessage("form-edit-receipt-item", "ADMIN_ERROR_FINANCIAL_RECEIPT_ITEM_NOT_CHANGED", FacesMessage.SEVERITY_WARN); //NOI18N
516 ReceiptItemUtils.copyReceiptItemData(receiptItem, this.getCurrentReceiptItem());
518 // Create updated receipt instance
519 final BillableReceiptItem updatedReceiptItem;
523 updatedReceiptItem = this.adminReceiptItemBean.updateReceiptItem(this.getCurrentReceiptItem());
524 } catch (final ReceiptItemNotFoundException ex) {
526 throw new FacesException(ex);
530 this.adminUpdatedReceiptItemEvent.fire(new AdminReceiptItemUpdatedEvent(updatedReceiptItem));
535 // Redirect to list view
536 return "admin_list_receipt_items"; //NOI18N
542 private void clear () {
544 this.setItemBrandName(null);
545 this.setItemCouponNumber(null);
546 this.setItemGrossPrice(null);
547 this.setItemId(null);
548 this.setItemIsDiscount(null);
549 this.setItemIsRefund(null);
550 this.setItemManufacturer(null);
551 this.setItemNetPrice(null);
552 this.setItemNumber(null);
553 this.setItemProduct(null);
554 this.setItemProductQuantity(null);
555 this.setItemReceipt(null);
556 this.setItemTaxRate(null);
560 * Creates a new instance from all available data of this bean.
562 * @return Receipt item instance
564 private BillableReceiptItem createReceiptItemInstance () {
565 // Create new instance with minimum required data
566 final BillableReceiptItem receiptItem = new FinancialReceiptItem(
567 this.getItemProduct(),
568 this.getItemProductQuantity(),
569 this.getItemReceipt()
573 receiptItem.setItemBrandName(this.getItemBrandName());
574 receiptItem.setItemCouponNumber(this.getItemCouponNumber());
575 receiptItem.setItemGrossPrice(this.getItemGrossPrice());
576 receiptItem.setItemId(this.getItemId());
577 receiptItem.setItemIsDiscount(this.getItemIsDiscount());
578 receiptItem.setItemIsRefund(this.getItemIsRefund());
579 receiptItem.setItemManufacturer(this.getItemManufacturer());
580 receiptItem.setItemNetPrice(this.getItemNetPrice());
581 receiptItem.setItemNumber(this.getItemNumber());
582 receiptItem.setItemTaxRate(this.getItemTaxRate());
584 // Is current instance given?
585 if (this.getCurrentReceiptItem() instanceof BillableReceiptItem) {
586 // Yes, copy created/updated instances
587 receiptItem.setItemEntryCreated(this.getCurrentReceiptItem().getItemEntryCreated());
588 receiptItem.setItemEntryUpdated(this.getCurrentReceiptItem().getItemEntryUpdated());
591 // Return prepared instance