diff --git a/app/Http/Controllers/Web/Contract/CreateParam.php b/app/Http/Controllers/Web/Contract/CreateParam.php index 1682245..f6db089 100644 --- a/app/Http/Controllers/Web/Contract/CreateParam.php +++ b/app/Http/Controllers/Web/Contract/CreateParam.php @@ -8,6 +8,7 @@ use App\Models\Contract; /** * @property string $name * @property ?string $custom + * @property ?string $contractCode */ class CreateParam extends BaseParam @@ -18,6 +19,7 @@ class CreateParam extends BaseParam [ Contract::COL_NAME_NAME => $this->str(), Contract::COL_NAME_CUSTOM => $this->str(true), + Contract::COL_NAME_CONTRACT_CODE => $this->str(true), ]; } } diff --git a/app/Logic/Contract/ContractManager.php b/app/Logic/Contract/ContractManager.php index 2e304f2..4ed0307 100644 --- a/app/Logic/Contract/ContractManager.php +++ b/app/Logic/Contract/ContractManager.php @@ -57,4 +57,31 @@ abstract class ContractManager $this->contract->save(); return $this; } + + /** + * 契約識別子がUNIQUE判定する + * + * @param array $messages + * @param boolean $forUpdate + * @return boolean + */ + protected function checkContractCode(array &$messages, bool $forUpdate): bool + { + $contractCode = $this->contract->contract_code; + + if ($contractCode === null) { + return true; + } + + $query = Contract::whereContractCode($contractCode); + if ($forUpdate) { + $query->whereNot(Contract::COL_NAME_ID, $this->contract->id); + } + + if ($query->exists()) { + $messages[Contract::COL_NAME_CONTRACT_CODE] = "すでに使われています"; + return false; + } + return true; + } } diff --git a/app/Logic/Contract/CreateManager.php b/app/Logic/Contract/CreateManager.php index 06673ca..209fe04 100644 --- a/app/Logic/Contract/CreateManager.php +++ b/app/Logic/Contract/CreateManager.php @@ -7,8 +7,24 @@ class CreateManager extends ContractManager public function create(): array { $messages = []; + + if (!$this->checkForCreate($messages)) { + return $messages; + } + $this->save(); return $messages; } + + private function checkForCreate(array &$messages): bool + { + + $messages = []; + $ret = true; + + $ret = $this->checkContractCode($messages, false); + + return $ret; + } } diff --git a/app/Models/Contract.php b/app/Models/Contract.php index 6ee9597..05217f5 100644 --- a/app/Models/Contract.php +++ b/app/Models/Contract.php @@ -10,6 +10,7 @@ class Contract extends AppModel const COL_NAME_NAME = 'name'; const COL_NAME_CUSTOM = 'custom'; + const COL_NAME_CONTRACT_CODE = 'contract_code'; public function getModelName(): string { diff --git a/app/Repositories/ContractRepository.php b/app/Repositories/ContractRepository.php index 1d1588c..6cc380e 100644 --- a/app/Repositories/ContractRepository.php +++ b/app/Repositories/ContractRepository.php @@ -65,6 +65,7 @@ class ContractRepository extends BaseRepository $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_ID]), $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_NAME]), $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_CUSTOM]), + $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_CONTRACT_CODE]), $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_UPDATED_AT]), $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_CREATED_AT]), ]; diff --git a/database/migrations/2023_08_22_100200_add_column_contracts_contract_code.php b/database/migrations/2023_08_22_100200_add_column_contracts_contract_code.php new file mode 100644 index 0000000..6d75cbb --- /dev/null +++ b/database/migrations/2023_08_22_100200_add_column_contracts_contract_code.php @@ -0,0 +1,44 @@ +string("contract_code")->nullable()->comment("契約識別子"); + $helper->unique(1, ['contract_code']); + }); + Schema::table('contract_histories', function (Blueprint $table) { + $table->string("contract_code")->nullable()->comment("契約識別子"); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('contracts', function (Blueprint $table) { + $helper = new MigrationHelper($table); + $table->dropColumn("contract_code"); + + $helper->dropIndex(1); + }); + Schema::table('contract_histories', function (Blueprint $table) { + $table->dropColumn("contract_code"); + }); + } +};