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