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 * Discount on product price (if any) Valid: values 0...1 (1=100% discount)
67 private Float itemDiscount;
72 private Product itemProduct;
77 private Short itemQuantity;
82 private BillableReceipt itemReceipt;
85 * Currency code like EUR or USD
87 private String productCurrencyCode;
90 * Product's gross price
92 private Float productGrossPrice;
97 private Float productNetPrice;
100 * Tax rate for this item
102 private Float productTaxRate;
105 * General receipt item controller
108 private FinancialsReceiptItemWebRequestController receiptItemController;
111 * Default constructor
113 public FinancialAdminReceiptItemWebRequestBean () {
114 // Call super constructor
119 * Adds the completed receipt item to database by calling an EJB business
120 * method. If not all required fields are set, a proper exception is being
123 * @return Link outcome
125 public String addReceiptItem () {
126 // Are all required fields set?
128 // Prepare receipt item instance
129 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
131 // Is the receipt already there?
132 if (this.receiptItemController.isReceiptItemAdded(receiptItem)) {
133 // Receipt has already been added
134 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
138 final BillableReceiptItem updatedReceiptItem;
140 // All is set, then try to call EJB
143 updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
144 } catch (final ReceiptItemAlreadyAddedException ex) {
146 throw new FaceletException(ex);
149 // Fire event with updated instance
150 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
155 // Return redirect outcome
156 return "add_receipt_item?faces-redirect=true"; //NOI18N
160 * Getter for item discount
162 * @return Item discount
164 public Float getItemDiscount () {
165 return this.itemDiscount;
169 * Setter for item discount
171 * @param itemDiscount Item discount
173 public void setItemDiscount (final Float itemDiscount) {
174 this.itemDiscount = itemDiscount;
178 * Getter for assigned product
180 * @return Assigned product
182 public Product getItemProduct () {
183 return this.itemProduct;
187 * Setter for assigned product
189 * @param itemProduct Assigned product
191 public void setItemProduct (final Product itemProduct) {
192 this.itemProduct = itemProduct;
196 * Getter for item quantity
198 * @return Item quantity
200 public Short getItemQuantity () {
201 return this.itemQuantity;
205 * Setter for item quantity
207 * @param itemQuantity Item quantity
209 public void setItemQuantity (final Short itemQuantity) {
210 this.itemQuantity = itemQuantity;
214 * Getter for assigned receipt
216 * @return Assigned receipt
218 public BillableReceipt getItemReceipt () {
219 return this.itemReceipt;
223 * Setter for assigned receipt
225 * @param itemReceipt Assigned receipt
227 public void setItemReceipt (final BillableReceipt itemReceipt) {
228 this.itemReceipt = itemReceipt;
232 * Getter for product's currency symbol
234 * @return Product's currency symbol
236 public String getProductCurrencyCode () {
237 return this.productCurrencyCode;
241 * Setter for product's currency symbol
243 * @param productCurrencyCode Product's currency symbol
245 public void setProductCurrencyCode (final String productCurrencyCode) {
246 this.productCurrencyCode = productCurrencyCode;
250 * Getter for product's gross price
252 * @return Product's gross price
254 public Float getProductGrossPrice () {
255 return this.productGrossPrice;
259 * Setter for product's gross price
261 * @param productGrossPrice Product's gross price
263 public void setProductGrossPrice (final Float productGrossPrice) {
264 this.productGrossPrice = productGrossPrice;
268 * Getter for product's net price
270 * @return Product's net price
272 public Float getProductNetPrice () {
273 return this.productNetPrice;
277 * Setter for product's net price
279 * @param productNetPrice Product's net price
281 public void setProductNetPrice (final Float productNetPrice) {
282 this.productNetPrice = productNetPrice;
286 * Getter for product's tax rate
288 * @return Product's tax rate
290 public Float getProductTaxRate () {
291 return this.productTaxRate;
295 * Setter for product's tax rate
297 * @param productTaxRate Product's tax rate
299 public void setProductTaxRate (final Float productTaxRate) {
300 this.productTaxRate = productTaxRate;
306 private void clear () {
308 this.setItemDiscount(null);
309 this.setItemProduct(null);
310 this.setItemQuantity(null);
311 this.setItemReceipt(null);
315 * Creates a new instance from all available data of this bean.
317 * @return Receipt item instance
319 private BillableReceiptItem createReceiptItemInstance () {
320 // Create new instance with minimum required data
321 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
324 receiptItem.setItemProductDiscount(this.getItemDiscount());
326 // Return prepared instance