From 6bc36a5c6fb6d98650ea4632d2a51639029fb6d0 Mon Sep 17 00:00:00 2001 From: "daeng.deni@dharma.or.id" Date: Sat, 3 Jun 2023 20:46:02 +0700 Subject: [PATCH] Initial Commit Module Company --- Config/.gitkeep | 0 Config/config.php | 5 + Console/.gitkeep | 0 DataTables/CompanyDataTable.php | 109 +++++++++++ Database/Migrations/.gitkeep | 0 ...23_06_03_003243_create_companies_table.php | 42 +++++ Database/Seeders/.gitkeep | 0 Database/Seeders/CompanyDatabaseSeeder.php | 21 +++ Database/factories/.gitkeep | 0 Entities/.gitkeep | 0 Entities/BaseModel.php | 34 ++++ Entities/Company.php | 18 ++ Http/Controllers/.gitkeep | 0 Http/Controllers/CompanyController.php | 174 ++++++++++++++++++ Http/Middleware/.gitkeep | 0 Http/Requests/.gitkeep | 0 Http/Requests/StoreCompanyRequest.php | 72 ++++++++ Http/Requests/UpdateCompanyRequest.php | 72 ++++++++ Providers/.gitkeep | 0 Providers/CompanyServiceProvider.php | 114 ++++++++++++ Providers/RouteServiceProvider.php | 69 +++++++ Resources/lang/.gitkeep | 0 Resources/views/.gitkeep | 0 Resources/views/_action.blade.php | 17 ++ Resources/views/_form.blade.php | 136 ++++++++++++++ Resources/views/_table.blade.php | 121 ++++++++++++ Resources/views/index.blade.php | 140 ++++++++++++++ Routes/.gitkeep | 0 Routes/api.php | 18 ++ Routes/web.php | 18 ++ composer.json | 24 +++ module.json | 12 ++ 32 files changed, 1216 insertions(+) create mode 100644 Config/.gitkeep create mode 100644 Config/config.php create mode 100644 Console/.gitkeep create mode 100644 DataTables/CompanyDataTable.php create mode 100644 Database/Migrations/.gitkeep create mode 100644 Database/Migrations/2023_06_03_003243_create_companies_table.php create mode 100644 Database/Seeders/.gitkeep create mode 100644 Database/Seeders/CompanyDatabaseSeeder.php create mode 100644 Database/factories/.gitkeep create mode 100644 Entities/.gitkeep create mode 100644 Entities/BaseModel.php create mode 100644 Entities/Company.php create mode 100644 Http/Controllers/.gitkeep create mode 100644 Http/Controllers/CompanyController.php create mode 100644 Http/Middleware/.gitkeep create mode 100644 Http/Requests/.gitkeep create mode 100644 Http/Requests/StoreCompanyRequest.php create mode 100644 Http/Requests/UpdateCompanyRequest.php create mode 100644 Providers/.gitkeep create mode 100644 Providers/CompanyServiceProvider.php create mode 100644 Providers/RouteServiceProvider.php create mode 100644 Resources/lang/.gitkeep create mode 100644 Resources/views/.gitkeep create mode 100644 Resources/views/_action.blade.php create mode 100644 Resources/views/_form.blade.php create mode 100644 Resources/views/_table.blade.php create mode 100644 Resources/views/index.blade.php create mode 100644 Routes/.gitkeep create mode 100644 Routes/api.php create mode 100644 Routes/web.php create mode 100644 composer.json create mode 100644 module.json diff --git a/Config/.gitkeep b/Config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Config/config.php b/Config/config.php new file mode 100644 index 0000000..5ede170 --- /dev/null +++ b/Config/config.php @@ -0,0 +1,5 @@ + 'Company' +]; diff --git a/Console/.gitkeep b/Console/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/DataTables/CompanyDataTable.php b/DataTables/CompanyDataTable.php new file mode 100644 index 0000000..ce25f14 --- /dev/null +++ b/DataTables/CompanyDataTable.php @@ -0,0 +1,109 @@ +eloquent($query) + ->filter(function ($query) { + $search = request()->get('search'); + if ($search['value'] !== "") { + $query->where('name', 'like', "%" . $search['value'] . "%") + ->orWhere('address', 'like', "%" . $search['value'] . "%") + ->orWhere('phone', 'like', "%" . $search['value'] . "%") + ->orWhere('website', 'like', "%" . $search['value'] . "%") + ->orWhere('email', 'like', "%" . $search['value'] . "%") + ->orWhere('npwp', 'like', "%" . $search['value'] . "%") + ->orWhere('company_type', 'like', "%" . $search['value'] . "%"); + } + }) + ->addIndexColumn() + ->addColumn('action', function ($model) { + return view('company::_action', compact('model')); + }); + } + + /** + * Get query source of dataTable. + * + * @param \Modules\Company\Entities\Company $model + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function query(Company $model) + { + return $model->newQuery(); + } + + /** + * Optional method if you want to use html builder. + * + * @return \Yajra\DataTables\Html\Builder + */ + public function html() + { + return $this->builder() + ->setTableId('company-table') + ->columns($this->getColumns()) + ->minifiedAjax() + ->orderBy(1, 'asc') + ->stateSave(false) + ->responsive() + ->autoWidth(false) + ->parameters([ + 'scrollX' => true, + 'drawCallback' => 'function() { KTMenu.createInstances(); }', + ]) + ->addTableClass('align-middle table-row-dashed fs-6 gy-5'); + } + + /** + * Get columns. + * + * @return array + */ + protected function getColumns() + { + return [ + Column::make('DT_RowIndex')->title('No')->orderable(false)->searchable(false), + Column::make('name')->title(__('Name')), + Column::make('npwp')->title(__('NPWP')), + Column::make('address')->title(__('Address')), + Column::make('email'), + Column::make('phone')->title(__('Phone')), + Column::make('website')->title(__('Website')), + Column::make('company_type')->title(__('Company Type')), + Column::computed('action') + ->exportable(false) + ->printable(false) + ->addClass('text-center') + ->responsivePriority(-1), + ]; + } + + /** + * Get filename for export. + * + * @return string + */ + protected function filename() + : string + { + return 'Company_' . date('YmdHis'); + } + } diff --git a/Database/Migrations/.gitkeep b/Database/Migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Database/Migrations/2023_06_03_003243_create_companies_table.php b/Database/Migrations/2023_06_03_003243_create_companies_table.php new file mode 100644 index 0000000..e635c53 --- /dev/null +++ b/Database/Migrations/2023_06_03_003243_create_companies_table.php @@ -0,0 +1,42 @@ +id(); + $table->string("name"); + $table->string("npwp"); + $table->string("address")->nullable(); + $table->string("phone")->nullable(); + $table->string("email")->nullable(); + $table->string("website")->nullable(); + $table->enum("company_type", ["PKP", "Non PKP"]); + $table->char("status", 1)->default(1); + $table->timestamps(); + $table->softDeletes(); + $table->unsignedBigInteger("created_by")->nullable(); + $table->unsignedBigInteger("updated_by")->nullable(); + $table->unsignedBigInteger("deleted_by")->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('companies'); + } + }; diff --git a/Database/Seeders/.gitkeep b/Database/Seeders/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Database/Seeders/CompanyDatabaseSeeder.php b/Database/Seeders/CompanyDatabaseSeeder.php new file mode 100644 index 0000000..49302c7 --- /dev/null +++ b/Database/Seeders/CompanyDatabaseSeeder.php @@ -0,0 +1,21 @@ +call("OthersTableSeeder"); + } +} diff --git a/Database/factories/.gitkeep b/Database/factories/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Entities/.gitkeep b/Entities/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Entities/BaseModel.php b/Entities/BaseModel.php new file mode 100644 index 0000000..5b12cdd --- /dev/null +++ b/Entities/BaseModel.php @@ -0,0 +1,34 @@ +connection = $module->database; + } + + public function getActivitylogOptions() + : LogOptions + { + return LogOptions::defaults()->logAll() + ->useLogName('Company : '); + } + + } diff --git a/Entities/Company.php b/Entities/Company.php new file mode 100644 index 0000000..2d5bbed --- /dev/null +++ b/Entities/Company.php @@ -0,0 +1,18 @@ +middleware(function ($request, $next) { + $this->user = Auth::guard('web')->user(); + return $next($request); + }); + + $module = file_get_contents(dirname(__FILE__, 3) . '/module.json'); + $this->module = json_decode($module); + } + + /** + * Display a listing of the resource. + * + * @return Renderable + */ + public function index(CompanyDataTable $dataTable) + { + if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) { + abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !'); + } + + return $dataTable->render($this->module->alias . '::index'); + } + + /** + * Store a newly created resource in storage. + * + * @param Request $request + * + * @return Renderable + */ + public function store(StoreCompanyRequest $request) + { + if (is_null($this->user) || !$this->user->can($this->module->alias . '.create')) { + abort(403, 'Sorry !! You are Unauthorized to create any ' . $this->module->alias . ' !'); + } + + //Validate the request + $validated = $request->validated(); + + // Store the Company... + if ($validated) { + try { + $this->model::create($validated); + echo json_encode(['status' => 'success', 'message' => $this->module->name . ' created successfully.']); + } catch (Exception $e) { + echo json_encode(['status' => 'error', 'message' => $this->module->name . ' created failed.']); + } + return; + } + + echo json_encode(['status' => 'error', 'message' => $this->module->name . ' created failed.']); + } + + /** + * Show the form for creating a new resource. + * + * @return Renderable + */ + public function create() + { + if (is_null($this->user) || !$this->user->can($this->module->alias . '.create')) { + abort(403, 'Sorry !! You are Unauthorized to create any ' . $this->module->alias . ' !'); + } + + abort(404); + } + + /** + * Show the specified resource. + * + * @param int $id + * + * @return Renderable + */ + public function show($id) + { + if(is_null($this->user) || !$this->user->can($this->module->alias . '.read')) { + abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !'); + } + + abort(404); + } + + /** + * Show the form for editing the specified resource. + * + * @param int $id + * + * @return Renderable + */ + public function edit($id) + { + if (is_null($this->user) || !$this->user->can($this->module->alias . '.update')) { + abort(403, 'Sorry !! You are Unauthorized to update any ' . $this->module->alias . ' !'); + } + + $data = $this->model::find($id); + echo json_encode($data); + } + + /** + * Update the specified resource in storage. + * + * @param Request $request + * @param int $id + * + * @return Renderable + */ + public function update(UpdateCompanyRequest $request, Company $company) + { + if (is_null($this->user) || !$this->user->can($this->module->alias . '.update')) { + abort(403, 'Sorry !! You are Unauthorized to update any ' . $this->module->alias . ' !'); + } + + //Validate the request + $validated = $request->validated(); + + // Update the Company... + if ($validated) { + try { + $company->update($validated); + echo json_encode(['status' => 'success', 'message' => $this->module->name . ' updated successfully.']); + } catch (Exception $e) { + echo json_encode(['status' => 'error', 'message' => $this->module->name . ' updated failed.']); + } + return; + } + + echo json_encode(['status' => 'error', 'message' => $this->module->name . ' updated failed.']); + } + + /** + * Remove the specified resource from storage. + * + * @param int $id + * + * @return Renderable + */ + public function destroy(Company $company) + { + if (is_null($this->user) || !$this->user->can($this->module->alias . '.delete')) { + abort(403, 'Sorry !! You are Unauthorized to delete any ' . $this->module->alias . ' !'); + } + + try { + $company->delete(); + echo json_encode(['status' => 'success', 'message' => $this->module->name . ' deleted successfully.']); + } catch (Exception $e) { + echo json_encode(['status' => 'error', 'message' => $this->module->name . ' deleted failed.']); + } + } + } diff --git a/Http/Middleware/.gitkeep b/Http/Middleware/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Http/Requests/.gitkeep b/Http/Requests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Http/Requests/StoreCompanyRequest.php b/Http/Requests/StoreCompanyRequest.php new file mode 100644 index 0000000..eaa75d2 --- /dev/null +++ b/Http/Requests/StoreCompanyRequest.php @@ -0,0 +1,72 @@ + + */ + public function rules() + : array + { + return [ + 'name' => 'required|string|max:50|unique:companies,name', + 'address' => 'required|string|max:255', + 'phone' => 'required|string|max:20', + 'website' => 'required|string|max:50', + 'email' => 'required|string|max:50', + 'company_type' => 'required|string|max:10', + 'npwp' => 'required|string|max:50', + 'status' => 'nullable|string|max:1', + ]; + } + + /** + * Configure the validator instance. + */ + public function withValidator(Validator $validator) + : void + { + $validator->after(function (Validator $validator) { + if ($validator->errors()->any()) { + $errors = json_decode($validator->errors()->toJson(), true); + + foreach ($errors as $key => $value) { + flash($value[0]); + } + return redirect()->route('company.index')->with('error', 'Company created failed.'); + } + + }); + } + + protected function failedValidation(Validator|\Illuminate\Contracts\Validation\Validator $validator) + : JsonResponse + { + $errors = (new ValidationException($validator))->errors(); + + throw new HttpResponseException(response()->json([ + 'success' => false, + 'errors' => $errors, + 'messages' => 'Company created failed.' + ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)); + } + } diff --git a/Http/Requests/UpdateCompanyRequest.php b/Http/Requests/UpdateCompanyRequest.php new file mode 100644 index 0000000..e34cfdd --- /dev/null +++ b/Http/Requests/UpdateCompanyRequest.php @@ -0,0 +1,72 @@ + + */ + public function rules() + : array + { + return [ + 'name' => 'required|string|max:50|unique:companies,name,' . $this->company->id, + 'address' => 'required|string|max:255', + 'phone' => 'required|string|max:20', + 'website' => 'required|string|max:50', + 'email' => 'required|string|max:50', + 'company_type' => 'required|string|max:10', + 'npwp' => 'required|string|max:50', + 'status' => 'nullable|string|max:1', + ]; + } + + /** + * Configure the validator instance. + */ + public function withValidator(Validator $validator) + : void + { + $validator->after(function (Validator $validator) { + if ($validator->errors()->any()) { + $errors = json_decode($validator->errors()->toJson(), true); + + foreach ($errors as $key => $value) { + flash($value[0]); + } + return redirect()->route('company.index')->with('error', 'Company created failed.'); + } + + }); + } + + protected function failedValidation(Validator|\Illuminate\Contracts\Validation\Validator $validator) + : JsonResponse + { + $errors = (new ValidationException($validator))->errors(); + + throw new HttpResponseException(response()->json([ + 'success' => false, + 'errors' => $errors, + 'messages' => 'Company created failed.' + ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)); + } + } diff --git a/Providers/.gitkeep b/Providers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Providers/CompanyServiceProvider.php b/Providers/CompanyServiceProvider.php new file mode 100644 index 0000000..5a19343 --- /dev/null +++ b/Providers/CompanyServiceProvider.php @@ -0,0 +1,114 @@ +registerTranslations(); + $this->registerConfig(); + $this->registerViews(); + $this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations')); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->register(RouteServiceProvider::class); + } + + /** + * Register config. + * + * @return void + */ + protected function registerConfig() + { + $this->publishes([ + module_path($this->moduleName, 'Config/config.php') => config_path($this->moduleNameLower . '.php'), + ], 'config'); + $this->mergeConfigFrom( + module_path($this->moduleName, 'Config/config.php'), $this->moduleNameLower + ); + } + + /** + * Register views. + * + * @return void + */ + public function registerViews() + { + $viewPath = resource_path('views/modules/' . $this->moduleNameLower); + + $sourcePath = module_path($this->moduleName, 'Resources/views'); + + $this->publishes([ + $sourcePath => $viewPath + ], ['views', $this->moduleNameLower . '-module-views']); + + $this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower); + } + + /** + * Register translations. + * + * @return void + */ + public function registerTranslations() + { + $langPath = resource_path('lang/modules/' . $this->moduleNameLower); + + if (is_dir($langPath)) { + $this->loadTranslationsFrom($langPath, $this->moduleNameLower); + $this->loadJsonTranslationsFrom($langPath); + } else { + $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower); + $this->loadJsonTranslationsFrom(module_path($this->moduleName, 'Resources/lang')); + } + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return []; + } + + private function getPublishableViewPaths(): array + { + $paths = []; + foreach (\Config::get('view.paths') as $path) { + if (is_dir($path . '/modules/' . $this->moduleNameLower)) { + $paths[] = $path . '/modules/' . $this->moduleNameLower; + } + } + return $paths; + } +} diff --git a/Providers/RouteServiceProvider.php b/Providers/RouteServiceProvider.php new file mode 100644 index 0000000..0e0adf2 --- /dev/null +++ b/Providers/RouteServiceProvider.php @@ -0,0 +1,69 @@ +mapApiRoutes(); + + $this->mapWebRoutes(); + } + + /** + * Define the "web" routes for the application. + * + * These routes all receive session state, CSRF protection, etc. + * + * @return void + */ + protected function mapWebRoutes() + { + Route::middleware('web') + ->namespace($this->moduleNamespace) + ->group(module_path('Company', '/Routes/web.php')); + } + + /** + * Define the "api" routes for the application. + * + * These routes are typically stateless. + * + * @return void + */ + protected function mapApiRoutes() + { + Route::prefix('api') + ->middleware('api') + ->namespace($this->moduleNamespace) + ->group(module_path('Company', '/Routes/api.php')); + } +} diff --git a/Resources/lang/.gitkeep b/Resources/lang/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Resources/views/.gitkeep b/Resources/views/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Resources/views/_action.blade.php b/Resources/views/_action.blade.php new file mode 100644 index 0000000..928d92d --- /dev/null +++ b/Resources/views/_action.blade.php @@ -0,0 +1,17 @@ +@php + $route = explode('.', Route::currentRouteName()); +@endphp +
+ @can('company.update') + + {!! getIcon("pencil", "fs-1 text-info","duotune") !!} + + @endcan + + @can('company.delete') + {!! Form::open(['method' => 'DELETE','route' => [$route[0].'.destroy', $model->id],'class'=>'']) !!} + {{ Form::button(getIcon("trash", "fs-1 text-danger","duotune"), ['type' => 'submit', 'class' => 'delete btn btn-icon btn-bg-light btn-active-light-danger btn-sm'] ) }} + {!! Form::close() !!} + @endcan +
diff --git a/Resources/views/_form.blade.php b/Resources/views/_form.blade.php new file mode 100644 index 0000000..dc60d17 --- /dev/null +++ b/Resources/views/_form.blade.php @@ -0,0 +1,136 @@ +@php + $route = explode('.', Route::currentRouteName()); +@endphp + + + + +@ diff --git a/Resources/views/_table.blade.php b/Resources/views/_table.blade.php new file mode 100644 index 0000000..70c56e5 --- /dev/null +++ b/Resources/views/_table.blade.php @@ -0,0 +1,121 @@ + +{{ $dataTable->table() }} + + +{{-- Inject Scripts --}} +@section('scripts') + {{ $dataTable->scripts() }} +@endsection + +@push('customscript') + @php + $route = explode('.', Route::currentRouteName()); + @endphp + + +@endpush + +@section('styles') + +@endsection diff --git a/Resources/views/index.blade.php b/Resources/views/index.blade.php new file mode 100644 index 0000000..6b98861 --- /dev/null +++ b/Resources/views/index.blade.php @@ -0,0 +1,140 @@ +@php + $route = explode('.', Route::currentRouteName()); +@endphp + + + +
+ +
+
+
+ + + + + + + + + +
+ + +
+ + +
+ +
+ + + + + + + +
+ + +
+
+
+ @include('company::_table') + @include('company::_form') +
+ +
+ + @push('customscript') + + @endpush +
diff --git a/Routes/.gitkeep b/Routes/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Routes/api.php b/Routes/api.php new file mode 100644 index 0000000..4367b46 --- /dev/null +++ b/Routes/api.php @@ -0,0 +1,18 @@ +get('/company', function (Request $request) { + return $request->user(); +}); \ No newline at end of file diff --git a/Routes/web.php b/Routes/web.php new file mode 100644 index 0000000..eb51296 --- /dev/null +++ b/Routes/web.php @@ -0,0 +1,18 @@ +group(function () { + Route::resource('company', CompanyController::class); + }); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d9a61f2 --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "putrakuningan/company", + "type": "laravel-module", + "description": "", + "authors": [ + { + "name": "Daeng Deni Mardaeni", + "email": "ddeni05@gmail.com" + } + ], + "extra": { + "laravel": { + "providers": [], + "aliases": { + + } + } + }, + "autoload": { + "psr-4": { + "Modules\\Company\\": "" + } + } +} diff --git a/module.json b/module.json new file mode 100644 index 0000000..0646e59 --- /dev/null +++ b/module.json @@ -0,0 +1,12 @@ +{ + "name": "Company", + "alias": "company", + "database": "", + "description": "", + "keywords": [], + "priority": 0, + "providers": [ + "Modules\\Company\\Providers\\CompanyServiceProvider" + ], + "files": [] +}