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.jcontactsbusiness.model.basicdata.BasicData;
28 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
29 import org.mxchange.jfinancials.events.receipt_item.added.ObservableReceiptItemAddedEvent;
30 import org.mxchange.jfinancials.events.receipt_item.added.ReceiptItemAddedEvent;
31 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException;
32 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
33 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
34 import org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote;
35 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItem;
36 import org.mxchange.jproduct.model.product.Product;
39 * An administrative backing bean for receipt items
41 * @author Roland Häder<roland@mxchange.org>
43 @Named ("adminReceiptItemController")
45 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
50 private static final long serialVersionUID = 595_754_280_374_172L;
53 * Event being fired when administrator has added a new receipt item
57 private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
60 * EJB for general financial receipt item purposes
62 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
63 private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
68 private String itemBrandName;
71 * Fixed discount on product price (if any)
73 private Float itemDiscountFixed;
76 * Percentage discount on product price (if any) Valid: values 0...1 (1=100%
79 private Float itemDiscountPercent;
84 private Product itemProduct;
89 private Short itemQuantity;
94 private BillableReceipt itemReceipt;
97 * Currency code like EUR or USD
99 private String productCurrencyCode;
102 * Product's gross price
104 private Float productGrossPrice;
107 * Item manufacturer/producer
109 private BasicData itemManufacturer;
112 * Product's net price
114 private Float productNetPrice;
117 * Tax rate for this item
119 private Float productTaxRate;
122 * General receipt item controller
125 private FinancialsReceiptItemWebRequestController receiptItemController;
128 * Default constructor
130 public FinancialAdminReceiptItemWebRequestBean () {
131 // Call super constructor
136 * Adds the completed receipt item to database by calling an EJB business
137 * method. If not all required fields are set, a proper exception is being
140 * @return Link outcome
142 public String addReceiptItem () {
143 // Are all required fields set?
145 // Prepare receipt item instance
146 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
148 // Is the receipt already there?
149 if (this.receiptItemController.isReceiptItemAdded(receiptItem)) {
150 // Receipt has already been added
151 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
155 final BillableReceiptItem updatedReceiptItem;
157 // All is set, then try to call EJB
160 updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
161 } catch (final ReceiptItemAlreadyAddedException ex) {
163 throw new FaceletException(ex);
166 // Fire event with updated instance
167 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
172 // Return redirect outcome
177 * Getter for item brand name
179 * @return Item brand name
181 public String getItemBrandName () {
182 return this.itemBrandName;
186 * Setter for item brand name
188 * @param itemBrandName Item brand name
190 public void setItemBrandName (final String itemBrandName) {
191 this.itemBrandName = itemBrandName;
195 * Getter for item discount (fixed)
197 * @return Item discount (fixed)
199 public Float getItemDiscountFixed () {
200 return this.itemDiscountFixed;
204 * Setter for item discount (fixed)
206 * @param itemDiscountFixed Item discount (fixed)
208 public void setItemDiscountFixed (final Float itemDiscountFixed) {
209 this.itemDiscountFixed = itemDiscountFixed;
213 * Getter for item discount (percent)
215 * @return Item discount (percent)
217 public Float getItemDiscountPercent () {
218 return this.itemDiscountPercent;
222 * Setter for item discount (percent)
224 * @param itemDiscountPercent Item discount (percent)
226 public void setItemDiscountPercent (final Float itemDiscountPercent) {
227 this.itemDiscountPercent = itemDiscountPercent;
231 * Getter for item manufacturer/producer
233 * @return Item manufacturer/producer
235 public BasicData getItemManufacturer () {
236 return this.itemManufacturer;
240 * Setter for item manufacturer/producer
242 * @param itemManufacturer Item manufacturer/producer
244 public void setItemManufacturer (final BasicData itemManufacturer) {
245 this.itemManufacturer = itemManufacturer;
249 * Getter for assigned product
251 * @return Assigned product
253 public Product getItemProduct () {
254 return this.itemProduct;
258 * Setter for assigned product
260 * @param itemProduct Assigned product
262 public void setItemProduct (final Product itemProduct) {
263 this.itemProduct = itemProduct;
267 * Getter for item quantity
269 * @return Item quantity
271 public Short getItemQuantity () {
272 return this.itemQuantity;
276 * Setter for item quantity
278 * @param itemQuantity Item quantity
280 public void setItemQuantity (final Short itemQuantity) {
281 this.itemQuantity = itemQuantity;
285 * Getter for assigned receipt
287 * @return Assigned receipt
289 public BillableReceipt getItemReceipt () {
290 return this.itemReceipt;
294 * Setter for assigned receipt
296 * @param itemReceipt Assigned receipt
298 public void setItemReceipt (final BillableReceipt itemReceipt) {
299 this.itemReceipt = itemReceipt;
303 * Getter for product's currency symbol
305 * @return Product's currency symbol
307 public String getProductCurrencyCode () {
308 return this.productCurrencyCode;
312 * Setter for product's currency symbol
314 * @param productCurrencyCode Product's currency symbol
316 public void setProductCurrencyCode (final String productCurrencyCode) {
317 this.productCurrencyCode = productCurrencyCode;
321 * Getter for product's gross price
323 * @return Product's gross price
325 public Float getProductGrossPrice () {
326 return this.productGrossPrice;
330 * Setter for product's gross price
332 * @param productGrossPrice Product's gross price
334 public void setProductGrossPrice (final Float productGrossPrice) {
335 this.productGrossPrice = productGrossPrice;
339 * Getter for product's net price
341 * @return Product's net price
343 public Float getProductNetPrice () {
344 return this.productNetPrice;
348 * Setter for product's net price
350 * @param productNetPrice Product's net price
352 public void setProductNetPrice (final Float productNetPrice) {
353 this.productNetPrice = productNetPrice;
357 * Getter for product's tax rate
359 * @return Product's tax rate
361 public Float getProductTaxRate () {
362 return this.productTaxRate;
366 * Setter for product's tax rate
368 * @param productTaxRate Product's tax rate
370 public void setProductTaxRate (final Float productTaxRate) {
371 this.productTaxRate = productTaxRate;
377 private void clear () {
379 this.setItemBrandName(null);
380 this.setItemDiscountFixed(null);
381 this.setItemDiscountPercent(null);
382 this.setItemManufacturer(null);
383 this.setItemProduct(null);
384 this.setItemQuantity(null);
385 this.setItemReceipt(null);
389 * Creates a new instance from all available data of this bean.
391 * @return Receipt item instance
393 private BillableReceiptItem createReceiptItemInstance () {
394 // Create new instance with minimum required data
395 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
398 receiptItem.setItemBrandName(this.getItemBrandName());
399 receiptItem.setItemDiscountFixed(this.getItemDiscountFixed());
400 receiptItem.setItemDiscountPercent(this.getItemDiscountPercent());
401 receiptItem.setItemManufacturer(this.getItemManufacturer());
403 // Return prepared instance