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