342 lines
13 KiB
PHP
342 lines
13 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 Cost</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;'>Rental Cost</span>
|
|
<b>{$accomodation->monthly}</b> <i>/mo</i> | <b>{$accomodation->yearly}</b> <i>/yr</i>
|
|
</div>";
|
|
}
|
|
if ($onetime) {
|
|
$totalOneTime = ($onetime->visa ?? 0)
|
|
+ ($onetime->biometrics ?? 0)
|
|
+ ($onetime->sevis ?? 0)
|
|
+ ($onetime->application ?? 0);
|
|
|
|
$html .= "<div style='margin-bottom: 12px; font-size: 13px;'>
|
|
<span style='background:#e8f5e9; padding:2px 4px; border-radius:4px; font-weight:600;'>One-time Cost</span>
|
|
Sum Up: <b>{$totalOneTime}</b>
|
|
</div>";
|
|
}
|
|
if ($service) {
|
|
$totalService = ($service->flight_ticket ?? 0)
|
|
+ ($service->insurance ?? 0)
|
|
+ ($service->extra ?? 0);
|
|
|
|
$html .= "<div style='margin-bottom: 12px; font-size: 13px;'>
|
|
<span style='background:#f3e5f5; padding:2px 4px; border-radius:4px; font-weight:600;'>Service Cost</span>
|
|
Sum Up: <b>{$totalService}</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)
|
|
{
|
|
$cost = CostCalculator::with([
|
|
'stayTypeLiving',
|
|
'stayTypeAccomodation',
|
|
'stayTypeOnetime',
|
|
'stayTypeService'
|
|
])->findOrFail($id);
|
|
|
|
$data['title'] = 'View Cost Calculation';
|
|
$data['cost'] = $cost;
|
|
|
|
$getBreakdown = function ($stayTypeTitle) use ($cost) {
|
|
$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;
|
|
|
|
return [
|
|
'living' => [
|
|
'monthly' => $living->monthly ?? 0,
|
|
'yearly' => $living->yearly ?? 0,
|
|
],
|
|
'accomodation' => [
|
|
'monthly' => $accomodation->monthly ?? 0,
|
|
'yearly' => $accomodation->yearly ?? 0,
|
|
],
|
|
'onetime' => [
|
|
'visa' => $onetime->visa ?? 0,
|
|
'biometrics' => $onetime->biometrics ?? 0,
|
|
'sevis' => $onetime->sevis ?? 0,
|
|
'application' => $onetime->application ?? 0,
|
|
'total' => ($onetime->visa ?? 0) + ($onetime->biometrics ?? 0) + ($onetime->sevis ?? 0) + ($onetime->application ?? 0),
|
|
],
|
|
'service' => [
|
|
'flight_ticket' => $service->flight_ticket ?? 0,
|
|
'insurance' => $service->insurance ?? 0,
|
|
'extra' => $service->extra ?? 0,
|
|
'total' => ($service->flight_ticket ?? 0) + ($service->insurance ?? 0) + ($service->extra ?? 0),
|
|
]
|
|
];
|
|
};
|
|
|
|
$data['breakdowns'] = [
|
|
'alone' => $getBreakdown('Alone'),
|
|
'with_spouse' => $getBreakdown('With Spouse'),
|
|
'with_spouse_and_child' => $getBreakdown('With Spouse and Child'),
|
|
];
|
|
|
|
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->except(['living_cost', 'accomodation_cost', 'onetime_cost', 'service_cost']);
|
|
|
|
|
|
DB::transaction(function () use ($input, $request, $id) {
|
|
$costs = CostCalculator::findOrFail($id);
|
|
$costs->update($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 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);
|
|
}
|
|
}
|