|
|
|
@ -209,69 +209,85 @@ func (ynab *YNABImport) ImportTransactions(context context.Context, r io.Reader)
|
|
|
|
|
|
|
|
|
|
func (ynab *YNABImport) ImportTransaction(payeeName string, context context.Context, transaction CreateTransactionParams, accountName string, openTransfers []Transfer, account *Account, amount Numeric) ([]Transfer, bool, error) {
|
|
|
|
|
if strings.HasPrefix(payeeName, "Transfer : ") {
|
|
|
|
|
transferToAccountName := payeeName[11:]
|
|
|
|
|
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(context, transfer.CreateTransactionParams)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
|
|
|
|
}
|
|
|
|
|
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
|
openTransfers = append(openTransfers, transfer)
|
|
|
|
|
openTransfers, shouldReturn, returnValue, returnValue1, returnValue2 := ynab.ImportTransferTransaction(payeeName, context, transaction, accountName, openTransfers, account, amount)
|
|
|
|
|
if shouldReturn {
|
|
|
|
|
return returnValue, returnValue1, returnValue2
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
payeeID, err := ynab.GetPayee(context, payeeName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, fmt.Errorf("get payee %s: %w", payeeName, err)
|
|
|
|
|
}
|
|
|
|
|
transaction.PayeeID = payeeID
|
|
|
|
|
|
|
|
|
|
_, err = ynab.queries.CreateTransaction(context, transaction)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, fmt.Errorf("save transaction %v: %w", transaction, err)
|
|
|
|
|
shouldReturn, returnValue, returnValue1, returnValue2 := ynab.ImportRegularTransaction(context, payeeName, transaction)
|
|
|
|
|
if shouldReturn {
|
|
|
|
|
return returnValue, returnValue1, returnValue2
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return openTransfers, false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ynab *YNABImport) ImportRegularTransaction(context context.Context, payeeName string, transaction CreateTransactionParams) (bool, []Transfer, bool, error) {
|
|
|
|
|
payeeID, err := ynab.GetPayee(context, payeeName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return true, nil, true, fmt.Errorf("get payee %s: %w", payeeName, err)
|
|
|
|
|
}
|
|
|
|
|
transaction.PayeeID = payeeID
|
|
|
|
|
|
|
|
|
|
_, err = ynab.queries.CreateTransaction(context, transaction)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return true, nil, true, fmt.Errorf("save transaction %v: %w", transaction, err)
|
|
|
|
|
}
|
|
|
|
|
return false, nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ynab *YNABImport) ImportTransferTransaction(payeeName string, context context.Context, transaction CreateTransactionParams, accountName string, openTransfers []Transfer, account *Account, amount Numeric) ([]Transfer, bool, []Transfer, bool, error) {
|
|
|
|
|
transferToAccountName := payeeName[11:]
|
|
|
|
|
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, nil, true, fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(context, transfer.CreateTransactionParams)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, nil, true, fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
|
|
|
|
}
|
|
|
|
|
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, nil, true, fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
|
openTransfers = append(openTransfers, transfer)
|
|
|
|
|
}
|
|
|
|
|
return openTransfers, false, nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func trimLastChar(s string) string {
|
|
|
|
|
r, size := utf8.DecodeLastRuneInString(s)
|
|
|
|
|
if r == utf8.RuneError && (size == 0 || size == 1) {
|
|
|
|
|