2 * Copyright (C) 2016 - 2020 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.NamedQueries;
33 import javax.persistence.NamedQuery;
34 import javax.persistence.OneToOne;
35 import javax.persistence.Table;
36 import javax.persistence.Temporal;
37 import javax.persistence.TemporalType;
38 import javax.persistence.Transient;
39 import org.apache.commons.lang3.StringUtils;
40 import org.mxchange.jbonuscard.model.bonus_card.BonusCard;
41 import org.mxchange.jbonuscard.model.bonus_card.RoyalityCard;
42 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
43 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
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.employee.Employees;
48 import org.mxchange.jcoreutils.Comparables;
49 import org.mxchange.jcoreutils.SafeNumberUtils;
50 import org.mxchange.jproduct.model.payment.PaymentType;
51 import org.mxchange.jusercore.model.user.LoginUser;
52 import org.mxchange.jusercore.model.user.User;
53 import org.mxchange.jusercore.model.user.Users;
58 * @author Roland Häder<roland@mxchange.org>
60 @Entity (name = "receipts")
66 @NamedQuery (name = "AllReceipts", query = "SELECT r FROM receipts AS r ORDER BY r.receiptId ASC")
69 @SuppressWarnings ("PersistenceUnitPresent")
70 public class FinancialReceipt implements BillableReceipt {
76 private static final long serialVersionUID = 185_867_217_461L;
79 * Receipt bar-code number
81 @Column (name = "receipt_barcode_number")
82 private String receiptBarCodeNumber;
87 @JoinColumn (name = "receipt_bonus_card_id", referencedColumnName = "bonus_card_id")
88 @OneToOne (targetEntity = RoyalityCard.class, cascade = CascadeType.REFRESH)
89 private BonusCard receiptBonusCard;
94 @JoinColumn (name = "receipt_branch_id", referencedColumnName = "branch_id", nullable = false, updatable = false)
95 @OneToOne (targetEntity = BusinessBranchOffice.class, cascade = CascadeType.REFRESH, optional = false)
96 private BranchOffice receiptBranchOffice;
99 * When this receipt entry has been created
101 @Basic (optional = false)
102 @Temporal (TemporalType.TIMESTAMP)
103 @Column (name = "receipt_entry_created", updatable = false, nullable = false)
104 private Date receiptEntryCreated;
107 * When this receipt entry has been updated
109 @Temporal (TemporalType.TIMESTAMP)
110 @Column (name = "receipt_entry_updated", insertable = false)
111 private Date receiptEntryUpdated;
117 @GeneratedValue (strategy = GenerationType.IDENTITY)
118 @Column (name = "receipt_id", nullable = false, updatable = false)
119 private Long receiptId;
122 * When this receipt has been issued
124 @Basic (optional = false)
125 @Temporal (TemporalType.TIMESTAMP)
126 @Column (name = "receipt_issued", nullable = false)
127 private Date receiptIssued;
132 @Column (name = "receipt_number")
133 private String receiptNumber;
136 * Payment type (cash, credit card, EC card ...)
138 @Basic (optional = false)
139 @Column (name = "receipt_payment_type", nullable = false)
140 @Enumerated (EnumType.STRING)
141 private PaymentType receiptPaymentType;
144 * Receipt register number
146 @Column (name = "receipt_register_number")
147 private Long receiptRegisterNumber;
150 * Receipt resumes an other existing receipt
152 @JoinColumn (name = "receipt_resumption_id", referencedColumnName = "receipt_id")
153 @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH)
154 private BillableReceipt receiptResumptionOf;
157 * Selling employee instance
159 @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
160 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
161 private Employable receiptSellerEmployee;
164 * Receipt sequence number
166 @Column (name = "receipt_sequence_number")
167 private Long receiptSequenceNumber;
170 * Receipt transaction number
172 @Column (name = "receipt_transaction_number")
173 private Long receiptTransactionNumber;
176 * Which user this receipt belongs to
178 @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
179 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
180 private User receiptUser;
183 * Default constructor
185 public FinancialReceipt () {
189 * Constructor with payment type, seller (branch office) and user
191 * @param receiptPaymentType Payment type
192 * @param receiptBranchOffice Branch office instance
193 * @param receiptUser User instance
194 * @param receiptIssued When this receipt has been issued
196 * @throws NullPointerException If user instance is not set
197 * @throws IllegalArgumentException If user instance's userId is invalid
199 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final User receiptUser, final Date receiptIssued) {
200 // Call other constructor first
201 this(receiptPaymentType, receiptBranchOffice, receiptIssued);
203 // Validate parameter
204 if (null == receiptUser) {
206 throw new NullPointerException("user is null"); //NOI18N
207 } else if (receiptUser.getUserId() == null) {
209 throw new NullPointerException("user.userId is null"); //NOI18N
210 } else if (receiptUser.getUserId() < 1) {
212 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
216 this.receiptUser = receiptUser;
220 * Constructor with payment type, branch office and when it has been issued
222 * @param receiptPaymentType Payment type
223 * @param receiptBranchOffice Branch office instance
224 * @param receiptIssued When this receipt has been issued
226 * @throws NullPointerException If user instance is not set
227 * @throws IllegalArgumentException If branchId is invalid
229 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
230 // Call other constructor first
233 // Validate all parameter
234 if (null == receiptPaymentType) {
236 throw new NullPointerException("receiptPaymentType is null"); //NOI18N
237 } else if (null == receiptBranchOffice) {
239 throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
240 } else if (receiptBranchOffice.getBranchId() == null) {
242 throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
243 } else if (receiptBranchOffice.getBranchId() < 1) {
245 throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
246 } else if (null == receiptIssued) {
248 throw new NullPointerException("receiptIssued is null"); //NOI18N
252 this.receiptPaymentType = receiptPaymentType;
253 this.receiptBranchOffice = receiptBranchOffice;
254 this.receiptIssued = receiptIssued;
258 public int compareTo (final BillableReceipt billableReceipt) {
259 // Check parameter on null-reference and equality to this
260 if (null == billableReceipt) {
262 throw new NullPointerException("billableReceipt is null"); //NOI18N
263 } else if (billableReceipt.equals(this)) {
269 final int comparators[] = {
270 // First compare receipt number
271 StringUtils.compare(this.getReceiptNumber(), billableReceipt.getReceiptNumber()),
273 StringUtils.compare(this.getReceiptBarCodeNumber(), billableReceipt.getReceiptBarCodeNumber()),
274 // ... sequence number
275 SafeNumberUtils.compare(this.getReceiptSequenceNumber(), billableReceipt.getReceiptSequenceNumber()),
276 // ... transaction number
277 SafeNumberUtils.compare(this.getReceiptTransactionNumber(), billableReceipt.getReceiptTransactionNumber()),
279 this.getReceiptPaymentType().compareTo(billableReceipt.getReceiptPaymentType()),
280 // ... register number
281 SafeNumberUtils.compare(this.getReceiptRegisterNumber(), billableReceipt.getReceiptRegisterNumber()),
283 this.getReceiptIssued().compareTo(billableReceipt.getReceiptIssued()),
284 // ... next is seller instance
285 Employees.compare(this.getReceiptSellerEmployee(), billableReceipt.getReceiptSellerEmployee()),
287 BranchOffices.compare(this.getReceiptBranchOffice(), billableReceipt.getReceiptBranchOffice()),
288 // ... and user instance
289 Users.compare(this.getReceiptUser(), billableReceipt.getReceiptUser())
293 final int comparison = Comparables.checkAll(comparators);
300 public boolean equals (final Object object) {
301 if (this == object) {
303 } else if (null == object) {
305 } else if (this.getClass() != object.getClass()) {
310 final BillableReceipt receipt = (BillableReceipt) object;
312 // Now check some distincting class fields
313 if (!Objects.equals(this.getReceiptBarCodeNumber(), receipt.getReceiptBarCodeNumber())) {
315 } else if (!Objects.equals(this.getReceiptBonusCard(), receipt.getReceiptBonusCard())) {
317 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
319 } else if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
321 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
323 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
325 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
327 } else if (!Objects.equals(this.getReceiptRegisterNumber(), receipt.getReceiptRegisterNumber())) {
329 } else if (!Objects.equals(this.getReceiptSellerEmployee(), receipt.getReceiptSellerEmployee())) {
331 } else if (!Objects.equals(this.getReceiptSequenceNumber(), receipt.getReceiptSequenceNumber())) {
333 } else if (!Objects.equals(this.getReceiptTransactionNumber(), receipt.getReceiptTransactionNumber())) {
335 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
343 public String getReceiptBarCodeNumber () {
344 return this.receiptBarCodeNumber;
348 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
349 this.receiptBarCodeNumber = receiptBarCodeNumber;
353 public BonusCard getReceiptBonusCard () {
354 return this.receiptBonusCard;
358 public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
359 this.receiptBonusCard = receiptBonusCard;
363 public BranchOffice getReceiptBranchOffice () {
364 return this.receiptBranchOffice;
368 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
369 this.receiptBranchOffice = receiptBranchOffice;
373 @SuppressWarnings ("ReturnOfDateField")
374 public Date getReceiptEntryCreated () {
375 return this.receiptEntryCreated;
379 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
380 public void setReceiptEntryCreated (final Date receiptEntryCreated) {
381 this.receiptEntryCreated = receiptEntryCreated;
385 @SuppressWarnings ("ReturnOfDateField")
386 public Date getReceiptEntryUpdated () {
387 return this.receiptEntryUpdated;
391 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
392 public void setReceiptEntryUpdated (final Date receiptEntryUpdated) {
393 this.receiptEntryUpdated = receiptEntryUpdated;
397 public Long getReceiptId () {
398 return this.receiptId;
402 public void setReceiptId (final Long receiptId) {
403 this.receiptId = receiptId;
407 @SuppressWarnings ("ReturnOfDateField")
408 public Date getReceiptIssued () {
409 return this.receiptIssued;
413 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
414 public void setReceiptIssued (final Date receiptIssued) {
415 this.receiptIssued = receiptIssued;
419 public String getReceiptNumber () {
420 return this.receiptNumber;
424 public void setReceiptNumber (final String receiptNumber) {
425 this.receiptNumber = receiptNumber;
429 public PaymentType getReceiptPaymentType () {
430 return this.receiptPaymentType;
434 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
435 this.receiptPaymentType = receiptPaymentType;
439 public Long getReceiptRegisterNumber () {
440 return this.receiptRegisterNumber;
444 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
445 this.receiptRegisterNumber = receiptRegisterNumber;
449 public BillableReceipt getReceiptResumptionOf () {
450 return this.receiptResumptionOf;
454 public void setReceiptResumptionOf (final BillableReceipt receiptResumptionOf) {
455 this.receiptResumptionOf = receiptResumptionOf;
459 public Employable getReceiptSellerEmployee () {
460 return this.receiptSellerEmployee;
464 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
465 this.receiptSellerEmployee = receiptSellerEmployee;
469 public Long getReceiptSequenceNumber () {
470 return this.receiptSequenceNumber;
474 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
475 this.receiptSequenceNumber = receiptSequenceNumber;
479 public Long getReceiptTransactionNumber () {
480 return this.receiptTransactionNumber;
484 public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
485 this.receiptTransactionNumber = receiptTransactionNumber;
489 public User getReceiptUser () {
490 return this.receiptUser;
494 public void setReceiptUser (final User receiptUser) {
495 this.receiptUser = receiptUser;
499 public int hashCode () {
502 hash = 89 * hash + Objects.hashCode(this.getReceiptBarCodeNumber());
503 hash = 89 * hash + Objects.hashCode(this.getReceiptBonusCard());
504 hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
505 hash = 89 * hash + Objects.hashCode(this.getReceiptId());
506 hash = 89 * hash + Objects.hashCode(this.getReceiptIssued());
507 hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
508 hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
509 hash = 89 * hash + Objects.hashCode(this.getReceiptRegisterNumber());
510 hash = 89 * hash + Objects.hashCode(this.getReceiptSellerEmployee());
511 hash = 89 * hash + Objects.hashCode(this.getReceiptSequenceNumber());
512 hash = 89 * hash + Objects.hashCode(this.getReceiptTransactionNumber());
513 hash = 89 * hash + Objects.hashCode(this.getReceiptUser());