]> git.mxchange.org Git - jfinancials-war.git/blob
479029aaeece4c45635f7cccce032cfbeb3bd533
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017, 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.events.receipt.added.ObservableReceiptAddedEvent;
33 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
34 import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException;
35 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
36 import org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote;
37 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
38 import org.mxchange.jproduct.model.payment.PaymentType;
39 import org.mxchange.jusercore.model.user.User;
40
41 /**
42  * An administrative backing bean for receipts
43  * <p>
44  * @author Roland Häder<roland@mxchange.org>
45  */
46 @Named ("adminReceiptController")
47 @RequestScoped
48 public class FinancialAdminReceiptWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptWebRequestController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 595_754_280_374_171L;
54
55         /**
56          * Event being fired when administrator has added a new receipt
57          */
58         @Inject
59         @Any
60         private Event<ObservableReceiptAddedEvent> adminAddedReceiptEvent;
61
62         /**
63          * EJB for general financial receipt purposes
64          */
65         @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceipt!org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote")
66         private FinancialAdminReceiptSessionBeanRemote adminReceiptBean;
67
68         /**
69          * Bar-code number
70          */
71         private String receiptBarCodeNumber;
72
73         /**
74          * Recipient issuing company (for example where the user went shopping to)
75          */
76         private BranchOffice receiptBranchOffice;
77
78         /**
79          * General receipt controller
80          */
81         @Inject
82         private FinancialsReceiptWebRequestController receiptController;
83
84         /**
85          * Date/time the receipt has been issued
86          */
87         private Date receiptIssued;
88
89         /**
90          * Receipt list controller
91          */
92         @Inject
93         private FinancialsReceiptListWebViewController receiptListController;
94
95         /**
96          * Receipt number (only numbers)
97          */
98         private String receiptNumber;
99
100         /**
101          * Payment type being used for this receipt
102          */
103         private PaymentType receiptPaymentType;
104
105         /**
106          * Register number
107          */
108         private Long receiptRegisterNumber;
109
110         /**
111          * Selling employee
112          */
113         private Employable receiptSellerEmployee;
114
115         /**
116          * Sequence number
117          */
118         private Long receiptSequenceNumber;
119
120         /**
121          * Transaction number
122          */
123         private Long receiptTransactionNumber;
124
125         /**
126          * User who "owns" this receipt
127          */
128         private User receiptUser;
129
130         /**
131          * Default constructor
132          */
133         public FinancialAdminReceiptWebRequestBean () {
134                 // Call super constructor
135                 super();
136         }
137
138         /**
139          * Adds the completed receipt to database by calling an EJB business method.
140          * If not all required fields are set, a proper exception is being thrown.
141          * <p>
142          */
143         public void addReceipt () {
144                 // Are all required fields set?
145                 if (this.getReceiptBranchOffice() == null) {
146                         // Is not set
147                         throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
148                 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
149                         // It must be an already peristed instance
150                         throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
151                 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
152                         // It must be an already peristed instance
153                         throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
154                 } else if (this.getReceiptPaymentType() == null) {
155                         // Is not set
156                         throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
157                 } else if (this.getReceiptIssued() == null) {
158                         // Is not set
159                         throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
160                 }
161
162                 // Prepare receipt instance
163                 final BillableReceipt receipt = this.createReceiptInstance();
164
165                 // Is the receipt already there?
166                 if (this.receiptListController.isReceiptAdded(receipt)) {
167                         // Receipt has already been added
168                         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
169                 }
170
171                 // Init variable
172                 final BillableReceipt updatedReceipt;
173
174                 // All is set, then try to call EJB
175                 try {
176                         // Add it
177                         updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
178                 } catch (final ReceiptAlreadyAddedException ex) {
179                         // Throw it again
180                         throw new FaceletException(ex);
181                 }
182
183                 // Fire event with updated instance
184                 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
185
186                 // Clear bean
187                 this.clear();
188         }
189
190         /**
191          * Getter for receipt's bar-code number
192          * <p>
193          * @return Receipt's bar-code number
194          */
195         public String getReceiptBarCodeNumber () {
196                 return this.receiptBarCodeNumber;
197         }
198
199         /**
200          * Setter for receipt's bar-code number
201          * <p>
202          * @param receiptBarCodeNumber Receipt's bar-code number
203          */
204         public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
205                 this.receiptBarCodeNumber = receiptBarCodeNumber;
206         }
207
208         /**
209          * Getter for receipt issuing branch office
210          * <p>
211          * @return Receipt issuing branch office
212          */
213         public BranchOffice getReceiptBranchOffice () {
214                 return this.receiptBranchOffice;
215         }
216
217         /**
218          * Setter for receipt issuing branch office
219          * <p>
220          * @param receiptBranchOffice Receipt issuing branch office
221          */
222         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
223                 this.receiptBranchOffice = receiptBranchOffice;
224         }
225
226         /**
227          * Getter for receipt issue date and time
228          * <p>
229          * @return Receipt issue date and time
230          */
231         @SuppressWarnings ("ReturnOfDateField")
232         public Date getReceiptIssued () {
233                 return this.receiptIssued;
234         }
235
236         /**
237          * Setter for receipt issue date and time
238          * <p>
239          * @param receiptIssued Receipt issue date and time
240          */
241         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
242         public void setReceiptIssued (final Date receiptIssued) {
243                 this.receiptIssued = receiptIssued;
244         }
245
246         /**
247          * Getter for receipt number
248          * <p>
249          * @return Receipt number
250          */
251         public String getReceiptNumber () {
252                 return this.receiptNumber;
253         }
254
255         /**
256          * Setter for receipt number
257          * <p>
258          * @param receiptNumber Receipt number
259          */
260         public void setReceiptNumber (final String receiptNumber) {
261                 this.receiptNumber = receiptNumber;
262         }
263
264         /**
265          * Getter for payment type
266          * <p>
267          * @return Payment type
268          */
269         public PaymentType getReceiptPaymentType () {
270                 return this.receiptPaymentType;
271         }
272
273         /**
274          * Setter for payment type
275          * <p>
276          * @param receiptPaymentType Payment type
277          */
278         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
279                 this.receiptPaymentType = receiptPaymentType;
280         }
281
282         /**
283          * Getter for receipt register's number
284          * <p>
285          * @return Receipt register's number
286          */
287         public Long getReceiptRegisterNumber () {
288                 return this.receiptRegisterNumber;
289         }
290
291         /**
292          * Setter for receipt register's number
293          * <p>
294          * @param receiptRegisterNumber Receipt register's number
295          */
296         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
297                 this.receiptRegisterNumber = receiptRegisterNumber;
298         }
299
300         /**
301          * Getter for receipt seller employee
302          * <p>
303          * @return Receipt seller employee
304          */
305         public Employable getReceiptSellerEmployee () {
306                 return this.receiptSellerEmployee;
307         }
308
309         /**
310          * Setter for receipt seller employee
311          * <p>
312          * @param receiptSellerEmployee Receipt seller employee
313          */
314         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
315                 this.receiptSellerEmployee = receiptSellerEmployee;
316         }
317
318         /**
319          * Getter for receipt sequence number
320          * <p>
321          * @return Receipt sequence number
322          */
323         public Long getReceiptSequenceNumber () {
324                 return this.receiptSequenceNumber;
325         }
326
327         /**
328          * Setter for receipt sequence number
329          * <p>
330          * @param receiptSequenceNumber Receipt sequence number
331          */
332         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
333                 this.receiptSequenceNumber = receiptSequenceNumber;
334         }
335
336         /**
337          * Getter for receipt transaction number
338          * <p>
339          * @return Receipt transaction number
340          */
341         public Long getReceiptTransactionNumber () {
342                 return this.receiptTransactionNumber;
343         }
344
345         /**
346          * Setter for receipt transaction number
347          * <p>
348          * @param receiptTransactionNumber Receipt transaction number
349          */
350         public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
351                 this.receiptTransactionNumber = receiptTransactionNumber;
352         }
353
354         /**
355          * Getter for user instance
356          * <p>
357          * @return User instance
358          */
359         public User getReceiptUser () {
360                 return this.receiptUser;
361         }
362
363         /**
364          * Setter for user instance
365          * <p>
366          * @param receiptUser User instance
367          */
368         public void setReceiptUser (final User receiptUser) {
369                 this.receiptUser = receiptUser;
370         }
371
372         /**
373          * Clears this bean
374          */
375         private void clear () {
376                 // Clear all fields
377                 this.setReceiptBarCodeNumber(null);
378                 this.setReceiptBranchOffice(null);
379                 this.setReceiptIssued(null);
380                 this.setReceiptNumber(null);
381                 this.setReceiptPaymentType(null);
382                 this.setReceiptRegisterNumber(null);
383                 this.setReceiptSellerEmployee(null);
384                 this.setReceiptSequenceNumber(null);
385                 this.setReceiptTransactionNumber(null);
386                 this.setReceiptUser(null);
387         }
388
389         /**
390          * Creates a new instance from all available data of this bean.
391          * <p>
392          * @return Receipt instance
393          */
394         private BillableReceipt createReceiptInstance () {
395                 // Create new instance with minimum required data
396                 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
397
398                 // Set optional data
399                 receipt.setReceiptUser(this.getReceiptUser());
400                 receipt.setReceiptNumber(this.getReceiptNumber());
401                 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
402                 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
403                 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
404                 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
405                 receipt.setReceiptTransactionNumber(this.getReceiptTransactionNumber());
406
407                 // Return prepared instance
408                 return receipt;
409         }
410
411 }