impl and test src.ledger.Files.Amend(old, new Delta)
All checks were successful
cicd / ci (push) Successful in 1m15s

This commit is contained in:
Bel LaPointe
2024-07-14 14:24:29 -06:00
parent 3e001b8ddd
commit f6b8b92bff
4 changed files with 155 additions and 45 deletions

View File

@@ -3,10 +3,12 @@ package ledger
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"regexp"
"slices"
"strconv"
"unicode"
)
@@ -112,19 +114,47 @@ func (files Files) _transactions(file string) ([]transaction, error) {
func readTransaction(name string, r *bufio.Reader) (transaction, error) {
result, err := _readTransaction(name, r)
if err != nil {
if err != nil && !errors.Is(err, io.EOF) {
return result, err
}
if result.empty() {
return result, nil
}
if result.payee == "" && len(result.recipients) < 2 {
return result, fmt.Errorf("found a transaction with no payee and less than 2 recipeints: %+v", result)
return result, err
}
if result.payee != "" && len(result.recipients) < 1 {
return result, fmt.Errorf("found a transaction with payee but no recipeints: %+v", result)
}
return result, nil
if result.payee == "" {
if len(result.recipients) < 2 {
return result, fmt.Errorf("found a transaction with no payee and less than 2 recipeints: %+v", result)
}
func() {
sumPerRecipient := map[string]float64{}
recipients := []string{}
for _, recipient := range result.recipients {
recipients = append(recipients, recipient.name)
sumPerRecipient[recipient.name] += recipient.value
}
slices.Sort(recipients)
for _, k := range recipients {
v := sumPerRecipient[k]
everyoneElse := 0.0
for j := range sumPerRecipient {
if k != j {
everyoneElse += sumPerRecipient[j]
}
}
if -1.0*v == everyoneElse {
result.payee = k
result.recipients = slices.DeleteFunc(result.recipients, func(recipient transactionRecipient) bool {
return recipient.name == k
})
return
}
}
return
}()
}
return result, err
}
func _readTransaction(name string, r *bufio.Reader) (transaction, error) {