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