mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
DX: [budget calculator] restore previous budget's element type to Charge/Resource kind
+ improve typing
This commit is contained in:
parent
0133e202d2
commit
561d069a3e
@ -11,7 +11,6 @@ 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\Charge;
|
||||||
use Chill\BudgetBundle\Entity\Resource;
|
use Chill\BudgetBundle\Entity\Resource;
|
||||||
|
|
||||||
|
@ -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 "
|
||||||
|
@ -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 = ''");
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
Subproject commit 5e478fdfbf429baf3ce852ae69eb1f7101b1b416
|
Subproject commit 5b35e7ccd0735e5593835e28acbf82386c18e1b6
|
Loading…
x
Reference in New Issue
Block a user