The fact that you can't debug your code is a huge problem preventing you from doing your job, therefore resolving it is more important than writing new code that you can't debug. If you need our help, create a new thread (with an appropriate title) and explain your problem in detail there.
Ask yourself why you added the group by clause there and whether the reasons are still valid. You can also explain your reasoning to us, if you want to discuss it.
It's difficult for me to discuss a design of a query that doesn't make sense to me. If I really wanted something like that, I would probably use a view with a computed column returning the first invoice ID for each packing slip ID, and I would likely use this view when needed instead of writing the information to packing slips. But that's likely too advanced for you.
What you can do, for example, is removing grouping and sorting the data by PackingSlipId. To prevent multiple updates of the same record, you can remember the previously processed PackingSlipId and if it's the same, you'll skip update().
Or you can use grouping and then use CustPackingSlipJour::find() to load the record to update.