2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.model.receipt;
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;
59 * @author Roland Häder<roland@mxchange.org>
61 @Entity (name = "receipts")
67 @NamedQuery (name = "AllReceipts", query = "SELECT r FROM receipts AS r ORDER BY r.receiptId ASC")
70 @SuppressWarnings ("PersistenceUnitPresent")
71 public class FinancialReceipt implements BillableReceipt {
77 private static final long serialVersionUID = 185_867_217_461L;
80 * Receipt bar-code number
82 @Column (name = "receipt_barcode_number")
83 private String receiptBarCodeNumber;
88 @JoinColumn (name = "receipt_bonus_card_id", referencedColumnName = "bonus_card_id")
89 @OneToOne (targetEntity = RoyalityCard.class, cascade = CascadeType.REFRESH)
90 private BonusCard receiptBonusCard;
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;
100 * When this receipt entry has been created
102 @Basic (optional = false)
103 @Temporal (TemporalType.TIMESTAMP)
104 @Column (name = "receipt_entry_created", updatable = false, nullable = false)
105 private Date receiptEntryCreated;
108 * When this receipt entry has been updated
110 @Temporal (TemporalType.TIMESTAMP)
111 @Column (name = "receipt_entry_updated", insertable = false)
112 private Date receiptEntryUpdated;
118 @GeneratedValue (strategy = GenerationType.IDENTITY)
119 @Column (name = "receipt_id", nullable = false, updatable = false)
120 private Long receiptId;
123 * When this receipt has been issued
125 @Basic (optional = false)
126 @Temporal (TemporalType.TIMESTAMP)
127 @Column (name = "receipt_issued", nullable = false)
128 private Date receiptIssued;
133 @Column (name = "receipt_number")
134 private String receiptNumber;
137 * Payment type (cash, credit card, EC card ...)
139 @Basic (optional = false)
140 @Column (name = "receipt_payment_type", nullable = false)
141 @Enumerated (EnumType.STRING)
142 private PaymentType receiptPaymentType;
148 @Column (name = "receipt_qr_code")
149 private String receiptQrCode;
152 * Receipt register number
154 @Column (name = "receipt_register_number")
155 private Long receiptRegisterNumber;
158 * Receipt resumes an other existing receipt
160 @JoinColumn (name = "receipt_resumption_id", referencedColumnName = "receipt_id")
161 @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH)
162 private BillableReceipt receiptResumptionOf;
165 * Selling employee instance
167 @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
168 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
169 private Employable receiptSellerEmployee;
172 * Receipt sequence number
174 @Column (name = "receipt_sequence_number")
175 private Long receiptSequenceNumber;
178 * Receipt transaction number
180 @Column (name = "receipt_transaction_number")
181 private Long receiptTransactionNumber;
184 * Which user this receipt belongs to
186 @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
187 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
188 private User receiptUser;
191 * Default constructor
193 public FinancialReceipt () {
197 * Constructor with payment type, seller (branch office) and user
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
204 * @throws NullPointerException If user instance is not set
205 * @throws IllegalArgumentException If user instance's userId is invalid
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);
211 // Validate parameter
212 if (null == receiptUser) {
214 throw new NullPointerException("user is null"); //NOI18N
215 } else if (receiptUser.getUserId() == null) {
217 throw new NullPointerException("user.userId is null"); //NOI18N
218 } else if (receiptUser.getUserId() < 1) {
220 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
224 this.receiptUser = receiptUser;
228 * Constructor with payment type, branch office and when it has been issued
230 * @param receiptPaymentType Payment type
231 * @param receiptBranchOffice Branch office instance
232 * @param receiptIssued When this receipt has been issued
234 * @throws NullPointerException If user instance is not set
235 * @throws IllegalArgumentException If branchId is invalid
237 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
238 // Call other constructor first
241 // Validate all parameter
242 if (null == receiptPaymentType) {
244 throw new NullPointerException("receiptPaymentType is null"); //NOI18N
245 } else if (null == receiptBranchOffice) {
247 throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
248 } else if (receiptBranchOffice.getBranchId() == null) {
250 throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
251 } else if (receiptBranchOffice.getBranchId() < 1) {
253 throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
254 } else if (null == receiptIssued) {
256 throw new NullPointerException("receiptIssued is null"); //NOI18N
260 this.receiptPaymentType = receiptPaymentType;
261 this.receiptBranchOffice = receiptBranchOffice;
262 this.receiptIssued = receiptIssued;
266 public int compareTo (final BillableReceipt billableReceipt) {
267 // Check parameter on null-reference and equality to this
268 if (null == billableReceipt) {
270 throw new NullPointerException("billableReceipt is null"); //NOI18N
271 } else if (billableReceipt.equals(this)) {
277 final int comparators[] = {
278 // First compare receipt number
279 StringUtils.compare(this.getReceiptNumber(), billableReceipt.getReceiptNumber()),
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()),
287 this.getReceiptPaymentType().compareTo(billableReceipt.getReceiptPaymentType()),
289 StringUtils.compare(this.getReceiptQrCode(), billableReceipt.getReceiptQrCode()),
290 // ... register number
291 SafeNumberUtils.compare(this.getReceiptRegisterNumber(), billableReceipt.getReceiptRegisterNumber()),
293 this.getReceiptIssued().compareTo(billableReceipt.getReceiptIssued()),
294 // ... next is seller instance
295 EmployeeUtils.compare(this.getReceiptSellerEmployee(), billableReceipt.getReceiptSellerEmployee()),
297 BranchOfficeUtils.compare(this.getReceiptBranchOffice(), billableReceipt.getReceiptBranchOffice()),
298 // ... and user instance
299 UserUtils.compare(this.getReceiptUser(), billableReceipt.getReceiptUser())
303 final int comparison = ComparableUtils.checkAll(comparators);
310 public boolean equals (final Object object) {
311 if (this == object) {
313 } else if (null == object) {
315 } else if (this.getClass() != object.getClass()) {
320 final BillableReceipt receipt = (BillableReceipt) object;
322 // Now check some distincting class fields
323 if (!Objects.equals(this.getReceiptBarCodeNumber(), receipt.getReceiptBarCodeNumber())) {
325 } else if (!Objects.equals(this.getReceiptBonusCard(), receipt.getReceiptBonusCard())) {
327 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
329 } else if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
331 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
333 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
335 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
337 } else if (!Objects.equals(this.getReceiptQrCode(), receipt.getReceiptQrCode())) {
339 } else if (!Objects.equals(this.getReceiptRegisterNumber(), receipt.getReceiptRegisterNumber())) {
341 } else if (!Objects.equals(this.getReceiptSellerEmployee(), receipt.getReceiptSellerEmployee())) {
343 } else if (!Objects.equals(this.getReceiptSequenceNumber(), receipt.getReceiptSequenceNumber())) {
345 } else if (!Objects.equals(this.getReceiptTransactionNumber(), receipt.getReceiptTransactionNumber())) {
347 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
355 public String getReceiptBarCodeNumber () {
356 return this.receiptBarCodeNumber;
360 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
361 this.receiptBarCodeNumber = receiptBarCodeNumber;
365 public BonusCard getReceiptBonusCard () {
366 return this.receiptBonusCard;
370 public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
371 this.receiptBonusCard = receiptBonusCard;
375 public BranchOffice getReceiptBranchOffice () {
376 return this.receiptBranchOffice;
380 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
381 this.receiptBranchOffice = receiptBranchOffice;
385 @SuppressWarnings ("ReturnOfDateField")
386 public Date getReceiptEntryCreated () {
387 return this.receiptEntryCreated;
391 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
392 public void setReceiptEntryCreated (final Date receiptEntryCreated) {
393 this.receiptEntryCreated = receiptEntryCreated;
397 @SuppressWarnings ("ReturnOfDateField")
398 public Date getReceiptEntryUpdated () {
399 return this.receiptEntryUpdated;
403 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
404 public void setReceiptEntryUpdated (final Date receiptEntryUpdated) {
405 this.receiptEntryUpdated = receiptEntryUpdated;
409 public Long getReceiptId () {
410 return this.receiptId;
414 public void setReceiptId (final Long receiptId) {
415 this.receiptId = receiptId;
419 @SuppressWarnings ("ReturnOfDateField")
420 public Date getReceiptIssued () {
421 return this.receiptIssued;
425 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
426 public void setReceiptIssued (final Date receiptIssued) {
427 this.receiptIssued = receiptIssued;
431 public String getReceiptNumber () {
432 return this.receiptNumber;
436 public void setReceiptNumber (final String receiptNumber) {
437 this.receiptNumber = receiptNumber;
441 public PaymentType getReceiptPaymentType () {
442 return this.receiptPaymentType;
446 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
447 this.receiptPaymentType = receiptPaymentType;
451 public String getReceiptQrCode () {
452 return this.receiptQrCode;
456 public void setReceiptQrCode (final String receiptQrCode) {
457 this.receiptQrCode = receiptQrCode;
461 public Long getReceiptRegisterNumber () {
462 return this.receiptRegisterNumber;
466 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
467 this.receiptRegisterNumber = receiptRegisterNumber;
471 public BillableReceipt getReceiptResumptionOf () {
472 return this.receiptResumptionOf;
476 public void setReceiptResumptionOf (final BillableReceipt receiptResumptionOf) {
477 this.receiptResumptionOf = receiptResumptionOf;
481 public Employable getReceiptSellerEmployee () {
482 return this.receiptSellerEmployee;
486 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
487 this.receiptSellerEmployee = receiptSellerEmployee;
491 public Long getReceiptSequenceNumber () {
492 return this.receiptSequenceNumber;
496 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
497 this.receiptSequenceNumber = receiptSequenceNumber;
501 public Long getReceiptTransactionNumber () {
502 return this.receiptTransactionNumber;
506 public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
507 this.receiptTransactionNumber = receiptTransactionNumber;
511 public User getReceiptUser () {
512 return this.receiptUser;
516 public void setReceiptUser (final User receiptUser) {
517 this.receiptUser = receiptUser;
521 public int hashCode () {
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());