Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

187 lines
5.4KB

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