Compare commits

..

6 Commits

16 changed files with 198 additions and 113 deletions

View File

@@ -11,12 +11,13 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Calculator; namespace Chill\BudgetBundle\Calculator;
use Chill\BudgetBundle\Entity\AbstractElement; use Chill\BudgetBundle\Entity\Charge;
use Chill\BudgetBundle\Entity\Resource;
interface CalculatorInterface interface CalculatorInterface
{ {
/** /**
* @param AbstractElement[] $elements * @param array<Charge|Resource> $elements
*/ */
public function calculate(array $elements): ?CalculatorResult; public function calculate(array $elements): ?CalculatorResult;

View File

@@ -12,6 +12,8 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Calculator; namespace Chill\BudgetBundle\Calculator;
use Chill\BudgetBundle\Entity\AbstractElement; use Chill\BudgetBundle\Entity\AbstractElement;
use Chill\BudgetBundle\Entity\Charge;
use Chill\BudgetBundle\Entity\Resource;
use OutOfBoundsException; use OutOfBoundsException;
use function array_key_exists; use function array_key_exists;
@@ -21,11 +23,14 @@ use function implode;
class CalculatorManager class CalculatorManager
{ {
/** /**
* @var CalculatorInterface[] * @var array<string, CalculatorInterface>
*/ */
protected $calculators = []; private array $calculators = [];
protected $defaultCalculator = []; /**
* @var string[]
*/
private array $defaultCalculator = [];
public function addCalculator(CalculatorInterface $calculator, bool $default) public function addCalculator(CalculatorInterface $calculator, bool $default)
{ {
@@ -37,7 +42,7 @@ class CalculatorManager
} }
/** /**
* @param AbstractElement[] $elements * @param array<Resource|Charge> $elements
* *
* @return CalculatorResult[] * @return CalculatorResult[]
*/ */
@@ -46,23 +51,17 @@ class CalculatorManager
$results = []; $results = [];
foreach ($this->defaultCalculator as $alias) { foreach ($this->defaultCalculator as $alias) {
$calculator = $this->calculators[$alias]; $result = $this->getCalculator($alias)->calculate($elements);
$result = $calculator->calculate($elements);
if (null !== $result) { if (null !== $result) {
$results[$calculator->getAlias()] = $result; $results[$alias] = $result;
} }
} }
return $results; return $results;
} }
/** public function getCalculator(string $alias): CalculatorInterface
* @param string $alias
*
* @return CalculatorInterface
*/
public function getCalculator($alias)
{ {
if (false === array_key_exists($alias, $this->calculators)) { if (false === array_key_exists($alias, $this->calculators)) {
throw new OutOfBoundsException("The calculator with alias '{$alias}' does " throw new OutOfBoundsException("The calculator with alias '{$alias}' does "

View File

@@ -14,6 +14,8 @@ namespace Chill\BudgetBundle\Controller;
use Chill\BudgetBundle\Calculator\CalculatorManager; use Chill\BudgetBundle\Calculator\CalculatorManager;
use Chill\BudgetBundle\Entity\Charge; use Chill\BudgetBundle\Entity\Charge;
use Chill\BudgetBundle\Entity\Resource; use Chill\BudgetBundle\Entity\Resource;
use Chill\BudgetBundle\Repository\ChargeRepository;
use Chill\BudgetBundle\Repository\ResourceRepository;
use Chill\BudgetBundle\Security\Authorization\BudgetElementVoter; use Chill\BudgetBundle\Security\Authorization\BudgetElementVoter;
use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
@@ -29,24 +31,32 @@ use function count;
class ElementController extends AbstractController class ElementController extends AbstractController
{ {
protected CalculatorManager $calculator; private CalculatorManager $calculator;
protected LoggerInterface $chillMainLogger; private LoggerInterface $chillMainLogger;
protected EntityManagerInterface $em; private EntityManagerInterface $em;
protected TranslatorInterface $translator; private ResourceRepository $resourceRepository;
private ChargeRepository $chargeRepository;
private TranslatorInterface $translator;
public function __construct( public function __construct(
EntityManagerInterface $em, EntityManagerInterface $em,
TranslatorInterface $translator, TranslatorInterface $translator,
LoggerInterface $chillMainLogger, LoggerInterface $chillMainLogger,
CalculatorManager $calculator CalculatorManager $calculator,
ResourceRepository $resourceRepository,
ChargeRepository $chargeRepository,
) { ) {
$this->em = $em; $this->em = $em;
$this->translator = $translator; $this->translator = $translator;
$this->chillMainLogger = $chillMainLogger; $this->chillMainLogger = $chillMainLogger;
$this->calculator = $calculator; $this->calculator = $calculator;
$this->resourceRepository = $resourceRepository;
$this->chargeRepository = $chargeRepository;
} }
/** /**
@@ -59,24 +69,10 @@ class ElementController extends AbstractController
{ {
$this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $person); $this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $person);
$charges = $this->em $charges = $this->chargeRepository->findAllByEntity($person);
->getRepository(Charge::class) $resources = $this->resourceRepository->findAllByEntity($person);
->findByPerson($person);
$ressources = $this->em $elements = array_merge($charges, $resources);
->getRepository(Resource::class)
->findByPerson($person);
$now = new DateTime('now');
$actualCharges = $this->em
->getRepository(Charge::class)
->findByEntityAndDate($person, $now);
$actualResources = $this->em
->getRepository(Resource::class)
->findByEntityAndDate($person, $now);
$elements = array_merge($actualCharges, $actualResources);
if (count($elements) > 0) { if (count($elements) > 0) {
$results = $this->calculator->calculateDefault($elements); $results = $this->calculator->calculateDefault($elements);
@@ -85,7 +81,7 @@ class ElementController extends AbstractController
return $this->render('ChillBudgetBundle:Person:index.html.twig', [ return $this->render('ChillBudgetBundle:Person:index.html.twig', [
'person' => $person, 'person' => $person,
'charges' => $charges, 'charges' => $charges,
'resources' => $ressources, 'resources' => $resources,
'results' => $results ?? [], 'results' => $results ?? [],
]); ]);
} }
@@ -100,60 +96,19 @@ class ElementController extends AbstractController
{ {
$this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $household); $this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $household);
$charges = $this->em $charges = $this->chargeRepository->findAllByEntity($household);
->getRepository(Charge::class) $resources = $this->resourceRepository->findAllByEntity($household);
->findByHousehold($household);
$ressources = $this->em $elements = array_merge($charges, $resources);
->getRepository(Resource::class)
->findByHousehold($household);
$now = new DateTime('now');
$actualCharges = $this->em
->getRepository(Charge::class)
->findByEntityAndDate($household, $now);
$actualResources = $this->em
->getRepository(Resource::class)
->findByEntityAndDate($household, $now);
$elements = array_merge($actualCharges, $actualResources);
if (count($elements) > 0) { if (count($elements) > 0) {
$results = $this->calculator->calculateDefault($elements); $results = $this->calculator->calculateDefault($elements);
} }
// quick solution to calculate the sum, difference and amount from
// controller. This should be done from the calculators
// TODO replace this by calculators
$wholeCharges = $actualCharges;
$wholeResources = $actualResources;
foreach ($household->getCurrentPersons() as $person) {
$wholeCharges = array_merge(
$wholeCharges,
$this->em
->getRepository(Charge::class)
->findByEntityAndDate($person, $now)
);
$wholeResources = array_merge(
$wholeResources,
$this->em
->getRepository(Resource::class)
->findByEntityAndDate($person, $now)
);
}
return $this->render('ChillBudgetBundle:Household:index.html.twig', [ return $this->render('ChillBudgetBundle:Household:index.html.twig', [
'household' => $household, 'household' => $household,
'charges' => $charges, 'charges' => $charges,
'resources' => $ressources, 'resources' => $resources,
'wholeResources' => array_filter($wholeResources, static function (Resource $r) use ($now) {
return $r->getStartDate() <= $now && ($r->getEndDate() === null || $r->getEndDate() >= $now);
}),
'wholeCharges' => array_filter($wholeCharges, static function (Charge $c) use ($now) {
return $c->getStartDate() <= $now && ($c->getEndDate() === null || $c->getEndDate() >= $now);
}),
'results' => $results ?? [], 'results' => $results ?? [],
]); ]);
} }

View File

@@ -11,9 +11,12 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Repository; namespace Chill\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\Charge;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use DateTime; use DateTime;
use Doctrine\ORM\EntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/** /**
* ChargeRepository. * ChargeRepository.
@@ -21,24 +24,44 @@ use Doctrine\ORM\EntityRepository;
* This class was generated by the Doctrine ORM. Add your own custom * This class was generated by the Doctrine ORM. Add your own custom
* repository methods below. * repository methods below.
*/ */
class ChargeRepository extends EntityRepository class ChargeRepository extends ServiceEntityRepository
{ {
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Charge::class);
}
/**
* @return Charge[]
*/
public function findAllByEntity(Person|Household $entity): array
{
$qb = $this->createQueryBuilder('c');
$property = $entity instanceof Person ? 'person' : 'household';
$qb->where("c.{$property} = :entity")
->setParameter('entity', $entity);
return $qb->getQuery()->getResult();
}
public function findByEntityAndDate($entity, DateTime $date, $sort = null) public function findByEntityAndDate($entity, DateTime $date, $sort = null)
{ {
$qb = $this->createQueryBuilder('c'); $qb = $this->createQueryBuilder('c');
$entityStr = $entity instanceof Person ? 'person' : 'household'; $entityStr = $entity instanceof Person ? 'person' : 'household';
$qb->where("c.{$entityStr} = :{$entityStr}") $qb->where("c.{$entityStr} = :entity")
->andWhere('c.startDate < :date') ->andWhere('c.startDate <= :date')
->andWhere('c.startDate < :date OR c.startDate IS NULL'); ->andWhere('c.endDate > :date OR c.endDate IS NULL');
if (null !== $sort) { if (null !== $sort) {
$qb->orderBy($sort); $qb->orderBy($sort);
} }
$qb->setParameters([ $qb->setParameters([
$entityStr => $entity, 'entity' => $entity,
'date' => $date, 'date' => $date,
]); ]);

View File

@@ -11,9 +11,13 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Repository; namespace Chill\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\Resource;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use DateTime; use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/** /**
* ResourceRepository. * ResourceRepository.
@@ -21,28 +25,45 @@ use Doctrine\ORM\EntityRepository;
* This class was generated by the Doctrine ORM. Add your own custom * This class was generated by the Doctrine ORM. Add your own custom
* repository methods below. * repository methods below.
*/ */
class ResourceRepository extends EntityRepository class ResourceRepository extends ServiceEntityRepository
{ {
public function findByEntityAndDate($entity, DateTime $date, $sort = null) public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Resource::class);
}
/**
* @return Resource[]
*/
public function findAllByEntity(Person|Household $entity): array
{
$qb = $this->createQueryBuilder('r');
$property = $entity instanceof Person ? 'person' : 'household';
$qb->where("r.{$property} = :entity")
->setParameter('entity', $entity);
return $qb->getQuery()->getResult();
}
public function findByEntityAndDate(Person|Household $entity, DateTime $date, $sort = null)
{ {
$qb = $this->createQueryBuilder('c'); $qb = $this->createQueryBuilder('c');
$entityStr = $entity instanceof Person ? 'person' : 'household'; $entityStr = $entity instanceof Person ? 'person' : 'household';
$qb->where("c.{$entityStr} = :{$entityStr}") $qb->where("c.{$entityStr} = :entity")
// TODO: in controller, the budget and charges asked are also for future and actual ->andWhere('c.startDate <= :date')
//->andWhere('c.startDate < :date') ->andWhere('c.endDate > :date OR c.endDate IS NULL');
// 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) { if (null !== $sort) {
$qb->orderBy($sort); $qb->orderBy($sort);
} }
$qb->setParameters([ $qb->setParameters([
$entityStr => $entity, 'entity' => $entity,
//'date' => $date, 'date' => $date,
]); ]);
return $qb->getQuery()->getResult(); return $qb->getQuery()->getResult();

View File

@@ -0,0 +1,56 @@
<?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\Migrations\Budget;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20230328155010 extends AbstractMigration
{
public function getDescription(): string
{
return 'budget elements: restore the previous type to resource/charge kind if applicable';
}
public function up(Schema $schema): void
{
$this->addSql(<<<'SQL'
WITH type_to_id AS (
SELECT DISTINCT charge.charge_id AS id, charge.type
FROM chill_budget.charge
WHERE type <> ''
)
UPDATE chill_budget.charge_type
SET kind = type_to_id.type
FROM type_to_id
WHERE type_to_id.type <> '' AND type_to_id.id = charge_type.id
SQL);
$this->addSql(<<<'SQL'
WITH type_to_id AS (
SELECT DISTINCT resource.resource_id AS id, resource.type
FROM chill_budget. resource
WHERE type <> ''
)
UPDATE chill_budget.resource_type
SET kind = type_to_id.type
FROM type_to_id
WHERE type_to_id.type <> '' AND type_to_id.id = resource_type.id
SQL);
}
public function down(Schema $schema): void
{
$this->addSql("UPDATE chill_budget.resource_type SET kind=md5(random()::text) WHERE kind = ''");
$this->addSql("UPDATE chill_budget.charge_type SET kind=md5(random()::text) WHERE kind = ''");
}
}

View File

@@ -92,6 +92,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface, HasCente
/** /**
* @ORM\ManyToOne(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod", inversedBy="calendars") * @ORM\ManyToOne(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod", inversedBy="calendars")
* @Serializer\Groups({"calendar:read", "read"})
*/ */
private ?AccompanyingPeriod $accompanyingPeriod = null; private ?AccompanyingPeriod $accompanyingPeriod = null;

View File

@@ -15,6 +15,7 @@
:picked="null !== this.$store.getters.getMainUser ? [this.$store.getters.getMainUser] : []" :picked="null !== this.$store.getters.getMainUser ? [this.$store.getters.getMainUser] : []"
:removableIfSet="false" :removableIfSet="false"
:displayPicked="false" :displayPicked="false"
:suggested="this.suggestedUsers"
@addNewEntity="setMainUser" @addNewEntity="setMainUser"
></pick-entity> ></pick-entity>
</div> </div>
@@ -143,6 +144,7 @@ export default {
slotMinTime: '09:00:00', slotMinTime: '09:00:00',
slotMaxTime: '18:00:00', slotMaxTime: '18:00:00',
hideWeekEnds: true, hideWeekEnds: true,
previousUser: [],
} }
}, },
computed: { computed: {
@@ -188,11 +190,23 @@ export default {
users.push(this.$store.getters.getUserDataById(id).user); users.push(this.$store.getters.getUserDataById(id).user);
} }
return users; return users;
} },
suggestedUsers() {
const suggested = [];
this.$data.previousUser.forEach(u => {
if (u.id !== this.$store.getters.getMainUser.id) {
suggested.push(u)
}
});
return suggested;
},
}, },
methods: { methods: {
setMainUser(user) { setMainUser({entity}) {
console.log('setMainUser APP', user); const user = entity;
console.log('setMainUser APP', entity);
if (user.id !== this.$store.getters.getMainUser && ( if (user.id !== this.$store.getters.getMainUser && (
this.$store.state.activity.calendarRange !== null this.$store.state.activity.calendarRange !== null
@@ -205,6 +219,14 @@ export default {
} }
} }
// add the previous user, if any, in the previous user list (in use for suggestion)
if (null !== this.$store.getters.getMainUser) {
const suggestedUids = new Set(this.$data.previousUser.map(u => u.id));
if (!suggestedUids.has(this.$store.getters.getMainUser.id)){
this.$data.previousUser.push(this.$store.getters.getMainUser);
}
}
this.$store.dispatch('setMainUser', user); this.$store.dispatch('setMainUser', user);
this.$store.commit('showUserOnCalendar', {user, ranges: true, remotes: true}); this.$store.commit('showUserOnCalendar', {user, ranges: true, remotes: true});
}, },

View File

@@ -202,6 +202,8 @@ export default {
return dispatch('associateCalendarToRange', { range: null }).then(() => { return dispatch('associateCalendarToRange', { range: null }).then(() => {
commit('setMainUser', mainUser); commit('setMainUser', mainUser);
return dispatch('fetchCalendarEvents');
}); });
}, },

View File

@@ -54,7 +54,7 @@ export const mapEntity = (entity: EventInput): EventInput => {
export const createUserData = (user: User, colorIndex: number): UserData => { export const createUserData = (user: User, colorIndex: number): UserData => {
const colorId = colorIndex % COLORS.length; const colorId = colorIndex % COLORS.length;
console.log('colorId', colorId);
return { return {
user: user, user: user,
calendarRanges: [], calendarRanges: [],

View File

@@ -9,13 +9,13 @@
{% block js %} {% block js %}
{{ parent() }} {{ parent() }}
{{ encore_entry_script_tags('mod_answer') }} {{ encore_entry_script_tags('mod_answer') }}
{{ encore_entry_script_tags('mod_async_upload') }} {{ encore_entry_script_tags('mod_document_action_buttons_group') }}
{% endblock %} {% endblock %}
{% block css %} {% block css %}
{{ parent() }} {{ parent() }}
{{ encore_entry_link_tags('mod_answer') }} {{ encore_entry_link_tags('mod_answer') }}
{{ encore_entry_link_tags('mod_async_upload') }} {{ encore_entry_link_tags('mod_document_action_buttons_group') }}
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View File

@@ -91,10 +91,10 @@ class Generator implements GeneratorInterface
$contextGenerationDataNormalized = array_merge( $contextGenerationDataNormalized = array_merge(
$contextGenerationDataNormalized, $contextGenerationDataNormalized,
['creator' => $creator], ['creator' => $creator],
$context instanceof DocGeneratorContextWithPublicFormInterface ? $context instanceof DocGeneratorContextWithPublicFormInterface ?
$context->contextGenerationDataDenormalize($template, $entity, $contextGenerationDataNormalized) $context->contextGenerationDataDenormalize($template, $entity, $contextGenerationDataNormalized)
: [] : []
); );
$data = $context->getData($template, $entity, $contextGenerationDataNormalized); $data = $context->getData($template, $entity, $contextGenerationDataNormalized);

View File

@@ -27,7 +27,7 @@ class RequestGenerationMessage
private array $contextGenerationData; private array $contextGenerationData;
private \DateTimeImmutable $createdAt; private \DateTimeImmutable $createdAt;
public function __construct( public function __construct(
User $creator, User $creator,

View File

@@ -64,7 +64,6 @@ class SocialWorkTypeFilter implements FilterInterface
$orX = $qb->expr()->orX(); $orX = $qb->expr()->orX();
foreach ($data['goal'] as $goal) { foreach ($data['goal'] as $goal) {
/** @var Goal $goal */ /** @var Goal $goal */
$andX = $qb->expr()->andX(); $andX = $qb->expr()->andX();
$andX->add($qb->expr()->eq('acpw_goal.goal', $goalId = ':goal_'.uniqid())); $andX->add($qb->expr()->eq('acpw_goal.goal', $goalId = ':goal_'.uniqid()));
@@ -73,7 +72,6 @@ class SocialWorkTypeFilter implements FilterInterface
if (count($data['result']) > 0) { if (count($data['result']) > 0) {
$orXResult = $qb->expr()->orX(); $orXResult = $qb->expr()->orX();
foreach ($data['result'] as $result) { foreach ($data['result'] as $result) {
/** @var Result $result */ /** @var Result $result */
$orXResult->add($qb->expr()->isMemberOf($resultId = ':result_'.uniqid(), 'acpw_goal.results')); $orXResult->add($qb->expr()->isMemberOf($resultId = ':result_'.uniqid(), 'acpw_goal.results'));
$qb->setParameter($resultId, $result); $qb->setParameter($resultId, $result);

View File

@@ -80,7 +80,7 @@ class SimilarPersonMatcher
->where('SIMILARITY(p.fullnameCanonical, UNACCENT(LOWER(:fullName))) >= :precision') ->where('SIMILARITY(p.fullnameCanonical, UNACCENT(LOWER(:fullName))) >= :precision')
->andWhere($qb->expr()->in('center_history.center', ':centers')) ->andWhere($qb->expr()->in('center_history.center', ':centers'))
->andWhere($qb->expr()->andX( ->andWhere($qb->expr()->andX(
$qb->expr()->lte('center_history.startDate', 'CURRENT_DATE()'), $qb->expr()->lte('center_history.startDate', 'CURRENT_DATE()'),
$qb->expr()->orX( $qb->expr()->orX(
$qb->expr()->isNull('center_history.endDate'), $qb->expr()->isNull('center_history.endDate'),
$qb->expr()->gt('center_history.endDate', 'CURRENT_DATE()') $qb->expr()->gt('center_history.endDate', 'CURRENT_DATE()')

View File

@@ -2,6 +2,13 @@
declare(strict_types=1); 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\Migrations\Person; namespace Chill\Migrations\Person;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;