Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

139 řádky
4.5KB

  1. <?php
  2. namespace App\Http\Controllers\Web\QRService\Certification;
  3. use App\Exceptions\GeneralErrorMessageException;
  4. use App\Http\Controllers\Web\WebController;
  5. use App\Logics\QRService\QRCryptoLogic;
  6. use App\Models\ColumnName;
  7. use App\Models\HtpmsCustomer\Existing\DiscountTicket;
  8. use App\Models\HtpmsCustomer\Existing\Parking;
  9. use App\Models\HtpmsCustomer\Mst\ShopNoRelation;
  10. use App\Models\HtpmsCustomer\QRService\CertificationAvailableSetting;
  11. use App\Models\HtpmsCustomer\QRService\CertificationTicket;
  12. use Illuminate\Http\JsonResponse;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Carbon;
  15. class CheckDataFormatController extends WebController
  16. {
  17. public function name(): string
  18. {
  19. return "データチェック";
  20. }
  21. public function description(): string
  22. {
  23. return "暗号化文字列をチェック認証に必要な情報を取得する";
  24. }
  25. public function __construct(protected CheckDataFormatParam $param)
  26. {
  27. parent::__construct();
  28. }
  29. protected function run(Request $request): JsonResponse
  30. {
  31. $param = $this->param;
  32. // 暗号化文字列の解読
  33. $data = DataConverter::read($param->data);
  34. // 対象の駐車場か判定
  35. $relation = ShopNoRelation::whereShopId($this->sessionUser->shopId())
  36. ->whereParkingManagementCode($data->駐車場管理コード)
  37. ->first();
  38. if ($relation === null) {
  39. throw new GeneralErrorMessageException("認証できない駐車場");
  40. }
  41. // 認証可能なサービス券一覧取得
  42. $setting = CertificationAvailableSetting::whereShopId($this->sessionUser->shopId())
  43. ->whereParkingManagementCode($data->駐車場管理コード)
  44. ->get();
  45. if ($setting->isEmpty()) {
  46. throw new GeneralErrorMessageException("認証できるサービス券なし");
  47. }
  48. // 駐車場情報の取得
  49. $parking = Parking::whereParkCode($data->駐車場管理コード)
  50. ->first();
  51. if ($parking instanceof Parking === false) {
  52. throw new GeneralErrorMessageException("存在しない駐車場");
  53. }
  54. // 認証済みチェック
  55. if (CertificationTicket::whereParkingManagementCode($data->駐車場管理コード)
  56. ->wherePublishingTerminalCode($data->発行端末)
  57. ->wherePublishingDate($data->発行日)
  58. ->wherePublishingNo($data->発行連番)
  59. ->exists()
  60. ) {
  61. throw new GeneralErrorMessageException("認証ずみのサービス券");
  62. }
  63. // サービス券一覧の取得
  64. $discountTicketCodes = $setting->pluck(CertificationAvailableSetting::COL_NAME_DISCOUNT_TICKET_CODE)->toArray();
  65. $discountTickets = DiscountTicket::whereParkId($parking->id)
  66. ->whereIn(DiscountTicket::COL_NAME_DISCOUNT_TICKET_CODE, $discountTicketCodes)
  67. ->select([
  68. sprintf("%s as %s", DiscountTicket::COL_NAME_DISCOUNT_TICKET_CODE, ColumnName::DISCOUNT_TICKET_CODE),
  69. sprintf("%s as %s", DiscountTicket::COL_NAME_TICKET_NAME, "ticket_name"),
  70. ])->get();
  71. $res = [
  72. "parking" => [
  73. "parking_name" => $parking->park_name,
  74. "parking_management_code" => $parking->park_code,
  75. "publishing_terminal_code" => $data->発行端末,
  76. "publishing_date" => $data->発行日,
  77. "publishing_no" => $data->発行連番,
  78. ],
  79. "discount_tickets" => $discountTickets,
  80. ];
  81. return $this->successResponse($res);
  82. }
  83. }
  84. /**
  85. * 精算機で暗号化された文字列をパースする
  86. */
  87. class DataConverter
  88. {
  89. public string $発行端末;
  90. public Carbon $発行日;
  91. public int $発行連番;
  92. public string $顧客コード;
  93. public string $駐車場管理コード;
  94. public static function read(string $data): static
  95. {
  96. $ret = new static();
  97. $dec = QRCryptoLogic::decrypt($data);
  98. if (strlen($dec) !== 30) {
  99. throw new GeneralErrorMessageException("文字数不正");
  100. }
  101. $data = substr($dec, 5);
  102. $ret->発行端末 = substr($data, 0, 2);
  103. $ret->発行日 = Carbon::createFromFormat("Ymd", substr($data, 2, 8));
  104. $ret->発行連番 = intval(substr($data, 10, 6));
  105. $ret->顧客コード = substr($data, 16, 4);
  106. $ret->駐車場管理コード = substr($data, 20, 5);
  107. return $ret;
  108. }
  109. }