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 * Selling employee instance
152 @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
153 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
154 private Employable receiptSellerEmployee;
157 * Receipt sequence number
159 @Column (name = "receipt_sequence_number")
160 private Long receiptSequenceNumber;
163 * Receipt transaction number
165 @Column (name = "receipt_transaction_number")
166 private Long receiptTransactionNumber;
169 * Which user this receipt belongs to
171 @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
172 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
173 private User receiptUser;
176 * Default constructor
178 public FinancialReceipt () {
182 * Constructor with payment type, seller (branch office) and user
184 * @param receiptPaymentType Payment type
185 * @param receiptBranchOffice Branch office instance
186 * @param receiptUser User instance
187 * @param receiptIssued When this receipt has been issued
189 * @throws NullPointerException If user instance is not set
190 * @throws IllegalArgumentException If user instance's userId is invalid
192 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final User receiptUser, final Date receiptIssued) {
193 // Call other constructor first
194 this(receiptPaymentType, receiptBranchOffice, receiptIssued);
196 // Validate parameter
197 if (null == receiptUser) {
199 throw new NullPointerException("user is null"); //NOI18N
200 } else if (receiptUser.getUserId() == null) {
202 throw new NullPointerException("user.userId is null"); //NOI18N
203 } else if (receiptUser.getUserId() < 1) {
205 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
209 this.receiptUser = receiptUser;
213 * Constructor with payment type, branch office and when it has been issued
215 * @param receiptPaymentType Payment type
216 * @param receiptBranchOffice Branch office instance
217 * @param receiptIssued When this receipt has been issued
219 * @throws NullPointerException If user instance is not set
220 * @throws IllegalArgumentException If branchId is invalid
222 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
223 // Call other constructor first
226 // Validate all parameter
227 if (null == receiptPaymentType) {
229 throw new NullPointerException("receiptPaymentType is null"); //NOI18N
230 } else if (null == receiptBranchOffice) {
232 throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
233 } else if (receiptBranchOffice.getBranchId() == null) {
235 throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
236 } else if (receiptBranchOffice.getBranchId() < 1) {
238 throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
239 } else if (null == receiptIssued) {
241 throw new NullPointerException("receiptIssued is null"); //NOI18N
245 this.receiptPaymentType = receiptPaymentType;
246 this.receiptBranchOffice = receiptBranchOffice;
247 this.receiptIssued = receiptIssued;
251 public int compareTo (final BillableReceipt billableReceipt) {
252 // Check parameter on null-reference and equality to this
253 if (null == billableReceipt) {
255 throw new NullPointerException("billableReceipt is null"); //NOI18N
256 } else if (billableReceipt.equals(this)) {
262 final int comparators[] = {
263 // First compare receipt number
264 StringUtils.compare(this.getReceiptNumber(), billableReceipt.getReceiptNumber()),
266 StringUtils.compare(this.getReceiptBarCodeNumber(), billableReceipt.getReceiptBarCodeNumber()),
267 // ... sequence number
268 SafeNumberUtils.compare(this.getReceiptSequenceNumber(), billableReceipt.getReceiptSequenceNumber()),
269 // ... transaction number
270 SafeNumberUtils.compare(this.getReceiptTransactionNumber(), billableReceipt.getReceiptTransactionNumber()),
272 this.getReceiptPaymentType().compareTo(billableReceipt.getReceiptPaymentType()),
273 // ... register number
274 SafeNumberUtils.compare(this.getReceiptRegisterNumber(), billableReceipt.getReceiptRegisterNumber()),
276 this.getReceiptIssued().compareTo(billableReceipt.getReceiptIssued()),
277 // ... next is seller instance
278 Employees.compare(this.getReceiptSellerEmployee(), billableReceipt.getReceiptSellerEmployee()),
280 BranchOffices.compare(this.getReceiptBranchOffice(), billableReceipt.getReceiptBranchOffice()),
281 // ... and user instance
282 Users.compare(this.getReceiptUser(), billableReceipt.getReceiptUser())
286 final int comparison = Comparables.checkAll(comparators);
293 public boolean equals (final Object object) {
294 if (this == object) {
296 } else if (null == object) {
298 } else if (this.getClass() != object.getClass()) {
303 final BillableReceipt receipt = (BillableReceipt) object;
305 // Now check some distincting class fields
306 if (!Objects.equals(this.getReceiptBarCodeNumber(), receipt.getReceiptBarCodeNumber())) {
308 } else if (!Objects.equals(this.getReceiptBonusCard(), receipt.getReceiptBonusCard())) {
310 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
312 } else if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
314 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
316 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
318 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
320 } else if (!Objects.equals(this.getReceiptRegisterNumber(), receipt.getReceiptRegisterNumber())) {
322 } else if (!Objects.equals(this.getReceiptSellerEmployee(), receipt.getReceiptSellerEmployee())) {
324 } else if (!Objects.equals(this.getReceiptSequenceNumber(), receipt.getReceiptSequenceNumber())) {
326 } else if (!Objects.equals(this.getReceiptTransactionNumber(), receipt.getReceiptTransactionNumber())) {
328 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
336 public String getReceiptBarCodeNumber () {
337 return this.receiptBarCodeNumber;
341 public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
342 this.receiptBarCodeNumber = receiptBarCodeNumber;
346 public BonusCard getReceiptBonusCard () {
347 return this.receiptBonusCard;
351 public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
352 this.receiptBonusCard = receiptBonusCard;
356 public BranchOffice getReceiptBranchOffice () {
357 return this.receiptBranchOffice;
361 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
362 this.receiptBranchOffice = receiptBranchOffice;
366 @SuppressWarnings ("ReturnOfDateField")
367 public Date getReceiptEntryCreated () {
368 return this.receiptEntryCreated;
372 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
373 public void setReceiptEntryCreated (final Date receiptEntryCreated) {
374 this.receiptEntryCreated = receiptEntryCreated;
378 @SuppressWarnings ("ReturnOfDateField")
379 public Date getReceiptEntryUpdated () {
380 return this.receiptEntryUpdated;
384 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
385 public void setReceiptEntryUpdated (final Date receiptEntryUpdated) {
386 this.receiptEntryUpdated = receiptEntryUpdated;
390 public Long getReceiptId () {
391 return this.receiptId;
395 public void setReceiptId (final Long receiptId) {
396 this.receiptId = receiptId;
400 @SuppressWarnings ("ReturnOfDateField")
401 public Date getReceiptIssued () {
402 return this.receiptIssued;
406 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
407 public void setReceiptIssued (final Date receiptIssued) {
408 this.receiptIssued = receiptIssued;
412 public String getReceiptNumber () {
413 return this.receiptNumber;
417 public void setReceiptNumber (final String receiptNumber) {
418 this.receiptNumber = receiptNumber;
422 public PaymentType getReceiptPaymentType () {
423 return this.receiptPaymentType;
427 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
428 this.receiptPaymentType = receiptPaymentType;
432 public Long getReceiptRegisterNumber () {
433 return this.receiptRegisterNumber;
437 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
438 this.receiptRegisterNumber = receiptRegisterNumber;
442 public Employable getReceiptSellerEmployee () {
443 return this.receiptSellerEmployee;
447 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
448 this.receiptSellerEmployee = receiptSellerEmployee;
452 public Long getReceiptSequenceNumber () {
453 return this.receiptSequenceNumber;
457 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
458 this.receiptSequenceNumber = receiptSequenceNumber;
462 public Long getReceiptTransactionNumber () {
463 return this.receiptTransactionNumber;
467 public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
468 this.receiptTransactionNumber = receiptTransactionNumber;
472 public User getReceiptUser () {
473 return this.receiptUser;
477 public void setReceiptUser (final User receiptUser) {
478 this.receiptUser = receiptUser;
482 public int hashCode () {
485 hash = 89 * hash + Objects.hashCode(this.getReceiptBarCodeNumber());
486 hash = 89 * hash + Objects.hashCode(this.getReceiptBonusCard());
487 hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
488 hash = 89 * hash + Objects.hashCode(this.getReceiptId());
489 hash = 89 * hash + Objects.hashCode(this.getReceiptIssued());
490 hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
491 hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
492 hash = 89 * hash + Objects.hashCode(this.getReceiptRegisterNumber());
493 hash = 89 * hash + Objects.hashCode(this.getReceiptSellerEmployee());
494 hash = 89 * hash + Objects.hashCode(this.getReceiptSequenceNumber());
495 hash = 89 * hash + Objects.hashCode(this.getReceiptTransactionNumber());
496 hash = 89 * hash + Objects.hashCode(this.getReceiptUser());