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