79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Reports;
|
|
|
|
use App\Enums\GcomCrfData;
|
|
use App\Exports\GcomCrfExport;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ActivityGpcOutput;
|
|
use App\Services\Reports\GcomCrfService;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Http\Request;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class GcomCrfController extends Controller
|
|
{
|
|
protected $service;
|
|
|
|
public function __construct(GcomCrfService $service)
|
|
{
|
|
$this->service = $service;
|
|
}
|
|
|
|
public function show($inventoryYear = null, $activityYear = null, $sectorCode = null)
|
|
{
|
|
$gpcOutput = ActivityGpcOutput::where('inventory_year', $inventoryYear)
|
|
->where('activity_year', $activityYear)->rowActive()
|
|
->first();
|
|
|
|
$gpc = [];
|
|
if ($gpcOutput) {
|
|
$gpc = $gpcOutput->getAllColumnsWithReference();
|
|
}
|
|
|
|
$gpcData = GcomCrfData::getAllData();
|
|
|
|
return view('reports.gcom-crf.index', [
|
|
'inventoryYear' => $inventoryYear ?? date('Y'),
|
|
'activityYear' => $activityYear ?? date('Y') - 1,
|
|
|
|
'gpcData' => $gpcData,
|
|
|
|
'gpcOutput' => $gpcOutput,
|
|
'gpc' => $gpc,
|
|
]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
// Validate and sanitize inputs
|
|
$validator = Validator::make($request->all(), [
|
|
'inventoryYear' => 'required|integer',
|
|
'activityYear' => 'required|integer',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withErrors($validator)->withInput();
|
|
}
|
|
|
|
$inventoryYear = $request->input('inventoryYear');
|
|
$activityYear = $request->input('activityYear');
|
|
|
|
// Fetch the necessary data
|
|
$gpcOutput = ActivityGpcOutput::where('inventory_year', $inventoryYear)
|
|
->where('activity_year', $activityYear)->rowActive()
|
|
->first();
|
|
|
|
$gpc = [];
|
|
if ($gpcOutput) {
|
|
$gpc = $gpcOutput->getAllColumnsWithReference();
|
|
}
|
|
|
|
$gpcData = GcomCrfData::getAllData();
|
|
|
|
$titleExcel = 'GCOM-CRF_' . $inventoryYear . '_' . $activityYear;
|
|
|
|
return Excel::download(new GcomCrfExport($inventoryYear, $activityYear, $gpcData, $gpc), $titleExcel . '.xlsx');
|
|
}
|
|
}
|