|
|
|
@ -209,10 +209,38 @@ 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 : ") {
|
|
|
|
|
openTransfers, shouldReturn, returnValue, returnValue1, returnValue2 := ynab.ImportTransferTransaction(payeeName, context, transaction, accountName, openTransfers, account, amount)
|
|
|
|
|
if shouldReturn {
|
|
|
|
|
return returnValue, returnValue1, returnValue2
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
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, fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
|
|
|
|
return nil, true, nil, true, fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transfer := Transfer{
|
|
|
|
@ -245,11 +273,11 @@ func (ynab *YNABImport) ImportTransaction(payeeName string, context context.Cont
|
|
|
|
|
|
|
|
|
|
_, err = ynab.queries.CreateTransaction(context, transfer.CreateTransactionParams)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
|
|
|
|
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, fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
|
|
|
|
return nil, true, nil, true, fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
@ -257,19 +285,7 @@ func (ynab *YNABImport) ImportTransaction(payeeName string, context context.Cont
|
|
|
|
|
if !found {
|
|
|
|
|
openTransfers = append(openTransfers, transfer)
|
|
|
|
|
}
|
|
|
|
|
} 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return openTransfers, false, nil
|
|
|
|
|
return openTransfers, false, nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func trimLastChar(s string) string {
|
|
|
|
|