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:
@@ -26,10 +26,15 @@ func NewFiles(p string, q ...string) (Files, error) {
|
||||
}
|
||||
|
||||
func (files Files) Amend(old, now Delta) error {
|
||||
if now.isSet {
|
||||
return fmt.Errorf("cannot ammend: immutable isSet is set")
|
||||
}
|
||||
|
||||
xactions, err := files.transactions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var transaction transaction
|
||||
for _, xaction := range xactions {
|
||||
if xaction.name != old.transaction {
|
||||
@@ -39,23 +44,13 @@ func (files Files) Amend(old, now Delta) error {
|
||||
break
|
||||
}
|
||||
|
||||
inverted := transaction.deltas()
|
||||
for i := range inverted {
|
||||
inverted[i].Value *= -1
|
||||
}
|
||||
|
||||
if now.isSet {
|
||||
return fmt.Errorf("cannot ammend: immutable isSet is set")
|
||||
}
|
||||
transaction.date = now.Date
|
||||
transaction.description = now.Description
|
||||
if transaction.payee == old.Name {
|
||||
if len(transaction.recipients) != 1 {
|
||||
return fmt.Errorf("cannot amend: modifying original payee, but many recipients cant share new value")
|
||||
}
|
||||
old.Value *= -1
|
||||
old.Name = transaction.recipients[0].name
|
||||
transaction.payee, transaction.recipients[0].name, transaction.recipients[0].value = transaction.recipients[0].name, transaction.payee, transaction.recipients[0].value*-1.0
|
||||
}
|
||||
|
||||
idx := -1
|
||||
for i, recipient := range transaction.recipients {
|
||||
if recipient.name == old.Name && recipient.value == old.Value {
|
||||
@@ -63,16 +58,11 @@ func (files Files) Amend(old, now Delta) error {
|
||||
}
|
||||
}
|
||||
if idx == -1 {
|
||||
return fmt.Errorf("cannot amend: no recipient with name %s found to set new value", old.Name)
|
||||
return fmt.Errorf("cannot amend: no recipient with name %q value %.2f found in %+v to set new value", old.Name, old.Value, transaction)
|
||||
}
|
||||
transaction.recipients[idx].name = now.Name
|
||||
transaction.recipients[idx].value = now.Value
|
||||
transaction.recipients[idx].currency = string(now.Currency)
|
||||
amendedDeltas := transaction.deltas().Like(func(d Delta) bool {
|
||||
return d.Name != transaction.payee
|
||||
})
|
||||
|
||||
return files.Add(transaction.payee, append(inverted, amendedDeltas...)...)
|
||||
old.Value *= -1
|
||||
return files.Add(transaction.payee, []Delta{old, now}...)
|
||||
}
|
||||
|
||||
func (files Files) TempGetLastNLines(n int) ([]string, error) {
|
||||
|
||||
Reference in New Issue
Block a user