diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php new file mode 100644 index 0000000..1ba2d42 --- /dev/null +++ b/app/Http/Controllers/CompanyController.php @@ -0,0 +1,78 @@ + Company::with(['region'])->paginate(), + 'regions' => Region::all(), + ]); + } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { + $request->validate([ + 'name' => 'required|string|max:255', + 'short' => 'required|string|max:255', + 'region_id' => 'required|exists:regions,id' + ]); + + Company::create([ + 'region_id' => $request->region_id, + 'name' => $request->name, + 'short' => $request->short + ]); + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param \App\Models\Company $company + * @return \Illuminate\Http\Response + */ + public function update(Request $request, Company $company) + { + $request->validate([ + 'name' => 'required|string|max:255', + 'short' => 'required|string|max:255', + 'region_id' => 'required|exists:regions,id' + ]); + + $company->update([ + 'region_id' => $request->region_id, + 'name' => $request->name, + 'short' => $request->short + ]); + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\Company $company + * @return \Illuminate\Http\Response + */ + public function destroy(Company $company) + { + $company->users()->delete(); + $company->delete(); + } +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 75d51d7..f9c04f8 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -15,6 +15,11 @@ class Company extends Model "short" ]; + public function region() + { + return $this->belongsTo(Region::class); + } + public function users() { return $this->hasMany(User::class); diff --git a/app/Models/Document.php b/app/Models/Document.php index 853aab1..188862d 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -21,7 +21,6 @@ class Document extends Model "no", "no_doc", "name", - "company_name", "type_id", "category_id", "publisher", @@ -30,10 +29,12 @@ class Document extends Model "due_date", "status", "type", - "group", - "region", "document", "user_id", + "company_id", + // "company_name", + // "group", + // "region", ]; protected $casts = [ diff --git a/database/migrations/2022_09_18_034528_create_documents_table.php b/database/migrations/2022_09_18_034528_create_documents_table.php index 07afc35..1ceb2b7 100644 --- a/database/migrations/2022_09_18_034528_create_documents_table.php +++ b/database/migrations/2022_09_18_034528_create_documents_table.php @@ -17,7 +17,6 @@ return new class extends Migration { $table->string("no")->nullable(); $table->string("no_doc")->nullable(); $table->string("name")->nullable(); - $table->string("company_name")->nullable(); $table->foreignId("type_id")->constrained(); //select jenis $table->foreignId("category_id")->constrained(); //select $table->string("publisher")->nullable(); @@ -26,10 +25,15 @@ return new class extends Migration { $table->timestamp("due_date")->nullable(); //for reminder $table->smallInteger("status")->default(1); //only 1 yes/ 0no $table->smallInteger("type")->default(1); //only 1 tetap/ 0tidak tetap - $table->string("group")->nullable(); - $table->string("region")->nullable(); $table->string("document")->nullable(); $table->foreignId("user_id")->constrained(); + + $table->foreignId("company_id")->constrained(); + // ? + // $table->string("group")->nullable(); + // $table->string("region")->nullable(); + // $table->string("company_name")->nullable(); + $table->timestamps(); }); } diff --git a/resources/js/Layouts/AuthenticatedLayout.jsx b/resources/js/Layouts/AuthenticatedLayout.jsx index d4e0b5f..584104b 100644 --- a/resources/js/Layouts/AuthenticatedLayout.jsx +++ b/resources/js/Layouts/AuthenticatedLayout.jsx @@ -16,7 +16,7 @@ const rs = [ {name: "Perusahaan", show: true, items: [ {name:"Group", route: "groups.index", show: true, permission: 'view-group'}, {name:"Region", route: "regions.index", show: true, permission: 'view-region'}, - {name:"Perusahaan", route: "groups.index", show: true, permission: 'view-company'}, + {name:"Perusahaan", route: "companies.index", show: true, permission: 'view-company'}, ]}, {name: "User", show: true, items: [ {name:"User", route: "users.index", show: true, permission: 'view-user'}, diff --git a/resources/js/Pages/Company/FormModal.jsx b/resources/js/Pages/Company/FormModal.jsx new file mode 100644 index 0000000..a5c2bc6 --- /dev/null +++ b/resources/js/Pages/Company/FormModal.jsx @@ -0,0 +1,158 @@ +import React, { useEffect } from 'react' +import { useForm, usePage } from '@inertiajs/react' +import { toast } from 'react-toastify' + +export default function FormModal(props) { + const { modalState } = props + const { props: { regions } } = usePage() + + const { data, setData, post, put, processing, errors, reset, clearErrors } = useForm({ + name: '', + short: '', + region_id: '' + }) + + const handleOnChange = (event) => { + setData(event.target.name, event.target.type === 'checkbox' ? event.target.checked : event.target.value); + } + + const handleReset = () => { + reset() + clearErrors() + modalState.setData(null) + } + + const handleCancel = () => { + handleReset() + modalState.toggle() + } + + const handleSubmit = () => { + const company = modalState.data + if(company !== null) { + put(route('companies.update', company), { + onSuccess: () => + Promise.all([ + handleReset(), + modalState.toggle(), + toast.success('The Data has been changed'), + ]), + }) + return + } + post(route('companies.store'), { + onSuccess: () => + Promise.all([ + handleReset(), + modalState.toggle(), + toast.success('The Data has been saved'), + ]), + }) + } + + useEffect(() => { + const company = modalState.data + if (company !== null) { + setData({ + name: company?.name, + short: company?.short, + region_id: company?.region_id, + }) + } + }, [modalState]) + + return ( +
Id | +Nama | +Inisial | +Region | ++ |
---|---|---|---|---|
{company.id} | +{company.name} | +{company.short} | +{company.region.name} | +
+ {canUpdate && (
+
+ toggle(company)
+ }
+ >
+ Edit
+
+ )}
+ {canDelete && (
+
+ handleDelete(company)
+ }
+ >
+ Delete
+
+ )}
+ |
+