src/Entity/Piece.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\PieceRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  10. /**
  11.  * @ORM\Entity(repositoryClass=PieceRepository::class)
  12.  * @ORM\HasLifecycleCallbacks()
  13.  */
  14. class Piece
  15. {
  16.     /**
  17.      * @ORM\Id
  18.      * @ORM\GeneratedValue
  19.      * @ORM\Column(type="integer")
  20.      */
  21.     private $id;
  22.     /**
  23.      * @ORM\Column(type="date", nullable=true)
  24.      * @Assert\NotBlank(message="La date est obligatoire")
  25.      */
  26.     private $date;
  27.     /**
  28.      * @ORM\Column(type="integer", nullable=true)
  29.      * @Assert\NotBlank(message="Le code est obligatoire")
  30.      */
  31.     private $numero;
  32.     /**
  33.      * @ORM\Column(type="string", length=255, nullable=true)
  34.      */
  35.     private $type;
  36.     /**
  37.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  38.      */
  39.     private $amountHT;
  40.     /**
  41.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  42.      */
  43.     private $amountTVA;
  44.     /**
  45.      * @ORM\Column(type="float", nullable=true)
  46.      */
  47.     private $discount;
  48.     /**
  49.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  50.      */
  51.     private $amountDiscount;
  52.     /**
  53.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  54.      */
  55.     private $amountTTC;
  56.     /**
  57.      * @ORM\Column(type="datetime", nullable=true)
  58.      */
  59.     private $createdAt;
  60.     /**
  61.      * @ORM\Column(type="datetime", nullable=true)
  62.      */
  63.     private $updatedAt;
  64.     /**
  65.      * @ORM\ManyToOne(targetEntity=Client::class, inversedBy="pieces")
  66.      * @Assert\NotBlank(message="Le client est obligatoire")
  67.      */
  68.     private $client;
  69.     /**
  70.      * @ORM\OneToMany(targetEntity=PieceLine::class, mappedBy="piece", orphanRemoval=true, cascade={"persist"})
  71.      */
  72.     private $pieceLines;
  73.     /**
  74.      * @ORM\Column(type="boolean", nullable=true)
  75.      */
  76.     private $payer;
  77.     /**
  78.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  79.      */
  80.     private $amountBuyHT;
  81.     /**
  82.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  83.      */
  84.     private $amountMarge;
  85.     /**
  86.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  87.      */
  88.     private $solde;
  89.     /**
  90.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="pieces")
  91.      * @ORM\JoinColumn(nullable=true)
  92.      */
  93.     private $user;
  94.     /**
  95.      * @ORM\Column(type="string", length=255, nullable=true)
  96.      */
  97.     private $modePaiement;
  98.     /**
  99.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  100.      */
  101.     private $amountDiscountTotal;
  102.     /**
  103.      * @ORM\OneToMany(targetEntity=Affectation::class, mappedBy="piece", orphanRemoval=true)
  104.      */
  105.     private $affectations;
  106.     public function liste_type_piece(){
  107.         return [
  108.             'devis'=>'Devis',
  109.             'facture'=>'Facture',
  110.             'avoir'=>'Avoir',
  111.         ];
  112.     }
  113.     public function liste_mode_paiement(){
  114.         return [
  115.             'comptant'=>'Comptant',
  116.             'carte'=>'Carte',
  117.             'virement'=>'Virement',
  118.             'twint'=>'Twint',
  119.         ];
  120.     }
  121.     public function __construct()
  122.     {
  123.         $this->pieceLines = new ArrayCollection();
  124.         $this->affectations = new ArrayCollection();
  125.     }
  126.     public function getId(): ?int
  127.     {
  128.         return $this->id;
  129.     }
  130.     public function getDate(): ?\DateTimeInterface
  131.     {
  132.         return $this->date;
  133.     }
  134.     public function setDate(?\DateTimeInterface $date): self
  135.     {
  136.         $this->date $date;
  137.         return $this;
  138.     }
  139.     public function getNumero(): ?int
  140.     {
  141.         return $this->numero;
  142.     }
  143.     public function setNumero(?int $numero): self
  144.     {
  145.         $this->numero $numero;
  146.         return $this;
  147.     }
  148.     public function getType(): ?string
  149.     {
  150.         return $this->type;
  151.     }
  152.     public function setType(?string $type): self
  153.     {
  154.         $this->type $type;
  155.         return $this;
  156.     }
  157.     public function getAmountHT(): ?string
  158.     {
  159.         return $this->amountHT;
  160.     }
  161.     public function setAmountHT(string $amountHT): self
  162.     {
  163.         $this->amountHT $amountHT;
  164.         return $this;
  165.     }
  166.     public function getAmountTVA(): ?string
  167.     {
  168.         return $this->amountTVA;
  169.     }
  170.     public function setAmountTVA(?string $amountTVA): self
  171.     {
  172.         $this->amountTVA $amountTVA;
  173.         return $this;
  174.     }
  175.     public function getDiscount(): ?float
  176.     {
  177.         return $this->discount;
  178.     }
  179.     public function setDiscount(?float $discount): self
  180.     {
  181.         $this->discount $discount;
  182.         return $this;
  183.     }
  184.     public function getAmountDiscount(): ?string
  185.     {
  186.         return $this->amountDiscount;
  187.     }
  188.     public function setAmountDiscount(?string $amountDiscount): self
  189.     {
  190.         $this->amountDiscount $amountDiscount;
  191.         return $this;
  192.     }
  193.     public function getAmountTTC(): ?string
  194.     {
  195.         return $this->amountTTC;
  196.     }
  197.     public function setAmountTTC(?string $amountTTC): self
  198.     {
  199.         $this->amountTTC $amountTTC;
  200.         return $this;
  201.     }
  202.     public function getCreatedAt(): ?\DateTimeInterface
  203.     {
  204.         return $this->createdAt;
  205.     }
  206.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  207.     {
  208.         $this->createdAt $createdAt;
  209.         return $this;
  210.     }
  211.     public function getUpdatedAt(): ?\DateTimeInterface
  212.     {
  213.         return $this->updatedAt;
  214.     }
  215.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  216.     {
  217.         $this->updatedAt $updatedAt;
  218.         return $this;
  219.     }
  220.     public function getClient(): ?Client
  221.     {
  222.         return $this->client;
  223.     }
  224.     public function setClient(?Client $client): self
  225.     {
  226.         $this->client $client;
  227.         return $this;
  228.     }
  229.     /**
  230.      * @return Collection|PieceLine[]
  231.      */
  232.     public function getPieceLines(): Collection
  233.     {
  234.         return $this->pieceLines;
  235.     }
  236.     public function addPieceLine(PieceLine $pieceLine): self
  237.     {
  238.         if (!$this->pieceLines->contains($pieceLine)) {
  239.             $this->pieceLines[] = $pieceLine;
  240.             $pieceLine->setPiece($this);
  241.         }
  242.         return $this;
  243.     }
  244.     public function removePieceLine(PieceLine $pieceLine): self
  245.     {
  246.         if ($this->pieceLines->contains($pieceLine)) {
  247.             $this->pieceLines->removeElement($pieceLine);
  248.             // set the owning side to null (unless already changed)
  249.             if ($pieceLine->getPiece() === $this) {
  250.                 $pieceLine->setPiece(null);
  251.             }
  252.         }
  253.         return $this;
  254.     }
  255.     public function getPayer(): ?bool
  256.     {
  257.         return $this->payer;
  258.     }
  259.     public function setPayer(?bool $payer): self
  260.     {
  261.         $this->payer $payer;
  262.         return $this;
  263.     }
  264.     public function getAmountBuyHT(): ?string
  265.     {
  266.         return $this->amountBuyHT;
  267.     }
  268.     public function setAmountBuyHT(?string $amountBuyHT): self
  269.     {
  270.         $this->amountBuyHT $amountBuyHT;
  271.         return $this;
  272.     }
  273.     public function getAmountMarge(): ?string
  274.     {
  275.         return $this->amountMarge;
  276.     }
  277.     public function setAmountMarge(?string $amountMarge): self
  278.     {
  279.         $this->amountMarge $amountMarge;
  280.         return $this;
  281.     }
  282.     public function getUser(): ?User
  283.     {
  284.         return $this->user;
  285.     }
  286.     public function setUser(?User $user): self
  287.     {
  288.         $this->user $user;
  289.         return $this;
  290.     }
  291.     public function getModePaiement(): ?string
  292.     {
  293.         return $this->modePaiement;
  294.     }
  295.     public function setModePaiement(?string $modePaiement): self
  296.     {
  297.         $this->modePaiement $modePaiement;
  298.         return $this;
  299.     }
  300.     /**
  301.      * @Assert\Callback
  302.      */
  303.     public function validate(ExecutionContextInterface $context$payload)
  304.     {
  305.         // check if the name is actually a fake name
  306.         if ($this->getPayer() && $this->getModePaiement()=='') {
  307.             $context->buildViolation('Il faut choisir au mode de paiement')
  308.                 ->atPath('modePaiement')
  309.                 ->addViolation();
  310.         }
  311.     }
  312.     public function getAmountDiscountTotal(): ?string
  313.     {
  314.         return $this->amountDiscountTotal;
  315.     }
  316.     public function setAmountDiscountTotal(?string $amountDiscountTotal): self
  317.     {
  318.         $this->amountDiscountTotal $amountDiscountTotal;
  319.         return $this;
  320.     }
  321.     public function isPayer(): ?bool
  322.     {
  323.         return $this->payer;
  324.     }
  325.     /**
  326.      * @return Collection<int, Affectaion>
  327.      */
  328.     public function getAffectations(): Collection
  329.     {
  330.         return $this->affectations;
  331.     }
  332.     public function addAffectation(Affectation $affectation): self
  333.     {
  334.         if (!$this->affectations->contains($affectation)) {
  335.             $this->affectations->add($affectation);
  336.             $affectation->setPiece($this);
  337.         }
  338.         return $this;
  339.     }
  340.     public function removeAffectation(Affectation $affectation): self
  341.     {
  342.         if ($this->affectations->removeElement($affectation)) {
  343.             // set the owning side to null (unless already changed)
  344.             if ($affectation->getPiece() === $this) {
  345.                 $affectation->setPiece(null);
  346.             }
  347.         }
  348.         return $this;
  349.     }
  350.     public function getPoints($percent null){
  351.         $points 0;
  352.         $gift $this->getClient()->getGiftConfigPerPiece($this);
  353.         if(is_null($gift)){
  354.             return 0;
  355.         }
  356.         if($this->type === 'devis'){
  357.             return 0;
  358.         }
  359.         if(is_null($percent)){
  360.             $percent $gift->getPercent();
  361.         }
  362.         foreach($this->getPieceLines() as $line){
  363.             if($line->getRabais() == or is_null($line->getRabais())){
  364.                 $points += round($line->getTotalHT() * $percent /100,2);
  365.             }
  366.         }
  367.         if($this->getType() == 'avoir'){
  368.             return -$points;
  369.         }
  370.         return $points;
  371.     }
  372.     public function getSolde(): ?string
  373.     {
  374.         return $this->solde;
  375.     }
  376.     public function setSolde(?string $solde): self
  377.     {
  378.         $this->solde $solde;
  379.         return $this;
  380.     }
  381.     /**
  382.      * @ORM\PrePersist
  383.      */
  384.     public function createDate()
  385.     {
  386.         $this->createdAt = new \DateTime();
  387.         $this->updatedAt = new \DateTime();
  388.         if($this->getModePaiement() == '' or is_null($this->getModePaiement())){
  389.             $this->solde $this->getAmountTTC();
  390.         }else{
  391.             $this->solde 0;
  392.         }
  393.     }
  394.     /**
  395.      * @ORM\PreUpdate
  396.      */
  397.     public function updateDate()
  398.     {
  399.         $this->updatedAt = new \DateTime();
  400.         $p 0;
  401.         foreach($this->getAffectations() as $affectation){
  402.             $p += $affectation->getAmount();
  403.         }
  404.         $this->solde $this->getAmountTTC() - $p;
  405.     }
  406. }