<?php
namespace App\Entity\OnlineShop;
use App\Annotation\SiteAware;
use App\Entity\AbstractBase;
use App\Entity\Interfaces\PublishedInterface;
use App\Entity\Interfaces\SiteInterface;
use App\Entity\MiniAbstractBase;
use App\Entity\Traits\HasImageTrait;
use App\Entity\Traits\PhoneTrait;
use App\Entity\Traits\PublishedTrait;
use App\Entity\Traits\SiteTrait;
use App\Entity\User;
use App\Enum\DocumentTypeEnum;
use App\Enum\MinOrderTypeEnum;
use App\Enum\NotInMinOrderTypeEnum;
use App\Enum\ShipmentTypeEnum;
use App\Repository\OnlineShop\SupplierRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Money\Money;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Table(name="vulco_supplier", indexes={@ORM\Index(name="supplier_site_idx", columns={"site"})})
*
* @ORM\Entity(repositoryClass=SupplierRepository::class)
*
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
*
* @SiteAware(siteFieldName="site")
*
* @Vich\Uploadable
*/
class Supplier extends AbstractBase implements SiteInterface, PublishedInterface
{
use HasImageTrait;
use PhoneTrait;
use PublishedTrait;
use SiteTrait;
/**
* @ORM\Column(type="string", length=255)
*/
private string $legalName;
/**
* @ORM\Column(type="string", length=255)
*/
private string $commercialName;
/**
* @ORM\Column(type="text", length=10000)
*/
private string $address;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $fax = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $website = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $contactPerson = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $contactEmail = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $orderEmail = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $orderCcEmail = null;
/**
* @ORM\Column(type="text", length=10000, nullable=true)
*/
private ?string $imageUrl = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $deliveryTime = null;
/**
* @ORM\Column(type="integer")
*/
private int $shipmentType;
/**
* @ORM\Column(type="money")
*/
private Money $fixedShipmentCost;
/**
* @ORM\Column(type="integer")
*/
private int $fixedShipmentMinOrder;
/**
* @ORM\Column(type="integer")
*/
private int $fixedShipmentMinOrderType;
/**
* @ORM\Column(type="integer")
*/
private int $fixedShipmentToDoNotInMinOrderType;
/**
* @ORM\Column(type="integer")
*/
private int $scaleShipmentMinPointsToBlockOrder;
/**
* @ORM\Column(type="integer")
*/
private int $scaleShipmentMinPointsToFreeShipment;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $imageName = null;
/**
* @ORM\OneToMany(targetEntity="App\Entity\OnlineShop\ScaleShippingCost", mappedBy="supplier", cascade={"persist", "remove"}, orphanRemoval=true)
*
* @ORM\OrderBy({"points": "ASC"})
*/
private ?Collection $scaleShippingCosts;
/**
* @ORM\OneToMany(targetEntity="App\Entity\OnlineShop\SupplierDocument", mappedBy="supplier", cascade={"persist", "remove"}, orphanRemoval=true, fetch="EAGER")
*
* @ORM\OrderBy({"createdAt": "DESC"})
*/
private ?Collection $documents;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="suppliers")
*
* @ORM\JoinColumn(name="user_id", referencedColumnName="id"))
*/
private ?User $user = null;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": 1})
*/
private bool $rappelCentralVulco = true;
/**
* @ORM\Column(type="decimal", precision=10, scale=2)
*/
private $rappelPercentage;
/**
* @ORM\Column(type="text", length=10000, nullable=true)
*/
private ?string $rappelText = null;
/**
* @Vich\UploadableField(mapping="supplier_image", fileNameProperty="imageName")
*/
private ?File $image = null;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\OnlineShop\SupplierCategory")
*
* @ORM\JoinTable(name="vulco_supplier_supplier_category",
* joinColumns={@ORM\JoinColumn(name="supplier_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="supplier_category_id", referencedColumnName="id")}
* )
*/
private ?Collection $categories;
public function __construct()
{
$this->scaleShippingCosts = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->documents = new ArrayCollection();
}
public function getLegalName(): string
{
return $this->legalName;
}
public function setLegalName(string $legalName): self
{
$this->legalName = $legalName;
return $this;
}
public function getCommercialName(): string
{
return $this->commercialName;
}
public function setCommercialName(string $commercialName): self
{
$this->commercialName = $commercialName;
return $this;
}
public function getAddress(): string
{
return $this->address;
}
public function setAddress(string $address): self
{
$this->address = $address;
return $this;
}
public function getFax(): ?string
{
return $this->fax;
}
public function setFax(?string $fax): self
{
$this->fax = $fax;
return $this;
}
public function getWebsite(): ?string
{
return $this->website;
}
public function setWebsite(?string $website): self
{
$this->website = $website;
return $this;
}
public function getContactPerson(): ?string
{
return $this->contactPerson;
}
public function setContactPerson(?string $contactPerson): self
{
$this->contactPerson = $contactPerson;
return $this;
}
public function getContactEmail(): ?string
{
return $this->contactEmail;
}
public function setContactEmail(?string $contactEmail): self
{
$this->contactEmail = $contactEmail;
return $this;
}
public function getOrderEmail(): ?string
{
return $this->orderEmail;
}
public function setOrderEmail(?string $orderEmail): self
{
$this->orderEmail = $orderEmail;
return $this;
}
public function getOrderCcEmail(): ?string
{
return $this->orderCcEmail;
}
public function setOrderCcEmail(?string $orderCcEmail): self
{
$this->orderCcEmail = $orderCcEmail;
return $this;
}
public function getImageUrl(): ?string
{
return $this->imageUrl;
}
public function setImageUrl(?string $imageUrl): self
{
$this->imageUrl = $imageUrl;
return $this;
}
public function getDeliveryTime(): ?string
{
return $this->deliveryTime;
}
public function setDeliveryTime(?string $deliveryTime): self
{
$this->deliveryTime = $deliveryTime;
return $this;
}
public function getShipmentType(): int
{
return $this->shipmentType;
}
public function getShipmentTypeAsString(): string
{
return ShipmentTypeEnum::getReversedTranslations()[$this->getShipmentType()];
}
public function setShipmentType(int $shipmentType): self
{
$this->shipmentType = $shipmentType;
return $this;
}
public function getFixedShipmentCost(): Money
{
return $this->fixedShipmentCost;
}
public function setFixedShipmentCost(Money $fixedShipmentCost): self
{
$this->fixedShipmentCost = $fixedShipmentCost;
return $this;
}
public function getFixedShipmentMinOrder(): int
{
return $this->fixedShipmentMinOrder;
}
public function setFixedShipmentMinOrder(int $fixedShipmentMinOrder): self
{
$this->fixedShipmentMinOrder = $fixedShipmentMinOrder;
return $this;
}
public function getFixedShipmentMinOrderType(): int
{
return $this->fixedShipmentMinOrderType;
}
public function getFixedShipmentMinOrderTypeAsString(): string
{
return MinOrderTypeEnum::getReversedTranslations()[$this->getFixedShipmentMinOrderType()];
}
public function setFixedShipmentMinOrderType(int $fixedShipmentMinOrderType): self
{
$this->fixedShipmentMinOrderType = $fixedShipmentMinOrderType;
return $this;
}
public function getFixedShipmentToDoNotInMinOrderType(): int
{
return $this->fixedShipmentToDoNotInMinOrderType;
}
public function getFixedShipmentToDoNotInMinOrderTypeAsString(): string
{
return NotInMinOrderTypeEnum::getReversedTranslations()[$this->getFixedShipmentToDoNotInMinOrderType()];
}
public function setFixedShipmentToDoNotInMinOrderType(int $fixedShipmentToDoNotInMinOrderType): self
{
$this->fixedShipmentToDoNotInMinOrderType = $fixedShipmentToDoNotInMinOrderType;
return $this;
}
public function getScaleShipmentMinPointsToBlockOrder(): int
{
return $this->scaleShipmentMinPointsToBlockOrder;
}
public function setScaleShipmentMinPointsToBlockOrder(int $scaleShipmentMinPointsToBlockOrder): self
{
$this->scaleShipmentMinPointsToBlockOrder = $scaleShipmentMinPointsToBlockOrder;
return $this;
}
public function getScaleShipmentMinPointsToFreeShipment(): int
{
return $this->scaleShipmentMinPointsToFreeShipment;
}
public function setScaleShipmentMinPointsToFreeShipment(int $scaleShipmentMinPointsToFreeShipment): self
{
$this->scaleShipmentMinPointsToFreeShipment = $scaleShipmentMinPointsToFreeShipment;
return $this;
}
public function getScaleShippingCosts(): ?Collection
{
return $this->scaleShippingCosts;
}
public function setScaleShippingCosts(?Collection $scaleShippingCosts): self
{
$this->scaleShippingCosts = $scaleShippingCosts;
return $this;
}
public function addScaleShippingCost(ScaleShippingCost $scaleShippingCost): self
{
if (!$this->scaleShippingCosts->contains($scaleShippingCost)) {
$scaleShippingCost->setSupplier($this);
$this->scaleShippingCosts->add($scaleShippingCost);
}
return $this;
}
public function removeScaleShippingCost(ScaleShippingCost $scaleShippingCost): self
{
if ($this->scaleShippingCosts->contains($scaleShippingCost)) {
$this->scaleShippingCosts->removeElement($scaleShippingCost);
}
return $this;
}
public function getDocuments(): ?Collection
{
return $this->documents;
}
public function setDocuments(?Collection $documents): self
{
$this->documents = $documents;
return $this;
}
public function addDocument(SupplierDocument $document): self
{
if (!$this->documents->contains($document)) {
$document->setSupplier($this);
$this->documents->add($document);
}
return $this;
}
public function addPromotionDocument(SupplierDocument $document): self
{
return $this->addDocument($document);
}
public function addConditionRateDocument(SupplierDocument $document): self
{
return $this->addDocument($document);
}
public function addWarrantyDocument(SupplierDocument $document): self
{
return $this->addDocument($document);
}
public function addRateDocument(SupplierDocument $document): self
{
return $this->addDocument($document);
}
public function addCatalogDocument(SupplierDocument $document): self
{
return $this->addDocument($document);
}
public function removeDocument(SupplierDocument $document): self
{
if ($this->documents->contains($document)) {
$this->documents->removeElement($document);
}
return $this;
}
public function removePromotionDocument(SupplierDocument $document): self
{
return $this->removeDocument($document);
}
public function removeConditionRateDocument(SupplierDocument $document): self
{
return $this->removeDocument($document);
}
public function removeWarrantyDocument(SupplierDocument $document): self
{
return $this->removeDocument($document);
}
public function removeRateDocument(SupplierDocument $document): self
{
return $this->removeDocument($document);
}
public function removeCatalogDocument(SupplierDocument $document): self
{
return $this->removeDocument($document);
}
public function getCategories(): ?Collection
{
return $this->categories;
}
public function setCategories(?Collection $categories): self
{
$this->categories = $categories;
return $this;
}
public function addCategorie(SupplierCategory $category): self
{
if (!$this->categories->contains($category)) {
$this->categories->add($category);
}
return $this;
}
public function removeCategorie(SupplierCategory $category): self
{
if ($this->categories->contains($category)) {
$this->categories->removeElement($category);
}
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setRappelCentralVulco(bool $rappelCentralVulco): self
{
$this->rappelCentralVulco = $rappelCentralVulco;
return $this;
}
public function isRappelCentralVulco(): bool
{
return $this->rappelCentralVulco;
}
public function isRappelDirectSupplier(): bool
{
return !$this->isRappelCentralVulco();
}
public function getRappelPercentage(): float
{
return $this->rappelPercentage;
}
public function setRappelPercentage(float $rappelPercentage): self
{
$this->rappelPercentage = $rappelPercentage;
return $this;
}
public function getRappelText(): ?string
{
return $this->rappelText;
}
public function setRappelText(?string $rappelText): self
{
$this->rappelText = $rappelText;
return $this;
}
public function setFeatured(bool $featured): self
{
$this->featured = $featured;
return $this;
}
public function getPromotionDocuments(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::PROMOTION);
}
public function getPromotionDocumentsEnabled(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::PROMOTION, true);
}
public function getConditionRateDocuments(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::CONDITION_RATE);
}
public function getConditionRateDocumentsEnabled(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::CONDITION_RATE, true);
}
public function getWarrantyDocuments(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::WARRANTY);
}
public function getWarrantyDocumentsEnabled(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::WARRANTY, true);
}
public function getRateDocuments(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::RATE);
}
public function getRateDocumentsEnabled(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::RATE, true);
}
public function getRateDocumentsEnabledAndCurrentDate(): Collection
{
$result = new ArrayCollection();
$enabledSupplierDocuments = $this->filterDocumentsByType(DocumentTypeEnum::RATE, true);
$today = (new \DateTimeImmutable())->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT);
/** @var SupplierDocument $document */
foreach ($enabledSupplierDocuments as $document) {
if ($document->getStartDate() && $document->getEndDate() && $today >= $document->getStartDate()->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT) && $today <= $document->getEndDate()->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT)) {
$result->add($document);
} elseif ($document->getStartDate() && !$document->getEndDate() && $today >= $document->getStartDate()->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT)) {
$result->add($document);
} elseif (!$document->getStartDate() && $document->getEndDate() && $today <= $document->getEndDate()->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT)) {
$result->add($document);
}
}
return $result;
}
public function getCatalogDocuments(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::CATALOG);
}
public function getCatalogDocumentsEnabled(): Collection
{
return $this->filterDocumentsByType(DocumentTypeEnum::CATALOG, true);
}
protected function filterDocumentsByType(int $type, bool $enabled = false): Collection
{
$result = new ArrayCollection();
/** @var SupplierDocument $document */
foreach ($this->documents as $document) {
if ($type === $document->getDocumentType()) {
if ($enabled) {
if ($document->isEnabled()) {
$result->add($document);
}
} else {
$result->add($document);
}
}
}
return $result;
}
public function __toString(): string
{
return $this->commercialName ? $this->getCommercialName() : MiniAbstractBase::DEFAULT_EMPTY_STRING;
}
}