2 * Copyright (C) 2016, 2017 Roland Häder
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.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
40 import org.mxchange.jcontactsbusiness.model.branchoffice.CompanyBranchOffice;
41 import org.mxchange.jproduct.model.payment.PaymentType;
42 import org.mxchange.jusercore.model.user.LoginUser;
43 import org.mxchange.jusercore.model.user.User;
47 * @author Roland Häder<roland@mxchange.org>
49 @Entity (name = "receipts")
55 @NamedQuery (name = "AllReceipts", query = "SELECT r FROM receipts AS r ORDER BY r.receiptId ASC"),
56 @NamedQuery (name = "SearchAllUserReceipts", query = "SELECT r FROM receipts AS r WHERE r.receiptUser = :receiptUser ORDER BY r.receiptId ASC")
59 @SuppressWarnings ("PersistenceUnitPresent")
60 public class FinancialReceipt implements BillableReceipt {
66 private static final long serialVersionUID = 185_867_217_461L;
71 @JoinColumn (name = "receipt_branch_id", referencedColumnName = "branch_id", nullable = false, updatable = false)
72 @OneToOne (targetEntity = CompanyBranchOffice.class, cascade = CascadeType.REFRESH, optional = false)
73 private BranchOffice receiptBranchOffice;
76 * When this receipt entry has been created
78 @Basic (optional = false)
79 @Temporal (TemporalType.TIMESTAMP)
80 @Column (name = "receipt_created", nullable = false)
81 private Date receiptCreated;
87 @GeneratedValue (strategy = GenerationType.IDENTITY)
88 @Column (name = "receipt_id", nullable = false, updatable = false)
89 private Long receiptId;
92 * When this receipt has been issued
94 @Basic (optional = false)
95 @Temporal (TemporalType.DATE)
96 @Column (name = "receipt_issued", nullable = false)
97 private Date receiptIssued;
102 @Column (name = "receipt_number")
103 private Long receiptNumber;
106 * Payment type (cash, credit card, EC card ...)
108 @Basic (optional = false)
109 @Column (name = "receipt_payment_type", nullable = false)
110 @Enumerated (EnumType.STRING)
111 private PaymentType receiptPaymentType;
114 * Which user this receipt belongs to
116 @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
117 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
118 private User receiptUser;
121 * Default constructor
123 public FinancialReceipt () {
127 * Constructor with payment type, seller (branch office) and user
129 * @param receiptPaymentType Payment type
130 * @param receiptBranchOffice Branch office instance
131 * @param receiptUser User instance
132 * @param receiptIssued When this receipt has been issued
134 * @throws NullPointerException If user instance is not set
135 * @throws IllegalArgumentException If user instance's userId is invalid
137 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final User receiptUser, final Date receiptIssued) {
138 // Call other constructor first
139 this(receiptPaymentType, receiptBranchOffice, receiptIssued);
141 // Validate parameter
142 if (null == receiptUser) {
144 throw new NullPointerException("user is null"); //NOI18N
145 } else if (receiptUser.getUserId() == null) {
147 throw new NullPointerException("user.userId is null"); //NOI18N
148 } else if (receiptUser.getUserId() < 1) {
150 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
154 this.receiptUser = receiptUser;
158 * Constructor with payment type, branch office and when it has been issued
160 * @param receiptPaymentType Payment type
161 * @param receiptBranchOffice Branch office instance
162 * @param receiptIssued When this receipt has been issued
164 * @throws NullPointerException If user instance is not set
165 * @throws IllegalArgumentException If branchId is invalid
167 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
168 // Call other constructor first
171 // Validate all parameter
172 if (null == receiptPaymentType) {
174 throw new NullPointerException("receiptPaymentType is null"); //NOI18N
175 } else if (null == receiptBranchOffice) {
177 throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
178 } else if (receiptBranchOffice.getBranchId() == null) {
180 throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
181 } else if (receiptBranchOffice.getBranchId() < 1) {
183 throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
184 } else if (null == receiptIssued) {
186 throw new NullPointerException("receiptIssued is null"); //NOI18N
190 this.receiptPaymentType = receiptPaymentType;
191 this.receiptBranchOffice = receiptBranchOffice;
192 this.receiptIssued = receiptIssued;
196 public boolean equals (final Object object) {
197 if (this == object) {
200 if (null == object) {
202 } else if (this.getClass() != object.getClass()) {
206 final BillableReceipt receipt = (BillableReceipt) object;
208 if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
210 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
212 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
214 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
216 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
218 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
226 public BranchOffice getReceiptBranchOffice () {
227 return this.receiptBranchOffice;
231 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
232 this.receiptBranchOffice = receiptBranchOffice;
236 @SuppressWarnings ("ReturnOfDateField")
237 public Date getReceiptCreated () {
238 return this.receiptCreated;
242 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
243 public void setReceiptCreated (final Date receiptCreated) {
244 this.receiptCreated = receiptCreated;
248 public Long getReceiptId () {
249 return this.receiptId;
253 public void setReceiptId (final Long receiptId) {
254 this.receiptId = receiptId;
258 @SuppressWarnings ("ReturnOfDateField")
259 public Date getReceiptIssued () {
260 return this.receiptIssued;
264 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
265 public void setReceiptIssued (final Date receiptIssued) {
266 this.receiptIssued = receiptIssued;
270 public Long getReceiptNumber () {
271 return this.receiptNumber;
275 public void setReceiptNumber (final Long receiptNumber) {
276 this.receiptNumber = receiptNumber;
280 public PaymentType getReceiptPaymentType () {
281 return this.receiptPaymentType;
285 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
286 this.receiptPaymentType = receiptPaymentType;
290 public User getReceiptUser () {
291 return this.receiptUser;
295 public void setReceiptUser (final User receiptUser) {
296 this.receiptUser = receiptUser;
300 public int hashCode () {
303 hash = 89 * hash + Objects.hashCode(this.getReceiptId());
304 hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
305 hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
306 hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
307 hash = 89 * hash + Objects.hashCode(this.getReceiptUser());