AchatMonnaie.php 2.47 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
<?php

namespace App\Entity;

use App\Enum\CurrencyEnum;
use App\Utils\OperationFactory;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Achat de monnaie en CB par un Adhérent ou Prestataire.
 *
 * @ORM\Entity
 */
class AchatMonnaie extends Flux
{
    const TYPE_ACHAT_ADHERENT = 'achat_monnaie_adherent';
    const TYPE_ACHAT_PRESTATAIRE = 'achat_monnaie_prestataire';

    /**
     * @ORM\ManyToOne(targetEntity="Siege")
     * @ORM\JoinColumn(name="siege_id", referencedColumnName="id", nullable=true)
     */
    protected $expediteur;

    /**
     * @var bool RECONVERTI => Validation du dépôt d'espèce/chèque/virement
     * @Assert\Type("bool")
     * @ORM\Column(type="boolean", nullable=false, options={"default": false})
     * @Groups({"read", "write"})
     */
    protected $reconverti = false;

35 36
    protected $don;

Julien Jorry committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    public function __construct()
    {
        parent::__construct();
        $this->reconverti = false;
    }

    public function getReconverti(): bool
    {
        return $this->reconverti;
    }

    public function setReconverti(bool $reconverti)
    {
        $this->reconverti = $reconverti;

        return $this;
    }

    public function getAllOperations($em)
    {
57
        $achatOperations = [
Julien Jorry committed
58 59 60
            OperationFactory::getOperation($this, $this->getExpediteur(), CurrencyEnum::CURRENCY_EMLC, $this->getMontant()),
            OperationFactory::getOperation($this, $this->getDestinataire(), CurrencyEnum::CURRENCY_EMLC, $this->getMontant()),
        ];
61
        if (null != $this->getDon()) {
62
            return array_merge($achatOperations, $this->getDon()->getAllOperations($em));
63 64 65
        }

        return $achatOperations;
Julien Jorry committed
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
    }

    public function operate($em)
    {
        if ($this->getReconverti()) {
            // Increment Ecompte of Siege
            $this->getExpediteur()->addEcompteNantie($this->getMontant());
            $this->getDestinataire()->addEcompte($this->getMontant());

            return [$this->getExpediteur(), $this->getDestinataire()];
        } else {
            //DO NOTHING IF NOT VALIDATE !
            return [];
        }
    }

    public function getUsersToNotify()
    {
        return [];
    }

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

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