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