whereIn('code', $codes)->where('status', 'paid')->where('payment_method', 'balance') ->whereIn('id', MiniBalanceLedger::query()->where('source', 'checkout_balance')->where('direction', -1)->select('checkout_id'))->get(); foreach ($checkouts as $checkout) { foreach ($candidates[$checkout->code] as [$row, $package, $index]) { foreach ((array) $checkout->items as $item) { if ((int) $row['enterprise_id'] === (int) $checkout->enterprise_id && $package === (int) $item['package_id'] && $index >= 1 && $index <= (int) $item['quantity']) { $sources[$row['id']] = '企业余额购水'; } } } } } return $sources; } }