<?phpnamespace App\Entity;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\ORM\Mapping as ORM;use App\Entity\Helpers\BaseEntity;use App\Entity\Helpers\TraitOrderTracking;use App\Repository\ProviderOrderRepository;#[ORM\Entity(repositoryClass: ProviderOrderRepository::class)]class ProviderOrder extends BaseEntity{ use TraitOrderTracking; #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private $id; #[ORM\Column(type: 'string', length: 255)] private $reference; #[ORM\Column(type: 'date_immutable')] private $orderedAt; #[ORM\Column(type: 'datetime_immutable')] private $deliveredEstimationAt; #[ORM\Column(type: 'boolean')] private $completed = false; #[ORM\ManyToOne(targetEntity: Provider::class)] #[ORM\JoinColumn(nullable: false)] private $provider; #[ORM\OneToMany(mappedBy: 'providerOrder', targetEntity: ProviderOrderItem::class, orphanRemoval: true, cascade: ['all'])] private $providerOrderItems; public function __construct() { parent::__construct(); $this->providerOrderItems = new ArrayCollection(); } public function getName(): string { return $this->reference; } public function getPriceUnities(): array { $priceUnities = []; foreach ($this->getProvider()->getPapers() as $paper) { $priceUnities[$paper->getId()] = $paper->getPriceUnity(); } return $priceUnities; } public function hasDeliveryDatePassed(): bool { return $this->getDeliveredEstimationAt() <= new \DateTimeImmutable(); } public function updatePapersPriceUnity(): self { foreach ($this->getProviderOrderItems() as $item) { if($item->getPriceUnityUpdated()) continue; $item->getPaper()->setPriceUnity($item->getPriceUnity()); $item->setPriceUnityUpdated(true); } return $this; } public function updateProviderOrderDelivery(): self { $productCount = 0; $productDeliveredCount = 0; foreach ($this->getProviderOrderItems() as $item) { // Update provider order items if (!$item->getDeliveryEdited()) $item->setDeliveredAt($this->getDeliveredEstimationAt()); $item->updateProviderOrderItemDelivery(); $productCount += $item->getProductCount(); $productDeliveredCount += $item->getProductDeliveredCount(); } $this->setProductCount($productCount); $this->setProductDeliveredCount($productDeliveredCount); $this->updateDeliveryProgress(); return $this; } public function updateProviderOrderDifference() { $price = 0; $productPrice = 0; $quantity = 0; $productQuantity = 0; foreach ($this->getProviderOrderItems() as $item) { $item->updateProviderOrderItemDifference(); $price += $item->getPrice(); $productPrice += $item->getProductPrice(); $quantity += $item->getQuantity(); $productQuantity += $item->getProductQuantity(); } $this->setPrice($price); $this->setProductPrice($productPrice); $this->setQuantity($quantity); $this->setProductQuantity($productQuantity); $this->updateDifference(); $this->setCompleted( $this->quantityDifference >= 0 ); } public function setDeliveredEstimationAt(\DateTimeImmutable $deliveredEstimationAt): self { $deliveredEstimationAt = $deliveredEstimationAt->setTime(12, 0); $this->deliveredEstimationAt = $deliveredEstimationAt; return $this; } // ##################################################################### // // ##################################################################### // // ##################################################################### // public function getId(): ?int { return $this->id; } public function getReference(): ?string { return $this->reference; } public function setReference(string $reference): self { $this->reference = $reference; return $this; } public function getOrderedAt(): ?\DateTimeImmutable { return $this->orderedAt; } public function setOrderedAt(\DateTimeImmutable $orderedAt): self { $this->orderedAt = $orderedAt; return $this; } public function getDeliveredEstimationAt(): ?\DateTimeImmutable { return $this->deliveredEstimationAt; } public function getCompleted(): ?bool { return $this->completed; } public function setCompleted(bool $completed): self { $this->completed = $completed; return $this; } public function getProvider(): ?Provider { return $this->provider; } public function setProvider(?Provider $provider): self { $this->provider = $provider; return $this; } /** * @return Collection|ProviderOrderItem[] */ public function getProviderOrderItems(): Collection { return $this->providerOrderItems; } public function addProviderOrderItem(ProviderOrderItem $providerOrderItem): self { if (!$this->providerOrderItems->contains($providerOrderItem)) { $this->providerOrderItems[] = $providerOrderItem; $providerOrderItem->setProviderOrder($this); } return $this; } public function removeProviderOrderItem(ProviderOrderItem $providerOrderItem): self { if ($this->providerOrderItems->removeElement($providerOrderItem)) { // set the owning side to null (unless already changed) if ($providerOrderItem->getProviderOrder() === $this) { $providerOrderItem->setProviderOrder(null); } } return $this; }}