247 lines
9.2 KiB
PHP
247 lines
9.2 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 formatCostForStayType(CostCalculator $cost, string $stayTypeTitle)
|
|
{
|
|
$living = optional($cost->stayTypeLiving->firstWhere('title', $stayTypeTitle))->pivot;
|
|
$accomodation = optional($cost->stayTypeAccomodation->firstWhere('title', $stayTypeTitle))->pivot;
|
|
$onetime = optional($cost->stayTypeOnetime->firstWhere('title', $stayTypeTitle))->pivot;
|
|
$service = optional($cost->stayTypeService->firstWhere('title', $stayTypeTitle))->pivot;
|
|
|
|
$html = "<div style='font-size: 12px; line-height: 1.4;'>";
|
|
|
|
if ($living) {
|
|
$html .= "<div style='margin-bottom: 6px;'>
|
|
<span style='background:#e3f2fd; padding:2px 4px; border-radius:4px; font-weight:600;'>Living</span>
|
|
<b>{$living->monthly}</b> <i>/mo</i> | <b>{$living->yearly}</b> <i>/yr</i>
|
|
</div>";
|
|
}
|
|
if ($accomodation) {
|
|
$html .= "<div style='margin-bottom: 6px;'>
|
|
<span style='background:#fff3e0; padding:2px 4px; border-radius:4px; font-weight:600;'>Accommodation</span>
|
|
<b>{$accomodation->monthly}</b> <i>/mo</i> | <b>{$accomodation->yearly}</b> <i>/yr</i>
|
|
</div>";
|
|
}
|
|
if ($onetime) {
|
|
$html .= "<div style='margin-bottom: 6px;'>
|
|
<span style='background:#e8f5e9; padding:2px 4px; border-radius:4px; font-weight:600;'>One-time</span>
|
|
Visa: <b>{$onetime->visa}</b>, Bio: <b>{$onetime->biometrics}</b>,
|
|
Sevis: <b>{$onetime->sevis}</b>, App: <b>{$onetime->application}</b>
|
|
</div>";
|
|
}
|
|
if ($service) {
|
|
$html .= "<div style='margin-bottom: 6px;'>
|
|
<span style='background:#f3e5f5; padding:2px 4px; border-radius:4px; font-weight:600;'>Service</span>
|
|
Ticket: <b>{$service->flight_ticket}</b>, Ins: <b>{$service->insurance}</b>,
|
|
Extra: <b>{$service->extra}</b>
|
|
</div>";
|
|
}
|
|
|
|
$html .= "</div>";
|
|
|
|
return trim($html) ?: '-';
|
|
}
|
|
|
|
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$model = CostCalculator::with([
|
|
'country',
|
|
'stayTypeLiving',
|
|
'stayTypeAccomodation',
|
|
'stayTypeOnetime',
|
|
'stayTypeService'
|
|
]);
|
|
|
|
return DataTables::eloquent($model)
|
|
->addIndexColumn()
|
|
->setRowClass('tableRow')
|
|
->editColumn('country', function (CostCalculator $cost) {
|
|
return "<div class='d-flex align-items-start'>
|
|
<div class='flex-grow-1'>
|
|
<p class='mb-0 fs-14'>{$cost->country?->title}</p>
|
|
</div>
|
|
</div>";
|
|
})
|
|
->editColumn('alone', function (CostCalculator $cost) {
|
|
return $this->formatCostForStayType($cost, 'Alone');
|
|
})
|
|
->editColumn('with_spouse', function (CostCalculator $cost) {
|
|
return $this->formatCostForStayType($cost, 'With Spouse');
|
|
})
|
|
->editColumn('with_spouse_and_child', function (CostCalculator $cost) {
|
|
return $this->formatCostForStayType($cost, 'With Spouse and Child');
|
|
})
|
|
->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(['country', 'alone', 'with_spouse', 'with_spouse_and_child', 'status', 'action'])
|
|
->toJson();
|
|
}
|
|
return view('costcalculator::cost.index', [
|
|
'title' => 'Estimated Cost Calculation',
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['title'] = 'Create Estimated Cost Calculation';
|
|
$data['editable'] = false;
|
|
$data['countryOptions'] = Country::where('status', 1)->where('parent_id', null)->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',
|
|
]);
|
|
|
|
$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) {
|
|
$attachOnetimeData[$item['stay_type_id']] = [
|
|
'visa' => $item['visa'],
|
|
'biometrics' => $item['biometrics'],
|
|
'sevis' => $item['sevis'],
|
|
'application' => $item['application'],
|
|
];
|
|
}
|
|
|
|
foreach ($request->service_cost as $item) {
|
|
$attachServiceData[$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('cost.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)->where('parent_id', null)->pluck('title', 'id');
|
|
$data['livingStatusOptions'] = StayType::where('status', 1)->pluck('title', 'id');
|
|
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('cost.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);
|
|
}
|
|
}
|