]> git.mxchange.org Git - jfinancials-core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Tue, 19 Sep 2017 19:06:24 +0000 (21:06 +0200)
committerRoland Häder <roland@mxchange.org>
Tue, 19 Sep 2017 19:26:50 +0000 (21:26 +0200)
- added Receipts utilities class which currently does only compare two receipt
  instances with each other: entity-equality or branch office, receipt number
  and "owning" user instance must all be the same
- added receiptNumber field
- added 2 named queries for receipts, finding all (any) and all user's receipts
- sorted members a bit

Signed-off-by: Roland Häder <roland@mxchange.org>
src/org/mxchange/jfinancials/exceptions/ReceiptAlreadyAddedException.java
src/org/mxchange/jfinancials/model/receipt/BillableReceipt.java
src/org/mxchange/jfinancials/model/receipt/FinancialReceipt.java
src/org/mxchange/jfinancials/model/receipt/Receipts.java [new file with mode: 0644]

index 3a67b83f18e4fe7d6b0a26c3a0db8b85ca6c4f68..a2ccf07b80f7a405640d0dd64dabd83da4242cea 100644 (file)
@@ -16,6 +16,9 @@
  */
 package org.mxchange.jfinancials.exceptions;
 
+import java.text.MessageFormat;
+import org.mxchange.jfinancials.model.receipt.BillableReceipt;
+
 /**
  * This exception is thrown when a receipt was already added
  * <p>
@@ -28,4 +31,21 @@ public class ReceiptAlreadyAddedException extends Exception {
         */
        private static final long serialVersionUID = 19_065_867_127_647_624L;
 
+       /**
+        * Default constructor
+        */
+       public ReceiptAlreadyAddedException () {
+               super();
+       }
+
+       /**
+        * Constructor with receipt instance
+        * <p>
+        * @param receipt Receipt instance
+        */
+       public ReceiptAlreadyAddedException (final BillableReceipt receipt) {
+               // Call super constructor
+               super(MessageFormat.format("Receipt with id {0} and number {1} has already been added.", receipt.getReceiptId(), receipt.getReceiptNumber()));
+       }
+
 }
index 04baca4f0ab9d235abc90fd1048285f1571881a2..06b33ff7f82efea74a1ab8744fd176d09221c60a 100644 (file)
@@ -18,7 +18,7 @@ package org.mxchange.jfinancials.model.receipt;
 
 import java.io.Serializable;
 import java.util.Calendar;
-import org.mxchange.jcontactsbusiness.basicdata.BusinessBasicData;
+import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
 import org.mxchange.jproduct.model.payment.PaymentType;
 import org.mxchange.jusercore.model.user.User;
 
@@ -72,6 +72,20 @@ public interface BillableReceipt extends Serializable {
        void setReceiptId (final Long receiptId);
 
        /**
+        * Getter for receipt number
+        * <p>
+        * @return Receipt number
+        */
+       Long getReceiptNumber ();
+
+       /**
+        * Setter for receipt number
+        * <p>
+        * @param receiptNumber Receipt number
+        */
+       void setReceiptNumber (final Long receiptNumber);
+
+               /**
         * Getter for receipt's user
         * <p>
         * @return Receipt's user
@@ -86,18 +100,18 @@ public interface BillableReceipt extends Serializable {
        void setReceiptUser (final User receiptUser);
 
        /**
-        * Getter for seller instance
+        * Getter for branch office instance
         * <p>
-        * @return Seller instance
+        * @return Branch office instance
         */
-       BusinessBasicData getReceiptSeller ();
+       BranchOffice getReceiptBranchOffice ();
 
        /**
-        * Setter for seller instance
+        * Setter for branch office instance
         * <p>
-        * @param receiptSeller Seller instance
+        * @param receiptBranch Branch office instance
         */
-       void setReceiptSeller (final BusinessBasicData receiptSeller);
+       void setReceiptBranchOffice (final BranchOffice receiptBranch);
 
        /**
         * Getter for payment type
index 83aebc7798b4f1379f6be903bd7c040e936ae3a9..9a9685fa812a2dddd94b4212c232b585bcaf7a84 100644 (file)
@@ -28,13 +28,15 @@ import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
 import javax.persistence.Transient;
-import org.mxchange.jcontactsbusiness.basicdata.BusinessBasicData;
-import org.mxchange.jcontactsbusiness.basicdata.CompanyBasicData;
+import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
+import org.mxchange.jcontactsbusiness.model.branchoffice.CompanyBranchOffice;
 import org.mxchange.jproduct.model.payment.PaymentType;
 import org.mxchange.jusercore.model.user.LoginUser;
 import org.mxchange.jusercore.model.user.User;
@@ -47,6 +49,12 @@ import org.mxchange.jusercore.model.user.User;
 @Table (
                name = "receipts"
 )
+@NamedQueries (
+               {
+                       @NamedQuery (name = "AllReceipts", query = "SELECT r FROM receipts AS r ORDER BY r.receiptId ASC"),
+                       @NamedQuery (name = "SearchAllUserReceipts", query = "SELECT r FROM receipts AS r WHERE r.receiptUser = :receiptUser ORDER BY r.receiptId ASC")
+               }
+)
 @SuppressWarnings ("PersistenceUnitPresent")
 public class FinancialReceipt implements BillableReceipt {
 
@@ -56,6 +64,13 @@ public class FinancialReceipt implements BillableReceipt {
        @Transient
        private static final long serialVersionUID = 185_867_217_461L;
 
+       /**
+        * Seller instance
+        */
+       @JoinColumn (name = "receipt_branch_id", referencedColumnName = "branch_id", nullable = false, updatable = false)
+       @OneToOne (targetEntity = CompanyBranchOffice.class, cascade = CascadeType.REFRESH, optional = false)
+       private BranchOffice receiptBranchOffice;
+
        /**
         * When this receipt entry has been created
         */
@@ -80,6 +95,12 @@ public class FinancialReceipt implements BillableReceipt {
        @Column (name = "receipt_issued", nullable = false)
        private Calendar receiptIssued;
 
+       /**
+        * Receipt number
+        */
+       @Column (name = "receipt_number")
+       private Long receiptNumber;
+
        /**
         * Payment type (cash, credit card, EC card ...)
         */
@@ -88,18 +109,11 @@ public class FinancialReceipt implements BillableReceipt {
        @Enumerated (EnumType.STRING)
        private PaymentType receiptPaymentType;
 
-       /**
-        * Seller instance
-        */
-       @JoinColumn (name = "receipt_seller_id", referencedColumnName = "business_data_id", nullable = false, updatable = false)
-       @OneToOne (targetEntity = CompanyBasicData.class, cascade = CascadeType.REFRESH, optional = false)
-       private BusinessBasicData receiptSeller;
-
        /**
         * Which user this receipt belongs to
         */
-       @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id", nullable = false, updatable = false)
-       @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
+       @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id")
+       @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
        private User receiptUser;
 
        /**
@@ -112,17 +126,17 @@ public class FinancialReceipt implements BillableReceipt {
         * Constructor with payment type, seller and user
         * <p>
         * @param receiptPaymentType Payment type
-        * @param receiptSeller      Seller instance
+        * @param branchOffice       Branch office instance
         * @param receiptUser        User instance
         * @param receiptIssued      When this receipt has been issued
         */
-       public FinancialReceipt (final PaymentType receiptPaymentType, final BusinessBasicData receiptSeller, final User receiptUser, final Calendar receiptIssued) {
+       public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice branchOffice, final User receiptUser, final Calendar receiptIssued) {
                // Call other constructor first
                this();
 
                // Set all values
                this.receiptPaymentType = receiptPaymentType;
-               this.receiptSeller = receiptSeller;
+               this.receiptBranchOffice = branchOffice;
                this.receiptUser = receiptUser;
                this.receiptIssued = receiptIssued;
        }
@@ -142,9 +156,11 @@ public class FinancialReceipt implements BillableReceipt {
 
                if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) {
                        return false;
+               } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) {
+                       return false;
                } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) {
                        return false;
-               } else if (!Objects.equals(this.getReceiptSeller(), receipt.getReceiptSeller())) {
+               } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) {
                        return false;
                } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) {
                        return false;
@@ -155,6 +171,16 @@ public class FinancialReceipt implements BillableReceipt {
                return true;
        }
 
+       @Override
+       public BranchOffice getReceiptBranchOffice () {
+               return this.receiptBranchOffice;
+       }
+
+       @Override
+       public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
+               this.receiptBranchOffice = receiptBranchOffice;
+       }
+
        @Override
        @SuppressWarnings ("ReturnOfDateField")
        public Calendar getReceiptCreated () {
@@ -190,23 +216,23 @@ public class FinancialReceipt implements BillableReceipt {
        }
 
        @Override
-       public PaymentType getReceiptPaymentType () {
-               return this.receiptPaymentType;
+       public Long getReceiptNumber () {
+               return this.receiptNumber;
        }
 
        @Override
-       public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
-               this.receiptPaymentType = receiptPaymentType;
+       public void setReceiptNumber (final Long receiptNumber) {
+               this.receiptNumber = receiptNumber;
        }
 
        @Override
-       public BusinessBasicData getReceiptSeller () {
-               return this.receiptSeller;
+       public PaymentType getReceiptPaymentType () {
+               return this.receiptPaymentType;
        }
 
        @Override
-       public void setReceiptSeller (final BusinessBasicData receiptSeller) {
-               this.receiptSeller = receiptSeller;
+       public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
+               this.receiptPaymentType = receiptPaymentType;
        }
 
        @Override
@@ -224,8 +250,9 @@ public class FinancialReceipt implements BillableReceipt {
                int hash = 5;
 
                hash = 89 * hash + Objects.hashCode(this.getReceiptId());
+               hash = 89 * hash + Objects.hashCode(this.getReceiptNumber());
                hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType());
-               hash = 89 * hash + Objects.hashCode(this.getReceiptSeller());
+               hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice());
                hash = 89 * hash + Objects.hashCode(this.getReceiptUser());
 
                return hash;
diff --git a/src/org/mxchange/jfinancials/model/receipt/Receipts.java b/src/org/mxchange/jfinancials/model/receipt/Receipts.java
new file mode 100644 (file)
index 0000000..7c00650
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2017 Roland Häder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jfinancials.model.receipt;
+
+import java.io.Serializable;
+import java.text.MessageFormat;
+import java.util.Objects;
+
+/**
+ * A utilities class for receipts
+ *
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class Receipts implements Serializable {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 2_867_938_676_165_401L;
+
+       public static boolean isSameReceipt (final BillableReceipt receipt1, final BillableReceipt receipt2) {
+               // Pre-compare both as entities (same id)
+               if (Objects.equals(receipt1, receipt2)) {
+                       // Same entity (with id number)
+                       return true;
+               }
+
+               // Validate parameter
+               if (null == receipt1) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt1 is null");
+               } else if ((receipt1.getReceiptId() instanceof Long) && (receipt1.getReceiptId() < 1)) {
+                       // Throw IAE
+                       throw new IllegalArgumentException(MessageFormat.format("receipt1.receiptId={0} is not valid.", receipt1.getReceiptId()));
+               } else if (receipt1.getReceiptNumber() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt1.receiptNumber is null");
+               } else if (receipt1.getReceiptNumber() < 1) {
+                       // Throw IAE
+                       throw new IllegalArgumentException(MessageFormat.format("receipt1.receiptNumber={0} is not valid", receipt1.getReceiptNumber()));
+               } else if (receipt1.getReceiptBranchOffice() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt1.receiptBranchOffice is null");
+               } else if (receipt1.getReceiptBranchOffice().getBranchId() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt1.receiptBranchOffice.branchId is null");
+               } else if (receipt1.getReceiptBranchOffice().getBranchId() < 1) {
+                       // Throw NPE
+                       throw new NullPointerException(MessageFormat.format("receipt1.receiptBranchOffice.branchId={0} is not valid", receipt1.getReceiptBranchOffice().getBranchId()));
+               } else if (receipt1.getReceiptPaymentType()== null) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt1.receiptPaymentType is null");
+               } else if (null == receipt2) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt2 is null");
+               } else if ((receipt2.getReceiptId() instanceof Long) && (receipt2.getReceiptId() < 1)) {
+                       // Throw IAE
+                       throw new IllegalArgumentException(MessageFormat.format("receipt2.receiptId={0} is not valid.", receipt2.getReceiptId()));
+               } else if (receipt2.getReceiptNumber() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt2.receiptNumber is null");
+               } else if (receipt2.getReceiptNumber() < 1) {
+                       // Throw IAE
+                       throw new IllegalArgumentException(MessageFormat.format("receipt2.receiptNumber={0} is not valid", receipt2.getReceiptNumber()));
+               } else if (receipt2.getReceiptBranchOffice() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt2.receiptBranchOffice is null");
+               } else if (receipt2.getReceiptBranchOffice().getBranchId() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt2.receiptBranchOffice.branchId is null");
+               } else if (receipt2.getReceiptBranchOffice().getBranchId() < 1) {
+                       // Throw NPE
+                       throw new NullPointerException(MessageFormat.format("receipt2.receiptBranchOffice.branchId={0} is not valid", receipt2.getReceiptBranchOffice().getBranchId()));
+               } else if (receipt2.getReceiptPaymentType()== null) {
+                       // Throw NPE
+                       throw new NullPointerException("receipt2.receiptPaymentType is null");
+               }
+
+               // Now check all individually
+               if (!Objects.equals(receipt1.getReceiptBranchOffice(), receipt2.getReceiptBranchOffice())) {
+                       // Other branch offices
+                       return false;
+               } else if (!Objects.equals(receipt1.getReceiptNumber(), receipt2.getReceiptNumber())) {
+                       // Other receipt number
+                       return false;
+               } else if (!Objects.equals(receipt1.getReceiptUser(), receipt2.getReceiptUser())) {
+                       // Other user (unlikely to happen
+                       return false;
+               }
+
+               // Maybe same receipt!
+               return true;
+       }
+
+       /**
+        * Private default constructor
+        */
+       private Receipts () {
+               // Utilities don't have instances
+       }
+
+}