]> git.mxchange.org Git - jfinancials-war.git/blob
34329211c8cb9cbc08197dc355f3afa27e81a77c
[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          * Fixed discount on product price (if any)
66          */
67         private Float itemDiscountFixed;
68
69         /**
70          * Percentage discount on product price (if any) Valid: values 0...1 (1=100%
71          * discount)
72          */
73         private Float itemDiscountPercent;
74
75         /**
76          * Item product
77          */
78         private Product itemProduct;
79
80         /**
81          * Quantity of item
82          */
83         private Short itemQuantity;
84
85         /**
86          * Assigned receipt
87          */
88         private BillableReceipt itemReceipt;
89
90         /**
91          * Currency code like EUR or USD
92          */
93         private String productCurrencyCode;
94
95         /**
96          * Product's gross price
97          */
98         private Float productGrossPrice;
99
100         /**
101          * Product's net price
102          */
103         private Float productNetPrice;
104
105         /**
106          * Tax rate for this item
107          */
108         private Float productTaxRate;
109
110         /**
111          * General receipt item controller
112          */
113         @Inject
114         private FinancialsReceiptItemWebRequestController receiptItemController;
115
116         /**
117          * Default constructor
118          */
119         public FinancialAdminReceiptItemWebRequestBean () {
120                 // Call super constructor
121                 super();
122         }
123
124         /**
125          * Adds the completed receipt item to database by calling an EJB business
126          * method. If not all required fields are set, a proper exception is being
127          * thrown.
128          * <p>
129          * @return Link outcome
130          */
131         public String addReceiptItem () {
132                 // Are all required fields set?
133
134                 // Prepare receipt item instance
135                 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
136
137                 // Is the receipt already there?
138                 if (this.receiptItemController.isReceiptItemAdded(receiptItem)) {
139                         // Receipt has already been added
140                         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
141                 }
142
143                 // Init variable
144                 final BillableReceiptItem updatedReceiptItem;
145
146                 // All is set, then try to call EJB
147                 try {
148                         // Add it
149                         updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
150                 } catch (final ReceiptItemAlreadyAddedException ex) {
151                         // Throw it again
152                         throw new FaceletException(ex);
153                 }
154
155                 // Fire event with updated instance
156                 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
157
158                 // Clear bean
159                 this.clear();
160
161                 // Return redirect outcome
162                 return "add_receipt_item?faces-redirect=true"; //NOI18N
163         }
164
165         /**
166          * Getter for item discount (fixed)
167          * <p>
168          * @return Item discount (fixed)
169          */
170         public Float getItemDiscountFixed () {
171                 return this.itemDiscountFixed;
172         }
173
174         /**
175          * Setter for item discount (fixed)
176          * <p>
177          * @param itemDiscountFixed Item discount (fixed)
178          */
179         public void setItemDiscountFixed (final Float itemDiscountFixed) {
180                 this.itemDiscountFixed = itemDiscountFixed;
181         }
182
183         /**
184          * Getter for item discount (percent)
185          * <p>
186          * @return Item discount (percent)
187          */
188         public Float getItemDiscountPercent () {
189                 return this.itemDiscountPercent;
190         }
191
192         /**
193          * Setter for item discount (percent)
194          * <p>
195          * @param itemDiscountPercent Item discount (percent)
196          */
197         public void setItemDiscountPercent (final Float itemDiscountPercent) {
198                 this.itemDiscountPercent = itemDiscountPercent;
199         }
200
201         /**
202          * Getter for assigned product
203          * <p>
204          * @return Assigned product
205          */
206         public Product getItemProduct () {
207                 return this.itemProduct;
208         }
209
210         /**
211          * Setter for assigned product
212          * <p>
213          * @param itemProduct Assigned product
214          */
215         public void setItemProduct (final Product itemProduct) {
216                 this.itemProduct = itemProduct;
217         }
218
219         /**
220          * Getter for item quantity
221          * <p>
222          * @return Item quantity
223          */
224         public Short getItemQuantity () {
225                 return this.itemQuantity;
226         }
227
228         /**
229          * Setter for item quantity
230          * <p>
231          * @param itemQuantity Item quantity
232          */
233         public void setItemQuantity (final Short itemQuantity) {
234                 this.itemQuantity = itemQuantity;
235         }
236
237         /**
238          * Getter for assigned receipt
239          * <p>
240          * @return Assigned receipt
241          */
242         public BillableReceipt getItemReceipt () {
243                 return this.itemReceipt;
244         }
245
246         /**
247          * Setter for assigned receipt
248          * <p>
249          * @param itemReceipt Assigned receipt
250          */
251         public void setItemReceipt (final BillableReceipt itemReceipt) {
252                 this.itemReceipt = itemReceipt;
253         }
254
255         /**
256          * Getter for product's currency symbol
257          * <p>
258          * @return Product's currency symbol
259          */
260         public String getProductCurrencyCode () {
261                 return this.productCurrencyCode;
262         }
263
264         /**
265          * Setter for product's currency symbol
266          * <p>
267          * @param productCurrencyCode Product's currency symbol
268          */
269         public void setProductCurrencyCode (final String productCurrencyCode) {
270                 this.productCurrencyCode = productCurrencyCode;
271         }
272
273         /**
274          * Getter for product's gross price
275          * <p>
276          * @return Product's gross price
277          */
278         public Float getProductGrossPrice () {
279                 return this.productGrossPrice;
280         }
281
282         /**
283          * Setter for product's gross price
284          * <p>
285          * @param productGrossPrice Product's gross price
286          */
287         public void setProductGrossPrice (final Float productGrossPrice) {
288                 this.productGrossPrice = productGrossPrice;
289         }
290
291         /**
292          * Getter for product's net price
293          * <p>
294          * @return Product's net price
295          */
296         public Float getProductNetPrice () {
297                 return this.productNetPrice;
298         }
299
300         /**
301          * Setter for product's net price
302          * <p>
303          * @param productNetPrice Product's net price
304          */
305         public void setProductNetPrice (final Float productNetPrice) {
306                 this.productNetPrice = productNetPrice;
307         }
308
309         /**
310          * Getter for product's tax rate
311          * <p>
312          * @return Product's tax rate
313          */
314         public Float getProductTaxRate () {
315                 return this.productTaxRate;
316         }
317
318         /**
319          * Setter for product's tax rate
320          * <p>
321          * @param productTaxRate Product's tax rate
322          */
323         public void setProductTaxRate (final Float productTaxRate) {
324                 this.productTaxRate = productTaxRate;
325         }
326
327         /**
328          * Clears this bean
329          */
330         private void clear () {
331                 // Clear all fields
332                 this.setItemDiscountFixed(null);
333                 this.setItemDiscountPercent(null);
334                 this.setItemProduct(null);
335                 this.setItemQuantity(null);
336                 this.setItemReceipt(null);
337         }
338
339         /**
340          * Creates a new instance from all available data of this bean.
341          * <p>
342          * @return Receipt item instance
343          */
344         private BillableReceiptItem createReceiptItemInstance () {
345                 // Create new instance with minimum required data
346                 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
347
348                 // Set optional data
349                 receiptItem.setItemDiscountFixed(this.getItemDiscountFixed());
350                 receiptItem.setItemDiscountPercent(this.getItemDiscountPercent());
351
352                 // Return prepared instance
353                 return receiptItem;
354         }
355
356 }