]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/receipt/FinancialReceipt.java
Continued:
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / receipt / FinancialReceipt.java
1 /*
2  * Copyright (C) 2016 - 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 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.BranchOffices;
45 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
46 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
47 import org.mxchange.jcontactsbusiness.model.employee.Employable;
48 import org.mxchange.jcontactsbusiness.model.employee.Employees;
49 import org.mxchange.jcoreutils.Comparables;
50 import org.mxchange.jcoreutils.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.user.Users;
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          * Receipt register number
146          */
147         @Column (name = "receipt_register_number")
148         private Long receiptRegisterNumber;
149
150         /**
151          * Receipt resumes an other existing receipt
152          */
153         @JoinColumn (name = "receipt_resumption_id", referencedColumnName = "receipt_id")
154         @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH)
155         private BillableReceipt receiptResumptionOf;
156
157         /**
158          * Selling employee instance
159          */
160         @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
161         @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
162         private Employable receiptSellerEmployee;
163
164         /**
165          * Receipt sequence number
166          */
167         @Column (name = "receipt_sequence_number")
168         private Long receiptSequenceNumber;
169
170         /**
171          * Receipt transaction number
172          */
173         @Column (name = "receipt_transaction_number")
174         private Long receiptTransactionNumber;
175
176         /**
177          * Which user this receipt belongs to
178          */
179         @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
180         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
181         private User receiptUser;
182
183         /**
184          * Recipient QR code
185          */
186         @Lob ()
187         @Column (name = "receipt_qr_code")
188         private String receiptQrCode;
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                         // ... register number
289                         SafeNumberUtils.compare(this.getReceiptRegisterNumber(), billableReceipt.getReceiptRegisterNumber()),
290                         // ... issue date
291                         this.getReceiptIssued().compareTo(billableReceipt.getReceiptIssued()),
292                         // ... next is seller instance
293                         Employees.compare(this.getReceiptSellerEmployee(), billableReceipt.getReceiptSellerEmployee()),
294                         // .. branch office
295                         BranchOffices.compare(this.getReceiptBranchOffice(), billableReceipt.getReceiptBranchOffice()),
296                         // ... and user instance
297                         Users.compare(this.getReceiptUser(), billableReceipt.getReceiptUser())
298                 };
299
300                 // Check all values
301                 final int comparison = Comparables.checkAll(comparators);
302
303                 // Return value
304                 return comparison;
305         }
306
307         @Override
308         public boolean equals (final Object object) {
309                 if (this == object) {
310                         return true;
311                 } else if (null == object) {
312                         return false;
313                 } else if (this.getClass() != object.getClass()) {
314                         return false;
315                 }
316
317                 // Cast securely
318                 final BillableReceipt receipt = (BillableReceipt) object;
319
320                 // Now check some distincting class fields
321                 if (!Objects.equals(this.getReceiptBarCodeNumber(), receipt.getReceiptBarCodeNumber())) {
322                         return false;
323                 } else if (!Objects.equals(this.getReceiptBonusCard(), receipt.getReceiptBonusCard())) {
324                         return false;
325                 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
326                         return false;
327                 } else if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
328                         return false;
329                 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
330                         return false;
331                 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
332                         return false;
333                 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
334                         return false;
335                 } else if (!Objects.equals(this.getReceiptRegisterNumber(), receipt.getReceiptRegisterNumber())) {
336                         return false;
337                 } else if (!Objects.equals(this.getReceiptSellerEmployee(), receipt.getReceiptSellerEmployee())) {
338                         return false;
339                 } else if (!Objects.equals(this.getReceiptSequenceNumber(), receipt.getReceiptSequenceNumber())) {
340                         return false;
341                 } else if (!Objects.equals(this.getReceiptTransactionNumber(), receipt.getReceiptTransactionNumber())) {
342                         return false;
343                 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
344                         return false;
345                 }
346
347                 return true;
348         }
349
350         @Override
351         public String getReceiptBarCodeNumber () {
352                 return this.receiptBarCodeNumber;
353         }
354
355         @Override
356         public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
357                 this.receiptBarCodeNumber = receiptBarCodeNumber;
358         }
359
360         @Override
361         public BonusCard getReceiptBonusCard () {
362                 return this.receiptBonusCard;
363         }
364
365         @Override
366         public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
367                 this.receiptBonusCard = receiptBonusCard;
368         }
369
370         @Override
371         public BranchOffice getReceiptBranchOffice () {
372                 return this.receiptBranchOffice;
373         }
374
375         @Override
376         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
377                 this.receiptBranchOffice = receiptBranchOffice;
378         }
379
380         @Override
381         @SuppressWarnings ("ReturnOfDateField")
382         public Date getReceiptEntryCreated () {
383                 return this.receiptEntryCreated;
384         }
385
386         @Override
387         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
388         public void setReceiptEntryCreated (final Date receiptEntryCreated) {
389                 this.receiptEntryCreated = receiptEntryCreated;
390         }
391
392         @Override
393         @SuppressWarnings ("ReturnOfDateField")
394         public Date getReceiptEntryUpdated () {
395                 return this.receiptEntryUpdated;
396         }
397
398         @Override
399         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
400         public void setReceiptEntryUpdated (final Date receiptEntryUpdated) {
401                 this.receiptEntryUpdated = receiptEntryUpdated;
402         }
403
404         @Override
405         public Long getReceiptId () {
406                 return this.receiptId;
407         }
408
409         @Override
410         public void setReceiptId (final Long receiptId) {
411                 this.receiptId = receiptId;
412         }
413
414         @Override
415         @SuppressWarnings ("ReturnOfDateField")
416         public Date getReceiptIssued () {
417                 return this.receiptIssued;
418         }
419
420         @Override
421         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
422         public void setReceiptIssued (final Date receiptIssued) {
423                 this.receiptIssued = receiptIssued;
424         }
425
426         @Override
427         public String getReceiptNumber () {
428                 return this.receiptNumber;
429         }
430
431         @Override
432         public void setReceiptNumber (final String receiptNumber) {
433                 this.receiptNumber = receiptNumber;
434         }
435
436         @Override
437         public PaymentType getReceiptPaymentType () {
438                 return this.receiptPaymentType;
439         }
440
441         @Override
442         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
443                 this.receiptPaymentType = receiptPaymentType;
444         }
445
446         @Override
447         public String getReceiptQrCode () {
448                 return this.receiptQrCode;
449         }
450
451         @Override
452         public void setReceiptQrCode (final String receiptQrCode) {
453                 this.receiptQrCode = receiptQrCode;
454         }
455
456         @Override
457         public Long getReceiptRegisterNumber () {
458                 return this.receiptRegisterNumber;
459         }
460
461         @Override
462         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
463                 this.receiptRegisterNumber = receiptRegisterNumber;
464         }
465
466         @Override
467         public BillableReceipt getReceiptResumptionOf () {
468                 return this.receiptResumptionOf;
469         }
470
471         @Override
472         public void setReceiptResumptionOf (final BillableReceipt receiptResumptionOf) {
473                 this.receiptResumptionOf = receiptResumptionOf;
474         }
475
476         @Override
477         public Employable getReceiptSellerEmployee () {
478                 return this.receiptSellerEmployee;
479         }
480
481         @Override
482         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
483                 this.receiptSellerEmployee = receiptSellerEmployee;
484         }
485
486         @Override
487         public Long getReceiptSequenceNumber () {
488                 return this.receiptSequenceNumber;
489         }
490
491         @Override
492         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
493                 this.receiptSequenceNumber = receiptSequenceNumber;
494         }
495
496         @Override
497         public Long getReceiptTransactionNumber () {
498                 return this.receiptTransactionNumber;
499         }
500
501         @Override
502         public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
503                 this.receiptTransactionNumber = receiptTransactionNumber;
504         }
505
506         @Override
507         public User getReceiptUser () {
508                 return this.receiptUser;
509         }
510
511         @Override
512         public void setReceiptUser (final User receiptUser) {
513                 this.receiptUser = receiptUser;
514         }
515
516         @Override
517         public int hashCode () {
518                 int hash = 5;
519
520                 hash = 89 * hash + Objects.hashCode(this.getReceiptBarCodeNumber());
521                 hash = 89 * hash + Objects.hashCode(this.getReceiptBonusCard());
522                 hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
523                 hash = 89 * hash + Objects.hashCode(this.getReceiptId());
524                 hash = 89 * hash + Objects.hashCode(this.getReceiptIssued());
525                 hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
526                 hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
527                 hash = 89 * hash + Objects.hashCode(this.getReceiptQrCode());
528                 hash = 89 * hash + Objects.hashCode(this.getReceiptRegisterNumber());
529                 hash = 89 * hash + Objects.hashCode(this.getReceiptSellerEmployee());
530                 hash = 89 * hash + Objects.hashCode(this.getReceiptSequenceNumber());
531                 hash = 89 * hash + Objects.hashCode(this.getReceiptTransactionNumber());
532                 hash = 89 * hash + Objects.hashCode(this.getReceiptUser());
533
534                 return hash;
535         }
536
537 }