Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

191 lignes
6.0KB

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Email\Members\BankAccountRegisterFailedNotice;
  4. use App\Http\API\SMBC\BankAccountRegister\PollResultRecord;
  5. use App\Http\API\SMBC\BankAccountRegister\SMBC;
  6. use App\Http\API\SMBC\BankAccountRegister\SMBCStatus;
  7. use App\Kintone\Models\BankAccountUpdateApplication;
  8. use App\Kintone\Models\Customer;
  9. use App\Logic\EmailManager;
  10. use App\Logic\GeneralApplicationManager;
  11. use App\Models\SmbcPollStatus;
  12. use App\Util\DateUtil;
  13. use App\Util\DBUtil;
  14. use Exception;
  15. use Illuminate\Support\Carbon;
  16. use Illuminate\Support\Collection;
  17. class SMBCBankAccountRegisterPoll extends BaseCommand
  18. {
  19. const COMMAND = "smbc:bank-account-register-poll";
  20. /**
  21. * The name and signature of the console command.
  22. *
  23. * @var string
  24. */
  25. protected $signature = self::COMMAND;
  26. /**
  27. * The console command description.
  28. *
  29. * @var string
  30. */
  31. protected $description = 'SMBCへ口座振替登録申請結果を取得する';
  32. static public function getCommand()
  33. {
  34. return self::COMMAND;
  35. }
  36. /**
  37. * @var Collection<int, BankAccountUpdateApplication>
  38. */
  39. private Collection $applications;
  40. /**
  41. * Create a new command instance.
  42. *
  43. * @return void
  44. */
  45. public function __construct()
  46. {
  47. parent::__construct();
  48. $this->applications = collect();
  49. }
  50. /**
  51. * Execute the console command.
  52. *
  53. * @return int
  54. */
  55. public function service(): int
  56. {
  57. try {
  58. $db = DBUtil::instance();
  59. $db->beginTransaction();
  60. // 検索範囲の取得
  61. [$from, $to] = $this->getFromTo();
  62. $this->outputInfo(sprintf("検索範囲 %s-%s", $from->format('Y/m/d H:i:s'), $to->format('Y/m/d H:i:s')));
  63. // 検索実行
  64. $result = SMBC::poll($from, $to);
  65. // 検索結果の確認
  66. if (!$result->ok()) {
  67. $this->outputError($result->getMessage());
  68. return self::RESULTCODE_FAILED;
  69. }
  70. $this->outputInfo(sprintf("取得対象 %d件", $result->getCount()));
  71. // データハンドリング
  72. foreach ($result->getRecord() as $data) {
  73. $this->handleData($data);
  74. }
  75. // 検索実績の登録
  76. $this->saveFromTo($from, $to);
  77. // キントーンへ各種申請登録
  78. foreach ($this->applications as $app) {
  79. $this->outputInfo(sprintf("申請登録 顧客コード:%s 申請番号:%s", $app->customerCode, $app->applicationNo));
  80. $app->save();
  81. }
  82. $this->outputInfo(sprintf("申請登録件数:%d件", $this->applications->count()));
  83. $db->commit();
  84. } catch (Exception $e) {
  85. $db->rollBack();
  86. throw $e;
  87. }
  88. return self::RESULTCODE_SUCCESS;
  89. }
  90. /**
  91. * @return Carbon[]
  92. */
  93. private function getFromTo()
  94. {
  95. $status = SmbcPollStatus::whereType(self::class)->first();
  96. $now = DateUtil::now();
  97. if ($status === null) {
  98. $this->outputInfo("検索範囲初期化");
  99. return [
  100. $now->clone()->addDays(-5),
  101. $now->clone(),
  102. ];
  103. }
  104. return [
  105. $status->condition_datetime_to->clone()->addSecond(),
  106. $now->clone(),
  107. ];
  108. }
  109. private function handleData(PollResultRecord $data)
  110. {
  111. if ($data->status === SMBCStatus::SUCCESS) {
  112. $customer = Customer::findByCustomerCode($data->getCustomerCode());
  113. $application = new BankAccountUpdateApplication();
  114. $manager = new GeneralApplicationManager($application);
  115. $manager
  116. ->setCustomer($customer)
  117. ->makeApplication();
  118. $application->bankBranchIdBefore = $customer->bankBranchId;
  119. $application->bankBranchIdAfter = $data->bankBranchCode;
  120. $application->bankCodeAfter = $data->bankCode;
  121. $application->bankNameAfter = $data->bankName;
  122. $application->branchCodeAfter = $data->branchCode;
  123. $application->branchNameAfter = $data->branchName;
  124. $application->accountTypeAfter = $data->accountType;
  125. $application->accountNameKanaAfter = $data->accountName;
  126. $application->accountNoAfter = $data->accountNo;
  127. $application->accountYuchoSignAfter = $data->yuchoSign;
  128. $application->accountYuchoNoAfter = $data->yuchoAccountNo;
  129. $application->smbcApplicationDatetime = $data->applicationDatetime;
  130. $application->smbcAcceptNo = $data->acceptNo;
  131. $application->smbcResult = $data->all;
  132. $this->applications->push($application);
  133. return;
  134. }
  135. if ($data->status === SMBCStatus::ERROR || $data->status === SMBCStatus::CANCEL) {
  136. // エラーメール送信
  137. $customer = Customer::findByCustomerCode($data->getCustomerCode());
  138. $this->outputWarn(sprintf("申請失敗のためエラーメール送信 受付番号%s 顧客コード:%d 氏名:%s", $data->acceptNo, $customer->customerCode, $customer->customerName));
  139. $email = new BankAccountRegisterFailedNotice($customer);
  140. $emailMmanager = new EmailManager($email);
  141. $emailMmanager->confirm();
  142. return;
  143. }
  144. if ($data->status === SMBCStatus::PROCESSING) {
  145. $this->outputInfo(sprintf("処理中のためスキップ 受付番号%s", $data->acceptNo));
  146. return;
  147. }
  148. if ($data->address5 !== SMBC::CONDITION_ADDR5_FROM_MY_PAGE) {
  149. $this->outputInfo(sprintf("MyPage以外からの申請のためスキップ 受付番号%s", $data->acceptNo));
  150. return;
  151. }
  152. }
  153. private function saveFromTo(Carbon $from, Carbon $to)
  154. {
  155. $status = SmbcPollStatus::whereType(self::class)->firstOrNew();
  156. $status->type = self::class;
  157. $status->condition_datetime_to = $to;
  158. $status->save();
  159. }
  160. }