Merge branch '341-export-csv-thirdparties' into 'master'

Resolve "Extraction de la liste des tiers au format CSV"

Closes #341

See merge request Chill-Projet/chill-bundles!789
This commit is contained in:
Julien Fastré 2025-02-19 11:37:46 +00:00
commit 350661a4fa
6 changed files with 190 additions and 5 deletions

View File

@ -0,0 +1,6 @@
kind: Feature
body: Add a list of third parties from within the admin (csv download)
time: 2025-02-19T12:09:28.487991703+01:00
custom:
Issue: "341"
SchemaChange: No schema change

View File

@ -71,9 +71,8 @@ services:
Chill\PersonBundle\Controller\PersonSignatureController:
tags: [ 'controller.service_arguments' ]
Chill\PersonBundle\Controller\SocialIssueCSVExportController:
tags: [ 'controller.service_arguments' ]
Chill\PersonBundle\Controller\SocialActionCSVExportController:
tags: [ 'controller.service_arguments' ]
Chill\PersonBundle\Controller\SocialIssueCSVExportController:
tags: [ 'controller.service_arguments' ]

View File

@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\ThirdPartyBundle\Controller;
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
use League\Csv\CannotInsertRecord;
use League\Csv\Exception;
use League\Csv\UnavailableStream;
use League\Csv\Writer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class ThirdpartyCSVExportController extends AbstractController
{
public function __construct(
private readonly Security $security,
private readonly TranslatorInterface $translator,
private readonly ThirdPartyRepository $thirdPartyRepository,
) {}
/**
* @throws UnavailableStream
* @throws CannotInsertRecord
* @throws Exception
*/
#[Route(path: '/{_locale}/admin/thirdparty/export/list.{_format}', name: 'chill_thirdparty_admin_export_list', requirements: ['_format' => 'csv'])]
public function socialIssueList(Request $request, string $_format = 'csv'): StreamedResponse
{
if (!$this->security->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedHttpException('Only ROLE_ADMIN can export this list');
}
$streamCallback = function () use ($request) {
$output = fopen('php://output', 'w');
// Add BOM for UTF-8
fwrite($output, "\xEF\xBB\xBF");
// Create CSV writer
$csv = Writer::createFromStream($output);
// Write header row
$header = array_map(
fn (string $e) => $this->translator->trans($e),
[
'Id',
'Kind',
'Civility',
'Firstname',
'Name',
'Profession',
'Telephone',
'Email',
'Address',
'Comment',
'Parent',
'Name_company',
'Acronym',
'Active',
'Contact id',
'Contact name',
'Contact firstname',
'Contact phone',
'Contact email',
'Contact address',
'Contact profession',
]
);
$csv->insertOne($header);
foreach ($this->thirdPartyRepository->findThirdpartiesWithContacts($request->getLocale()) as $row) {
$csv->insertOne($row);
flush();
}
};
return new StreamedResponse(
function () use ($streamCallback) {
$streamCallback();
},
Response::HTTP_OK,
[
'Content-Encoding' => 'none',
'Content-Type' => 'text/csv; charset=UTF-8',
'Content-Disposition' => 'attachment; filename="thirdparties.csv"',
]
);
}
}

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\ThirdPartyBundle\Repository;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
@ -22,11 +23,63 @@ class ThirdPartyRepository implements ObjectRepository
{
private readonly EntityRepository $repository;
public function __construct(EntityManagerInterface $em)
public function __construct(EntityManagerInterface $em, private readonly Connection $connection)
{
$this->repository = $em->getRepository(ThirdParty::class);
}
public function findThirdpartiesWithContacts(string $lang): iterable
{
$sql = "
SELECT
parent.id AS Id,
CASE
WHEN parent.kind = 'company' THEN 'Personne morale'
ELSE 'Personne physique'
END AS kind,
parent_civility.name->>? AS civility,
parent.firstname AS firstname,
parent.name AS name,
parent.profession AS profession,
parent.telephone AS telephone,
parent.email AS email,
CONCAT_WS(' ', parent_address.street, parent_address.streetnumber, parent_postal.code, parent_postal.label) AS address,
parent.comment AS comment,
parent.parent_id AS parent,
parent.name_company AS name_company,
parent.acronym AS acronym,
CASE WHEN parent.active THEN 'X' ELSE '' END AS active,
-- Contact (child third-party) details
contact.id AS contact_id,
contact.name AS contact_name,
contact.firstname AS contact_firstname,
contact.telephone AS contact_phone,
contact.email AS contact_email,
contact.profession AS contact_profession,
CONCAT_WS(' ', contact_address.street, contact_address.streetnumber, contact_postal.code, contact_postal.label) AS contact_address
FROM chill_3party.third_party parent
LEFT JOIN chill_main_civility parent_civility ON parent.civility_id = parent_civility.id
LEFT JOIN chill_main_address parent_address ON parent.address_id = parent_address.id
LEFT JOIN chill_main_postal_code parent_postal ON parent_address.postcode_id = parent_postal.id
-- Join contacts (child third parties)
LEFT JOIN chill_3party.third_party contact ON contact.parent_id = parent.id
LEFT JOIN chill_main_address contact_address ON contact.address_id = contact_address.id
LEFT JOIN chill_main_postal_code contact_postal ON contact_address.postcode_id = contact_postal.id
-- Exclude contacts from appearing as standalone rows
WHERE parent.parent_id IS NULL
-- Sorting order
ORDER BY parent.name ASC, parent.name_company ASC, parent.id ASC,
contact.name ASC, contact.firstname ASC, contact.id ASC
";
return $this->connection->iterateAssociative($sql, [$lang]);
}
/**
* count amongst parties associated to $centers, with $terms parameters.
*/

View File

@ -9,5 +9,9 @@
{% block layout_wvm_content %}
{% block admin_content %}<!-- block content empty -->
<h1>{{ 'Third party configuration' |trans }}</h1>
<div>
<p>{{ 'admin.export_description'|trans }}</p>
<a href="{{ path('chill_thirdparty_admin_export_list') }}" class="btn btn-download"></a>
</div>
{% endblock %}
{% endblock %}

View File

@ -133,3 +133,22 @@ is thirdparty: Le demandeur est un tiers
Filter by person's who have a residential address located at a thirdparty of type: Filtrer les usagers qui ont une addresse de résidence chez un tiers
"Filtered by person's who have a residential address located at a thirdparty of type %thirdparty_type% and valid on %date_calc%": "Uniquement les usagers qui ont une addresse de résidence chez un tiers de catégorie %thirdparty_type% et valide sur la date %date_calc%"
# admin
admin:
export_description: Liste des tiers (format CSV)
Profession: Profession
Firstname: Prénom
Name_company: Service/Département
Address: Adresse
Civility: Civilité
Id: Identifiant
Contact id: Identifiant du contact
Contact name: Nom du contact
Contact firstname: Prénom du contact
Contact phone: Téléphone du contact
Contact email: Courrier électronique du contact
Contact address: Adresse du contact
Contact profession: Profession du contact