]> git.mxchange.org Git - jfinancials-war.git/blob
c48b3d1ceb31437cd386038d7b7c1a8f336c5c63
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 - 2020 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.action;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.ejb.EJB;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.FacesException;
26 import javax.faces.application.FacesMessage;
27 import javax.faces.view.ViewScoped;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import org.mxchange.jbonuscard.model.bonus_card.BonusCard;
31 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
32 import org.mxchange.jcontactsbusiness.model.employee.Employable;
33 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
34 import org.mxchange.jfinancials.beans.financial.model.receipt.list.FinancialsReceiptListWebViewController;
35 import org.mxchange.jfinancials.events.receipt.added.ObservableReceiptAddedEvent;
36 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
37 import org.mxchange.jfinancials.events.receipt.updated.ObservableReceiptUpdatedEvent;
38 import org.mxchange.jfinancials.events.receipt.updated.ReceiptUpdatedEvent;
39 import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException;
40 import org.mxchange.jfinancials.exceptions.receipt.ReceiptNotFoundException;
41 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
42 import org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote;
43 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
44 import org.mxchange.jfinancials.model.receipt.Receipts;
45 import org.mxchange.jproduct.model.payment.PaymentType;
46 import org.mxchange.jusercore.model.user.User;
47
48 /**
49  * An administrative backing bean for receipts
50  * <p>
51  * @author Roland Häder<roland@mxchange.org>
52  */
53 @Named ("adminReceiptActionController")
54 @ViewScoped
55 public class FinancialAdminReceiptActionWebViewBean extends BaseFinancialsBean implements FinancialAdminReceiptActionWebViewController {
56
57         /**
58          * Serial number
59          */
60         private static final long serialVersionUID = 595_754_280_374_172L;
61
62         /**
63          * Event being fired when administrator has added a new receipt
64          */
65         @Inject
66         @Any
67         private Event<ObservableReceiptAddedEvent> adminAddedReceiptEvent;
68
69         /**
70          * EJB for general financial receipt purposes
71          */
72         @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceipt!org.mxchange.jfinancials.model.receipt.FinancialAdminReceiptSessionBeanRemote")
73         private FinancialAdminReceiptSessionBeanRemote adminReceiptBean;
74
75         /**
76          * Event being fired when administrator has added a new receipt
77          */
78         @Inject
79         @Any
80         private Event<ObservableReceiptUpdatedEvent> adminUpdatedReceiptEvent;
81
82         /**
83          * Current receipt
84          */
85         private BillableReceipt currentReceipt;
86
87         /**
88          * Bar-code number
89          */
90         private String receiptBarCodeNumber;
91
92         /**
93          * Bonus card assigned with receipt
94          */
95         private BonusCard receiptBonusCard;
96
97         /**
98          * Recipient issuing company (for example where the user went shopping to)
99          */
100         private BranchOffice receiptBranchOffice;
101
102         /**
103          * Receipt id number
104          */
105         private Long receiptId;
106
107         /**
108          * Date/time the receipt has been issued
109          */
110         private Date receiptIssued;
111
112         /**
113          * Receipt list controller
114          */
115         @Inject
116         private FinancialsReceiptListWebViewController receiptListController;
117
118         /**
119          * Receipt number (only numbers)
120          */
121         private String receiptNumber;
122
123         /**
124          * Payment type being used for this receipt
125          */
126         private PaymentType receiptPaymentType;
127
128         /**
129          * Register number
130          */
131         private Long receiptRegisterNumber;
132
133         /**
134          * Current receipt is a resumption of this receipt
135          */
136         private BillableReceipt receiptResumptionOf;
137
138         /**
139          * Selling employee
140          */
141         private Employable receiptSellerEmployee;
142
143         /**
144          * Sequence number
145          */
146         private Long receiptSequenceNumber;
147
148         /**
149          * Transaction number
150          */
151         private Long receiptTransactionNumber;
152
153         /**
154          * User who "owns" this receipt
155          */
156         private User receiptUser;
157
158         /**
159          * Default constructor
160          */
161         public FinancialAdminReceiptActionWebViewBean () {
162                 // Call super constructor
163                 super();
164         }
165
166         /**
167          * Adds the completed receipt to database by calling an EJB business method.
168          * If not all required fields are set, a proper exception is being thrown.
169          * <p>
170          */
171         public void addReceipt () {
172                 // Are all required fields set?
173                 if (this.getReceiptBranchOffice() == null) {
174                         // Is not set
175                         throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
176                 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
177                         // It must be an already peristed instance
178                         throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
179                 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
180                         // It must be an already peristed instance
181                         throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
182                 } else if (this.getReceiptPaymentType() == null) {
183                         // Is not set
184                         throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
185                 } else if (this.getReceiptIssued() == null) {
186                         // Is not set
187                         throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
188                 }
189
190                 // Prepare receipt instance
191                 final BillableReceipt receipt = this.createReceiptInstance();
192
193                 // Is the receipt already there?
194                 if (this.receiptListController.isReceiptAdded(receipt)) {
195                         // Receipt has already been added
196                         throw new FacesException(MessageFormat.format(
197                                         "Receipt for receiptBranchOffice={0},receiptIssued={1},receiptNumber={2} has already been added.", //NOI18N
198                                         this.getReceiptBranchOffice().getBranchId(),
199                                         this.getReceiptIssued().toString(),
200                                         this.getReceiptNumber()
201                         ));
202                 }
203
204                 // Init variable
205                 final BillableReceipt updatedReceipt;
206
207                 // All is set, then try to call EJB
208                 try {
209                         // Add it
210                         updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
211                 } catch (final ReceiptAlreadyAddedException ex) {
212                         // Throw it again
213                         throw new FacesException(ex);
214                 }
215
216                 // Fire event with updated instance
217                 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
218
219                 // Clear bean
220                 this.clear();
221         }
222
223         /**
224          * Copies all fields from current receipt instance to local bean properties.
225          */
226         public void copyAllReceiptProperties () {
227                 // Is current receipt instance valid?
228                 if (this.getCurrentReceipt() == null) {
229                         // Not set
230                         throw new NullPointerException("this.currentReceipt is null"); //NOI18N
231                 }
232
233                 // Copy all fields
234                 this.setReceiptBarCodeNumber(this.getCurrentReceipt().getReceiptBarCodeNumber());
235                 this.setReceiptBonusCard(this.getCurrentReceipt().getReceiptBonusCard());
236                 this.setReceiptBranchOffice(this.getCurrentReceipt().getReceiptBranchOffice());
237                 this.setReceiptId(this.getCurrentReceipt().getReceiptId());
238                 this.setReceiptIssued(this.getCurrentReceipt().getReceiptIssued());
239                 this.setReceiptNumber(this.getCurrentReceipt().getReceiptNumber());
240                 this.setReceiptPaymentType(this.getCurrentReceipt().getReceiptPaymentType());
241                 this.setReceiptRegisterNumber(this.getCurrentReceipt().getReceiptRegisterNumber());
242                 this.setReceiptSellerEmployee(this.getCurrentReceipt().getReceiptSellerEmployee());
243                 this.setReceiptSequenceNumber(this.getCurrentReceipt().getReceiptSequenceNumber());
244                 this.setReceiptTransactionNumber(this.getCurrentReceipt().getReceiptTransactionNumber());
245                 this.setReceiptUser(this.getCurrentReceipt().getReceiptUser());
246         }
247
248         /**
249          * Getter for current receipt
250          * <p>
251          * @return Current receipt
252          */
253         public BillableReceipt getCurrentReceipt () {
254                 return this.currentReceipt;
255         }
256
257         /**
258          * Setter for current receipt
259          * <p>
260          * @param currentReceipt Current receipt
261          */
262         public void setCurrentReceipt (final BillableReceipt currentReceipt) {
263                 this.currentReceipt = currentReceipt;
264         }
265
266         /**
267          * Getter for receipt's bar-code number
268          * <p>
269          * @return Receipt's bar-code number
270          */
271         public String getReceiptBarCodeNumber () {
272                 return this.receiptBarCodeNumber;
273         }
274
275         /**
276          * Setter for receipt's bar-code number
277          * <p>
278          * @param receiptBarCodeNumber Receipt's bar-code number
279          */
280         public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
281                 this.receiptBarCodeNumber = receiptBarCodeNumber;
282         }
283
284         /**
285          * Getter for bonus card
286          * <p>
287          * @return Bonus card
288          */
289         public BonusCard getReceiptBonusCard () {
290                 return this.receiptBonusCard;
291         }
292
293         /**
294          * Setter for bonus card
295          * <p>
296          * @param receiptBonusCard Bonus card
297          */
298         public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
299                 this.receiptBonusCard = receiptBonusCard;
300         }
301
302         /**
303          * Getter for receipt issuing branch office
304          * <p>
305          * @return Receipt issuing branch office
306          */
307         public BranchOffice getReceiptBranchOffice () {
308                 return this.receiptBranchOffice;
309         }
310
311         /**
312          * Setter for receipt issuing branch office
313          * <p>
314          * @param receiptBranchOffice Receipt issuing branch office
315          */
316         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
317                 this.receiptBranchOffice = receiptBranchOffice;
318         }
319
320         /**
321          * Getter for receipt id
322          * <p>
323          * @return Receipt it
324          */
325         public Long getReceiptId () {
326                 return this.receiptId;
327         }
328
329         /**
330          * Setter for receipt id
331          * <p>
332          * @param receiptId Receipt id
333          */
334         public void setReceiptId (final Long receiptId) {
335                 this.receiptId = receiptId;
336         }
337
338         /**
339          * Getter for receipt issue date and time
340          * <p>
341          * @return Receipt issue date and time
342          */
343         @SuppressWarnings ("ReturnOfDateField")
344         public Date getReceiptIssued () {
345                 return this.receiptIssued;
346         }
347
348         /**
349          * Setter for receipt issue date and time
350          * <p>
351          * @param receiptIssued Receipt issue date and time
352          */
353         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
354         public void setReceiptIssued (final Date receiptIssued) {
355                 this.receiptIssued = receiptIssued;
356         }
357
358         /**
359          * Getter for receipt number
360          * <p>
361          * @return Receipt number
362          */
363         public String getReceiptNumber () {
364                 return this.receiptNumber;
365         }
366
367         /**
368          * Setter for receipt number
369          * <p>
370          * @param receiptNumber Receipt number
371          */
372         public void setReceiptNumber (final String receiptNumber) {
373                 this.receiptNumber = receiptNumber;
374         }
375
376         /**
377          * Getter for payment type
378          * <p>
379          * @return Payment type
380          */
381         public PaymentType getReceiptPaymentType () {
382                 return this.receiptPaymentType;
383         }
384
385         /**
386          * Setter for payment type
387          * <p>
388          * @param receiptPaymentType Payment type
389          */
390         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
391                 this.receiptPaymentType = receiptPaymentType;
392         }
393
394         /**
395          * Getter for receipt register's number
396          * <p>
397          * @return Receipt register's number
398          */
399         public Long getReceiptRegisterNumber () {
400                 return this.receiptRegisterNumber;
401         }
402
403         /**
404          * Setter for receipt register's number
405          * <p>
406          * @param receiptRegisterNumber Receipt register's number
407          */
408         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
409                 this.receiptRegisterNumber = receiptRegisterNumber;
410         }
411
412         /**
413          * Getter for receipt's resumption of an other receipt
414          * <p>
415          * @return Current receipt is based on this receipt
416          */
417         public BillableReceipt getReceiptResumptionOf () {
418                 return this.receiptResumptionOf;
419         }
420
421         /**
422          * Setter for receipt's resumption of an other receipt
423          * <p>
424          * @param receiptResumptionOf Current receipt is based on this receipt
425          */
426         public void setReceiptResumptionOf (final BillableReceipt receiptResumptionOf) {
427                 this.receiptResumptionOf = receiptResumptionOf;
428         }
429
430         /**
431          * Getter for receipt seller employee
432          * <p>
433          * @return Receipt seller employee
434          */
435         public Employable getReceiptSellerEmployee () {
436                 return this.receiptSellerEmployee;
437         }
438
439         /**
440          * Setter for receipt seller employee
441          * <p>
442          * @param receiptSellerEmployee Receipt seller employee
443          */
444         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
445                 this.receiptSellerEmployee = receiptSellerEmployee;
446         }
447
448         /**
449          * Getter for receipt sequence number
450          * <p>
451          * @return Receipt sequence number
452          */
453         public Long getReceiptSequenceNumber () {
454                 return this.receiptSequenceNumber;
455         }
456
457         /**
458          * Setter for receipt sequence number
459          * <p>
460          * @param receiptSequenceNumber Receipt sequence number
461          */
462         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
463                 this.receiptSequenceNumber = receiptSequenceNumber;
464         }
465
466         /**
467          * Getter for receipt transaction number
468          * <p>
469          * @return Receipt transaction number
470          */
471         public Long getReceiptTransactionNumber () {
472                 return this.receiptTransactionNumber;
473         }
474
475         /**
476          * Setter for receipt transaction number
477          * <p>
478          * @param receiptTransactionNumber Receipt transaction number
479          */
480         public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
481                 this.receiptTransactionNumber = receiptTransactionNumber;
482         }
483
484         /**
485          * Getter for user instance
486          * <p>
487          * @return User instance
488          */
489         public User getReceiptUser () {
490                 return this.receiptUser;
491         }
492
493         /**
494          * Setter for user instance
495          * <p>
496          * @param receiptUser User instance
497          */
498         public void setReceiptUser (final User receiptUser) {
499                 this.receiptUser = receiptUser;
500         }
501
502         /**
503          * Updates receipt instance with new data. This copies all fields from this
504          * backing bean into currentReceipt instance which should be still there.
505          */
506         public String updateReceipt () {
507                 // Is current instance still there?
508                 if (this.getCurrentReceipt() == null) {
509                         // Throw NPE
510                         throw new NullPointerException("this.currentReceipt is null"); //NOI18N
511                 } else if (this.getCurrentReceipt().getReceiptId() == null) {
512                         // Throw NPE again
513                         throw new NullPointerException("this.currentReceipt.receiptId is null"); //NOI18N
514                 }
515
516                 // Create new instance
517                 final BillableReceipt receipt = this.createReceiptInstance();
518
519                 // Is it the same?
520                 if (Objects.equals(receipt, this.getCurrentReceipt())) {
521                         // Yes, then abort here with message to user
522                         this.showFacesMessage("form-edit-receipt", "ADMIN_ERROR_FINANCIAL_RECEIPT_NOT_CHANGED", FacesMessage.SEVERITY_WARN); //NOI18N
523                         return ""; //NOI18N
524                 }
525
526                 // Copy all data
527                 Receipts.copyReceiptData(receipt, this.getCurrentReceipt());
528
529                 // Create updated receipt instance
530                 final BillableReceipt updatedReceipt;
531
532                 try {
533                         // Invoke EJB method
534                         updatedReceipt = this.adminReceiptBean.updateReceipt(this.getCurrentReceipt());
535                 } catch (final ReceiptNotFoundException ex) {
536                         // Throw as cause
537                         throw new FacesException(ex);
538                 }
539
540                 // Fire event
541                 this.adminUpdatedReceiptEvent.fire(new ReceiptUpdatedEvent(updatedReceipt));
542
543                 // Clear bean
544                 this.clear();
545
546                 // Redirect to list view
547                 return "admin_list_receipts"; //NOI18N
548         }
549
550         /**
551          * Clears this bean
552          */
553         private void clear () {
554                 // Clear all fields
555                 this.setReceiptBarCodeNumber(null);
556                 this.setReceiptBonusCard(null);
557                 this.setReceiptBranchOffice(null);
558                 this.setReceiptId(null);
559                 this.setReceiptIssued(null);
560                 this.setReceiptNumber(null);
561                 this.setReceiptPaymentType(null);
562                 this.setReceiptRegisterNumber(null);
563                 this.setReceiptResumptionOf(null);
564                 this.setReceiptSellerEmployee(null);
565                 this.setReceiptSequenceNumber(null);
566                 this.setReceiptTransactionNumber(null);
567                 this.setReceiptUser(null);
568         }
569
570         /**
571          * Creates a new instance from all available data of this bean.
572          * <p>
573          * @return Receipt instance
574          */
575         private BillableReceipt createReceiptInstance () {
576                 // Create new instance with minimum required data
577                 final BillableReceipt receipt = new FinancialReceipt(
578                                                           this.getReceiptPaymentType(),
579                                                           this.getReceiptBranchOffice(),
580                                                           this.getReceiptIssued()
581                                           );
582
583                 // Set optional data
584                 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
585                 receipt.setReceiptBonusCard(this.getReceiptBonusCard());
586                 receipt.setReceiptId(this.getReceiptId());
587                 receipt.setReceiptNumber(this.getReceiptNumber());
588                 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
589                 receipt.setReceiptResumptionOf(this.getReceiptResumptionOf());
590                 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
591                 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
592                 receipt.setReceiptTransactionNumber(this.getReceiptTransactionNumber());
593                 receipt.setReceiptUser(this.getReceiptUser());
594
595                 // Return prepared instance
596                 return receipt;
597         }
598
599 }