src/Entity/ArticleDepartment.php line 30

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\Get;
  5. use ApiPlatform\Metadata\GetCollection;
  6. use App\Kimia;
  7. use App\Model\Control;
  8. use App\Trait\AuthorTrait;
  9. use App\Repository\ArticleDepartmentRepository;
  10. use App\Trait\ActivableTrait;
  11. use App\Trait\DateTrait;
  12. use DateTimeInterface;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\DBAL\Types\Types;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use function Symfony\Component\String\u;
  19. #[ApiResource(
  20.     operations: [
  21.         new GetCollection(),
  22.         new Get(),
  23.     ],
  24.     normalizationContext: ['groups' => ['article:list']],
  25. )]
  26. #[ORM\Entity(repositoryClassArticleDepartmentRepository::class)]
  27. class ArticleDepartment
  28. {
  29.     use DateTrait{DateTrait::__construct as private dateConstruct;}
  30.     use ActivableTrait;
  31.     use AuthorTrait;
  32.     #[ORM\Id]
  33.     #[ORM\GeneratedValue]
  34.     #[ORM\Column]
  35.     #[Groups(['article:list''order:list'])]
  36.     private ?int $id null;
  37.     #[ORM\Column(length255nullabletrue)]
  38.     #[Groups(['article:list'])]
  39.     private ?string $name null;
  40.     #[ORM\ManyToOne(inversedBy'departments')]
  41.     #[ORM\JoinColumn(nullabletrue)]
  42.     #[Groups(['order:list'])]
  43.     private ?Article $article null;
  44.     #[ORM\Column(nullabletrue)]
  45.     #[Groups(['article:list''order:list'])]
  46.     private ?int $quantity null;
  47.     #[ORM\Column(nullabletrue)]
  48.     #[Groups(['article:list'])]
  49.     private ?float $margin null;
  50.     #[ORM\ManyToOne]
  51.     #[ORM\JoinColumn(nullablefalse)]
  52.     #[Groups(['article:list''order:list'])]
  53.     private ?Currency $currency null;
  54.     #[ORM\ManyToOne(inversedBy'articles')]
  55.     #[ORM\JoinColumn(nullablefalse)]
  56.     #[Groups(['article:list''order:list'])]
  57.     private ?Department $department null;
  58.     #[ORM\OneToMany(mappedBy'article'targetEntityOrderItems::class, orphanRemovaltrue)]
  59.     private Collection $orderItems;
  60.     #[ORM\OneToMany(mappedBy'article'targetEntityPromotion::class)]
  61.     private Collection $promotions;
  62.     #[ORM\OneToMany(mappedBy'articleDepartment'targetEntityArticleDepartmentPackage::class, cascade: ['persist''remove'])]
  63.     private Collection $articleDepartmentPackages;
  64.     #[ORM\Column]
  65.     #[Groups(['article:list'])]
  66.     private ?bool $isPackage;
  67.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  68.     private ?DateTimeInterface $packageStartedAt null;
  69.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  70.     private ?DateTimeInterface $packageEndedAt null;
  71.     #[ORM\Column(nullabletrue)]
  72.     private ?float $amount null;
  73.     #[ORM\Column(nullabletrue)]
  74.     private ?array $controls null;
  75.     #[ORM\ManyToOne(inversedBy'articleDepartments')]
  76.     #[ORM\JoinColumn(nullabletrue)]
  77.     private ?Establishment $establishment null;
  78.     public function __construct()
  79.     {
  80.         $this->dateConstruct();
  81.         $this->isActive Kimia::ACTIVE;
  82.         $this->isPackage 0;
  83.         $this->orderItems = new ArrayCollection();
  84.         $this->promotions = new ArrayCollection();
  85.         $this->articleDepartmentPackages = new ArrayCollection();
  86.     }
  87.     public function __toString(): string
  88.     {
  89.         return $this->getName();
  90.     }
  91.     public function getId(): ?int
  92.     {
  93.         return $this->id;
  94.     }
  95.     #[Groups(['order:list'])]
  96.     public function getName(): ?string
  97.     {
  98.         $name $this->name;
  99.         if ($this->getArticle()) $name $this->getArticle()->getName();
  100.         return $name;
  101.     }
  102.     #[Groups(['article:list'])]
  103.     public function getNameOrder(): ?string
  104.     {
  105.         return "{$this->getName()} ({$this->getAmount()} {$this->getCurrency()?->getMin()}) [{$this->getDepartment()}]";
  106.     }
  107.     public function setName(?string $name): self
  108.     {
  109.         $this->name $name;
  110.         return $this;
  111.     }
  112.     public function getArticle(): ?Article
  113.     {
  114.         return $this->article;
  115.     }
  116.     public function setArticle(?Article $article): self
  117.     {
  118.         $this->article $article;
  119.         return $this;
  120.     }
  121.     #[Groups(['article:list'])]
  122.     public function getType(): ?string
  123.     {
  124.         return $this->isPackage 'Packet de promotion' $this->article->getTypeName();
  125.     }
  126.     #[Groups(['order:list''article:list'])]
  127.     public function getAmount(): ?float
  128.     {
  129.         $amount 0;
  130.         if ($this->amount) {
  131.             $amount $this->amount;
  132.         } else {
  133.             if ($this->isIsPackage()) {
  134.                 /** @var ArticleDepartmentPackage $articleDepartmentPackage */
  135.                 foreach ($this->articleDepartmentPackages as $articleDepartmentPackage)
  136.                     $amount += $articleDepartmentPackage->getAmount();
  137.             } else {
  138.                 $amount $this->article?->getAmount() + $this->margin;
  139.             }
  140.         }
  141.         return $amount;
  142.     }
  143.     public function setAmount(?float $amount): self
  144.     {
  145.         $this->amount $amount;
  146.         return $this;
  147.     }
  148.     public function getQuantity(): ?int
  149.     {
  150.         return $this->quantity;
  151.     }
  152.     #[Groups(['article:list'])]
  153.     public function getQuantityTotal(): ?int
  154.     {
  155.         $quantity $this->quantity;
  156.         /** @var ArticleDepartmentPackage $package_ */
  157.         foreach ($this->getArticleDepartmentPackages() as $package_)
  158.             $quantity += ($package_->getQuantity() * $quantity);
  159.         return $quantity;
  160.     }
  161.     public function setQuantity(?int $quantity): self
  162.     {
  163.         $this->quantity $quantity;
  164.         return $this;
  165.     }
  166.     #[Groups(['article:list'])]
  167.     public function getMargin(): ?float
  168.     {
  169.         $margin $this->margin;
  170.         if ($this->isPackage) {
  171.             /** @var ArticleDepartmentPackage $package */
  172.             foreach ($this->getArticleDepartmentPackages() as $package) {
  173.                 $margin += ($package->getMargin() * $package->getQuantity());
  174.             }
  175.         }
  176.         return $margin;
  177.     }
  178.     public function setMargin(?float $margin): self
  179.     {
  180.         $this->margin $margin;
  181.         return $this;
  182.     }
  183.     public function getCurrency(): ?Currency
  184.     {
  185.         return $this->currency;
  186.     }
  187.     public function setCurrency(?Currency $currency): self
  188.     {
  189.         $this->currency $currency;
  190.         return $this;
  191.     }
  192.     public function getDepartment(): ?Department
  193.     {
  194.         return $this->department;
  195.     }
  196.     public function setDepartment(?Department $department): self
  197.     {
  198.         $this->department $department;
  199.         return $this;
  200.     }
  201.     /**
  202.      * @return Collection<int, OrderItems>
  203.      */
  204.     public function getOrderItems(): Collection
  205.     {
  206.         return $this->orderItems;
  207.     }
  208.     public function addOrderItem(OrderItems $orderItem): self
  209.     {
  210.         if (!$this->orderItems->contains($orderItem)) {
  211.             $this->orderItems->add($orderItem);
  212.             $orderItem->setArticle($this);
  213.         }
  214.         return $this;
  215.     }
  216.     public function removeOrderItem(OrderItems $orderItem): self
  217.     {
  218.         if ($this->orderItems->removeElement($orderItem)) {
  219.             // set the owning side to null (unless already changed)
  220.             if ($orderItem->getArticle() === $this) {
  221.                 $orderItem->setArticle(null);
  222.             }
  223.         }
  224.         return $this;
  225.     }
  226.     /**
  227.      * @return Collection<int, Promotion>
  228.      */
  229.     public function getPromotions(): Collection
  230.     {
  231.         return $this->promotions;
  232.     }
  233.     public function addPromotion(Promotion $promotion): self
  234.     {
  235.         if (!$this->promotions->contains($promotion)) {
  236.             $this->promotions->add($promotion);
  237.             $promotion->setArticle($this);
  238.         }
  239.         return $this;
  240.     }
  241.     public function removePromotion(Promotion $promotion): self
  242.     {
  243.         if ($this->promotions->removeElement($promotion)) {
  244.             // set the owning side to null (unless already changed)
  245.             if ($promotion->getArticle() === $this) {
  246.                 $promotion->setArticle(null);
  247.             }
  248.         }
  249.         return $this;
  250.     }
  251.     /**
  252.      * @return Collection<int, ArticleDepartmentPackage>
  253.      */
  254.     public function getArticleDepartmentPackages(): Collection
  255.     {
  256.         return $this->articleDepartmentPackages;
  257.     }
  258.     public function addArticleDepartmentPackage(ArticleDepartmentPackage $articleDepartmentPackage): self
  259.     {
  260.         if (!$this->articleDepartmentPackages->contains($articleDepartmentPackage)) {
  261.             $this->articleDepartmentPackages->add($articleDepartmentPackage);
  262.             $articleDepartmentPackage->setArticleDepartment($this);
  263.         }
  264.         return $this;
  265.     }
  266.     public function removeArticleDepartmentPackage(ArticleDepartmentPackage $articleDepartmentPackage): self
  267.     {
  268.         if ($this->articleDepartmentPackages->removeElement($articleDepartmentPackage)) {
  269.             // set the owning side to null (unless already changed)
  270.             if ($articleDepartmentPackage->getArticleDepartment() === $this) {
  271.                 $articleDepartmentPackage->setArticleDepartment(null);
  272.             }
  273.         }
  274.         return $this;
  275.     }
  276.     public function isIsPackage(): ?bool
  277.     {
  278.         return $this->isPackage;
  279.     }
  280.     public function setIsPackage(bool $isPackage): self
  281.     {
  282.         $this->isPackage $isPackage;
  283.         return $this;
  284.     }
  285.     public function getPackageStartedAt(): ?DateTimeInterface
  286.     {
  287.         return $this->packageStartedAt;
  288.     }
  289.     public function setPackageStartedAt(?DateTimeInterface $packageStartedAt): self
  290.     {
  291.         $this->packageStartedAt $packageStartedAt;
  292.         return $this;
  293.     }
  294.     public function getPackageEndedAt(): ?DateTimeInterface
  295.     {
  296.         return $this->packageEndedAt;
  297.     }
  298.     public function setPackageEndedAt(?DateTimeInterface $packageEndedAt): self
  299.     {
  300.         $this->packageEndedAt $packageEndedAt;
  301.         return $this;
  302.     }
  303.     public function getControls(): ?array
  304.     {
  305.         return $this->controls;
  306.     }
  307.     public function hasControls(): bool
  308.     {
  309.         return !empty($this->controls);
  310.     }
  311.     public function getController(): float|bool
  312.     {
  313.         if ($this->controls) {
  314.             foreach ($this->controls as $control) {
  315.                 $method u($control['variable'])->title()->prepend('get');
  316.                 $article $this->getArticle();
  317.                 if (is_object($article) && method_exists($article$method->toString())) {
  318.                     if (Control::evaluate($control['value'], $control['type'], call_user_func([$article$method->toString()]))) {
  319.                         return $control['amount'];
  320.                     }
  321.                 }
  322.             }
  323.         }
  324.         return false;
  325.     }
  326.     public function setControls(?array $controls): static
  327.     {
  328.         $this->controls $controls;
  329.         return $this;
  330.     }
  331.     public static function getVariables(): array
  332.     {
  333.         return array_merge(
  334.             Article::getVariables(),
  335.             [
  336.                 "Marge bénéficiaire" => "margin"
  337.             ]
  338.         );
  339.     }
  340.     public function getEstablishment(): ?Establishment
  341.     {
  342.         return $this->establishment;
  343.     }
  344.     public function setEstablishment(?Establishment $establishment): static
  345.     {
  346.         $this->establishment $establishment;
  347.         return $this;
  348.     }
  349. }