]> 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 - 2018 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_item;
18
19 import java.math.BigDecimal;
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.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.NamedQueries;
31 import javax.persistence.NamedQuery;
32 import javax.persistence.OneToOne;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import javax.persistence.TemporalType;
36 import javax.persistence.Transient;
37 import org.apache.commons.lang3.StringUtils;
38 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
39 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
40 import org.mxchange.jcoreutils.Comparables;
41 import org.mxchange.jcoreutils.SafeNumberUtils;
42 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
43 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
44 import org.mxchange.jproduct.model.category.Category;
45 import org.mxchange.jproduct.model.category.ProductCategory;
46 import org.mxchange.jproduct.model.product.GenericProduct;
47 import org.mxchange.jproduct.model.product.Product;
48 import org.mxchange.jproduct.model.product.Products;
49
50 /**
51  * A POJO for receipt items
52  * <p>
53  * @author Roland Häder<roland@mxchange.org>
54  */
55 @Entity (name = "receipt_items")
56 @Table (
57                 name = "receipt_items"
58 )
59 @NamedQueries (
60                 {
61                         @NamedQuery (name = "AllReceiptItems", query = "SELECT ri FROM receipt_items AS ri ORDER BY ri.itemId ASC"),
62                         @NamedQuery (name = "SearchAssignedReceiptItems", query = "SELECT ri FROM receipt_items AS ri WHERE ri.itemReceipt = :itemReceipt ORDER BY ri.itemId ASC"),
63                         @NamedQuery (name = "SearchAllUserReceiptItems", query = "SELECT ri FROM receipt_items AS ri JOIN receipts AS r ON ri.itemReceipt=r WHERE r.receiptUser = :receiptUser ORDER BY ri.itemId ASC")
64                 }
65 )
66 @SuppressWarnings ("PersistenceUnitPresent")
67 public class FinancialReceiptItem implements BillableReceiptItem {
68
69         /**
70          * Serial number
71          */
72         @Transient
73         private static final long serialVersionUID = 126_498_698_378_571L;
74
75         /**
76          * Item branding
77          */
78         @Column (name = "item_brand_name")
79         private String itemBrandName;
80
81         /**
82          * Category being assigned to item's product
83          */
84         @JoinColumn (name = "item_category_id", referencedColumnName = "category_id", updatable = false)
85         @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH, optional = false)
86         private Category itemCategory;
87
88         /**
89          * Item 's coupon number
90          */
91         @Column (name = "item_coupon_number")
92         private String itemCouponNumber;
93
94         /**
95          * When this item has been created in database
96          */
97         @Basic (optional = false)
98         @Temporal (TemporalType.TIMESTAMP)
99         @Column (name = "item_created", nullable = false)
100         private Date itemCreated;
101
102         /**
103          * Gross price of item
104          */
105         @Basic (optional = false)
106         @Column (name = "item_gross_price", nullable = false, precision = 10, scale = 2)
107         private BigDecimal itemGrossPrice;
108
109         /**
110          * Primary key
111          */
112         @Id
113         @GeneratedValue (strategy = GenerationType.IDENTITY)
114         @Column (name = "item_id", nullable = false, updatable = false)
115         private Long itemId;
116
117         /**
118          * Whether the item is a discount on whole receipt or single item
119          */
120         @Basic (optional = false)
121         @Column (name = "item_is_discount", nullable = false)
122         private Boolean itemIsDiscount;
123
124         /**
125          * Whether the item is a refund
126          */
127         @Basic (optional = false)
128         @Column (name = "item_is_refund", nullable = false)
129         private Boolean itemIsRefund;
130
131         /**
132          * Manufacturer/producer of this item
133          */
134         @JoinColumn (name = "item_manufacturer_id", referencedColumnName = "company_data_id")
135         @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
136         private BasicData itemManufacturer;
137
138         /**
139          * Net price of item
140          */
141         @Column (name = "item_net_price", precision = 10, scale = 2)
142         private BigDecimal itemNetPrice;
143
144         /**
145          * Item's number
146          */
147         @Column (name = "item_number")
148         private Long itemNumber;
149
150         /**
151          * Product being linked in this itemReceipt item
152          */
153         @JoinColumn (name = "item_product_id", referencedColumnName = "product_id", nullable = false, updatable = false)
154         @OneToOne (targetEntity = GenericProduct.class, cascade = CascadeType.REFRESH, optional = false)
155         private Product itemProduct;
156
157         /**
158          * Product quantity
159          */
160         @Basic (optional = false)
161         @Column (name = "item_product_quantity", nullable = false, precision = 3)
162         private BigDecimal itemProductQuantity;
163
164         /**
165          * Connected itemReceipt item
166          */
167         @JoinColumn (name = "item_receipt_id", referencedColumnName = "receipt_id", nullable = false, updatable = false)
168         @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH, optional = false)
169         private BillableReceipt itemReceipt;
170
171         /**
172          * Tax rate
173          */
174         @Column (name = "item_tax_rate", precision = 2)
175         private BigDecimal itemTaxRate;
176
177         /**
178          * Default constructor
179          */
180         public FinancialReceiptItem () {
181         }
182
183         /**
184          * Constructor with product, price, quantity and receipt instance
185          * <p>
186          * @param itemProduct         Product instance
187          * @param itemProductQuantity Product quantity
188          * @param itemReceipt         FinancialReceipt instance
189          */
190         public FinancialReceiptItem (final Product itemProduct, final BigDecimal itemProductQuantity, final BillableReceipt itemReceipt) {
191                 // Call other constructor
192                 this();
193
194                 // Set all values
195                 this.itemCategory = itemProduct.getProductCategory();
196                 this.itemProduct = itemProduct;
197                 this.itemProductQuantity = itemProductQuantity;
198                 this.itemReceipt = itemReceipt;
199         }
200
201         @Override
202         public int compareTo (final BillableReceiptItem billableReceiptItem) {
203                 // Check parameter on null-reference and equality to this
204                 if (null == billableReceiptItem) {
205                         // Should not happen
206                         throw new NullPointerException("billableReceiptItem is null"); //NOI18N
207                 } else if (billableReceiptItem.equals(this)) {
208                         // Same object
209                         return 0;
210                 }
211
212                 // Init comparators
213                 final int comparators[] = {
214                         // First check brand name ...
215                         StringUtils.compare(this.getItemBrandName(), billableReceiptItem.getItemBrandName()),
216                         // ... item number
217                         SafeNumberUtils.compare(this.getItemNumber(), billableReceiptItem.getItemNumber()),
218                         // ... coupon number
219                         StringUtils.compare(this.getItemCouponNumber(), billableReceiptItem.getItemCouponNumber()),
220                         // ... gross price
221                         SafeNumberUtils.compare(this.getItemGrossPrice(), billableReceiptItem.getItemGrossPrice()),
222                         // ... net price
223                         SafeNumberUtils.compare(this.getItemNetPrice(), billableReceiptItem.getItemNetPrice()),
224                         // ... tax rate
225                         SafeNumberUtils.compare(this.getItemTaxRate(), billableReceiptItem.getItemTaxRate()),
226                         // ... product quanity
227                         this.getItemProductQuantity().compareTo(billableReceiptItem.getItemProductQuantity()),
228                         // ... product instance
229                         Products.compare(this.getItemProduct(), billableReceiptItem.getItemProduct()),
230                 };
231
232                 // Check all values
233                 final int comparison = Comparables.checkAll(comparators);
234
235                 // Return value
236                 return comparison;
237         }
238
239         @Override
240         public boolean equals (final Object object) {
241                 if (this == object) {
242                         return true;
243                 } else if (null == object) {
244                         return false;
245                 } else if (this.getClass() != object.getClass()) {
246                         return false;
247                 }
248
249                 final BillableReceiptItem receiptItem = (BillableReceiptItem) object;
250
251                 if (!Objects.equals(this.getItemId(), receiptItem.getItemId())) {
252                         return false;
253                 } else if (!Objects.equals(this.getItemProduct(), receiptItem.getItemProduct())) {
254                         return false;
255                 } else if (!Objects.equals(this.getItemProductQuantity(), receiptItem.getItemProductQuantity())) {
256                         return false;
257                 } else if (!Objects.equals(this.getItemReceipt(), receiptItem.getItemReceipt())) {
258                         return false;
259                 }
260
261                 return true;
262         }
263
264         @Override
265         public String getItemBrandName () {
266                 return this.itemBrandName;
267         }
268
269         @Override
270         public void setItemBrandName (final String itemBrandName) {
271                 this.itemBrandName = itemBrandName;
272         }
273
274         @Override
275         public Category getItemCategory () {
276                 return this.itemCategory;
277         }
278
279         @Override
280         public void setItemCategory (final Category itemCategory) {
281                 this.itemCategory = itemCategory;
282         }
283
284         @Override
285         public String getItemCouponNumber () {
286                 return this.itemCouponNumber;
287         }
288
289         @Override
290         public void setItemCouponNumber (final String itemCouponNumber) {
291                 this.itemCouponNumber = itemCouponNumber;
292         }
293
294         @Override
295         @SuppressWarnings ("ReturnOfDateField")
296         public Date getItemCreated () {
297                 return this.itemCreated;
298         }
299
300         @Override
301         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
302         public void setItemCreated (final Date itemCreated) {
303                 this.itemCreated = itemCreated;
304         }
305
306         @Override
307         public BigDecimal getItemGrossPrice () {
308                 return this.itemGrossPrice;
309         }
310
311         @Override
312         public void setItemGrossPrice (final BigDecimal itemGrossPrice) {
313                 this.itemGrossPrice = itemGrossPrice;
314         }
315
316         @Override
317         public Long getItemId () {
318                 return this.itemId;
319         }
320
321         @Override
322         public void setItemId (final Long itemId) {
323                 this.itemId = itemId;
324         }
325
326         @Override
327         public Boolean getItemIsDiscount () {
328                 return this.itemIsDiscount;
329         }
330
331         @Override
332         public void setItemIsDiscount (final Boolean itemIsDiscount) {
333                 this.itemIsDiscount = itemIsDiscount;
334         }
335
336         @Override
337         public Boolean getItemIsRefund () {
338                 return this.itemIsRefund;
339         }
340
341         @Override
342         public void setItemIsRefund (final Boolean itemIsRefund) {
343                 this.itemIsRefund = itemIsRefund;
344         }
345
346         @Override
347         public BasicData getItemManufacturer () {
348                 return this.itemManufacturer;
349         }
350
351         @Override
352         public void setItemManufacturer (final BasicData itemManufacturer) {
353                 this.itemManufacturer = itemManufacturer;
354         }
355
356         @Override
357         public BigDecimal getItemNetPrice () {
358                 return this.itemNetPrice;
359         }
360
361         @Override
362         public void setItemNetPrice (final BigDecimal itemNetPrice) {
363                 this.itemNetPrice = itemNetPrice;
364         }
365
366         @Override
367         public Long getItemNumber () {
368                 return this.itemNumber;
369         }
370
371         @Override
372         public void setItemNumber (final Long itemNumber) {
373                 this.itemNumber = itemNumber;
374         }
375
376         @Override
377         public Product getItemProduct () {
378                 return this.itemProduct;
379         }
380
381         @Override
382         public void setItemProduct (final Product itemProduct) {
383                 this.itemProduct = itemProduct;
384         }
385
386         @Override
387         public BigDecimal getItemProductQuantity () {
388                 return this.itemProductQuantity;
389         }
390
391         @Override
392         public void setItemProductQuantity (final BigDecimal itemProductQuantity) {
393                 this.itemProductQuantity = itemProductQuantity;
394         }
395
396         @Override
397         public BillableReceipt getItemReceipt () {
398                 return this.itemReceipt;
399         }
400
401         @Override
402         public void setItemReceipt (final BillableReceipt itemReceipt) {
403                 this.itemReceipt = itemReceipt;
404         }
405
406         @Override
407         public BigDecimal getItemTaxRate () {
408                 return this.itemTaxRate;
409         }
410
411         @Override
412         public void setItemTaxRate (final BigDecimal itemTaxRate) {
413                 this.itemTaxRate = itemTaxRate;
414         }
415
416         @Override
417         public int hashCode () {
418                 int hash = 5;
419
420                 hash = 53 * hash + Objects.hashCode(this.getItemId());
421                 hash = 53 * hash + Objects.hashCode(this.getItemProduct());
422                 hash = 53 * hash + Objects.hashCode(this.getItemProductQuantity());
423                 hash = 53 * hash + Objects.hashCode(this.getItemReceipt());
424
425                 return hash;
426         }
427
428 }