diff --git a/Modules/CostCalculator/app/Http/Controllers/CostCalculatorController.php b/Modules/CostCalculator/app/Http/Controllers/CostCalculatorController.php index 6a3b170..5518d6d 100644 --- a/Modules/CostCalculator/app/Http/Controllers/CostCalculatorController.php +++ b/Modules/CostCalculator/app/Http/Controllers/CostCalculatorController.php @@ -13,6 +13,7 @@ use Modules\CourseFinder\Models\ProgramLevel; use Modules\CostCalculator\Models\CostCalculator; use Modules\CostCalculator\Services\CostCalculatorService; use Modules\CourseFinder\Models\Program; +use Modules\CostCalculator\Models\StayType; use Yajra\DataTables\Facades\DataTables; class CostCalculatorController extends Controller @@ -65,10 +66,10 @@ class CostCalculatorController extends Controller { $data['title'] = 'Create Cost Calculator'; $data['editable'] = false; - $data['countryOptions'] = Country::where('status', 1)->pluck('title', 'id'); + $data['countryOptions'] = Country::where('status', 1)->where('parent_id', null)->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'); + $data['livingStatusOptions'] = StayType::where('status', 1)->pluck('title', 'id'); return view('costcalculator::cost.create', $data); } @@ -78,18 +79,57 @@ class CostCalculatorController extends Controller */ public function store(Request $request) { - $request->validate([ 'country_id' => 'required', 'programlevel_id' => 'required', - 'living_status_id' => 'required', ]); - $input = $request->all(); + $input = $request->except(['living_cost', 'accomodation_cost', 'onetime_cost', 'service_cost']); DB::transaction(function () use ($input, $request) { - CostCalculator::create($input); + $costs = CostCalculator::create($input); + + $attachLivingData = []; + $attachAccData = []; + $attachOnetimeData = []; + $attachServiceData = []; + + foreach ($request->living_cost as $item) { + $attachLivingData[$item['stay_type_id']] = [ + 'monthly' => $item['monthly'], + 'yearly' => $item['yearly'], + ]; + } + + foreach ($request->accomodation_cost as $item) { + $attachAccData[$item['stay_type_id']] = [ + 'monthly' => $item['monthly'], + 'yearly' => $item['yearly'], + ]; + } + + foreach ($request->onetime_cost as $item) { + $attachLivingData[$item['stay_type_id']] = [ + 'visa' => $item['visa'], + 'biometrics' => $item['biometrics'], + 'sevis' => $item['sevis'], + 'application' => $item['application'], + ]; + } + + foreach ($request->service_cost as $item) { + $attachLivingData[$item['stay_type_id']] = [ + 'flight_ticket' => $item['flight_ticket'], + 'insurance' => $item['insurance'], + 'extra' => $item['extra'], + ]; + } + + $costs->stayTypeLiving()->sync($attachLivingData); + $costs->stayTypeAccomodation()->sync($attachAccData); + $costs->stayTypeOnetime()->sync($attachOnetimeData); + $costs->stayTypeService()->sync($attachServiceData); flash()->success('Cost Calculation has been created!'); }); diff --git a/Modules/CostCalculator/app/Models/CostCalculator.php b/Modules/CostCalculator/app/Models/CostCalculator.php index 3465741..f376af2 100644 --- a/Modules/CostCalculator/app/Models/CostCalculator.php +++ b/Modules/CostCalculator/app/Models/CostCalculator.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Modules\CCMS\Models\Country; use Modules\CourseFinder\Models\Program; use Modules\CourseFinder\Models\ProgramLevel; +use Modules\CostCalculator\Models\StayType; // use Modules\CostCalculator\Database\Factories\CostCalculatorFactory; @@ -15,19 +16,16 @@ 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', + 'service_cost' => 'object' ]; - public function institution() + public function country() { return $this->belongsTo(Country::class, 'country_id'); } @@ -41,4 +39,24 @@ class CostCalculator extends Model { return $this->belongsTo(Program::class, 'program_id'); } + + public function stayTypeLiving(): BelongsToMany + { + return $this->belongsToMany(StayType::class, 'living_costs', 'cost_calculator_id', 'stay_type_id')->withPivot('id', 'monthly', 'yearly')->withTimestamps(); + } + + public function stayTypeAccomodation(): BelongsToMany + { + return $this->belongsToMany(StayType::class, 'accomodation_costs', 'cost_calculator_id', 'stay_type_id')->withPivot('id', 'monthly', 'yearly')->withTimestamps(); + } + + public function stayTypeOnetime(): BelongsToMany + { + return $this->belongsToMany(StayType::class, 'onetime_costs', 'cost_calculator_id', 'stay_type_id')->withPivot('id', 'visa', 'biometrics', 'sevis', 'application')->withTimestamps(); + } + + public function stayTypeService(): BelongsToMany + { + return $this->belongsToMany(StayType::class, 'service_costs', 'cost_calculator_id', 'stay_type_id')->withPivot('id', 'flight_ticket', 'insurance', 'extra')->withTimestamps(); + } } diff --git a/Modules/CostCalculator/database/migrations/2025_08_13_102211_create_cost_calculators_table.php b/Modules/CostCalculator/database/migrations/2025_08_13_102211_create_cost_calculators_table.php index 046d2a4..f36e669 100644 --- a/Modules/CostCalculator/database/migrations/2025_08_13_102211_create_cost_calculators_table.php +++ b/Modules/CostCalculator/database/migrations/2025_08_13_102211_create_cost_calculators_table.php @@ -16,16 +16,51 @@ return new class extends Migration $table->unsignedInteger('country_id')->nullable(); $table->unsignedInteger('programlevel_id')->nullable(); $table->unsignedInteger('program_id')->nullable(); - $table->unsignedInteger('living_status_id')->nullable(); - $table->json('living_cost')->nullable(); - $table->json('accomodation_cost')->nullable(); - $table->json('onetime_cost')->nullable(); - $table->json('other_services')->nullable(); $table->unsignedInteger('createdby')->nullable(); $table->unsignedInteger('updatedby')->nullable(); $table->boolean('status')->default(1); $table->timestamps(); }); + + Schema::create('living_costs', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('cost_calculator_id')->nullable(); + $table->unsignedInteger('stay_type_id')->nullable(); + $table->string('monthly')->nullable(); + $table->string('yearly')->nullable(); + $table->timestamps(); + }); + + Schema::create('accomodation_costs', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('cost_calculator_id')->nullable(); + $table->unsignedInteger('stay_type_id')->nullable(); + $table->string('monthly')->nullable(); + $table->string('yearly')->nullable(); + $table->timestamps(); + }); + + Schema::create('onetime_costs', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('cost_calculator_id')->nullable(); + $table->unsignedInteger('stay_type_id')->nullable(); + $table->string('visa')->nullable(); + $table->string('biometrics')->nullable(); + $table->string('sevis')->nullable(); + $table->string('application')->nullable(); + $table->timestamps(); + }); + + Schema::create('service_costs', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('cost_calculator_id')->nullable(); + $table->unsignedInteger('stay_type_id')->nullable(); + $table->string('flight_ticket')->nullable(); + $table->string('insurance')->nullable(); + $table->string('extra')->nullable(); + $table->timestamps(); + }); + } /** @@ -34,5 +69,9 @@ return new class extends Migration public function down(): void { Schema::dropIfExists('cost_calculators'); + Schema::dropIfExists('living_costs'); + Schema::dropIfExists('accomodation_costs'); + Schema::dropIfExists('onetime_costs'); + Schema::dropIfExists('service_costs'); } }; diff --git a/Modules/CostCalculator/resources/views/cost/partials/accomodation-cost.blade.php b/Modules/CostCalculator/resources/views/cost/partials/accomodation-cost.blade.php index 0176ea5..8dd31e1 100644 --- a/Modules/CostCalculator/resources/views/cost/partials/accomodation-cost.blade.php +++ b/Modules/CostCalculator/resources/views/cost/partials/accomodation-cost.blade.php @@ -1,6 +1,6 @@