From 38235bd7d1d1ac1ce73c49cf271223dbe36c22fe Mon Sep 17 00:00:00 2001
From: Michael Hoennig <michael@hoennig.de>
Date: Sat, 29 Oct 2022 18:18:19 +0200
Subject: [PATCH] import bank-accounts + sepa-mandates

---
 src/test/resources/migration/sepa-mandates.csv                                          |    2 
 src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java |   61 +++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 4 deletions(-)

diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java
index 3fd284f..09ec454 100644
--- a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java
+++ b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java
@@ -5,6 +5,7 @@
 import com.opencsv.CSVReaderBuilder;
 import net.hostsharing.hsadminng.context.Context;
 import net.hostsharing.hsadminng.context.ContextBasedTest;
+import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountEntity;
 import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
 import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
 import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipEntity;
@@ -13,8 +14,12 @@
 import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
 import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
 import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonType;
+import net.hostsharing.hsadminng.hs.office.sepamandate.HsOfficeSepaMandateEntity;
 import net.hostsharing.test.JpaAttempt;
-import org.junit.jupiter.api.*;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
 import org.springframework.boot.test.mock.mockito.MockBean;
@@ -48,6 +53,8 @@
     private static Map<Integer, HsOfficePartnerEntity> partners = new HashMap<>();
     private static Map<Integer, HsOfficeDebitorEntity> debitors = new HashMap<>();
     private static Map<Integer, HsOfficeMembershipEntity> memberships = new HashMap<>();
+    private static Map<Integer, HsOfficeSepaMandateEntity> sepaMandates = new HashMap<>();
+    private static Map<Integer, HsOfficeBankAccountEntity> bankAccounts = new HashMap<>();
 
     @PersistenceContext
     EntityManager em;
@@ -83,9 +90,21 @@
     }
 
     @Test
+    @Order(3)
+    void importSepaMandates() {
+
+        try (Reader reader = resourceReader("migration/sepa-mandates.csv")) {
+            final var records = readAllLines(reader);
+            importSepaMandates(records);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Test
     @Order(10)
     void persistEntities() {
-        jpaAttempt.transacted( () -> {
+        jpaAttempt.transacted(() -> {
             context("superuser-alex@hostsharing.net"); // TODO: use real user
 
             contacts.forEach((id, contact) -> em.persist(contact));
@@ -93,6 +112,8 @@
             partners.forEach((id, partner) -> em.persist(partner));
             debitors.forEach((id, debitor) -> em.persist(debitor));
             memberships.forEach((id, membership) -> em.persist(membership));
+            bankAccounts.forEach((id, account) -> em.persist(account));
+            sepaMandates.forEach((id, mandate) -> em.persist(mandate));
         }).assertSuccessful();
     }
 
@@ -158,12 +179,39 @@
                 });
     }
 
+    private void importSepaMandates(final List<String[]> records) {
+        records.stream()
+                .map(this::trimAll)
+                .forEach(record -> {
+
+                    final var debitor = debitors.get(toInt(record[1]));
+
+                    final var sepaMandate = HsOfficeSepaMandateEntity.builder()
+                            .debitor(debitor)
+                            .bankAccount(HsOfficeBankAccountEntity.builder()
+                                    .holder(record[2])
+                                    // .bankName(record[3]) // TODO
+                                    .iban(record[4])
+                                    .bic(record[5])
+                                    .build())
+                            .reference(record[6])
+                            .agreement(LocalDate.parse(record[7]))
+                            .validity(toPostgresDateRange(
+                                    toLocalDate(record[8]),
+                                    toLocalDate(record[9])))
+                            .build();
+
+                    sepaMandates.put(toInt(record[0]), sepaMandate);
+                    bankAccounts.put(toInt(record[0]), sepaMandate.getBankAccount());
+                });
+    }
+
     private void importContacts(final List<String[]> records) {
         records.stream()
                 .map(this::trimAll)
                 .forEach(record -> {
 
-                    if ( isNotBlank(record[17]) &&  record[17].contains("billing") ) {
+                    if (isNotBlank(record[17]) && record[17].contains("billing")) {
 
                         final var partner = partners.get(toInt(record[1]));
 
@@ -266,6 +314,13 @@
         return toLabel(salut, title, firstname, lastname, null);
     }
 
+    private LocalDate toLocalDate(final String dateString) {
+        if (isNotBlank(dateString)) {
+            return LocalDate.parse(dateString);
+        }
+        return null;
+    }
+
     private static Integer toInt(final String value) {
         return isNotBlank(value) ? Integer.parseInt(value.trim()) : 0;
     }
diff --git a/src/test/resources/migration/sepa-mandates.csv b/src/test/resources/migration/sepa-mandates.csv
index a97a875..9ee6720 100644
--- a/src/test/resources/migration/sepa-mandates.csv
+++ b/src/test/resources/migration/sepa-mandates.csv
@@ -1,3 +1,3 @@
 sepa_mandat_id;	bp_id;	bank_customer;	bank_name;	bank_iban;		bank_bic;	mandat_ref;	mandat_signed;	mandat_since;	mandat_until;	mandat_used
 234234;		7;	Michael Mellies;	ING Bank AG;	DE37500105177419788228;	INGDDEFFXXX;	MH12345;	2004-06-12;	2004-06-15;	;		2022-10-20
-235662;		10;	JM e.K.;	ING Bank AG;	DE49500105174516484892;	INGDDEFFXXX;	JM33344;	2005-06-282;	2005-07-01;	;		2016-01-18
+235662;		10;	JM e.K.;	ING Bank AG;	DE49500105174516484892;	INGDDEFFXXX;	JM33344;	2005-06-28;	2005-07-01;	;		2016-01-18

--
Gitblit v1.9.3