From 5201cdb0e305760d5ac6106c33f50c0149c5f6de Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Fri, 1 Nov 2024 09:39:41 +0700 Subject: [PATCH 1/4] Drop unique constraint on 'cif' in debitures table Remove the unique constraint on the 'cif' field in the debitures table and refactor the `UniqueCifExceptZero` validation rule to handle the uniqueness check more robustly. Also, add an unused import statement for `UniqueExcept` in `DebitureRequest`. --- app/Http/Requests/DebitureRequest.php | 1 + app/Rules/UniqueCifExceptZero.php | 41 +++++++++++-------- ...24_11_01_023117_update_debitures_table.php | 28 +++++++++++++ 3 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 database/migrations/2024_11_01_023117_update_debitures_table.php diff --git a/app/Http/Requests/DebitureRequest.php b/app/Http/Requests/DebitureRequest.php index 7f83fbb..40b608e 100644 --- a/app/Http/Requests/DebitureRequest.php +++ b/app/Http/Requests/DebitureRequest.php @@ -4,6 +4,7 @@ use Illuminate\Foundation\Http\FormRequest; use Modules\Lpj\Rules\UniqueCifExceptZero; + use Modules\Lpj\Rules\UniqueExcept; class DebitureRequest extends FormRequest { diff --git a/app/Rules/UniqueCifExceptZero.php b/app/Rules/UniqueCifExceptZero.php index c8173d1..03accd3 100644 --- a/app/Rules/UniqueCifExceptZero.php +++ b/app/Rules/UniqueCifExceptZero.php @@ -1,25 +1,32 @@ id = $id; - } + protected $id; - public function validate($attribute, $value, $fail): void - { - if (Debiture::where($attribute, $value) - ->where('id', '!=', $this->id) - ->where($attribute, '!=', '0000000000') - ->exists()) { - $fail('The :attribute field must be uniquse.'.$this->id); + public function __construct($id = null) + { + $this->id = $id; + } + + /** + * Run the validation rule. + */ + public function validate(string $attribute, mixed $value, Closure $fail) + : void { + if ($value !== '0000000000' && $value !== null && Debiture::query()->where($attribute, $value)->when( + $this->id, + function ($query) { + $query->where('id', '!=', $this->id); + }, + )->exists()) { + $fail('The :attribute field must be unique.'); + } } } -} diff --git a/database/migrations/2024_11_01_023117_update_debitures_table.php b/database/migrations/2024_11_01_023117_update_debitures_table.php new file mode 100644 index 0000000..cadf6b0 --- /dev/null +++ b/database/migrations/2024_11_01_023117_update_debitures_table.php @@ -0,0 +1,28 @@ +dropUnique(['cif']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('debitures', function (Blueprint $table) { + $table->string('cif')->unique()->change(); + }); + } +}; From 24b38ca4f19fc671af5b0101c0957a49e1dc36e4 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Fri, 1 Nov 2024 16:53:31 +0700 Subject: [PATCH 2/4] Add support for multiple detail sertifikat Enhanced the request validation, model, and Blade template to support multiple detail sertifikat entries, which are now encoded in JSON format. Additionally, added a dynamic form for entry, including delete functionality for each sertifikat entry. --- .../Controllers/PemilikJaminanController.php | 3 + app/Http/Requests/PemilikJaminanRequest.php | 23 +++++ app/Models/PemilikJaminan.php | 3 +- .../views/pemilik_jaminan/form.blade.php | 92 ++++++++++++------- 4 files changed, 87 insertions(+), 34 deletions(-) diff --git a/app/Http/Controllers/PemilikJaminanController.php b/app/Http/Controllers/PemilikJaminanController.php index 7c38601..b8e0ecd 100644 --- a/app/Http/Controllers/PemilikJaminanController.php +++ b/app/Http/Controllers/PemilikJaminanController.php @@ -63,6 +63,7 @@ public function update(PemilikJaminanRequest $request, $id, $pemilik) { $validate = $request->validated(); + if ($validate) { try { $pemilik = PemilikJaminan::find($pemilik); @@ -88,6 +89,7 @@ $districts = District::where('city_code', $pemilik->city_code)->get(); $villages = Village::where('district_code', $pemilik->district_code)->get(); $hubunganPemilik = HubunganPemilikJaminan::all(); + $detailSertifikat = $pemilik->detail_sertifikat; return view( 'lpj::pemilik_jaminan.form', @@ -99,6 +101,7 @@ 'villages', 'hubunganPemilik', 'pemilik', + 'detailSertifikat' ), ); } diff --git a/app/Http/Requests/PemilikJaminanRequest.php b/app/Http/Requests/PemilikJaminanRequest.php index b6e267d..ca76c83 100644 --- a/app/Http/Requests/PemilikJaminanRequest.php +++ b/app/Http/Requests/PemilikJaminanRequest.php @@ -26,6 +26,7 @@ 'address' => 'nullable|string', 'postal_code' => 'nullable|string|max:10', 'status' => 'nullable|boolean', + 'detail_sertifikat' => 'nullable|string|max:255', ]; //$rules['nomor_id'] = 'nullable|max:16|unique:pemilik_jaminan,nomor_id,debiture_id,' . $this->debiture_id; @@ -41,4 +42,26 @@ { return true; } + + public function prepareForValidation() { + + $detailSertifikat = []; + $names = $this->input('detail_sertifikat.name', []); + $nomorIds = $this->input('detail_sertifikat.nomor_id', []); + + foreach ($names as $index => $name) { + if (isset($nomorIds[$index])) { + $detailSertifikat[] = [ + 'name' => $name, + 'nomor_id' => $nomorIds[$index] + ]; + } + } + + $this->merge([ + 'detail_sertifikat' => json_encode($detailSertifikat), + ]); + + + } } diff --git a/app/Models/PemilikJaminan.php b/app/Models/PemilikJaminan.php index d92dc0c..6f0addc 100644 --- a/app/Models/PemilikJaminan.php +++ b/app/Models/PemilikJaminan.php @@ -30,7 +30,8 @@ class PemilikJaminan extends Base 'status', 'authorized_at', 'authorized_status', - 'authorized_by' + 'authorized_by', + 'detail_sertifikat', ]; public function province() diff --git a/resources/views/pemilik_jaminan/form.blade.php b/resources/views/pemilik_jaminan/form.blade.php index 03c4a11..27af2b1 100644 --- a/resources/views/pemilik_jaminan/form.blade.php +++ b/resources/views/pemilik_jaminan/form.blade.php @@ -79,7 +79,7 @@ @enderror
- + @error('nomor_id') {{ $message }} @enderror @@ -88,7 +88,28 @@
+ @if(isset($detailSertifikat)) + @foreach(json_decode($detailSertifikat) as $sertifikat) +
+ +
+
+
+ +
+
+ + +
+ +
+
+
+ @endforeach + @endif
@endsection - @push('scripts') From 5f5882d4204011a8ec2e80d1851ec32d515929fb Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Fri, 1 Nov 2024 16:57:34 +0700 Subject: [PATCH 3/4] Add detail_sertifikat column to pemilik_jaminan table This migration introduces a new nullable string column named detail_sertifikat to the pemilik_jaminan table. It allows adding certificate details for each pemilik jaminan. The down method ensures this column is dropped if the migration is rolled back. --- ...01_083625_update_pemilik_jaminan_table.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 database/migrations/2024_11_01_083625_update_pemilik_jaminan_table.php diff --git a/database/migrations/2024_11_01_083625_update_pemilik_jaminan_table.php b/database/migrations/2024_11_01_083625_update_pemilik_jaminan_table.php new file mode 100644 index 0000000..48f8921 --- /dev/null +++ b/database/migrations/2024_11_01_083625_update_pemilik_jaminan_table.php @@ -0,0 +1,28 @@ +string('detail_sertifikat')->nullable()->after('nama_pemilik'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('pemilik_jaminan', function (Blueprint $table) { + $table->dropColumn('detail_sertifikat'); + }); + } +}; From 56ba6ddae15ef765fe9684347a39bb8d72714576 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Fri, 1 Nov 2024 16:59:19 +0700 Subject: [PATCH 4/4] Update column position in pemilik_jaminan migration Changed the position of 'detail_sertifikat' column to come after 'name' instead of 'nama_pemilik' in the migration. This ensures consistency with other tables and prevents errors during deployment. --- .../2024_11_01_083625_update_pemilik_jaminan_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2024_11_01_083625_update_pemilik_jaminan_table.php b/database/migrations/2024_11_01_083625_update_pemilik_jaminan_table.php index 48f8921..ec38fd5 100644 --- a/database/migrations/2024_11_01_083625_update_pemilik_jaminan_table.php +++ b/database/migrations/2024_11_01_083625_update_pemilik_jaminan_table.php @@ -12,7 +12,7 @@ return new class extends Migration public function up(): void { Schema::table('pemilik_jaminan', function (Blueprint $table) { - $table->string('detail_sertifikat')->nullable()->after('nama_pemilik'); + $table->string('detail_sertifikat')->nullable()->after('name'); }); }