Fix bugs for invoices and add unit test for them