<?php
declare(strict_types=1);
namespace App\Entity\Order;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Mezcalito\SyliusFileUploadPlugin\Model\FileInterface;
use Mezcalito\SyliusFileUploadPlugin\Model\FilesAwareTrait;
use Sylius\Component\Core\Model\OrderItem as BaseOrderItem;
use Mezcalito\SyliusFileUploadPlugin\Model\FilesAwareInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Entity
* @ORM\Table(name="sylius_order_item")
*/
class OrderItem extends BaseOrderItem implements OrderItemInterface, FilesAwareInterface
{
use FilesAwareTrait {
FilesAwareTrait::__construct as private initializeFilesCollection;
}
/**
* @ORM\Column(name="is_gift", type="boolean")
*/
private $gift = false;
/**
* @ORM\Column(name="note", type="text", nullable=true)
*/
private $note;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Order\OrderItemFileInterface", mappedBy="owner", orphanRemoval=true, cascade={"all"})
* @var Collection|FileInterface[]
*/
protected $files;
/**
* @var ArrayCollection|UploadedFile[]
*/
private $filesUploaded;
public function __construct()
{
parent::__construct();
$this->initializeFilesCollection();
$this->filesUploaded = new ArrayCollection();
}
public function isGift(): bool
{
return $this->gift;
}
public function setGift(bool $gift): void
{
$this->gift = $gift;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): void
{
$this->note = $note;
}
public function getFilesUploaded()
{
return $this->filesUploaded;
}
public function setFilesUploaded($filesUploaded): void
{
$this->filesUploaded = $filesUploaded;
}
public function removeFileUploaded(UploadedFile $uploadedFile): void
{
$this->filesUploaded->removeElement($uploadedFile);
}
public function setQuantity(?int $quantity): void
{
$this->quantity = $quantity;
$this->recalculateUnitsTotal();
}
public function recalculateUnitsTotal(): void
{
$total = $this->quantity * $this->unitPrice;
$variant = $this->getVariant();
$product = $variant->getProduct();
$brand = $product->getBrand();
$setupDefault = $variant->getOptionValueByCode('setup-charge-1');
$typeOptionValue = $variant->getOptionValueByOptionCode('type');
if ($brand && $brand->getSetupDefault() && $setupDefault) {
$total += ($brand->getSetupDefault() * 100000);
}
if ($brand && $brand->getPlatePerColor() && $variant->getOptionValueByCode('new-matrix') && $typeOptionValue) {
$colors = ['1-color' => 1, '2-color' => 2, '3-color' => 3, '4-color' => 4, '5-color' => 5, '6-color' => 6, '7-color' => 7, '8-color' => 8];
$total += ($brand->getPlatePerColor() * $colors[$typeOptionValue->getCode()] * 100000);
} else if ($brand && $brand->getPlateDefault() && $variant->getOptionValueByCode('new-matrix')) {
$total += ($brand->getPlateDefault() * 100000);
}
$this->unitsTotal = $total;
$this->recalculateTotal();
}
}