import java.text.MessageFormat;
import java.util.Date;
-import java.util.List;
+import java.util.Objects;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
} else if (receiptItem.getItemReceipt().getReceiptId() < 1) {
// Throw NPE
throw new NullPointerException(MessageFormat.format("receiptItem.itemReceipt.receiptId={0} is invalid.", receiptItem.getItemReceipt().getReceiptId())); //NOI18N
- } else if (this.isReceiptItemRegistered(receiptItem)) {
+ } else if (this.isReceiptItemRegistered(receiptItem, false, false)) {
// Is already registered
throw new ReceiptItemAlreadyAddedException(receiptItem);
}
} else if (receiptItem.getItemId() < 1) {
// Throw IAE
throw new IllegalArgumentException(MessageFormat.format("receiptItem.itemId={0} is invalid", receiptItem.getItemId())); //NOI18N
- } else if (!this.isReceiptItemRegistered(receiptItem)) {
- // Not found
+ } else if (!this.isReceiptItemRegistered(receiptItem, true, true)) {
+ // No discount and not found
throw new ReceiptItemNotFoundException(receiptItem);
}
/**
* Checks if given receipt item has already been added to receipt.
* <p>
- * @param receiptItem Receipt item
+ * @param receiptItem Receipt item
+ * @param checkDiscountRefund Whether to check discounts and refunds
+ * @param checkOnlyPrimaryKey Whether to check only primary key
* <p>
* @return Whether item has already been added to receipt item's receipt
*/
- private boolean isReceiptItemRegistered (final BillableReceiptItem receiptItem) {
+ private boolean isReceiptItemRegistered (final BillableReceiptItem receiptItem, final boolean checkDiscountRefund, final boolean checkOnlyPrimaryKey) {
// Default is not found
boolean isFound = false;
// Is the item type a discount?
- if (receiptItem.getItemIsDiscount()) {
+ if (!checkDiscountRefund && receiptItem.getItemIsDiscount()) {
// Yes, then allow it multiple items
return false;
}
* Get all receipt's receipt items. No need to look global as people may
* buy the same item again and again ... ;-)
*/
- final List<BillableReceiptItem> receiptItems = this.receiptItemBean.fetchReceiptItemsByReceipt(receiptItem.getItemReceipt());
-
- // Loop through all entries
- for (final BillableReceiptItem item : receiptItems) {
+ for (final BillableReceiptItem currentReceiptItem : this.receiptItemBean.fetchReceiptItemsByReceipt(receiptItem.getItemReceipt())) {
// Is it the same item?
- if (ReceiptItems.isSameReceiptItem(item, receiptItem) && (!item.getItemIsRefund())) {
+ if (((checkOnlyPrimaryKey && Objects.equals(currentReceiptItem.getItemId(), receiptItem.getItemId())) || ReceiptItems.isSameReceiptItem(currentReceiptItem, receiptItem)) && (checkDiscountRefund || !currentReceiptItem.getItemIsRefund())) {
// Found it
isFound = true;
break;