<?php
namespace App\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Validator\Constraints as Assert;
use \App\Entity\User;
use \App\Entity\Media;
use \App\Entity\Order;
use \App\Entity\Address;
use \App\Repository\BuyingGroupRepository;
#[ORM\Entity(repositoryClass: BuyingGroupRepository::class)]
class BuyingGroup
{
use BlameableEntity; //Hook blameable behaviour. Updates createdBy, updatedBy fields
use TimestampableEntity; //Hook timestampable behaviour. Updates createdAt, updatedAt fields
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "IDENTITY")]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'buyingGroups')]
#[ORM\JoinColumn(nullable: false)]
private ?User $mainUser = null;
#[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'buyingGroups')]
private Collection $members;
#[ORM\Column(length: 100)]
#[Assert\NotNull()]
#[Assert\Length(max: 100)]
private ?string $name = null;
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: true)]
private ?Media $image = null;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private ?bool $isCorporate = null;
#[ORM\Column(length: 100, nullable: true)]
#[Assert\Length(max: 100)]
private ?string $corporateContactName = null;
#[ORM\Column(length: 100, nullable: true)]
#[Assert\Length(max: 100)]
private ?string $corporateContactEmail = null;
#[ORM\Column(length: 100, nullable: true)]
#[Assert\Length(max: 100)]
private ?string $corporateName = null;
#[ORM\Column(length: 20, nullable: true)]
#[Assert\Length(max: 20)]
private ?string $corporateVatNumber = null;
#[ORM\ManyToOne(inversedBy: 'buyingGroups', cascade:["persist"])]
#[Assert\Valid()]
// #[Assert\NotNull()]
private ?Address $address = null;
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $whitelistedDomains = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private ?bool $membersCanInvite = null;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private ?bool $freeShipping = false;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private ?bool $openGroup = false;
#[ORM\OneToMany(mappedBy: 'buyingGroupEntity', targetEntity: ShoppingCart::class)]
private ?Collection $shoppingCart = null;
#[ORM\OneToMany(mappedBy: 'buyingGroup', targetEntity: Order::class)]
private ?Collection $orders = null;
// #[ORM\Column(length: 255)]
// private ?string $address = null;
// #[ORM\Column(length: 20)]
// private ?string $postCode = null;
// #[ORM\Column(length: 255)]
// private ?string $city = null;
// #[ORM\ManyToOne]
// #[ORM\JoinColumn(nullable: false)]
// private ?Province $state = null;
public function __construct()
{
$this->members = new ArrayCollection();
$this->orders = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getMainUser(): ?User
{
return $this->mainUser;
}
public function setMainUser(?User $mainUser): self
{
$this->mainUser = $mainUser;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getImage(): ?Media
{
return $this->image;
}
public function setImage(Media $image): self
{
$image->setDirectory('buyingGroup');
$this->image = $image;
return $this;
}
public function isIsCorporate(): ?bool
{
return $this->isCorporate;
}
public function setIsCorporate(bool $isCorporate): self
{
$this->isCorporate = $isCorporate;
return $this;
}
public function getCorporateContactName(): ?string
{
return $this->corporateContactName;
}
public function setCorporateContactName(?string $corporateContactName): self
{
$this->corporateContactName = $corporateContactName;
return $this;
}
public function getCorporateContactEmail(): ?string
{
return $this->corporateContactEmail;
}
public function setCorporateContactEmail(?string $corporateContactEmail): self
{
$this->corporateContactEmail = $corporateContactEmail;
return $this;
}
public function getCorporateName(): ?string
{
return $this->corporateName;
}
public function setCorporateName(?string $corporateName): self
{
$this->corporateName = $corporateName;
return $this;
}
public function getCorporateVatNumber(): ?string
{
return $this->corporateVatNumber;
}
public function setCorporateVatNumber(?string $corporateVatNumber): self
{
$this->corporateVatNumber = $corporateVatNumber;
return $this;
}
// public function getAddress(): ?string
// {
// return $this->address;
// }
// public function setAddress(string $address): self
// {
// $this->address = $address;
// return $this;
// }
// public function getPostCode(): ?string
// {
// return $this->postCode;
// }
// public function setPostCode(string $postCode): self
// {
// $this->postCode = $postCode;
// return $this;
// }
// public function getCity(): ?string
// {
// return $this->city;
// }
// public function setCity(string $city): self
// {
// $this->city = $city;
// return $this;
// }
// public function getState(): ?Province
// {
// return $this->state;
// }
// public function setState(?Province $state): self
// {
// $this->state = $state;
// return $this;
// }
public function getAddress(): ?Address
{
return $this->address;
}
public function setAddress(?Address $address): self
{
$this->address = $address;
return $this;
}
public function getWhitelistedDomains(): ?string
{
return $this->whitelistedDomains;
}
public function setWhitelistedDomains(?string $whitelistedDomains): self
{
$this->whitelistedDomains = $whitelistedDomains;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getFreeShipping(): bool
{
return $this->freeShipping;
}
public function setFreeShipping(bool $freeShipping): self
{
$this->freeShipping = $freeShipping;
return $this;
}
public function getOpenGroup(): bool
{
return $this->openGroup;
}
public function setOpenGroup(bool $openGroup): self
{
$this->openGroup = $openGroup;
return $this;
}
public function isMembersCanInvite(): ?bool
{
return $this->membersCanInvite;
}
public function setMembersCanInvite(bool $membersCanInvite): self
{
$this->membersCanInvite = $membersCanInvite;
return $this;
}
public function getMembers(): ?Collection
{
return $this->members;
}
public function addMember(User $member): self
{
if (!$this->members->contains($member)) {
$this->members->add($member);
}
return $this;
}
public function removeMember(User $member): self
{
if ($this->members->removeElement($member)) {
$member->removeBuyingGroup($this);
}
return $this;
}
/**
* @return Collection<int, Order>
*/
public function getOrders(): ?Collection
{
return $this->orders;
}
/**
* @return Collection<int, Order>
*/
public function getPendingOrdersForProducts(Collection $productsList): ?Collection
{
return $this->orders->filter(function($o) use ($productsList) {
if (!in_array($o->getStatus()->getKey(), [OrderStatus::MONEY_WITHHELD, OrderStatus::BANK_TRANSFER])) {
return false;
}
return $o->getOrderDetails()->filter(fn($i) => in_array($i->getProduct()->getId(), $productsList->toArray()))->count() > 0;
});
}
/**
* @return Collection<int, Order>
*/
public function getPendingOrdersForProduct(Product $product): ?Collection
{
return $this->orders->filter(fn($o) =>
$o->getStatus()->getKey() !== OrderStatus::MONEY_WITHHELD
? false
: $o->getOrderDetails()->filter(fn($d) => $d->getProduct()->getId() === $product->getId())->count() > 0
);
}
public function addOrder(Order $order): self
{
if (!$this->orders->contains($order)) {
$this->orders->add($order);
$order->setBuyingGroup($this);
}
return $this;
}
public function removeOrder(Order $order): self
{
if ($this->orders->removeElement($order)) {
// set the owning side to null (unless already changed)
if ($order->getBuyingGroup() === $this) {
$order->setBuyingGroup(null);
}
}
return $this;
}
}