ReceiptSources.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. declare(strict_types=1);
  3. namespace plugin\saiadmin\app\service\finance;
  4. use app\miniapi\model\MiniCheckout;
  5. use app\miniapi\model\MiniBalanceLedger;
  6. /** Resolve historical balance receipts from persisted paid checkouts and debit ledgers. */
  7. final class ReceiptSources
  8. {
  9. public static function label(array $rows): array
  10. {
  11. $candidates = [];
  12. foreach ($rows as $row) {
  13. if (preg_match('/^(.+)-(\d+)-(\d+)$/D', $row['reference'], $match)) {
  14. $candidates[$match[1]][] = [$row, (int) $match[2], (int) $match[3]];
  15. }
  16. }
  17. $sources = [];
  18. foreach (array_chunk(array_keys($candidates), 500) as $codes) {
  19. $checkouts = MiniCheckout::query()->whereIn('code', $codes)->where('status', 'paid')->where('payment_method', 'balance')
  20. ->whereIn('id', MiniBalanceLedger::query()->where('source', 'checkout_balance')->where('direction', -1)->select('checkout_id'))->get();
  21. foreach ($checkouts as $checkout) {
  22. foreach ($candidates[$checkout->code] as [$row, $package, $index]) {
  23. foreach ((array) $checkout->items as $item) {
  24. if ((int) $row['enterprise_id'] === (int) $checkout->enterprise_id
  25. && $package === (int) $item['package_id'] && $index >= 1 && $index <= (int) $item['quantity']) {
  26. $sources[$row['id']] = '企业余额购水';
  27. }
  28. }
  29. }
  30. }
  31. }
  32. return $sources;
  33. }
  34. }