This commit is contained in:
@@ -26,7 +26,53 @@ func NewFiles(p string, q ...string) (Files, error) {
|
||||
}
|
||||
|
||||
func (files Files) Amend(old, now Delta) error {
|
||||
return io.EOF
|
||||
xactions, err := files.transactions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var transaction transaction
|
||||
for _, xaction := range xactions {
|
||||
if xaction.name != old.transaction {
|
||||
continue
|
||||
}
|
||||
transaction = xaction
|
||||
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
|
||||
}
|
||||
idx := -1
|
||||
for i, recipient := range transaction.recipients {
|
||||
if recipient.name == old.Name && recipient.value == old.Value {
|
||||
idx = i
|
||||
}
|
||||
}
|
||||
if idx == -1 {
|
||||
return fmt.Errorf("cannot amend: no recipient with name %s found to set new value", old.Name)
|
||||
}
|
||||
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...)...)
|
||||
}
|
||||
|
||||
func (files Files) TempGetLastNLines(n int) ([]string, error) {
|
||||
@@ -134,7 +180,16 @@ func (files Files) paths() []string {
|
||||
return result
|
||||
}
|
||||
|
||||
func (files Files) Add(payee string, delta Delta) error {
|
||||
func (files Files) Add(payee string, deltas ...Delta) error {
|
||||
for _, delta := range deltas {
|
||||
if err := files.add(payee, delta); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (files Files) add(payee string, delta Delta) error {
|
||||
currencyValue := fmt.Sprintf("%s%.2f", delta.Currency, delta.Value)
|
||||
if delta.Currency != USD {
|
||||
currencyValue = fmt.Sprintf("%.2f %s", delta.Value, delta.Currency)
|
||||
@@ -214,39 +269,7 @@ func (files Files) Deltas(like ...Like) (Deltas, error) {
|
||||
|
||||
result := make(Deltas, 0, len(transactions)*2)
|
||||
for _, transaction := range transactions {
|
||||
sums := map[string]float64{}
|
||||
for _, recipient := range transaction.recipients {
|
||||
sums[recipient.currency] += recipient.value
|
||||
delta := newDelta(
|
||||
transaction.name,
|
||||
transaction.date,
|
||||
transaction.description,
|
||||
recipient.name,
|
||||
recipient.value,
|
||||
recipient.currency,
|
||||
recipient.isSet,
|
||||
)
|
||||
result = append(result, delta)
|
||||
}
|
||||
for currency, value := range sums {
|
||||
if value == 0 {
|
||||
continue
|
||||
}
|
||||
if transaction.payee == "" {
|
||||
//return nil, fmt.Errorf("didnt find net zero and no dumping ground payee set: %+v", transaction)
|
||||
} else {
|
||||
delta := newDelta(
|
||||
transaction.name,
|
||||
transaction.date,
|
||||
transaction.description,
|
||||
transaction.payee,
|
||||
-1.0*value,
|
||||
currency,
|
||||
false,
|
||||
)
|
||||
result = append(result, delta)
|
||||
}
|
||||
}
|
||||
result = append(result, transaction.deltas()...)
|
||||
}
|
||||
|
||||
balances := make(Balances)
|
||||
|
||||
Reference in New Issue
Block a user