Skip to content

Commit

Permalink
Merge pull request #78 from jpos/get-accountdetail-options
Browse files Browse the repository at this point in the history
Get accountdetail options
  • Loading branch information
ar authored Jun 18, 2018
2 parents 1e72081 + 05f8ddf commit 70a6c23
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 60 deletions.
42 changes: 30 additions & 12 deletions modules/minigl/src/main/java/org/jpos/gl/AccountDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,50 @@ public class AccountDetail {
* Constructs an AccountDetail.
* @param journal the Journal.
* @param account the account.
* @param initialBalance initial balance (reporting currency).
* @param balance balance (reporting currency), could be initial if naturalOrder or final if not.
* @param start start date (inclusive).
* @param end end date (inclusive).
* @param entries list of GLEntries.
* @param layers the layers involved in this detail
* @param ascendingOrder if we should compute balance normally or inverted
*/
public AccountDetail(
Journal journal, Account account,
BigDecimal initialBalance,
Date start, Date end, List<GLEntry> entries, short[] layers)
BigDecimal balance,
Date start, Date end, List<GLEntry> entries, short[] layers, boolean ascendingOrder)
{
super();
this.journal = journal;
this.account = account;
this.initialBalance = initialBalance;
this.start = start;
this.end = end;
this.entries = entries;
this.layers = layers;
computeBalances();
if (ascendingOrder) {
this.initialBalance = balance;
computeBalances();
} else {
this.finalBalance = balance;
computeReverseBalances(balance);
}
}

/**
* Constructs an AccountDetail.
* @param journal the Journal.
* @param account the account.
* @param balance balance (reporting currency), could be initial if naturalOrder or final if not.
* @param start start date (inclusive).
* @param end end date (inclusive).
* @param entries list of GLEntries.
* @param layers the layers involved in this detail
*/
public AccountDetail(
Journal journal, Account account,
BigDecimal balance,
Date start, Date end, List<GLEntry> entries, short[] layers)
{
this(journal, account, balance, start, end, entries, layers, true);
}

/**
Expand All @@ -79,13 +103,7 @@ public AccountDetail(
BigDecimal balance,
List<GLEntry> entries, short[] layers)
{
super();
this.journal = journal;
this.account = account;
this.finalBalance = balance;
this.entries = entries;
this.layers = layers;
computeReverseBalances(balance);
this(journal, account, balance, null, null, entries, layers, false);
}

public Journal getJournal() {
Expand Down
100 changes: 52 additions & 48 deletions modules/minigl/src/main/java/org/jpos/gl/GLSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -1092,17 +1092,17 @@ public List<FinalAccount> getDeepFinalChildren(Account acct) throws HibernateExc
* @param acct the account.
* @param start date (inclusive).
* @param end date (inclusive).
* @param layers array of the layers included.
* @param ascendingOrder boolean.
* @param maxResults int.
* @return Account detail for given period.
* @throws GLException if user doesn't have READ permission on this journal.
*/
public AccountDetail getAccountDetail
(Journal journal, Account acct, Date start, Date end, short[] layers)
(Journal journal, Account acct, Date start, Date end, short[] layers, boolean ascendingOrder, int maxResults)
throws HibernateException, GLException
{
checkPermission (GLPermission.READ);
start = Util.floor (start);
end = Util.ceil (end);

Criteria crit = session.createCriteria (GLEntry.class);

boolean hasChildren = false;
Expand All @@ -1123,21 +1123,55 @@ public List<FinalAccount> getDeepFinalChildren(Account acct) throws HibernateExc

crit.add (Restrictions.in ("layer", (Object[])toShortArray (layers)));
crit = crit.createCriteria ("transaction")
.add (Restrictions.eq ("journal", journal))
.add (Restrictions.ge ("postDate", start))
.add (Restrictions.le ("postDate", end));
.add (Restrictions.eq ("journal", journal));
if (start != null || (start == null && ascendingOrder)) {
start = Util.floor(start);
crit.add (Restrictions.ge ("postDate", start));
}
if (end != null || (end == null && ascendingOrder)) {
end = Util.ceil(end);
crit.add (Restrictions.le ("postDate", end));
}

BigDecimal initialBalance[] = getBalances (journal, acct, start, false, layers, 0L);
crit.addOrder (Order.asc ("postDate"));
crit.addOrder (Order.asc ("timestamp"));
crit.addOrder (Order.asc ("id"));
List entries = crit.list();
// BigDecimal finalBalance = applyEntries (initialBalance[0], entries);
if (maxResults > 0)
crit.setMaxResults(maxResults);

long maxEntry = 0L;
List <GLEntry> entries;
BigDecimal initialBalance[];
if (ascendingOrder) {
crit.addOrder (Order.asc ("postDate"));
crit.addOrder (Order.asc ("timestamp"));
crit.addOrder (Order.asc ("id"));
entries = crit.list();
initialBalance = getBalances(journal, acct, start, false, layers, maxEntry);
} else {
crit.addOrder (Order.desc ("postDate"));
crit.addOrder (Order.desc ("timestamp"));
crit.addOrder (Order.desc ("id"));
entries = crit.list();
if (entries.size() > 0) {
maxEntry = entries.get(0).getId();
}
initialBalance = getBalances(journal, acct, end, true, layers, maxEntry);
}
return new AccountDetail(journal, acct, initialBalance[0], start, end, entries, layers, ascendingOrder);
}

return new AccountDetail (
journal, acct,
initialBalance[0],
start, end, entries, layers );
/**
* AccountDetail for date range
* @param journal the journal.
* @param acct the account.
* @param start date (inclusive).
* @param end date (inclusive).
* @return Account detail for given period.
* @throws GLException if user doesn't have READ permission on this journal.
*/
public AccountDetail getAccountDetail
(Journal journal, Account acct, Date start, Date end, short[] layers)
throws HibernateException, GLException
{
return getAccountDetail(journal, acct, start, end, layers, true, 0);
}

/**
Expand All @@ -1153,37 +1187,7 @@ public List<FinalAccount> getDeepFinalChildren(Account acct) throws HibernateExc
(Journal journal, Account acct, short[] layers, int maxResults)
throws HibernateException, GLException
{
checkPermission (GLPermission.READ);
Criteria crit = session.createCriteria (GLEntry.class);

boolean hasChildren = false;
if (acct.isCompositeAccount()) {
Disjunction dis = Restrictions.disjunction();
for (Long l : getChildren (acct)) {
hasChildren = true;
dis.add (Restrictions.idEq(l));
}
if (hasChildren) {
Criteria subCrit = crit.createCriteria(("account"));
subCrit.add (dis);
}
}
if (!hasChildren) {
crit.add (Restrictions.eq ("account", acct));
}

crit.add (Restrictions.in ("layer", (Object[])toShortArray (layers)));
crit = crit.createCriteria ("transaction")
.add (Restrictions.eq ("journal", journal));

crit.addOrder (Order.desc ("id"));
crit.setMaxResults(maxResults);
List<GLEntry> entries = crit.list();
BigDecimal balance = ZERO;
if (entries.size() > 0)
balance = getBalances(journal, acct, (Date) null, true, layers, entries.get(0).getId())[0];

return new AccountDetail(journal, acct, balance, entries, layers);
return getAccountDetail(journal, acct, null, null, layers, false, maxResults);
}


Expand Down

0 comments on commit 70a6c23

Please sign in to comment.