Add transfers to list and skip
This commit is contained in:
		| @@ -125,6 +125,8 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { | ||||
| 		return fmt.Errorf("could not read from tsv: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	var openTransfers []CreateTransactionParams | ||||
|  | ||||
| 	count := 0 | ||||
| 	for _, record := range csvData[1:] { | ||||
| 		accountName := record[0] | ||||
| @@ -141,12 +143,6 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { | ||||
| 			return fmt.Errorf("could not parse date %s: %w", dateString, err) | ||||
| 		} | ||||
|  | ||||
| 		payeeName := record[3] | ||||
| 		payeeID, err := ynab.GetPayee(payeeName) | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("could not get payee %s: %w", payeeName, err) | ||||
| 		} | ||||
|  | ||||
| 		categoryGroup, categoryName := record[5], record[6] //also in 4 joined by : | ||||
| 		category, err := ynab.GetCategory(categoryGroup, categoryName) | ||||
| 		if err != nil { | ||||
| @@ -162,20 +158,38 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { | ||||
| 			return fmt.Errorf("could not parse amount from (%s/%s): %w", inflow, outflow, err) | ||||
| 		} | ||||
|  | ||||
| 		//status := record[10] | ||||
|  | ||||
| 		transaction := CreateTransactionParams{ | ||||
| 			Date:       date, | ||||
| 			Memo:       memo, | ||||
| 			AccountID:  account.ID, | ||||
| 			PayeeID:    payeeID, | ||||
| 			CategoryID: category, | ||||
| 			Amount:     amount, | ||||
| 		} | ||||
|  | ||||
| 		payeeName := record[3] | ||||
| 		if strings.HasPrefix(payeeName, "Transfer : ") { | ||||
| 			// Transaction is a transfer to | ||||
| 			transferToAccountName := payeeName[11:] | ||||
| 			transferToAccount, err := ynab.GetAccount(transferToAccountName) | ||||
| 			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()) | ||||
| 		} else { | ||||
| 			payeeID, err := ynab.GetPayee(payeeName) | ||||
| 			if err != nil { | ||||
| 				return fmt.Errorf("could not get payee %s: %w", payeeName, err) | ||||
| 			} | ||||
| 			transaction.PayeeID = payeeID | ||||
|  | ||||
| 			_, err = ynab.queries.CreateTransaction(ynab.Context, transaction) | ||||
| 			if err != nil { | ||||
| 				return fmt.Errorf("could not save transaction %v: %w", transaction, err) | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		//status := record[10] | ||||
|  | ||||
| 		count++ | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user