]> git.mxchange.org Git - jfinancials-war.git/blob
00af46b6f2c3ec4a74ecaf20d6f8e2c732d90765
[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.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 adminFinancialBean;
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.adminFinancialBean.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                 // Return redirect outcome
171                 return "add_receipt_item?faces-redirect=true"; //NOI18N
172         }
173
174         /**
175          * Getter for receipt's bar-code number
176          * <p>
177          * @return Receipt's bar-code number
178          */
179         public Long getReceiptBarCodeNumber () {
180                 return this.receiptBarCodeNumber;
181         }
182
183         /**
184          * Setter for receipt's bar-code number
185          * <p>
186          * @param receiptBarCodeNumber Receipt's bar-code number
187          */
188         public void setReceiptBarCodeNumber (final Long receiptBarCodeNumber) {
189                 this.receiptBarCodeNumber = receiptBarCodeNumber;
190         }
191
192         /**
193          * Getter for receipt issuing branch office
194          * <p>
195          * @return Receipt issuing branch office
196          */
197         public BranchOffice getReceiptBranchOffice () {
198                 return this.receiptBranchOffice;
199         }
200
201         /**
202          * Setter for receipt issuing branch office
203          * <p>
204          * @param receiptBranchOffice Receipt issuing branch office
205          */
206         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
207                 this.receiptBranchOffice = receiptBranchOffice;
208         }
209
210         /**
211          * Getter for receipt issue date and time
212          * <p>
213          * @return Receipt issue date and time
214          */
215         @SuppressWarnings ("ReturnOfDateField")
216         public Date getReceiptIssued () {
217                 return this.receiptIssued;
218         }
219
220         /**
221          * Setter for receipt issue date and time
222          * <p>
223          * @param receiptIssued Receipt issue date and time
224          */
225         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
226         public void setReceiptIssued (final Date receiptIssued) {
227                 this.receiptIssued = receiptIssued;
228         }
229
230         /**
231          * Getter for receipt number
232          * <p>
233          * @return Receipt number
234          */
235         public Long getReceiptNumber () {
236                 return this.receiptNumber;
237         }
238
239         /**
240          * Setter for receipt number
241          * <p>
242          * @param receiptNumber Receipt number
243          */
244         public void setReceiptNumber (final Long receiptNumber) {
245                 this.receiptNumber = receiptNumber;
246         }
247
248         /**
249          * Getter for payment type
250          * <p>
251          * @return Payment type
252          */
253         public PaymentType getReceiptPaymentType () {
254                 return this.receiptPaymentType;
255         }
256
257         /**
258          * Setter for payment type
259          * <p>
260          * @param receiptPaymentType Payment type
261          */
262         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
263                 this.receiptPaymentType = receiptPaymentType;
264         }
265
266         /**
267          * Getter for receipt register's number
268          * <p>
269          * @return Receipt register's number
270          */
271         public Long getReceiptRegisterNumber () {
272                 return this.receiptRegisterNumber;
273         }
274
275         /**
276          * Setter for receipt register's number
277          * <p>
278          * @param receiptRegisterNumber Receipt register's number
279          */
280         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
281                 this.receiptRegisterNumber = receiptRegisterNumber;
282         }
283
284         /**
285          * Getter for receipt seller employee
286          * <p>
287          * @return Receipt seller employee
288          */
289         public Employee getReceiptSellerEmployee () {
290                 return this.receiptSellerEmployee;
291         }
292
293         /**
294          * Setter for receipt seller employee
295          * <p>
296          * @param receiptSellerEmployee Receipt seller employee
297          */
298         public void setReceiptSellerEmployee (final Employee receiptSellerEmployee) {
299                 this.receiptSellerEmployee = receiptSellerEmployee;
300         }
301
302         /**
303          * Getter for user instance
304          * <p>
305          * @return User instance
306          */
307         public User getReceiptUser () {
308                 return this.receiptUser;
309         }
310
311         /**
312          * Setter for user instance
313          * <p>
314          * @param receiptUser User instance
315          */
316         public void setReceiptUser (final User receiptUser) {
317                 this.receiptUser = receiptUser;
318         }
319
320         /**
321          * Creates a new instance from all available data of this bean.
322          * <p>
323          * @return Receipt instance
324          */
325         private BillableReceipt createReceiptInstance () {
326                 // Create new instance with minimum required data
327                 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.getReceiptIssued());
328
329                 // Set optional data
330                 receipt.setReceiptUser(this.getReceiptUser());
331                 receipt.setReceiptNumber(this.getReceiptNumber());
332                 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
333                 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
334                 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
335
336                 // Return prepared instance
337                 return receipt;
338         }
339
340 }