|
- <?php
-
- namespace App\Console\Commands\PoolTransfer;
-
- use App\Console\Commands\BaseCommand;
- use App\Exceptions\SkipException;
- use App\Kintone\KintoneRecordQueryOperator;
- use App\Kintone\Models\YuchoPaymentRelationSetting;
- use App\Kintone\Models\YuchoPaymentResult;
- use App\Util\DateUtil;
- use Illuminate\Support\Collection;
-
- class YuchoResolveCustomer extends BaseCommand
- {
-
- const COMMAND = "pool-transfer:yucho-resolve-customer";
-
-
- /**
- * 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 Collection<int, YuchoPaymentRelationSetting> */
- private Collection $settings;
-
- /**
- * 振込者名がキー
- * @var Collection<string, YuchoPaymentRelationSetting>
- **/
- private Collection $settingMap;
-
-
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
-
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function service(): int
- {
-
- $this->getSettings();
-
- $targets = $this->getTargets();
-
- $this->outputInfo(sprintf("総件数:%d件", $targets->count()));
-
- foreach ($targets as $target) {
-
- try {
- $this->handleData($target);
- } catch (SkipException $e) {
- $this->outputWarn($e->getMessage());
- }
- }
- return self::RESULTCODE_SUCCESS;
- }
-
- public function handleData(YuchoPaymentResult $payment)
- {
- $this->resolve($payment);
- }
-
- private function getSettings()
- {
-
- // 設定をキャッシュする
- $this->settings = YuchoPaymentRelationSetting::getAccess()->all();
- $ret = collect();
- foreach ($this->settings as $setting) {
- $name = $setting->paymentName;
- $ret->put($name, $setting);
- }
- $this->settingMap = $ret;
- }
-
- private function findSetting(string $paymentName)
- {
- return $this->settingMap->get($paymentName);
- }
-
- private function resolve(YuchoPaymentResult $payment)
- {
- $setting = $this->findSetting($payment->paymentName);
- if ($setting) {
- $payment->customerCode = $setting->customerCode;
- $this->outputInfo(sprintf("割り当て from <%s> to <%s>(%d)", $payment->paymentName, $setting->customerName, $setting->customerCode));
- $payment->save();
- }
- }
-
- public function getTargets()
- {
-
- $limitDate = DateUtil::now()->addDays(-7);
- // 未充当の入金一覧を取得する
- // 直近7日分を対象とする
- $targets = YuchoPaymentResult::getAccess()->all(YuchoPaymentResult::getQuery()
- ->whereNull(YuchoPaymentResult::FIELD_CUSTOMER_CODE)
- ->whereDate(YuchoPaymentResult::FIELD_PAYMENT_DATE, $limitDate, KintoneRecordQueryOperator::GE));
-
- return $targets;
- }
- }
|