impl and test src.ledger.Files.Amend(old, new Delta)
All checks were successful
cicd / ci (push) Successful in 1m15s
All checks were successful
cicd / ci (push) Successful in 1m15s
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user