change template, receive units and lock sheet 'list' so it can't be changed

This commit is contained in:
ygbanzato 2023-07-27 10:36:00 +01:00
parent fa669b7419
commit fb4612fd15
4 changed files with 66 additions and 14 deletions

View File

@ -6,6 +6,9 @@
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Symfony\Component\HttpFoundation\StreamedResponse;
use App\Models\User;
use App\Models\CompanyProject;
@ -30,10 +33,58 @@
class CreateProjectController extends Controller
{
// public function receiveUnitsForExcelTemplate($numberProject)
// {
// $receveCompanyProject = CompanyProject::where('company_projects_id',$numberProject)->first();
// $recevePlant = Plant::where('plant_id', $receveCompanyProject->plant_id)->first();
// $receveUnits = Unit::where('plant_id',$recevePlant->plant_id)->get();
// // dd($receveUnits);
// $filePath = public_path('templateExcel/TestTemplate.xlsx');
// $fileName = 'Valves_Template.xlsx';
// return response()->download($filePath, $fileName);
// }
public function receiveUnitsForExcelTemplate($numberProject)
{
$receveCompanyProject = CompanyProject::where('company_projects_id', $numberProject)->first();
$recevePlant = Plant::where('plant_id', $receveCompanyProject->plant_id)->first();
$receveUnits = Unit::where('plant_id', $recevePlant->plant_id)->get();
$filePath = public_path('templateExcel/TestTemplate.xlsx');
// Load the spreadsheet
$spreadsheet = IOFactory::load($filePath);
// Get the second sheet
$sheet = $spreadsheet->getSheet(1); // Sheet index starts from 0
$row = 1; // Row number where you want to start inserting data
foreach ($receveUnits as $unit) {
// Set value for column D
$sheet->setCellValue('D' . $row, $unit->unit_name);
$row++;
}
// Generate and return the download response
return $this->createDownloadResponse($spreadsheet, 'Valves_Template.xlsx');
}
protected function createDownloadResponse($spreadsheet, $filename)
{
// Create a writer object
$writer = new Xlsx($spreadsheet);
// Create a StreamedResponse with a callback
$response = new StreamedResponse(
function () use ($writer) {
$writer->save('php://output');
}
);
// Set headers to indicate we're sending an Excel file
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->headers->set('Content-Disposition', 'attachment;filename="' . $filename . '"');
$response->headers->set('Cache-Control', 'max-age=0');
return $response;
}
public function finishCreatingProject($numberProject)
{
$project = CompanyProject::find($numberProject);
$project->order_project = 2;
$project->save();
@ -86,11 +137,6 @@ public function deleteWorkstation($name)
return back()->with('danger', 'Posto de Trabalho não encontrado!');
}
public function AddNomenclatureWorkstation(Request $request)
{
foreach ($request->nameWorkstations as $key => $value) {

Binary file not shown.

View File

@ -1165,8 +1165,8 @@ class="fas fa-plus"></i>
<input type="hidden" name="numberProject" value="{{ $numberProject }}">
<div class="row">
<div class="col-sm-4 d-flex justify-content-center">
<a id="btn-download-template" href="{{ route('download') }}" class="btn btn-info">Baixar
Template</a>
<a href="{{ route('download',['numberProject'=>$numberProject]) }}" class="btn btn-info">Baixar Template</a>
{{-- <a id="btn-download-template" href="{{ route('download1',['numberProject'=>$numberProject]) }}" class="btn btn-info">Baixar Template</a> --}}
</div>
<div class="form-group col-sm-4">
<div class="input-group">

View File

@ -198,12 +198,18 @@
|
*/
Route::get('/template', function () {
$filePath = public_path('templateExcel/FinalTemplate1.xlsx');
$fileName = 'Valves_Template.xlsx';
// Route::get('/template', function () {
// $filePath = public_path('templateExcel/FinalTemplate1.xlsx');
// $fileName = 'Valves_Template.xlsx';
// return response()->download($filePath, $fileName);
// })->name('download');
// Route::get('/Template/{numberProject}')->name('download');
Route::get('/template1/{numberProject}',[CreateProjectController::class,'receiveUnitsForExcelTemplate'])->name('download');
return response()->download($filePath, $fileName);
})->name('download');
/*