]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/receipt/item/FinancialReceiptItem.java
Continued:
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / receipt / item / FinancialReceiptItem.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.item;
18
19 import java.util.Date;
20 import java.util.Objects;
21 import javax.persistence.Basic;
22 import javax.persistence.CascadeType;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.Index;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.OneToOne;
31 import javax.persistence.Table;
32 import javax.persistence.Temporal;
33 import javax.persistence.TemporalType;
34 import javax.persistence.Transient;
35 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
36 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
37 import org.mxchange.jproduct.model.product.GenericProduct;
38 import org.mxchange.jproduct.model.product.Product;
39
40 /**
41  * A POJO for receipt items
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Entity (name = "receipt_items")
46 @Table (
47                 name = "receipt_items",
48                 indexes = {
49                         @Index (name = "item_receipt_product", columnList = "item_receipt_id,item_product_id", unique = true)
50                 }
51 )
52 @SuppressWarnings ("PersistenceUnitPresent")
53 public class FinancialReceiptItem implements BillableReceiptItem {
54
55         /**
56          * Serial number
57          */
58         @Transient
59         private static final long serialVersionUID = 126_498_698_378_571L;
60
61         /**
62          * When this item has been created in database
63          */
64         @Basic (optional = false)
65         @Temporal (TemporalType.TIMESTAMP)
66         @Column (name = "item_created", nullable = false)
67         private Date itemCreated;
68
69         /**
70          * Discount on item
71          */
72         @Column (name = "item_product_discount")
73         private Float itemDiscount;
74
75         /**
76          * Primary key
77          */
78         @Id
79         @GeneratedValue (strategy = GenerationType.IDENTITY)
80         @Column (name = "item_id", nullable = false, updatable = false)
81         private Long itemId;
82
83         /**
84          * Product being linked in this itemReceipt item
85          */
86         @JoinColumn (name = "item_product_id", referencedColumnName = "product_id", nullable = false, updatable = false)
87         @OneToOne (targetEntity = GenericProduct.class, cascade = CascadeType.REFRESH, optional = false)
88         private Product itemProduct;
89
90         /**
91          * Single product price (being copied here from GenericProduct)
92          */
93         @Basic (optional = false)
94         @Column (name = "item_product_price", nullable = false)
95         private Float itemProductPrice;
96
97         /**
98          * Product quantity
99          */
100         @Basic (optional = false)
101         @Column (name = "item_product_quantity", nullable = false)
102         private Long itemProductQuantity;
103
104         /**
105          * Connected itemReceipt item
106          */
107         @JoinColumn (name = "item_receipt_id", referencedColumnName = "receipt_id", nullable = false, updatable = false)
108         @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH, optional = false)
109         private BillableReceipt itemReceipt;
110
111         /**
112          * Default constructor
113          */
114         public FinancialReceiptItem () {
115         }
116
117         /**
118          * Constructor with product, price, quantity and receipt instance
119          * <p>
120          * @param itemProduct         Product instance
121          * @param itemProductPrice    Product price (copied)
122          * @param itemProductQuantity Product quantity
123          * @param itemReceipt         FinancialReceipt instance
124          */
125         public FinancialReceiptItem (final Product itemProduct, final Float itemProductPrice, final Long itemProductQuantity, final BillableReceipt itemReceipt) {
126                 // Call other constructor
127                 this();
128
129                 // Set all values
130                 this.itemProduct = itemProduct;
131                 this.itemProductPrice = itemProductPrice;
132                 this.itemProductQuantity = itemProductQuantity;
133                 this.itemReceipt = itemReceipt;
134         }
135
136         @Override
137         public boolean equals (final Object object) {
138                 if (this == object) {
139                         return true;
140                 } else if (null == object) {
141                         return false;
142                 } else if (this.getClass() != object.getClass()) {
143                         return false;
144                 }
145
146                 final BillableReceiptItem receiptItem = (BillableReceiptItem) object;
147
148                 if (!Objects.equals(this.getItemId(), receiptItem.getItemId())) {
149                         return false;
150                 } else if (!Objects.equals(this.getItemProduct(), receiptItem.getItemProduct())) {
151                         return false;
152                 } else if (!Objects.equals(this.getItemProductPrice(), receiptItem.getItemProductPrice())) {
153                         return false;
154                 } else if (!Objects.equals(this.getItemProductQuantity(), receiptItem.getItemProductQuantity())) {
155                         return false;
156                 } else if (!Objects.equals(this.getItemReceipt(), receiptItem.getItemReceipt())) {
157                         return false;
158                 }
159
160                 return true;
161         }
162
163         @Override
164         @SuppressWarnings ("ReturnOfDateField")
165         public Date getItemCreated () {
166                 return this.itemCreated;
167         }
168
169         @Override
170         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
171         public void setItemCreated (final Date itemCreated) {
172                 this.itemCreated = itemCreated;
173         }
174
175         @Override
176         public Float getItemDiscount () {
177                 return this.itemDiscount;
178         }
179
180         @Override
181         public void setItemDiscount (final Float itemDiscount) {
182                 this.itemDiscount = itemDiscount;
183         }
184
185         @Override
186         public Long getItemId () {
187                 return this.itemId;
188         }
189
190         @Override
191         public void setItemId (final Long itemId) {
192                 this.itemId = itemId;
193         }
194
195         @Override
196         public Product getItemProduct () {
197                 return this.itemProduct;
198         }
199
200         @Override
201         public void setItemProduct (final Product itemProduct) {
202                 this.itemProduct = itemProduct;
203         }
204
205         @Override
206         public Float getItemProductPrice () {
207                 return this.itemProductPrice;
208         }
209
210         @Override
211         public void setItemProductPrice (final Float itemProductPrice) {
212                 this.itemProductPrice = itemProductPrice;
213         }
214
215         @Override
216         public Long getItemProductQuantity () {
217                 return this.itemProductQuantity;
218         }
219
220         @Override
221         public void setItemProductQuantity (final Long itemProductQuantity) {
222                 this.itemProductQuantity = itemProductQuantity;
223         }
224
225         @Override
226         public BillableReceipt getItemReceipt () {
227                 return this.itemReceipt;
228         }
229
230         @Override
231         public void setItemReceipt (final BillableReceipt itemReceipt) {
232                 this.itemReceipt = itemReceipt;
233         }
234
235         @Override
236         public int hashCode () {
237                 int hash = 5;
238
239                 hash = 53 * hash + Objects.hashCode(this.getItemId());
240                 hash = 53 * hash + Objects.hashCode(this.getItemProduct());
241                 hash = 53 * hash + Objects.hashCode(this.getItemProductPrice());
242                 hash = 53 * hash + Objects.hashCode(this.getItemProductQuantity());
243                 hash = 53 * hash + Objects.hashCode(this.getItemReceipt());
244
245                 return hash;
246         }
247
248 }