src/Flexy/ShopBundle/Entity/Order/OrderItem.php line 27
<?php
namespace App\Flexy\ShopBundle\Entity\Order;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Flexy\ShopBundle\Entity\Customer\PackEngagement;
use App\Flexy\ShopBundle\Entity\Product\Product;
use App\Flexy\ShopBundle\Entity\Shipping\Shipment;
use App\Flexy\ShopBundle\Entity\Shipping\ShipmentItem;
use App\Flexy\ShopBundle\Repository\Order\OrderItemRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\InheritanceType;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: OrderItemRepository::class)]
#[InheritanceType('JOINED')]
#[ApiResource(
normalizationContext: ['groups' => ['read','readPackEngagements']],
denormalizationContext: ['groups' => ['write']],
)]
class OrderItem implements \Stringable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['read', 'readDeep'])]
private $id;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column(type: 'text', nullable: true)]
#[Groups(['read', 'readDeep'])]
private ?string $description = null;
#[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'orderItems', cascade: ['persist'])]
#[Groups(['read', 'readDeep'])]
private ?\App\Flexy\ShopBundle\Entity\Product\Product $product = null;
#[ORM\Column(type: 'integer')]
#[Groups(['read', 'readDeep'])]
private ?int $quantity = null;
#[ORM\Column(type: 'float', nullable: true)]
#[Groups(['read', 'readDeep'])]
private ?float $decimalQuantity = null;
#[ORM\Column(type: 'float')]
#[Groups(['read', 'readDeep'])]
private ?float $price = null;
#[ORM\ManyToOne(targetEntity: Order::class, inversedBy: 'orderItems', cascade: ['persist'])]
private ?\App\Flexy\ShopBundle\Entity\Order\Order $parentOrder = null;
#[ORM\Column(type: 'float', nullable: true)]
private int|float|null $reduction = 0;
#[ORM\OneToMany(targetEntity: OrderItemNote::class, mappedBy: 'orderItem', cascade: ['persist', 'remove'])]
private \Doctrine\Common\Collections\Collection|array $orderItemNotes;
#[ORM\OneToOne(targetEntity: PackEngagement::class, inversedBy: 'orderItem', cascade: ['persist', 'remove'])]
private ?\App\Flexy\ShopBundle\Entity\Customer\PackEngagement $packEngagement = null;
#[ORM\OneToOne(targetEntity: ShipmentItem::class, inversedBy: 'orderItem', cascade: ['persist', 'remove'] )]
private $shipmentItem;
#[ORM\Column(length: 255, nullable: true)]
private ?string $image = null;
#[ORM\Column(nullable: true)]
private ?float $shippingFees = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $currency = null;
#[ORM\Column(nullable: true)]
private ?array $metaData = null;
public function __construct()
{
}
public function __toString(): string
{
return $this->getDescription()."(". $this->getPrice() ."DH x".$this->getQuantity().")";
}
public function getName()
{
$name = "Undefined Name";
if($this->packEngagement){
$name = $this->packEngagement->getPack()->getTitle();
}elseif($this->product){
$name = $this->product->getName();
}
return $name;
}
public function getId(): ?int
{
return $this->id;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getParentOrder(): ?Order
{
return $this->parentOrder;
}
public function setParentOrder(?Order $parentOrder): self
{
$this->parentOrder = $parentOrder;
return $this;
}
public function getFormattedPrice(): ?float
{
return $this->price/100;
}
public function getTotalAmount(): ?float
{
$price = $this->getFinalPrice();
if($this->getProduct() and !$price){
$price = $this->getProduct()->getFinalPrice();
return $price * $this->quantity;
}
if($this->decimalQuantity and (float)$this->decimalQuantity >= 0){
return (($price - $this->getReduction()) * $this->decimalQuantity);
}
return $price * $this->quantity;
}
#[Groups(['read', 'readDeep'])]
public function getFinalPrice(): ?float
{
$price = $this->price;
$reduction = 0;
if((float)$this->getReduction() > 0){
$reduction = $this->getReduction();
}elseif($this->getProduct()){
if($this->getProduct()->getReduction()){
$reduction = $this->getProduct()->getReduction();
}
}
if($this->getProduct()){
if($this->getProduct()->getPrice() > $this->getPrice()){
return $this->getPrice();
}
}
$price = $price - $reduction;
return $price;
}
public function getReduction(): ?float
{
return (float)$this->reduction;
}
public function setReduction(?float $reduction): self
{
$this->reduction = $reduction;
return $this;
}
/**
* @return Collection<int, OrderItemNote>
*/
public function getOrderItemNotes(): Collection
{
return $this->orderItemNotes;
}
public function addOrderItemNote(OrderItemNote $orderItemNote): self
{
if (!$this->orderItemNotes->contains($orderItemNote)) {
$this->orderItemNotes[] = $orderItemNote;
$orderItemNote->setOrderItem($this);
}
return $this;
}
public function removeOrderItemNote(OrderItemNote $orderItemNote): self
{
if ($this->orderItemNotes->removeElement($orderItemNote)) {
// set the owning side to null (unless already changed)
if ($orderItemNote->getOrderItem() === $this) {
$orderItemNote->setOrderItem(null);
}
}
return $this;
}
public function getPackEngagement(): ?PackEngagement
{
return $this->packEngagement;
}
public function setPackEngagement(?PackEngagement $packEngagement): self
{
$this->packEngagement = $packEngagement;
return $this;
}
/**
* Get the value of shipmentItem
*/
public function getShipmentItem()
{
return $this->shipmentItem;
}
/**
* Set the value of shipmentItem
*
* @return self
*/
public function setShipmentItem($shipmentItem)
{
$this->shipmentItem = $shipmentItem;
return $this;
}
public function getImage(): ?string
{
if($this->getProduct()){
return $this->getProduct()->getImage();
}
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
#[Groups(['readPackEngagements'])]
public function getOrderNumber(){
return $this->getParentOrder()->getOrderNumber();
}
public function getShippingFees(): ?float
{
return $this->shippingFees;
}
public function setShippingFees(?float $shippingFees): static
{
$this->shippingFees = $shippingFees;
return $this;
}
public function getCurrency(): ?string
{
return $this->currency;
}
public function setCurrency(?string $currency): static
{
$this->currency = $currency;
return $this;
}
public function getMetaData(): ?array
{
return $this->metaData;
}
public function setMetaData(?array $metaData): static
{
$this->metaData = $metaData;
return $this;
}
/**
* Get the value of decimalQuantity
*/
public function getDecimalQuantity()
{
return $this->decimalQuantity;
}
/**
* Set the value of decimalQuantity
*
* @return self
*/
public function setDecimalQuantity($decimalQuantity)
{
$this->decimalQuantity = $decimalQuantity;
return $this;
}
}