CreateAccountsCommand.php 7.36 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
<?php

declare(strict_types=1);

namespace App\Command;

use App\Entity\AccountAdherent;
use App\Entity\AccountComptoir;
use App\Entity\AccountGroupe;
use App\Entity\AccountPrestataire;
use App\Entity\AccountSiege;
use App\Entity\Comptoir;
use App\Entity\Groupe;
use App\Entity\Prestataire;
use App\Entity\Siege;
use App\Entity\User;
use App\Enum\CurrencyEnum;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 */
class CreateAccountsCommand extends Command
{
    protected static $defaultName = 'kohinos:accounts:create';

    protected $em;
    protected $io;

    public function __construct(
        EntityManagerInterface $em
    ) {
        $this->em = $em;
        parent::__construct();
    }

    protected function configure()
    {
        $this
            ->setDescription('Create accounts for all entities (siege, groupe, comptoir, adherent, prestataire)')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->io = new SymfonyStyle($input, $output);
        $this->io->title('--- CREATION DES COMPTES ---');
        $this->updateAllAccount();
        $this->io->success('--- FIN DE LA CREATION DES COMPTES ---');

        return 0;
    }

    private function updateAllAccount()
    {
        $users = $this->em->getRepository(User::class)->findAll();
        $countAccountCreated = 0;
        foreach ($users as $entity) {
            // Create all accounts link to this entity if not exist !
            if (!empty($entity->getAdherent())) {
                if (null == $entity->getAdherent()->getAccountWithCurrency(CurrencyEnum::CURRENCY_EMLC)) {
                    $account = new AccountAdherent();
                    $account
                        ->setBalance($entity->getAdherent()->getEcompte())
                        ->setCurrency(CurrencyEnum::CURRENCY_EMLC)
                    ;
                    $entity->getAdherent()->addAccount($account);
                    $this->em->persist($account);
                    $this->em->persist($entity->getAdherent());
                    ++$countAccountCreated;
                }
            }
        }
        $this->io->success('Nb de comptes adherent crée : ' . $countAccountCreated . ' !');
        $countAccountCreated = 0;
        $this->em->flush();
        $prestas = $this->em->getRepository(Prestataire::class)->findAll();
        foreach ($prestas as $presta) {
            if (null == $presta->getAccountWithCurrency(CurrencyEnum::CURRENCY_EMLC)) {
                $account = new AccountPrestataire();
                $account
                    ->setBalance($presta->getEcompte())
                    ->setCurrency(CurrencyEnum::CURRENCY_EMLC)
                ;
                $presta->addAccount($account);
                $this->em->persist($account);
                ++$countAccountCreated;
            }
            if ($presta->isMlc() && null == $presta->getAccountWithCurrency(CurrencyEnum::CURRENCY_EURO)) {
                // Ajout du compte de fonctionnement euro pour le prestataire MLC
                $accountEuro = new AccountPrestataire();
                $accountEuro
                        ->setCurrency(CurrencyEnum::CURRENCY_EURO)
                    ;
                $presta->addAccount($accountEuro);
                $this->em->persist($accountEuro);
                ++$countAccountCreated;
            }
            $this->em->persist($presta);
        }
        $this->io->success('Nb de comptes prestataire crée : ' . $countAccountCreated . ' !');
        $countAccountCreated = 0;
        $this->em->flush();
        $comptoirs = $this->em->getRepository(Comptoir::class)->findAll();
        foreach ($comptoirs as $comptoir) {
            if (null == $comptoir->getAccountWithCurrency(CurrencyEnum::CURRENCY_MLC)) {
                $account = new AccountComptoir();
                $account
                    ->setBalance($comptoir->getCompte())
                    ->setCurrency(CurrencyEnum::CURRENCY_MLC)
                ;
                $comptoir->addAccount($account);
                $this->em->persist($account);
                $this->em->persist($comptoir);
                ++$countAccountCreated;
            }
        }
        $this->io->success('Nb de comptes comptoir créés : ' . $countAccountCreated . ' !');
        $countAccountCreated = 0;
        $this->em->flush();
        $groupes = $this->em->getRepository(Groupe::class)->findAll();
        foreach ($groupes as $groupe) {
            if (null == $groupe->getAccountWithCurrency(CurrencyEnum::CURRENCY_MLC)) {
                $account = new AccountGroupe();
                $account
                    ->setBalance($groupe->getCompte())
                    ->setCurrency(CurrencyEnum::CURRENCY_MLC)
                ;
                $groupe->addAccount($account);
                $this->em->persist($account);
                $this->em->persist($groupe);
                ++$countAccountCreated;
            }
        }
        $this->io->success('Nb de comptes groupe créé : ' . $countAccountCreated . ' !');
        $this->em->flush();
        $siege = $this->em->getRepository(Siege::class)->getTheOne();

        if (null == $siege->getAccountWithCurrency(CurrencyEnum::CURRENCY_EMLC)) {
            $account = new AccountSiege();
            $account
                ->setBalance($siege->getEcompteNantie())
                ->setCurrency(CurrencyEnum::CURRENCY_EMLC) // compte de monnaie numérique nantie
            ;
            $siege->addAccount($account);
            $this->em->persist($account);
            $this->io->success('Compte emlc  du siege créé !');
        }
        if (null == $siege->getAccountWithCurrency(CurrencyEnum::CURRENCY_MLC)) {
            $accountMLC = new AccountSiege();
            $accountMLC
                ->setBalance($siege->getCompte())
                ->setCurrency(CurrencyEnum::CURRENCY_MLC) // compte de monnaie papier au siege non nantie
            ;
            $siege->addAccount($accountMLC);
            $this->em->persist($accountMLC);
            $this->io->success('Compte mlc papier du siege créé !');
        }
        // if ($siege->getAccountWithCurrency(CurrencyEnum::CURRENCY_EURO) == null) {
        //     $accountEuro = new AccountSiege();
        //     $accountEuro
        //         ->setCurrency(CurrencyEnum::CURRENCY_EURO) // compte réel de nantissement // @TODO : offrir la possibilité de voir les opérations du compte euro du fond de garanti ? Ou plutôt de suivre les euros à récupérés aux comptoirs
        //     ;
        //     $siege->addAccount($accountEuro);
        //     $this->em->persist($accountEuro);
        // }
        if (null == $siege->getAccountWithCurrency(CurrencyEnum::CURRENCY_MLC_NANTIE)) {
            $accountMlcNantie = new AccountSiege();
            $accountMlcNantie
                ->setBalance($siege->getCompteNantie())
                ->setCurrency(CurrencyEnum::CURRENCY_MLC_NANTIE) // compte de monnaie papier nantie
            ;
            $siege->addAccount($accountMlcNantie);
            $this->em->persist($accountMlcNantie);
            $this->em->persist($siege);
            $this->io->success('Compte mlc nantie du siege créé !');
        }
        $this->em->flush();
    }
}