| @@ -8,6 +8,7 @@ use App\Models\Contract; | |||||
| /** | /** | ||||
| * @property string $name | * @property string $name | ||||
| * @property ?string $custom | * @property ?string $custom | ||||
| * @property ?string $contractCode | |||||
| */ | */ | ||||
| class CreateParam extends BaseParam | class CreateParam extends BaseParam | ||||
| @@ -18,6 +19,7 @@ class CreateParam extends BaseParam | |||||
| [ | [ | ||||
| Contract::COL_NAME_NAME => $this->str(), | Contract::COL_NAME_NAME => $this->str(), | ||||
| Contract::COL_NAME_CUSTOM => $this->str(true), | Contract::COL_NAME_CUSTOM => $this->str(true), | ||||
| Contract::COL_NAME_CONTRACT_CODE => $this->str(true), | |||||
| ]; | ]; | ||||
| } | } | ||||
| } | } | ||||
| @@ -57,4 +57,31 @@ abstract class ContractManager | |||||
| $this->contract->save(); | $this->contract->save(); | ||||
| return $this; | 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; | |||||
| } | |||||
| } | } | ||||
| @@ -7,8 +7,24 @@ class CreateManager extends ContractManager | |||||
| public function create(): array | public function create(): array | ||||
| { | { | ||||
| $messages = []; | $messages = []; | ||||
| if (!$this->checkForCreate($messages)) { | |||||
| return $messages; | |||||
| } | |||||
| $this->save(); | $this->save(); | ||||
| return $messages; | return $messages; | ||||
| } | } | ||||
| private function checkForCreate(array &$messages): bool | |||||
| { | |||||
| $messages = []; | |||||
| $ret = true; | |||||
| $ret = $this->checkContractCode($messages, false); | |||||
| return $ret; | |||||
| } | |||||
| } | } | ||||
| @@ -10,6 +10,7 @@ class Contract extends AppModel | |||||
| const COL_NAME_NAME = 'name'; | const COL_NAME_NAME = 'name'; | ||||
| const COL_NAME_CUSTOM = 'custom'; | const COL_NAME_CUSTOM = 'custom'; | ||||
| const COL_NAME_CONTRACT_CODE = 'contract_code'; | |||||
| public function getModelName(): string | public function getModelName(): string | ||||
| { | { | ||||
| @@ -65,6 +65,7 @@ class ContractRepository extends BaseRepository | |||||
| $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_ID]), | $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_ID]), | ||||
| $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_NAME]), | $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_NAME]), | ||||
| $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_CUSTOM]), | $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_UPDATED_AT]), | ||||
| $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_CREATED_AT]), | $this->makeColumnNameForSelect([$contract, Contract::COL_NAME_CREATED_AT]), | ||||
| ]; | ]; | ||||
| @@ -0,0 +1,44 @@ | |||||
| <?php | |||||
| use App\Util\MigrationHelper; | |||||
| use Illuminate\Database\Migrations\Migration; | |||||
| use Illuminate\Database\Schema\Blueprint; | |||||
| use Illuminate\Support\Facades\Schema; | |||||
| return new class extends Migration | |||||
| { | |||||
| /** | |||||
| * Run the migrations. | |||||
| * | |||||
| * @return void | |||||
| */ | |||||
| public function up() | |||||
| { | |||||
| Schema::table('contracts', function (Blueprint $table) { | |||||
| $helper = new MigrationHelper($table); | |||||
| $table->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"); | |||||
| }); | |||||
| } | |||||
| }; | |||||