Small performance fix + cleanup of account_invoice

Three separate commits.

The first one adds a small performance improvement: there are two writes being performed: the first optional, the second one always. Instead of writing twice, we keep the values of the optional write in a dictionary, and merge it with the values for the second write so we only need to write to the DB once.

The second one is just cleaning up unused imports and variables.

The third one is a fix for a potential problem discovered by MAT: the invoice date is used throughout the method, but if it needed to be written in the original, optional write, the written values are not used for the rest of the logic.