Merge branch 'master' into upgrade-php82

This commit is contained in:
Julien Fastré 2023-02-08 15:35:29 +01:00
commit 3b255e8482
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
11 changed files with 374 additions and 90 deletions

View File

@ -14,9 +14,8 @@ namespace Chill\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\ChargeKind;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
class ChargeKindRepository implements ObjectRepository
final class ChargeKindRepository implements ChargeKindRepositoryInterface
{
private EntityRepository $repository;
@ -50,7 +49,8 @@ class ChargeKindRepository implements ObjectRepository
->where($qb->expr()->eq('c.isActive', 'true'))
->orderBy('c.ordering', 'ASC')
->getQuery()
->getResult();
->getResult()
;
}
/**
@ -77,6 +77,11 @@ class ChargeKindRepository implements ObjectRepository
return $this->repository->findOneBy($criteria);
}
public function findOneByKind(string $kind): ?ChargeKind
{
return $this->repository->findOneBy(['kind' => $kind]);
}
public function getClassName(): string
{
return ChargeKind::class;

View File

@ -0,0 +1,49 @@
<?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\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\ChargeKind;
use Doctrine\Persistence\ObjectRepository;
interface ChargeKindRepositoryInterface extends ObjectRepository
{
public function find($id): ?ChargeKind;
/**
* @return ChargeType[]
*/
public function findAll(): array;
/**
* @return ChargeType[]
*/
public function findAllActive(): array;
public function findOneByKind(string $kind): ?ChargeKind;
/**
* @return ChargeType[]
*/
public function findAllByType(string $type): array;
/**
* @param mixed|null $limit
* @param mixed|null $offset
*
* @return ChargeType[]
*/
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array;
public function findOneBy(array $criteria): ?ChargeKind;
public function getClassName(): string;
}

View File

@ -14,9 +14,8 @@ namespace Chill\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\ResourceKind;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
class ResourceKindRepository implements ObjectRepository
final class ResourceKindRepository implements ResourceKindRepositoryInterface
{
private EntityRepository $repository;
@ -50,7 +49,8 @@ class ResourceKindRepository implements ObjectRepository
->where($qb->expr()->eq('r.isActive', 'true'))
->orderBy('r.ordering', 'ASC')
->getQuery()
->getResult();
->getResult()
;
}
public function findOneByKind(string $kind): ?ResourceKind
@ -82,6 +82,11 @@ class ResourceKindRepository implements ObjectRepository
return $this->repository->findOneBy($criteria);
}
public function findOneByKind(string $kind): ?ResourceKind
{
return $this->repository->findOneBy(['kind' => $kind]);
}
public function getClassName(): string
{
return ResourceKind::class;

View File

@ -0,0 +1,49 @@
<?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\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\ResourceKind;
use Doctrine\Persistence\ObjectRepository;
interface ResourceKindRepositoryInterface extends ObjectRepository
{
public function find($id): ?ResourceKind;
/**
* @return ResourceType[]
*/
public function findAll(): array;
/**
* @return ResourceType[]
*/
public function findAllActive(): array;
/**
* @return ResourceType[]
*/
public function findAllByType(string $type): array;
/**
* @param mixed|null $limit
* @param mixed|null $offset
*
* @return ResourceType[]
*/
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array;
public function findOneBy(array $criteria): ?ResourceKind;
public function findOneByKind(string $kind): ?ResourceKind;
public function getClassName(): string;
}

View File

@ -34,7 +34,7 @@ class ResourceRepository extends EntityRepository
//->andWhere('c.startDate < :date')
// TODO: there is a misconception here, the end date must be lower or null. startDate are never null
//->andWhere('c.startDate < :date OR c.startDate IS NULL');
;
;
if (null !== $sort) {
$qb->orderBy($sort);

View File

@ -14,7 +14,9 @@ namespace Chill\BudgetBundle\Service\Summary;
use Chill\BudgetBundle\Entity\ChargeKind;
use Chill\BudgetBundle\Entity\ResourceKind;
use Chill\BudgetBundle\Repository\ChargeKindRepository;
use Chill\BudgetBundle\Repository\ChargeKindRepositoryInterface;
use Chill\BudgetBundle\Repository\ResourceKindRepository;
use Chill\BudgetBundle\Repository\ResourceKindRepositoryInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
@ -27,7 +29,7 @@ use function count;
/**
* Helps to find a summary of the budget: the sum of resources and charges.
*/
class SummaryBudget implements SummaryBudgetInterface
final class SummaryBudget implements SummaryBudgetInterface
{
private const QUERY_CHARGE_BY_HOUSEHOLD = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, charge_id AS kind_id FROM chill_budget.charge WHERE (person_id IN (_ids_) OR household_id = ?) AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY charge_id';
@ -37,23 +39,19 @@ class SummaryBudget implements SummaryBudgetInterface
private const QUERY_RESOURCE_BY_PERSON = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, resource_id AS kind_id FROM chill_budget.resource WHERE person_id = ? AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY resource_id';
private ChargeKindRepository $chargeKindRepository;
private array $chargeLabels;
private ChargeKindRepositoryInterface $chargeKindRepository;
private EntityManagerInterface $em;
private ResourceKindRepository $resourceKindRepository;
private array $resourcesLabels;
private ResourceKindRepositoryInterface $resourceKindRepository;
private TranslatableStringHelperInterface $translatableStringHelper;
public function __construct(
EntityManagerInterface $em,
TranslatableStringHelperInterface $translatableStringHelper,
ResourceKindRepository $resourceKindRepository,
ChargeKindRepository $chargeKindRepository
ResourceKindRepositoryInterface $resourceKindRepository,
ChargeKindRepositoryInterface $chargeKindRepository
) {
$this->em = $em;
$this->translatableStringHelper = $translatableStringHelper;
@ -129,19 +127,19 @@ class SummaryBudget implements SummaryBudgetInterface
private function getEmptyChargeArray(): array
{
$keys = array_map(static fn (ChargeKind $kind) => $kind->getId(), $this->chargeKindRepository->findAll());
$keys = array_map(static fn (ChargeKind $kind) => $kind->getKind(), $this->chargeKindRepository->findAll());
return array_combine($keys, array_map(function ($id) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($this->chargeKindRepository->find($id)->getName()), 'comment' => ''];
return array_combine($keys, array_map(function ($kind) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($this->chargeKindRepository->findOneByKind($kind)->getName()), 'comment' => ''];
}, $keys));
}
private function getEmptyResourceArray(): array
{
$keys = array_map(static fn (ResourceKind $kind) => $kind->getId(), $this->resourceKindRepository->findAll());
$keys = array_map(static fn (ResourceKind $kind) => $kind->getKind(), $this->resourceKindRepository->findAll());
return array_combine($keys, array_map(function ($id) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($this->resourceKindRepository->find($id)->getName()), 'comment' => ''];
return array_combine($keys, array_map(function ($kind) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($this->resourceKindRepository->findOneByKind($kind)->getName()), 'comment' => ''];
}, $keys));
}
@ -155,7 +153,7 @@ class SummaryBudget implements SummaryBudgetInterface
$chargeKind = $this->chargeKindRepository->find($row['kind_id']);
if (null === $chargeKind) {
throw new RuntimeException('charge kind not found');
throw new RuntimeException('charge kind not found: ' . $row['kind_id']);
}
$result[$chargeKind->getKind()] = [
'sum' => (float) $row['sum'],
@ -171,7 +169,7 @@ class SummaryBudget implements SummaryBudgetInterface
$resourceKind = $this->resourceKindRepository->find($row['kind_id']);
if (null === $resourceKind) {
throw new RuntimeException('charge kind not found');
throw new RuntimeException('charge kind not found: ' . $row['kind_id']);
}
$result[$resourceKind->getKind()] = [

View File

@ -0,0 +1,158 @@
<?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\BudgetBundle\Tests\Service\Summary;
use Chill\BudgetBundle\Entity\ChargeKind;
use Chill\BudgetBundle\Entity\ResourceKind;
use Chill\BudgetBundle\Repository\ChargeKindRepositoryInterface;
use Chill\BudgetBundle\Repository\ResourceKindRepositoryInterface;
use Chill\BudgetBundle\Service\Summary\SummaryBudget;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
/**
* @internal
*
* @coversNothing
*/
final class SummaryBudgetTest extends TestCase
{
use ProphecyTrait;
public function testGenerateSummaryForPerson(): void
{
$queryCharges = $this->prophesize(AbstractQuery::class);
$queryCharges->getResult()->willReturn([
[
'sum' => 250.0,
'comment' => '',
'kind_id' => 1, // kind: rental
],
]);
$queryCharges->setParameters(Argument::type('array'))
->will(function ($args, $query) {
return $query;
})
;
$queryResources = $this->prophesize(AbstractQuery::class);
$queryResources->getResult()->willReturn([
[
'sum' => 1500.0,
'comment' => '',
'kind_id' => 2, // kind: 'salary',
],
]);
$queryResources->setParameters(Argument::type('array'))
->will(function ($args, $query) {
return $query;
})
;
$em = $this->prophesize(EntityManagerInterface::class);
$em->createNativeQuery(Argument::type('string'), Argument::type(Query\ResultSetMapping::class))
->will(function ($args) use ($queryResources, $queryCharges) {
if (false !== strpos($args[0], 'chill_budget.resource')) {
return $queryResources->reveal();
}
if (false !== strpos($args[0], 'chill_budget.charge')) {
return $queryCharges->reveal();
}
throw new \RuntimeException('this query does not have a stub counterpart: '.$args[0]);
})
;
$chargeRepository = $this->prophesize(ChargeKindRepositoryInterface::class);
$chargeRepository->findAll()->willReturn([
$rental = (new ChargeKind())->setKind('rental')->setName(['fr' => 'Rental']),
$other = (new ChargeKind())->setKind('other')->setName(['fr' => 'Other']),
]);
$chargeRepository->find(1)->willReturn($rental);
$chargeRepository->findOneByKind('rental')->willReturn($rental);
$chargeRepository->findOneByKind('other')->willReturn($other);
$resourceRepository = $this->prophesize(ResourceKindRepositoryInterface::class);
$resourceRepository->findAll()->willReturn([
$salary = (new ResourceKind())->setKind('salary')->setName(['fr' => 'Salary']),
$misc = (new ResourceKind())->setKind('misc')->setName(['fr' => 'Misc']),
]);
$resourceRepository->find(2)->willReturn($salary);
$resourceRepository->findOneByKind('salary')->willReturn($salary);
$resourceRepository->findOneByKind('misc')->willReturn($misc);
$translatableStringHelper = $this->prophesize(TranslatableStringHelperInterface::class);
$translatableStringHelper->localize(Argument::type('array'))->will(function ($arg) {
return $arg[0]['fr'];
});
$person = new Person();
$personReflection = new \ReflectionClass($person);
$personIdReflection = $personReflection->getProperty('id');
$personIdReflection->setAccessible(true);
$personIdReflection->setValue($person, 1);
$household = new Household();
$householdReflection = new \ReflectionClass($household);
$householdId = $householdReflection->getProperty('id');
$householdId->setAccessible(true);
$householdId->setValue($household, 1);
$householdMember = (new HouseholdMember())->setPerson($person)
->setStartDate(new \DateTimeImmutable('1 month ago'))
;
$household->addMember($householdMember);
$summaryBudget = new SummaryBudget(
$em->reveal(),
$translatableStringHelper->reveal(),
$resourceRepository->reveal(),
$chargeRepository->reveal()
);
$summary = $summaryBudget->getSummaryForPerson($person);
$summaryForHousehold = $summaryBudget->getSummaryForHousehold($household);
// we check the structure for the summary. The structure is the same for household
// and persons
$expected = [
'charges' => [
'rental' => ['sum' => 250.0, 'comment' => '', 'label' => 'Rental'],
'other' => ['sum' => 0.0, 'comment' => '', 'label' => 'Other'],
],
'resources' => [
'salary' => ['sum' => 1500.0, 'comment' => '', 'label' => 'Salary'],
'misc' => ['sum' => 0.0, 'comment' => '', 'label' => 'Misc'],
],
];
foreach ([$summaryForHousehold, $summary] as $summary) {
$this->assertIsArray($summary);
$this->assertEqualsCanonicalizing(['charges', 'resources'], array_keys($summary));
$this->assertEqualsCanonicalizing(['rental', 'other'], array_keys($summary['charges']));
$this->assertEqualsCanonicalizing(['salary', 'misc'], array_keys($summary['resources']));
foreach ($expected as $resCha => $contains) {
foreach ($contains as $kind => $row) {
$this->assertEqualsCanonicalizing($row, $summary[$resCha][$kind]);
}
}
}
}
}

View File

@ -272,8 +272,9 @@ final class DocGeneratorTemplateController extends AbstractController
}
if ($isTest && isset($form) && $form['show_data']->getData()) {
// very ugly hack...
dd($context->getData($template, $entity, $contextGenerationData));
return $this->render('@ChillDocGenerator/Generator/debug_value.html.twig', [
'datas' => json_encode($context->getData($template, $entity, $contextGenerationData), JSON_PRETTY_PRINT)
]);
}
try {

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>{{ 'Doc generator debug'|trans }}</title>
</head>
<body>
<pre>{{ datas }}</pre>
</body>
</html>

View File

@ -7,7 +7,7 @@
<li v-if="props.canEdit && is_extension_editable(props.storedObject.type)">
<wopi-edit-button :stored-object="props.storedObject" :classes="{'dropdown-item': true}" :execute-before-leave="props.executeBeforeLeave"></wopi-edit-button>
</li>
<li v-if="props.storedObject.type != 'application/pdf' && props.canConvertPdf">
<li v-if="props.storedObject.type != 'application/pdf' && is_extension_viewable(props.storedObject.type) && props.canConvertPdf">
<convert-button :stored-object="props.storedObject" :filename="filename" :classes="{'dropdown-item': true}"></convert-button>
</li>
<li v-if="props.canDownload">
@ -23,7 +23,7 @@
import ConvertButton from "./StoredObjectButton/ConvertButton.vue";
import DownloadButton from "./StoredObjectButton/DownloadButton.vue";
import WopiEditButton from "./StoredObjectButton/WopiEditButton.vue";
import {is_extension_editable} from "./StoredObjectButton/helpers";
import {is_extension_editable, is_extension_viewable} from "./StoredObjectButton/helpers";
import {StoredObject, WopiEditButtonExecutableBeforeLeaveFunction} from "../types";
interface DocumentActionButtonsGroupConfig {

View File

@ -1,97 +1,107 @@
const SUPPORTED_MIMES = new Set([
'image/svg+xml',
const MIMES_EDIT = new Set([
'application/vnd.ms-powerpoint',
'application/vnd.ms-excel',
'application/vnd.sun.xml.writer',
'application/vnd.oasis.opendocument.text',
'application/vnd.oasis.opendocument.text-flat-xml',
'application/vnd.sun.xml.calc',
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.oasis.opendocument.spreadsheet-flat-xml',
'application/vnd.sun.xml.impress',
'application/vnd.oasis.opendocument.presentation',
'application/vnd.oasis.opendocument.presentation-flat-xml',
'application/vnd.sun.xml.draw',
'application/vnd.oasis.opendocument.graphics',
'application/vnd.oasis.opendocument.graphics-flat-xml',
'application/vnd.oasis.opendocument.chart',
'application/vnd.sun.xml.writer.global',
'application/vnd.oasis.opendocument.text-master',
'application/vnd.sun.xml.writer.template',
'application/vnd.oasis.opendocument.text-template',
'application/vnd.oasis.opendocument.text-master-template',
'application/vnd.sun.xml.calc.template',
'application/vnd.oasis.opendocument.spreadsheet-template',
'application/vnd.sun.xml.impress.template',
'application/vnd.oasis.opendocument.presentation-template',
'application/vnd.sun.xml.draw.template',
'application/vnd.oasis.opendocument.graphics-template',
'application/msword',
'application/msword',
'application/vnd.ms-excel',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-word.document.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'application/vnd.ms-word.template.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'application/vnd.ms-excel.template.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
'application/vnd.ms-excel.sheet.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.presentationml.template',
'application/vnd.ms-powerpoint.template.macroEnabled.12',
'application/vnd.wordperfect',
'application/x-aportisdoc',
'application/x-hwp',
'application/vnd.ms-works',
'application/x-mswrite',
'application/x-dif-document',
'text/spreadsheet',
'text/csv',
'application/x-dbase',
'application/vnd.lotus-1-2-3',
'image/cgm',
'image/vnd.dxf',
'image/x-emf',
'image/x-wmf',
'application/coreldraw',
'application/vnd.visio2013',
'application/vnd.visio',
'application/vnd.ms-visio.drawing',
'application/x-mspublisher',
'application/x-sony-bbeb',
'application/x-gnumeric',
'application/macwriteii',
'application/x-iwork-numbers-sffnumbers',
'application/vnd.oasis.opendocument.text-web',
'application/x-pagemaker',
'text/rtf',
'text/plain',
'application/x-fictionbook+xml',
'application/clarisworks',
'image/x-wpg',
'application/x-iwork-pages-sffpages',
'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'application/x-iwork-keynote-sffkey',
'application/x-abiword',
'image/x-freehand',
'application/vnd.sun.xml.chart',
'application/x-t602',
'image/bmp',
'image/png',
'image/gif',
'image/tiff',
'image/jpg',
'image/jpeg',
'application/pdf',
]);
const MIMES_VIEW = new Set([
...MIMES_EDIT,
[
'image/svg+xml',
'application/vnd.sun.xml.writer',
'application/vnd.sun.xml.calc',
'application/vnd.sun.xml.impress',
'application/vnd.sun.xml.draw',
'application/vnd.sun.xml.writer.global',
'application/vnd.sun.xml.writer.template',
'application/vnd.sun.xml.calc.template',
'application/vnd.sun.xml.impress.template',
'application/vnd.sun.xml.draw.template',
'application/vnd.oasis.opendocument.text-master',
'application/vnd.oasis.opendocument.text-template',
'application/vnd.oasis.opendocument.text-master-template',
'application/vnd.oasis.opendocument.spreadsheet-template',
'application/vnd.oasis.opendocument.presentation-template',
'application/vnd.oasis.opendocument.graphics-template',
'application/vnd.ms-word.template.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'application/vnd.ms-excel.template.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.presentationml.template',
'application/vnd.ms-powerpoint.template.macroEnabled.12',
'application/vnd.wordperfect',
'application/x-aportisdoc',
'application/x-hwp',
'application/vnd.ms-works',
'application/x-mswrite',
'application/vnd.lotus-1-2-3',
'image/cgm',
'image/vnd.dxf',
'image/x-emf',
'image/x-wmf',
'application/coreldraw',
'application/vnd.visio2013',
'application/vnd.visio',
'application/vnd.ms-visio.drawing',
'application/x-mspublisher',
'application/x-sony-bbeb',
'application/x-gnumeric',
'application/macwriteii',
'application/x-iwork-numbers-sffnumbers',
'application/vnd.oasis.opendocument.text-web',
'application/x-pagemaker',
'application/x-fictionbook+xml',
'application/clarisworks',
'image/x-wpg',
'application/x-iwork-pages-sffpages',
'application/x-iwork-keynote-sffkey',
'application/x-abiword',
'image/x-freehand',
'application/vnd.sun.xml.chart',
'application/x-t602',
'image/bmp',
'image/png',
'image/gif',
'image/tiff',
'image/jpg',
'image/jpeg',
'application/pdf',
]
])
function is_extension_editable(mimeType: string): boolean {
return SUPPORTED_MIMES.has(mimeType);
return MIMES_EDIT.has(mimeType);
}
function is_extension_viewable(mimeType: string): boolean {
return MIMES_VIEW.has(mimeType);
}
function build_convert_link(uuid: string) {
@ -165,4 +175,5 @@ export {
download_and_decrypt_doc,
download_doc,
is_extension_editable,
is_extension_viewable,
};