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