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_item;
19 import java.math.BigDecimal;
20 import java.text.MessageFormat;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.FacesException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
29 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
30 import org.mxchange.jfinancials.beans.financial.model.receipt_item.list.FinancialsReceiptItemListWebViewController;
31 import org.mxchange.jfinancials.events.receipt_item.added.ObservableReceiptItemAddedEvent;
32 import org.mxchange.jfinancials.events.receipt_item.added.ReceiptItemAddedEvent;
33 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException;
34 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
35 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
36 import org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote;
37 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItem;
38 import org.mxchange.jproduct.model.product.Product;
41 * An administrative backing bean for receipt items
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("adminReceiptItemController")
47 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
52 private static final long serialVersionUID = 595_754_280_374_172L;
55 * Event being fired when administrator has added a new receipt item
59 private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
62 * EJB for general financial receipt item purposes
64 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
65 private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
70 private String itemBrandName;
73 * Coupon number assigned with item
75 private String itemCouponNumber;
78 * Whether the item is a discount on whole receipt or item
80 private Boolean itemIsDiscount;
83 * Whether the item is a refund
85 private Boolean itemIsRefund;
88 * Item manufacturer/producer
90 private BasicData itemManufacturer;
95 private Long itemNumber;
100 private Product itemProduct;
105 private BigDecimal itemQuantity;
110 private BillableReceipt itemReceipt;
113 * Currency code like EUR or USD
115 private String productCurrencyCode;
118 * Product's gross price
120 private BigDecimal productGrossPrice;
123 * Product's net price
125 private BigDecimal productNetPrice;
128 * Tax rate for this item
130 private BigDecimal productTaxRate;
133 * Receipt item list controller
136 private FinancialsReceiptItemListWebViewController receiptItemListController;
139 * Default constructor
141 public FinancialAdminReceiptItemWebRequestBean () {
142 // Call super constructor
147 * Adds the completed receipt item to database by calling an EJB business
148 * method. If not all required fields are set, a proper exception is being
152 public void addReceiptItem () {
153 // Are all required fields set?
155 // Prepare receipt item instance
156 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
158 // Is the receipt already there?
159 if (this.receiptItemListController.isReceiptItemAdded(receiptItem)) {
160 // Receipt has already been added
161 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
165 final BillableReceiptItem updatedReceiptItem;
167 // All is set, then try to call EJB
170 updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
171 } catch (final ReceiptItemAlreadyAddedException ex) {
173 throw new FacesException(ex);
176 // Fire event with updated instance
177 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
184 * Getter for item brand name
186 * @return Item brand name
188 public String getItemBrandName () {
189 return this.itemBrandName;
193 * Setter for item brand name
195 * @param itemBrandName Item brand name
197 public void setItemBrandName (final String itemBrandName) {
198 this.itemBrandName = itemBrandName;
202 * Getter for item's coupon number
204 * @return Item's coupon number
206 public String getItemCouponNumber () {
207 return this.itemCouponNumber;
211 * Setter for item's coupon number
213 * @param itemCouponNumber Item's coupon number
215 public void setItemCouponNumber (final String itemCouponNumber) {
216 this.itemCouponNumber = itemCouponNumber;
220 * Getter for whether the item is a discount on whole receipt or item
222 * @return Whether the item is a discount on whole receipt or item
224 public Boolean getItemIsDiscount () {
225 return this.itemIsDiscount;
229 * Setter for whether the item is a discount on whole receipt or item
231 * @param itemIsDiscount Whether the item is a discount on whole receipt or
234 public void setItemIsDiscount (final Boolean itemIsDiscount) {
235 this.itemIsDiscount = itemIsDiscount;
239 * Getter for whether the item is a refund
241 * @return Whether the item is a refund
243 public Boolean getItemIsRefund () {
244 return this.itemIsRefund;
248 * Setter for whether the item is a refund
250 * @param itemIsRefund Whether the item is a refund
252 public void setItemIsRefund (final Boolean itemIsRefund) {
253 this.itemIsRefund = itemIsRefund;
257 * Getter for item manufacturer/producer
259 * @return Item manufacturer/producer
261 public BasicData getItemManufacturer () {
262 return this.itemManufacturer;
266 * Setter for item manufacturer/producer
268 * @param itemManufacturer Item manufacturer/producer
270 public void setItemManufacturer (final BasicData itemManufacturer) {
271 this.itemManufacturer = itemManufacturer;
275 * Getter for item's number
277 * @return Item's number
279 public Long getItemNumber () {
280 return this.itemNumber;
284 * Setter for item's number
286 * @param itemNumber Item's number
288 public void setItemNumber (final Long itemNumber) {
289 this.itemNumber = itemNumber;
293 * Getter for assigned product
295 * @return Assigned product
297 public Product getItemProduct () {
298 return this.itemProduct;
302 * Setter for assigned product
304 * @param itemProduct Assigned product
306 public void setItemProduct (final Product itemProduct) {
307 this.itemProduct = itemProduct;
311 * Getter for item quantity
313 * @return Item quantity
315 public BigDecimal getItemQuantity () {
316 return this.itemQuantity;
320 * Setter for item quantity
322 * @param itemQuantity Item quantity
324 public void setItemQuantity (final BigDecimal itemQuantity) {
325 this.itemQuantity = itemQuantity;
329 * Getter for assigned receipt
331 * @return Assigned receipt
333 public BillableReceipt getItemReceipt () {
334 return this.itemReceipt;
338 * Setter for assigned receipt
340 * @param itemReceipt Assigned receipt
342 public void setItemReceipt (final BillableReceipt itemReceipt) {
343 this.itemReceipt = itemReceipt;
347 * Getter for product's currency symbol
349 * @return Product's currency symbol
351 public String getProductCurrencyCode () {
352 return this.productCurrencyCode;
356 * Setter for product's currency symbol
358 * @param productCurrencyCode Product's currency symbol
360 public void setProductCurrencyCode (final String productCurrencyCode) {
361 this.productCurrencyCode = productCurrencyCode;
365 * Getter for product's gross price
367 * @return Product's gross price
369 public BigDecimal getProductGrossPrice () {
370 return this.productGrossPrice;
374 * Setter for product's gross price
376 * @param productGrossPrice Product's gross price
378 public void setProductGrossPrice (final BigDecimal productGrossPrice) {
379 this.productGrossPrice = productGrossPrice;
383 * Getter for product's net price
385 * @return Product's net price
387 public BigDecimal getProductNetPrice () {
388 return this.productNetPrice;
392 * Setter for product's net price
394 * @param productNetPrice Product's net price
396 public void setProductNetPrice (final BigDecimal productNetPrice) {
397 this.productNetPrice = productNetPrice;
401 * Getter for product's tax rate
403 * @return Product's tax rate
405 public BigDecimal getProductTaxRate () {
406 return this.productTaxRate;
410 * Setter for product's tax rate
412 * @param productTaxRate Product's tax rate
414 public void setProductTaxRate (final BigDecimal productTaxRate) {
415 this.productTaxRate = productTaxRate;
421 private void clear () {
423 this.setItemBrandName(null);
424 this.setItemCouponNumber(null);
425 this.setItemIsDiscount(null);
426 this.setItemIsRefund(null);
427 this.setItemManufacturer(null);
428 this.setItemNumber(null);
429 this.setItemProduct(null);
430 this.setItemQuantity(null);
431 this.setItemReceipt(null);
432 this.setProductCurrencyCode(null);
433 this.setProductGrossPrice(null);
434 this.setProductNetPrice(null);
435 this.setProductTaxRate(null);
439 * Creates a new instance from all available data of this bean.
441 * @return Receipt item instance
443 private BillableReceiptItem createReceiptItemInstance () {
444 // Create new instance with minimum required data
445 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
448 receiptItem.setItemBrandName(this.getItemBrandName());
449 receiptItem.setItemCouponNumber(this.getItemCouponNumber());
450 receiptItem.setItemIsDiscount(this.getItemIsDiscount());
451 receiptItem.setItemIsRefund(this.getItemIsRefund());
452 receiptItem.setItemManufacturer(this.getItemManufacturer());
453 receiptItem.setItemNumber(this.getItemNumber());
454 receiptItem.setItemGrossPrice(this.getProductGrossPrice());
455 receiptItem.setItemNetPrice(this.getProductNetPrice());
456 receiptItem.setItemTaxRate(this.getProductTaxRate());
458 // Return prepared instance