Retrait.php 2.98 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
<?php

namespace App\Entity;

use App\Enum\CurrencyEnum;
use App\Enum\MoyenEnum;
use App\Utils\OperationFactory;
use Doctrine\ORM\Mapping as ORM;

/**
 * Retrait => (Retrait de monnaie papier en échange de monnaie numérique).
 *
 * Types de retraits :
 *
 *   - ADHERENTS        =>    COMPTOIR          (Retrait d'un adherent)
 *   - PRESTATAIRES     =>    COMPTOIR          (Retrait d'un prestataire)
 *
 * @ORM\Entity
 */
class Retrait extends Flux
{
    const TYPE_RETRAIT_ADHERENT = 'retrait_adherent';
    const TYPE_RETRAIT_PRESTATAIRE = 'retrait_prestataire';

    /**
     * @ORM\OneToOne(targetEntity="Comptoir")
     * @ORM\JoinColumn(name="comptoir_id", referencedColumnName="id")
     */
    protected $expediteur;

    public static function getAvailableTypes(): array
    {
        return [
            self::TYPE_RETRAIT_ADHERENT => self::TYPE_RETRAIT_ADHERENT,
            self::TYPE_RETRAIT_PRESTATAIRE => self::TYPE_RETRAIT_PRESTATAIRE,
        ];
    }

    /**
     * @return string
     */
    public function getParenttype(): string
    {
        return parent::TYPE_RETRAIT;
    }

    public function getAllOperations($em)
    {
        if (MoyenEnum::MOYEN_MLC == $this->getMoyen()) {
            return [
                OperationFactory::getOperation($this, $this->getExpediteur(), CurrencyEnum::CURRENCY_MLC, -$this->getMontant()),
                OperationFactory::getOperation($this, $this->getDestinataire(), CurrencyEnum::CURRENCY_EMLC, -$this->getMontant()),
                OperationFactory::getOperation($this, $this->getExpediteur()->getGroupe()->getSiege(), CurrencyEnum::CURRENCY_EMLC, -$this->getMontant()),
                OperationFactory::getOperation($this, $this->getExpediteur()->getGroupe()->getSiege(), CurrencyEnum::CURRENCY_MLC_NANTIE, $this->getMontant()),
            ];
        }

        return [];
    }

    public function operate($em)
    {
        $compteExp = $this->getExpediteur()->getCompte() - $this->getMontant();
        $compteDest = $this->getDestinataire()->getEcompte() - $this->getMontant();
        if ($compteExp < 0) {
            throw new \Exception('[FLUX] Retrait impossible ! Montant supérieur au solde du comptoir !');
        } elseif ($compteDest < 0) {
            throw new \Exception('[FLUX] Retrait impossible ! Montant supérieur au solde du demandeur !');
        } else {
            $this->getExpediteur()->removeCompte($this->getMontant());
            $this->getExpediteur()->getGroupe()->getSiege()->addCompteNantie($this->getMontant());
            $this->getExpediteur()->getGroupe()->getSiege()->removeEcompteNantie($this->getMontant());
            $this->getDestinataire()->removeEcompte($this->getMontant());

            return [$this->getExpediteur(), $this->getDestinataire(), $this->getExpediteur()->getGroupe()->getSiege()];
        }

        return [];
    }

    /**
     * @return string
     */
    public function getType(): string
    {
        return '';
    }

    public function getUsersToNotify()
    {
        return [];
    }
}