]> git.mxchange.org Git - jfinancials-war.git/blob
3d21e850d2ba4d6fa5d70ac3b60fbe939ada792a
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 Roland Häder
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.text.MessageFormat;
20 import javax.ejb.EJB;
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;
36
37 /**
38  * An administrative backing bean for receipt items
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("adminReceiptItemController")
43 @RequestScoped
44 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 595_754_280_374_172L;
50
51         /**
52          * Event being fired when administrator has added a new receipt item
53          */
54         @Inject
55         @Any
56         private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
57
58         /**
59          * EJB for general financial receipt item purposes
60          */
61         @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
62         private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
63
64         /**
65          * Discount on product price (if any) Valid: values 0...1 (1=100% discount)
66          */
67         private Float itemDiscount;
68
69         /**
70          * Item product
71          */
72         private Product itemProduct;
73
74         /**
75          * Quantity of item
76          */
77         private Short itemQuantity;
78
79         /**
80          * Assigned receipt
81          */
82         private BillableReceipt itemReceipt;
83
84         /**
85          * Currency code like EUR or USD
86          */
87         private String productCurrencyCode;
88
89         /**
90          * Product's gross price
91          */
92         private Float productGrossPrice;
93
94         /**
95          * Product's net price
96          */
97         private Float productNetPrice;
98
99         /**
100          * Tax rate for this item
101          */
102         private Float productTaxRate;
103
104         /**
105          * General receipt item controller
106          */
107         @Inject
108         private FinancialsReceiptItemWebRequestController receiptItemController;
109
110         /**
111          * Default constructor
112          */
113         public FinancialAdminReceiptItemWebRequestBean () {
114                 // Call super constructor
115                 super();
116         }
117
118         /**
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
121          * thrown.
122          * <p>
123          * @return Link outcome
124          */
125         public String addReceiptItem () {
126                 // Are all required fields set?
127
128                 // Prepare receipt item instance
129                 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
130
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
135                 }
136
137                 // Init variable
138                 final BillableReceiptItem updatedReceiptItem;
139
140                 // All is set, then try to call EJB
141                 try {
142                         // Add it
143                         updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
144                 } catch (final ReceiptItemAlreadyAddedException ex) {
145                         // Throw it again
146                         throw new FaceletException(ex);
147                 }
148
149                 // Fire event with updated instance
150                 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
151
152                 // Clear bean
153                 this.clear();
154
155                 // Return redirect outcome
156                 return "add_receipt_item?faces-redirect=true"; //NOI18N
157         }
158
159         /**
160          * Getter for item discount
161          * <p>
162          * @return Item discount
163          */
164         public Float getItemDiscount () {
165                 return this.itemDiscount;
166         }
167
168         /**
169          * Setter for item discount
170          * <p>
171          * @param itemDiscount Item discount
172          */
173         public void setItemDiscount (final Float itemDiscount) {
174                 this.itemDiscount = itemDiscount;
175         }
176
177         /**
178          * Getter for assigned product
179          * <p>
180          * @return Assigned product
181          */
182         public Product getItemProduct () {
183                 return this.itemProduct;
184         }
185
186         /**
187          * Setter for assigned product
188          * <p>
189          * @param itemProduct Assigned product
190          */
191         public void setItemProduct (final Product itemProduct) {
192                 this.itemProduct = itemProduct;
193         }
194
195         /**
196          * Getter for item quantity
197          * <p>
198          * @return Item quantity
199          */
200         public Short getItemQuantity () {
201                 return this.itemQuantity;
202         }
203
204         /**
205          * Setter for item quantity
206          * <p>
207          * @param itemQuantity Item quantity
208          */
209         public void setItemQuantity (final Short itemQuantity) {
210                 this.itemQuantity = itemQuantity;
211         }
212
213         /**
214          * Getter for assigned receipt
215          * <p>
216          * @return Assigned receipt
217          */
218         public BillableReceipt getItemReceipt () {
219                 return this.itemReceipt;
220         }
221
222         /**
223          * Setter for assigned receipt
224          * <p>
225          * @param itemReceipt Assigned receipt
226          */
227         public void setItemReceipt (final BillableReceipt itemReceipt) {
228                 this.itemReceipt = itemReceipt;
229         }
230
231         /**
232          * Getter for product's currency symbol
233          * <p>
234          * @return Product's currency symbol
235          */
236         public String getProductCurrencyCode () {
237                 return this.productCurrencyCode;
238         }
239
240         /**
241          * Setter for product's currency symbol
242          * <p>
243          * @param productCurrencyCode Product's currency symbol
244          */
245         public void setProductCurrencyCode (final String productCurrencyCode) {
246                 this.productCurrencyCode = productCurrencyCode;
247         }
248
249         /**
250          * Getter for product's gross price
251          * <p>
252          * @return Product's gross price
253          */
254         public Float getProductGrossPrice () {
255                 return this.productGrossPrice;
256         }
257
258         /**
259          * Setter for product's gross price
260          * <p>
261          * @param productGrossPrice Product's gross price
262          */
263         public void setProductGrossPrice (final Float productGrossPrice) {
264                 this.productGrossPrice = productGrossPrice;
265         }
266
267         /**
268          * Getter for product's net price
269          * <p>
270          * @return Product's net price
271          */
272         public Float getProductNetPrice () {
273                 return this.productNetPrice;
274         }
275
276         /**
277          * Setter for product's net price
278          * <p>
279          * @param productNetPrice Product's net price
280          */
281         public void setProductNetPrice (final Float productNetPrice) {
282                 this.productNetPrice = productNetPrice;
283         }
284
285         /**
286          * Getter for product's tax rate
287          * <p>
288          * @return Product's tax rate
289          */
290         public Float getProductTaxRate () {
291                 return this.productTaxRate;
292         }
293
294         /**
295          * Setter for product's tax rate
296          * <p>
297          * @param productTaxRate Product's tax rate
298          */
299         public void setProductTaxRate (final Float productTaxRate) {
300                 this.productTaxRate = productTaxRate;
301         }
302
303         /**
304          * Clears this bean
305          */
306         private void clear () {
307                 // Clear all fields
308                 this.setItemDiscount(null);
309                 this.setItemProduct(null);
310                 this.setItemQuantity(null);
311                 this.setItemReceipt(null);
312         }
313
314         /**
315          * Creates a new instance from all available data of this bean.
316          * <p>
317          * @return Receipt item instance
318          */
319         private BillableReceiptItem createReceiptItemInstance () {
320                 // Create new instance with minimum required data
321                 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
322
323                 // Set optional data
324                 receiptItem.setItemProductDiscount(this.getItemDiscount());
325
326                 // Return prepared instance
327                 return receiptItem;
328         }
329
330 }