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