|
- <?php
-
- namespace App\Kintone\Models;
-
- use App\Util\DateUtil;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Collection;
-
- /**
- * アプリ名 定期駐車場マスタ
- * @property string parkingName
- * @property array canTerminateDate
- * @property string canTerminateCurrentMonth
- * @property string untilCanTerminateCurrentMonth
- */
- class Parking extends KintoneModel
- {
- const CONFIG_KEY = "KINTONE_APP_PARKING";
-
- const FIELD_PARKING_NAME = "駐車場名";
- const FIELD_CAN_TERMINATE_DATE = "解約可能日";
- const FIELD_CAN_TERMINATE_CURRENT_MONTH = "当月解約申請";
- const FIELD_UNTIL_CAN_TERMINATE_CURRENT_MONTH = "当月解約いつまで可能";
-
- const ELEMENT_CAN_TERMINATE_DATE_END_OF_MONTH = "末日";
- const ELEMENT_CAN_TERMINATE_DATE_15 = "15日";
-
- const ELEMENT_CAN_TERMINATE_CURRENT_MONTH_OK = "可";
- const ELEMENT_CAN_TERMINATE_CURRENT_MONTH_NG = "不可";
-
- const ELEMENT_UNTIL_CAN_TERMINATE_CURRENT_MONTH_10 = "10日まで";
- const ELEMENT_UNTIL_CAN_TERMINATE_CURRENT_MONTH_TODAY = "当日";
-
- protected const FIELDS = [
- ...parent::FIELDS,
- self::FIELD_PARKING_NAME => FieldType::SINGLE_LINE_TEXT,
- self::FIELD_CAN_TERMINATE_DATE => FieldType::CHECK_BOX,
- self::FIELD_CAN_TERMINATE_CURRENT_MONTH => FieldType::RADIO_BUTTON,
- self::FIELD_UNTIL_CAN_TERMINATE_CURRENT_MONTH => FieldType::RADIO_BUTTON,
- ];
-
- protected const FIELD_NAMES = [
- ...parent::FIELD_NAMES,
- self::FIELD_PARKING_NAME => 'parking_name',
- ];
-
- static function findByParkingName(string $parkingName)
- {
- return static::getAccess()->first(static::getQuery()->where(static::FIELD_PARKING_NAME, $parkingName));
- }
-
- public function canTerminateDateEndOfMonth(): bool
- {
- return in_array(self::ELEMENT_CAN_TERMINATE_DATE_END_OF_MONTH, $this->canTerminateDate);
- }
- public function canTerminateDate15(): bool
- {
- return in_array(self::ELEMENT_CAN_TERMINATE_DATE_15, $this->canTerminateDate);
- }
-
- public function canTerminateCurrentMonth(): bool
- {
- return $this->canTerminateCurrentMonth == self::ELEMENT_CAN_TERMINATE_CURRENT_MONTH_OK;
- }
-
- public function isTerminateDay(Carbon $target, ?Carbon $now = null)
- {
- if ($now === null) {
- $now = DateUtil::now();
- }
-
- $currentMonthJadge = false;
- if ($now->format('Ym') === $target->format('Ym')) {
- if (!$this->canTerminateCurrentMonth()) {
- return false;
- }
- if ($this->untilCanTerminateCurrentMonth === self::ELEMENT_UNTIL_CAN_TERMINATE_CURRENT_MONTH_10) {
- if ($target->day <= 10) {
- $currentMonthJadge = true;
- }
- }
- if ($this->untilCanTerminateCurrentMonth === self::ELEMENT_UNTIL_CAN_TERMINATE_CURRENT_MONTH_TODAY) {
- // 当月ならいつでもOK
- $currentMonthJadge = true;
- }
- } else {
- $currentMonthJadge = true;
- }
-
- if ($currentMonthJadge === false) {
- return false;
- }
-
- if ($target->day === 15 && $this->canTerminateDate15()) {
- return true;
- }
- if ($target->isLastOfMonth() && $this->canTerminateDateEndOfMonth()) {
- return true;
- }
- return false;
- }
-
- /**
- * @param Carbon $from
- * @param Carbon $to
- * @param Carbon $now
- * @return Collection<int, Carbon>
- */
- public function getCanTerminateDateList(Carbon $from, Carbon $to, Carbon $now): Collection
- {
- $ret = collect();
- $target = $from->clone();
-
- while ($target->lessThanOrEqualTo($to)) {
-
- if ($this->isTerminateDay($target, $now)) {
- $ret->push($target->clone());
- }
-
- $target->addDay();
- }
- return $ret;
- }
- }
|