<?php
namespace App\Entity;
use App\Repository\MagasinRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: MagasinRepository::class)]
class Magasin
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 150)]
private ?string $nom = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $logo_path = null;
#[ORM\Column(nullable: true)]
private ?bool $active = null;
// ── Subscription: shop is live on the storefront until this date ──
#[ORM\Column(name: 'subscription_paid_until', type: 'date', nullable: true)]
private ?\DateTimeInterface $subscriptionPaidUntil = null;
// ── location ─────────────────────────────────────────────────
#[ORM\ManyToOne(targetEntity: Country::class)]
#[ORM\JoinColumn(name: 'country_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
private ?Country $country = null;
#[ORM\ManyToOne(targetEntity: Region::class)]
#[ORM\JoinColumn(name: 'region_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
private ?Region $region = null;
#[ORM\ManyToOne(targetEntity: City::class)]
#[ORM\JoinColumn(name: 'city_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
private ?City $city = null;
// ── seller / invoice details (legal info shown on invoices) ──
#[ORM\Column(name: 'adresse', length: 255, nullable: true)]
private ?string $adresse = null;
#[ORM\Column(name: 'telephone', length: 50, nullable: true)]
private ?string $telephone = null;
/** Commercial Register number (CR / Registre du Commerce). */
#[ORM\Column(name: 'numero_cr', length: 100, nullable: true)]
private ?string $numeroCR = null;
/** VAT registration number (TVA). */
#[ORM\Column(name: 'numero_tva', length: 100, nullable: true)]
private ?string $numeroTVA = null;
// ── delivery fee matrix ──────────────────────────────────────
/** @var Collection<int, MagasinDeliveryFee> */
#[ORM\OneToMany(mappedBy: 'magasin', targetEntity: MagasinDeliveryFee::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $deliveryFees;
#[ORM\OneToMany(mappedBy: 'magasin', targetEntity: MagasinUser::class)]
private Collection $magasinsUser;
#[ORM\OneToMany(mappedBy: 'magasin', targetEntity: Produit::class)]
private Collection $produits;
public function __construct()
{
$this->user_id = new ArrayCollection();
$this->users = new ArrayCollection();
$this->magasinsUser = new ArrayCollection();
$this->produits = new ArrayCollection();
$this->deliveryFees = new ArrayCollection();
}
public function getId(): ?int { return $this->id; }
public function getNom(): ?string { return $this->nom; }
public function setNom(string $nom): static { $this->nom = $nom; return $this; }
public function getLogoPath(): ?string { return $this->logo_path; }
public function setLogoPath(?string $logo_path): static { $this->logo_path = $logo_path; return $this; }
public function isActive(): ?bool { return $this->active; }
public function setActive(?bool $active): static { $this->active = $active; return $this; }
// ── Subscription ─────────────────────────────────────────────
public function getSubscriptionPaidUntil(): ?\DateTimeInterface { return $this->subscriptionPaidUntil; }
public function setSubscriptionPaidUntil(?\DateTimeInterface $d): static { $this->subscriptionPaidUntil = $d; return $this; }
/** Visible on the storefront: must be active and, if a date is set, not expired. */
public function isLive(): bool
{
if ($this->active !== true) { return false; }
if ($this->subscriptionPaidUntil === null) { return true; } // grandfathered / no expiry tracked
return $this->subscriptionPaidUntil >= new \DateTime('today');
}
/** Days remaining; null if no date set, negative if already expired. */
public function daysLeft(): ?int
{
if ($this->subscriptionPaidUntil === null) { return null; }
$diff = (new \DateTime('today'))->diff($this->subscriptionPaidUntil);
return $diff->invert === 1 ? -((int) $diff->days) : (int) $diff->days;
}
// ── location getters/setters ─────────────────────────────────
public function getCountry(): ?Country { return $this->country; }
public function setCountry(?Country $country): static { $this->country = $country; return $this; }
public function getRegion(): ?Region { return $this->region; }
public function setRegion(?Region $region): static { $this->region = $region; return $this; }
public function getCity(): ?City { return $this->city; }
public function setCity(?City $city): static { $this->city = $city; return $this; }
// ── seller / invoice details getters/setters ─────────────────
public function getAdresse(): ?string { return $this->adresse; }
public function setAdresse(?string $adresse): static { $this->adresse = $adresse; return $this; }
public function getTelephone(): ?string { return $this->telephone; }
public function setTelephone(?string $telephone): static { $this->telephone = $telephone; return $this; }
public function getNumeroCR(): ?string { return $this->numeroCR; }
public function setNumeroCR(?string $numeroCR): static { $this->numeroCR = $numeroCR; return $this; }
public function getNumeroTVA(): ?string { return $this->numeroTVA; }
public function setNumeroTVA(?string $numeroTVA): static { $this->numeroTVA = $numeroTVA; return $this; }
// ── delivery fees ────────────────────────────────────────────
/** @return Collection<int, MagasinDeliveryFee> */
public function getDeliveryFees(): Collection { return $this->deliveryFees; }
public function addDeliveryFee(MagasinDeliveryFee $fee): static
{
if (!$this->deliveryFees->contains($fee)) {
$this->deliveryFees->add($fee);
$fee->setMagasin($this);
}
return $this;
}
public function removeDeliveryFee(MagasinDeliveryFee $fee): static
{
if ($this->deliveryFees->removeElement($fee)) {
if ($fee->getMagasin() === $this) {
$fee->setMagasin(null);
}
}
return $this;
}
/** Convenience: returns the fee row for a given region, or null if none exists yet. */
public function getDeliveryFeeForRegion(Region $region): ?MagasinDeliveryFee
{
foreach ($this->deliveryFees as $f) {
if ($f->getRegion() && $f->getRegion()->getId() === $region->getId()) {
return $f;
}
}
return null;
}
/** Convenience: true if shop has explicitly enabled delivery to this region. */
public function canDeliverTo(Region $region): bool
{
$f = $this->getDeliveryFeeForRegion($region);
return $f !== null && $f->canDeliver();
}
public function __toString(): string
{
return $this->nom ?? '';
}
/** @return Collection<int, MagasinUser> */
public function getMagasinsUser(): Collection { return $this->magasinsUser; }
public function addMagasinsUser(MagasinUser $magasinsUser): static
{
if (!$this->magasinsUser->contains($magasinsUser)) {
$this->magasinsUser->add($magasinsUser);
$magasinsUser->setMagasin($this);
}
return $this;
}
public function removeMagasinsUser(MagasinUser $magasinsUser): static
{
if ($this->magasinsUser->removeElement($magasinsUser)) {
if ($magasinsUser->getMagasin() === $this) {
$magasinsUser->setMagasin(null);
}
}
return $this;
}
/** @return Collection<int, Produit> */
public function getProduits(): Collection { return $this->produits; }
public function addProduit(Produit $produit): static
{
if (!$this->produits->contains($produit)) {
$this->produits->add($produit);
$produit->setMagasin($this);
}
return $this;
}
public function removeProduit(Produit $produit): static
{
if ($this->produits->removeElement($produit)) {
if ($produit->getMagasin() === $this) {
$produit->setMagasin(null);
}
}
return $this;
}
}