]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/receipt/FinancialReceipt.java
Continued:
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / receipt / FinancialReceipt.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.model.receipt;
18
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;
46
47 /**
48  *
49  * @author Roland Häder<roland@mxchange.org>
50  */
51 @Entity (name = "receipts")
52 @Table (
53                 name = "receipts"
54 )
55 @NamedQueries (
56                 {
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")
59                 }
60 )
61 @SuppressWarnings ("PersistenceUnitPresent")
62 public class FinancialReceipt implements BillableReceipt {
63
64         /**
65          * Serial number
66          */
67         @Transient
68         private static final long serialVersionUID = 185_867_217_461L;
69
70         /**
71          * Receipt bar-code number
72          */
73         @Column (name = "receipt_barcode_number")
74         private Long receiptBarCodeNumber;
75
76         /**
77          * Seller instance
78          */
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;
82
83         /**
84          * When this receipt entry has been created
85          */
86         @Basic (optional = false)
87         @Temporal (TemporalType.TIMESTAMP)
88         @Column (name = "receipt_created", nullable = false)
89         private Date receiptCreated;
90
91         /**
92          * Primary key
93          */
94         @Id
95         @GeneratedValue (strategy = GenerationType.IDENTITY)
96         @Column (name = "receipt_id", nullable = false, updatable = false)
97         private Long receiptId;
98
99         /**
100          * When this receipt has been issued
101          */
102         @Basic (optional = false)
103         @Temporal (TemporalType.TIMESTAMP)
104         @Column (name = "receipt_issued", nullable = false)
105         private Date receiptIssued;
106
107         /**
108          * Receipt number
109          */
110         @Column (name = "receipt_number")
111         private Long receiptNumber;
112
113         /**
114          * Payment type (cash, credit card, EC card ...)
115          */
116         @Basic (optional = false)
117         @Column (name = "receipt_payment_type", nullable = false)
118         @Enumerated (EnumType.STRING)
119         private PaymentType receiptPaymentType;
120
121         /**
122          * Receipt register number
123          */
124         @Column (name = "receipt_register_number")
125         private Long receiptRegisterNumber;
126
127         /**
128          * Selling employee instance
129          */
130         @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
131         @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
132         private Employable receiptSellerEmployee;
133
134         /**
135          * Receipt sequence number
136          */
137         @Column (name = "receipt_sequence_number")
138         private Long receiptSequenceNumber;
139
140         /**
141          * Which user this receipt belongs to
142          */
143         @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
144         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
145         private User receiptUser;
146
147         /**
148          * Default constructor
149          */
150         public FinancialReceipt () {
151         }
152
153         /**
154          * Constructor with payment type, seller (branch office) and user
155          * <p>
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
160          * <p>
161          * @throws NullPointerException If user instance is not set
162          * @throws IllegalArgumentException If user instance's userId is invalid
163          */
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);
167
168                 // Validate parameter
169                 if (null == receiptUser) {
170                         // Throw NPE
171                         throw new NullPointerException("user is null"); //NOI18N
172                 } else if (receiptUser.getUserId() == null) {
173                         // Throw it again
174                         throw new NullPointerException("user.userId is null"); //NOI18N
175                 } else if (receiptUser.getUserId() < 1) {
176                         // Throw IAE
177                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
178                 }
179
180                 // Set user instance
181                 this.receiptUser = receiptUser;
182         }
183
184         /**
185          * Constructor with payment type, branch office and when it has been issued
186          * <p>
187          * @param receiptPaymentType  Payment type
188          * @param receiptBranchOffice Branch office instance
189          * @param receiptIssued       When this receipt has been issued
190          * <p>
191          * @throws NullPointerException If user instance is not set
192          * @throws IllegalArgumentException If branchId is invalid
193          */
194         public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
195                 // Call other constructor first
196                 this();
197
198                 // Validate all parameter
199                 if (null == receiptPaymentType) {
200                         // Throw NPE
201                         throw new NullPointerException("receiptPaymentType is null"); //NOI18N
202                 } else if (null == receiptBranchOffice) {
203                         // Throw NPE
204                         throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
205                 } else if (receiptBranchOffice.getBranchId() == null) {
206                         // Throw NPE
207                         throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
208                 } else if (receiptBranchOffice.getBranchId() < 1) {
209                         // Throw IAE
210                         throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
211                 } else if (null == receiptIssued) {
212                         // Throw NPE
213                         throw new NullPointerException("receiptIssued is null"); //NOI18N
214                 }
215
216                 // Set all values
217                 this.receiptPaymentType = receiptPaymentType;
218                 this.receiptBranchOffice = receiptBranchOffice;
219                 this.receiptIssued = receiptIssued;
220         }
221
222         @Override
223         public boolean equals (final Object object) {
224                 if (this == object) {
225                         return true;
226                 } else if (null == object) {
227                         return false;
228                 } else if (this.getClass() != object.getClass()) {
229                         return false;
230                 }
231
232                 // Cast securely
233                 final BillableReceipt receipt = (BillableReceipt) object;
234
235                 // Now check some distincting class fields
236                 if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
237                         return false;
238                 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
239                         return false;
240                 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
241                         return false;
242                 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
243                         return false;
244                 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
245                         return false;
246                 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
247                         return false;
248                 }
249
250                 return true;
251         }
252
253         @Override
254         public Long getReceiptBarCodeNumber () {
255                 return this.receiptBarCodeNumber;
256         }
257
258         @Override
259         public void setReceiptBarCodeNumber (final Long receiptBarCodeNumber) {
260                 this.receiptBarCodeNumber = receiptBarCodeNumber;
261         }
262
263         @Override
264         public BranchOffice getReceiptBranchOffice () {
265                 return this.receiptBranchOffice;
266         }
267
268         @Override
269         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
270                 this.receiptBranchOffice = receiptBranchOffice;
271         }
272
273         @Override
274         @SuppressWarnings ("ReturnOfDateField")
275         public Date getReceiptCreated () {
276                 return this.receiptCreated;
277         }
278
279         @Override
280         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
281         public void setReceiptCreated (final Date receiptCreated) {
282                 this.receiptCreated = receiptCreated;
283         }
284
285         @Override
286         public Long getReceiptId () {
287                 return this.receiptId;
288         }
289
290         @Override
291         public void setReceiptId (final Long receiptId) {
292                 this.receiptId = receiptId;
293         }
294
295         @Override
296         @SuppressWarnings ("ReturnOfDateField")
297         public Date getReceiptIssued () {
298                 return this.receiptIssued;
299         }
300
301         @Override
302         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
303         public void setReceiptIssued (final Date receiptIssued) {
304                 this.receiptIssued = receiptIssued;
305         }
306
307         @Override
308         public Long getReceiptNumber () {
309                 return this.receiptNumber;
310         }
311
312         @Override
313         public void setReceiptNumber (final Long receiptNumber) {
314                 this.receiptNumber = receiptNumber;
315         }
316
317         @Override
318         public PaymentType getReceiptPaymentType () {
319                 return this.receiptPaymentType;
320         }
321
322         @Override
323         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
324                 this.receiptPaymentType = receiptPaymentType;
325         }
326
327         @Override
328         public Long getReceiptRegisterNumber () {
329                 return this.receiptRegisterNumber;
330         }
331
332         @Override
333         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
334                 this.receiptRegisterNumber = receiptRegisterNumber;
335         }
336
337         @Override
338         public Employable getReceiptSellerEmployee () {
339                 return this.receiptSellerEmployee;
340         }
341
342         @Override
343         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
344                 this.receiptSellerEmployee = receiptSellerEmployee;
345         }
346
347         @Override
348         public Long getReceiptSequenceNumber () {
349                 return this.receiptSequenceNumber;
350         }
351
352         @Override
353         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
354                 this.receiptSequenceNumber = receiptSequenceNumber;
355         }
356
357         @Override
358         public User getReceiptUser () {
359                 return this.receiptUser;
360         }
361
362         @Override
363         public void setReceiptUser (final User receiptUser) {
364                 this.receiptUser = receiptUser;
365         }
366
367         @Override
368         public int hashCode () {
369                 int hash = 5;
370
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());
376
377                 return hash;
378         }
379
380 }