]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/receipt/FinancialReceipt.java
951ba6046722f1fbc00b347846cc0bafa6688c5d
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / receipt / FinancialReceipt.java
1 /*
2  * Copyright (C) 2016 - 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 General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jfinancials.model.receipt;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.EnumType;
27 import javax.persistence.Enumerated;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.Lob;
33 import javax.persistence.NamedQueries;
34 import javax.persistence.NamedQuery;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
37 import javax.persistence.Temporal;
38 import javax.persistence.TemporalType;
39 import javax.persistence.Transient;
40 import org.apache.commons.lang3.StringUtils;
41 import org.mxchange.jbonuscard.model.bonus_card.BonusCard;
42 import org.mxchange.jbonuscard.model.bonus_card.RoyalityCard;
43 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
44 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
45 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
46 import org.mxchange.jcontactsbusiness.model.employee.Employable;
47 import org.mxchange.jcontactsbusiness.model.utils.BranchOfficeUtils;
48 import org.mxchange.jcontactsbusiness.model.utils.EmployeeUtils;
49 import org.mxchange.jcoreutils.comparable.ComparableUtils;
50 import org.mxchange.jcoreutils.number.SafeNumberUtils;
51 import org.mxchange.jproduct.model.payment.PaymentType;
52 import org.mxchange.jusercore.model.user.LoginUser;
53 import org.mxchange.jusercore.model.user.User;
54 import org.mxchange.jusercore.model.utils.UserUtils;
55
56 /**
57  * Receipt POJO
58  * <p>
59  * @author Roland Häder<roland@mxchange.org>
60  */
61 @Entity (name = "receipts")
62 @Table (
63                 name = "receipts"
64 )
65 @NamedQueries (
66                 {
67                         @NamedQuery (name = "AllReceipts", query = "SELECT r FROM receipts AS r ORDER BY r.receiptId ASC")
68                 }
69 )
70 @SuppressWarnings ("PersistenceUnitPresent")
71 public class FinancialReceipt implements BillableReceipt {
72
73         /**
74          * Serial number
75          */
76         @Transient
77         private static final long serialVersionUID = 185_867_217_461L;
78
79         /**
80          * Receipt bar-code number
81          */
82         @Column (name = "receipt_barcode_number")
83         private String receiptBarCodeNumber;
84
85         /**
86          * Assigned bonus card
87          */
88         @JoinColumn (name = "receipt_bonus_card_id", referencedColumnName = "bonus_card_id")
89         @OneToOne (targetEntity = RoyalityCard.class, cascade = CascadeType.REFRESH)
90         private BonusCard receiptBonusCard;
91
92         /**
93          * Seller instance
94          */
95         @JoinColumn (name = "receipt_branch_id", referencedColumnName = "branch_id", nullable = false, updatable = false)
96         @OneToOne (targetEntity = BusinessBranchOffice.class, cascade = CascadeType.REFRESH, optional = false)
97         private BranchOffice receiptBranchOffice;
98
99         /**
100          * When this receipt entry has been created
101          */
102         @Basic (optional = false)
103         @Temporal (TemporalType.TIMESTAMP)
104         @Column (name = "receipt_entry_created", updatable = false, nullable = false)
105         private Date receiptEntryCreated;
106
107         /**
108          * When this receipt entry has been updated
109          */
110         @Temporal (TemporalType.TIMESTAMP)
111         @Column (name = "receipt_entry_updated", insertable = false)
112         private Date receiptEntryUpdated;
113
114         /**
115          * Primary key
116          */
117         @Id
118         @GeneratedValue (strategy = GenerationType.IDENTITY)
119         @Column (name = "receipt_id", nullable = false, updatable = false)
120         private Long receiptId;
121
122         /**
123          * When this receipt has been issued
124          */
125         @Basic (optional = false)
126         @Temporal (TemporalType.TIMESTAMP)
127         @Column (name = "receipt_issued", nullable = false)
128         private Date receiptIssued;
129
130         /**
131          * Receipt number
132          */
133         @Column (name = "receipt_number")
134         private String receiptNumber;
135
136         /**
137          * Payment type (cash, credit card, EC card ...)
138          */
139         @Basic (optional = false)
140         @Column (name = "receipt_payment_type", nullable = false)
141         @Enumerated (EnumType.STRING)
142         private PaymentType receiptPaymentType;
143
144         /**
145          * Recipient QR code
146          */
147         @Lob ()
148         @Column (name = "receipt_qr_code")
149         private String receiptQrCode;
150
151         /**
152          * Receipt register number
153          */
154         @Column (name = "receipt_register_number")
155         private Long receiptRegisterNumber;
156
157         /**
158          * Receipt resumes an other existing receipt
159          */
160         @JoinColumn (name = "receipt_resumption_id", referencedColumnName = "receipt_id")
161         @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH)
162         private BillableReceipt receiptResumptionOf;
163
164         /**
165          * Selling employee instance
166          */
167         @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
168         @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
169         private Employable receiptSellerEmployee;
170
171         /**
172          * Receipt sequence number
173          */
174         @Column (name = "receipt_sequence_number")
175         private Long receiptSequenceNumber;
176
177         /**
178          * Receipt transaction number
179          */
180         @Column (name = "receipt_transaction_number")
181         private Long receiptTransactionNumber;
182
183         /**
184          * Which user this receipt belongs to
185          */
186         @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
187         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
188         private User receiptUser;
189
190         /**
191          * Default constructor
192          */
193         public FinancialReceipt () {
194         }
195
196         /**
197          * Constructor with payment type, seller (branch office) and user
198          * <p>
199          * @param receiptPaymentType  Payment type
200          * @param receiptBranchOffice Branch office instance
201          * @param receiptUser         User instance
202          * @param receiptIssued       When this receipt has been issued
203          * <p>
204          * @throws NullPointerException If user instance is not set
205          * @throws IllegalArgumentException If user instance's userId is invalid
206          */
207         public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final User receiptUser, final Date receiptIssued) {
208                 // Call other constructor first
209                 this(receiptPaymentType, receiptBranchOffice, receiptIssued);
210
211                 // Validate parameter
212                 if (null == receiptUser) {
213                         // Throw NPE
214                         throw new NullPointerException("user is null"); //NOI18N
215                 } else if (receiptUser.getUserId() == null) {
216                         // Throw it again
217                         throw new NullPointerException("user.userId is null"); //NOI18N
218                 } else if (receiptUser.getUserId() < 1) {
219                         // Throw IAE
220                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
221                 }
222
223                 // Set user instance
224                 this.receiptUser = receiptUser;
225         }
226
227         /**
228          * Constructor with payment type, branch office and when it has been issued
229          * <p>
230          * @param receiptPaymentType  Payment type
231          * @param receiptBranchOffice Branch office instance
232          * @param receiptIssued       When this receipt has been issued
233          * <p>
234          * @throws NullPointerException If user instance is not set
235          * @throws IllegalArgumentException If branchId is invalid
236          */
237         public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
238                 // Call other constructor first
239                 this();
240
241                 // Validate all parameter
242                 if (null == receiptPaymentType) {
243                         // Throw NPE
244                         throw new NullPointerException("receiptPaymentType is null"); //NOI18N
245                 } else if (null == receiptBranchOffice) {
246                         // Throw NPE
247                         throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
248                 } else if (receiptBranchOffice.getBranchId() == null) {
249                         // Throw NPE
250                         throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
251                 } else if (receiptBranchOffice.getBranchId() < 1) {
252                         // Throw IAE
253                         throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
254                 } else if (null == receiptIssued) {
255                         // Throw NPE
256                         throw new NullPointerException("receiptIssued is null"); //NOI18N
257                 }
258
259                 // Set all values
260                 this.receiptPaymentType = receiptPaymentType;
261                 this.receiptBranchOffice = receiptBranchOffice;
262                 this.receiptIssued = receiptIssued;
263         }
264
265         @Override
266         public int compareTo (final BillableReceipt billableReceipt) {
267                 // Check parameter on null-reference and equality to this
268                 if (null == billableReceipt) {
269                         // Should not happen
270                         throw new NullPointerException("billableReceipt is null"); //NOI18N
271                 } else if (billableReceipt.equals(this)) {
272                         // Same object
273                         return 0;
274                 }
275
276                 // Init comparators
277                 final int comparators[] = {
278                         // First compare receipt number
279                         StringUtils.compare(this.getReceiptNumber(), billableReceipt.getReceiptNumber()),
280                         // ... next bar-code
281                         StringUtils.compare(this.getReceiptBarCodeNumber(), billableReceipt.getReceiptBarCodeNumber()),
282                         // ... sequence number
283                         SafeNumberUtils.compare(this.getReceiptSequenceNumber(), billableReceipt.getReceiptSequenceNumber()),
284                         // ... transaction number
285                         SafeNumberUtils.compare(this.getReceiptTransactionNumber(), billableReceipt.getReceiptTransactionNumber()),
286                         // ... payment type
287                         this.getReceiptPaymentType().compareTo(billableReceipt.getReceiptPaymentType()),
288                         // ... next QR code
289                         StringUtils.compare(this.getReceiptQrCode(), billableReceipt.getReceiptQrCode()),
290                         // ... register number
291                         SafeNumberUtils.compare(this.getReceiptRegisterNumber(), billableReceipt.getReceiptRegisterNumber()),
292                         // ... issue date
293                         this.getReceiptIssued().compareTo(billableReceipt.getReceiptIssued()),
294                         // ... next is seller instance
295                         EmployeeUtils.compare(this.getReceiptSellerEmployee(), billableReceipt.getReceiptSellerEmployee()),
296                         // .. branch office
297                         BranchOfficeUtils.compare(this.getReceiptBranchOffice(), billableReceipt.getReceiptBranchOffice()),
298                         // ... and user instance
299                         UserUtils.compare(this.getReceiptUser(), billableReceipt.getReceiptUser())
300                 };
301
302                 // Check all values
303                 final int comparison = ComparableUtils.checkAll(comparators);
304
305                 // Return value
306                 return comparison;
307         }
308
309         @Override
310         public boolean equals (final Object object) {
311                 if (this == object) {
312                         return true;
313                 } else if (null == object) {
314                         return false;
315                 } else if (this.getClass() != object.getClass()) {
316                         return false;
317                 }
318
319                 // Cast securely
320                 final BillableReceipt receipt = (BillableReceipt) object;
321
322                 // Now check some distincting class fields
323                 if (!Objects.equals(this.getReceiptBarCodeNumber(), receipt.getReceiptBarCodeNumber())) {
324                         return false;
325                 } else if (!Objects.equals(this.getReceiptBonusCard(), receipt.getReceiptBonusCard())) {
326                         return false;
327                 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
328                         return false;
329                 } else if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
330                         return false;
331                 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
332                         return false;
333                 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
334                         return false;
335                 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
336                         return false;
337                 } else if (!Objects.equals(this.getReceiptQrCode(), receipt.getReceiptQrCode())) {
338                         return false;
339                 } else if (!Objects.equals(this.getReceiptRegisterNumber(), receipt.getReceiptRegisterNumber())) {
340                         return false;
341                 } else if (!Objects.equals(this.getReceiptSellerEmployee(), receipt.getReceiptSellerEmployee())) {
342                         return false;
343                 } else if (!Objects.equals(this.getReceiptSequenceNumber(), receipt.getReceiptSequenceNumber())) {
344                         return false;
345                 } else if (!Objects.equals(this.getReceiptTransactionNumber(), receipt.getReceiptTransactionNumber())) {
346                         return false;
347                 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
348                         return false;
349                 }
350
351                 return true;
352         }
353
354         @Override
355         public String getReceiptBarCodeNumber () {
356                 return this.receiptBarCodeNumber;
357         }
358
359         @Override
360         public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
361                 this.receiptBarCodeNumber = receiptBarCodeNumber;
362         }
363
364         @Override
365         public BonusCard getReceiptBonusCard () {
366                 return this.receiptBonusCard;
367         }
368
369         @Override
370         public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
371                 this.receiptBonusCard = receiptBonusCard;
372         }
373
374         @Override
375         public BranchOffice getReceiptBranchOffice () {
376                 return this.receiptBranchOffice;
377         }
378
379         @Override
380         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
381                 this.receiptBranchOffice = receiptBranchOffice;
382         }
383
384         @Override
385         @SuppressWarnings ("ReturnOfDateField")
386         public Date getReceiptEntryCreated () {
387                 return this.receiptEntryCreated;
388         }
389
390         @Override
391         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
392         public void setReceiptEntryCreated (final Date receiptEntryCreated) {
393                 this.receiptEntryCreated = receiptEntryCreated;
394         }
395
396         @Override
397         @SuppressWarnings ("ReturnOfDateField")
398         public Date getReceiptEntryUpdated () {
399                 return this.receiptEntryUpdated;
400         }
401
402         @Override
403         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
404         public void setReceiptEntryUpdated (final Date receiptEntryUpdated) {
405                 this.receiptEntryUpdated = receiptEntryUpdated;
406         }
407
408         @Override
409         public Long getReceiptId () {
410                 return this.receiptId;
411         }
412
413         @Override
414         public void setReceiptId (final Long receiptId) {
415                 this.receiptId = receiptId;
416         }
417
418         @Override
419         @SuppressWarnings ("ReturnOfDateField")
420         public Date getReceiptIssued () {
421                 return this.receiptIssued;
422         }
423
424         @Override
425         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
426         public void setReceiptIssued (final Date receiptIssued) {
427                 this.receiptIssued = receiptIssued;
428         }
429
430         @Override
431         public String getReceiptNumber () {
432                 return this.receiptNumber;
433         }
434
435         @Override
436         public void setReceiptNumber (final String receiptNumber) {
437                 this.receiptNumber = receiptNumber;
438         }
439
440         @Override
441         public PaymentType getReceiptPaymentType () {
442                 return this.receiptPaymentType;
443         }
444
445         @Override
446         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
447                 this.receiptPaymentType = receiptPaymentType;
448         }
449
450         @Override
451         public String getReceiptQrCode () {
452                 return this.receiptQrCode;
453         }
454
455         @Override
456         public void setReceiptQrCode (final String receiptQrCode) {
457                 this.receiptQrCode = receiptQrCode;
458         }
459
460         @Override
461         public Long getReceiptRegisterNumber () {
462                 return this.receiptRegisterNumber;
463         }
464
465         @Override
466         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
467                 this.receiptRegisterNumber = receiptRegisterNumber;
468         }
469
470         @Override
471         public BillableReceipt getReceiptResumptionOf () {
472                 return this.receiptResumptionOf;
473         }
474
475         @Override
476         public void setReceiptResumptionOf (final BillableReceipt receiptResumptionOf) {
477                 this.receiptResumptionOf = receiptResumptionOf;
478         }
479
480         @Override
481         public Employable getReceiptSellerEmployee () {
482                 return this.receiptSellerEmployee;
483         }
484
485         @Override
486         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
487                 this.receiptSellerEmployee = receiptSellerEmployee;
488         }
489
490         @Override
491         public Long getReceiptSequenceNumber () {
492                 return this.receiptSequenceNumber;
493         }
494
495         @Override
496         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
497                 this.receiptSequenceNumber = receiptSequenceNumber;
498         }
499
500         @Override
501         public Long getReceiptTransactionNumber () {
502                 return this.receiptTransactionNumber;
503         }
504
505         @Override
506         public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
507                 this.receiptTransactionNumber = receiptTransactionNumber;
508         }
509
510         @Override
511         public User getReceiptUser () {
512                 return this.receiptUser;
513         }
514
515         @Override
516         public void setReceiptUser (final User receiptUser) {
517                 this.receiptUser = receiptUser;
518         }
519
520         @Override
521         public int hashCode () {
522                 int hash = 5;
523
524                 hash = 89 * hash + Objects.hashCode(this.getReceiptBarCodeNumber());
525                 hash = 89 * hash + Objects.hashCode(this.getReceiptBonusCard());
526                 hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
527                 hash = 89 * hash + Objects.hashCode(this.getReceiptId());
528                 hash = 89 * hash + Objects.hashCode(this.getReceiptIssued());
529                 hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
530                 hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
531                 hash = 89 * hash + Objects.hashCode(this.getReceiptQrCode());
532                 hash = 89 * hash + Objects.hashCode(this.getReceiptRegisterNumber());
533                 hash = 89 * hash + Objects.hashCode(this.getReceiptSellerEmployee());
534                 hash = 89 * hash + Objects.hashCode(this.getReceiptSequenceNumber());
535                 hash = 89 * hash + Objects.hashCode(this.getReceiptTransactionNumber());
536                 hash = 89 * hash + Objects.hashCode(this.getReceiptUser());
537
538                 return hash;
539         }
540
541 }