src/Entity/Purchase.php line 22

Open in your IDE?
  1. <?php
  2.     namespace App\Entity;
  3.     use App\Constants\Purchase as PurchaseStatus;
  4.     use App\Repository\PurchaseRepository;
  5.     use App\Traits\DateTrait;
  6.     use DateTimeInterface;
  7.     use Doctrine\Common\Collections\ArrayCollection;
  8.     use Doctrine\Common\Collections\Collection;
  9.     use Doctrine\ORM\Mapping as ORM;
  10.     use JMS\Serializer\Annotation as Serializer;
  11.     use JMS\Serializer\Annotation\Expose;
  12.     use JMS\Serializer\Annotation\Groups;
  13.     /**
  14.      * @ORM\Entity(repositoryClass=PurchaseRepository::class)
  15.      *
  16.      * @Serializer\ExclusionPolicy("ALL")
  17.      */
  18.     class Purchase
  19.     {
  20.         // Un nombre de points est attribué à chaque référence
  21.         public const POINT_WITH_REFERENCE 'point_with_reference';
  22.         //  On se référence au prix total payé par l’installateur
  23.         public const POINT_PRICE_PAID_TOTAL 'point_price_paid_total';
  24.         //  On se référence au prix unitaire par produit payé par l’installateur
  25.         public const POINT_PRICE_PAID_PER_UNIT 'point_price_paid_per_unit';
  26.         public const STATUS_PENDING   PurchaseStatus::STATUS_PENDING;
  27.         public const STATUS_VALIDATED PurchaseStatus::STATUS_VALIDATED;
  28.         public const STATUS_RETURNED  PurchaseStatus::STATUS_RETURNED;
  29.         public const STATUS_REJECTED  PurchaseStatus::STATUS_REJECTED;
  30.         /**
  31.          * @ORM\Id
  32.          * @ORM\GeneratedValue
  33.          * @ORM\Column(type="integer")
  34.          *
  35.          * @Expose
  36.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  37.          */
  38.         private ?int $id NULL;
  39.         /**
  40.          * Numéro de facture
  41.          *
  42.          * @ORM\Column(type="string", length=64)
  43.          *
  44.          * @Expose
  45.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  46.          */
  47.         private $invoiceNumber;
  48.         /**
  49.          * Date de facture
  50.          *
  51.          * @ORM\Column(type="date", nullable=true)
  52.          *
  53.          * @Expose
  54.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  55.          */
  56.         private $invoiceDate;
  57.         /**
  58.          * Status de la déclaration
  59.          *
  60.          * @ORM\Column(type="integer", options={"default": self::STATUS_PENDING})
  61.          *
  62.          * @Expose
  63.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  64.          */
  65.         private $status self::STATUS_PENDING;
  66.         /**
  67.          * Raison du statut
  68.          *
  69.          * @ORM\Column(type="string", length=512, nullable=true)
  70.          */
  71.         private ?string $lastStatusReason NULL;
  72.         /**
  73.          * Nom du distributeur
  74.          *
  75.          * @deprecated  Relation sur {@see Distributor}
  76.          *
  77.          * @ORM\Column(type="string", length=255, nullable=true)
  78.          */
  79.         private ?string $distributorName NULL;
  80.         /**
  81.          * Code postal du distributeur
  82.          *
  83.          * @deprecated Relation sur {@see Distributor}
  84.          *
  85.          * @ORM\Column(type="string", length=255, nullable=true)
  86.          */
  87.         private ?string $distributorPostalCode NULL;
  88.         /**
  89.          * Ville du distributeur
  90.          *
  91.          * @deprecated Relation sur {@see Distributor}
  92.          *
  93.          * @ORM\Column(type="string", length=255, nullable=true)
  94.          */
  95.         private ?string $distributorCity NULL;
  96.         /**
  97.          * Pays du distributeur
  98.          *
  99.          * @deprecated Relation sur {@see Distributor}
  100.          *
  101.          * @ORM\Column(type="string", length=255, nullable=true)
  102.          */
  103.         private ?string $distributorCountry NULL;
  104.         /**
  105.          * Valeur de la déclaration
  106.          *
  107.          * @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
  108.          *
  109.          * @ORM\Column(type="integer", nullable=true)
  110.          */
  111.         private ?int $value NULL;
  112.         /**
  113.          * Personne qui a validé la déclaration
  114.          *
  115.          * @ORM\ManyToOne(targetEntity=User::class,inversedBy="purchasesIHaveProcessed")
  116.          * @ORM\JoinColumn(onDelete="SET NULL")
  117.          *
  118.          * @Expose
  119.          * @Groups({"purchase"})
  120.          */
  121.         private $validator;
  122.         /**
  123.          * Utilisateur qui a fait la déclaration
  124.          *
  125.          * @ORM\ManyToOne(targetEntity=User::class,inversedBy="purchases")
  126.          *
  127.          * @Expose
  128.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  129.          */
  130.         private $user;
  131.         /**
  132.          * Distributeur rattaché à la déclaration
  133.          *
  134.          * @ORM\ManyToOne(targetEntity=Distributor::class, inversedBy="purchases")
  135.          *
  136.          * @Expose()
  137.          * @Groups({"export_purchase_declaration_datatable"})
  138.          */
  139.         private $distributor;
  140.         /**
  141.          * @TODO Check que toujours utilse
  142.          */
  143.         private $imageFile;
  144.         /**
  145.          * @ORM\OneToMany(
  146.          *     targetEntity=PurchaseHistory::class,
  147.          *     mappedBy="purchase",
  148.          *     cascade={"persist", "remove"}
  149.          * )
  150.          */
  151.         private $purchaseHistories;
  152.         /**
  153.          * @ORM\OneToMany(
  154.          *     targetEntity=PurchaseFile::class,
  155.          *     mappedBy="purchase",
  156.          *     cascade={"remove", "persist"}
  157.          * )
  158.          */
  159.         private $files;
  160.         /**
  161.          * @ORM\OneToMany(
  162.          *     targetEntity=PurchaseProductItem::class,
  163.          *     mappedBy="purchase",
  164.          *     cascade={"remove", "persist"},
  165.          *     orphanRemoval=true
  166.          * )
  167.          *
  168.          * @Expose()
  169.          * @Groups({"export_purchase_declaration_datatable"})
  170.          */
  171.         private $items;
  172.         /**
  173.          * @ORM\OneToMany(targetEntity=PointTransaction::class, mappedBy="purchase")
  174.          */
  175.         private Collection $pointTransactions;
  176.         /**
  177.          * Date de la validation
  178.          *
  179.          * @ORM\Column(type="datetime", nullable=true)
  180.          */
  181.         private $validationDate;
  182.         /**
  183.          * @ORM\Column(type="string", length=255, nullable=true)
  184.          */
  185.         private ?string $distributorAddress1 NULL;
  186.         /**
  187.          * Prénom du client
  188.          *
  189.          * @ORM\Column(type="string", length=255, nullable=true)
  190.          *
  191.          * @Expose
  192.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  193.          */
  194.         private ?string $customerFirstName NULL;
  195.         /**
  196.          * Nom du client
  197.          *
  198.          * @ORM\Column(type="string", length=255, nullable=true)
  199.          *
  200.          * @Expose
  201.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  202.          */
  203.         private ?string $customerLastName NULL;
  204.         /**
  205.          * Adresse du client
  206.          *
  207.          * @ORM\Column(type="string", length=255, nullable=true)
  208.          *
  209.          * @Expose
  210.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  211.          */
  212.         private ?string $customerAddress1 NULL;
  213.         /**
  214.          * Ville du client
  215.          *
  216.          * @ORM\Column(type="string", length=255, nullable=true)
  217.          *
  218.          * @Expose
  219.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  220.          */
  221.         private ?string $customerCity NULL;
  222.         /**
  223.          * Code postal du client
  224.          *
  225.          * @ORM\Column(type="string", length=255, nullable=true)
  226.          *
  227.          * @Expose
  228.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  229.          */
  230.         private ?string $customerPostcode NULL;
  231.         /**
  232.          * Complément d'adresse du client
  233.          *
  234.          * @ORM\Column(type="text", nullable=true)
  235.          *
  236.          * @Expose
  237.          * @Groups({"purchase", "export_purchase_declaration_datatable"})
  238.          */
  239.         private ?string $customerAddress2 NULL;
  240.         /**
  241.          * @ORM\Column(type="string", length=255, nullable=true)
  242.          */
  243.         private ?string $commercialName NULL;
  244.         public function __construct()
  245.         {
  246.             $this->purchaseHistories = new ArrayCollection();
  247.             $this->files             = new ArrayCollection();
  248.             $this->items             = new ArrayCollection();
  249.             $this->pointTransactions = new ArrayCollection();
  250.         }
  251.         use DateTrait;
  252.         /*
  253.          * ============================================================================================
  254.          * =============================== FONCTIONS CUSTOM ===========================================
  255.          * ============================================================================================
  256.          */
  257.         /**
  258.          * @return float|int|null
  259.          * @Serializer\VirtualProperty()
  260.          * @Serializer\SerializedName("get_sum_values")
  261.          *
  262.          * @Expose()
  263.          * @Groups({"purchase"})
  264.          */
  265.         public function getSumValues()
  266.         {
  267.             $sum 0;
  268.             /** @var PointTransaction $pointTransaction */
  269.             foreach ( $this->pointTransactions as $pointTransaction ) {
  270.                 $sum += $pointTransaction->getValue();
  271.             }
  272.             return $sum;
  273.         }
  274.         /**
  275.          * @return float|int
  276.          * @deprecated
  277.          * $value returns points acquired including boosters
  278.          * This method returns points without booster effects
  279.          */
  280.         public function getBulkValue()
  281.         {
  282.             $value 0;
  283.             foreach ( $this->items as $item ) {
  284.                 $value += $item->getProduct()->getValue() * $item->getQuantity();
  285.             }
  286.             return $value;
  287.         }
  288.         /**
  289.          * @return int|null
  290.          * @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
  291.          */
  292.         public function getValue(): ?int
  293.         {
  294.             return $this->value;
  295.         }
  296.         /**
  297.          * @param int|null $value
  298.          *
  299.          * @return $this
  300.          * @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
  301.          */
  302.         public function setValue( ?int $value ): self
  303.         {
  304.             $this->value $value;
  305.             return $this;
  306.         }
  307.         /**
  308.          * @return mixed
  309.          */
  310.         public function getImageFile()
  311.         {
  312.             return $this->imageFile;
  313.         }
  314.         public function getArrayCategoryValues()
  315.         {
  316.             $categoryValues = [];
  317.             /** @var PurchaseProductItem $item */
  318.             foreach ( $this->items as $item ) {
  319.                 $product   $item->getProduct();
  320.                 $productCV $product->getArrayCategoryValues();
  321.                 if ( $productCV !== NULL ) {
  322.                     if ( $categoryValues === [] ) {
  323.                         $categoryValues $productCV;
  324.                     } else {
  325.                         $categoryValues array_combine(
  326.                             array_keys$categoryValues ),
  327.                             array_map( function ( $a$b ) {
  328.                                 return $a $b;
  329.                             }, $categoryValues$productCV )
  330.                         );
  331.                     }
  332.                 }
  333.             }
  334.             return $categoryValues;
  335.         }
  336.         /*
  337.          * ============================================================================================
  338.          * ============================== FIN FONCTIONS CUSTOM ========================================
  339.          * ============================================================================================
  340.          */
  341.         /**
  342.          * @param $imageFile
  343.          *
  344.          * @return $this
  345.          */
  346.         public function setImageFile$imageFile ): Purchase
  347.         {
  348.             $this->imageFile $imageFile;
  349.             return $this;
  350.         }
  351.         /**
  352.          * @param array $files
  353.          *
  354.          * @return $this
  355.          */
  356.         public function addFiles( array $files ): Purchase
  357.         {
  358.             $this->files = new ArrayCollectionarray_merge$this->files->toArray(), $files ) );
  359.             return $this;
  360.         }
  361.         public function getId(): ?int
  362.         {
  363.             return $this->id;
  364.         }
  365.         public function getInvoiceNumber(): ?string
  366.         {
  367.             return $this->invoiceNumber;
  368.         }
  369.         public function setInvoiceNumberstring $invoiceNumber ): self
  370.         {
  371.             $this->invoiceNumber $invoiceNumber;
  372.             return $this;
  373.         }
  374.         public function getInvoiceDate(): ?DateTimeInterface
  375.         {
  376.             return $this->invoiceDate;
  377.         }
  378.         public function setInvoiceDate( ?DateTimeInterface $invoiceDate ): self
  379.         {
  380.             $this->invoiceDate $invoiceDate;
  381.             return $this;
  382.         }
  383.         public function getStatus(): ?string
  384.         {
  385.             return $this->status;
  386.         }
  387.         public function setStatus( ?string $status ): self
  388.         {
  389.             $this->status $status;
  390.             return $this;
  391.         }
  392.         public function getLastStatusReason(): ?string
  393.         {
  394.             return $this->lastStatusReason;
  395.         }
  396.         public function setLastStatusReason( ?string $lastStatusReason ): self
  397.         {
  398.             $this->lastStatusReason $lastStatusReason;
  399.             return $this;
  400.         }
  401.         /**
  402.          * @return string|null
  403.          * @deprecated Relation sur {@see Distributor}
  404.          */
  405.         public function getDistributorName(): ?string
  406.         {
  407.             return $this->distributorName;
  408.         }
  409.         /**
  410.          * @param string|null $distributorName
  411.          *
  412.          * @return $this
  413.          * @deprecated Relation sur {@see Distributor}
  414.          */
  415.         public function setDistributorName( ?string $distributorName ): self
  416.         {
  417.             $this->distributorName $distributorName;
  418.             return $this;
  419.         }
  420.         /**
  421.          * @return string|null
  422.          * @deprecated Relation sur {@see Distributor}
  423.          */
  424.         public function getDistributorPostalCode(): ?string
  425.         {
  426.             return $this->distributorPostalCode;
  427.         }
  428.         /**
  429.          * @param string|null $distributorPostalCode
  430.          *
  431.          * @return $this
  432.          * @deprecated Relation sur {@see Distributor}
  433.          */
  434.         public function setDistributorPostalCode( ?string $distributorPostalCode ): self
  435.         {
  436.             $this->distributorPostalCode $distributorPostalCode;
  437.             return $this;
  438.         }
  439.         /**
  440.          * @return string|null
  441.          * @deprecated Relation sur {@see Distributor}
  442.          */
  443.         public function getDistributorCity(): ?string
  444.         {
  445.             return $this->distributorCity;
  446.         }
  447.         /**
  448.          * @param string|null $distributorCity
  449.          *
  450.          * @return $this
  451.          * @deprecated Relation sur {@see Distributor}
  452.          */
  453.         public function setDistributorCity( ?string $distributorCity ): self
  454.         {
  455.             $this->distributorCity $distributorCity;
  456.             return $this;
  457.         }
  458.         /**
  459.          * @return string|null
  460.          * @deprecated Relation sur {@see Distributor}
  461.          */
  462.         public function getDistributorCountry(): ?string
  463.         {
  464.             return $this->distributorCountry;
  465.         }
  466.         /**
  467.          * @param string|null $distributorCountry
  468.          *
  469.          * @return $this
  470.          * @deprecated Relation sur {@see Distributor}
  471.          */
  472.         public function setDistributorCountry( ?string $distributorCountry ): self
  473.         {
  474.             $this->distributorCountry $distributorCountry;
  475.             return $this;
  476.         }
  477.         public function getValidator(): ?User
  478.         {
  479.             return $this->validator;
  480.         }
  481.         public function setValidator( ?User $validator ): self
  482.         {
  483.             $this->validator $validator;
  484.             return $this;
  485.         }
  486.         public function getUser(): ?User
  487.         {
  488.             return $this->user;
  489.         }
  490.         public function setUser( ?User $user ): self
  491.         {
  492.             $this->user $user;
  493.             return $this;
  494.         }
  495.         public function getDistributor(): ?Distributor
  496.         {
  497.             return $this->distributor;
  498.         }
  499.         public function setDistributor( ?Distributor $distributor ): self
  500.         {
  501.             $this->distributor $distributor;
  502.             return $this;
  503.         }
  504.         /**
  505.          * @return Collection|PurchaseHistory[]
  506.          */
  507.         public function getPurchaseHistories(): Collection
  508.         {
  509.             return $this->purchaseHistories;
  510.         }
  511.         public function addPurchaseHistoryPurchaseHistory $purchaseHistory ): self
  512.         {
  513.             if ( !$this->purchaseHistories->contains$purchaseHistory ) ) {
  514.                 $this->purchaseHistories[] = $purchaseHistory;
  515.                 $purchaseHistory->setPurchase$this );
  516.             }
  517.             return $this;
  518.         }
  519.         public function removePurchaseHistoryPurchaseHistory $purchaseHistory ): self
  520.         {
  521.             if ( $this->purchaseHistories->removeElement$purchaseHistory ) ) {
  522.                 // set the owning side to null (unless already changed)
  523.                 if ( $purchaseHistory->getPurchase() === $this ) {
  524.                     $purchaseHistory->setPurchaseNULL );
  525.                 }
  526.             }
  527.             return $this;
  528.         }
  529.         /**
  530.          * @return Collection|PurchaseFile[]
  531.          */
  532.         public function getFiles(): Collection
  533.         {
  534.             return $this->files;
  535.         }
  536.         public function addFilePurchaseFile $file ): self
  537.         {
  538.             if ( !$this->files->contains$file ) ) {
  539.                 $this->files[] = $file;
  540.                 $file->setPurchase$this );
  541.             }
  542.             return $this;
  543.         }
  544.         public function removeFilePurchaseFile $file ): self
  545.         {
  546.             if ( $this->files->removeElement$file ) ) {
  547.                 // set the owning side to null (unless already changed)
  548.                 if ( $file->getPurchase() === $this ) {
  549.                     $file->setPurchaseNULL );
  550.                 }
  551.             }
  552.             return $this;
  553.         }
  554.         /**
  555.          * @return Collection|PurchaseProductItem[]
  556.          */
  557.         public function getItems(): Collection
  558.         {
  559.             return $this->items;
  560.         }
  561.         public function addItemPurchaseProductItem $item ): self
  562.         {
  563.             if ( !$this->items->contains$item ) ) {
  564.                 $this->items[] = $item;
  565.                 $item->setPurchase$this );
  566.             }
  567.             return $this;
  568.         }
  569.         public function removeItemPurchaseProductItem $item ): self
  570.         {
  571.             if ( $this->items->removeElement$item ) ) {
  572.                 // set the owning side to null (unless already changed)
  573.                 if ( $item->getPurchase() === $this ) {
  574.                     $item->setPurchaseNULL );
  575.                 }
  576.             }
  577.             return $this;
  578.         }
  579.         /**
  580.          * @return Collection|PointTransaction[]
  581.          */
  582.         public function getPointTransactions(): Collection
  583.         {
  584.             return $this->pointTransactions;
  585.         }
  586.         public function addPointTransactionPointTransaction $pointTransaction ): self
  587.         {
  588.             if ( !$this->pointTransactions->contains$pointTransaction ) ) {
  589.                 $this->pointTransactions[] = $pointTransaction;
  590.                 $pointTransaction->setPurchase$this );
  591.             }
  592.             return $this;
  593.         }
  594.         public function removePointTransactionPointTransaction $pointTransaction ): self
  595.         {
  596.             if ( $this->pointTransactions->removeElement$pointTransaction ) ) {
  597.                 // set the owning side to null (unless already changed)
  598.                 if ( $pointTransaction->getPurchase() === $this ) {
  599.                     $pointTransaction->setPurchaseNULL );
  600.                 }
  601.             }
  602.             return $this;
  603.         }
  604.         public function getValidationDate(): ?DateTimeInterface
  605.         {
  606.             return $this->validationDate;
  607.         }
  608.         public function setValidationDate( ?DateTimeInterface $validationDate ): self
  609.         {
  610.             $this->validationDate $validationDate;
  611.             return $this;
  612.         }
  613.         public function getDistributorAddress1(): ?string
  614.         {
  615.             return $this->distributorAddress1;
  616.         }
  617.         public function setDistributorAddress1( ?string $distributorAddress1 ): self
  618.         {
  619.             $this->distributorAddress1 $distributorAddress1;
  620.             return $this;
  621.         }
  622.         public function getCustomerFirstName(): ?string
  623.         {
  624.             return $this->customerFirstName;
  625.         }
  626.         public function setCustomerFirstName( ?string $customerFirstName ): self
  627.         {
  628.             $this->customerFirstName $customerFirstName;
  629.             return $this;
  630.         }
  631.         public function getCustomerLastName(): ?string
  632.         {
  633.             return $this->customerLastName;
  634.         }
  635.         public function setCustomerLastName( ?string $customerLastName ): self
  636.         {
  637.             $this->customerLastName $customerLastName;
  638.             return $this;
  639.         }
  640.         public function getCustomerAddress1(): ?string
  641.         {
  642.             return $this->customerAddress1;
  643.         }
  644.         public function setCustomerAddress1( ?string $customerAddress1 ): self
  645.         {
  646.             $this->customerAddress1 $customerAddress1;
  647.             return $this;
  648.         }
  649.         public function getCustomerCity(): ?string
  650.         {
  651.             return $this->customerCity;
  652.         }
  653.         public function setCustomerCity( ?string $customerCity ): self
  654.         {
  655.             $this->customerCity $customerCity;
  656.             return $this;
  657.         }
  658.         public function getCustomerPostcode(): ?string
  659.         {
  660.             return $this->customerPostcode;
  661.         }
  662.         public function setCustomerPostcode( ?string $customerPostcode ): self
  663.         {
  664.             $this->customerPostcode $customerPostcode;
  665.             return $this;
  666.         }
  667.         public function getCustomerAddress2(): ?string
  668.         {
  669.             return $this->customerAddress2;
  670.         }
  671.         public function setCustomerAddress2( ?string $customerAddress2 ): self
  672.         {
  673.             $this->customerAddress2 $customerAddress2;
  674.             return $this;
  675.         }
  676.         public function getCommercialName(): ?string
  677.         {
  678.             return $this->commercialName;
  679.         }
  680.         public function setCommercialName( ?string $commercialName ): self
  681.         {
  682.             $this->commercialName $commercialName;
  683.             return $this;
  684.         }
  685.     }