MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / matchesGroup

Method matchesGroup

reconciliation.go:1060–1094  ·  view source on GitHub ↗

matchesGroup compares a group of internal transactions with a single external transaction using matching rules. If a match is found, it returns true; otherwise, it returns false. Parameters: - externalTxn: The external transaction to compare. - group: The group of internal transactions to compare ag

(externalTxn *model.Transaction, group []*model.Transaction, matchingRules []model.MatchingRule)

Source from the content-addressed store, hash-verified

1058// Returns:
1059// - bool: True if the group matches the external transaction, false otherwise.
1060func (s *LedgerForge) matchesGroup(externalTxn *model.Transaction, group []*model.Transaction, matchingRules []model.MatchingRule) bool {
1061 var totalAmount float64
1062 var minDate, maxDate time.Time
1063 descriptions := make([]string, 0, len(group))
1064 references := make([]string, 0, len(group))
1065 currencies := make(map[string]bool)
1066
1067 // Iterate over the group of internal transactions and accumulate information.
1068 for i, internalTxn := range group {
1069 totalAmount += internalTxn.Amount
1070
1071 if i == 0 || internalTxn.CreatedAt.Before(minDate) {
1072 minDate = internalTxn.CreatedAt
1073 }
1074 if i == 0 || internalTxn.CreatedAt.After(maxDate) {
1075 maxDate = internalTxn.CreatedAt
1076 }
1077
1078 descriptions = append(descriptions, internalTxn.Description)
1079 references = append(references, internalTxn.Reference)
1080 currencies[internalTxn.Currency] = true
1081 }
1082
1083 // Create a virtual transaction representing the group for comparison.
1084 groupTxn := model.Transaction{
1085 Amount: totalAmount,
1086 CreatedAt: minDate, // Use the earliest date in the group.
1087 Description: strings.Join(descriptions, " | "),
1088 Reference: strings.Join(references, " | "),
1089 Currency: s.dominantCurrency(currencies), // Determine the dominant currency in the group.
1090 }
1091
1092 // Use the matching rules to compare the group with the external transaction.
1093 return s.matchesRules(externalTxn, groupTxn, matchingRules)
1094}
1095
1096// dominantCurrency returns the dominant currency in a group of transactions.
1097// If there is only one currency, it returns that currency, otherwise, it returns "MIXED".

Callers 1

Calls 2

dominantCurrencyMethod · 0.95
matchesRulesMethod · 0.95

Tested by

no test coverage detected