Loop on each contract. If it is a paid contract, and there is no unpaid invoice for contract, and line not suspended and end date <= today + 2 days (so expired or soon expired, we must be sure to make refresh before new generation of invoice), we update qty on contract + qty on linked template invoice