Flux.php 8.32 KB
Newer Older
1 2 3 4 5
<?php

namespace App\Entity;

use App\Entity\EntityTrait\EnablableEntityTrait;
6
use App\Enum\MoyenEnum;
7
use Doctrine\ORM\Event\LifecycleEventArgs;
8 9 10 11 12
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Validator\Constraints as Assert;

/**
13
 * FLUX = TRANSFERT ou TRANSACTION ou COTISATIONS
14 15
 * @ORM\Entity(repositoryClass="App\Repository\FluxRepository")
 * @ORM\HasLifecycleCallbacks()
16 17
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
18
 * @ORM\DiscriminatorMap({"cotisation" = "Cotisation", "cotisation_adherent" = "CotisationAdherent", "cotisation_prestataire" = "CotisationPrestataire", "tro_adh_pre" = "TransactionAdherentPrestataire", "tro_adh_adh" = "TransactionAdherentAdherent", "tro_pre_adh" = "TransactionPrestataireAdherent", "tro_pre_pre" = "TransactionPrestatairePrestataire", "tre_cpt_adh" = "TransfertComptoirAdherent", "tre_cpt_grp" = "TransfertComptoirGroupe", "tre_cpt_pre" = "TransfertComptoirPrestataire", "tre_grp_cpt" = "TransfertGroupeComptoir", "tre_pre_cpt" = "TransfertPrestataireComptoir", "tre_pre_sie" = "TransfertPrestataireSiege", "tre_sie_grp" = "TransfertSiegeGroupe", "tre_grp_sie" = "TransfertGroupeSiege", "vte_cpt_pre" = "VenteComptoirPrestataire", "vte_cpt_adh"  = "VenteComptoirAdherent"})
19 20 21 22 23
 */
abstract class Flux
{
    use TimestampableEntity;

24 25 26 27
    const TYPE_TRANSFERT = 'transfert';
    const TYPE_TRANSACTION = 'transaction';
    const TYPE_VENTE = 'vente';

28 29 30 31 32 33 34 35 36 37
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
38
     * @ORM\ManyToOne(targetEntity="User", inversedBy="flux", cascade={"all"})
39 40 41 42 43
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
     */
    protected $operateur;

    /**
44
     * Type de transfert / transaction : exemple : Prestataire à Adhérent
45 46
     * @var string
     *
47
     * @ORM\Column(name="type", type="string", length=200)
48
     * @Assert\NotBlank
49 50 51
     */
    protected $type;

52 53 54 55 56
    /**
     * Type de flux : transfert / transaction
     * @var string
     *
     * @ORM\Column(name="parenttype", type="string", length=20)
57
     * @Assert\NotBlank
58 59 60
     */
    protected $parenttype;

61 62 63 64
    /**
     * @var float
     *
     * @ORM\Column(name="montant", type="decimal", precision=7, scale=2)
65 66 67 68
     * @Assert\Type("float")
     * @Assert\GreaterThan(
     *     value = 0
     * )
69 70 71
     */
    protected $montant;

72 73 74 75
    /**
     * @var string
     *
     * @ORM\Column(name="moyen", type="string", length=100)
76
     * @Assert\NotBlank
77 78 79
     */
    private $moyen;

80 81 82 83
    /**
     * @var null|string
     *
     * @ORM\Column(name="reference", type="string", length=255, nullable=true)
84
     * @Assert\NotBlank
85 86 87
     */
    protected $reference;

88 89
    protected $expediteur = null;
    protected $destinataire = null;
90
    protected $cotisationInfos = null;
91 92 93

    abstract public function getParenttype();

94 95 96
    public function __construct()
    {
        $this->parenttype = $this->getParenttype();
97
        $this->type = $this->getType();
98 99
    }

100
    public function getId(): ?int
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    {
        return $this->id;
    }

    /**
     * @param $parenttype
     * @return $this
     */
    public function setParenttype($parenttype)
    {
        $this->parenttype = $parenttype;
        return $this;
    }

    /**
     * @param User $destinataire
     * @return $this
     */
119
    public function setOperateur(?User $operateur)
120 121 122 123 124 125 126 127
    {
        $this->operateur = $operateur;
        return $this;
    }

    /**
     * @return User operateur
     */
128
    public function getOperateur(): ?User
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 185 186 187 188 189
    {
        return $this->operateur;
    }

    /**
     * @param $destinataire
     * @return $this
     */
    public function setDestinataire($destinataire)
    {
        $this->destinataire = $destinataire;
        return $this;
    }

    /**
     * @return destinataire
     */
    public function getDestinataire()
    {
        return $this->destinataire;
    }

    /**
     * @param $expediteur
     * @return $this
     */
    public function setExpediteur($expediteur)
    {
        $this->expediteur = $expediteur;
        return $this;
    }

    /**
     * @return expediteur
     */
    public function getExpediteur()
    {
        return $this->expediteur;
    }

    /**
     * @return string
     */
    public function getType(): string
    {
        return $this->type;
    }

    /**
     * @param string $type
     * @return Transaction
     */
    public function setType(string $type)
    {
        $this->type = $type;
        return $this;
    }

    /**
     * @return float
     */
190
    public function getMontant(): ?float
191 192 193
    {
        return $this->montant;
    }
194

195 196 197 198 199 200 201 202 203
    /**
     * @param float $montant
     * @return Transaction
     */
    public function setMontant(float $montant)
    {
        $this->montant = $montant;
        return $this;
    }
204

205 206 207
    /**
     * @return string
     */
208
    public function getReference(): ?string
209 210 211 212 213 214 215 216 217 218 219 220 221
    {
        return $this->reference;
    }

    /**
     * @param string $reference
     * @return Transaction
     */
    public function setReference(string $reference)
    {
        $this->reference = $reference;
        return $this;
    }
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

    /**
     * @return string
     */
    public function getMoyen(): ?string
    {
        return $this->moyen;
    }

    public function setMoyen($moyen)
    {
        if (!in_array($moyen, MoyenEnum::getAvailableTypes())) {
            throw new \InvalidArgumentException("Moyen de paiement invalide !");
        }
        $this->moyen = $moyen;

        return $this;
    }
240

241 242 243 244 245
    public function isVente()
    {
        return false;
    }

246 247 248 249 250 251 252
    /**
     * @ORM\PostPersist
     * @param LifecycleEventArgs $event
     */
    public function postPersist(LifecycleEventArgs $event)
    {
        $flux = $event->getEntity();
253 254 255 256 257 258
        if (empty($flux->getExpediteur())) {
            throw new \Exception("[FLUX] Opération impossible ! Pas d'expéditeur !");
        }
        if ($flux->getMontant() <= 0) {
            throw new \Exception("[FLUX] Opération impossible ! Montant inférieur ou égal à zéro !");
        }
Julien Jorry committed
259 260 261
        if ($flux->getExpediteur() == $flux->getDestinataire()) {
            throw new \Exception("[FLUX] Opération impossible ! Expediteur et destinataire sont les mêmes !");
        }
262
        $compteExp = $flux->getExpediteur()->getCompte() - $flux->getMontant();
263 264 265 266 267 268 269
        if ($flux->getParenttype() != 'cotisation' || ($flux->getParenttype() == 'cotisation' && $flux->getMoyen() == MoyenEnum::MOYEN_MLC)) {
            if ($compteExp < 0) {
                throw new \Exception("[FLUX] Opération impossible ! Montant supérieur au solde de l'expéditeur !");
            } else {
                $em = $event->getEntityManager();
                $flux->getExpediteur()->setCompte($compteExp);
                $em->persist($flux->getExpediteur());
270
                if ($flux->getParenttype() != 'vente') {
271 272 273 274
                    $compteDest = $flux->getDestinataire()->getCompte() + $flux->getMontant();
                    $flux->getDestinataire()->setCompte($compteDest);
                    $em->persist($flux->getDestinataire());
                }
275 276
                $em->flush();
            }
277 278
        }
    }
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

    public function toHtmlArray(): string
    {
        if (empty($this->getDestinataire()) || empty($this->getExpediteur()) || empty($this->getMontant())) {
            return "[FLUX] Visualisation impossible ! Destinataire / Expéditeur et/ou montant manquant(s) !";
        }
        $return = '<tr>';
        $return .= '<td>'.$this->getCreatedAt()->format('d/m/Y H:i').'</td>';
        $return .= '<td>'.ucwords($this->getParenttype()).'</td>';
        $return .= '<td>'.$this->getExpediteur().'</td>';
        $return .= '<td>'.$this->getDestinataire().'</td>';
        $return .= '<td>'.$this->getMontant().'&euro;</td>';
        $return .= '</tr>';
        return $return;
    }

    public function __toString(): string
    {
        if (empty($this->getDestinataire()) || empty($this->getExpediteur()) || empty($this->getMontant())) {
            return "[FLUX] Visualisation impossible ! Destinataire / Expéditeur et/ou montant manquant(s) !";
        }
300
        return ($this->getCreatedAt()?$this->getCreatedAt()->format('d/m/Y H:i').' | ':'').ucwords($this->getParenttype()).' : '.$this->getDestinataire().' => '.$this->getExpediteur().' ; '.$this->getMontant().'&euro;';
301
    }
302
}