]> git.mxchange.org Git - jfinancials-war.git/blob
30621c301ccad528b49c491ce4d82e0fdaf9a65a
[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          * User who "owns" this receipt
122          */
123         private User receiptUser;
124
125         /**
126          * Default constructor
127          */
128         public FinancialAdminReceiptWebRequestBean () {
129                 // Call super constructor
130                 super();
131         }
132
133         /**
134          * Adds the completed receipt to database by calling an EJB business method.
135          * If not all required fields are set, a proper exception is being thrown.
136          * <p>
137          */
138         public void addReceipt () {
139                 // Are all required fields set?
140                 if (this.getReceiptBranchOffice() == null) {
141                         // Is not set
142                         throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
143                 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
144                         // It must be an already peristed instance
145                         throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
146                 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
147                         // It must be an already peristed instance
148                         throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
149                 } else if (this.getReceiptPaymentType() == null) {
150                         // Is not set
151                         throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
152                 } else if (this.getReceiptIssued() == null) {
153                         // Is not set
154                         throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
155                 }
156
157                 // Prepare receipt instance
158                 final BillableReceipt receipt = this.createReceiptInstance();
159
160                 // Is the receipt already there?
161                 if (this.receiptListController.isReceiptAdded(receipt)) {
162                         // Receipt has already been added
163                         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
164                 }
165
166                 // Init variable
167                 final BillableReceipt updatedReceipt;
168
169                 // All is set, then try to call EJB
170                 try {
171                         // Add it
172                         updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
173                 } catch (final ReceiptAlreadyAddedException ex) {
174                         // Throw it again
175                         throw new FaceletException(ex);
176                 }
177
178                 // Fire event with updated instance
179                 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
180
181                 // Clear bean
182                 this.clear();
183         }
184
185         /**
186          * Getter for receipt's bar-code number
187          * <p>
188          * @return Receipt's bar-code number
189          */
190         public String getReceiptBarCodeNumber () {
191                 return this.receiptBarCodeNumber;
192         }
193
194         /**
195          * Setter for receipt's bar-code number
196          * <p>
197          * @param receiptBarCodeNumber Receipt's bar-code number
198          */
199         public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
200                 this.receiptBarCodeNumber = receiptBarCodeNumber;
201         }
202
203         /**
204          * Getter for receipt issuing branch office
205          * <p>
206          * @return Receipt issuing branch office
207          */
208         public BranchOffice getReceiptBranchOffice () {
209                 return this.receiptBranchOffice;
210         }
211
212         /**
213          * Setter for receipt issuing branch office
214          * <p>
215          * @param receiptBranchOffice Receipt issuing branch office
216          */
217         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
218                 this.receiptBranchOffice = receiptBranchOffice;
219         }
220
221         /**
222          * Getter for receipt issue date and time
223          * <p>
224          * @return Receipt issue date and time
225          */
226         @SuppressWarnings ("ReturnOfDateField")
227         public Date getReceiptIssued () {
228                 return this.receiptIssued;
229         }
230
231         /**
232          * Setter for receipt issue date and time
233          * <p>
234          * @param receiptIssued Receipt issue date and time
235          */
236         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
237         public void setReceiptIssued (final Date receiptIssued) {
238                 this.receiptIssued = receiptIssued;
239         }
240
241         /**
242          * Getter for receipt number
243          * <p>
244          * @return Receipt number
245          */
246         public String getReceiptNumber () {
247                 return this.receiptNumber;
248         }
249
250         /**
251          * Setter for receipt number
252          * <p>
253          * @param receiptNumber Receipt number
254          */
255         public void setReceiptNumber (final String receiptNumber) {
256                 this.receiptNumber = receiptNumber;
257         }
258
259         /**
260          * Getter for payment type
261          * <p>
262          * @return Payment type
263          */
264         public PaymentType getReceiptPaymentType () {
265                 return this.receiptPaymentType;
266         }
267
268         /**
269          * Setter for payment type
270          * <p>
271          * @param receiptPaymentType Payment type
272          */
273         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
274                 this.receiptPaymentType = receiptPaymentType;
275         }
276
277         /**
278          * Getter for receipt register's number
279          * <p>
280          * @return Receipt register's number
281          */
282         public Long getReceiptRegisterNumber () {
283                 return this.receiptRegisterNumber;
284         }
285
286         /**
287          * Setter for receipt register's number
288          * <p>
289          * @param receiptRegisterNumber Receipt register's number
290          */
291         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
292                 this.receiptRegisterNumber = receiptRegisterNumber;
293         }
294
295         /**
296          * Getter for receipt seller employee
297          * <p>
298          * @return Receipt seller employee
299          */
300         public Employable getReceiptSellerEmployee () {
301                 return this.receiptSellerEmployee;
302         }
303
304         /**
305          * Setter for receipt seller employee
306          * <p>
307          * @param receiptSellerEmployee Receipt seller employee
308          */
309         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
310                 this.receiptSellerEmployee = receiptSellerEmployee;
311         }
312
313         /**
314          * Getter for receipt sequence number
315          * <p>
316          * @return Receipt sequence number
317          */
318         public Long getReceiptSequenceNumber () {
319                 return this.receiptSequenceNumber;
320         }
321
322         /**
323          * Setter for receipt sequence number
324          * <p>
325          * @param receiptSequenceNumber Receipt sequence number
326          */
327         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
328                 this.receiptSequenceNumber = receiptSequenceNumber;
329         }
330
331         /**
332          * Getter for user instance
333          * <p>
334          * @return User instance
335          */
336         public User getReceiptUser () {
337                 return this.receiptUser;
338         }
339
340         /**
341          * Setter for user instance
342          * <p>
343          * @param receiptUser User instance
344          */
345         public void setReceiptUser (final User receiptUser) {
346                 this.receiptUser = receiptUser;
347         }
348
349         /**
350          * Clears this bean
351          */
352         private void clear () {
353                 // Clear all fields
354                 this.setReceiptBarCodeNumber(null);
355                 this.setReceiptBranchOffice(null);
356                 this.setReceiptIssued(null);
357                 this.setReceiptNumber(null);
358                 this.setReceiptPaymentType(null);
359                 this.setReceiptRegisterNumber(null);
360                 this.setReceiptSellerEmployee(null);
361                 this.setReceiptSequenceNumber(null);
362                 this.setReceiptUser(null);
363         }
364
365         /**
366          * Creates a new instance from all available data of this bean.
367          * <p>
368          * @return Receipt instance
369          */
370         private BillableReceipt createReceiptInstance () {
371                 // Create new instance with minimum required data
372                 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
373
374                 // Set optional data
375                 receipt.setReceiptUser(this.getReceiptUser());
376                 receipt.setReceiptNumber(this.getReceiptNumber());
377                 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
378                 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
379                 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
380                 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
381
382                 // Return prepared instance
383                 return receipt;
384         }
385
386 }