200 lines
7.1 KiB
PHP
200 lines
7.1 KiB
PHP
<?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;
|
|
use Modules\CostCalculator\Models\StayType;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class CostCalculatorController extends Controller
|
|
{
|
|
protected $costCalculatorService;
|
|
|
|
public function __construct(CostCalculatorService $costCalculatorService)
|
|
{
|
|
$this->costCalculatorService = $costCalculatorService;
|
|
}
|
|
|
|
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);
|
|
|
|
if (request()->ajax()) {
|
|
$model = CostCalculator::query()->orderBy('order');
|
|
return DataTables::eloquent($model)
|
|
->addIndexColumn()
|
|
->setRowClass('tableRow')
|
|
->editColumn('image', function (CostCalculator $cost) {
|
|
return $cost->getRawOriginal('image') ? "<img src='{$cost->country->image}' alt='{$cost->country->image}' class='rounded avatar-sm material-shadow ms-2 img-thumbnail'>" : '-';
|
|
})
|
|
->editColumn('status', function (CostCalculator $cost) {
|
|
$status = $cost->status ? 'Published' : 'Draft';
|
|
$color = $cost->status ? 'text-success' : 'text-danger';
|
|
return "<p class='{$color}'>{$status}</p>";
|
|
})
|
|
->addColumn('action', 'costcalculator::cost.datatable.action')
|
|
->rawColumns(['image', 'status', 'action'])
|
|
->toJson();
|
|
}
|
|
|
|
return view('costcalculator::cost.index', [
|
|
'title' => 'Cost Calculator List',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['title'] = 'Create Cost Calculator';
|
|
$data['editable'] = false;
|
|
$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'] = StayType::where('status', 1)->pluck('title', 'id');
|
|
|
|
return view('costcalculator::cost.create', $data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'country_id' => 'required',
|
|
'programlevel_id' => 'required',
|
|
]);
|
|
|
|
$input = $request->except(['living_cost', 'accomodation_cost', 'onetime_cost', 'service_cost']);
|
|
|
|
DB::transaction(function () use ($input, $request) {
|
|
|
|
$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!');
|
|
});
|
|
|
|
return redirect()->route('costCalculator.index');
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$data['title'] = 'View Cost Calculation';
|
|
$data['program'] = CostCalculator::findOrFail($id);
|
|
$data['intakeOptions'] = Program::INTAKE;
|
|
return view('costcalculator::cost.show', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data['title'] = 'Edit Cost Calculator';
|
|
$data['editable'] = true;
|
|
$data['cost'] = CostCalculator::findOrFail($id);
|
|
$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.edit', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$input = $request->all();
|
|
|
|
DB::transaction(function () use ($input, $request, $id) {
|
|
$program = CostCalculator::findOrFail($id);
|
|
$program->update($input);
|
|
|
|
flash()->success('Cost Calculation has been updated!');
|
|
});
|
|
|
|
return redirect()->route('costCalculator.index')->withSuccess('Program has been updated!');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$program = CostCalculator::findOrFail($id);
|
|
$program->delete();
|
|
|
|
flash()->success('Cost Calculator has been deleted!');
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
}
|
|
return response()->json(['status' => 200, 'message' => 'Cost Calculator has been deleted!'], 200);
|
|
}
|
|
|
|
}
|