]> git.mxchange.org Git - jfinancials-war.git/blob
15b4ad5448aeb9c70fa9c3d0bf5cdf3899cf01d4
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2016 - 2018 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;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
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.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
29 import org.mxchange.jcontactsbusiness.model.employee.Employable;
30 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
31 import org.mxchange.jfinancials.beans.financial.model.receipt.list.FinancialsReceiptListWebViewController;
32 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
33 import org.mxchange.jfinancials.events.receipt.added.ObservableReceiptAddedEvent;
34 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
35 import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException;
36 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
37 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
38 import org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote;
39 import org.mxchange.jproduct.model.payment.PaymentType;
40
41 /**
42  * A general financial receipt bean (controller)
43  * <p>
44  * @author Roland Häder<roland@mxchange.org>
45  */
46 @Named ("receiptController")
47 @RequestScoped
48 public class FinancialsReceiptWebRequestBean extends BaseFinancialsBean implements FinancialsReceiptWebRequestController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 56_189_028_928_371L;
54
55         /**
56          * Event being fired when user has added a new receipt
57          */
58         @Inject
59         @Any
60         private Event<ObservableReceiptAddedEvent> addedReceiptEvent;
61
62         /**
63          * Bar-code number
64          */
65         private String receiptBarCodeNumber;
66
67         /**
68          * EJB for general financial receipt purposes
69          */
70         @EJB (lookup = "java:global/jfinancials-ejb/financialReceipt!org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote")
71         private FinancialReceiptSessionBeanRemote receiptBean;
72
73         /**
74          * Recipient issuing company (for example where the user went shopping to)
75          */
76         private BranchOffice receiptBranchOffice;
77
78         /**
79          * Date/time the receipt has been issued
80          */
81         private Date receiptIssued;
82
83         /**
84          * Receipt list controller
85          */
86         @Inject
87         private FinancialsReceiptListWebViewController receiptListController;
88
89         /**
90          * Receipt number (only numbers)
91          */
92         private String receiptNumber;
93
94         /**
95          * Payment type being used for this receipt
96          */
97         private PaymentType receiptPaymentType;
98
99         /**
100          * Register number
101          */
102         private Long receiptRegisterNumber;
103
104         /**
105          * Selling employee
106          */
107         private Employable receiptSellerEmployee;
108
109         /**
110          * User instance
111          */
112         @Inject
113         private FinancialsUserLoginWebSessionController userLoginController;
114
115         /**
116          * Constructor
117          */
118         @SuppressWarnings ("CollectionWithoutInitialCapacity")
119         public FinancialsReceiptWebRequestBean () {
120                 // Call super constructor
121                 super();
122         }
123
124         /**
125          * Adds the completed receipt to database by calling an EJB business method.
126          * If not all required fields are set, a proper exception is being thrown.
127          * <p>
128          */
129         public void addReceipt () {
130                 // Are all required fields set?
131                 if (!this.userLoginController.isUserLoggedIn()) {
132                         // Not logged-in
133                         throw new IllegalStateException("User is not logged-in"); //NOI18N
134                 } else if (this.getReceiptBranchOffice() == null) {
135                         // Is not set
136                         throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
137                 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
138                         // It must be an already peristed instance
139                         throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
140                 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
141                         // It must be an already peristed instance
142                         throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
143                 } else if (this.getReceiptPaymentType() == null) {
144                         // Is not set
145                         throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
146                 } else if (this.getReceiptIssued() == null) {
147                         // Is not set
148                         throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
149                 }
150
151                 // Prepare receipt instance
152                 final BillableReceipt receipt = this.createReceiptInstance();
153
154                 // Is the receipt already there?
155                 if (this.receiptListController.isReceiptAdded(receipt)) {
156                         // Receipt has already been added
157                         throw new FaceletException(MessageFormat.format("Receipt for receiptBranchOffice={0},receiptIssued={1},receiptNumber={2} has already been added.", this.getReceiptBranchOffice().getBranchId(), this.getReceiptIssued().toString(), this.getReceiptNumber())); //NOI18N
158                 }
159
160                 // Init variable
161                 final BillableReceipt updatedReceipt;
162
163                 // All is set, then try to call EJB
164                 try {
165                         // Add it
166                         updatedReceipt = this.receiptBean.addReceipt(receipt);
167                 } catch (final ReceiptAlreadyAddedException ex) {
168                         // Throw it again
169                         throw new FaceletException(ex);
170                 }
171
172                 // Fire event with updated instance
173                 this.addedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
174
175                 // Clear bean
176                 this.clear();
177         }
178
179         /**
180          * Getter for receipt's bar-code number
181          * <p>
182          * @return Receipt's bar-code number
183          */
184         public String getReceiptBarCodeNumber () {
185                 return this.receiptBarCodeNumber;
186         }
187
188         /**
189          * Setter for receipt's bar-code number
190          * <p>
191          * @param receiptBarCodeNumber Receipt's bar-code number
192          */
193         public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
194                 this.receiptBarCodeNumber = receiptBarCodeNumber;
195         }
196
197         /**
198          * Getter for receipt issuing branch office
199          * <p>
200          * @return Receipt issuing branch office
201          */
202         public BranchOffice getReceiptBranchOffice () {
203                 return this.receiptBranchOffice;
204         }
205
206         /**
207          * Setter for receipt issuing branch office
208          * <p>
209          * @param receiptBranchOffice Receipt issuing branch office
210          */
211         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
212                 this.receiptBranchOffice = receiptBranchOffice;
213         }
214
215         /**
216          * Getter for receipt issue date and time
217          * <p>
218          * @return Receipt issue date and time
219          */
220         @SuppressWarnings ("ReturnOfDateField")
221         public Date getReceiptIssued () {
222                 return this.receiptIssued;
223         }
224
225         /**
226          * Setter for receipt issue date and time
227          * <p>
228          * @param receiptIssued Receipt issue date and time
229          */
230         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
231         public void setReceiptIssued (final Date receiptIssued) {
232                 this.receiptIssued = receiptIssued;
233         }
234
235         /**
236          * Getter for receipt number
237          * <p>
238          * @return Receipt number
239          */
240         public String getReceiptNumber () {
241                 return this.receiptNumber;
242         }
243
244         /**
245          * Setter for receipt number
246          * <p>
247          * @param receiptNumber Receipt number
248          */
249         public void setReceiptNumber (final String receiptNumber) {
250                 this.receiptNumber = receiptNumber;
251         }
252
253         /**
254          * Getter for payment type
255          * <p>
256          * @return Payment type
257          */
258         public PaymentType getReceiptPaymentType () {
259                 return this.receiptPaymentType;
260         }
261
262         /**
263          * Setter for payment type
264          * <p>
265          * @param receiptPaymentType Payment type
266          */
267         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
268                 this.receiptPaymentType = receiptPaymentType;
269         }
270
271         /**
272          * Getter for receipt register's number
273          * <p>
274          * @return Receipt register's number
275          */
276         public Long getReceiptRegisterNumber () {
277                 return this.receiptRegisterNumber;
278         }
279
280         /**
281          * Setter for receipt register's number
282          * <p>
283          * @param receiptRegisterNumber Receipt register's number
284          */
285         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
286                 this.receiptRegisterNumber = receiptRegisterNumber;
287         }
288
289         /**
290          * Getter for receipt seller employee
291          * <p>
292          * @return Receipt seller employee
293          */
294         public Employable getReceiptSellerEmployee () {
295                 return this.receiptSellerEmployee;
296         }
297
298         /**
299          * Setter for receipt seller employee
300          * <p>
301          * @param receiptSellerEmployee Receipt seller employee
302          */
303         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
304                 this.receiptSellerEmployee = receiptSellerEmployee;
305         }
306
307         /**
308          * Clears this bean
309          */
310         private void clear () {
311                 // Clear all fields
312                 this.setReceiptBarCodeNumber(null);
313                 this.setReceiptBranchOffice(null);
314                 this.setReceiptIssued(null);
315                 this.setReceiptNumber(null);
316                 this.setReceiptPaymentType(null);
317                 this.setReceiptRegisterNumber(null);
318                 this.setReceiptSellerEmployee(null);
319         }
320
321         /**
322          * Returns a fully created receipt instance (except primary key, of course)
323          * <p>
324          * @return Receipt instance
325          */
326         private BillableReceipt createReceiptInstance () {
327                 // Init receipt instance
328                 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.userLoginController.getLoggedInUser(), this.getReceiptIssued());
329
330                 // Set optional fields
331                 receipt.setReceiptNumber(this.getReceiptNumber());
332                 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
333                 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
334                 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
335
336                 // Return it
337                 return receipt;
338         }
339 }