2 * Copyright (C) 2017 Roland Häder
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.text.MessageFormat;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
28 import org.mxchange.jfinancials.events.receipt_item.added.ObservableReceiptItemAddedEvent;
29 import org.mxchange.jfinancials.events.receipt_item.added.ReceiptItemAddedEvent;
30 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException;
31 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
32 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
33 import org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote;
34 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItem;
35 import org.mxchange.jproduct.model.product.Product;
38 * An administrative backing bean for receipt items
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("adminReceiptItemController")
44 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
49 private static final long serialVersionUID = 595_754_280_374_172L;
52 * Event being fired when administrator has added a new receipt item
56 private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
59 * EJB for general financial receipt item purposes
61 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
62 private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
65 * Fixed discount on product price (if any)
67 private Float itemDiscountFixed;
70 * Percentage discount on product price (if any) Valid: values 0...1 (1=100%
73 private Float itemDiscountPercent;
78 private Product itemProduct;
83 private Short itemQuantity;
88 private BillableReceipt itemReceipt;
91 * Currency code like EUR or USD
93 private String productCurrencyCode;
96 * Product's gross price
98 private Float productGrossPrice;
101 * Product's net price
103 private Float productNetPrice;
106 * Tax rate for this item
108 private Float productTaxRate;
111 * General receipt item controller
114 private FinancialsReceiptItemWebRequestController receiptItemController;
117 * Default constructor
119 public FinancialAdminReceiptItemWebRequestBean () {
120 // Call super constructor
125 * Adds the completed receipt item to database by calling an EJB business
126 * method. If not all required fields are set, a proper exception is being
129 * @return Link outcome
131 public String addReceiptItem () {
132 // Are all required fields set?
134 // Prepare receipt item instance
135 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
137 // Is the receipt already there?
138 if (this.receiptItemController.isReceiptItemAdded(receiptItem)) {
139 // Receipt has already been added
140 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
144 final BillableReceiptItem updatedReceiptItem;
146 // All is set, then try to call EJB
149 updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
150 } catch (final ReceiptItemAlreadyAddedException ex) {
152 throw new FaceletException(ex);
155 // Fire event with updated instance
156 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
161 // Return redirect outcome
162 return "add_receipt_item?faces-redirect=true"; //NOI18N
166 * Getter for item discount (fixed)
168 * @return Item discount (fixed)
170 public Float getItemDiscountFixed () {
171 return this.itemDiscountFixed;
175 * Setter for item discount (fixed)
177 * @param itemDiscountFixed Item discount (fixed)
179 public void setItemDiscountFixed (final Float itemDiscountFixed) {
180 this.itemDiscountFixed = itemDiscountFixed;
184 * Getter for item discount (percent)
186 * @return Item discount (percent)
188 public Float getItemDiscountPercent () {
189 return this.itemDiscountPercent;
193 * Setter for item discount (percent)
195 * @param itemDiscountPercent Item discount (percent)
197 public void setItemDiscountPercent (final Float itemDiscountPercent) {
198 this.itemDiscountPercent = itemDiscountPercent;
202 * Getter for assigned product
204 * @return Assigned product
206 public Product getItemProduct () {
207 return this.itemProduct;
211 * Setter for assigned product
213 * @param itemProduct Assigned product
215 public void setItemProduct (final Product itemProduct) {
216 this.itemProduct = itemProduct;
220 * Getter for item quantity
222 * @return Item quantity
224 public Short getItemQuantity () {
225 return this.itemQuantity;
229 * Setter for item quantity
231 * @param itemQuantity Item quantity
233 public void setItemQuantity (final Short itemQuantity) {
234 this.itemQuantity = itemQuantity;
238 * Getter for assigned receipt
240 * @return Assigned receipt
242 public BillableReceipt getItemReceipt () {
243 return this.itemReceipt;
247 * Setter for assigned receipt
249 * @param itemReceipt Assigned receipt
251 public void setItemReceipt (final BillableReceipt itemReceipt) {
252 this.itemReceipt = itemReceipt;
256 * Getter for product's currency symbol
258 * @return Product's currency symbol
260 public String getProductCurrencyCode () {
261 return this.productCurrencyCode;
265 * Setter for product's currency symbol
267 * @param productCurrencyCode Product's currency symbol
269 public void setProductCurrencyCode (final String productCurrencyCode) {
270 this.productCurrencyCode = productCurrencyCode;
274 * Getter for product's gross price
276 * @return Product's gross price
278 public Float getProductGrossPrice () {
279 return this.productGrossPrice;
283 * Setter for product's gross price
285 * @param productGrossPrice Product's gross price
287 public void setProductGrossPrice (final Float productGrossPrice) {
288 this.productGrossPrice = productGrossPrice;
292 * Getter for product's net price
294 * @return Product's net price
296 public Float getProductNetPrice () {
297 return this.productNetPrice;
301 * Setter for product's net price
303 * @param productNetPrice Product's net price
305 public void setProductNetPrice (final Float productNetPrice) {
306 this.productNetPrice = productNetPrice;
310 * Getter for product's tax rate
312 * @return Product's tax rate
314 public Float getProductTaxRate () {
315 return this.productTaxRate;
319 * Setter for product's tax rate
321 * @param productTaxRate Product's tax rate
323 public void setProductTaxRate (final Float productTaxRate) {
324 this.productTaxRate = productTaxRate;
330 private void clear () {
332 this.setItemDiscountFixed(null);
333 this.setItemDiscountPercent(null);
334 this.setItemProduct(null);
335 this.setItemQuantity(null);
336 this.setItemReceipt(null);
340 * Creates a new instance from all available data of this bean.
342 * @return Receipt item instance
344 private BillableReceiptItem createReceiptItemInstance () {
345 // Create new instance with minimum required data
346 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
349 receiptItem.setItemDiscountFixed(this.getItemDiscountFixed());
350 receiptItem.setItemDiscountPercent(this.getItemDiscountPercent());
352 // Return prepared instance