Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

GLTransaction simplify method #214

Merged
merged 3 commits into from
May 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions modules/minigl/src/main/java/org/jpos/gl/GLTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.text.ParseException;
Expand Down Expand Up @@ -391,6 +392,28 @@ public GLTransaction createReverse(Tags withTags, Tags withoutTags, boolean keep
return glt;
}

/**
* Create a simplified transaction based on this one
*/

public GLTransaction simplify() {
GLTransaction glt = new GLTransaction(getDetail());
for (GLEntry e : getEntries()) {
if (BigDecimal.ZERO.compareTo(e.getAmount()) != 0) {
GLEntry redundantEntry = getEntries().stream().filter(entry ->
entry.getAccount().equals(e.getAccount()) &&
entry.getAmount().compareTo(e.getAmount()) == 0 &&
entry.getLayer() == e.getLayer() &&
(e.isCredit() ^ entry.isCredit())).findAny().orElse(null);
if (redundantEntry == null) {
glt.createGLEntry(e.getAccount(), e.getAmount(), e.getDetail(), e.isCredit(), e.getLayer());
glt.setTags(e.getTags());
}
}
}
return glt;
}

/**
* Parses a JDOM Element as defined in
* <a href="http://jpos.org/minigl.dtd">minigl.dtd</a>
Expand Down