Do you have this structure?
If so, you can run your script on the LineItems table.
Find all records whose related Invoice record is in a specified date interval.
Sort records by client.
Use summary fields and calculations with the getsummary function to compute sub totals
loop through these records computing and applying discounts per your business rules.
This script can update line item records to show the discount, it can update fields in invoices or even create records in a related discount table.
I do have that relationship. Thank you very much this seems like a really good way to go about it.