Hi. I think it only needs to create two credit memos.
1. Use Copy Document to copy the posted sales invoice to create Sales Credit Memo (Recalculate = False), then post.
2. Use Copy Document to copy the posted purchase invoice to create Purchase Credit Memo (Recalculate = False), then post.
Hope this will help.
Thanks.