feat: Add CostCalculator module with CRUD functionality and frontend integration
- Created new module for CostCalculator with necessary routes and controllers. - Implemented views for creating, editing, and displaying costs. - Added form partials for cost input with validation. - Integrated living status options in the form. - Developed frontend logic for cost calculation steps with dynamic UI updates. - Included necessary assets (JS and SCSS) for styling and functionality. - Updated constants for living status options. - Enhanced existing client-side cost calculator page with new features.
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\CostCalculator\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\CCMS\Models\Country;
|
||||
use Modules\CCMS\Models\Institution;
|
||||
use Modules\CCMS\Models\Test;
|
||||
use Modules\CourseFinder\Models\ProgramLevel;
|
||||
use Modules\CostCalculator\Models\CostCalculator;
|
||||
use Modules\CostCalculator\Services\CostCalculatorService;
|
||||
use Modules\CourseFinder\Models\Program;
|
||||
|
||||
class ProgramController extends Controller
|
||||
{
|
||||
protected $costCalculatorService;
|
||||
|
||||
public function __construct(CostCalculatorService $costCalculatorService)
|
||||
{
|
||||
$this->costCalculatorService = $costCalculatorService;
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$data['title'] = 'Cost Calculator List';
|
||||
$data['costs'] = $this->costCalculatorService->findAll($request);
|
||||
$data['countryOptions'] = Country::where('status', 1)->pluck('title', 'id');
|
||||
$data['programLevelOptions'] = ProgramLevel::where('status', 1)->pluck('title', 'id');
|
||||
$data['programOptions'] = Program::where('status', 1)->pluck('title', 'id');
|
||||
$data['livingStatusOptions'] = config('constants.living_status');
|
||||
|
||||
return view('costCalculator::cost.index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['title'] = 'Program Create';
|
||||
$data['editable'] = false;
|
||||
$data['intakeOptions'] = Program::INTAKE;
|
||||
$data['institutionOptions'] = Institution::where('status', 1)->pluck('title', 'id');
|
||||
$data['programLevelOptions'] = ProgramLevel::where('status', 1)->pluck('title', 'id');
|
||||
$data['testOptions'] = Test::where('status', 1)->where('parent_id', null)->pluck('title', 'id');
|
||||
|
||||
return view('coursefinder::program.create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
|
||||
$request->validate([
|
||||
'title' => 'required',
|
||||
]);
|
||||
|
||||
$input = $request->except(['prof_test_accepted']);
|
||||
|
||||
DB::transaction(function () use ($input, $request) {
|
||||
|
||||
$program = Program::create($input);
|
||||
|
||||
$attachData = [];
|
||||
|
||||
foreach ($request->prof_test_accepted as $item) {
|
||||
$attachData[$item['test_id']] = [
|
||||
'min_score' => $item['min_score'],
|
||||
'band_score' => $item['band_score'],
|
||||
];
|
||||
}
|
||||
|
||||
$program->tests()->sync($attachData);
|
||||
|
||||
flash()->success('Program has been created!');
|
||||
});
|
||||
|
||||
return redirect()->route('program.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$data['title'] = 'View Program';
|
||||
$data['program'] = Program::findOrFail($id);
|
||||
$data['intakeOptions'] = Program::INTAKE;
|
||||
return view('coursefinder::program.show', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$data['title'] = 'Edit Program';
|
||||
$data['editable'] = true;
|
||||
$data['program'] = Program::findOrFail($id);
|
||||
$data['intakeOptions'] = Program::INTAKE;
|
||||
$data['institutionOptions'] = Institution::where('status', 1)->pluck('title', 'id');
|
||||
$data['programLevelOptions'] = ProgramLevel::where('status', 1)->pluck('title', 'id');
|
||||
$data['testOptions'] = Test::where('status', 1)->where('parent_id', null)->pluck('title', 'id');
|
||||
$data['coopOptions'] = Coop::where('status', 1)->pluck('title', 'id');
|
||||
$data['requiredDocumentOptions'] = RequiredDocument::where('status', 1)->pluck('title', 'id');
|
||||
return view('coursefinder::program.edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$input = $request->except(['prof_test_accepted']);
|
||||
|
||||
DB::transaction(function () use ($input, $request, $id) {
|
||||
$program = Program::findOrFail($id);
|
||||
$program->update($input);
|
||||
|
||||
$attachData = [];
|
||||
|
||||
foreach ($request->prof_test_accepted as $item) {
|
||||
$attachData[$item['test_id']] = [
|
||||
'min_score' => $item['min_score'],
|
||||
'band_score' => $item['band_score'],
|
||||
];
|
||||
}
|
||||
|
||||
$program->tests()->sync($attachData);
|
||||
});
|
||||
|
||||
flash()->success('program has been updated!');
|
||||
|
||||
return redirect()->route('program.index')->withSuccess('Program has been updated!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
$program = Program::findOrFail($id);
|
||||
$program->delete();
|
||||
|
||||
flash()->success('Program has been deleted!');
|
||||
} catch (\Throwable $th) {
|
||||
flash()->error($th->getMessage());
|
||||
}
|
||||
return response()->json(['status' => 200, 'message' => 'Program has been deleted!'], 200);
|
||||
}
|
||||
|
||||
public function getProgramByInstitution(Request $request)
|
||||
{
|
||||
try {
|
||||
$program = Program::where(['institution_id' => $request->institution_id])
|
||||
->select('id', 'title')
|
||||
->get();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'data' => $program,
|
||||
'msg' => 'Fetch',
|
||||
], 200);
|
||||
} catch (\Throwable $th) {
|
||||
return response()->json([
|
||||
'status' => false,
|
||||
'msg' => $th->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function import(Request $request)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
Excel::import(new ProgramImport(), $request->file('file')->store('temp'));
|
||||
DB::commit();
|
||||
return redirect()->back()->with('success', "Upload Succesfully");
|
||||
} catch (\Throwable $th) {
|
||||
DB::rollback();
|
||||
return redirect()->back()->with('error', $th->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function getCoursesList(Request $request)
|
||||
{
|
||||
$data['intakes'] = Program::INTAKE;
|
||||
|
||||
$query = Program::query();
|
||||
|
||||
if ($request->filled('countries_id')) {
|
||||
$query->whereRelation('institution', 'countries_id', $request->countries_id);
|
||||
}
|
||||
|
||||
if ($request->filled('institution_id')) {
|
||||
$query->where('institutions_id', $request->institution_id);
|
||||
}
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$query->where('title', 'like', "%{$request->search}%");
|
||||
}
|
||||
|
||||
if ($request->filled('programlevels_id')) {
|
||||
$query->where('programlevels_id', $request->programlevels_id);
|
||||
}
|
||||
|
||||
if ($request->filled('intake_id')) {
|
||||
$query->whereJsonContains('intakes', $request->intake_id);
|
||||
}
|
||||
|
||||
if ($request->filled('preffered_location')) {
|
||||
$query->where('location', 'like', "%{$request->preffered_location}%");
|
||||
}
|
||||
|
||||
if ($request->filled('service_id')) {
|
||||
$query->whereRelation('services', 'service_id', '=', $request->service_id);
|
||||
|
||||
if ($request->filled('min_score')) {
|
||||
$query->whereRelation('services', 'min_score', '<=', $request->min_score);
|
||||
}
|
||||
|
||||
if ($request->filled('max_score')) {
|
||||
$query->whereRelation('services', 'band_score', '<=', $request->max_score);
|
||||
}
|
||||
}
|
||||
|
||||
$data['courses'] = $query
|
||||
->orderBy('title', 'asc')
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
|
||||
$queryCount = $data['courses']->total();
|
||||
|
||||
if ($request->ajax()) {
|
||||
$view = view('client.raffles.pages.course.list', $data)->render();
|
||||
return response()->json(['html' => $view, 'queryCount' => $queryCount]);
|
||||
}
|
||||
}
|
||||
}
|
44
Modules/CostCalculator/app/Models/CostCalculator.php
Normal file
44
Modules/CostCalculator/app/Models/CostCalculator.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\CostCalculator\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Modules\CCMS\Models\Country;
|
||||
use Modules\CourseFinder\Models\Program;
|
||||
use Modules\CourseFinder\Models\ProgramLevel;
|
||||
|
||||
// use Modules\CostCalculator\Database\Factories\CostCalculatorFactory;
|
||||
|
||||
class CostCalculator extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
protected $casts = [
|
||||
'living_cost' => 'object',
|
||||
'accomodation_cost' => 'object',
|
||||
'onetime_cost' => 'object',
|
||||
'other_services' => 'object',
|
||||
];
|
||||
|
||||
public function institution()
|
||||
{
|
||||
return $this->belongsTo(Country::class, 'country_id');
|
||||
}
|
||||
|
||||
public function programLevel()
|
||||
{
|
||||
return $this->belongsTo(ProgramLevel::class, 'programlevel_id');
|
||||
}
|
||||
|
||||
public function program()
|
||||
{
|
||||
return $this->belongsTo(Program::class, 'program_id');
|
||||
}
|
||||
}
|
0
Modules/CostCalculator/app/Models/Scopes/.gitkeep
Normal file
0
Modules/CostCalculator/app/Models/Scopes/.gitkeep
Normal file
0
Modules/CostCalculator/app/Providers/.gitkeep
Normal file
0
Modules/CostCalculator/app/Providers/.gitkeep
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\CostCalculator\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Nwidart\Modules\Traits\PathNamespace;
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
|
||||
class CostCalculatorServiceProvider extends ServiceProvider
|
||||
{
|
||||
use PathNamespace;
|
||||
|
||||
protected string $name = 'CostCalculator';
|
||||
|
||||
protected string $nameLower = 'costcalculator';
|
||||
|
||||
/**
|
||||
* Boot the application events.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
$this->registerCommandSchedules();
|
||||
$this->registerTranslations();
|
||||
$this->registerConfig();
|
||||
$this->registerViews();
|
||||
$this->loadMigrationsFrom(module_path($this->name, 'database/migrations'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->register(EventServiceProvider::class);
|
||||
$this->app->register(RouteServiceProvider::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register commands in the format of Command::class
|
||||
*/
|
||||
protected function registerCommands(): void
|
||||
{
|
||||
// $this->commands([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register command Schedules.
|
||||
*/
|
||||
protected function registerCommandSchedules(): void
|
||||
{
|
||||
// $this->app->booted(function () {
|
||||
// $schedule = $this->app->make(Schedule::class);
|
||||
// $schedule->command('inspire')->hourly();
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* Register translations.
|
||||
*/
|
||||
public function registerTranslations(): void
|
||||
{
|
||||
$langPath = resource_path('lang/modules/'.$this->nameLower);
|
||||
|
||||
if (is_dir($langPath)) {
|
||||
$this->loadTranslationsFrom($langPath, $this->nameLower);
|
||||
$this->loadJsonTranslationsFrom($langPath);
|
||||
} else {
|
||||
$this->loadTranslationsFrom(module_path($this->name, 'lang'), $this->nameLower);
|
||||
$this->loadJsonTranslationsFrom(module_path($this->name, 'lang'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register config.
|
||||
*/
|
||||
protected function registerConfig(): void
|
||||
{
|
||||
$relativeConfigPath = config('modules.paths.generator.config.path');
|
||||
$configPath = module_path($this->name, $relativeConfigPath);
|
||||
|
||||
if (is_dir($configPath)) {
|
||||
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if ($file->isFile() && $file->getExtension() === 'php') {
|
||||
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
|
||||
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
|
||||
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
|
||||
|
||||
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
|
||||
$this->mergeConfigFrom($file->getPathname(), $key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register views.
|
||||
*/
|
||||
public function registerViews(): void
|
||||
{
|
||||
$viewPath = resource_path('views/modules/'.$this->nameLower);
|
||||
$sourcePath = module_path($this->name, 'resources/views');
|
||||
|
||||
$this->publishes([$sourcePath => $viewPath], ['views', $this->nameLower.'-module-views']);
|
||||
|
||||
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->nameLower);
|
||||
|
||||
$componentNamespace = $this->module_namespace($this->name, $this->app_path(config('modules.paths.generator.component-class.path')));
|
||||
Blade::componentNamespace($componentNamespace, $this->nameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*/
|
||||
public function provides(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private function getPublishableViewPaths(): array
|
||||
{
|
||||
$paths = [];
|
||||
foreach (config('view.paths') as $path) {
|
||||
if (is_dir($path.'/modules/'.$this->nameLower)) {
|
||||
$paths[] = $path.'/modules/'.$this->nameLower;
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\CostCalculator\Providers;
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The event handler mappings for the application.
|
||||
*
|
||||
* @var array<string, array<int, string>>
|
||||
*/
|
||||
protected $listen = [];
|
||||
|
||||
/**
|
||||
* Indicates if events should be discovered.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected static $shouldDiscoverEvents = true;
|
||||
|
||||
/**
|
||||
* Configure the proper event listeners for email verification.
|
||||
*/
|
||||
protected function configureEmailVerification(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\CostCalculator\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $name = 'CostCalculator';
|
||||
|
||||
/**
|
||||
* Called before routes are registered.
|
||||
*
|
||||
* Register any model bindings or pattern based filters.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the routes for the application.
|
||||
*/
|
||||
public function map(): void
|
||||
{
|
||||
$this->mapApiRoutes();
|
||||
$this->mapWebRoutes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "web" routes for the application.
|
||||
*
|
||||
* These routes all receive session state, CSRF protection, etc.
|
||||
*/
|
||||
protected function mapWebRoutes(): void
|
||||
{
|
||||
Route::middleware('web')->group(module_path($this->name, '/routes/web.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "api" routes for the application.
|
||||
*
|
||||
* These routes are typically stateless.
|
||||
*/
|
||||
protected function mapApiRoutes(): void
|
||||
{
|
||||
Route::middleware('api')->prefix('api')->name('api.')->group(module_path($this->name, '/routes/api.php'));
|
||||
}
|
||||
}
|
0
Modules/CostCalculator/app/Services/.gitkeep
Normal file
0
Modules/CostCalculator/app/Services/.gitkeep
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\CostCalculator\Services;
|
||||
|
||||
use Modules\CostCalculator\Models\CostCalculator;
|
||||
|
||||
class CostCalculatorService
|
||||
{
|
||||
public function findAll($request)
|
||||
{
|
||||
return CostCalculator::when($request, function ($query) use ($request) {
|
||||
if ($request->filled('country_id')) {
|
||||
$query->whereRelation('institution', 'country_id', $request->country_id);
|
||||
}
|
||||
|
||||
if ($request->filled('institution_id')) {
|
||||
$query->where("institution_id", $request->institution_id);
|
||||
}
|
||||
|
||||
if ($request->filled('programlevel_id')) {
|
||||
$query->where("programlevel_id", $request->programlevel_id);
|
||||
}
|
||||
|
||||
if ($request->filled('intake_id')) {
|
||||
$intakeId = $request->intake_id;
|
||||
$query->whereJsonContains('intakes', $intakeId);
|
||||
}
|
||||
|
||||
if ($request->filled('status')) {
|
||||
$query->where('status', $request->status);
|
||||
}
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = $request->search;
|
||||
$query->where('title', 'like', "%{$search}%");
|
||||
}
|
||||
|
||||
if ($request->filled('location')) {
|
||||
$location = $request->location;
|
||||
$query->where('location', 'like', "%{$location}%");
|
||||
}
|
||||
})->latest()->paginate(10)->withQueryString();
|
||||
}
|
||||
|
||||
public function pluck(callable $query = null)
|
||||
{
|
||||
$baseQuery = CostCalculator::query();
|
||||
if (is_callable($query)) {
|
||||
$query($baseQuery);
|
||||
}
|
||||
return $baseQuery->pluck('title', 'id');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user