You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.0KB

  1. <?php
  2. namespace App\Kintone\Models;
  3. use App\Kintone\Repositories\SeasonTicketContractRepository;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Facades\Auth;
  6. /**
  7. * アプリ名 顧客マスタ
  8. * @property int customerCode
  9. * @property string customerName
  10. * @property string customerNameKana
  11. * @property string paymentMethod
  12. * @property string[] allowPayByCreditcard
  13. * @property string email
  14. * @property string phoneNumber
  15. * @property string zipCode
  16. * @property string address
  17. * @property string bankBranchId
  18. * @property ?Carbon bankAccountRegisterRemaindDatetime
  19. * @property string[] allowAccessMyPage
  20. * @property ?int icSeasonTicektUserId
  21. */
  22. class Customer extends KintoneModel
  23. {
  24. const CONFIG_KEY = "KINTONE_APP_CUSTOMER";
  25. const FIELD_CUSTOMER_CODE = "CustomerCode";
  26. const FIELD_CUSTOMER_NAME = "CustomerName";
  27. const FIELD_CUSTOMER_NAME_KANA = "顧客名カナ";
  28. const FIELD_PAYMENT_METHOD = "支払方法";
  29. const FIELD_ALLOW_PAY_BY_CREDITCARD = "クレジットカード支払許可";
  30. const FIELD_EMAIL = "メールアドレス";
  31. const FIELD_PHONE_NUMBER = "電話番号";
  32. const FIELD_ZIP_CODE = "契約者_郵便番号";
  33. const FIELD_ADDRESS = "住所";
  34. const FIELD_BANK_BRANCH_ID = "ChargedBankBranchCode";
  35. const FIELD_BANK_ACCOUNT_REGISTER_REMAIND_DATETIME = "口座登録催促予定日時";
  36. const FIELD_ALLOW_ACCESS_MY_PAGE = "MyPageアクセス許可";
  37. const FIELD_IC_SEASON_TICEKT_USER_ID = "IC定期関連情報_ユーザーID";
  38. protected const FIELDS = [
  39. ...parent::FIELDS,
  40. self::FIELD_CUSTOMER_CODE => FieldType::NUMBER,
  41. self::FIELD_CUSTOMER_NAME => FieldType::SINGLE_LINE_TEXT,
  42. self::FIELD_CUSTOMER_NAME_KANA => FieldType::SINGLE_LINE_TEXT,
  43. self::FIELD_PAYMENT_METHOD => FieldType::SINGLE_LINE_TEXT,
  44. self::FIELD_ALLOW_PAY_BY_CREDITCARD => FieldType::CHECK_BOX,
  45. self::FIELD_EMAIL => FieldType::LINK,
  46. self::FIELD_PHONE_NUMBER => FieldType::LINK,
  47. self::FIELD_ZIP_CODE => FieldType::SINGLE_LINE_TEXT,
  48. self::FIELD_ADDRESS => FieldType::SINGLE_LINE_TEXT,
  49. self::FIELD_BANK_BRANCH_ID => FieldType::SINGLE_LINE_TEXT,
  50. self::FIELD_BANK_ACCOUNT_REGISTER_REMAIND_DATETIME => FieldType::DATETIME,
  51. self::FIELD_ALLOW_ACCESS_MY_PAGE => FieldType::CHECK_BOX,
  52. self::FIELD_IC_SEASON_TICEKT_USER_ID => FieldType::NUMBER,
  53. ];
  54. protected const FIELD_NAMES = [
  55. ...parent::FIELD_NAMES,
  56. self::FIELD_CUSTOMER_CODE => 'customer_code',
  57. self::FIELD_CUSTOMER_NAME => 'customer_name',
  58. self::FIELD_CUSTOMER_NAME_KANA => 'customer_name_kana',
  59. self::FIELD_EMAIL => 'email',
  60. self::FIELD_ZIP_CODE => 'zip_code',
  61. self::FIELD_ADDRESS => 'address',
  62. self::FIELD_PHONE_NUMBER => 'phone_no',
  63. ];
  64. // キャッシュ
  65. private bool|null $canPayByCreditcard = null;
  66. public static function getSelf(): static
  67. {
  68. return static::getAccess()->find(Auth::user()->kintone_id);
  69. }
  70. public static function findByCustomerCode(string $customerCode)
  71. {
  72. return static::getAccess()->first(static::getQuery()->where(self::FIELD_CUSTOMER_CODE, $customerCode));
  73. }
  74. protected function toArrayCustom(): array
  75. {
  76. return [
  77. 'customer_name_kana_hankaku' => mb_convert_kana($this->customerNameKana, "sk"),
  78. 'can_pay_by_creditcard' => $this->canPayByCreditcard(),
  79. 'can_apply_to_change_payment_method_creditcard' => $this->canApplyToChangePaymentMethodCreditcard(),
  80. ];
  81. }
  82. public function isHTICWebUser(): bool
  83. {
  84. return !!$this->icSeasonTicektUserId;
  85. }
  86. public function canPayByCreditcard(bool $refresh = false): bool
  87. {
  88. // キャッシュがあればそれを返却する
  89. if (is_bool($this->canPayByCreditcard) && $refresh === false) {
  90. return $this->canPayByCreditcard;
  91. }
  92. $list = SeasonTicketContractRepository::get($this->customerCode);
  93. foreach ($list as $ele) {
  94. if (!$ele->plan->canPayByCreditcard()) {
  95. $this->canPayByCreditcard = false;
  96. return $this->canPayByCreditcard;
  97. }
  98. }
  99. $this->canPayByCreditcard = true;
  100. return $this->canPayByCreditcard;
  101. }
  102. // マイページアクセス許可
  103. public function allowAccessMyPage(): bool
  104. {
  105. $target = $this->allowAccessMyPage;
  106. if (is_array($target) && Arr::has($this->allowAccessMyPage, "許可")) {
  107. return true;
  108. }
  109. return false;
  110. }
  111. // クレジットカード支払許可
  112. public function allowPayByCreditcard(): bool
  113. {
  114. $target = $this->allowPayByCreditcard;
  115. if (is_array($target) && Arr::has($this->allowPayByCreditcard, "許可")) {
  116. return true;
  117. }
  118. return false;
  119. }
  120. // クレジットカード登録申請可否
  121. public function canApplyToChangePaymentMethodCreditcard()
  122. {
  123. return $this->allowPayByCreditcard() && $this->paymentMethod !== "クレジット" && $this->canPayByCreditcard();
  124. }
  125. }