[eluser]OverZealous[/eluser]
One thing I forgot to mention that might save you a query is to use where_related() to reduce the number of queries.
Example (assumes Events has a client_id field):
Code:
// get all receipts for the customer
$receipts = new Receipt();
$receipts->where_related_event('client_id', $client->id)->select('id')->get();
$receipt_ids = new array();
foreach($receipts->all as $r) {
$receipt_ids[] = $r->id;
}
// get all payments that belong to one of the receipts
$payments = new Payment();
$payments->where_in_related_receipt('id', $receipt_ids)->distinct()->get();
This should get all of the payments, distinctly, that are related to the client, in just 2 queries.
The nice thing about this is that you can still sort the payments list all at once.
Let me know if the above code works, since I typed it off the top of my head.