From 2ec9c923dfb155b95d63c4f690fd6e69dd70b352 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Sun, 9 Jan 2022 22:29:56 +0000 Subject: [PATCH] Implement matching --- postgres/ynab-import.go | 57 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/postgres/ynab-import.go b/postgres/ynab-import.go index 766e668..f976df4 100644 --- a/postgres/ynab-import.go +++ b/postgres/ynab-import.go @@ -113,6 +113,13 @@ func (ynab *YNABImport) ImportAssignments(r io.Reader) error { return nil } +type Transfer struct { + CreateTransactionParams + TransferToAccount *Account + FromAccount string + ToAccount string +} + // ImportTransactions expects a TSV-file as exported by YNAB in the following format: func (ynab *YNABImport) ImportTransactions(r io.Reader) error { @@ -125,7 +132,7 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { return fmt.Errorf("could not read from tsv: %w", err) } - var openTransfers []CreateTransactionParams + var openTransfers []Transfer count := 0 for _, record := range csvData[1:] { @@ -174,8 +181,49 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { if err != nil { return fmt.Errorf("Could not get transfer account %s: %w", transferToAccountName, err) } - openTransfers = append(openTransfers, transaction) - fmt.Printf("Found transfer from %s to %s over %f\n", account.Name, transferToAccount.Name, amount.GetFloat64()) + + transfer := Transfer{ + transaction, + transferToAccount, + accountName, + transferToAccountName, + } + + found := false + for i, openTransfer := range openTransfers { + if openTransfer.TransferToAccount.ID != transfer.AccountID { + continue + } + if openTransfer.AccountID != transfer.TransferToAccount.ID { + continue + } + if openTransfer.Amount.GetFloat64() != -1*transfer.Amount.GetFloat64() { + continue + } + + fmt.Printf("Matched transfers from %s to %s over %f\n", account.Name, transferToAccount.Name, amount.GetFloat64()) + openTransfers[i] = openTransfers[len(openTransfers)-1] + openTransfers = openTransfers[:len(openTransfers)-1] + found = true + + groupID := uuid.New() + transfer.GroupID = uuid.NullUUID{UUID: groupID, Valid: true} + openTransfer.GroupID = uuid.NullUUID{UUID: groupID, Valid: true} + + _, err = ynab.queries.CreateTransaction(ynab.Context, transfer.CreateTransactionParams) + if err != nil { + return fmt.Errorf("could not save transaction %v: %w", transaction, err) + } + _, err = ynab.queries.CreateTransaction(ynab.Context, openTransfer.CreateTransactionParams) + if err != nil { + return fmt.Errorf("could not save transaction %v: %w", transaction, err) + } + break + } + + if !found { + openTransfers = append(openTransfers, transfer) + } } else { payeeID, err := ynab.GetPayee(payeeName) if err != nil { @@ -194,6 +242,9 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { count++ } + for _, openTransfer := range openTransfers { + fmt.Printf("Found transfer from %s to %s on %s over %f\n", openTransfer.FromAccount, openTransfer.ToAccount, openTransfer.Date, openTransfer.Amount.GetFloat64()) + } fmt.Printf("Imported %d transactions\n", count) return nil