|
- <?php
-
- namespace App\Console\Commands;
-
- use App\Email\Members\BankAccountRegisterRemaind as MembersBankAccountRegisterRemaind;
- use App\Kintone\KintoneRecordQueryOperator;
- use App\Kintone\Models\Customer;
- use App\Logic\EmailManager;
- use App\Util\DateUtil;
- use App\Util\DBUtil;
- use Exception;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Collection;
-
- class BankAccountRegisterRemaind extends BaseCommand
- {
-
- const COMMAND = "remaind:bank-account-register";
-
-
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = self::COMMAND;
-
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '口座登録催促通知';
-
- static public function getCommand()
- {
- return self::COMMAND;
- }
-
- /**
- * 今回対象の日時
- *
- * @var Carbon
- */
- private Carbon $targetDatetime;
-
- /**
- * 次回対象の日時
- *
- * @var Carbon
- */
- private Carbon $nextDatetime;
-
-
- /**
- * @var Collection<int, Customer>
- */
- private Collection $customers;
-
-
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- $this->customers = collect();
- }
-
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function service(): int
- {
- try {
- $db = DBUtil::instance();
- $db->beginTransaction();
-
- // 基準日時の設定
- $this->targetDatetime = DateUtil::now()->setMinute(0)->setSecond(0);
-
- // 次回日時の設定 7日後
- $this->nextDatetime = $this->targetDatetime->clone()->addDays(7);
-
- // 対象の取得
- $targets = $this->getTargets();
-
- // 対象の処理
- foreach ($targets as $target) {
- $this->handleData($target);
- }
-
- // キントーンへ各種申請登録
- foreach ($this->customers as $customer) {
- if ($customer->bankAccountRegisterRemaindDatetime) {
- $this->outputInfo(sprintf("口座登録催促予定日時更新 顧客コード:%s 氏名:%s", $customer->customerCode, $customer->customerName));
- } else {
- $this->outputInfo(sprintf("口座登録催促予定日時クリアー 顧客コード:%s 氏名:%s", $customer->customerCode, $customer->customerName));
- }
- $customer->save();
- }
- $this->outputInfo(sprintf("登録件数:%d件", $this->customers->count()));
-
- $db->commit();
- } catch (Exception $e) {
- $db->rollBack();
- throw $e;
- }
-
- return self::RESULTCODE_SUCCESS;
- }
-
- /**
- * @return Collection<int, Customer>
- */
- private function getTargets(): Collection
- {
- $access = Customer::getAccess();
- $query = Customer::getQuery()->whereDateTime(
- Customer::FIELD_BANK_ACCOUNT_REGISTER_REMAIND_DATETIME,
- $this->targetDatetime,
- KintoneRecordQueryOperator::LE
- );
-
- $ret = $access->all($query);
-
- return $ret;
- }
-
-
- private function handleData(Customer $customer)
- {
- if ($customer->bankBranchId) {
- // 口座登録済みの場合は、口座登録催促予定日時をクリアーする
- $customer->bankAccountRegisterRemaindDatetime = null;
- } else {
- // メール通知し、次回通知日時を更新する
- $customer->bankAccountRegisterRemaindDatetime = $this->nextDatetime->clone();
-
- // 通知メール送信
- $email = new MembersBankAccountRegisterRemaind($customer);
- $emailMmanager = new EmailManager($email);
- $emailMmanager->confirm();
- }
- $this->customers->push($customer);
- }
- }
|