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.jcontactsbusiness.model.branchoffice.BranchOffice;
41 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
42 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
43 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
44 import org.mxchange.jcontactsbusiness.model.employee.Employable;
45 import org.mxchange.jcontactsbusiness.model.employee.Employees;
46 import org.mxchange.jcoreutils.Comparables;
47 import org.mxchange.jcoreutils.SafeNumberUtils;
48 import org.mxchange.jfinancials.model.bonus_card.BonusCard;
49 import org.mxchange.jfinancials.model.bonus_card.FinancialBonusCard;
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"),
67 @NamedQuery (name = "SearchAllUserReceipts", query = "SELECT r FROM receipts AS r WHERE r.receiptUser = :receiptUser 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 = FinancialBonusCard.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_created", nullable = false)
105 private Date receiptCreated;
111 @GeneratedValue (strategy = GenerationType.IDENTITY)
112 @Column (name = "receipt_id", nullable = false, updatable = false)
113 private Long receiptId;
116 * When this receipt has been issued
118 @Basic (optional = false)
119 @Temporal (TemporalType.TIMESTAMP)
120 @Column (name = "receipt_issued", nullable = false)
121 private Date receiptIssued;
126 @Column (name = "receipt_number")
127 private String receiptNumber;
130 * Payment type (cash, credit card, EC card ...)
132 @Basic (optional = false)
133 @Column (name = "receipt_payment_type", nullable = false)
134 @Enumerated (EnumType.STRING)
135 private PaymentType receiptPaymentType;
138 * Receipt register number
140 @Column (name = "receipt_register_number")
141 private Long receiptRegisterNumber;
144 * Selling employee instance
146 @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
147 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
148 private Employable receiptSellerEmployee;
151 * Receipt sequence number
153 @Column (name = "receipt_sequence_number")
154 private Long receiptSequenceNumber;
157 * Receipt transaction number
159 @Column (name = "receipt_transaction_number")
160 private Long receiptTransactionNumber;
163 * When this receipt entry has been updated
165 @Temporal (TemporalType.TIMESTAMP)
166 @Column (name = "receipt_updated")
167 private Date receiptUpdated;
170 * Which user this receipt belongs to
172 @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
173 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
174 private User receiptUser;
177 * Default constructor
179 public FinancialReceipt () {
183 * Constructor with payment type, seller (branch office) and user
185 * @param receiptPaymentType Payment type
186 * @param receiptBranchOffice Branch office instance
187 * @param receiptUser User instance
188 * @param receiptIssued When this receipt has been issued
190 * @throws NullPointerException If user instance is not set
191 * @throws IllegalArgumentException If user instance's userId is invalid
193 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final User receiptUser, final Date receiptIssued) {
194 // Call other constructor first
195 this(receiptPaymentType, receiptBranchOffice, receiptIssued);
197 // Validate parameter
198 if (null == receiptUser) {
200 throw new NullPointerException("user is null"); //NOI18N
201 } else if (receiptUser.getUserId() == null) {
203 throw new NullPointerException("user.userId is null"); //NOI18N
204 } else if (receiptUser.getUserId() < 1) {
206 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
210 this.receiptUser = receiptUser;
214 * Constructor with payment type, branch office and when it has been issued
216 * @param receiptPaymentType Payment type
217 * @param receiptBranchOffice Branch office instance
218 * @param receiptIssued When this receipt has been issued
220 * @throws NullPointerException If user instance is not set
221 * @throws IllegalArgumentException If branchId is invalid
223 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
224 // Call other constructor first
227 // Validate all parameter
228 if (null == receiptPaymentType) {
230 throw new NullPointerException("receiptPaymentType is null"); //NOI18N
231 } else if (null == receiptBranchOffice) {
233 throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
234 } else if (receiptBranchOffice.getBranchId() == null) {
236 throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
237 } else if (receiptBranchOffice.getBranchId() < 1) {
239 throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
240 } else if (null == receiptIssued) {
242 throw new NullPointerException("receiptIssued is null"); //NOI18N
246 this.receiptPaymentType = receiptPaymentType;
247 this.receiptBranchOffice = receiptBranchOffice;
248 this.receiptIssued = receiptIssued;
252 public int compareTo (final BillableReceipt billableReceipt) {
253 // Check parameter on null-reference and equality to this
254 if (null == billableReceipt) {
256 throw new NullPointerException("billableReceipt is null"); //NOI18N
257 } else if (billableReceipt.equals(this)) {
263 final int comparators[] = {
264 // First compare receipt number
265 StringUtils.compare(this.getReceiptNumber(), billableReceipt.getReceiptNumber()),
267 StringUtils.compare(this.getReceiptBarCodeNumber(), billableReceipt.getReceiptBarCodeNumber()),
268 // ... sequence number
269 SafeNumberUtils.compare(this.getReceiptSequenceNumber(), billableReceipt.getReceiptSequenceNumber()),
270 // ... transaction number
271 SafeNumberUtils.compare(this.getReceiptTransactionNumber(), billableReceipt.getReceiptTransactionNumber()),
273 this.getReceiptPaymentType().compareTo(billableReceipt.getReceiptPaymentType()),
274 // ... register number
275 SafeNumberUtils.compare(this.getReceiptRegisterNumber(), billableReceipt.getReceiptRegisterNumber()),
277 this.getReceiptIssued().compareTo(billableReceipt.getReceiptIssued()),
278 // ... next is seller instance
279 Employees.compare(this.getReceiptSellerEmployee(), billableReceipt.getReceiptSellerEmployee()),
281 BranchOffices.compare(this.getReceiptBranchOffice(), billableReceipt.getReceiptBranchOffice()),
282 // ... and user instance
283 Users.compare(this.getReceiptUser(), billableReceipt.getReceiptUser())
287 final int comparison = Comparables.checkAll(comparators);
294 public boolean equals (final Object object) {
295 if (this == object) {
297 } else if (null == object) {
299 } else if (this.getClass() != object.getClass()) {
304 final BillableReceipt receipt = (BillableReceipt) object;
306 // Now check some distincting class fields
307 if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
309 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
311 } else if (!Objects.equals(this.getReceiptRegisterNumber(), receipt.getReceiptRegisterNumber())) {
313 } else if (!Objects.equals(this.getReceiptSequenceNumber(), receipt.getReceiptSequenceNumber())) {
315 } else if (!Objects.equals(this.getReceiptTransactionNumber(), receipt.getReceiptTransactionNumber())) {
317 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
319 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
321 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
323 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
325 } else if (!Objects.equals(this.getReceiptSellerEmployee(), receipt.getReceiptSellerEmployee())) {
333 public String getReceiptBarCodeNumber () {
334 return this.receiptBarCodeNumber;
338 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
339 this.receiptBarCodeNumber = receiptBarCodeNumber;
343 public BonusCard getReceiptBonusCard () {
344 return this.receiptBonusCard;
348 public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
349 this.receiptBonusCard = receiptBonusCard;
353 public BranchOffice getReceiptBranchOffice () {
354 return this.receiptBranchOffice;
358 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
359 this.receiptBranchOffice = receiptBranchOffice;
363 @SuppressWarnings ("ReturnOfDateField")
364 public Date getReceiptCreated () {
365 return this.receiptCreated;
369 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
370 public void setReceiptCreated (final Date receiptCreated) {
371 this.receiptCreated = receiptCreated;
375 public Long getReceiptId () {
376 return this.receiptId;
380 public void setReceiptId (final Long receiptId) {
381 this.receiptId = receiptId;
385 @SuppressWarnings ("ReturnOfDateField")
386 public Date getReceiptIssued () {
387 return this.receiptIssued;
391 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
392 public void setReceiptIssued (final Date receiptIssued) {
393 this.receiptIssued = receiptIssued;
397 public String getReceiptNumber () {
398 return this.receiptNumber;
402 public void setReceiptNumber (final String receiptNumber) {
403 this.receiptNumber = receiptNumber;
407 public PaymentType getReceiptPaymentType () {
408 return this.receiptPaymentType;
412 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
413 this.receiptPaymentType = receiptPaymentType;
417 public Long getReceiptRegisterNumber () {
418 return this.receiptRegisterNumber;
422 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
423 this.receiptRegisterNumber = receiptRegisterNumber;
427 public Employable getReceiptSellerEmployee () {
428 return this.receiptSellerEmployee;
432 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
433 this.receiptSellerEmployee = receiptSellerEmployee;
437 public Long getReceiptSequenceNumber () {
438 return this.receiptSequenceNumber;
442 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
443 this.receiptSequenceNumber = receiptSequenceNumber;
447 public Long getReceiptTransactionNumber () {
448 return this.receiptTransactionNumber;
452 public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
453 this.receiptTransactionNumber = receiptTransactionNumber;
457 @SuppressWarnings ("ReturnOfDateField")
458 public Date getReceiptUpdated () {
459 return this.receiptUpdated;
463 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
464 public void setReceiptUpdated (final Date receiptUpdated) {
465 this.receiptUpdated = receiptUpdated;
469 public User getReceiptUser () {
470 return this.receiptUser;
474 public void setReceiptUser (final User receiptUser) {
475 this.receiptUser = receiptUser;
479 public int hashCode () {
482 hash = 89 * hash + Objects.hashCode(this.getReceiptId());
483 hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
484 hash = 89 * hash + Objects.hashCode(this.getReceiptRegisterNumber());
485 hash = 89 * hash + Objects.hashCode(this.getReceiptSequenceNumber());
486 hash = 89 * hash + Objects.hashCode(this.getReceiptTransactionNumber());
487 hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
488 hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
489 hash = 89 * hash + Objects.hashCode(this.getReceiptUser());
490 hash = 89 * hash + Objects.hashCode(this.getReceiptSellerEmployee());