<?php
namespace App\Entity;
use App\Constants\Purchase as PurchaseStatus;
use App\Repository\PurchaseRepository;
use App\Traits\DateTrait;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=PurchaseRepository::class)
*
* @Serializer\ExclusionPolicy("ALL")
*/
class Purchase
{
// Un nombre de points est attribué à chaque référence
public const POINT_WITH_REFERENCE = 'point_with_reference';
// On se référence au prix total payé par l’installateur
public const POINT_PRICE_PAID_TOTAL = 'point_price_paid_total';
// On se référence au prix unitaire par produit payé par l’installateur
public const POINT_PRICE_PAID_PER_UNIT = 'point_price_paid_per_unit';
// On se refere à la taille en m² (expert-fenetre.rehau) pour calculer les points multiplier par un coefficient
public const POINT_PAID_PER_COEFFICIENT = 'point_paid_per_coefficient';
public const STATUS_PENDING = PurchaseStatus::STATUS_PENDING;
public const STATUS_VALIDATED = PurchaseStatus::STATUS_VALIDATED;
public const STATUS_RETURNED = PurchaseStatus::STATUS_RETURNED;
public const STATUS_REJECTED = PurchaseStatus::STATUS_REJECTED;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private ?int $id = NULL;
/**
* Numéro de facture
*
* @ORM\Column(type="string", length=64)
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private $invoiceNumber;
/**
* Date de facture
*
* @ORM\Column(type="date", nullable=true)
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private $invoiceDate;
/**
* Status de la déclaration
*
* @ORM\Column(type="integer", options={"default": self::STATUS_PENDING})
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private $status = self::STATUS_PENDING;
/**
* Raison du statut
*
* @ORM\Column(type="string", length=512, nullable=true)
*/
private ?string $lastStatusReason = NULL;
/**
* Nom du distributeur
*
* @deprecated Relation sur {@see Distributor}
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $distributorName = NULL;
/**
* Code postal du distributeur
*
* @deprecated Relation sur {@see Distributor}
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $distributorPostalCode = NULL;
/**
* Ville du distributeur
*
* @deprecated Relation sur {@see Distributor}
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $distributorCity = NULL;
/**
* Pays du distributeur
*
* @deprecated Relation sur {@see Distributor}
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $distributorCountry = NULL;
/**
* Valeur de la déclaration
*
* @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
*
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $value = NULL;
/**
* Personne qui a validé la déclaration
*
* @ORM\ManyToOne(targetEntity=User::class,inversedBy="purchasesIHaveProcessed")
* @ORM\JoinColumn(onDelete="SET NULL")
*
* @Expose
* @Groups({"purchase"})
*/
private $validator;
/**
* Utilisateur qui a fait la déclaration
*
* @ORM\ManyToOne(targetEntity=User::class,inversedBy="purchases")
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private $user;
/**
* Distributeur rattaché à la déclaration
*
* @ORM\ManyToOne(targetEntity=Distributor::class, inversedBy="purchases")
*
* @Expose()
* @Groups({"export_purchase_declaration_datatable"})
*/
private $distributor;
/**
* @TODO Check que toujours utilse
*/
private $imageFile;
/**
* @ORM\OneToMany(
* targetEntity=PurchaseHistory::class,
* mappedBy="purchase",
* cascade={"persist", "remove"}
* )
*/
private $purchaseHistories;
/**
* @ORM\OneToMany(
* targetEntity=PurchaseFile::class,
* mappedBy="purchase",
* cascade={"remove", "persist"}
* )
*/
private $files;
/**
* @ORM\OneToMany(
* targetEntity=PurchaseProductItem::class,
* mappedBy="purchase",
* cascade={"remove", "persist"},
* orphanRemoval=true
* )
*
* @Expose()
* @Groups({"export_purchase_declaration_datatable"})
*/
private $items;
/**
* @ORM\OneToMany(targetEntity=PointTransaction::class, mappedBy="purchase")
*/
private Collection $pointTransactions;
/**
* Date de la validation
*
* @ORM\Column(type="datetime", nullable=true)
*/
private $validationDate;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $distributorAddress1 = NULL;
/**
* Prénom du client
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private ?string $customerFirstName = NULL;
/**
* Nom du client
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private ?string $customerLastName = NULL;
/**
* Adresse du client
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private ?string $customerAddress1 = NULL;
/**
* Ville du client
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private ?string $customerCity = NULL;
/**
* Code postal du client
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private ?string $customerPostcode = NULL;
/**
* Complément d'adresse du client
*
* @ORM\Column(type="text", nullable=true)
*
* @Expose
* @Groups({"purchase", "export_purchase_declaration_datatable"})
*/
private ?string $customerAddress2 = NULL;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $commercialName = NULL;
public function __construct()
{
$this->purchaseHistories = new ArrayCollection();
$this->files = new ArrayCollection();
$this->items = new ArrayCollection();
$this->pointTransactions = new ArrayCollection();
}
use DateTrait;
/*
* ============================================================================================
* =============================== FONCTIONS CUSTOM ===========================================
* ============================================================================================
*/
/**
* @return float|int|null
* @Serializer\VirtualProperty()
* @Serializer\SerializedName("get_sum_values")
*
* @Expose()
* @Groups({"purchase"})
*/
public function getSumValues()
{
$sum = 0;
/** @var PointTransaction $pointTransaction */
foreach ( $this->pointTransactions as $pointTransaction ) {
$sum += $pointTransaction->getValue();
}
return $sum;
}
/**
* @return float|int
* @deprecated
* $value returns points acquired including boosters
* This method returns points without booster effects
*/
public function getBulkValue()
{
$value = 0;
foreach ( $this->items as $item ) {
$value += $item->getProduct()->getValue() * $item->getQuantity();
}
return $value;
}
/**
* @return int|null
* @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
*/
public function getValue(): ?int
{
return $this->value;
}
/**
* @param int|null $value
*
* @return $this
* @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
*/
public function setValue( ?int $value ): self
{
$this->value = $value;
return $this;
}
/**
* @return mixed
*/
public function getImageFile()
{
return $this->imageFile;
}
public function getArrayCategoryValues()
{
$categoryValues = [];
/** @var PurchaseProductItem $item */
foreach ( $this->items as $item ) {
$product = $item->getProduct();
$productCV = $product->getArrayCategoryValues();
if ( $productCV !== NULL ) {
if ( $categoryValues === [] ) {
$categoryValues = $productCV;
} else {
$categoryValues = array_combine(
array_keys( $categoryValues ),
array_map( function ( $a, $b ) {
return $a + $b;
}, $categoryValues, $productCV )
);
}
}
}
return $categoryValues;
}
/*
* ============================================================================================
* ============================== FIN FONCTIONS CUSTOM ========================================
* ============================================================================================
*/
/**
* @param $imageFile
*
* @return $this
*/
public function setImageFile( $imageFile ): Purchase
{
$this->imageFile = $imageFile;
return $this;
}
/**
* @param array $files
*
* @return $this
*/
public function addFiles( array $files ): Purchase
{
$this->files = new ArrayCollection( array_merge( $this->files->toArray(), $files ) );
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getInvoiceNumber(): ?string
{
return $this->invoiceNumber;
}
public function setInvoiceNumber( string $invoiceNumber ): self
{
$this->invoiceNumber = $invoiceNumber;
return $this;
}
public function getInvoiceDate(): ?DateTimeInterface
{
return $this->invoiceDate;
}
public function setInvoiceDate( ?DateTimeInterface $invoiceDate ): self
{
$this->invoiceDate = $invoiceDate;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus( ?string $status ): self
{
$this->status = $status;
return $this;
}
public function getLastStatusReason(): ?string
{
return $this->lastStatusReason;
}
public function setLastStatusReason( ?string $lastStatusReason ): self
{
$this->lastStatusReason = $lastStatusReason;
return $this;
}
/**
* @return string|null
* @deprecated Relation sur {@see Distributor}
*/
public function getDistributorName(): ?string
{
return $this->distributorName;
}
/**
* @param string|null $distributorName
*
* @return $this
* @deprecated Relation sur {@see Distributor}
*/
public function setDistributorName( ?string $distributorName ): self
{
$this->distributorName = $distributorName;
return $this;
}
/**
* @return string|null
* @deprecated Relation sur {@see Distributor}
*/
public function getDistributorPostalCode(): ?string
{
return $this->distributorPostalCode;
}
/**
* @param string|null $distributorPostalCode
*
* @return $this
* @deprecated Relation sur {@see Distributor}
*/
public function setDistributorPostalCode( ?string $distributorPostalCode ): self
{
$this->distributorPostalCode = $distributorPostalCode;
return $this;
}
/**
* @return string|null
* @deprecated Relation sur {@see Distributor}
*/
public function getDistributorCity(): ?string
{
return $this->distributorCity;
}
/**
* @param string|null $distributorCity
*
* @return $this
* @deprecated Relation sur {@see Distributor}
*/
public function setDistributorCity( ?string $distributorCity ): self
{
$this->distributorCity = $distributorCity;
return $this;
}
/**
* @return string|null
* @deprecated Relation sur {@see Distributor}
*/
public function getDistributorCountry(): ?string
{
return $this->distributorCountry;
}
/**
* @param string|null $distributorCountry
*
* @return $this
* @deprecated Relation sur {@see Distributor}
*/
public function setDistributorCountry( ?string $distributorCountry ): self
{
$this->distributorCountry = $distributorCountry;
return $this;
}
public function getValidator(): ?User
{
return $this->validator;
}
public function setValidator( ?User $validator ): self
{
$this->validator = $validator;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser( ?User $user ): self
{
$this->user = $user;
return $this;
}
public function getDistributor(): ?Distributor
{
return $this->distributor;
}
public function setDistributor( ?Distributor $distributor ): self
{
$this->distributor = $distributor;
return $this;
}
/**
* @return Collection|PurchaseHistory[]
*/
public function getPurchaseHistories(): Collection
{
return $this->purchaseHistories;
}
public function addPurchaseHistory( PurchaseHistory $purchaseHistory ): self
{
if ( !$this->purchaseHistories->contains( $purchaseHistory ) ) {
$this->purchaseHistories[] = $purchaseHistory;
$purchaseHistory->setPurchase( $this );
}
return $this;
}
public function removePurchaseHistory( PurchaseHistory $purchaseHistory ): self
{
if ( $this->purchaseHistories->removeElement( $purchaseHistory ) ) {
// set the owning side to null (unless already changed)
if ( $purchaseHistory->getPurchase() === $this ) {
$purchaseHistory->setPurchase( NULL );
}
}
return $this;
}
/**
* @return Collection|PurchaseFile[]
*/
public function getFiles(): Collection
{
return $this->files;
}
public function addFile( PurchaseFile $file ): self
{
if ( !$this->files->contains( $file ) ) {
$this->files[] = $file;
$file->setPurchase( $this );
}
return $this;
}
public function removeFile( PurchaseFile $file ): self
{
if ( $this->files->removeElement( $file ) ) {
// set the owning side to null (unless already changed)
if ( $file->getPurchase() === $this ) {
$file->setPurchase( NULL );
}
}
return $this;
}
/**
* @return Collection|PurchaseProductItem[]
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem( PurchaseProductItem $item ): self
{
if ( !$this->items->contains( $item ) ) {
$this->items[] = $item;
$item->setPurchase( $this );
}
return $this;
}
public function removeItem( PurchaseProductItem $item ): self
{
if ( $this->items->removeElement( $item ) ) {
// set the owning side to null (unless already changed)
if ( $item->getPurchase() === $this ) {
$item->setPurchase( NULL );
}
}
return $this;
}
/**
* @return Collection|PointTransaction[]
*/
public function getPointTransactions(): Collection
{
return $this->pointTransactions;
}
public function addPointTransaction( PointTransaction $pointTransaction ): self
{
if ( !$this->pointTransactions->contains( $pointTransaction ) ) {
$this->pointTransactions[] = $pointTransaction;
$pointTransaction->setPurchase( $this );
}
return $this;
}
public function removePointTransaction( PointTransaction $pointTransaction ): self
{
if ( $this->pointTransactions->removeElement( $pointTransaction ) ) {
// set the owning side to null (unless already changed)
if ( $pointTransaction->getPurchase() === $this ) {
$pointTransaction->setPurchase( NULL );
}
}
return $this;
}
public function getValidationDate(): ?DateTimeInterface
{
return $this->validationDate;
}
public function setValidationDate( ?DateTimeInterface $validationDate ): self
{
$this->validationDate = $validationDate;
return $this;
}
public function getDistributorAddress1(): ?string
{
return $this->distributorAddress1;
}
public function setDistributorAddress1( ?string $distributorAddress1 ): self
{
$this->distributorAddress1 = $distributorAddress1;
return $this;
}
public function getCustomerFirstName(): ?string
{
return $this->customerFirstName;
}
public function setCustomerFirstName( ?string $customerFirstName ): self
{
$this->customerFirstName = $customerFirstName;
return $this;
}
public function getCustomerLastName(): ?string
{
return $this->customerLastName;
}
public function setCustomerLastName( ?string $customerLastName ): self
{
$this->customerLastName = $customerLastName;
return $this;
}
public function getCustomerAddress1(): ?string
{
return $this->customerAddress1;
}
public function setCustomerAddress1( ?string $customerAddress1 ): self
{
$this->customerAddress1 = $customerAddress1;
return $this;
}
public function getCustomerCity(): ?string
{
return $this->customerCity;
}
public function setCustomerCity( ?string $customerCity ): self
{
$this->customerCity = $customerCity;
return $this;
}
public function getCustomerPostcode(): ?string
{
return $this->customerPostcode;
}
public function setCustomerPostcode( ?string $customerPostcode ): self
{
$this->customerPostcode = $customerPostcode;
return $this;
}
public function getCustomerAddress2(): ?string
{
return $this->customerAddress2;
}
public function setCustomerAddress2( ?string $customerAddress2 ): self
{
$this->customerAddress2 = $customerAddress2;
return $this;
}
public function getCommercialName(): ?string
{
return $this->commercialName;
}
public function setCommercialName( ?string $commercialName ): self
{
$this->commercialName = $commercialName;
return $this;
}
}