]> git.mxchange.org Git - jfinancials-war.git/blob
2efe2a489a35ef7469135271aa82a9bb3b24fee1
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 - 2022 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.utils.ReceiptUtils;
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          * QR code
130          */
131         private String receiptQrCode;
132
133         /**
134          * Register number
135          */
136         private Long receiptRegisterNumber;
137
138         /**
139          * Current receipt is a resumption of this receipt
140          */
141         private BillableReceipt receiptResumptionOf;
142
143         /**
144          * Selling employee
145          */
146         private Employable receiptSellerEmployee;
147
148         /**
149          * Sequence number
150          */
151         private Long receiptSequenceNumber;
152
153         /**
154          * Transaction number
155          */
156         private Long receiptTransactionNumber;
157
158         /**
159          * User who "owns" this receipt
160          */
161         private User receiptUser;
162
163         /**
164          * Default constructor
165          */
166         public FinancialAdminReceiptActionWebViewBean () {
167                 // Call super constructor
168                 super();
169         }
170
171         /**
172          * Adds the completed receipt to database by calling an EJB business method.
173          * If not all required fields are set, a proper exception is being thrown.
174          * <p>
175          */
176         public void addReceipt () {
177                 // Are all required fields set?
178                 if (this.getReceiptBranchOffice() == null) {
179                         // Is not set
180                         throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
181                 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
182                         // It must be an already peristed instance
183                         throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
184                 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
185                         // It must be an already peristed instance
186                         throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
187                 } else if (this.getReceiptPaymentType() == null) {
188                         // Is not set
189                         throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
190                 } else if (this.getReceiptIssued() == null) {
191                         // Is not set
192                         throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
193                 }
194
195                 // Prepare receipt instance
196                 final BillableReceipt receipt = this.createReceiptInstance();
197
198                 // Is the receipt already there?
199                 if (this.receiptListController.isReceiptAdded(receipt)) {
200                         // Receipt has already been added
201                         throw new FacesException(MessageFormat.format(
202                                         "Receipt for receiptBranchOffice={0},receiptIssued={1},receiptNumber={2} has already been added.", //NOI18N
203                                         this.getReceiptBranchOffice().getBranchId(),
204                                         this.getReceiptIssued().toString(),
205                                         this.getReceiptNumber()
206                         ));
207                 }
208
209                 // Init variable
210                 final BillableReceipt updatedReceipt;
211
212                 // All is set, then try to call EJB
213                 try {
214                         // Add it
215                         updatedReceipt = this.adminReceiptBean.addReceipt(receipt);
216                 } catch (final ReceiptAlreadyAddedException ex) {
217                         // Throw it again
218                         throw new FacesException(ex);
219                 }
220
221                 // Fire event with updated instance
222                 this.adminAddedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
223
224                 // Clear bean
225                 this.clear();
226         }
227
228         /**
229          * Copies all fields from current receipt instance to local bean properties.
230          */
231         public void copyAllReceiptProperties () {
232                 // Is current receipt instance valid?
233                 if (this.getCurrentReceipt() == null) {
234                         // Not set
235                         throw new NullPointerException("this.currentReceipt is null"); //NOI18N
236                 }
237
238                 // Copy all fields
239                 this.setReceiptBarCodeNumber(this.getCurrentReceipt().getReceiptBarCodeNumber());
240                 this.setReceiptBonusCard(this.getCurrentReceipt().getReceiptBonusCard());
241                 this.setReceiptBranchOffice(this.getCurrentReceipt().getReceiptBranchOffice());
242                 this.setReceiptId(this.getCurrentReceipt().getReceiptId());
243                 this.setReceiptIssued(this.getCurrentReceipt().getReceiptIssued());
244                 this.setReceiptNumber(this.getCurrentReceipt().getReceiptNumber());
245                 this.setReceiptPaymentType(this.getCurrentReceipt().getReceiptPaymentType());
246                 this.setReceiptQrCode(this.getCurrentReceipt().getReceiptQrCode());
247                 this.setReceiptRegisterNumber(this.getCurrentReceipt().getReceiptRegisterNumber());
248                 this.setReceiptSellerEmployee(this.getCurrentReceipt().getReceiptSellerEmployee());
249                 this.setReceiptSequenceNumber(this.getCurrentReceipt().getReceiptSequenceNumber());
250                 this.setReceiptTransactionNumber(this.getCurrentReceipt().getReceiptTransactionNumber());
251                 this.setReceiptUser(this.getCurrentReceipt().getReceiptUser());
252         }
253
254         /**
255          * Getter for current receipt
256          * <p>
257          * @return Current receipt
258          */
259         public BillableReceipt getCurrentReceipt () {
260                 return this.currentReceipt;
261         }
262
263         /**
264          * Setter for current receipt
265          * <p>
266          * @param currentReceipt Current receipt
267          */
268         public void setCurrentReceipt (final BillableReceipt currentReceipt) {
269                 this.currentReceipt = currentReceipt;
270         }
271
272         /**
273          * Getter for receipt's bar-code number
274          * <p>
275          * @return Receipt's bar-code number
276          */
277         public String getReceiptBarCodeNumber () {
278                 return this.receiptBarCodeNumber;
279         }
280
281         /**
282          * Setter for receipt's bar-code number
283          * <p>
284          * @param receiptBarCodeNumber Receipt's bar-code number
285          */
286         public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
287                 this.receiptBarCodeNumber = receiptBarCodeNumber;
288         }
289
290         /**
291          * Getter for bonus card
292          * <p>
293          * @return Bonus card
294          */
295         public BonusCard getReceiptBonusCard () {
296                 return this.receiptBonusCard;
297         }
298
299         /**
300          * Setter for bonus card
301          * <p>
302          * @param receiptBonusCard Bonus card
303          */
304         public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
305                 this.receiptBonusCard = receiptBonusCard;
306         }
307
308         /**
309          * Getter for receipt issuing branch office
310          * <p>
311          * @return Receipt issuing branch office
312          */
313         public BranchOffice getReceiptBranchOffice () {
314                 return this.receiptBranchOffice;
315         }
316
317         /**
318          * Setter for receipt issuing branch office
319          * <p>
320          * @param receiptBranchOffice Receipt issuing branch office
321          */
322         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
323                 this.receiptBranchOffice = receiptBranchOffice;
324         }
325
326         /**
327          * Getter for receipt id
328          * <p>
329          * @return Receipt it
330          */
331         public Long getReceiptId () {
332                 return this.receiptId;
333         }
334
335         /**
336          * Setter for receipt id
337          * <p>
338          * @param receiptId Receipt id
339          */
340         public void setReceiptId (final Long receiptId) {
341                 this.receiptId = receiptId;
342         }
343
344         /**
345          * Getter for receipt issue date and time
346          * <p>
347          * @return Receipt issue date and time
348          */
349         @SuppressWarnings ("ReturnOfDateField")
350         public Date getReceiptIssued () {
351                 return this.receiptIssued;
352         }
353
354         /**
355          * Setter for receipt issue date and time
356          * <p>
357          * @param receiptIssued Receipt issue date and time
358          */
359         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
360         public void setReceiptIssued (final Date receiptIssued) {
361                 this.receiptIssued = receiptIssued;
362         }
363
364         /**
365          * Getter for receipt number
366          * <p>
367          * @return Receipt number
368          */
369         public String getReceiptNumber () {
370                 return this.receiptNumber;
371         }
372
373         /**
374          * Setter for receipt number
375          * <p>
376          * @param receiptNumber Receipt number
377          */
378         public void setReceiptNumber (final String receiptNumber) {
379                 this.receiptNumber = receiptNumber;
380         }
381
382         /**
383          * Getter for payment type
384          * <p>
385          * @return Payment type
386          */
387         public PaymentType getReceiptPaymentType () {
388                 return this.receiptPaymentType;
389         }
390
391         /**
392          * Setter for payment type
393          * <p>
394          * @param receiptPaymentType Payment type
395          */
396         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
397                 this.receiptPaymentType = receiptPaymentType;
398         }
399
400         /**
401          * Getter for receipt QR code
402          * <p>
403          * @return Receipt QR code
404          */
405         public String getReceiptQrCode () {
406                 return this.receiptQrCode;
407         }
408
409         /**
410          * Setter for receipt QR code
411          * <p>
412          * @param receiptQrCode Receipt QR code
413          */
414         public void setReceiptQrCode (final String receiptQrCode) {
415                 this.receiptQrCode = receiptQrCode;
416         }
417
418         /**
419          * Getter for receipt register's number
420          * <p>
421          * @return Receipt register's number
422          */
423         public Long getReceiptRegisterNumber () {
424                 return this.receiptRegisterNumber;
425         }
426
427         /**
428          * Setter for receipt register's number
429          * <p>
430          * @param receiptRegisterNumber Receipt register's number
431          */
432         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
433                 this.receiptRegisterNumber = receiptRegisterNumber;
434         }
435
436         /**
437          * Getter for receipt's resumption of an other receipt
438          * <p>
439          * @return Current receipt is based on this receipt
440          */
441         public BillableReceipt getReceiptResumptionOf () {
442                 return this.receiptResumptionOf;
443         }
444
445         /**
446          * Setter for receipt's resumption of an other receipt
447          * <p>
448          * @param receiptResumptionOf Current receipt is based on this receipt
449          */
450         public void setReceiptResumptionOf (final BillableReceipt receiptResumptionOf) {
451                 this.receiptResumptionOf = receiptResumptionOf;
452         }
453
454         /**
455          * Getter for receipt seller employee
456          * <p>
457          * @return Receipt seller employee
458          */
459         public Employable getReceiptSellerEmployee () {
460                 return this.receiptSellerEmployee;
461         }
462
463         /**
464          * Setter for receipt seller employee
465          * <p>
466          * @param receiptSellerEmployee Receipt seller employee
467          */
468         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
469                 this.receiptSellerEmployee = receiptSellerEmployee;
470         }
471
472         /**
473          * Getter for receipt sequence number
474          * <p>
475          * @return Receipt sequence number
476          */
477         public Long getReceiptSequenceNumber () {
478                 return this.receiptSequenceNumber;
479         }
480
481         /**
482          * Setter for receipt sequence number
483          * <p>
484          * @param receiptSequenceNumber Receipt sequence number
485          */
486         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
487                 this.receiptSequenceNumber = receiptSequenceNumber;
488         }
489
490         /**
491          * Getter for receipt transaction number
492          * <p>
493          * @return Receipt transaction number
494          */
495         public Long getReceiptTransactionNumber () {
496                 return this.receiptTransactionNumber;
497         }
498
499         /**
500          * Setter for receipt transaction number
501          * <p>
502          * @param receiptTransactionNumber Receipt transaction number
503          */
504         public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
505                 this.receiptTransactionNumber = receiptTransactionNumber;
506         }
507
508         /**
509          * Getter for user instance
510          * <p>
511          * @return User instance
512          */
513         public User getReceiptUser () {
514                 return this.receiptUser;
515         }
516
517         /**
518          * Setter for user instance
519          * <p>
520          * @param receiptUser User instance
521          */
522         public void setReceiptUser (final User receiptUser) {
523                 this.receiptUser = receiptUser;
524         }
525
526         /**
527          * Updates receipt instance with new data. This copies all fields from this
528          * backing bean into currentReceipt instance which should be still there.
529          *
530          * @return Redirect target
531          */
532         public String updateReceipt () {
533                 // Is current instance still there?
534                 if (this.getCurrentReceipt() == null) {
535                         // Throw NPE
536                         throw new NullPointerException("this.currentReceipt is null"); //NOI18N
537                 } else if (this.getCurrentReceipt().getReceiptId() == null) {
538                         // Throw NPE again
539                         throw new NullPointerException("this.currentReceipt.receiptId is null"); //NOI18N
540                 }
541
542                 // Create new instance
543                 final BillableReceipt receipt = this.createReceiptInstance();
544
545                 // Is it the same?
546                 if (Objects.equals(receipt, this.getCurrentReceipt())) {
547                         // Yes, then abort here with message to user
548                         this.showFacesMessage("form-edit-receipt", "ADMIN_ERROR_FINANCIAL_RECEIPT_NOT_CHANGED", FacesMessage.SEVERITY_WARN); //NOI18N
549                         return ""; //NOI18N
550                 }
551
552                 // Copy all data
553                 ReceiptUtils.copyReceiptData(receipt, this.getCurrentReceipt());
554
555                 // Create updated receipt instance
556                 final BillableReceipt updatedReceipt;
557
558                 try {
559                         // Invoke EJB method
560                         updatedReceipt = this.adminReceiptBean.updateReceipt(this.getCurrentReceipt());
561                 } catch (final ReceiptNotFoundException ex) {
562                         // Throw as cause
563                         throw new FacesException(ex);
564                 }
565
566                 // Fire event
567                 this.adminUpdatedReceiptEvent.fire(new ReceiptUpdatedEvent(updatedReceipt));
568
569                 // Clear bean
570                 this.clear();
571
572                 // Redirect to list view
573                 return "admin_list_receipts"; //NOI18N
574         }
575
576         /**
577          * Clears this bean
578          */
579         private void clear () {
580                 // Clear all fields
581                 this.setReceiptBarCodeNumber(null);
582                 this.setReceiptBonusCard(null);
583                 this.setReceiptBranchOffice(null);
584                 this.setReceiptId(null);
585                 this.setReceiptIssued(null);
586                 this.setReceiptNumber(null);
587                 this.setReceiptPaymentType(null);
588                 this.setReceiptQrCode(null);
589                 this.setReceiptRegisterNumber(null);
590                 this.setReceiptResumptionOf(null);
591                 this.setReceiptSellerEmployee(null);
592                 this.setReceiptSequenceNumber(null);
593                 this.setReceiptTransactionNumber(null);
594                 this.setReceiptUser(null);
595         }
596
597         /**
598          * Creates a new instance from all available data of this bean.
599          * <p>
600          * @return Receipt instance
601          */
602         private BillableReceipt createReceiptInstance () {
603                 // Create new instance with minimum required data
604                 final BillableReceipt receipt = new FinancialReceipt(
605                                                           this.getReceiptPaymentType(),
606                                                           this.getReceiptBranchOffice(),
607                                                           this.getReceiptIssued()
608                                           );
609
610                 // Set optional data
611                 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
612                 receipt.setReceiptBonusCard(this.getReceiptBonusCard());
613                 receipt.setReceiptId(this.getReceiptId());
614                 receipt.setReceiptNumber(this.getReceiptNumber());
615                 receipt.setReceiptQrCode(this.getReceiptQrCode());
616                 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
617                 receipt.setReceiptResumptionOf(this.getReceiptResumptionOf());
618                 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
619                 receipt.setReceiptSequenceNumber(this.getReceiptSequenceNumber());
620                 receipt.setReceiptTransactionNumber(this.getReceiptTransactionNumber());
621                 receipt.setReceiptUser(this.getReceiptUser());
622
623                 // Return prepared instance
624                 return receipt;
625         }
626
627 }