| 123456789101112131415161718192021222324252627282930313233343536373839 |
- <?php
- declare(strict_types=1);
- namespace plugin\saiadmin\app\service\finance;
- use app\miniapi\model\MiniCheckout;
- use app\miniapi\model\MiniBalanceLedger;
- /** Resolve historical balance receipts from persisted paid checkouts and debit ledgers. */
- final class ReceiptSources
- {
- public static function label(array $rows): array
- {
- $candidates = [];
- foreach ($rows as $row) {
- if (preg_match('/^(.+)-(\d+)-(\d+)$/D', $row['reference'], $match)) {
- $candidates[$match[1]][] = [$row, (int) $match[2], (int) $match[3]];
- }
- }
- $sources = [];
- foreach (array_chunk(array_keys($candidates), 500) as $codes) {
- $checkouts = MiniCheckout::query()->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;
- }
- }
|