No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

129 líneas
3.2KB

  1. <?php
  2. namespace App\Console\Commands\PoolTransfer;
  3. use App\Console\Commands\BaseCommand;
  4. use App\Exceptions\SkipException;
  5. use App\Kintone\KintoneRecordQueryOperator;
  6. use App\Kintone\Models\YuchoPaymentRelationSetting;
  7. use App\Kintone\Models\YuchoPaymentResult;
  8. use App\Util\DateUtil;
  9. use Illuminate\Support\Collection;
  10. class YuchoResolveCustomer extends BaseCommand
  11. {
  12. const COMMAND = "pool-transfer:yucho-resolve-customer";
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = self::COMMAND;
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'ゆうちょ振込情報の顧客コードを解決する';
  25. static public function getCommand()
  26. {
  27. return self::COMMAND;
  28. }
  29. /** @var Collection<int, YuchoPaymentRelationSetting> */
  30. private Collection $settings;
  31. /**
  32. * 振込者名がキー
  33. * @var Collection<string, YuchoPaymentRelationSetting>
  34. **/
  35. private Collection $settingMap;
  36. /**
  37. * Create a new command instance.
  38. *
  39. * @return void
  40. */
  41. public function __construct()
  42. {
  43. parent::__construct();
  44. }
  45. /**
  46. * Execute the console command.
  47. *
  48. * @return int
  49. */
  50. public function service(): int
  51. {
  52. $this->getSettings();
  53. $targets = $this->getTargets();
  54. $this->outputInfo(sprintf("総件数:%d件", $targets->count()));
  55. foreach ($targets as $target) {
  56. try {
  57. $this->handleData($target);
  58. } catch (SkipException $e) {
  59. $this->outputWarn($e->getMessage());
  60. }
  61. }
  62. return self::RESULTCODE_SUCCESS;
  63. }
  64. public function handleData(YuchoPaymentResult $payment)
  65. {
  66. $this->resolve($payment);
  67. }
  68. private function getSettings()
  69. {
  70. // 設定をキャッシュする
  71. $this->settings = YuchoPaymentRelationSetting::getAccess()->all();
  72. $ret = collect();
  73. foreach ($this->settings as $setting) {
  74. $name = $setting->paymentName;
  75. $ret->put($name, $setting);
  76. }
  77. $this->settingMap = $ret;
  78. }
  79. private function findSetting(string $paymentName)
  80. {
  81. return $this->settingMap->get($paymentName);
  82. }
  83. private function resolve(YuchoPaymentResult $payment)
  84. {
  85. $setting = $this->findSetting($payment->paymentName);
  86. if ($setting) {
  87. $payment->customerCode = $setting->customerCode;
  88. $this->outputInfo(sprintf("割り当て from <%s> to <%s>(%d)", $payment->paymentName, $setting->customerName, $setting->customerCode));
  89. $payment->save();
  90. }
  91. }
  92. public function getTargets()
  93. {
  94. $limitDate = DateUtil::now()->addDays(-7);
  95. // 未充当の入金一覧を取得する
  96. // 直近7日分を対象とする
  97. $targets = YuchoPaymentResult::getAccess()->all(YuchoPaymentResult::getQuery()
  98. ->whereNull(YuchoPaymentResult::FIELD_CUSTOMER_CODE)
  99. ->whereDate(YuchoPaymentResult::FIELD_PAYMENT_DATE, $limitDate, KintoneRecordQueryOperator::GE));
  100. return $targets;
  101. }
  102. }