]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/utils/ReceiptItemUtils.java
f3da63370b284a8a54926f57ef24cd6093272c15
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / utils / ReceiptItemUtils.java
1 /*
2  * Copyright (C) 2017 - 2022 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 Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jfinancials.model.utils;
18
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import java.util.Objects;
22 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
23
24 /**
25  * A utilities class for receipt items
26  *
27  * @author Roland Häder<roland@mxchange.org>
28  */
29 public class ReceiptItemUtils implements Serializable {
30
31         /**
32          * Serial number
33          */
34         private static final long serialVersionUID = 2_867_938_676_165_402L;
35
36         /**
37          * Copies all fields from source entity to target entity. An exception might
38          * be thrown when both instances are the same.
39          * <p>
40          * @param sourceReceiptItem Source receipt item instance
41          * @param targetReceiptItem Target receipt item instance
42          */
43         public static void copyReceiptItemData (final BillableReceiptItem sourceReceiptItem, final BillableReceiptItem targetReceiptItem) {
44                 // Check parameter
45                 if (null == sourceReceiptItem) {
46                         // Throw NPE
47                         throw new NullPointerException("sourceReceiptItem is null"); //NOI18N
48                 } else if (null == targetReceiptItem) {
49                         // Throw NPE
50                         throw new NullPointerException("targetReceiptItem is null"); //NOI18N
51                 } else if (Objects.equals(sourceReceiptItem, targetReceiptItem)) {
52                         // Throw IAE
53                         throw new IllegalArgumentException("sourceReceiptItem and targetReceiptItem are the same."); //NOI18N
54                 }
55
56                 // Copy all fields
57                 targetReceiptItem.setItemBrandName(sourceReceiptItem.getItemBrandName());
58                 targetReceiptItem.setItemCouponNumber(sourceReceiptItem.getItemCouponNumber());
59                 targetReceiptItem.setItemGrossPrice(sourceReceiptItem.getItemGrossPrice());
60                 targetReceiptItem.setItemId(sourceReceiptItem.getItemId());
61                 targetReceiptItem.setItemIsDiscount(sourceReceiptItem.getItemIsDiscount());
62                 targetReceiptItem.setItemIsRefund(sourceReceiptItem.getItemIsRefund());
63                 targetReceiptItem.setItemManufacturer(sourceReceiptItem.getItemManufacturer());
64                 targetReceiptItem.setItemNetPrice(sourceReceiptItem.getItemNetPrice());
65                 targetReceiptItem.setItemNumber(sourceReceiptItem.getItemNumber());
66                 targetReceiptItem.setItemProduct(sourceReceiptItem.getItemProduct());
67                 targetReceiptItem.setItemProductQuantity(sourceReceiptItem.getItemProductQuantity());
68                 targetReceiptItem.setItemReceipt(sourceReceiptItem.getItemReceipt());
69                 targetReceiptItem.setItemTaxRate(sourceReceiptItem.getItemTaxRate());
70         }
71
72         /**
73          * Checks if both receipt items are the same by assigned receipt, product
74          * and quantity.
75          * <p>
76          * @param receiptItem1 Receipt item 1
77          * @param receiptItem2 Receipt item 2
78          * <p>
79          * @return Whether both are equal
80          */
81         public static boolean isSameReceiptItem (final BillableReceiptItem receiptItem1, final BillableReceiptItem receiptItem2) {
82                 // Pre-compare both as entities (same id)
83                 if (Objects.equals(receiptItem1, receiptItem2)) {
84                         // Same entity (with id number)
85                         return true;
86                 }
87
88                 // Validate parameter
89                 if (null == receiptItem1) {
90                         // Throw NPE
91                         throw new NullPointerException("receiptItem1 is null"); //NOI18N
92                 } else if ((receiptItem1.getItemId() instanceof Long) && (receiptItem1.getItemId() < 1)) {
93                         // Throw IAE
94                         throw new IllegalArgumentException(MessageFormat.format("receiptItem1.itemId={0} is not valid.", receiptItem1.getItemId())); //NOI18N
95                 } else if (receiptItem1.getItemReceipt() == null) {
96                         // Throw NPE
97                         throw new NullPointerException("receiptItem1.itemReceipt is null"); //NOI18N
98                 } else if (receiptItem1.getItemReceipt().getReceiptId() == null) {
99                         // Throw NPE
100                         throw new NullPointerException("receiptItem1.itemReceipt.receiptId is null"); //NOI18N
101                 } else if (receiptItem1.getItemReceipt().getReceiptId() < 1) {
102                         // Throw NPE
103                         throw new NullPointerException(MessageFormat.format("receiptItem1.itemReceipt.receiptId={0} is not valid", receiptItem1.getItemReceipt().getReceiptId())); //NOI18N
104                 } else if (receiptItem1.getItemProduct() == null) {
105                         // Throw NPE again
106                         throw new NullPointerException("receiptItem1.itemProduct is null."); //NOI18N
107                 } else if (receiptItem1.getItemProduct().getProductId() == null) {
108                         // Throw NPE again
109                         throw new NullPointerException("receiptItem1.itemProduct.productId is null."); //NOI18N
110                 } else if (receiptItem1.getItemProduct().getProductId() < 1) {
111                         // Throw IAE
112                         throw new IllegalArgumentException(MessageFormat.format("receiptItem1.itemProduct.productId={0} is invalid.", receiptItem1.getItemProduct().getProductId())); //NOI18N
113                 } else if (null == receiptItem2) {
114                         // Throw NPE
115                         throw new NullPointerException("receiptItem2 is null"); //NOI18N
116                 } else if ((receiptItem2.getItemId() instanceof Long) && (receiptItem2.getItemId() < 1)) {
117                         // Throw IAE
118                         throw new IllegalArgumentException(MessageFormat.format("receiptItem2.itemId={0} is not valid.", receiptItem2.getItemId())); //NOI18N
119                 } else if (receiptItem2.getItemReceipt() == null) {
120                         // Throw NPE
121                         throw new NullPointerException("receiptItem2.itemReceipt is null"); //NOI18N
122                 } else if (receiptItem2.getItemReceipt().getReceiptId() == null) {
123                         // Throw NPE
124                         throw new NullPointerException("receiptItem2.itemReceipt.receiptId is null"); //NOI18N
125                 } else if (receiptItem2.getItemReceipt().getReceiptId() < 1) {
126                         // Throw NPE
127                         throw new NullPointerException(MessageFormat.format("receiptItem2.itemReceipt.receiptId={0} is not valid", receiptItem2.getItemReceipt().getReceiptId())); //NOI18N
128                 } else if (receiptItem2.getItemProduct() == null) {
129                         // Throw NPE again
130                         throw new NullPointerException("receiptItem2.itemProduct is null."); //NOI18N
131                 } else if (receiptItem2.getItemProduct().getProductId() == null) {
132                         // Throw NPE again
133                         throw new NullPointerException("receiptItem2.itemProduct.productId is null."); //NOI18N
134                 } else if (receiptItem2.getItemProduct().getProductId() < 1) {
135                         // Throw IAE
136                         throw new IllegalArgumentException(MessageFormat.format("receiptItem2.itemProduct.productId={0} is invalid.", receiptItem2.getItemProduct().getProductId())); //NOI18N
137                 } else if (null == receiptItem2) {
138                         // Throw NPE
139                         throw new NullPointerException("receiptItem2 is null"); //NOI18N
140                 }
141
142                 // Now check all individually
143                 if (!Objects.equals(receiptItem1.getItemReceipt(), receiptItem2.getItemReceipt())) {
144                         // Other item receipt
145                         return false;
146                 } else if (!Objects.equals(receiptItem1.getItemProduct(), receiptItem2.getItemProduct())) {
147                         // Other item product
148                         return false;
149                 } else if (!Objects.equals(receiptItem1.getItemProductQuantity(), receiptItem2.getItemProductQuantity())) {
150                         // Other item quantity
151                         return false;
152                 } else if (!Objects.equals(receiptItem1.getItemNumber(), receiptItem2.getItemNumber())) {
153                         // Other item number
154                         return false;
155                 }
156
157                 // Maybe same receipt!
158                 return true;
159         }
160
161         /**
162          * Private default constructor
163          */
164         private ReceiptItemUtils () {
165                 // Utilities don't have instances
166         }
167
168 }