領収証発行サービス
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.

88 lines
1.9KB

  1. <?php
  2. namespace App\Logic\Contract;
  3. use App\Models\Contract;
  4. use LogicException;
  5. abstract class ContractManager
  6. {
  7. // ------------MEMBER----
  8. protected Contract $contract;
  9. protected bool $initialized = false;
  10. // -------------PUBLIC--------
  11. public function initById(string $id): static
  12. {
  13. $contract = Contract::findOrFail($id);
  14. $this->contract = $contract;
  15. $this->initialized = true;
  16. return $this;
  17. }
  18. public function initForCreate(): static
  19. {
  20. $contract = new Contract();
  21. $contract->setId();
  22. $this->contract = $contract;
  23. $this->initialized = true;
  24. return $this;
  25. }
  26. public function fill(array $attr): static
  27. {
  28. if (!$this->initialized) {
  29. throw new LogicException("初期化ミス");
  30. }
  31. $this->contract->fill($attr);
  32. return $this;
  33. }
  34. public function id(): string
  35. {
  36. if (!$this->initialized) {
  37. throw new LogicException("初期化ミス");
  38. }
  39. return $this->contract->id;
  40. }
  41. // ----------------PROTECTED----------
  42. protected function save(): static
  43. {
  44. $this->contract->save();
  45. return $this;
  46. }
  47. /**
  48. * 契約識別子がUNIQUE判定する
  49. *
  50. * @param array $messages
  51. * @param boolean $forUpdate
  52. * @return boolean
  53. */
  54. protected function checkContractCode(array &$messages, bool $forUpdate): bool
  55. {
  56. $contractCode = $this->contract->contract_code;
  57. if ($contractCode === null) {
  58. return true;
  59. }
  60. $query = Contract::whereContractCode($contractCode);
  61. if ($forUpdate) {
  62. $query->whereNot(Contract::COL_NAME_ID, $this->contract->id);
  63. }
  64. if ($query->exists()) {
  65. $messages[Contract::COL_NAME_CONTRACT_CODE] = "すでに使われています";
  66. return false;
  67. }
  68. return true;
  69. }
  70. }