<?php
namespace App\Entity;
use App\Repository\PieceRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @ORM\Entity(repositoryClass=PieceRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class Piece
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="date", nullable=true)
* @Assert\NotBlank(message="La date est obligatoire")
*/
private $date;
/**
* @ORM\Column(type="integer", nullable=true)
* @Assert\NotBlank(message="Le code est obligatoire")
*/
private $numero;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $type;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
private $amountHT;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
private $amountTVA;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $discount;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
private $amountDiscount;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
private $amountTTC;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity=Client::class, inversedBy="pieces")
* @Assert\NotBlank(message="Le client est obligatoire")
*/
private $client;
/**
* @ORM\OneToMany(targetEntity=PieceLine::class, mappedBy="piece", orphanRemoval=true, cascade={"persist"})
*/
private $pieceLines;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $payer;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
private $amountBuyHT;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
private $amountMarge;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
private $solde;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="pieces")
* @ORM\JoinColumn(nullable=true)
*/
private $user;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $modePaiement;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
private $amountDiscountTotal;
/**
* @ORM\OneToMany(targetEntity=Affectation::class, mappedBy="piece", orphanRemoval=true)
*/
private $affectations;
public function liste_type_piece(){
return [
'devis'=>'Devis',
'facture'=>'Facture',
'avoir'=>'Avoir',
];
}
public function liste_mode_paiement(){
return [
'comptant'=>'Comptant',
'carte'=>'Carte',
'virement'=>'Virement',
];
}
public function __construct()
{
$this->pieceLines = new ArrayCollection();
$this->affectations = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(?\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getNumero(): ?int
{
return $this->numero;
}
public function setNumero(?int $numero): self
{
$this->numero = $numero;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getAmountHT(): ?string
{
return $this->amountHT;
}
public function setAmountHT(string $amountHT): self
{
$this->amountHT = $amountHT;
return $this;
}
public function getAmountTVA(): ?string
{
return $this->amountTVA;
}
public function setAmountTVA(?string $amountTVA): self
{
$this->amountTVA = $amountTVA;
return $this;
}
public function getDiscount(): ?float
{
return $this->discount;
}
public function setDiscount(?float $discount): self
{
$this->discount = $discount;
return $this;
}
public function getAmountDiscount(): ?string
{
return $this->amountDiscount;
}
public function setAmountDiscount(?string $amountDiscount): self
{
$this->amountDiscount = $amountDiscount;
return $this;
}
public function getAmountTTC(): ?string
{
return $this->amountTTC;
}
public function setAmountTTC(?string $amountTTC): self
{
$this->amountTTC = $amountTTC;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
$this->client = $client;
return $this;
}
/**
* @return Collection|PieceLine[]
*/
public function getPieceLines(): Collection
{
return $this->pieceLines;
}
public function addPieceLine(PieceLine $pieceLine): self
{
if (!$this->pieceLines->contains($pieceLine)) {
$this->pieceLines[] = $pieceLine;
$pieceLine->setPiece($this);
}
return $this;
}
public function removePieceLine(PieceLine $pieceLine): self
{
if ($this->pieceLines->contains($pieceLine)) {
$this->pieceLines->removeElement($pieceLine);
// set the owning side to null (unless already changed)
if ($pieceLine->getPiece() === $this) {
$pieceLine->setPiece(null);
}
}
return $this;
}
public function getPayer(): ?bool
{
return $this->payer;
}
public function setPayer(?bool $payer): self
{
$this->payer = $payer;
return $this;
}
public function getAmountBuyHT(): ?string
{
return $this->amountBuyHT;
}
public function setAmountBuyHT(?string $amountBuyHT): self
{
$this->amountBuyHT = $amountBuyHT;
return $this;
}
public function getAmountMarge(): ?string
{
return $this->amountMarge;
}
public function setAmountMarge(?string $amountMarge): self
{
$this->amountMarge = $amountMarge;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getModePaiement(): ?string
{
return $this->modePaiement;
}
public function setModePaiement(?string $modePaiement): self
{
$this->modePaiement = $modePaiement;
return $this;
}
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context, $payload)
{
// check if the name is actually a fake name
if ($this->getPayer() && $this->getModePaiement()=='') {
$context->buildViolation('Il faut choisir au mode de paiement')
->atPath('modePaiement')
->addViolation();
}
}
public function getAmountDiscountTotal(): ?string
{
return $this->amountDiscountTotal;
}
public function setAmountDiscountTotal(?string $amountDiscountTotal): self
{
$this->amountDiscountTotal = $amountDiscountTotal;
return $this;
}
public function isPayer(): ?bool
{
return $this->payer;
}
/**
* @return Collection<int, Affectaion>
*/
public function getAffectations(): Collection
{
return $this->affectations;
}
public function addAffectation(Affectation $affectation): self
{
if (!$this->affectations->contains($affectation)) {
$this->affectations->add($affectation);
$affectation->setPiece($this);
}
return $this;
}
public function removeAffectation(Affectation $affectation): self
{
if ($this->affectations->removeElement($affectation)) {
// set the owning side to null (unless already changed)
if ($affectation->getPiece() === $this) {
$affectation->setPiece(null);
}
}
return $this;
}
public function getPoints($percent = null){
$points = 0;
$gift = $this->getClient()->getGiftConfigPerPiece($this);
if(is_null($gift)){
return 0;
}
if($this->type === 'devis'){
return 0;
}
if(is_null($percent)){
$percent = $gift->getPercent();
}
foreach($this->getPieceLines() as $line){
if($line->getRabais() == 0 or is_null($line->getRabais())){
$points += round($line->getTotalHT() * $percent /100,2);
}
}
if($this->getType() == 'avoir'){
return -$points;
}
return $points;
}
public function getSolde(): ?string
{
return $this->solde;
}
public function setSolde(?string $solde): self
{
$this->solde = $solde;
return $this;
}
/**
* @ORM\PrePersist
*/
public function createDate()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
if($this->getModePaiement() == '' or is_null($this->getModePaiement())){
$this->solde = $this->getAmountTTC();
}else{
$this->solde = 0;
}
}
/**
* @ORM\PreUpdate
*/
public function updateDate()
{
$this->updatedAt = new \DateTime();
$p = 0;
foreach($this->getAffectations() as $affectation){
$p += $affectation->getAmount();
}
$this->solde = $this->getAmountTTC() - $p;
}
}