<?php
namespace App\Entity;
use App\Repository\ShoppingCartRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: ShoppingCartRepository::class)]
class ShoppingCart
{
use BlameableEntity; //Hook blameable behaviour. Updates createdBy, updatedBy fields
use TimestampableEntity; //Hook timestampable behaviour. Updates createdAt, updatedAt fields
const PAYMENT_CARD = 'PAYMENT_CARD';
const PAYMENT_PAYPAL = 'PAYMENT_PAYPAL';
const PAYMENT_BIZUM = 'PAYMENT_BIZUM';
const DELIVERY_HOME = 'DELIVERY_HOME';
const DELIVERY_PICKUP = 'DELIVERY_PICKUP';
const DELIVERY_HOME_GROUPED = 'DELIVERY_HOME_GROUPED';
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "IDENTITY")]
#[ORM\Column]
private ?int $id = null;
#[ORM\OneToOne(inversedBy: 'shoppingCart')]
#[ORM\JoinColumn(nullable: false)]
#[Assert\NotNull()]
private ?User $buyer = null;
#[ORM\OneToMany(mappedBy: 'header', targetEntity: ShoppingCartDetail::class, cascade: ['persist', 'remove'])]
#[Assert\Valid()]
#[ORM\OrderBy(['id' => 'ASC'])]
private Collection $shoppingCartDetails;
#[ORM\ManyToOne]
private ?Card $card = null;
#[ORM\Column(nullable: true)]
private ?bool $saveCard = false;
#[ORM\ManyToOne]
#[Assert\Expression(
"this.getBuyingGroupEntity() or this.getSharedShippingAddressEntity() or value",
)]
private ?Address $shippingAddressEntity = null;
#[ORM\ManyToOne(inversedBy: 'shoppingCart', targetEntity: BuyingGroup::class)]
private ?BuyingGroup $buyingGroupEntity = null;
#[ORM\ManyToOne]
private ?SharedAddress $sharedShippingAddressEntity = null;
#[ORM\Column]
#[Assert\NotNull()]
#[Assert\Expression(
"!value or (value and (this.getBuyingGroupEntity() or this.getShippingAddressEntity() or this.getSharedShippingAddressEntity()))",
)]
private ?bool $isBillingAddressSameAsShipping = true;
#[ORM\ManyToOne]
#[Assert\Expression(
"this.isIsBillingAddressSameAsShipping() or value",
)]
private ?Address $billingAddressEntity = null;
#[ORM\Column(length: 50, nullable: true)]
#[Assert\NotNull()]
private ?string $deliveryMethod = self::DELIVERY_HOME;
#[ORM\Column(nullable: true)]
private ?bool $useCoins = false;
#[ORM\Column(length: 50, nullable: true)]
private ?string $paymentMethod = null;
#[ORM\Column(length: 100, nullable: true)]
#[Assert\NotNull()]
#[Assert\Length(max: 100)]
private ?string $phone = null;
#[ORM\Column(nullable: true)]
private ?bool $needBill = false;
#[ORM\Column(length: 100, nullable: true)]
#[Assert\Expression(
"!this.getNeedBill() or value",
)]
#[Assert\Length(max: 100)]
private ?string $cif = null;
#[ORM\Column(nullable: true)]
private ?bool $buy_not_enought = null;
#[ORM\Column(nullable: true)]
private ?bool $better_price = null;
#[ORM\Column(nullable: true)]
private ?bool $isIndividualPurchase = null;
public function __construct()
{
$this->shoppingCartDetails = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getBuyer(): ?User
{
return $this->buyer;
}
public function setBuyer(User $buyer): self
{
$this->buyer = $buyer;
return $this;
}
/**
* @return Collection<int, ShoppingCartDetail>
*/
public function getShoppingCartDetails(): Collection
{
return $this->shoppingCartDetails;
}
public function addShoppingCartDetail(ShoppingCartDetail $shoppingCartDetail): self
{
if (!$this->shoppingCartDetails->contains($shoppingCartDetail)) {
$this->shoppingCartDetails->add($shoppingCartDetail);
$shoppingCartDetail->setHeader($this);
}
return $this;
}
public function removeShoppingCartDetail(ShoppingCartDetail $shoppingCartDetail): self
{
if ($this->shoppingCartDetails->removeElement($shoppingCartDetail)) {
// set the owning side to null (unless already changed)
if ($shoppingCartDetail->getHeader() === $this) {
$shoppingCartDetail->setHeader(null);
}
}
return $this;
}
public function getCard(): ?Card
{
return $this->card;
}
public function setCard(?Card $card): self
{
$this->card = $card;
return $this;
}
public function getSaveCard(): ?bool
{
return $this->saveCard;
}
public function setSaveCard(?bool $saveCard): self
{
$this->saveCard = $saveCard;
return $this;
}
public function getShippingAddressEntity(): ?Address
{
return $this->shippingAddressEntity;
}
public function setShippingAddressEntity(?Address $shippingAddressEntity): self
{
$this->shippingAddressEntity = $shippingAddressEntity;
return $this;
}
public function getSharedShippingAddressEntity(): ?SharedAddress
{
return $this->sharedShippingAddressEntity;
}
public function setSharedShippingAddressEntity(?SharedAddress $sharedShippingAddressEntity): self
{
$this->sharedShippingAddressEntity = $sharedShippingAddressEntity;
return $this;
}
public function getBuyingGroupEntity(): ?BuyingGroup
{
return $this->buyingGroupEntity;
}
public function setBuyingGroupEntity(?BuyingGroup $buyingGroupEntity): self
{
$this->buyingGroupEntity = $buyingGroupEntity;
return $this;
}
public function isIsBillingAddressSameAsShipping(): ?bool
{
return $this->isBillingAddressSameAsShipping;
}
public function setIsBillingAddressSameAsShipping(bool $isBillingAddressSameAsShipping): self
{
$this->isBillingAddressSameAsShipping = $isBillingAddressSameAsShipping;
return $this;
}
public function getBillingAddressEntity(): ?Address
{
return $this->billingAddressEntity;
}
public function setBillingAddressEntity(?Address $billingAddressEntity): self
{
$this->billingAddressEntity = $billingAddressEntity;
return $this;
}
public function getDeliveryMethod(): ?string
{
return $this->deliveryMethod;
}
public function setDeliveryMethod(?string $deliveryMethod): self
{
$this->deliveryMethod = $deliveryMethod;
return $this;
}
public function isUseCoins(): ?bool
{
return $this->useCoins;
}
public function setUseCoins(bool $useCoins): self
{
$this->useCoins = $useCoins;
return $this;
}
public function getPaymentMethod(): ?string
{
return $this->paymentMethod;
}
public function setPaymentMethod(?string $paymentMethod): self
{
$this->paymentMethod = $paymentMethod;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getNeedBill(): ?bool
{
return $this->needBill;
}
public function setNeedBill(?bool $needBill): self
{
$this->needBill = $needBill;
return $this;
}
public function getCif(): ?string
{
return $this->cif;
}
public function setCif(?string $cif): self
{
$this->cif = $cif;
return $this;
}
public function isBuyNotEnought(): ?bool
{
return $this->buy_not_enought;
}
public function setBuyNotEnought(?bool $buy_not_enought): self
{
$this->buy_not_enought = $buy_not_enought;
return $this;
}
public function isBetterPrice(): ?bool
{
return $this->better_price;
}
public function setBetterPrice(?bool $better_price): self
{
$this->better_price = $better_price;
return $this;
}
public function isIndividualPurchase(): ?bool
{
return $this->isIndividualPurchase;
}
public function setIsIndividualPurchase(bool $isIndividualPurchase): self
{
$this->isIndividualPurchase = $isIndividualPurchase;
return $this;
}
public function getDeliveryDate($shoppingCart, $product)
{
$homeDeliveryDate = "";
$individualDeliveryDate = $product->getIndividualShippingDuration() ? date_modify(new \DateTime('now'), "+{$product->getIndividualShippingDuration()}weekdays")->format('d-m-Y') : '';
$sharedDeliveryDate = (
(!$product->hasBeenPurchasedRecently() && $product->getSharedShippingDuration())
? date_modify(new \DateTime('now'), "+{$product->getSharedShippingDuration()}weekdays")
: $product->getShippingDate()
)->format('d-m-Y');
if ($shoppingCart->isIndividualPurchase()) {
$homeDeliveryDate = $individualDeliveryDate;
} else {
$homeDeliveryDate = $sharedDeliveryDate;
}
return $homeDeliveryDate;
}
}