db: fix invoice and order updating

- order and invoice didn't remove items at the end of the list when updating
- added jUnit tests that cover this defect
- fixed the database code