src/Entity/ArticleDepartment.php line 30
<?phpnamespace App\Entity;use ApiPlatform\Metadata\ApiResource;use ApiPlatform\Metadata\Get;use ApiPlatform\Metadata\GetCollection;use App\Kimia;use App\Model\Control;use App\Trait\AuthorTrait;use App\Repository\ArticleDepartmentRepository;use App\Trait\ActivableTrait;use App\Trait\DateTrait;use DateTimeInterface;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\DBAL\Types\Types;use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Serializer\Annotation\Groups;use function Symfony\Component\String\u;#[ApiResource(operations: [new GetCollection(),new Get(),],normalizationContext: ['groups' => ['article:list']],)]#[ORM\Entity(repositoryClass: ArticleDepartmentRepository::class)]class ArticleDepartment{use DateTrait{DateTrait::__construct as private dateConstruct;}use ActivableTrait;use AuthorTrait;#[ORM\Id]#[ORM\GeneratedValue]#[ORM\Column]#[Groups(['article:list', 'order:list'])]private ?int $id = null;#[ORM\Column(length: 255, nullable: true)]#[Groups(['article:list'])]private ?string $name = null;#[ORM\ManyToOne(inversedBy: 'departments')]#[ORM\JoinColumn(nullable: true)]#[Groups(['order:list'])]private ?Article $article = null;#[ORM\Column(nullable: true)]#[Groups(['article:list', 'order:list'])]private ?int $quantity = null;#[ORM\Column(nullable: true)]#[Groups(['article:list'])]private ?float $margin = null;#[ORM\ManyToOne]#[ORM\JoinColumn(nullable: false)]#[Groups(['article:list', 'order:list'])]private ?Currency $currency = null;#[ORM\ManyToOne(inversedBy: 'articles')]#[ORM\JoinColumn(nullable: false)]#[Groups(['article:list', 'order:list'])]private ?Department $department = null;#[ORM\OneToMany(mappedBy: 'article', targetEntity: OrderItems::class, orphanRemoval: true)]private Collection $orderItems;#[ORM\OneToMany(mappedBy: 'article', targetEntity: Promotion::class)]private Collection $promotions;#[ORM\OneToMany(mappedBy: 'articleDepartment', targetEntity: ArticleDepartmentPackage::class, cascade: ['persist', 'remove'])]private Collection $articleDepartmentPackages;#[ORM\Column]#[Groups(['article:list'])]private ?bool $isPackage;#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]private ?DateTimeInterface $packageStartedAt = null;#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]private ?DateTimeInterface $packageEndedAt = null;#[ORM\Column(nullable: true)]private ?float $amount = null;#[ORM\Column(nullable: true)]private ?array $controls = null;#[ORM\ManyToOne(inversedBy: 'articleDepartments')]#[ORM\JoinColumn(nullable: true)]private ?Establishment $establishment = null;public function __construct(){$this->dateConstruct();$this->isActive = Kimia::ACTIVE;$this->isPackage = 0;$this->orderItems = new ArrayCollection();$this->promotions = new ArrayCollection();$this->articleDepartmentPackages = new ArrayCollection();}public function __toString(): string{return $this->getName();}public function getId(): ?int{return $this->id;}#[Groups(['order:list'])]public function getName(): ?string{$name = $this->name;if ($this->getArticle()) $name = $this->getArticle()->getName();return $name;}#[Groups(['article:list'])]public function getNameOrder(): ?string{return "{$this->getName()} ({$this->getAmount()} {$this->getCurrency()?->getMin()}) [{$this->getDepartment()}]";}public function setName(?string $name): self{$this->name = $name;return $this;}public function getArticle(): ?Article{return $this->article;}public function setArticle(?Article $article): self{$this->article = $article;return $this;}#[Groups(['article:list'])]public function getType(): ?string{return $this->isPackage ? 'Packet de promotion' : $this->article->getTypeName();}#[Groups(['order:list', 'article:list'])]public function getAmount(): ?float{$amount = 0;if ($this->amount) {$amount = $this->amount;} else {if ($this->isIsPackage()) {/** @var ArticleDepartmentPackage $articleDepartmentPackage */foreach ($this->articleDepartmentPackages as $articleDepartmentPackage)$amount += $articleDepartmentPackage->getAmount();} else {$amount = $this->article?->getAmount() + $this->margin;}}return $amount;}public function setAmount(?float $amount): self{$this->amount = $amount;return $this;}public function getQuantity(): ?int{return $this->quantity;}#[Groups(['article:list'])]public function getQuantityTotal(): ?int{$quantity = $this->quantity;/** @var ArticleDepartmentPackage $package_ */foreach ($this->getArticleDepartmentPackages() as $package_)$quantity += ($package_->getQuantity() * $quantity);return $quantity;}public function setQuantity(?int $quantity): self{$this->quantity = $quantity;return $this;}#[Groups(['article:list'])]public function getMargin(): ?float{$margin = $this->margin;if ($this->isPackage) {/** @var ArticleDepartmentPackage $package */foreach ($this->getArticleDepartmentPackages() as $package) {$margin += ($package->getMargin() * $package->getQuantity());}}return $margin;}public function setMargin(?float $margin): self{$this->margin = $margin;return $this;}public function getCurrency(): ?Currency{return $this->currency;}public function setCurrency(?Currency $currency): self{$this->currency = $currency;return $this;}public function getDepartment(): ?Department{return $this->department;}public function setDepartment(?Department $department): self{$this->department = $department;return $this;}/*** @return Collection<int, OrderItems>*/public function getOrderItems(): Collection{return $this->orderItems;}public function addOrderItem(OrderItems $orderItem): self{if (!$this->orderItems->contains($orderItem)) {$this->orderItems->add($orderItem);$orderItem->setArticle($this);}return $this;}public function removeOrderItem(OrderItems $orderItem): self{if ($this->orderItems->removeElement($orderItem)) {// set the owning side to null (unless already changed)if ($orderItem->getArticle() === $this) {$orderItem->setArticle(null);}}return $this;}/*** @return Collection<int, Promotion>*/public function getPromotions(): Collection{return $this->promotions;}public function addPromotion(Promotion $promotion): self{if (!$this->promotions->contains($promotion)) {$this->promotions->add($promotion);$promotion->setArticle($this);}return $this;}public function removePromotion(Promotion $promotion): self{if ($this->promotions->removeElement($promotion)) {// set the owning side to null (unless already changed)if ($promotion->getArticle() === $this) {$promotion->setArticle(null);}}return $this;}/*** @return Collection<int, ArticleDepartmentPackage>*/public function getArticleDepartmentPackages(): Collection{return $this->articleDepartmentPackages;}public function addArticleDepartmentPackage(ArticleDepartmentPackage $articleDepartmentPackage): self{if (!$this->articleDepartmentPackages->contains($articleDepartmentPackage)) {$this->articleDepartmentPackages->add($articleDepartmentPackage);$articleDepartmentPackage->setArticleDepartment($this);}return $this;}public function removeArticleDepartmentPackage(ArticleDepartmentPackage $articleDepartmentPackage): self{if ($this->articleDepartmentPackages->removeElement($articleDepartmentPackage)) {// set the owning side to null (unless already changed)if ($articleDepartmentPackage->getArticleDepartment() === $this) {$articleDepartmentPackage->setArticleDepartment(null);}}return $this;}public function isIsPackage(): ?bool{return $this->isPackage;}public function setIsPackage(bool $isPackage): self{$this->isPackage = $isPackage;return $this;}public function getPackageStartedAt(): ?DateTimeInterface{return $this->packageStartedAt;}public function setPackageStartedAt(?DateTimeInterface $packageStartedAt): self{$this->packageStartedAt = $packageStartedAt;return $this;}public function getPackageEndedAt(): ?DateTimeInterface{return $this->packageEndedAt;}public function setPackageEndedAt(?DateTimeInterface $packageEndedAt): self{$this->packageEndedAt = $packageEndedAt;return $this;}public function getControls(): ?array{return $this->controls;}public function hasControls(): bool{return !empty($this->controls);}public function getController(): float|bool{if ($this->controls) {foreach ($this->controls as $control) {$method = u($control['variable'])->title()->prepend('get');$article = $this->getArticle();if (is_object($article) && method_exists($article, $method->toString())) {if (Control::evaluate($control['value'], $control['type'], call_user_func([$article, $method->toString()]))) {return $control['amount'];}}}}return false;}public function setControls(?array $controls): static{$this->controls = $controls;return $this;}public static function getVariables(): array{return array_merge(Article::getVariables(),["Marge bénéficiaire" => "margin"]);}public function getEstablishment(): ?Establishment{return $this->establishment;}public function setEstablishment(?Establishment $establishment): static{$this->establishment = $establishment;return $this;}}