Improve general ledger

- Use sort from database
- Use groupby instead of dict

issue1399
issue1085
review987003