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.

152 lignes
3.9KB

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Email\Members\BankAccountRegisterRemaind as MembersBankAccountRegisterRemaind;
  4. use App\Kintone\KintoneRecordQueryOperator;
  5. use App\Kintone\Models\Customer;
  6. use App\Logic\EmailManager;
  7. use App\Util\DateUtil;
  8. use App\Util\DBUtil;
  9. use Exception;
  10. use Illuminate\Support\Carbon;
  11. use Illuminate\Support\Collection;
  12. class BankAccountRegisterRemaind extends BaseCommand
  13. {
  14. const COMMAND = "remaind:bank-account-register";
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = self::COMMAND;
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '口座登録催促通知';
  27. static public function getCommand()
  28. {
  29. return self::COMMAND;
  30. }
  31. /**
  32. * 今回対象の日時
  33. *
  34. * @var Carbon
  35. */
  36. private Carbon $targetDatetime;
  37. /**
  38. * 次回対象の日時
  39. *
  40. * @var Carbon
  41. */
  42. private Carbon $nextDatetime;
  43. /**
  44. * @var Collection<int, Customer>
  45. */
  46. private Collection $customers;
  47. /**
  48. * Create a new command instance.
  49. *
  50. * @return void
  51. */
  52. public function __construct()
  53. {
  54. parent::__construct();
  55. $this->customers = collect();
  56. }
  57. /**
  58. * Execute the console command.
  59. *
  60. * @return int
  61. */
  62. public function service(): int
  63. {
  64. try {
  65. $db = DBUtil::instance();
  66. $db->beginTransaction();
  67. // 基準日時の設定
  68. $this->targetDatetime = DateUtil::now()->setMinute(0)->setSecond(0);
  69. // 次回日時の設定 7日後
  70. $this->nextDatetime = $this->targetDatetime->clone()->addDays(7);
  71. // 対象の取得
  72. $targets = $this->getTargets();
  73. // 対象の処理
  74. foreach ($targets as $target) {
  75. $this->handleData($target);
  76. }
  77. // キントーンへ各種申請登録
  78. foreach ($this->customers as $customer) {
  79. if ($customer->bankAccountRegisterRemaindDatetime) {
  80. $this->outputInfo(sprintf("口座登録催促予定日時更新 顧客コード:%s 氏名:%s", $customer->customerCode, $customer->customerName));
  81. } else {
  82. $this->outputInfo(sprintf("口座登録催促予定日時クリアー 顧客コード:%s 氏名:%s", $customer->customerCode, $customer->customerName));
  83. }
  84. $customer->save();
  85. }
  86. $this->outputInfo(sprintf("登録件数:%d件", $this->customers->count()));
  87. $db->commit();
  88. } catch (Exception $e) {
  89. $db->rollBack();
  90. throw $e;
  91. }
  92. return self::RESULTCODE_SUCCESS;
  93. }
  94. /**
  95. * @return Collection<int, Customer>
  96. */
  97. private function getTargets(): Collection
  98. {
  99. $access = Customer::getAccess();
  100. $query = Customer::getQuery()->whereDateTime(
  101. Customer::FIELD_BANK_ACCOUNT_REGISTER_REMAIND_DATETIME,
  102. $this->targetDatetime,
  103. KintoneRecordQueryOperator::LE
  104. );
  105. $ret = $access->all($query);
  106. return $ret;
  107. }
  108. private function handleData(Customer $customer)
  109. {
  110. if ($customer->bankBranchId) {
  111. // 口座登録済みの場合は、口座登録催促予定日時をクリアーする
  112. $customer->bankAccountRegisterRemaindDatetime = null;
  113. } else {
  114. // メール通知し、次回通知日時を更新する
  115. $customer->bankAccountRegisterRemaindDatetime = $this->nextDatetime->clone();
  116. // 通知メール送信
  117. $email = new MembersBankAccountRegisterRemaind($customer);
  118. $emailMmanager = new EmailManager($email);
  119. $emailMmanager->confirm();
  120. }
  121. $this->customers->push($customer);
  122. }
  123. }