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.BusinessBranchOffice;
41 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
42 import org.mxchange.jcontactsbusiness.model.employee.Employable;
43 import org.mxchange.jproduct.model.payment.PaymentType;
44 import org.mxchange.jusercore.model.user.LoginUser;
45 import org.mxchange.jusercore.model.user.User;
49 * @author Roland Häder<roland@mxchange.org>
51 @Entity (name = "receipts")
57 @NamedQuery (name = "AllReceipts", query = "SELECT r FROM receipts AS r ORDER BY r.receiptId ASC"),
58 @NamedQuery (name = "SearchAllUserReceipts", query = "SELECT r FROM receipts AS r WHERE r.receiptUser = :receiptUser ORDER BY r.receiptId ASC")
61 @SuppressWarnings ("PersistenceUnitPresent")
62 public class FinancialReceipt implements BillableReceipt {
68 private static final long serialVersionUID = 185_867_217_461L;
71 * Receipt bar-code number
73 @Column (name = "receipt_barcode_number")
74 private Long receiptBarCodeNumber;
79 @JoinColumn (name = "receipt_branch_id", referencedColumnName = "branch_id", nullable = false, updatable = false)
80 @OneToOne (targetEntity = BusinessBranchOffice.class, cascade = CascadeType.REFRESH, optional = false)
81 private BranchOffice receiptBranchOffice;
84 * When this receipt entry has been created
86 @Basic (optional = false)
87 @Temporal (TemporalType.TIMESTAMP)
88 @Column (name = "receipt_created", nullable = false)
89 private Date receiptCreated;
95 @GeneratedValue (strategy = GenerationType.IDENTITY)
96 @Column (name = "receipt_id", nullable = false, updatable = false)
97 private Long receiptId;
100 * When this receipt has been issued
102 @Basic (optional = false)
103 @Temporal (TemporalType.TIMESTAMP)
104 @Column (name = "receipt_issued", nullable = false)
105 private Date receiptIssued;
110 @Column (name = "receipt_number")
111 private Long receiptNumber;
114 * Payment type (cash, credit card, EC card ...)
116 @Basic (optional = false)
117 @Column (name = "receipt_payment_type", nullable = false)
118 @Enumerated (EnumType.STRING)
119 private PaymentType receiptPaymentType;
122 * Receipt register number
124 @Column (name = "receipt_register_number")
125 private Long receiptRegisterNumber;
128 * Selling employee instance
130 @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
131 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
132 private Employable receiptSellerEmployee;
135 * Receipt sequence number
137 @Column (name = "receipt_sequence_number")
138 private Long receiptSequenceNumber;
141 * Which user this receipt belongs to
143 @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
144 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
145 private User receiptUser;
148 * Default constructor
150 public FinancialReceipt () {
154 * Constructor with payment type, seller (branch office) and user
156 * @param receiptPaymentType Payment type
157 * @param receiptBranchOffice Branch office instance
158 * @param receiptUser User instance
159 * @param receiptIssued When this receipt has been issued
161 * @throws NullPointerException If user instance is not set
162 * @throws IllegalArgumentException If user instance's userId is invalid
164 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final User receiptUser, final Date receiptIssued) {
165 // Call other constructor first
166 this(receiptPaymentType, receiptBranchOffice, receiptIssued);
168 // Validate parameter
169 if (null == receiptUser) {
171 throw new NullPointerException("user is null"); //NOI18N
172 } else if (receiptUser.getUserId() == null) {
174 throw new NullPointerException("user.userId is null"); //NOI18N
175 } else if (receiptUser.getUserId() < 1) {
177 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
181 this.receiptUser = receiptUser;
185 * Constructor with payment type, branch office and when it has been issued
187 * @param receiptPaymentType Payment type
188 * @param receiptBranchOffice Branch office instance
189 * @param receiptIssued When this receipt has been issued
191 * @throws NullPointerException If user instance is not set
192 * @throws IllegalArgumentException If branchId is invalid
194 public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
195 // Call other constructor first
198 // Validate all parameter
199 if (null == receiptPaymentType) {
201 throw new NullPointerException("receiptPaymentType is null"); //NOI18N
202 } else if (null == receiptBranchOffice) {
204 throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
205 } else if (receiptBranchOffice.getBranchId() == null) {
207 throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
208 } else if (receiptBranchOffice.getBranchId() < 1) {
210 throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
211 } else if (null == receiptIssued) {
213 throw new NullPointerException("receiptIssued is null"); //NOI18N
217 this.receiptPaymentType = receiptPaymentType;
218 this.receiptBranchOffice = receiptBranchOffice;
219 this.receiptIssued = receiptIssued;
223 public boolean equals (final Object object) {
224 if (this == object) {
226 } else if (null == object) {
228 } else if (this.getClass() != object.getClass()) {
233 final BillableReceipt receipt = (BillableReceipt) object;
235 // Now check some distincting class fields
236 if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
238 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
240 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
242 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
244 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
246 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
254 public Long getReceiptBarCodeNumber () {
255 return this.receiptBarCodeNumber;
259 public void setReceiptBarCodeNumber (final Long receiptBarCodeNumber) {
260 this.receiptBarCodeNumber = receiptBarCodeNumber;
264 public BranchOffice getReceiptBranchOffice () {
265 return this.receiptBranchOffice;
269 public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
270 this.receiptBranchOffice = receiptBranchOffice;
274 @SuppressWarnings ("ReturnOfDateField")
275 public Date getReceiptCreated () {
276 return this.receiptCreated;
280 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
281 public void setReceiptCreated (final Date receiptCreated) {
282 this.receiptCreated = receiptCreated;
286 public Long getReceiptId () {
287 return this.receiptId;
291 public void setReceiptId (final Long receiptId) {
292 this.receiptId = receiptId;
296 @SuppressWarnings ("ReturnOfDateField")
297 public Date getReceiptIssued () {
298 return this.receiptIssued;
302 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
303 public void setReceiptIssued (final Date receiptIssued) {
304 this.receiptIssued = receiptIssued;
308 public Long getReceiptNumber () {
309 return this.receiptNumber;
313 public void setReceiptNumber (final Long receiptNumber) {
314 this.receiptNumber = receiptNumber;
318 public PaymentType getReceiptPaymentType () {
319 return this.receiptPaymentType;
323 public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
324 this.receiptPaymentType = receiptPaymentType;
328 public Long getReceiptRegisterNumber () {
329 return this.receiptRegisterNumber;
333 public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
334 this.receiptRegisterNumber = receiptRegisterNumber;
338 public Employable getReceiptSellerEmployee () {
339 return this.receiptSellerEmployee;
343 public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
344 this.receiptSellerEmployee = receiptSellerEmployee;
348 public Long getReceiptSequenceNumber () {
349 return this.receiptSequenceNumber;
353 public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
354 this.receiptSequenceNumber = receiptSequenceNumber;
358 public User getReceiptUser () {
359 return this.receiptUser;
363 public void setReceiptUser (final User receiptUser) {
364 this.receiptUser = receiptUser;
368 public int hashCode () {
371 hash = 89 * hash + Objects.hashCode(this.getReceiptId());
372 hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
373 hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
374 hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
375 hash = 89 * hash + Objects.hashCode(this.getReceiptUser());