2 * Copyright (C) 2016 - 2024 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.model.receipt_item;
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;
51 * A POJO for receipt items
53 * @author Roland Häder<roland@mxchange.org>
55 @Entity (name = "receipt_items")
57 name = "receipt_items"
61 @NamedQuery (name = "AllReceiptItems", query = "SELECT ri FROM receipt_items AS ri ORDER BY ri.itemId ASC")
64 @SuppressWarnings ("PersistenceUnitPresent")
65 public class FinancialReceiptItem implements BillableReceiptItem {
71 private static final long serialVersionUID = 126_498_698_378_571L;
76 @Column (name = "item_brand_name")
77 private String itemBrandName;
80 * Item 's coupon number
82 @Column (name = "item_coupon_number")
83 private String itemCouponNumber;
86 * When this item has been created in database
88 @Basic (optional = false)
89 @Temporal (TemporalType.TIMESTAMP)
90 @Column (name = "item_entry_created", updatable = false, nullable = false)
91 private Date itemEntryCreated;
94 * When this item has been updated
96 @Temporal (TemporalType.TIMESTAMP)
97 @Column (name = "item_entry_updated", insertable = false)
98 private Date itemEntryUpdated;
101 * Gross price of item
103 @Basic (optional = false)
104 @Column (name = "item_gross_price", nullable = false, precision = 10, scale = 2)
105 private BigDecimal itemGrossPrice;
111 @GeneratedValue (strategy = GenerationType.IDENTITY)
112 @Column (name = "item_id", nullable = false, updatable = false)
116 * Whether the item is a discount on whole receipt or single item
118 @Basic (optional = false)
119 @Column (name = "item_is_discount", nullable = false)
120 private Boolean itemIsDiscount;
123 * Whether the item is a refund
125 @Basic (optional = false)
126 @Column (name = "item_is_refund", nullable = false)
127 private Boolean itemIsRefund;
130 * Manufacturer/producer of this item
132 @JoinColumn (name = "item_manufacturer_id", referencedColumnName = "company_data_id")
133 @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
134 private BasicData itemManufacturer;
139 @Column (name = "item_net_price", precision = 10, scale = 2)
140 private BigDecimal itemNetPrice;
145 @Column (name = "item_number")
146 private Long itemNumber;
149 * Product being linked in this itemReceipt item
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;
158 @Basic (optional = false)
159 @Column (name = "item_product_quantity", nullable = false, precision = 4)
160 private BigDecimal itemProductQuantity;
163 * Connected itemReceipt item
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;
172 @Column (name = "item_tax_rate", precision = 2)
173 private BigDecimal itemTaxRate;
176 * Default constructor
178 public FinancialReceiptItem () {
182 * Constructor with product, price, quantity and receipt instance
184 * @param itemProduct Product instance
185 * @param itemProductQuantity Product quantity
186 * @param itemReceipt FinancialReceipt instance
188 public FinancialReceiptItem (final Product itemProduct, final BigDecimal itemProductQuantity, final BillableReceipt itemReceipt) {
189 // Call other constructor
192 // Validate parameter
193 if (null == itemProduct) {
195 throw new NullPointerException("itemProduct is null"); //NOI18N
196 } else if (itemProduct.getProductId() == null) {
198 throw new NullPointerException("itemProduct.productId is null"); //NOI18N
199 } else if (itemProduct.getProductId() < 1) {
201 throw new IllegalArgumentException(MessageFormat.format("itemProduct.productId={0} is not valid.", itemProduct.getProductId())); //NOI18N
202 } else if (null == itemProductQuantity) {
204 throw new NullPointerException("itemProductQuanity is null"); //NOI18N
205 } else if (itemProductQuantity.floatValue() < 0) {
207 throw new IllegalArgumentException(MessageFormat.format("itemProductQuanity={0} is not valid.", itemProductQuantity)); //NOI18N
208 } else if (null == itemReceipt) {
210 throw new NullPointerException("itemReceipt is null"); //NOI18N
211 } else if (itemReceipt.getReceiptId() == null) {
213 throw new NullPointerException("itemReceipt.receiptId is null"); //NOI18N
214 } else if (itemReceipt.getReceiptId() < 1) {
216 throw new IllegalArgumentException(MessageFormat.format("itemReceipt.receiptId={0} is not valid.", itemReceipt.getReceiptId())); //NOI18N
220 this.itemProduct = itemProduct;
221 this.itemProductQuantity = itemProductQuantity;
222 this.itemReceipt = itemReceipt;
226 public int compareTo (final BillableReceiptItem billableReceiptItem) {
227 // Check parameter on null-reference and equality to this
228 if (null == billableReceiptItem) {
230 throw new NullPointerException("billableReceiptItem is null"); //NOI18N
231 } else if (billableReceiptItem.equals(this)) {
237 final int comparators[] = {
238 // First check brand name ...
239 StringUtils.compare(this.getItemBrandName(), billableReceiptItem.getItemBrandName()),
241 SafeNumberUtils.compare(this.getItemNumber(), billableReceiptItem.getItemNumber()),
243 StringUtils.compare(this.getItemCouponNumber(), billableReceiptItem.getItemCouponNumber()),
245 SafeNumberUtils.compare(this.getItemGrossPrice(), billableReceiptItem.getItemGrossPrice()),
247 SafeNumberUtils.compare(this.getItemNetPrice(), billableReceiptItem.getItemNetPrice()),
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())
259 final int comparison = ComparableUtils.checkAll(comparators);
266 public boolean equals (final Object object) {
267 if (this == object) {
269 } else if (null == object) {
271 } else if (this.getClass() != object.getClass()) {
275 final BillableReceiptItem receiptItem = (BillableReceiptItem) object;
277 if (!Objects.equals(this.getItemBrandName(), receiptItem.getItemBrandName())) {
279 } else if (!Objects.equals(this.getItemCouponNumber(), receiptItem.getItemCouponNumber())) {
281 } else if (!Objects.equals(this.getItemGrossPrice(), receiptItem.getItemGrossPrice())) {
283 } else if (!Objects.equals(this.getItemId(), receiptItem.getItemId())) {
285 } else if (!Objects.equals(this.getItemIsDiscount(), receiptItem.getItemIsDiscount())) {
287 } else if (!Objects.equals(this.getItemIsRefund(), receiptItem.getItemIsRefund())) {
289 } else if (!Objects.equals(this.getItemManufacturer(), receiptItem.getItemManufacturer())) {
291 } else if (!Objects.equals(this.getItemNumber(), receiptItem.getItemNumber())) {
293 } else if (!Objects.equals(this.getItemProduct(), receiptItem.getItemProduct())) {
295 } else if (!Objects.equals(this.getItemProductQuantity(), receiptItem.getItemProductQuantity())) {
297 } else if (!Objects.equals(this.getItemReceipt(), receiptItem.getItemReceipt())) {
299 } else if (!Objects.equals(this.getItemTaxRate(), receiptItem.getItemTaxRate())) {
307 public String getItemBrandName () {
308 return this.itemBrandName;
312 public void setItemBrandName (final String itemBrandName) {
313 this.itemBrandName = itemBrandName;
317 public String getItemCouponNumber () {
318 return this.itemCouponNumber;
322 public void setItemCouponNumber (final String itemCouponNumber) {
323 this.itemCouponNumber = itemCouponNumber;
327 @SuppressWarnings ("ReturnOfDateField")
328 public Date getItemEntryCreated () {
329 return this.itemEntryCreated;
333 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
334 public void setItemEntryCreated (final Date itemEntryCreated) {
335 this.itemEntryCreated = itemEntryCreated;
339 @SuppressWarnings ("ReturnOfDateField")
340 public Date getItemEntryUpdated () {
341 return this.itemEntryUpdated;
345 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
346 public void setItemEntryUpdated (final Date itemEntryUpdated) {
347 this.itemEntryUpdated = itemEntryUpdated;
351 public BigDecimal getItemGrossPrice () {
352 return this.itemGrossPrice;
356 public void setItemGrossPrice (final BigDecimal itemGrossPrice) {
357 this.itemGrossPrice = itemGrossPrice;
361 public Long getItemId () {
366 public void setItemId (final Long itemId) {
367 this.itemId = itemId;
371 public Boolean getItemIsDiscount () {
372 return this.itemIsDiscount;
376 public void setItemIsDiscount (final Boolean itemIsDiscount) {
377 this.itemIsDiscount = itemIsDiscount;
381 public Boolean getItemIsRefund () {
382 return this.itemIsRefund;
386 public void setItemIsRefund (final Boolean itemIsRefund) {
387 this.itemIsRefund = itemIsRefund;
391 public BasicData getItemManufacturer () {
392 return this.itemManufacturer;
396 public void setItemManufacturer (final BasicData itemManufacturer) {
397 this.itemManufacturer = itemManufacturer;
401 public BigDecimal getItemNetPrice () {
402 return this.itemNetPrice;
406 public void setItemNetPrice (final BigDecimal itemNetPrice) {
407 this.itemNetPrice = itemNetPrice;
411 public Long getItemNumber () {
412 return this.itemNumber;
416 public void setItemNumber (final Long itemNumber) {
417 this.itemNumber = itemNumber;
421 public Product getItemProduct () {
422 return this.itemProduct;
426 public void setItemProduct (final Product itemProduct) {
427 this.itemProduct = itemProduct;
431 public BigDecimal getItemProductQuantity () {
432 return this.itemProductQuantity;
436 public void setItemProductQuantity (final BigDecimal itemProductQuantity) {
437 this.itemProductQuantity = itemProductQuantity;
441 public BillableReceipt getItemReceipt () {
442 return this.itemReceipt;
446 public void setItemReceipt (final BillableReceipt itemReceipt) {
447 this.itemReceipt = itemReceipt;
451 public BigDecimal getItemTaxRate () {
452 return this.itemTaxRate;
456 public void setItemTaxRate (final BigDecimal itemTaxRate) {
457 this.itemTaxRate = itemTaxRate;
461 public int hashCode () {
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());