2 * Copyright (C) 2016 - 2018 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.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;
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"),
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")
66 @SuppressWarnings ("PersistenceUnitPresent")
67 public class FinancialReceiptItem implements BillableReceiptItem {
73 private static final long serialVersionUID = 126_498_698_378_571L;
78 @Column (name = "item_brand_name")
79 private String itemBrandName;
82 * Category being assigned to item's product
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;
89 * Item 's coupon number
91 @Column (name = "item_coupon_number")
92 private String itemCouponNumber;
95 * When this item has been created in database
97 @Basic (optional = false)
98 @Temporal (TemporalType.TIMESTAMP)
99 @Column (name = "item_created", nullable = false)
100 private Date itemCreated;
103 * Gross price of item
105 @Basic (optional = false)
106 @Column (name = "item_gross_price", nullable = false, precision = 10, scale = 2)
107 private BigDecimal itemGrossPrice;
113 @GeneratedValue (strategy = GenerationType.IDENTITY)
114 @Column (name = "item_id", nullable = false, updatable = false)
118 * Whether the item is a discount on whole receipt or single item
120 @Basic (optional = false)
121 @Column (name = "item_is_discount", nullable = false)
122 private Boolean itemIsDiscount;
125 * Whether the item is a refund
127 @Basic (optional = false)
128 @Column (name = "item_is_refund", nullable = false)
129 private Boolean itemIsRefund;
132 * Manufacturer/producer of this item
134 @JoinColumn (name = "item_manufacturer_id", referencedColumnName = "company_data_id")
135 @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
136 private BasicData itemManufacturer;
141 @Column (name = "item_net_price", precision = 10, scale = 2)
142 private BigDecimal itemNetPrice;
147 @Column (name = "item_number")
148 private Long itemNumber;
151 * Product being linked in this itemReceipt item
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;
160 @Basic (optional = false)
161 @Column (name = "item_product_quantity", nullable = false, precision = 3)
162 private BigDecimal itemProductQuantity;
165 * Connected itemReceipt item
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;
174 @Column (name = "item_tax_rate", precision = 2)
175 private BigDecimal itemTaxRate;
178 * Default constructor
180 public FinancialReceiptItem () {
184 * Constructor with product, price, quantity and receipt instance
186 * @param itemProduct Product instance
187 * @param itemProductQuantity Product quantity
188 * @param itemReceipt FinancialReceipt instance
190 public FinancialReceiptItem (final Product itemProduct, final BigDecimal itemProductQuantity, final BillableReceipt itemReceipt) {
191 // Call other constructor
195 this.itemCategory = itemProduct.getProductCategory();
196 this.itemProduct = itemProduct;
197 this.itemProductQuantity = itemProductQuantity;
198 this.itemReceipt = itemReceipt;
202 public int compareTo (final BillableReceiptItem billableReceiptItem) {
203 // Check parameter on null-reference and equality to this
204 if (null == billableReceiptItem) {
206 throw new NullPointerException("billableReceiptItem is null"); //NOI18N
207 } else if (billableReceiptItem.equals(this)) {
213 final int comparators[] = {
214 // First check brand name ...
215 StringUtils.compare(this.getItemBrandName(), billableReceiptItem.getItemBrandName()),
217 SafeNumberUtils.compare(this.getItemNumber(), billableReceiptItem.getItemNumber()),
219 StringUtils.compare(this.getItemCouponNumber(), billableReceiptItem.getItemCouponNumber()),
221 SafeNumberUtils.compare(this.getItemGrossPrice(), billableReceiptItem.getItemGrossPrice()),
223 SafeNumberUtils.compare(this.getItemNetPrice(), billableReceiptItem.getItemNetPrice()),
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()),
233 final int comparison = Comparables.checkAll(comparators);
240 public boolean equals (final Object object) {
241 if (this == object) {
243 } else if (null == object) {
245 } else if (this.getClass() != object.getClass()) {
249 final BillableReceiptItem receiptItem = (BillableReceiptItem) object;
251 if (!Objects.equals(this.getItemId(), receiptItem.getItemId())) {
253 } else if (!Objects.equals(this.getItemProduct(), receiptItem.getItemProduct())) {
255 } else if (!Objects.equals(this.getItemProductQuantity(), receiptItem.getItemProductQuantity())) {
257 } else if (!Objects.equals(this.getItemReceipt(), receiptItem.getItemReceipt())) {
265 public String getItemBrandName () {
266 return this.itemBrandName;
270 public void setItemBrandName (final String itemBrandName) {
271 this.itemBrandName = itemBrandName;
275 public Category getItemCategory () {
276 return this.itemCategory;
280 public void setItemCategory (final Category itemCategory) {
281 this.itemCategory = itemCategory;
285 public String getItemCouponNumber () {
286 return this.itemCouponNumber;
290 public void setItemCouponNumber (final String itemCouponNumber) {
291 this.itemCouponNumber = itemCouponNumber;
295 @SuppressWarnings ("ReturnOfDateField")
296 public Date getItemCreated () {
297 return this.itemCreated;
301 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
302 public void setItemCreated (final Date itemCreated) {
303 this.itemCreated = itemCreated;
307 public BigDecimal getItemGrossPrice () {
308 return this.itemGrossPrice;
312 public void setItemGrossPrice (final BigDecimal itemGrossPrice) {
313 this.itemGrossPrice = itemGrossPrice;
317 public Long getItemId () {
322 public void setItemId (final Long itemId) {
323 this.itemId = itemId;
327 public Boolean getItemIsDiscount () {
328 return this.itemIsDiscount;
332 public void setItemIsDiscount (final Boolean itemIsDiscount) {
333 this.itemIsDiscount = itemIsDiscount;
337 public Boolean getItemIsRefund () {
338 return this.itemIsRefund;
342 public void setItemIsRefund (final Boolean itemIsRefund) {
343 this.itemIsRefund = itemIsRefund;
347 public BasicData getItemManufacturer () {
348 return this.itemManufacturer;
352 public void setItemManufacturer (final BasicData itemManufacturer) {
353 this.itemManufacturer = itemManufacturer;
357 public BigDecimal getItemNetPrice () {
358 return this.itemNetPrice;
362 public void setItemNetPrice (final BigDecimal itemNetPrice) {
363 this.itemNetPrice = itemNetPrice;
367 public Long getItemNumber () {
368 return this.itemNumber;
372 public void setItemNumber (final Long itemNumber) {
373 this.itemNumber = itemNumber;
377 public Product getItemProduct () {
378 return this.itemProduct;
382 public void setItemProduct (final Product itemProduct) {
383 this.itemProduct = itemProduct;
387 public BigDecimal getItemProductQuantity () {
388 return this.itemProductQuantity;
392 public void setItemProductQuantity (final BigDecimal itemProductQuantity) {
393 this.itemProductQuantity = itemProductQuantity;
397 public BillableReceipt getItemReceipt () {
398 return this.itemReceipt;
402 public void setItemReceipt (final BillableReceipt itemReceipt) {
403 this.itemReceipt = itemReceipt;
407 public BigDecimal getItemTaxRate () {
408 return this.itemTaxRate;
412 public void setItemTaxRate (final BigDecimal itemTaxRate) {
413 this.itemTaxRate = itemTaxRate;
417 public int hashCode () {
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());