]> git.mxchange.org Git - jfinancials-war.git/blob
87c13e0cea1a2adc77b561220a13fd2ac636d007
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 - 2022 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.action;
18
19 import java.math.BigDecimal;
20 import java.text.MessageFormat;
21 import java.util.Objects;
22 import javax.ejb.EJB;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.inject.Any;
26 import javax.faces.FacesException;
27 import javax.faces.application.FacesMessage;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
31 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
32 import org.mxchange.jfinancials.beans.financial.model.receipt_item.list.FinancialsReceiptItemListWebViewController;
33 import org.mxchange.jfinancials.events.receipt_item.added.AdminReceiptItemAddedEvent;
34 import org.mxchange.jfinancials.events.receipt_item.added.ObservableAdminReceiptItemAddedEvent;
35 import org.mxchange.jfinancials.events.receipt_item.updated.AdminReceiptItemUpdatedEvent;
36 import org.mxchange.jfinancials.events.receipt_item.updated.ObservableAdminReceiptItemUpdatedEvent;
37 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException;
38 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemNotFoundException;
39 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
40 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
41 import org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote;
42 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItem;
43 import org.mxchange.jfinancials.model.utils.ReceiptItemUtils;
44 import org.mxchange.jproduct.model.product.Product;
45
46 /**
47  * An administrative action backing bean for receipt items
48  * <p>
49  * @author Roland Häder<roland@mxchange.org>
50  */
51 @Named ("adminReceiptItemActionController")
52 @RequestScoped
53 public class FinancialAdminReceiptItemActionWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemActionWebRequestController {
54
55         /**
56          * Serial number
57          */
58         private static final long serialVersionUID = 595_754_280_374_173L;
59
60         /**
61          * Event being fired when administrator has added a new receipt item
62          */
63         @Inject
64         @Any
65         private Event<ObservableAdminReceiptItemAddedEvent> adminAddedReceiptItemEvent;
66
67         /**
68          * EJB for general financial receipt item purposes
69          */
70         @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
71         private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
72
73         /**
74          * Event being fired when an administrator has updated a receipt item
75          * instance
76          */
77         @Any
78         @Inject
79         private Event<ObservableAdminReceiptItemUpdatedEvent> adminUpdatedReceiptItemEvent;
80
81         /**
82          * Currently worked on receipt item
83          */
84         private BillableReceiptItem currentReceiptItem;
85
86         /**
87          * Item brand name
88          */
89         private String itemBrandName;
90
91         /**
92          * Coupon number assigned with item
93          */
94         private String itemCouponNumber;
95
96         /**
97          * Product's gross price
98          */
99         private BigDecimal itemGrossPrice;
100
101         /**
102          * Item's primary key
103          */
104         private Long itemId;
105
106         /**
107          * Whether the item is a discount on whole receipt or item
108          */
109         private Boolean itemIsDiscount;
110
111         /**
112          * Whether the item is a refund
113          */
114         private Boolean itemIsRefund;
115
116         /**
117          * Item manufacturer/producer
118          */
119         private BasicData itemManufacturer;
120
121         /**
122          * Product's net price
123          */
124         private BigDecimal itemNetPrice;
125
126         /**
127          * Item's number
128          */
129         private Long itemNumber;
130
131         /**
132          * Item product
133          */
134         private Product itemProduct;
135
136         /**
137          * Quantity of item
138          */
139         private BigDecimal itemProductQuantity;
140
141         /**
142          * Assigned receipt
143          */
144         private BillableReceipt itemReceipt;
145
146         /**
147          * Tax rate for this item
148          */
149         private BigDecimal itemTaxRate;
150
151         /**
152          * Receipt item list controller
153          */
154         @Inject
155         private FinancialsReceiptItemListWebViewController receiptItemListController;
156
157         /**
158          * Default constructor
159          */
160         public FinancialAdminReceiptItemActionWebRequestBean () {
161                 // Call super constructor
162                 super();
163         }
164
165         /**
166          * Adds the completed receipt item to database by calling an EJB business
167          * method. If not all required fields are set, a proper exception is being
168          * thrown.
169          * <p>
170          */
171         public void addReceiptItem () {
172                 // Are all required fields set?
173
174                 // Prepare receipt item instance
175                 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
176
177                 // Is the receipt already there?
178                 if (this.receiptItemListController.isReceiptItemAdded(receiptItem)) {
179                         // Receipt has already been added
180                         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
181                 }
182
183                 // Init variable
184                 final BillableReceiptItem updatedReceiptItem;
185
186                 // All is set, then try to call EJB
187                 try {
188                         // Add it
189                         updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
190                 } catch (final ReceiptItemAlreadyAddedException ex) {
191                         // Throw it again
192                         throw new FacesException(ex);
193                 }
194
195                 // Fire event with updated instance
196                 this.adminAddedReceiptItemEvent.fire(new AdminReceiptItemAddedEvent(updatedReceiptItem));
197
198                 // Clear bean
199                 this.clear();
200         }
201
202         /**
203          * Copies all receipt item properties from currentReceiptItem instance
204          */
205         public void copyAllReceiptItemProperties () {
206                 // Is the instance set?
207                 if (this.getCurrentReceiptItem() == null) {
208                         // Throw NPE
209                         throw new NullPointerException("this.currrentReceiptItem is null"); //NOI18N
210                 } else if (this.getCurrentReceiptItem().getItemId() == null) {
211                         // Throw NPE again
212                         throw new NullPointerException("this.currrentReceiptItem.itemId is null"); //NOI18N
213                 } else if (this.getCurrentReceiptItem().getItemId() < 1) {
214                         // Throw IAE
215                         throw new IllegalArgumentException(MessageFormat.format("this.currrentReceiptItem.itemId={0} is invalid", this.getCurrentReceiptItem().getItemId())); //NOI18N
216                 }
217
218                 // Copy all item fields
219                 this.setItemBrandName(this.getCurrentReceiptItem().getItemBrandName());
220                 this.setItemCouponNumber(this.getCurrentReceiptItem().getItemCouponNumber());
221                 this.setItemGrossPrice(this.getCurrentReceiptItem().getItemGrossPrice());
222                 this.setItemId(this.getCurrentReceiptItem().getItemId());
223                 this.setItemIsDiscount(this.getCurrentReceiptItem().getItemIsDiscount());
224                 this.setItemIsRefund(this.getCurrentReceiptItem().getItemIsRefund());
225                 this.setItemManufacturer(this.getCurrentReceiptItem().getItemManufacturer());
226                 this.setItemNetPrice(this.getCurrentReceiptItem().getItemNetPrice());
227                 this.setItemNumber(this.getCurrentReceiptItem().getItemNumber());
228                 this.setItemProduct(this.getCurrentReceiptItem().getItemProduct());
229                 this.setItemProductQuantity(this.getCurrentReceiptItem().getItemProductQuantity());
230                 this.setItemReceipt(this.getCurrentReceiptItem().getItemReceipt());
231                 this.setItemTaxRate(this.getCurrentReceiptItem().getItemTaxRate());
232         }
233
234         /**
235          * Getter for current receipt item
236          * <p>
237          * @return Current receipt item
238          */
239         public BillableReceiptItem getCurrentReceiptItem () {
240                 return this.currentReceiptItem;
241         }
242
243         /**
244          * Setter for current receipt item
245          * <p>
246          * @param currentReceiptItem Current receipt item
247          */
248         public void setCurrentReceiptItem (final BillableReceiptItem currentReceiptItem) {
249                 this.currentReceiptItem = currentReceiptItem;
250         }
251
252         /**
253          * Getter for item brand name
254          * <p>
255          * @return Item brand name
256          */
257         public String getItemBrandName () {
258                 return this.itemBrandName;
259         }
260
261         /**
262          * Setter for item brand name
263          * <p>
264          * @param itemBrandName Item brand name
265          */
266         public void setItemBrandName (final String itemBrandName) {
267                 this.itemBrandName = itemBrandName;
268         }
269
270         /**
271          * Getter for item's coupon number
272          * <p>
273          * @return Item's coupon number
274          */
275         public String getItemCouponNumber () {
276                 return this.itemCouponNumber;
277         }
278
279         /**
280          * Setter for item's coupon number
281          * <p>
282          * @param itemCouponNumber Item's coupon number
283          */
284         public void setItemCouponNumber (final String itemCouponNumber) {
285                 this.itemCouponNumber = itemCouponNumber;
286         }
287
288         /**
289          * Getter for product's gross price
290          * <p>
291          * @return Product's gross price
292          */
293         public BigDecimal getItemGrossPrice () {
294                 return this.itemGrossPrice;
295         }
296
297         /**
298          * Setter for product's gross price
299          * <p>
300          * @param itemGrossPrice Product's gross price
301          */
302         public void setItemGrossPrice (final BigDecimal itemGrossPrice) {
303                 this.itemGrossPrice = itemGrossPrice;
304         }
305
306         /**
307          * Getter for item's primary key
308          * <p>
309          * @return Item's primary key
310          */
311         public Long getItemId () {
312                 return this.itemId;
313         }
314
315         /**
316          * Setter for item's primary key
317          * <p>
318          * @param itemId Item's primary key
319          */
320         public void setItemId (final Long itemId) {
321                 this.itemId = itemId;
322         }
323
324         /**
325          * Getter for whether the item is a discount on whole receipt or item
326          * <p>
327          * @return Whether the item is a discount on whole receipt or item
328          */
329         public Boolean getItemIsDiscount () {
330                 return this.itemIsDiscount;
331         }
332
333         /**
334          * Setter for whether the item is a discount on whole receipt or item
335          * <p>
336          * @param itemIsDiscount Whether the item is a discount on whole receipt or
337          *                       item
338          */
339         public void setItemIsDiscount (final Boolean itemIsDiscount) {
340                 this.itemIsDiscount = itemIsDiscount;
341         }
342
343         /**
344          * Getter for whether the item is a refund
345          * <p>
346          * @return Whether the item is a refund
347          */
348         public Boolean getItemIsRefund () {
349                 return this.itemIsRefund;
350         }
351
352         /**
353          * Setter for whether the item is a refund
354          * <p>
355          * @param itemIsRefund Whether the item is a refund
356          */
357         public void setItemIsRefund (final Boolean itemIsRefund) {
358                 this.itemIsRefund = itemIsRefund;
359         }
360
361         /**
362          * Getter for item manufacturer/producer
363          * <p>
364          * @return Item manufacturer/producer
365          */
366         public BasicData getItemManufacturer () {
367                 return this.itemManufacturer;
368         }
369
370         /**
371          * Setter for item manufacturer/producer
372          * <p>
373          * @param itemManufacturer Item manufacturer/producer
374          */
375         public void setItemManufacturer (final BasicData itemManufacturer) {
376                 this.itemManufacturer = itemManufacturer;
377         }
378
379         /**
380          * Getter for product's net price
381          * <p>
382          * @return Product's net price
383          */
384         public BigDecimal getItemNetPrice () {
385                 return this.itemNetPrice;
386         }
387
388         /**
389          * Setter for product's net price
390          * <p>
391          * @param itemNetPrice Product's net price
392          */
393         public void setItemNetPrice (final BigDecimal itemNetPrice) {
394                 this.itemNetPrice = itemNetPrice;
395         }
396
397         /**
398          * Getter for item's number
399          * <p>
400          * @return Item's number
401          */
402         public Long getItemNumber () {
403                 return this.itemNumber;
404         }
405
406         /**
407          * Setter for item's number
408          * <p>
409          * @param itemNumber Item's number
410          */
411         public void setItemNumber (final Long itemNumber) {
412                 this.itemNumber = itemNumber;
413         }
414
415         /**
416          * Getter for assigned product
417          * <p>
418          * @return Assigned product
419          */
420         public Product getItemProduct () {
421                 return this.itemProduct;
422         }
423
424         /**
425          * Setter for assigned product
426          * <p>
427          * @param itemProduct Assigned product
428          */
429         public void setItemProduct (final Product itemProduct) {
430                 this.itemProduct = itemProduct;
431         }
432
433         /**
434          * Getter for item product quantity
435          * <p>
436          * @return Item product quantity
437          */
438         public BigDecimal getItemProductQuantity () {
439                 return this.itemProductQuantity;
440         }
441
442         /**
443          * Setter for item product quantity
444          * <p>
445          * @param itemProductQuantity Item product quantity
446          */
447         public void setItemProductQuantity (final BigDecimal itemProductQuantity) {
448                 this.itemProductQuantity = itemProductQuantity;
449         }
450
451         /**
452          * Getter for assigned receipt
453          * <p>
454          * @return Assigned receipt
455          */
456         public BillableReceipt getItemReceipt () {
457                 return this.itemReceipt;
458         }
459
460         /**
461          * Setter for assigned receipt
462          * <p>
463          * @param itemReceipt Assigned receipt
464          */
465         public void setItemReceipt (final BillableReceipt itemReceipt) {
466                 this.itemReceipt = itemReceipt;
467         }
468
469         /**
470          * Getter for product's tax rate
471          * <p>
472          * @return Product's tax rate
473          */
474         public BigDecimal getItemTaxRate () { //NOI18N
475                 return this.itemTaxRate;
476         }
477
478         /**
479          * Setter for product's tax rate
480          * <p>
481          * @param itemTaxRate Product's tax rate
482          */
483         public void setItemTaxRate (final BigDecimal itemTaxRate) { //NOI18N
484                 this.itemTaxRate = itemTaxRate;
485         }
486
487         /**
488          * Updates currently worked on receipt item instance.
489          * <p>
490          * @return Redirection outcome
491          */
492         public String updateReceiptItem () {
493                 // Is current instance set?
494                 if (this.getCurrentReceiptItem() == null) {
495                         // Throw NPE
496                         throw new NullPointerException("this.currentReceiptItem is null"); //NOI18N
497                 } else if (this.getCurrentReceiptItem().getItemId() == null) {
498                         // Throw NPE again
499                         throw new NullPointerException("this.currentReceiptItem.itemId is null"); //NOI18N
500                 } else if (this.getCurrentReceiptItem().getItemId() < 1) {
501                         // Throw IAE
502                         throw new IllegalArgumentException(MessageFormat.format("this.currentReceiptItem.itemId={0} is null", this.getCurrentReceiptItem().getItemId())); //NOI18N
503                 }
504
505                 // Init instance
506                 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
507
508                 // Is it the same?
509                 if (Objects.equals(receiptItem, this.getCurrentReceiptItem())) {
510                         // Yes, then abort here with message to user
511                         this.showFacesMessage("form-edit-receipt-item", "ADMIN_ERROR_FINANCIAL_RECEIPT_ITEM_NOT_CHANGED", FacesMessage.SEVERITY_WARN); //NOI18N
512                         return ""; //NOI18N
513                 }
514
515                 // Copy all data
516                 ReceiptItemUtils.copyReceiptItemData(receiptItem, this.getCurrentReceiptItem());
517
518                 // Create updated receipt instance
519                 final BillableReceiptItem updatedReceiptItem;
520
521                 try {
522                         // Invoke EJB method
523                         updatedReceiptItem = this.adminReceiptItemBean.updateReceiptItem(this.getCurrentReceiptItem());
524                 } catch (final ReceiptItemNotFoundException ex) {
525                         // Throw as cause
526                         throw new FacesException(ex);
527                 }
528
529                 // Fire event
530                 this.adminUpdatedReceiptItemEvent.fire(new AdminReceiptItemUpdatedEvent(updatedReceiptItem));
531
532                 // Clear bean
533                 this.clear();
534
535                 // Redirect to list view
536                 return "admin_list_receipt_items"; //NOI18N
537         }
538
539         /**
540          * Clears this bean
541          */
542         private void clear () {
543                 // Clear all fields
544                 this.setItemBrandName(null);
545                 this.setItemCouponNumber(null);
546                 this.setItemGrossPrice(null);
547                 this.setItemId(null);
548                 this.setItemIsDiscount(null);
549                 this.setItemIsRefund(null);
550                 this.setItemManufacturer(null);
551                 this.setItemNetPrice(null);
552                 this.setItemNumber(null);
553                 this.setItemProduct(null);
554                 this.setItemProductQuantity(null);
555                 this.setItemReceipt(null);
556                 this.setItemTaxRate(null);
557         }
558
559         /**
560          * Creates a new instance from all available data of this bean.
561          * <p>
562          * @return Receipt item instance
563          */
564         private BillableReceiptItem createReceiptItemInstance () {
565                 // Create new instance with minimum required data
566                 final BillableReceiptItem receiptItem = new FinancialReceiptItem(
567                                                                   this.getItemProduct(),
568                                                                   this.getItemProductQuantity(),
569                                                                   this.getItemReceipt()
570                                                   );
571
572                 // Set optional data
573                 receiptItem.setItemBrandName(this.getItemBrandName());
574                 receiptItem.setItemCouponNumber(this.getItemCouponNumber());
575                 receiptItem.setItemGrossPrice(this.getItemGrossPrice());
576                 receiptItem.setItemId(this.getItemId());
577                 receiptItem.setItemIsDiscount(this.getItemIsDiscount());
578                 receiptItem.setItemIsRefund(this.getItemIsRefund());
579                 receiptItem.setItemManufacturer(this.getItemManufacturer());
580                 receiptItem.setItemNetPrice(this.getItemNetPrice());
581                 receiptItem.setItemNumber(this.getItemNumber());
582                 receiptItem.setItemTaxRate(this.getItemTaxRate());
583
584                 // Is current instance given?
585                 if (this.getCurrentReceiptItem() instanceof BillableReceiptItem) {
586                         // Yes, copy created/updated instances
587                         receiptItem.setItemEntryCreated(this.getCurrentReceiptItem().getItemEntryCreated());
588                         receiptItem.setItemEntryUpdated(this.getCurrentReceiptItem().getItemEntryUpdated());
589                 }
590
591                 // Return prepared instance
592                 return receiptItem;
593         }
594
595 }