|
- <?php
-
- namespace App\Console\Commands\PoolTransfer;
-
- use App\Console\Commands\BaseCommand;
- use App\Email\Members\EntryPaymentComplete;
- use App\Exceptions\SkipException;
- use App\Kintone\KintoneRecordQueryOperator;
- use App\Kintone\Models\PaymentPlan;
- use App\Kintone\Models\Pool;
- use App\Kintone\Models\SeasonTicketContractEntry;
- use App\Logic\EmailManager;
- use App\Logic\PoolAttachManager;
-
- class AttachToPaymentPlan extends BaseCommand
- {
-
- const COMMAND = "pool-transfer:attach-to-payment-plan";
-
-
- /**
- * 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;
- }
-
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
-
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function service(): int
-
-
- {
- $targets = $this->getTargets();
-
-
- $this->outputInfo(sprintf("総件数:%d件", $targets->count()));
-
-
-
- foreach ($targets as $index => $target) {
-
- // 100件ごとに経過を出力
- if ($index % 100 === 0) {
- $this->outputInfo(sprintf("処理中:%d件目", $index + 1));
- }
- try {
- $this->handleData($target);
- } catch (SkipException $e) {
- $this->outputWarn($e->getMessage());
- }
- }
-
-
- return self::RESULTCODE_SUCCESS;
- }
-
- public function handleData(Pool $pool)
- {
- $manager = new PoolAttachManager();
- $manager->attach($pool)
- ->save();
-
- // 初回振り込み完了処理
- if ($manager->getFirstPaymnetEntryRecordIds()->isNotEmpty()) {
- foreach ($manager->getFirstPaymnetEntryRecordIds() as $entryRecordId) {
- $this->handleFirstPaymentDone($entryRecordId);
- }
- }
- }
-
- public function handleFirstPaymentDone(int $entryRecordNo)
- {
- // 未払いチェック
- $query = PaymentPlan::getQuery()->where(PaymentPlan::FIELD_FIRST_PAYMENT_ENTRY_RECORD_NO, $entryRecordNo);
- $plans = PaymentPlan::getAccess()->all($query);
- if ($plans->isEmpty()) {
- return;
- }
-
- // 未払い検索
- $未払い = $plans->first((function (PaymentPlan $plan) {
- return !$plan->appropriationDate || $plan->appropriationAmount !== $plan->paymentPlanAmount;
- }));
- if ($未払い) {
- return;
- }
-
- // 充当済み処理
- $entry = SeasonTicketContractEntry::find($entryRecordNo);
- $entry->firstPaymentDone = ["充当済み"];
- $entry->save();
- }
-
- public function getTargets()
- {
- $query = Pool::getQuery()->where(Pool::FIELD_POOL_AMOUNT, 0, KintoneRecordQueryOperator::GT);
- $ret = Pool::getAccess()->all($query);
- return $ret;
- }
- }
|