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.jfinancials.model.receipt.Receipts;
45 import org.mxchange.jproduct.model.category.Categories;
46 import org.mxchange.jproduct.model.category.Category;
47 import org.mxchange.jproduct.model.category.ProductCategory;
48 import org.mxchange.jproduct.model.product.GenericProduct;
49 import org.mxchange.jproduct.model.product.Product;
50 import org.mxchange.jproduct.model.product.Products;
53 * A POJO for receipt items
55 * @author Roland Häder<roland@mxchange.org>
57 @Entity (name = "receipt_items")
59 name = "receipt_items"
63 @NamedQuery (name = "AllReceiptItems", query = "SELECT ri FROM receipt_items AS ri ORDER BY ri.itemId ASC"),
64 @NamedQuery (name = "SearchAssignedReceiptItems", query = "SELECT ri FROM receipt_items AS ri WHERE ri.itemReceipt = :itemReceipt ORDER BY ri.itemId ASC"),
65 @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")
68 @SuppressWarnings ("PersistenceUnitPresent")
69 public class FinancialReceiptItem implements BillableReceiptItem {
75 private static final long serialVersionUID = 126_498_698_378_571L;
80 @Column (name = "item_brand_name")
81 private String itemBrandName;
84 * Category being assigned to item's product
86 @JoinColumn (name = "item_category_id", referencedColumnName = "category_id", updatable = false)
87 @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH, optional = false)
88 private Category itemCategory;
91 * Item 's coupon number
93 @Column (name = "item_coupon_number")
94 private String itemCouponNumber;
97 * When this item has been created in database
99 @Basic (optional = false)
100 @Temporal (TemporalType.TIMESTAMP)
101 @Column (name = "item_created", nullable = false)
102 private Date itemCreated;
105 * Gross price of item
107 @Basic (optional = false)
108 @Column (name = "item_gross_price", nullable = false, precision = 10, scale = 2)
109 private BigDecimal itemGrossPrice;
115 @GeneratedValue (strategy = GenerationType.IDENTITY)
116 @Column (name = "item_id", nullable = false, updatable = false)
120 * Whether the item is a discount on whole receipt or single item
122 @Basic (optional = false)
123 @Column (name = "item_is_discount", nullable = false)
124 private Boolean itemIsDiscount;
127 * Whether the item is a refund
129 @Basic (optional = false)
130 @Column (name = "item_is_refund", nullable = false)
131 private Boolean itemIsRefund;
134 * Manufacturer/producer of this item
136 @JoinColumn (name = "item_manufacturer_id", referencedColumnName = "company_data_id")
137 @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
138 private BasicData itemManufacturer;
143 @Column (name = "item_net_price", precision = 10, scale = 2)
144 private BigDecimal itemNetPrice;
149 @Column (name = "item_number")
150 private Long itemNumber;
153 * Product being linked in this itemReceipt item
155 @JoinColumn (name = "item_product_id", referencedColumnName = "product_id", nullable = false, updatable = false)
156 @OneToOne (targetEntity = GenericProduct.class, cascade = CascadeType.REFRESH, optional = false)
157 private Product itemProduct;
162 @Basic (optional = false)
163 @Column (name = "item_product_quantity", nullable = false, precision = 3)
164 private BigDecimal itemProductQuantity;
167 * Connected itemReceipt item
169 @JoinColumn (name = "item_receipt_id", referencedColumnName = "receipt_id", nullable = false, updatable = false)
170 @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH, optional = false)
171 private BillableReceipt itemReceipt;
176 @Column (name = "item_tax_rate", precision = 2)
177 private BigDecimal itemTaxRate;
180 * Default constructor
182 public FinancialReceiptItem () {
186 * Constructor with product, price, quantity and receipt instance
188 * @param itemProduct Product instance
189 * @param itemProductQuantity Product quantity
190 * @param itemReceipt FinancialReceipt instance
192 public FinancialReceiptItem (final Product itemProduct, final BigDecimal itemProductQuantity, final BillableReceipt itemReceipt) {
193 // Call other constructor
197 this.itemCategory = itemProduct.getProductCategory();
198 this.itemProduct = itemProduct;
199 this.itemProductQuantity = itemProductQuantity;
200 this.itemReceipt = itemReceipt;
204 public int compareTo (final BillableReceiptItem billableReceiptItem) {
205 // Check parameter on null-reference and equality to this
206 if (null == billableReceiptItem) {
208 throw new NullPointerException("billableReceiptItem is null"); //NOI18N
209 } else if (billableReceiptItem.equals(this)) {
215 final int comparators[] = {
216 // First check brand name ...
217 StringUtils.compare(this.getItemBrandName(), billableReceiptItem.getItemBrandName()),
219 SafeNumberUtils.compare(this.getItemNumber(), billableReceiptItem.getItemNumber()),
221 StringUtils.compare(this.getItemCouponNumber(), billableReceiptItem.getItemCouponNumber()),
223 SafeNumberUtils.compare(this.getItemGrossPrice(), billableReceiptItem.getItemGrossPrice()),
225 SafeNumberUtils.compare(this.getItemNetPrice(), billableReceiptItem.getItemNetPrice()),
227 SafeNumberUtils.compare(this.getItemTaxRate(), billableReceiptItem.getItemTaxRate()),
228 // ... product quanity
229 this.getItemProductQuantity().compareTo(billableReceiptItem.getItemProductQuantity()),
230 // ... product instance
231 Products.compare(this.getItemProduct(), billableReceiptItem.getItemProduct()),
232 // ... category instance
233 Categories.compare(this.getItemCategory(), billableReceiptItem.getItemCategory()),
234 // and finally receipt instance
235 Receipts.compare(this.getItemReceipt(), billableReceiptItem.getItemReceipt())
239 final int comparison = Comparables.checkAll(comparators);
246 public boolean equals (final Object object) {
247 if (this == object) {
249 } else if (null == object) {
251 } else if (this.getClass() != object.getClass()) {
255 final BillableReceiptItem receiptItem = (BillableReceiptItem) object;
257 if (!Objects.equals(this.getItemId(), receiptItem.getItemId())) {
259 } else if (!Objects.equals(this.getItemNumber(), receiptItem.getItemNumber())) {
261 } else if (!Objects.equals(this.getItemProduct(), receiptItem.getItemProduct())) {
263 } else if (!Objects.equals(this.getItemProductQuantity(), receiptItem.getItemProductQuantity())) {
265 } else if (!Objects.equals(this.getItemReceipt(), receiptItem.getItemReceipt())) {
267 } else if (!Objects.equals(this.getItemCouponNumber(), receiptItem.getItemCouponNumber())) {
275 public String getItemBrandName () {
276 return this.itemBrandName;
280 public void setItemBrandName (final String itemBrandName) {
281 this.itemBrandName = itemBrandName;
285 public Category getItemCategory () {
286 return this.itemCategory;
290 public void setItemCategory (final Category itemCategory) {
291 this.itemCategory = itemCategory;
295 public String getItemCouponNumber () {
296 return this.itemCouponNumber;
300 public void setItemCouponNumber (final String itemCouponNumber) {
301 this.itemCouponNumber = itemCouponNumber;
305 @SuppressWarnings ("ReturnOfDateField")
306 public Date getItemCreated () {
307 return this.itemCreated;
311 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
312 public void setItemCreated (final Date itemCreated) {
313 this.itemCreated = itemCreated;
317 public BigDecimal getItemGrossPrice () {
318 return this.itemGrossPrice;
322 public void setItemGrossPrice (final BigDecimal itemGrossPrice) {
323 this.itemGrossPrice = itemGrossPrice;
327 public Long getItemId () {
332 public void setItemId (final Long itemId) {
333 this.itemId = itemId;
337 public Boolean getItemIsDiscount () {
338 return this.itemIsDiscount;
342 public void setItemIsDiscount (final Boolean itemIsDiscount) {
343 this.itemIsDiscount = itemIsDiscount;
347 public Boolean getItemIsRefund () {
348 return this.itemIsRefund;
352 public void setItemIsRefund (final Boolean itemIsRefund) {
353 this.itemIsRefund = itemIsRefund;
357 public BasicData getItemManufacturer () {
358 return this.itemManufacturer;
362 public void setItemManufacturer (final BasicData itemManufacturer) {
363 this.itemManufacturer = itemManufacturer;
367 public BigDecimal getItemNetPrice () {
368 return this.itemNetPrice;
372 public void setItemNetPrice (final BigDecimal itemNetPrice) {
373 this.itemNetPrice = itemNetPrice;
377 public Long getItemNumber () {
378 return this.itemNumber;
382 public void setItemNumber (final Long itemNumber) {
383 this.itemNumber = itemNumber;
387 public Product getItemProduct () {
388 return this.itemProduct;
392 public void setItemProduct (final Product itemProduct) {
393 this.itemProduct = itemProduct;
397 public BigDecimal getItemProductQuantity () {
398 return this.itemProductQuantity;
402 public void setItemProductQuantity (final BigDecimal itemProductQuantity) {
403 this.itemProductQuantity = itemProductQuantity;
407 public BillableReceipt getItemReceipt () {
408 return this.itemReceipt;
412 public void setItemReceipt (final BillableReceipt itemReceipt) {
413 this.itemReceipt = itemReceipt;
417 public BigDecimal getItemTaxRate () {
418 return this.itemTaxRate;
422 public void setItemTaxRate (final BigDecimal itemTaxRate) {
423 this.itemTaxRate = itemTaxRate;
427 public int hashCode () {
430 hash = 53 * hash + Objects.hashCode(this.getItemId());
431 hash = 53 * hash + Objects.hashCode(this.getItemNumber());
432 hash = 53 * hash + Objects.hashCode(this.getItemProduct());
433 hash = 53 * hash + Objects.hashCode(this.getItemProductQuantity());
434 hash = 53 * hash + Objects.hashCode(this.getItemReceipt());
435 hash = 53 * hash + Objects.hashCode(this.getItemCouponNumber());