]> git.mxchange.org Git - jfinancials-war.git/blob
7b5505455cd0101a56c3d36f202c66e2423981b1
[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.Employable;
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 Employable receiptSellerEmployee;
107
108         /**
109          * Sequence number
110          */
111         private Long receiptSequenceNumber;
112
113         /**
114          * User who "owns" this receipt
115          */
116         private User receiptUser;
117
118         /**
119          * Default constructor
120          */
121         public FinancialAdminReceiptWebRequestBean () {
122                 // Call super constructor
123                 super();
124         }
125
126         /**
127          * Adds the completed receipt to database by calling an EJB business method.
128          * If not all required fields are set, a proper exception is being thrown.
129          * <p>
130          * @return Link outcome
131          */
132         public String addReceipt () {
133                 // Are all required fields set?
134                 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.receiptController.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.adminReceiptBean.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.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
174
175                 // Clear bean
176                 this.clear();
177
178                 // Return redirect outcome
179                 return "add_receipt_item?faces-redirect=true"; //NOI18N
180         }
181
182         /**
183          * Getter for receipt's bar-code number
184          * <p>
185          * @return Receipt's bar-code number
186          */
187         public Long getReceiptBarCodeNumber () {
188                 return this.receiptBarCodeNumber;
189         }
190
191         /**
192          * Setter for receipt's bar-code number
193          * <p>
194          * @param receiptBarCodeNumber Receipt's bar-code number
195          */
196         public void setReceiptBarCodeNumber (final Long receiptBarCodeNumber) {
197                 this.receiptBarCodeNumber = receiptBarCodeNumber;
198         }
199
200         /**
201          * Getter for receipt issuing branch office
202          * <p>
203          * @return Receipt issuing branch office
204          */
205         public BranchOffice getReceiptBranchOffice () {
206                 return this.receiptBranchOffice;
207         }
208
209         /**
210          * Setter for receipt issuing branch office
211          * <p>
212          * @param receiptBranchOffice Receipt issuing branch office
213          */
214         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
215                 this.receiptBranchOffice = receiptBranchOffice;
216         }
217
218         /**
219          * Getter for receipt issue date and time
220          * <p>
221          * @return Receipt issue date and time
222          */
223         @SuppressWarnings ("ReturnOfDateField")
224         public Date getReceiptIssued () {
225                 return this.receiptIssued;
226         }
227
228         /**
229          * Setter for receipt issue date and time
230          * <p>
231          * @param receiptIssued Receipt issue date and time
232          */
233         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
234         public void setReceiptIssued (final Date receiptIssued) {
235                 this.receiptIssued = receiptIssued;
236         }
237
238         /**
239          * Getter for receipt number
240          * <p>
241          * @return Receipt number
242          */
243         public Long getReceiptNumber () {
244                 return this.receiptNumber;
245         }
246
247         /**
248          * Setter for receipt number
249          * <p>
250          * @param receiptNumber Receipt number
251          */
252         public void setReceiptNumber (final Long receiptNumber) {
253                 this.receiptNumber = receiptNumber;
254         }
255
256         /**
257          * Getter for payment type
258          * <p>
259          * @return Payment type
260          */
261         public PaymentType getReceiptPaymentType () {
262                 return this.receiptPaymentType;
263         }
264
265         /**
266          * Setter for payment type
267          * <p>
268          * @param receiptPaymentType Payment type
269          */
270         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
271                 this.receiptPaymentType = receiptPaymentType;
272         }
273
274         /**
275          * Getter for receipt register's number
276          * <p>
277          * @return Receipt register's number
278          */
279         public Long getReceiptRegisterNumber () {
280                 return this.receiptRegisterNumber;
281         }
282
283         /**
284          * Setter for receipt register's number
285          * <p>
286          * @param receiptRegisterNumber Receipt register's number
287          */
288         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
289                 this.receiptRegisterNumber = receiptRegisterNumber;
290         }
291
292         /**
293          * Getter for receipt seller employee
294          * <p>
295          * @return Receipt seller employee
296          */
297         public Employable getReceiptSellerEmployee () {
298                 return this.receiptSellerEmployee;
299         }
300
301         /**
302          * Setter for receipt seller employee
303          * <p>
304          * @param receiptSellerEmployee Receipt seller employee
305          */
306         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
307                 this.receiptSellerEmployee = receiptSellerEmployee;
308         }
309
310         /**
311          * Getter for receipt sequence number
312          * <p>
313          * @return Receipt sequence number
314          */
315         public Long getReceiptSequenceNumber () {
316                 return this.receiptSequenceNumber;
317         }
318
319         /**
320          * Setter for receipt sequence number
321          * <p>
322          * @param receiptSequenceNumber Receipt sequence number
323          */
324         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
325                 this.receiptSequenceNumber = receiptSequenceNumber;
326         }
327
328         /**
329          * Getter for user instance
330          * <p>
331          * @return User instance
332          */
333         public User getReceiptUser () {
334                 return this.receiptUser;
335         }
336
337         /**
338          * Setter for user instance
339          * <p>
340          * @param receiptUser User instance
341          */
342         public void setReceiptUser (final User receiptUser) {
343                 this.receiptUser = receiptUser;
344         }
345
346         /**
347          * Clears this bean
348          */
349         private void clear () {
350                 // Clear all fields
351                 this.setReceiptBarCodeNumber(null);
352                 this.setReceiptBranchOffice(null);
353                 this.setReceiptIssued(null);
354                 this.setReceiptNumber(null);
355                 this.setReceiptPaymentType(null);
356                 this.setReceiptRegisterNumber(null);
357                 this.setReceiptSellerEmployee(null);
358                 this.setReceiptSequenceNumber(null);
359                 this.setReceiptUser(null);
360         }
361
362         /**
363          * Creates a new instance from all available data of this bean.
364          * <p>
365          * @return Receipt instance
366          */
367         private BillableReceipt createReceiptInstance () {
368                 // Create new instance with minimum required data
369                 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
370
371                 // Set optional data
372                 receipt.setReceiptUser(this.getReceiptUser());
373                 receipt.setReceiptNumber(this.getReceiptNumber());
374                 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
375                 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
376                 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
377                 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
378
379                 // Return prepared instance
380                 return receipt;
381         }
382
383 }