]> 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          * Selling employee instance
151          */
152         @JoinColumn (name = "receipt_seller_id", referencedColumnName = "employee_id")
153         @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
154         private Employable receiptSellerEmployee;
155
156         /**
157          * Receipt sequence number
158          */
159         @Column (name = "receipt_sequence_number")
160         private Long receiptSequenceNumber;
161
162         /**
163          * Receipt transaction number
164          */
165         @Column (name = "receipt_transaction_number")
166         private Long receiptTransactionNumber;
167
168         /**
169          * Which user this receipt belongs to
170          */
171         @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
172         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
173         private User receiptUser;
174
175         /**
176          * Default constructor
177          */
178         public FinancialReceipt () {
179         }
180
181         /**
182          * Constructor with payment type, seller (branch office) and user
183          * <p>
184          * @param receiptPaymentType  Payment type
185          * @param receiptBranchOffice Branch office instance
186          * @param receiptUser         User instance
187          * @param receiptIssued       When this receipt has been issued
188          * <p>
189          * @throws NullPointerException If user instance is not set
190          * @throws IllegalArgumentException If user instance's userId is invalid
191          */
192         public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final User receiptUser, final Date receiptIssued) {
193                 // Call other constructor first
194                 this(receiptPaymentType, receiptBranchOffice, receiptIssued);
195
196                 // Validate parameter
197                 if (null == receiptUser) {
198                         // Throw NPE
199                         throw new NullPointerException("user is null"); //NOI18N
200                 } else if (receiptUser.getUserId() == null) {
201                         // Throw it again
202                         throw new NullPointerException("user.userId is null"); //NOI18N
203                 } else if (receiptUser.getUserId() < 1) {
204                         // Throw IAE
205                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
206                 }
207
208                 // Set user instance
209                 this.receiptUser = receiptUser;
210         }
211
212         /**
213          * Constructor with payment type, branch office and when it has been issued
214          * <p>
215          * @param receiptPaymentType  Payment type
216          * @param receiptBranchOffice Branch office instance
217          * @param receiptIssued       When this receipt has been issued
218          * <p>
219          * @throws NullPointerException If user instance is not set
220          * @throws IllegalArgumentException If branchId is invalid
221          */
222         public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
223                 // Call other constructor first
224                 this();
225
226                 // Validate all parameter
227                 if (null == receiptPaymentType) {
228                         // Throw NPE
229                         throw new NullPointerException("receiptPaymentType is null"); //NOI18N
230                 } else if (null == receiptBranchOffice) {
231                         // Throw NPE
232                         throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
233                 } else if (receiptBranchOffice.getBranchId() == null) {
234                         // Throw NPE
235                         throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
236                 } else if (receiptBranchOffice.getBranchId() < 1) {
237                         // Throw IAE
238                         throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
239                 } else if (null == receiptIssued) {
240                         // Throw NPE
241                         throw new NullPointerException("receiptIssued is null"); //NOI18N
242                 }
243
244                 // Set all values
245                 this.receiptPaymentType = receiptPaymentType;
246                 this.receiptBranchOffice = receiptBranchOffice;
247                 this.receiptIssued = receiptIssued;
248         }
249
250         @Override
251         public int compareTo (final BillableReceipt billableReceipt) {
252                 // Check parameter on null-reference and equality to this
253                 if (null == billableReceipt) {
254                         // Should not happen
255                         throw new NullPointerException("billableReceipt is null"); //NOI18N
256                 } else if (billableReceipt.equals(this)) {
257                         // Same object
258                         return 0;
259                 }
260
261                 // Init comparators
262                 final int comparators[] = {
263                         // First compare receipt number
264                         StringUtils.compare(this.getReceiptNumber(), billableReceipt.getReceiptNumber()),
265                         // ... next bar-code
266                         StringUtils.compare(this.getReceiptBarCodeNumber(), billableReceipt.getReceiptBarCodeNumber()),
267                         // ... sequence number
268                         SafeNumberUtils.compare(this.getReceiptSequenceNumber(), billableReceipt.getReceiptSequenceNumber()),
269                         // ... transaction number
270                         SafeNumberUtils.compare(this.getReceiptTransactionNumber(), billableReceipt.getReceiptTransactionNumber()),
271                         // ... payment type
272                         this.getReceiptPaymentType().compareTo(billableReceipt.getReceiptPaymentType()),
273                         // ... register number
274                         SafeNumberUtils.compare(this.getReceiptRegisterNumber(), billableReceipt.getReceiptRegisterNumber()),
275                         // ... issue date
276                         this.getReceiptIssued().compareTo(billableReceipt.getReceiptIssued()),
277                         // ... next is seller instance
278                         Employees.compare(this.getReceiptSellerEmployee(), billableReceipt.getReceiptSellerEmployee()),
279                         // .. branch office
280                         BranchOffices.compare(this.getReceiptBranchOffice(), billableReceipt.getReceiptBranchOffice()),
281                         // ... and user instance
282                         Users.compare(this.getReceiptUser(), billableReceipt.getReceiptUser())
283                 };
284
285                 // Check all values
286                 final int comparison = Comparables.checkAll(comparators);
287
288                 // Return value
289                 return comparison;
290         }
291
292         @Override
293         public boolean equals (final Object object) {
294                 if (this == object) {
295                         return true;
296                 } else if (null == object) {
297                         return false;
298                 } else if (this.getClass() != object.getClass()) {
299                         return false;
300                 }
301
302                 // Cast securely
303                 final BillableReceipt receipt = (BillableReceipt) object;
304
305                 // Now check some distincting class fields
306                 if (!Objects.equals(this.getReceiptBarCodeNumber(), receipt.getReceiptBarCodeNumber())) {
307                         return false;
308                 } else if (!Objects.equals(this.getReceiptBonusCard(), receipt.getReceiptBonusCard())) {
309                         return false;
310                 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
311                         return false;
312                 } else if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
313                         return false;
314                 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
315                         return false;
316                 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
317                         return false;
318                 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
319                         return false;
320                 } else if (!Objects.equals(this.getReceiptRegisterNumber(), receipt.getReceiptRegisterNumber())) {
321                         return false;
322                 } else if (!Objects.equals(this.getReceiptSellerEmployee(), receipt.getReceiptSellerEmployee())) {
323                         return false;
324                 } else if (!Objects.equals(this.getReceiptSequenceNumber(), receipt.getReceiptSequenceNumber())) {
325                         return false;
326                 } else if (!Objects.equals(this.getReceiptTransactionNumber(), receipt.getReceiptTransactionNumber())) {
327                         return false;
328                 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
329                         return false;
330                 }
331
332                 return true;
333         }
334
335         @Override
336         public String getReceiptBarCodeNumber () {
337                 return this.receiptBarCodeNumber;
338         }
339
340         @Override
341         public void setReceiptBarCodeNumber (final String receiptBarCodeNumber) {
342                 this.receiptBarCodeNumber = receiptBarCodeNumber;
343         }
344
345         @Override
346         public BonusCard getReceiptBonusCard () {
347                 return this.receiptBonusCard;
348         }
349
350         @Override
351         public void setReceiptBonusCard (final BonusCard receiptBonusCard) {
352                 this.receiptBonusCard = receiptBonusCard;
353         }
354
355         @Override
356         public BranchOffice getReceiptBranchOffice () {
357                 return this.receiptBranchOffice;
358         }
359
360         @Override
361         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
362                 this.receiptBranchOffice = receiptBranchOffice;
363         }
364
365         @Override
366         @SuppressWarnings ("ReturnOfDateField")
367         public Date getReceiptEntryCreated () {
368                 return this.receiptEntryCreated;
369         }
370
371         @Override
372         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
373         public void setReceiptEntryCreated (final Date receiptEntryCreated) {
374                 this.receiptEntryCreated = receiptEntryCreated;
375         }
376
377         @Override
378         @SuppressWarnings ("ReturnOfDateField")
379         public Date getReceiptEntryUpdated () {
380                 return this.receiptEntryUpdated;
381         }
382
383         @Override
384         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
385         public void setReceiptEntryUpdated (final Date receiptEntryUpdated) {
386                 this.receiptEntryUpdated = receiptEntryUpdated;
387         }
388
389         @Override
390         public Long getReceiptId () {
391                 return this.receiptId;
392         }
393
394         @Override
395         public void setReceiptId (final Long receiptId) {
396                 this.receiptId = receiptId;
397         }
398
399         @Override
400         @SuppressWarnings ("ReturnOfDateField")
401         public Date getReceiptIssued () {
402                 return this.receiptIssued;
403         }
404
405         @Override
406         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
407         public void setReceiptIssued (final Date receiptIssued) {
408                 this.receiptIssued = receiptIssued;
409         }
410
411         @Override
412         public String getReceiptNumber () {
413                 return this.receiptNumber;
414         }
415
416         @Override
417         public void setReceiptNumber (final String receiptNumber) {
418                 this.receiptNumber = receiptNumber;
419         }
420
421         @Override
422         public PaymentType getReceiptPaymentType () {
423                 return this.receiptPaymentType;
424         }
425
426         @Override
427         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
428                 this.receiptPaymentType = receiptPaymentType;
429         }
430
431         @Override
432         public Long getReceiptRegisterNumber () {
433                 return this.receiptRegisterNumber;
434         }
435
436         @Override
437         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
438                 this.receiptRegisterNumber = receiptRegisterNumber;
439         }
440
441         @Override
442         public Employable getReceiptSellerEmployee () {
443                 return this.receiptSellerEmployee;
444         }
445
446         @Override
447         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
448                 this.receiptSellerEmployee = receiptSellerEmployee;
449         }
450
451         @Override
452         public Long getReceiptSequenceNumber () {
453                 return this.receiptSequenceNumber;
454         }
455
456         @Override
457         public void setReceiptSequenceNumber (final Long receiptSequenceNumber) {
458                 this.receiptSequenceNumber = receiptSequenceNumber;
459         }
460
461         @Override
462         public Long getReceiptTransactionNumber () {
463                 return this.receiptTransactionNumber;
464         }
465
466         @Override
467         public void setReceiptTransactionNumber (final Long receiptTransactionNumber) {
468                 this.receiptTransactionNumber = receiptTransactionNumber;
469         }
470
471         @Override
472         public User getReceiptUser () {
473                 return this.receiptUser;
474         }
475
476         @Override
477         public void setReceiptUser (final User receiptUser) {
478                 this.receiptUser = receiptUser;
479         }
480
481         @Override
482         public int hashCode () {
483                 int hash = 5;
484
485                 hash = 89 * hash + Objects.hashCode(this.getReceiptBarCodeNumber());
486                 hash = 89 * hash + Objects.hashCode(this.getReceiptBonusCard());
487                 hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
488                 hash = 89 * hash + Objects.hashCode(this.getReceiptId());
489                 hash = 89 * hash + Objects.hashCode(this.getReceiptIssued());
490                 hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
491                 hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
492                 hash = 89 * hash + Objects.hashCode(this.getReceiptRegisterNumber());
493                 hash = 89 * hash + Objects.hashCode(this.getReceiptSellerEmployee());
494                 hash = 89 * hash + Objects.hashCode(this.getReceiptSequenceNumber());
495                 hash = 89 * hash + Objects.hashCode(this.getReceiptTransactionNumber());
496                 hash = 89 * hash + Objects.hashCode(this.getReceiptUser());
497
498                 return hash;
499         }
500
501 }