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.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;
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;
145 * Receipt register number
147 @Column (name = "receipt_register_number")
148 private Long receiptRegisterNumber;
151 * Receipt resumes an other existing receipt
153 @JoinColumn (name = "receipt_resumption_id", referencedColumnName = "receipt_id")
154 @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH)
155 private BillableReceipt receiptResumptionOf;
158 * Selling employee instance
160 @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
161 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
162 private Employable receiptSellerEmployee;
165 * Receipt sequence number
167 @Column (name = "receipt_sequence_number")
168 private Long receiptSequenceNumber;
171 * Receipt transaction number
173 @Column (name = "receipt_transaction_number")
174 private Long receiptTransactionNumber;
177 * Which user this receipt belongs to
179 @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
180 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
181 private User receiptUser;
187 @Column (name = "receipt_qr_code")
188 private String receiptQrCode;
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()),
288 // ... register number
289 SafeNumberUtils.compare(this.getReceiptRegisterNumber(), billableReceipt.getReceiptRegisterNumber()),
291 this.getReceiptIssued().compareTo(billableReceipt.getReceiptIssued()),
292 // ... next is seller instance
293 Employees.compare(this.getReceiptSellerEmployee(), billableReceipt.getReceiptSellerEmployee()),
295 BranchOffices.compare(this.getReceiptBranchOffice(), billableReceipt.getReceiptBranchOffice()),
296 // ... and user instance
297 Users.compare(this.getReceiptUser(), billableReceipt.getReceiptUser())
301 final int comparison = Comparables.checkAll(comparators);
308 public boolean equals (final Object object) {
309 if (this == object) {
311 } else if (null == object) {
313 } else if (this.getClass() != object.getClass()) {
318 final BillableReceipt receipt = (BillableReceipt) object;
320 // Now check some distincting class fields
321 if (!Objects.equals(this.getReceiptBarCodeNumber(), receipt.getReceiptBarCodeNumber())) {
323 } else if (!Objects.equals(this.getReceiptBonusCard(), receipt.getReceiptBonusCard())) {
325 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
327 } else if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
329 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
331 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
333 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
335 } else if (!Objects.equals(this.getReceiptRegisterNumber(), receipt.getReceiptRegisterNumber())) {
337 } else if (!Objects.equals(this.getReceiptSellerEmployee(), receipt.getReceiptSellerEmployee())) {
339 } else if (!Objects.equals(this.getReceiptSequenceNumber(), receipt.getReceiptSequenceNumber())) {
341 } else if (!Objects.equals(this.getReceiptTransactionNumber(), receipt.getReceiptTransactionNumber())) {
343 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
351 public String getReceiptBarCodeNumber () {
352 return this.receiptBarCodeNumber;
356 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
357 this.receiptBarCodeNumber = receiptBarCodeNumber;
361 public BonusCard getReceiptBonusCard () {
362 return this.receiptBonusCard;
366 public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
367 this.receiptBonusCard = receiptBonusCard;
371 public BranchOffice getReceiptBranchOffice () {
372 return this.receiptBranchOffice;
376 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
377 this.receiptBranchOffice = receiptBranchOffice;
381 @SuppressWarnings ("ReturnOfDateField")
382 public Date getReceiptEntryCreated () {
383 return this.receiptEntryCreated;
387 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
388 public void setReceiptEntryCreated (final Date receiptEntryCreated) {
389 this.receiptEntryCreated = receiptEntryCreated;
393 @SuppressWarnings ("ReturnOfDateField")
394 public Date getReceiptEntryUpdated () {
395 return this.receiptEntryUpdated;
399 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
400 public void setReceiptEntryUpdated (final Date receiptEntryUpdated) {
401 this.receiptEntryUpdated = receiptEntryUpdated;
405 public Long getReceiptId () {
406 return this.receiptId;
410 public void setReceiptId (final Long receiptId) {
411 this.receiptId = receiptId;
415 @SuppressWarnings ("ReturnOfDateField")
416 public Date getReceiptIssued () {
417 return this.receiptIssued;
421 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
422 public void setReceiptIssued (final Date receiptIssued) {
423 this.receiptIssued = receiptIssued;
427 public String getReceiptNumber () {
428 return this.receiptNumber;
432 public void setReceiptNumber (final String receiptNumber) {
433 this.receiptNumber = receiptNumber;
437 public PaymentType getReceiptPaymentType () {
438 return this.receiptPaymentType;
442 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
443 this.receiptPaymentType = receiptPaymentType;
447 public String getReceiptQrCode () {
448 return this.receiptQrCode;
452 public void setReceiptQrCode (final String receiptQrCode) {
453 this.receiptQrCode = receiptQrCode;
457 public Long getReceiptRegisterNumber () {
458 return this.receiptRegisterNumber;
462 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
463 this.receiptRegisterNumber = receiptRegisterNumber;
467 public BillableReceipt getReceiptResumptionOf () {
468 return this.receiptResumptionOf;
472 public void setReceiptResumptionOf (final BillableReceipt receiptResumptionOf) {
473 this.receiptResumptionOf = receiptResumptionOf;
477 public Employable getReceiptSellerEmployee () {
478 return this.receiptSellerEmployee;
482 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
483 this.receiptSellerEmployee = receiptSellerEmployee;
487 public Long getReceiptSequenceNumber () {
488 return this.receiptSequenceNumber;
492 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
493 this.receiptSequenceNumber = receiptSequenceNumber;
497 public Long getReceiptTransactionNumber () {
498 return this.receiptTransactionNumber;
502 public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
503 this.receiptTransactionNumber = receiptTransactionNumber;
507 public User getReceiptUser () {
508 return this.receiptUser;
512 public void setReceiptUser (final User receiptUser) {
513 this.receiptUser = receiptUser;
517 public int hashCode () {
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());