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