public function generateDocx(): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
request()->validate([
'startDate' => 'required|date',
'endDate' => 'required|date',
'users' => ['nullable', 'array'],
'selectAll' => ['nullable', 'boolean'],
'showUser' => ['nullable', 'boolean'],
'showDate' => ['nullable', 'boolean'],
]);
$start = Carbon::parse(request('startDate'))->setTime(0, 0);
$end = Carbon::parse(request('endDate'))->setTimezone('UTC')->setTime(23, 59, 59);
if (request('selectAll')) {
$userReports = Report::with(['contact', 'user', 'highlights', 'lowlights', 'project_updates',
'new_projects', 'claims', 'portofolios', 'order_status', 'payment_status',
'tech_support_requests', 'competition_updates' => function ($query) {
$query->with('contact');
}, 'contracts' => function ($query) {
$query->with('contractComponents', 'currency')->withSum('contractComponents', 'total');
}])
->whereBetween('rdv_date', [$start, $end])
->where('status', '=', 'send')
->where('laboratory', request('type_report'))
->get()
->groupBy('contact_id')
->sortBy('contact.name');
} else {
$users = User::whereIn('hash_id', request('users'))->get();
$userReports = Report::with(['contact', 'user', 'highlights', 'lowlights', 'project_updates',
'new_projects', 'claims', 'portofolios' => function ($query) {
$query->with('components');
}, 'order_status', 'payment_status',
'tech_support_requests', 'competition_updates' => function ($query) {
$query->with('contact');
}, 'contracts' => function ($query) {
$query->with('contractComponents', 'currency')->withSum('contractComponents', 'total');
}])
->whereIn('user_id', $users->pluck('id')->toArray())
->whereBetween('rdv_date', [$start, $end])
->where('status', '=', 'send')
->where('laboratory', request('type_report'))
->get()
->groupBy('contact_id')
->sortBy('contact.name');
}
$userReports = $userReports->map(function ($reports) {
$group = $reports->groupBy(function ($item, $key) {
return $item['rdv_date']->format('d/m/Y');
});
return $group->sortKeys();
});
$userCollection = collect();
foreach ($userReports as $reportWithData) {
$dateCollection = collect();
foreach ($reportWithData as $key => $reports) {
$reportsCollection = collect();
foreach ($reports as $report) {
$reportsCollection->push($report);
}
$dateCollection->push([
'day' => $report->rdv_date,
'reports' => $reportsCollection,
]);
}
$userCollection->push([
'contact' => $report->contact->address->country ? $report->contact->name . ', ' . $report->contact->address->country->name : $report->contact->name,
'meetings' => $dateCollection,
]);
}
// creation du fichier
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// font style
$phpWord->addFontStyle('contactStyle', ['name' => 'Verdana', 'bold' => true, 'size' => 10]);
$phpWord->addFontStyle('defaultStyle', ['name' => 'Verdana', 'bold' => false, 'size' => 10]);
$phpWord->addFontStyle('wafStyle', ['name' => 'Verdana', 'bold' => true, 'size' => 10, 'color' => 'black']);
$phpWord->addFontStyle('headerStyle', ['name' => 'Verdana', 'bold' => true, 'size' => 15, 'color' => '001d3d', 'marginTop' => 50, 'width' => 400]);
$wafTableStyle = ['borderColor' => '4f81bd', 'borderSize' => 2, 'cellMargin' => 30, 'valign' => 'center', 'bgColor' => '4f81bd'];
$wafFirstRowStyle = ['bgColor' => '4f81bd', 'bold' => true, 'size' => 11, 'valign' => 'center'];
$wafCellStyle = ['borderColor' => '4f81bd', 'borderSize' => 6, 'bgColor' => '4f81bd'];
$phpWord->addTableStyle('wafTable', $wafTableStyle, $wafFirstRowStyle);
//font tableau
$tableStyle = ['borderColor' => 'bebfbd', 'borderSize' => 1, 'valign' => 'center', 'cellMarginLeft' => 80];
$cellContactStyle = ['borderColor' => 'bebfbd', 'borderSize' => 1, 'height' => 50, 'gridSpan' => 2];
$cellStyle = ['borderColor' => 'bebfbd', 'borderSize' => 1];
$phpWord->addTableStyle('table', $tableStyle);
//line header
// section
$section = $phpWord->addSection(['headerHeight' => 150, 'footerHeight' => 50]);
//header
$header = $section->addHeader();
$header->addText(' ');
$footer = $section->addFooter();
$footer->addPreserveText('- {PAGE} -', ['color' => '4c6077'], ['align' => 'right']);
$footer->addText('', ['marginTop' => 40]);
// ajout de la bande waf
$wafTable = $section->addTable('wafTable');
$wafTable->addRow(300);
$wafTable->addCell(9000)->addText('');
$section->addText(' ');
$section->addText('Main achievements:', 'defaultStyle');
// creation du tableau
$table = $section->addTable('table');
foreach ($userCollection as $user) {
$table->addRow();
$table->addCell(10000, $cellContactStyle)->addText(htmlspecialchars($user['contact']), 'contactStyle');
foreach ($user['meetings'] as $meeting) {
$table->addRow();
$table->addCell(300, $cellStyle)->addText(' ');
$cell = $table->addCell(9700, $cellStyle);
foreach ($meeting['reports'] as $report) {
if ($report->laboratory) {
$cell->addText('DO: ' . htmlspecialchars($report->project), 'contactStyle');
}
if (request('showDate')) {
$cell->addText(htmlspecialchars(Carbon::parse($meeting['day'], 'UTC')->locale('en_EN')->isoFormat('dddd, MMMM D, YYYY')), 'contactStyle');
}
if (request('showUser')) {
$cell->addText(htmlspecialchars($report->user->name), 'contactStyle');
}
if ($report->resume == '') {
if ($report->highlights->count()) {
$cell->addText('Highlights', 'contactStyle');
$highlight = '
';
foreach ($report->highlights as $high) {
$high = str_replace('&', 'and', $high->value);
$highlight .= '- ' . $high . '
';
}
$highlight .= '
';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $highlight);
}
if ($report->lowlights->count()) {
$cell->addText('Lowlights', 'contactStyle');
$lowlights = '';
foreach ($report->lowlights as $low) {
$low = str_replace('&', 'and', $low->value);
$lowlights .= '- ' . $low . '
';
}
$lowlights .= '
';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $lowlights);
}
if ($report->new_projects->count()) {
$cell->addText('New project', 'contactStyle');
$newProjects = '';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $newProjects);
}
if ($report->project_updates->count()) {
$cell->addText('Projet update', 'contactStyle');
$projects = '';
foreach ($report->project_updates as $project_update) {
$project = str_replace('&', 'and', $project_update->value);
$projects .= '- ' . $project . '
';
}
$projects .= '
';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $projects);
}
if ($report->claims->count()) {
$cell->addText('Claim', 'contactStyle');
$claims = '';
foreach ($report->claims as $claim) {
$claim_temp = str_replace('&', 'and', $claim->value);
$claims .= '- ' . $claim_temp . '
';
}
$claims .= '
';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $claims);
}
if ($report->order_status->count()) {
$cell->addText('Order status', 'contactStyle');
$orderStatus = '';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $orderStatus);
}
if ($report->payment_status->count()) {
$cell->addText('Payment status', 'contactStyle');
$paymentStatus = '';
foreach ($report->payment_status as $payment) {
$payment_temp = str_replace('&', 'and', $payment->value);
$paymentStatus .= '- ' . $payment_temp . '
';
}
$paymentStatus .= '
';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $paymentStatus);
}
if ($report->tech_support_requests->count()) {
$cell->addText('Technical support request', 'contactStyle');
$techs = '';
foreach ($report->tech_support_requests as $tech) {
$tech_temp = str_replace('&', 'and', $tech->value);
$techs .= '- ' . $tech_temp . '
';
}
$techs .= '
';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $techs);
}
if ($report->competition_updates->count()) {
$cell->addText('Competition update', 'contactStyle');
$competition_updates = '';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $competition_updates);
}
if ($report->portfolioFormatted->count()) {
$cell->addText('New launch', 'contactStyle');
foreach ($report->portfolioFormatted as $portofolio) {
if ($portofolio->application) {
$application = str_replace('&', 'and', $portofolio->application->name);
$textApplication = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textApplication->addText('Application: ', ['bold' => true, 'size' => 10]);
$textApplication->addText($application);
}
if ($portofolio->b2c) {
$b2cNL = str_replace('&', 'and', $portofolio->b2c->name);
$textB2c = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textB2c->addText('B2C: ', ['bold' => true, 'size' => 10]);
$textB2c->addText($b2cNL);
}
if ($portofolio->b2b) {
$b2bNL = str_replace('&', 'and', $portofolio->b2b->name);
$textB2b = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textB2b->addText('B2B: ', ['bold' => true, 'size' => 10]);
$textB2b->addText($b2bNL);
}
if ($portofolio->description) {
$descriptionNL = str_replace('&', 'and', $portofolio->description);
$textDescription = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textDescription->addText('Description: ', ['bold' => true, 'size' => 10]);
$textDescription->addText($descriptionNL);
}
if ($portofolio->brand) {
$brandNL = str_replace('&', 'and', $portofolio->brand->name);
$textBrand = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textBrand->addText('Brand: ', ['bold' => true, 'size' => 10]);
$textBrand->addText($brandNL);
}
if ($portofolio->recipe) {
$recipeNL = str_replace('&', 'and', $portofolio->recipe->recipe);
$textRecipe = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textRecipe->addText('Recipe: ', ['bold' => true, 'size' => 10]);
$textRecipe->addText($recipeNL);
}
if (optional($portofolio)->packaging) {
$packagingFormatted = [];
if (optional($portofolio)->packaging) {
$packagingFormatted = $portofolio->packaging->map(function ($pack) {
return $pack->name;
});
}
$all = implode(', ', $packagingFormatted->toArray());
$textPack = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textPack->addText('Packaging: ', ['bold' => true, 'size' => 10]);
$textPack->addText($all);
}
if ($portofolio->componentWithSupplier->count()) {
$textRecipe = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textRecipe->addText('Components: ', ['bold' => true, 'size' => 10]);
foreach ($portofolio->componentWithSupplier as $portfolioFormatted) {
$supplierName = $portfolioFormatted['supplier'] ?? 'Unknown supplier';
$textSupplier = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch(2)]);
$supplierName = str_replace('&', 'and', $supplierName);
$textSupplier->addText($supplierName, ['bold' => true, 'size' => 11]);
foreach ($portfolioFormatted['components'] as $key => $component) {
$key = $key + 1;
$componentName = str_replace('&', 'and', $component->component ? $component->component->full_name : '');
$textComponent = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch(3)]);
$textComponent->addText("{$key}. {$componentName}");
}
}
}
}
}
if ($report->contracts->count()) {
$cell->addText('Contracts', 'contactStyle');
foreach ($report->contracts as $key => $contracts) {
$cell->addText('Contract ' . $key + 1, 'contactStyle');
$date = str_replace('&', 'and', $contracts->start_date->format('d-m-Y') . ' to ' . $contracts->end_date->format('d-m-Y'));
$textDate = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textDate->addText('Validate period: ', ['bold' => true, 'size' => 10]);
$textDate->addText($date);
$total = number_format(floatval($contracts->contract_components_sum_total), 2) . ' ' . ($contracts->currency ? $contracts->currency->symbol : '');
$textTotal = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textTotal->addText('Total: ', ['bold' => true, 'size' => 10]);
$textTotal->addText($total);
foreach ($contracts->contractComponents as $key => $contract) {
$textTotal = $cell->addTextRun(['indent' => \PhpOffice\PhpWord\Shared\Converter::cmToInch()]);
$textTotal->addText('Component ' . $key + 1, ['bold' => true, 'size' => 10]);
$components = '';
$components .= 'Price in kilogram : ' . (number_format(floatval($contract->price), 2) . ' ' . ($contracts->currency ? $contracts->currency->symbol : '')) . '
';
$components .= ' Quantity : ' . number_format(floatval($contract->quantity), 2) . '
';
$components .= ' Component : ' . ($contract->component ? $contract->component->full_name : '') . '
';
$components .= ' Total : ' . (number_format(floatval($contract->total), 2) . ' ' . ($contracts->currency ? $contracts->currency->symbol : '')) . '
';
$components .= '
';
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $components);
}
}
}
if (! ($meeting['reports']->last()->id == $report->id)) {
$cell->addText('', [], ['borderBottomSize' => 6]);
}
} else {
$resume = str_replace('
', '
', $report->resume);
$resume = str_replace('
', '
', $resume);
$resume = str_replace('&', 'and', $resume);
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, $resume);
if (! ($meeting['reports']->last()->id == $report->id)) {
$cell->addText('', [], ['borderBottomSize' => 6]);
}
}
}
}
}
try {
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save(storage_path('/app/public/Rapport.docx'));
} catch (Exception $e) {
}
$publicPath = 'storage/Rapport.docx';
return response()->download(public_path($publicPath));
}