]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/receipt_item/FinancialReceiptItem.java
Updated copyright year
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / receipt_item / FinancialReceiptItem.java
1 /*
2  * Copyright (C) 2016 - 2024 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.text.MessageFormat;
21 import java.util.Date;
22 import java.util.Objects;
23 import javax.persistence.Basic;
24 import javax.persistence.CascadeType;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.JoinColumn;
31 import javax.persistence.NamedQueries;
32 import javax.persistence.NamedQuery;
33 import javax.persistence.OneToOne;
34 import javax.persistence.Table;
35 import javax.persistence.Temporal;
36 import javax.persistence.TemporalType;
37 import javax.persistence.Transient;
38 import org.apache.commons.lang3.StringUtils;
39 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
40 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
41 import org.mxchange.jcoreutils.comparable.ComparableUtils;
42 import org.mxchange.jcoreutils.number.SafeNumberUtils;
43 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
44 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
45 import org.mxchange.jfinancials.model.utils.ReceiptUtils;
46 import org.mxchange.jproduct.model.product.GenericProduct;
47 import org.mxchange.jproduct.model.product.Product;
48 import org.mxchange.jproduct.model.utils.ProductUtils;
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                 }
63 )
64 @SuppressWarnings ("PersistenceUnitPresent")
65 public class FinancialReceiptItem implements BillableReceiptItem {
66
67         /**
68          * Serial number
69          */
70         @Transient
71         private static final long serialVersionUID = 126_498_698_378_571L;
72
73         /**
74          * Item branding
75          */
76         @Column (name = "item_brand_name")
77         private String itemBrandName;
78
79         /**
80          * Item 's coupon number
81          */
82         @Column (name = "item_coupon_number")
83         private String itemCouponNumber;
84
85         /**
86          * When this item has been created in database
87          */
88         @Basic (optional = false)
89         @Temporal (TemporalType.TIMESTAMP)
90         @Column (name = "item_entry_created", updatable = false, nullable = false)
91         private Date itemEntryCreated;
92
93         /**
94          * When this item has been updated
95          */
96         @Temporal (TemporalType.TIMESTAMP)
97         @Column (name = "item_entry_updated", insertable = false)
98         private Date itemEntryUpdated;
99
100         /**
101          * Gross price of item
102          */
103         @Basic (optional = false)
104         @Column (name = "item_gross_price", nullable = false, precision = 10, scale = 2)
105         private BigDecimal itemGrossPrice;
106
107         /**
108          * Primary key
109          */
110         @Id
111         @GeneratedValue (strategy = GenerationType.IDENTITY)
112         @Column (name = "item_id", nullable = false, updatable = false)
113         private Long itemId;
114
115         /**
116          * Whether the item is a discount on whole receipt or single item
117          */
118         @Basic (optional = false)
119         @Column (name = "item_is_discount", nullable = false)
120         private Boolean itemIsDiscount;
121
122         /**
123          * Whether the item is a refund
124          */
125         @Basic (optional = false)
126         @Column (name = "item_is_refund", nullable = false)
127         private Boolean itemIsRefund;
128
129         /**
130          * Manufacturer/producer of this item
131          */
132         @JoinColumn (name = "item_manufacturer_id", referencedColumnName = "company_data_id")
133         @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
134         private BasicData itemManufacturer;
135
136         /**
137          * Net price of item
138          */
139         @Column (name = "item_net_price", precision = 10, scale = 2)
140         private BigDecimal itemNetPrice;
141
142         /**
143          * Item's number
144          */
145         @Column (name = "item_number")
146         private Long itemNumber;
147
148         /**
149          * Product being linked in this itemReceipt item
150          */
151         @JoinColumn (name = "item_product_id", referencedColumnName = "product_id", nullable = false, updatable = false)
152         @OneToOne (targetEntity = GenericProduct.class, cascade = CascadeType.REFRESH, optional = false)
153         private Product itemProduct;
154
155         /**
156          * Product quantity
157          */
158         @Basic (optional = false)
159         @Column (name = "item_product_quantity", nullable = false, precision = 4)
160         private BigDecimal itemProductQuantity;
161
162         /**
163          * Connected itemReceipt item
164          */
165         @JoinColumn (name = "item_receipt_id", referencedColumnName = "receipt_id", nullable = false, updatable = false)
166         @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH, optional = false)
167         private BillableReceipt itemReceipt;
168
169         /**
170          * Tax rate
171          */
172         @Column (name = "item_tax_rate", precision = 2)
173         private BigDecimal itemTaxRate;
174
175         /**
176          * Default constructor
177          */
178         public FinancialReceiptItem () {
179         }
180
181         /**
182          * Constructor with product, price, quantity and receipt instance
183          * <p>
184          * @param itemProduct         Product instance
185          * @param itemProductQuantity Product quantity
186          * @param itemReceipt         FinancialReceipt instance
187          */
188         public FinancialReceiptItem (final Product itemProduct, final BigDecimal itemProductQuantity, final BillableReceipt itemReceipt) {
189                 // Call other constructor
190                 this();
191
192                 // Validate parameter
193                 if (null == itemProduct) {
194                         // Throw NPE
195                         throw new NullPointerException("itemProduct is null"); //NOI18N
196                 } else if (itemProduct.getProductId() == null) {
197                         // Throw NPE again
198                         throw new NullPointerException("itemProduct.productId is null"); //NOI18N
199                 } else if (itemProduct.getProductId() < 1) {
200                         // Throw IAE
201                         throw new IllegalArgumentException(MessageFormat.format("itemProduct.productId={0} is not valid.", itemProduct.getProductId())); //NOI18N
202                 } else if (null == itemProductQuantity) {
203                         // Throw NPE
204                         throw new NullPointerException("itemProductQuanity is null"); //NOI18N
205                 } else if (itemProductQuantity.floatValue() < 0) {
206                         // Throw IAE
207                         throw new IllegalArgumentException(MessageFormat.format("itemProductQuanity={0} is not valid.", itemProductQuantity)); //NOI18N
208                 } else if (null == itemReceipt) {
209                         // Throw NPE
210                         throw new NullPointerException("itemReceipt is null"); //NOI18N
211                 } else if (itemReceipt.getReceiptId() == null) {
212                         // Throw NPE again
213                         throw new NullPointerException("itemReceipt.receiptId is null"); //NOI18N
214                 } else if (itemReceipt.getReceiptId() < 1) {
215                         // Throw IAE
216                         throw new IllegalArgumentException(MessageFormat.format("itemReceipt.receiptId={0} is not valid.", itemReceipt.getReceiptId())); //NOI18N
217                 }
218
219                 // Set all values
220                 this.itemProduct = itemProduct;
221                 this.itemProductQuantity = itemProductQuantity;
222                 this.itemReceipt = itemReceipt;
223         }
224
225         @Override
226         public int compareTo (final BillableReceiptItem billableReceiptItem) {
227                 // Check parameter on null-reference and equality to this
228                 if (null == billableReceiptItem) {
229                         // Should not happen
230                         throw new NullPointerException("billableReceiptItem is null"); //NOI18N
231                 } else if (billableReceiptItem.equals(this)) {
232                         // Same object
233                         return 0;
234                 }
235
236                 // Init comparators
237                 final int comparators[] = {
238                         // First check brand name ...
239                         StringUtils.compare(this.getItemBrandName(), billableReceiptItem.getItemBrandName()),
240                         // ... item number
241                         SafeNumberUtils.compare(this.getItemNumber(), billableReceiptItem.getItemNumber()),
242                         // ... coupon number
243                         StringUtils.compare(this.getItemCouponNumber(), billableReceiptItem.getItemCouponNumber()),
244                         // ... gross price
245                         SafeNumberUtils.compare(this.getItemGrossPrice(), billableReceiptItem.getItemGrossPrice()),
246                         // ... net price
247                         SafeNumberUtils.compare(this.getItemNetPrice(), billableReceiptItem.getItemNetPrice()),
248                         // ... tax rate
249                         SafeNumberUtils.compare(this.getItemTaxRate(), billableReceiptItem.getItemTaxRate()),
250                         // ... product quanity
251                         this.getItemProductQuantity().compareTo(billableReceiptItem.getItemProductQuantity()),
252                         // ... product instance
253                         ProductUtils.compare(this.getItemProduct(), billableReceiptItem.getItemProduct()),
254                         // and finally receipt instance
255                         ReceiptUtils.compare(this.getItemReceipt(), billableReceiptItem.getItemReceipt())
256                 };
257
258                 // Check all values
259                 final int comparison = ComparableUtils.checkAll(comparators);
260
261                 // Return value
262                 return comparison;
263         }
264
265         @Override
266         public boolean equals (final Object object) {
267                 if (this == object) {
268                         return true;
269                 } else if (null == object) {
270                         return false;
271                 } else if (this.getClass() != object.getClass()) {
272                         return false;
273                 }
274
275                 final BillableReceiptItem receiptItem = (BillableReceiptItem) object;
276
277                 if (!Objects.equals(this.getItemBrandName(), receiptItem.getItemBrandName())) {
278                         return false;
279                 } else if (!Objects.equals(this.getItemCouponNumber(), receiptItem.getItemCouponNumber())) {
280                         return false;
281                 } else if (!Objects.equals(this.getItemGrossPrice(), receiptItem.getItemGrossPrice())) {
282                         return false;
283                 } else if (!Objects.equals(this.getItemId(), receiptItem.getItemId())) {
284                         return false;
285                 } else if (!Objects.equals(this.getItemIsDiscount(), receiptItem.getItemIsDiscount())) {
286                         return false;
287                 } else if (!Objects.equals(this.getItemIsRefund(), receiptItem.getItemIsRefund())) {
288                         return false;
289                 } else if (!Objects.equals(this.getItemManufacturer(), receiptItem.getItemManufacturer())) {
290                         return false;
291                 } else if (!Objects.equals(this.getItemNumber(), receiptItem.getItemNumber())) {
292                         return false;
293                 } else if (!Objects.equals(this.getItemProduct(), receiptItem.getItemProduct())) {
294                         return false;
295                 } else if (!Objects.equals(this.getItemProductQuantity(), receiptItem.getItemProductQuantity())) {
296                         return false;
297                 } else if (!Objects.equals(this.getItemReceipt(), receiptItem.getItemReceipt())) {
298                         return false;
299                 } else if (!Objects.equals(this.getItemTaxRate(), receiptItem.getItemTaxRate())) {
300                         return false;
301                 }
302
303                 return true;
304         }
305
306         @Override
307         public String getItemBrandName () {
308                 return this.itemBrandName;
309         }
310
311         @Override
312         public void setItemBrandName (final String itemBrandName) {
313                 this.itemBrandName = itemBrandName;
314         }
315
316         @Override
317         public String getItemCouponNumber () {
318                 return this.itemCouponNumber;
319         }
320
321         @Override
322         public void setItemCouponNumber (final String itemCouponNumber) {
323                 this.itemCouponNumber = itemCouponNumber;
324         }
325
326         @Override
327         @SuppressWarnings ("ReturnOfDateField")
328         public Date getItemEntryCreated () {
329                 return this.itemEntryCreated;
330         }
331
332         @Override
333         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
334         public void setItemEntryCreated (final Date itemEntryCreated) {
335                 this.itemEntryCreated = itemEntryCreated;
336         }
337
338         @Override
339         @SuppressWarnings ("ReturnOfDateField")
340         public Date getItemEntryUpdated () {
341                 return this.itemEntryUpdated;
342         }
343
344         @Override
345         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
346         public void setItemEntryUpdated (final Date itemEntryUpdated) {
347                 this.itemEntryUpdated = itemEntryUpdated;
348         }
349
350         @Override
351         public BigDecimal getItemGrossPrice () {
352                 return this.itemGrossPrice;
353         }
354
355         @Override
356         public void setItemGrossPrice (final BigDecimal itemGrossPrice) {
357                 this.itemGrossPrice = itemGrossPrice;
358         }
359
360         @Override
361         public Long getItemId () {
362                 return this.itemId;
363         }
364
365         @Override
366         public void setItemId (final Long itemId) {
367                 this.itemId = itemId;
368         }
369
370         @Override
371         public Boolean getItemIsDiscount () {
372                 return this.itemIsDiscount;
373         }
374
375         @Override
376         public void setItemIsDiscount (final Boolean itemIsDiscount) {
377                 this.itemIsDiscount = itemIsDiscount;
378         }
379
380         @Override
381         public Boolean getItemIsRefund () {
382                 return this.itemIsRefund;
383         }
384
385         @Override
386         public void setItemIsRefund (final Boolean itemIsRefund) {
387                 this.itemIsRefund = itemIsRefund;
388         }
389
390         @Override
391         public BasicData getItemManufacturer () {
392                 return this.itemManufacturer;
393         }
394
395         @Override
396         public void setItemManufacturer (final BasicData itemManufacturer) {
397                 this.itemManufacturer = itemManufacturer;
398         }
399
400         @Override
401         public BigDecimal getItemNetPrice () {
402                 return this.itemNetPrice;
403         }
404
405         @Override
406         public void setItemNetPrice (final BigDecimal itemNetPrice) {
407                 this.itemNetPrice = itemNetPrice;
408         }
409
410         @Override
411         public Long getItemNumber () {
412                 return this.itemNumber;
413         }
414
415         @Override
416         public void setItemNumber (final Long itemNumber) {
417                 this.itemNumber = itemNumber;
418         }
419
420         @Override
421         public Product getItemProduct () {
422                 return this.itemProduct;
423         }
424
425         @Override
426         public void setItemProduct (final Product itemProduct) {
427                 this.itemProduct = itemProduct;
428         }
429
430         @Override
431         public BigDecimal getItemProductQuantity () {
432                 return this.itemProductQuantity;
433         }
434
435         @Override
436         public void setItemProductQuantity (final BigDecimal itemProductQuantity) {
437                 this.itemProductQuantity = itemProductQuantity;
438         }
439
440         @Override
441         public BillableReceipt getItemReceipt () {
442                 return this.itemReceipt;
443         }
444
445         @Override
446         public void setItemReceipt (final BillableReceipt itemReceipt) {
447                 this.itemReceipt = itemReceipt;
448         }
449
450         @Override
451         public BigDecimal getItemTaxRate () {
452                 return this.itemTaxRate;
453         }
454
455         @Override
456         public void setItemTaxRate (final BigDecimal itemTaxRate) {
457                 this.itemTaxRate = itemTaxRate;
458         }
459
460         @Override
461         public int hashCode () {
462                 int hash = 5;
463
464                 hash = 53 * hash + Objects.hashCode(this.getItemBrandName());
465                 hash = 53 * hash + Objects.hashCode(this.getItemCouponNumber());
466                 hash = 53 * hash + Objects.hashCode(this.getItemGrossPrice());
467                 hash = 53 * hash + Objects.hashCode(this.getItemId());
468                 hash = 53 * hash + Objects.hashCode(this.getItemIsDiscount());
469                 hash = 53 * hash + Objects.hashCode(this.getItemIsRefund());
470                 hash = 53 * hash + Objects.hashCode(this.getItemManufacturer());
471                 hash = 53 * hash + Objects.hashCode(this.getItemNumber());
472                 hash = 53 * hash + Objects.hashCode(this.getItemProduct());
473                 hash = 53 * hash + Objects.hashCode(this.getItemProductQuantity());
474                 hash = 53 * hash + Objects.hashCode(this.getItemReceipt());
475                 hash = 53 * hash + Objects.hashCode(this.getItemTaxRate());
476
477                 return hash;
478         }
479
480 }