]> git.mxchange.org Git - jfinancials-war.git/blob
b114fafca34d59adf7cc497de1959b2155f76f56
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017, 2018 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.financial.model.receipt_item;
18
19 import java.math.BigDecimal;
20 import java.text.MessageFormat;
21 import javax.ejb.EJB;
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;
38
39 /**
40  * An administrative backing bean for receipt items
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 @Named ("adminReceiptItemController")
45 @RequestScoped
46 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 595_754_280_374_172L;
52
53         /**
54          * Event being fired when administrator has added a new receipt item
55          */
56         @Inject
57         @Any
58         private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
59
60         /**
61          * EJB for general financial receipt item purposes
62          */
63         @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
64         private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
65
66         /**
67          * Item brand name
68          */
69         private String itemBrandName;
70
71         /**
72          * Coupon number assigned with item
73          */
74         private String itemCouponNumber;
75
76         /**
77          * Whether the item is a discount on whole receipt or item
78          */
79         private Boolean itemIsDiscount;
80
81         /**
82          * Whether the item is a refund
83          */
84         private Boolean itemIsRefund;
85
86         /**
87          * Item manufacturer/producer
88          */
89         private BasicData itemManufacturer;
90
91         /**
92          * Item's number
93          */
94         private Long itemNumber;
95
96         /**
97          * Item product
98          */
99         private Product itemProduct;
100
101         /**
102          * Quantity of item
103          */
104         private BigDecimal itemQuantity;
105
106         /**
107          * Assigned receipt
108          */
109         private BillableReceipt itemReceipt;
110
111         /**
112          * Currency code like EUR or USD
113          */
114         private String productCurrencyCode;
115
116         /**
117          * Product's gross price
118          */
119         private BigDecimal productGrossPrice;
120
121         /**
122          * Product's net price
123          */
124         private BigDecimal productNetPrice;
125
126         /**
127          * Tax rate for this item
128          */
129         private BigDecimal productTaxRate;
130
131         /**
132          * General receipt item controller
133          */
134         @Inject
135         private FinancialsReceiptItemWebRequestController receiptItemController;
136
137         /**
138          * Default constructor
139          */
140         public FinancialAdminReceiptItemWebRequestBean () {
141                 // Call super constructor
142                 super();
143         }
144
145         /**
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
148          * thrown.
149          * <p>
150          * @return Link outcome
151          */
152         public String addReceiptItem () {
153                 // Are all required fields set?
154
155                 // Prepare receipt item instance
156                 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
157
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
162                 }
163
164                 // Init variable
165                 final BillableReceiptItem updatedReceiptItem;
166
167                 // All is set, then try to call EJB
168                 try {
169                         // Add it
170                         updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
171                 } catch (final ReceiptItemAlreadyAddedException ex) {
172                         // Throw it again
173                         throw new FaceletException(ex);
174                 }
175
176                 // Fire event with updated instance
177                 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
178
179                 // Clear bean
180                 this.clear();
181
182                 // Return redirect outcome
183                 return ""; //NOI18N
184         }
185
186         /**
187          * Getter for item brand name
188          * <p>
189          * @return Item brand name
190          */
191         public String getItemBrandName () {
192                 return this.itemBrandName;
193         }
194
195         /**
196          * Setter for item brand name
197          * <p>
198          * @param itemBrandName Item brand name
199          */
200         public void setItemBrandName (final String itemBrandName) {
201                 this.itemBrandName = itemBrandName;
202         }
203
204         /**
205          * Getter for item's coupon number
206          * <p>
207          * @return Item's coupon number
208          */
209         public String getItemCouponNumber () {
210                 return this.itemCouponNumber;
211         }
212
213         /**
214          * Setter for item's coupon number
215          * <p>
216          * @param itemCouponNumber Item's coupon number
217          */
218         public void setItemCouponNumber (final String itemCouponNumber) {
219                 this.itemCouponNumber = itemCouponNumber;
220         }
221
222         /**
223          * Getter for whether the item is a discount on whole receipt or item
224          * <p>
225          * @return Whether the item is a discount on whole receipt or item
226          */
227         public Boolean getItemIsDiscount () {
228                 return this.itemIsDiscount;
229         }
230
231         /**
232          * Setter for whether the item is a discount on whole receipt or item
233          * <p>
234          * @param itemIsDiscount Whether the item is a discount on whole receipt or
235          *                       item
236          */
237         public void setItemIsDiscount (final Boolean itemIsDiscount) {
238                 this.itemIsDiscount = itemIsDiscount;
239         }
240
241         /**
242          * Getter for whether the item is a refund
243          * <p>
244          * @return Whether the item is a refund
245          */
246         public Boolean getItemIsRefund () {
247                 return this.itemIsRefund;
248         }
249
250         /**
251          * Setter for whether the item is a refund
252          * <p>
253          * @param itemIsRefund Whether the item is a refund
254          */
255         public void setItemIsRefund (final Boolean itemIsRefund) {
256                 this.itemIsRefund = itemIsRefund;
257         }
258
259         /**
260          * Getter for item manufacturer/producer
261          * <p>
262          * @return Item manufacturer/producer
263          */
264         public BasicData getItemManufacturer () {
265                 return this.itemManufacturer;
266         }
267
268         /**
269          * Setter for item manufacturer/producer
270          * <p>
271          * @param itemManufacturer Item manufacturer/producer
272          */
273         public void setItemManufacturer (final BasicData itemManufacturer) {
274                 this.itemManufacturer = itemManufacturer;
275         }
276
277         /**
278          * Getter for item's number
279          * <p>
280          * @return Item's number
281          */
282         public Long getItemNumber () {
283                 return this.itemNumber;
284         }
285
286         /**
287          * Setter for item's number
288          * <p>
289          * @param itemNumber Item's number
290          */
291         public void setItemNumber (final Long itemNumber) {
292                 this.itemNumber = itemNumber;
293         }
294
295         /**
296          * Getter for assigned product
297          * <p>
298          * @return Assigned product
299          */
300         public Product getItemProduct () {
301                 return this.itemProduct;
302         }
303
304         /**
305          * Setter for assigned product
306          * <p>
307          * @param itemProduct Assigned product
308          */
309         public void setItemProduct (final Product itemProduct) {
310                 this.itemProduct = itemProduct;
311         }
312
313         /**
314          * Getter for item quantity
315          * <p>
316          * @return Item quantity
317          */
318         public BigDecimal getItemQuantity () {
319                 return this.itemQuantity;
320         }
321
322         /**
323          * Setter for item quantity
324          * <p>
325          * @param itemQuantity Item quantity
326          */
327         public void setItemQuantity (final BigDecimal itemQuantity) {
328                 this.itemQuantity = itemQuantity;
329         }
330
331         /**
332          * Getter for assigned receipt
333          * <p>
334          * @return Assigned receipt
335          */
336         public BillableReceipt getItemReceipt () {
337                 return this.itemReceipt;
338         }
339
340         /**
341          * Setter for assigned receipt
342          * <p>
343          * @param itemReceipt Assigned receipt
344          */
345         public void setItemReceipt (final BillableReceipt itemReceipt) {
346                 this.itemReceipt = itemReceipt;
347         }
348
349         /**
350          * Getter for product's currency symbol
351          * <p>
352          * @return Product's currency symbol
353          */
354         public String getProductCurrencyCode () {
355                 return this.productCurrencyCode;
356         }
357
358         /**
359          * Setter for product's currency symbol
360          * <p>
361          * @param productCurrencyCode Product's currency symbol
362          */
363         public void setProductCurrencyCode (final String productCurrencyCode) {
364                 this.productCurrencyCode = productCurrencyCode;
365         }
366
367         /**
368          * Getter for product's gross price
369          * <p>
370          * @return Product's gross price
371          */
372         public BigDecimal getProductGrossPrice () {
373                 return this.productGrossPrice;
374         }
375
376         /**
377          * Setter for product's gross price
378          * <p>
379          * @param productGrossPrice Product's gross price
380          */
381         public void setProductGrossPrice (final BigDecimal productGrossPrice) {
382                 this.productGrossPrice = productGrossPrice;
383         }
384
385         /**
386          * Getter for product's net price
387          * <p>
388          * @return Product's net price
389          */
390         public BigDecimal getProductNetPrice () {
391                 return this.productNetPrice;
392         }
393
394         /**
395          * Setter for product's net price
396          * <p>
397          * @param productNetPrice Product's net price
398          */
399         public void setProductNetPrice (final BigDecimal productNetPrice) {
400                 this.productNetPrice = productNetPrice;
401         }
402
403         /**
404          * Getter for product's tax rate
405          * <p>
406          * @return Product's tax rate
407          */
408         public BigDecimal getProductTaxRate () {
409                 return this.productTaxRate;
410         }
411
412         /**
413          * Setter for product's tax rate
414          * <p>
415          * @param productTaxRate Product's tax rate
416          */
417         public void setProductTaxRate (final BigDecimal productTaxRate) {
418                 this.productTaxRate = productTaxRate;
419         }
420
421         /**
422          * Clears this bean
423          */
424         private void clear () {
425                 // Clear all fields
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);
439         }
440
441         /**
442          * Creates a new instance from all available data of this bean.
443          * <p>
444          * @return Receipt item instance
445          */
446         private BillableReceiptItem createReceiptItemInstance () {
447                 // Create new instance with minimum required data
448                 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
449
450                 // Set optional data
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());
460
461                 // Return prepared instance
462                 return receiptItem;
463         }
464
465 }