]> 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 - 2020 Free Software Foundation
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.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;
54
55 /**
56  * Receipt POJO
57  * <p>
58  * @author Roland Häder<roland@mxchange.org>
59  */
60 @Entity (name = "receipts")
61 @Table (
62                 name = "receipts"
63 )
64 @NamedQueries (
65                 {
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")
68                 }
69 )
70 @SuppressWarnings ("PersistenceUnitPresent")
71 public class FinancialReceipt implements BillableReceipt {
72
73         /**
74          * Serial number
75          */
76         @Transient
77         private static final long serialVersionUID = 185_867_217_461L;
78
79         /**
80          * Receipt bar-code number
81          */
82         @Column (name = "receipt_barcode_number")
83         private String receiptBarCodeNumber;
84
85         /**
86          * Assigned bonus card
87          */
88         @JoinColumn (name = "receipt_bonus_card_id", referencedColumnName = "bonus_card_id")
89         @OneToOne (targetEntity = FinancialBonusCard.class, cascade = CascadeType.REFRESH)
90         private BonusCard receiptBonusCard;
91
92         /**
93          * Seller instance
94          */
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;
98
99         /**
100          * When this receipt entry has been created
101          */
102         @Basic (optional = false)
103         @Temporal (TemporalType.TIMESTAMP)
104         @Column (name = "receipt_created", nullable = false)
105         private Date receiptCreated;
106
107         /**
108          * Primary key
109          */
110         @Id
111         @GeneratedValue (strategy = GenerationType.IDENTITY)
112         @Column (name = "receipt_id", nullable = false, updatable = false)
113         private Long receiptId;
114
115         /**
116          * When this receipt has been issued
117          */
118         @Basic (optional = false)
119         @Temporal (TemporalType.TIMESTAMP)
120         @Column (name = "receipt_issued", nullable = false)
121         private Date receiptIssued;
122
123         /**
124          * Receipt number
125          */
126         @Column (name = "receipt_number")
127         private String receiptNumber;
128
129         /**
130          * Payment type (cash, credit card, EC card ...)
131          */
132         @Basic (optional = false)
133         @Column (name = "receipt_payment_type", nullable = false)
134         @Enumerated (EnumType.STRING)
135         private PaymentType receiptPaymentType;
136
137         /**
138          * Receipt register number
139          */
140         @Column (name = "receipt_register_number")
141         private Long receiptRegisterNumber;
142
143         /**
144          * Selling employee instance
145          */
146         @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
147         @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
148         private Employable receiptSellerEmployee;
149
150         /**
151          * Receipt sequence number
152          */
153         @Column (name = "receipt_sequence_number")
154         private Long receiptSequenceNumber;
155
156         /**
157          * Receipt transaction number
158          */
159         @Column (name = "receipt_transaction_number")
160         private Long receiptTransactionNumber;
161
162         /**
163          * When this receipt entry has been updated
164          */
165         @Temporal (TemporalType.TIMESTAMP)
166         @Column (name = "receipt_updated")
167         private Date receiptUpdated;
168
169         /**
170          * Which user this receipt belongs to
171          */
172         @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
173         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
174         private User receiptUser;
175
176         /**
177          * Default constructor
178          */
179         public FinancialReceipt () {
180         }
181
182         /**
183          * Constructor with payment type, seller (branch office) and user
184          * <p>
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
189          * <p>
190          * @throws NullPointerException If user instance is not set
191          * @throws IllegalArgumentException If user instance's userId is invalid
192          */
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);
196
197                 // Validate parameter
198                 if (null == receiptUser) {
199                         // Throw NPE
200                         throw new NullPointerException("user is null"); //NOI18N
201                 } else if (receiptUser.getUserId() == null) {
202                         // Throw it again
203                         throw new NullPointerException("user.userId is null"); //NOI18N
204                 } else if (receiptUser.getUserId() < 1) {
205                         // Throw IAE
206                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
207                 }
208
209                 // Set user instance
210                 this.receiptUser = receiptUser;
211         }
212
213         /**
214          * Constructor with payment type, branch office and when it has been issued
215          * <p>
216          * @param receiptPaymentType  Payment type
217          * @param receiptBranchOffice Branch office instance
218          * @param receiptIssued       When this receipt has been issued
219          * <p>
220          * @throws NullPointerException If user instance is not set
221          * @throws IllegalArgumentException If branchId is invalid
222          */
223         public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
224                 // Call other constructor first
225                 this();
226
227                 // Validate all parameter
228                 if (null == receiptPaymentType) {
229                         // Throw NPE
230                         throw new NullPointerException("receiptPaymentType is null"); //NOI18N
231                 } else if (null == receiptBranchOffice) {
232                         // Throw NPE
233                         throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
234                 } else if (receiptBranchOffice.getBranchId() == null) {
235                         // Throw NPE
236                         throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
237                 } else if (receiptBranchOffice.getBranchId() < 1) {
238                         // Throw IAE
239                         throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
240                 } else if (null == receiptIssued) {
241                         // Throw NPE
242                         throw new NullPointerException("receiptIssued is null"); //NOI18N
243                 }
244
245                 // Set all values
246                 this.receiptPaymentType = receiptPaymentType;
247                 this.receiptBranchOffice = receiptBranchOffice;
248                 this.receiptIssued = receiptIssued;
249         }
250
251         @Override
252         public int compareTo (final BillableReceipt billableReceipt) {
253                 // Check parameter on null-reference and equality to this
254                 if (null == billableReceipt) {
255                         // Should not happen
256                         throw new NullPointerException("billableReceipt is null"); //NOI18N
257                 } else if (billableReceipt.equals(this)) {
258                         // Same object
259                         return 0;
260                 }
261
262                 // Init comparators
263                 final int comparators[] = {
264                         // First compare receipt number
265                         StringUtils.compare(this.getReceiptNumber(), billableReceipt.getReceiptNumber()),
266                         // ... next bar-code
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()),
272                         // ... payment type
273                         this.getReceiptPaymentType().compareTo(billableReceipt.getReceiptPaymentType()),
274                         // ... register number
275                         SafeNumberUtils.compare(this.getReceiptRegisterNumber(), billableReceipt.getReceiptRegisterNumber()),
276                         // ... issue date
277                         this.getReceiptIssued().compareTo(billableReceipt.getReceiptIssued()),
278                         // ... next is seller instance
279                         Employees.compare(this.getReceiptSellerEmployee(), billableReceipt.getReceiptSellerEmployee()),
280                         // .. branch office
281                         BranchOffices.compare(this.getReceiptBranchOffice(), billableReceipt.getReceiptBranchOffice()),
282                         // ... and user instance
283                         Users.compare(this.getReceiptUser(), billableReceipt.getReceiptUser())
284                 };
285
286                 // Check all values
287                 final int comparison = Comparables.checkAll(comparators);
288
289                 // Return value
290                 return comparison;
291         }
292
293         @Override
294         public boolean equals (final Object object) {
295                 if (this == object) {
296                         return true;
297                 } else if (null == object) {
298                         return false;
299                 } else if (this.getClass() != object.getClass()) {
300                         return false;
301                 }
302
303                 // Cast securely
304                 final BillableReceipt receipt = (BillableReceipt) object;
305
306                 // Now check some distincting class fields
307                 if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
308                         return false;
309                 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
310                         return false;
311                 } else if (!Objects.equals(this.getReceiptRegisterNumber(), receipt.getReceiptRegisterNumber())) {
312                         return false;
313                 } else if (!Objects.equals(this.getReceiptSequenceNumber(), receipt.getReceiptSequenceNumber())) {
314                         return false;
315                 } else if (!Objects.equals(this.getReceiptTransactionNumber(), receipt.getReceiptTransactionNumber())) {
316                         return false;
317                 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
318                         return false;
319                 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
320                         return false;
321                 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
322                         return false;
323                 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
324                         return false;
325                 } else if (!Objects.equals(this.getReceiptSellerEmployee(), receipt.getReceiptSellerEmployee())) {
326                         return false;
327                 }
328
329                 return true;
330         }
331
332         @Override
333         public String getReceiptBarCodeNumber () {
334                 return this.receiptBarCodeNumber;
335         }
336
337         @Override
338         public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
339                 this.receiptBarCodeNumber = receiptBarCodeNumber;
340         }
341
342         @Override
343         public BonusCard getReceiptBonusCard () {
344                 return this.receiptBonusCard;
345         }
346
347         @Override
348         public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
349                 this.receiptBonusCard = receiptBonusCard;
350         }
351
352         @Override
353         public BranchOffice getReceiptBranchOffice () {
354                 return this.receiptBranchOffice;
355         }
356
357         @Override
358         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
359                 this.receiptBranchOffice = receiptBranchOffice;
360         }
361
362         @Override
363         @SuppressWarnings ("ReturnOfDateField")
364         public Date getReceiptCreated () {
365                 return this.receiptCreated;
366         }
367
368         @Override
369         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
370         public void setReceiptCreated (final Date receiptCreated) {
371                 this.receiptCreated = receiptCreated;
372         }
373
374         @Override
375         public Long getReceiptId () {
376                 return this.receiptId;
377         }
378
379         @Override
380         public void setReceiptId (final Long receiptId) {
381                 this.receiptId = receiptId;
382         }
383
384         @Override
385         @SuppressWarnings ("ReturnOfDateField")
386         public Date getReceiptIssued () {
387                 return this.receiptIssued;
388         }
389
390         @Override
391         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
392         public void setReceiptIssued (final Date receiptIssued) {
393                 this.receiptIssued = receiptIssued;
394         }
395
396         @Override
397         public String getReceiptNumber () {
398                 return this.receiptNumber;
399         }
400
401         @Override
402         public void setReceiptNumber (final String receiptNumber) {
403                 this.receiptNumber = receiptNumber;
404         }
405
406         @Override
407         public PaymentType getReceiptPaymentType () {
408                 return this.receiptPaymentType;
409         }
410
411         @Override
412         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
413                 this.receiptPaymentType = receiptPaymentType;
414         }
415
416         @Override
417         public Long getReceiptRegisterNumber () {
418                 return this.receiptRegisterNumber;
419         }
420
421         @Override
422         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
423                 this.receiptRegisterNumber = receiptRegisterNumber;
424         }
425
426         @Override
427         public Employable getReceiptSellerEmployee () {
428                 return this.receiptSellerEmployee;
429         }
430
431         @Override
432         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
433                 this.receiptSellerEmployee = receiptSellerEmployee;
434         }
435
436         @Override
437         public Long getReceiptSequenceNumber () {
438                 return this.receiptSequenceNumber;
439         }
440
441         @Override
442         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
443                 this.receiptSequenceNumber = receiptSequenceNumber;
444         }
445
446         @Override
447         public Long getReceiptTransactionNumber () {
448                 return this.receiptTransactionNumber;
449         }
450
451         @Override
452         public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
453                 this.receiptTransactionNumber = receiptTransactionNumber;
454         }
455
456         @Override
457         @SuppressWarnings ("ReturnOfDateField")
458         public Date getReceiptUpdated () {
459                 return this.receiptUpdated;
460         }
461
462         @Override
463         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
464         public void setReceiptUpdated (final Date receiptUpdated) {
465                 this.receiptUpdated = receiptUpdated;
466         }
467
468         @Override
469         public User getReceiptUser () {
470                 return this.receiptUser;
471         }
472
473         @Override
474         public void setReceiptUser (final User receiptUser) {
475                 this.receiptUser = receiptUser;
476         }
477
478         @Override
479         public int hashCode () {
480                 int hash = 5;
481
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());
491
492                 return hash;
493         }
494
495 }