2 * Copyright (C) 2017, 2018 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.view.facelets.FaceletException;
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.events.receipt_item.added.ObservableReceiptItemAddedEvent;
31 import org.mxchange.jfinancials.events.receipt_item.added.ReceiptItemAddedEvent;
32 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException;
33 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
34 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
35 import org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote;
36 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItem;
37 import org.mxchange.jproduct.model.product.Product;
40 * An administrative backing bean for receipt items
42 * @author Roland Häder<roland@mxchange.org>
44 @Named ("adminReceiptItemController")
46 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
51 private static final long serialVersionUID = 595_754_280_374_172L;
54 * Event being fired when administrator has added a new receipt item
58 private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
61 * EJB for general financial receipt item purposes
63 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
64 private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
69 private String itemBrandName;
72 * Coupon number assigned with item
74 private String itemCouponNumber;
77 * Whether the item is a discount on whole receipt or item
79 private Boolean itemIsDiscount;
82 * Whether the item is a refund
84 private Boolean itemIsRefund;
87 * Item manufacturer/producer
89 private BasicData itemManufacturer;
94 private Long itemNumber;
99 private Product itemProduct;
104 private BigDecimal itemQuantity;
109 private BillableReceipt itemReceipt;
112 * Currency code like EUR or USD
114 private String productCurrencyCode;
117 * Product's gross price
119 private BigDecimal productGrossPrice;
122 * Product's net price
124 private BigDecimal productNetPrice;
127 * Tax rate for this item
129 private BigDecimal productTaxRate;
132 * General receipt item controller
135 private FinancialsReceiptItemWebRequestController receiptItemController;
138 * Default constructor
140 public FinancialAdminReceiptItemWebRequestBean () {
141 // Call super constructor
146 * Adds the completed receipt item to database by calling an EJB business
147 * method. If not all required fields are set, a proper exception is being
150 * @return Link outcome
152 public String 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.receiptItemController.isReceiptItemAdded(receiptItem)) {
160 // Receipt has already been added
161 throw new FaceletException(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 FaceletException(ex);
176 // Fire event with updated instance
177 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
182 // Return redirect outcome
187 * Getter for item brand name
189 * @return Item brand name
191 public String getItemBrandName () {
192 return this.itemBrandName;
196 * Setter for item brand name
198 * @param itemBrandName Item brand name
200 public void setItemBrandName (final String itemBrandName) {
201 this.itemBrandName = itemBrandName;
205 * Getter for item's coupon number
207 * @return Item's coupon number
209 public String getItemCouponNumber () {
210 return this.itemCouponNumber;
214 * Setter for item's coupon number
216 * @param itemCouponNumber Item's coupon number
218 public void setItemCouponNumber (final String itemCouponNumber) {
219 this.itemCouponNumber = itemCouponNumber;
223 * Getter for whether the item is a discount on whole receipt or item
225 * @return Whether the item is a discount on whole receipt or item
227 public Boolean getItemIsDiscount () {
228 return this.itemIsDiscount;
232 * Setter for whether the item is a discount on whole receipt or item
234 * @param itemIsDiscount Whether the item is a discount on whole receipt or
237 public void setItemIsDiscount (final Boolean itemIsDiscount) {
238 this.itemIsDiscount = itemIsDiscount;
242 * Getter for whether the item is a refund
244 * @return Whether the item is a refund
246 public Boolean getItemIsRefund () {
247 return this.itemIsRefund;
251 * Setter for whether the item is a refund
253 * @param itemIsRefund Whether the item is a refund
255 public void setItemIsRefund (final Boolean itemIsRefund) {
256 this.itemIsRefund = itemIsRefund;
260 * Getter for item manufacturer/producer
262 * @return Item manufacturer/producer
264 public BasicData getItemManufacturer () {
265 return this.itemManufacturer;
269 * Setter for item manufacturer/producer
271 * @param itemManufacturer Item manufacturer/producer
273 public void setItemManufacturer (final BasicData itemManufacturer) {
274 this.itemManufacturer = itemManufacturer;
278 * Getter for item's number
280 * @return Item's number
282 public Long getItemNumber () {
283 return this.itemNumber;
287 * Setter for item's number
289 * @param itemNumber Item's number
291 public void setItemNumber (final Long itemNumber) {
292 this.itemNumber = itemNumber;
296 * Getter for assigned product
298 * @return Assigned product
300 public Product getItemProduct () {
301 return this.itemProduct;
305 * Setter for assigned product
307 * @param itemProduct Assigned product
309 public void setItemProduct (final Product itemProduct) {
310 this.itemProduct = itemProduct;
314 * Getter for item quantity
316 * @return Item quantity
318 public BigDecimal getItemQuantity () {
319 return this.itemQuantity;
323 * Setter for item quantity
325 * @param itemQuantity Item quantity
327 public void setItemQuantity (final BigDecimal itemQuantity) {
328 this.itemQuantity = itemQuantity;
332 * Getter for assigned receipt
334 * @return Assigned receipt
336 public BillableReceipt getItemReceipt () {
337 return this.itemReceipt;
341 * Setter for assigned receipt
343 * @param itemReceipt Assigned receipt
345 public void setItemReceipt (final BillableReceipt itemReceipt) {
346 this.itemReceipt = itemReceipt;
350 * Getter for product's currency symbol
352 * @return Product's currency symbol
354 public String getProductCurrencyCode () {
355 return this.productCurrencyCode;
359 * Setter for product's currency symbol
361 * @param productCurrencyCode Product's currency symbol
363 public void setProductCurrencyCode (final String productCurrencyCode) {
364 this.productCurrencyCode = productCurrencyCode;
368 * Getter for product's gross price
370 * @return Product's gross price
372 public BigDecimal getProductGrossPrice () {
373 return this.productGrossPrice;
377 * Setter for product's gross price
379 * @param productGrossPrice Product's gross price
381 public void setProductGrossPrice (final BigDecimal productGrossPrice) {
382 this.productGrossPrice = productGrossPrice;
386 * Getter for product's net price
388 * @return Product's net price
390 public BigDecimal getProductNetPrice () {
391 return this.productNetPrice;
395 * Setter for product's net price
397 * @param productNetPrice Product's net price
399 public void setProductNetPrice (final BigDecimal productNetPrice) {
400 this.productNetPrice = productNetPrice;
404 * Getter for product's tax rate
406 * @return Product's tax rate
408 public BigDecimal getProductTaxRate () {
409 return this.productTaxRate;
413 * Setter for product's tax rate
415 * @param productTaxRate Product's tax rate
417 public void setProductTaxRate (final BigDecimal productTaxRate) {
418 this.productTaxRate = productTaxRate;
424 private void clear () {
426 this.setItemBrandName(null);
427 this.setItemCouponNumber(null);
428 this.setItemIsDiscount(null);
429 this.setItemIsRefund(null);
430 this.setItemManufacturer(null);
431 this.setItemNumber(null);
432 this.setItemProduct(null);
433 this.setItemQuantity(null);
434 this.setItemReceipt(null);
435 this.setProductCurrencyCode(null);
436 this.setProductGrossPrice(null);
437 this.setProductNetPrice(null);
438 this.setProductTaxRate(null);
442 * Creates a new instance from all available data of this bean.
444 * @return Receipt item instance
446 private BillableReceiptItem createReceiptItemInstance () {
447 // Create new instance with minimum required data
448 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
451 receiptItem.setItemBrandName(this.getItemBrandName());
452 receiptItem.setItemCouponNumber(this.getItemCouponNumber());
453 receiptItem.setItemIsDiscount(this.getItemIsDiscount());
454 receiptItem.setItemIsRefund(this.getItemIsRefund());
455 receiptItem.setItemManufacturer(this.getItemManufacturer());
456 receiptItem.setItemNumber(this.getItemNumber());
457 receiptItem.setItemGrossPrice(this.getProductGrossPrice());
458 receiptItem.setItemNetPrice(this.getProductNetPrice());
459 receiptItem.setItemTaxRate(this.getProductTaxRate());
461 // Return prepared instance