From dd5dbcab8fff8c4a460fd073f71a9eb17614e919 Mon Sep 17 00:00:00 2001 From: Fiqh Pratama Date: Mon, 13 Oct 2025 10:55:21 +0700 Subject: [PATCH] enhanced services attachment --- app/Enums/AttachmentTableName.php | 26 +++++++ app/Models/AttachmentForm.php | 28 ++++++++ app/Models/FormEntertaimentPresentation.php | 7 ++ app/Models/FormMeetingSeminar.php | 6 ++ app/Models/FormOthers.php | 6 ++ app/Models/FormUpCountry.php | 6 ++ app/Models/FormVehicleRunningCost.php | 6 ++ app/Services/AttachmentService.php | 71 +++++++++++++++++++ ...01_000000_create_attachment_form_table.php | 28 ++++++++ 9 files changed, 184 insertions(+) create mode 100644 app/Enums/AttachmentTableName.php create mode 100644 app/Models/AttachmentForm.php create mode 100644 app/Services/AttachmentService.php create mode 100644 database/migrations/2025_01_01_000000_create_attachment_form_table.php diff --git a/app/Enums/AttachmentTableName.php b/app/Enums/AttachmentTableName.php new file mode 100644 index 0000000..4c8a258 --- /dev/null +++ b/app/Enums/AttachmentTableName.php @@ -0,0 +1,26 @@ +morphTo(__FUNCTION__, 'table_name', 'form_id'); + } +} diff --git a/app/Models/FormEntertaimentPresentation.php b/app/Models/FormEntertaimentPresentation.php index c1949b9..492ee0f 100644 --- a/app/Models/FormEntertaimentPresentation.php +++ b/app/Models/FormEntertaimentPresentation.php @@ -37,4 +37,11 @@ class FormEntertaimentPresentation extends Model { return $this->belongsTo('App\Models\Admin'); } + + public function attachments() + { + return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id') + ->where('table_name', \App\Enums\AttachmentTableName::FORM_ENTERTAINMENT_PRESENTATION); + } + } diff --git a/app/Models/FormMeetingSeminar.php b/app/Models/FormMeetingSeminar.php index 0d2a9a2..5dd5f94 100644 --- a/app/Models/FormMeetingSeminar.php +++ b/app/Models/FormMeetingSeminar.php @@ -40,4 +40,10 @@ class FormMeetingSeminar extends Model { return $this->belongsTo(Admin::class, 'user_id'); } + + public function attachments() + { + return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id') + ->where('table_name', \App\Enums\AttachmentTableName::FORM_MEETING_SEMINAR); + } } diff --git a/app/Models/FormOthers.php b/app/Models/FormOthers.php index b373263..c6f5e1b 100644 --- a/app/Models/FormOthers.php +++ b/app/Models/FormOthers.php @@ -39,4 +39,10 @@ class FormOthers extends Model { return $this->belongsTo(Kategori::class); } + + public function attachments() + { + return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id') + ->where('table_name', \App\Enums\AttachmentTableName::FORM_OTHERS); + } } diff --git a/app/Models/FormUpCountry.php b/app/Models/FormUpCountry.php index e92ee34..e20c3c5 100644 --- a/app/Models/FormUpCountry.php +++ b/app/Models/FormUpCountry.php @@ -52,4 +52,10 @@ class FormUpCountry extends Model { return $this->belongsTo('App\Models\Rayon'); } + + public function attachments() + { + return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id') + ->where('table_name', \App\Enums\AttachmentTableName::FORM_UP_COUNTRY); + } } diff --git a/app/Models/FormVehicleRunningCost.php b/app/Models/FormVehicleRunningCost.php index 2e2b9d8..a2e3708 100644 --- a/app/Models/FormVehicleRunningCost.php +++ b/app/Models/FormVehicleRunningCost.php @@ -38,4 +38,10 @@ class FormVehicleRunningCost extends Model { return $this->belongsTo('App\Models\Admin', 'user_id'); } + + public function attachments() + { + return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id') + ->where('table_name', \App\Enums\AttachmentTableName::FORM_VEHICLE_RUNNING_COST); + } } diff --git a/app/Services/AttachmentService.php b/app/Services/AttachmentService.php new file mode 100644 index 0000000..545b273 --- /dev/null +++ b/app/Services/AttachmentService.php @@ -0,0 +1,71 @@ + $formId, + 'table_name' => $tableName, + 'file_category'=> $category, + 'file_path' => $filePath, + ]); + } + + /** + * Store multiple attachments + */ + public function addMultipleAttachments(int $formId, string $tableName, array $attachments): void + { + $data = []; + + foreach ($attachments as $file) { + $data[] = [ + 'form_id' => $formId, + 'table_name' => $tableName, + 'file_category' => $file['file_category'] ?? null, + 'file_path' => $file['file_path'], + 'created_at' => now(), + 'updated_at' => now(), + ]; + } + + AttachmentForm::insert($data); + } + + /** + * Get attachments by form + */ + public function getAttachments(int $formId, string $tableName) + { + return AttachmentForm::where('form_id', $formId) + ->where('table_name', $tableName) + ->get(); + } + + /** + * Delete attachment by ID (soft delete) + */ + public function deleteAttachment(int $attachmentId): bool + { + return AttachmentForm::where('id', $attachmentId)->delete(); + } + + /** + * Delete all attachment by form + */ + public function deleteByForm(int $formId, string $tableName): bool + { + return AttachmentForm::where('form_id', $formId) + ->where('table_name', $tableName) + ->delete(); + } +} diff --git a/database/migrations/2025_01_01_000000_create_attachment_form_table.php b/database/migrations/2025_01_01_000000_create_attachment_form_table.php new file mode 100644 index 0000000..e8c11ce --- /dev/null +++ b/database/migrations/2025_01_01_000000_create_attachment_form_table.php @@ -0,0 +1,28 @@ +bigIncrements('id'); + $table->unsignedBigInteger('form_id'); + $table->string('table_name', 100)->comment('nama tabel form yang direferensikan'); + $table->string('file_category', 100)->nullable(); + $table->string('file_path', 255); + $table->timestamps(); + $table->softDeletes(); + + $table->index(['form_id', 'table_name'], 'idx_form_table'); + }); + } + + public function down(): void + { + Schema::dropIfExists('attachment_form'); + } +};