]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/receipt/FinancialReceipt.java
7993c96a9f41d7a9fa58494715d57c98bd21f29c
[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.CompanyBranchOffice;
41 import org.mxchange.jproduct.model.payment.PaymentType;
42 import org.mxchange.jusercore.model.user.LoginUser;
43 import org.mxchange.jusercore.model.user.User;
44
45 /**
46  *
47  * @author Roland Häder<roland@mxchange.org>
48  */
49 @Entity (name = "receipts")
50 @Table (
51                 name = "receipts"
52 )
53 @NamedQueries (
54                 {
55                         @NamedQuery (name = "AllReceipts", query = "SELECT r FROM receipts AS r ORDER BY r.receiptId ASC"),
56                         @NamedQuery (name = "SearchAllUserReceipts", query = "SELECT r FROM receipts AS r WHERE r.receiptUser = :receiptUser ORDER BY r.receiptId ASC")
57                 }
58 )
59 @SuppressWarnings ("PersistenceUnitPresent")
60 public class FinancialReceipt implements BillableReceipt {
61
62         /**
63          * Serial number
64          */
65         @Transient
66         private static final long serialVersionUID = 185_867_217_461L;
67
68         /**
69          * Seller instance
70          */
71         @JoinColumn (name = "receipt_branch_id", referencedColumnName = "branch_id", nullable = false, updatable = false)
72         @OneToOne (targetEntity = CompanyBranchOffice.class, cascade = CascadeType.REFRESH, optional = false)
73         private BranchOffice receiptBranchOffice;
74
75         /**
76          * When this receipt entry has been created
77          */
78         @Basic (optional = false)
79         @Temporal (TemporalType.TIMESTAMP)
80         @Column (name = "receipt_created", nullable = false)
81         private Date receiptCreated;
82
83         /**
84          * Primary key
85          */
86         @Id
87         @GeneratedValue (strategy = GenerationType.IDENTITY)
88         @Column (name = "receipt_id", nullable = false, updatable = false)
89         private Long receiptId;
90
91         /**
92          * When this receipt has been issued
93          */
94         @Basic (optional = false)
95         @Temporal (TemporalType.DATE)
96         @Column (name = "receipt_issued", nullable = false)
97         private Date receiptIssued;
98
99         /**
100          * Receipt number
101          */
102         @Column (name = "receipt_number")
103         private Long receiptNumber;
104
105         /**
106          * Payment type (cash, credit card, EC card ...)
107          */
108         @Basic (optional = false)
109         @Column (name = "receipt_payment_type", nullable = false)
110         @Enumerated (EnumType.STRING)
111         private PaymentType receiptPaymentType;
112
113         /**
114          * Which user this receipt belongs to
115          */
116         @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
117         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
118         private User receiptUser;
119
120         /**
121          * Default constructor
122          */
123         public FinancialReceipt () {
124         }
125
126         /**
127          * Constructor with payment type, seller (branch office) and user
128          * <p>
129          * @param receiptPaymentType  Payment type
130          * @param receiptBranchOffice Branch office instance
131          * @param receiptUser         User instance
132          * @param receiptIssued       When this receipt has been issued
133          *
134          * @throws NullPointerException If user instance is not set
135          * @throws IllegalArgumentException If user instance's userId is invalid
136          */
137         public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final User receiptUser, final Date receiptIssued) {
138                 // Call other constructor first
139                 this(receiptPaymentType, receiptBranchOffice, receiptIssued);
140
141                 // Validate parameter
142                 if (null == receiptUser) {
143                         // Throw NPE
144                         throw new NullPointerException("user is null"); //NOI18N
145                 } else if (receiptUser.getUserId() == null) {
146                         // Throw it again
147                         throw new NullPointerException("user.userId is null"); //NOI18N
148                 } else if (receiptUser.getUserId() < 1) {
149                         // Throw IAE
150                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", receiptUser.getUserId())); //NOI18N
151                 }
152
153                 // Set user instance
154                 this.receiptUser = receiptUser;
155         }
156
157         /**
158          * Constructor with payment type, branch office and when it has been issued
159          * <p>
160          * @param receiptPaymentType  Payment type
161          * @param receiptBranchOffice Branch office instance
162          * @param receiptIssued       When this receipt has been issued
163          * <p>
164          * @throws NullPointerException If user instance is not set
165          * @throws IllegalArgumentException If branchId is invalid
166          */
167         public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice receiptBranchOffice, final Date receiptIssued) {
168                 // Call other constructor first
169                 this();
170
171                 // Validate all parameter
172                 if (null == receiptPaymentType) {
173                         // Throw NPE
174                         throw new NullPointerException("receiptPaymentType is null"); //NOI18N
175                 } else if (null == receiptBranchOffice) {
176                         // Throw NPE
177                         throw new NullPointerException("receiptBranchOffice is null"); //NOI18N
178                 } else if (receiptBranchOffice.getBranchId() == null) {
179                         // Throw NPE
180                         throw new NullPointerException("receiptBranchOffice.branchId is null"); //NOI18N
181                 } else if (receiptBranchOffice.getBranchId() < 1) {
182                         // Throw IAE
183                         throw new IllegalArgumentException(MessageFormat.format("receiptBranchOffice.branchId={0} is invalid", receiptBranchOffice.getBranchId())); //NOI18N
184                 } else if (null == receiptIssued) {
185                         // Throw NPE
186                         throw new NullPointerException("receiptIssued is null"); //NOI18N
187                 }
188
189                 // Set all values
190                 this.receiptPaymentType = receiptPaymentType;
191                 this.receiptBranchOffice = receiptBranchOffice;
192                 this.receiptIssued = receiptIssued;
193         }
194
195         @Override
196         public boolean equals (final Object object) {
197                 if (this == object) {
198                         return true;
199                 }
200                 if (null == object) {
201                         return false;
202                 } else if (this.getClass() != object.getClass()) {
203                         return false;
204                 }
205
206                 final BillableReceipt receipt = (BillableReceipt) object;
207
208                 if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
209                         return false;
210                 } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
211                         return false;
212                 } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
213                         return false;
214                 } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
215                         return false;
216                 } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
217                         return false;
218                 } else if (!Objects.equals(this.getReceiptIssued(), receipt.getReceiptIssued())) {
219                         return false;
220                 }
221
222                 return true;
223         }
224
225         @Override
226         public BranchOffice getReceiptBranchOffice () {
227                 return this.receiptBranchOffice;
228         }
229
230         @Override
231         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
232                 this.receiptBranchOffice = receiptBranchOffice;
233         }
234
235         @Override
236         @SuppressWarnings ("ReturnOfDateField")
237         public Date getReceiptCreated () {
238                 return this.receiptCreated;
239         }
240
241         @Override
242         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
243         public void setReceiptCreated (final Date receiptCreated) {
244                 this.receiptCreated = receiptCreated;
245         }
246
247         @Override
248         public Long getReceiptId () {
249                 return this.receiptId;
250         }
251
252         @Override
253         public void setReceiptId (final Long receiptId) {
254                 this.receiptId = receiptId;
255         }
256
257         @Override
258         @SuppressWarnings ("ReturnOfDateField")
259         public Date getReceiptIssued () {
260                 return this.receiptIssued;
261         }
262
263         @Override
264         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
265         public void setReceiptIssued (final Date receiptIssued) {
266                 this.receiptIssued = receiptIssued;
267         }
268
269         @Override
270         public Long getReceiptNumber () {
271                 return this.receiptNumber;
272         }
273
274         @Override
275         public void setReceiptNumber (final Long receiptNumber) {
276                 this.receiptNumber = receiptNumber;
277         }
278
279         @Override
280         public PaymentType getReceiptPaymentType () {
281                 return this.receiptPaymentType;
282         }
283
284         @Override
285         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
286                 this.receiptPaymentType = receiptPaymentType;
287         }
288
289         @Override
290         public User getReceiptUser () {
291                 return this.receiptUser;
292         }
293
294         @Override
295         public void setReceiptUser (final User receiptUser) {
296                 this.receiptUser = receiptUser;
297         }
298
299         @Override
300         public int hashCode () {
301                 int hash = 5;
302
303                 hash = 89 * hash + Objects.hashCode(this.getReceiptId());
304                 hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
305                 hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
306                 hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
307                 hash = 89 * hash + Objects.hashCode(this.getReceiptUser());
308
309                 return hash;
310         }
311
312 }