<?php
namespace App\Entity;
use App\Entity\Product\Product;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class Customer
*
* @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
* @ORM\Table(name="malys_customer")
* @ORM\HasLifecycleCallbacks()
* @ExclusionPolicy("all")
*/
class Customer
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Expose
*/
protected $id;
/**
* @var string
* @ORM\Column(name="brand_name", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Veuillez indiquer le nom de l'établissement")
* @Expose
*/
protected $brandName;
/**
* @var string
* @ORM\Column(name="company_name", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Veuillez indiquer la raison sociale de la société")
* @Expose
*/
protected $companyName;
/**
* @var string
*
* @ORM\Column(name="business_number", type="string", length=255, nullable=true)
* @Expose
*/
protected $businessNumber;
/**
* @var string
*
* @ORM\Column(name="address", type="string", nullable=false)
* @Assert\NotBlank(message="Veuillez indiquer l'adresse de l'établissement")
* @Expose
*/
protected $address;
/**
* @var string
*
* @ORM\Column(name="zip_code", type="string", nullable=false)
* @Assert\NotBlank(message="Veuillez indiquer le code postal")
* @Expose
*/
protected $zipCode;
/**
* @var string
*
* @ORM\Column(name="city", type="string", nullable=false)
* @Assert\NotBlank(message="Veuillez indiquer la ville")
* @Expose
*/
protected $city;
/**
* @var string
*
* @ORM\Column(name="country", type="string", nullable=true)
* @Expose
*/
protected $country;
/**
* @var string
*
* @ORM\Column(name="phone", type="string", nullable=true)
* @Expose
*/
protected $phone;
/**
* @var string
*
* @ORM\Column(name="mobile", type="string", nullable=true)
* @Expose
*/
protected $mobile;
/**
* @var string
*
* @ORM\Column(name="comment", type="text", nullable=true)
*/
protected $comment;
/**
* @var boolean
*
* @ORM\Column(name="enabled", type="boolean", options={"default" : true})
* @Expose
*/
protected $enabled = true;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="App\Entity\User", cascade={"persist"}, mappedBy="shops")
*/
protected $teamMembers;
/**
* @var BusinessType
*
* @ORM\ManyToOne(targetEntity="BusinessType")
* @ORM\JoinColumn(name="business_type_id", referencedColumnName="id")
*/
protected $businessType;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="SupplierCustomer", mappedBy="customer", cascade={"remove"})
*/
protected $supplierCustomers;
/**
* @var DateTime
*
* @ORM\Column(name="createdAt", type="datetime", nullable=false)
* @Expose
*/
protected $createdAt;
/**
* @var DateTime
*
* @ORM\Column(name="updatedAt", type="datetime", nullable=true)
* @Expose
*/
protected $updatedAt;
/**
* One Customer has Many PriceOptimized
* @ORM\OneToMany(targetEntity="App\Entity\PriceOptimized", mappedBy="customer")
*/
protected $optimizedPrices;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Product\Product", mappedBy="customers")
*/
protected $favoritesProducts;
/**
* One Customer has Many OldPrice
* @ORM\OneToMany(targetEntity="App\Entity\OldPrice", mappedBy="customer")
*/
protected $oldPrices;
/**
* @var array
*/
protected $suppliers;
/**
* @var DateTime
*
* @ORM\Column(name="slotStart", type="datetime", nullable=true)
* @Expose
* @Assert\Expression("value <= this.slotFinish", message="La date de début de livraison ne peut être supérieure à la date de fin de livraison")
*/
public $slotStart;
/**
* @var DateTime
*
* @ORM\Column(name="slotFinish", type="datetime", nullable=true)
* @Expose
* @Assert\Expression("value >= this.slotStart", message="La date de fin de livraison ne peut être inférieure à la date de début de livraison")
*/
public $slotFinish;
/**
* @var string
*
* @ORM\Column(name="delivery_comment", type="text", nullable=true)
* @Expose
*/
protected $deliveryComment;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(name="saleperson_id", referencedColumnName="id", nullable=true)
* @Expose
*/
protected $salePerson;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Supplier", inversedBy="suggestedForCustomers")
* @ORM\JoinTable(name="malys_customer_suggested_suppliers")
*/
protected $suggestedSuppliers;
/**
* @ORM\Column(name="latitude", type="decimal", precision=18, scale=12, nullable=true)
*/
public $latitude;
/**
* @ORM\Column(name="longitude", type="decimal", precision=18, scale=12, nullable=true)
*/
public $longitude;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\CustomerGroup", inversedBy="customers")
* @ORM\JoinColumn(name="customer_group_id", referencedColumnName="id", onDelete="CASCADE")
* @Assert\NotBlank
*/
public $customerGroup;
/**
* @var string
*
* @ORM\Column(name="source_system", type="string", nullable=true)
* @Expose
*/
protected $sourceSystem;
/**
* Customer constructor.
*/
public function __construct()
{
$this->supplierCustomers = new ArrayCollection();
$this->optimizedPrices = new ArrayCollection();
$this->favoritesProducts = new ArrayCollection();
$this->oldPrices = new ArrayCollection();
$this->suggestedSuppliers = new ArrayCollection();
$this->teamMembers = new ArrayCollection();
$this->suppliers = null;
}
public function __toString()
{
return $this->brandName . ' (' . $this->address . ')';
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getBrandName()
{
return $this->brandName;
}
/**
* @param string $brandName
*/
public function setBrandName($brandName)
{
$this->brandName = $brandName;
}
/**
* @return string
*/
public function getCompanyName()
{
return $this->companyName;
}
/**
* @param string $companyName
*/
public function setCompanyName($companyName)
{
$this->companyName = $companyName;
}
/**
* @return string
*/
public function getBusinessNumber()
{
return $this->businessNumber;
}
/**
* @param string $businessNumber
*/
public function setBusinessNumber($businessNumber)
{
$this->businessNumber = $businessNumber;
}
/**
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* @param string $address
*/
public function setAddress($address)
{
$this->address = $address;
}
/**
* @return string
*/
public function getZipCode()
{
return $this->zipCode;
}
/**
* @param string $zipCode
*/
public function setZipCode($zipCode)
{
$this->zipCode = $zipCode;
}
/**
* @return string
*/
public function getCity()
{
return $this->city;
}
/**
* @param string $city
*/
public function setCity($city)
{
$this->city = $city;
}
/**
* @return string
*/
public function getCountry()
{
return $this->country;
}
/**
* @param string $country
*/
public function setCountry($country)
{
$this->country = $country;
}
/**
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param string $phone
*/
public function setPhone($phone)
{
$this->phone = $phone;
}
/**
* @return string
*/
public function getMobile()
{
return $this->mobile;
}
/**
* @param string $mobile
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
}
/**
* @return string
*/
public function getComment()
{
return $this->comment;
}
/**
* @param string $comment
*/
public function setComment($comment)
{
$this->comment = $comment;
}
/**
* @return bool
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* @param bool $enabled
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
}
/**
* @return ArrayCollection
*/
public function getUsers()
{
return $this->users;
}
/**
* @param ArrayCollection $users
*/
public function setUsers($users)
{
$this->users = $users;
}
/**
* @param User $user
*/
public function addUser($user)
{
$this->users->add($user);
}
/**
* @param User $user
*/
public function removeUser($user)
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
}
}
/**
* @return BusinessType
*/
public function getBusinessType()
{
return $this->businessType;
}
/**
* @param BusinessType $businessType
*/
public function setBusinessType($businessType)
{
$this->businessType = $businessType;
}
/**
* @return ArrayCollection
*/
public function getSupplierCustomers()
{
return $this->supplierCustomers;
}
/**
* @param ArrayCollection $supplierCustomers
*/
public function setSupplierCustomers($supplierCustomers)
{
$this->supplierCustomers = $supplierCustomers;
}
public function hasSupplier(Supplier $supplier)
{
$suppliers = $this->getSuppliers();
return isset($suppliers[ $supplier->getId() ]);
}
/**
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set createdAt
*
* @return Customer
* @ORM\PrePersist
*/
public function setCreatedAt()
{
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set updatedAt
*
* @return Customer
* @ORM\PreUpdate
*/
public function setUpdatedAt()
{
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return mixed
*/
public function getOptimizedPrices()
{
return $this->optimizedPrices;
}
/**
* @param mixed $optimizedPrices
*/
public function setOptimizedPrices($optimizedPrices)
{
$this->optimizedPrices = $optimizedPrices;
}
/**
* @return ArrayCollection|Collection
*/
public function getFavoritesProducts()
{
return $this->favoritesProducts;
// Je comment out car le controle de la vente de produit n'est pas censé se faire ici
// return $this->favoritesProducts->filter(function($product) {
// if($product instanceof Product && $product->isEnabled()
// && $product->getSupplier() instanceof Supplier
// && $product->getSupplier()->isEnabled()
// && $this->getSupplierCustomerBySupplier($product->getSupplier()) instanceof SupplierCustomer
// ) {
// return true;
// }
// return false;
// });
}
public function setFavoritesProducts($favoritesProducts)
{
$this->favoritesProducts = $favoritesProducts;
}
/**
* @param Product $product
*/
public function addFavoriteProduct(Product $product)
{
if (! $this->favoritesProducts->contains($product)) {
$this->favoritesProducts->add($product);
}
}
/**
* @param Product $product
*/
public function removeFavoriteProduct(Product $product)
{
if ($this->favoritesProducts->contains($product)) {
$this->favoritesProducts->removeElement($product);
}
}
/**
* Add or Remove Favorite and return true if product is favorite, false is product is not a favorite
*
* @param Product $product
*
* @return bool
*/
public function addOrremoveFavoriteProduct(Product $product)
{
if ($this->favoritesProducts->contains($product)) {
$this->favoritesProducts->removeElement($product);
return false;
} else {
$this->favoritesProducts->add($product);
return true;
}
}
/**
* @return mixed
*/
public function getOldPrices()
{
return $this->oldPrices;
}
/**
* @param mixed $oldPrices
*/
public function setOldPrices($oldPrices)
{
$this->oldPrices = $oldPrices;
}
/**
* @return array
*/
public function getSuppliers()
{
if ($this->suppliers === null) {
$this->suppliers = [];
/** @var SupplierCustomer $supplierCustomer */
foreach ($this->getSupplierCustomers() as $supplierCustomer) {
$this->suppliers[ $supplierCustomer->getSupplier()->getId() ] = $supplierCustomer->getSupplier();
}
}
return $this->suppliers;
}
/**
* @return DateTime
*/
public function getSlotStart()
{
return $this->slotStart;
}
/**
* Set slotStart
*
* @param Datetime $slotStart
*/
public function setSlotStart($slotStart)
{
$this->slotStart = $slotStart;
}
/**
* @return DateTime
*/
public function getSlotFinish()
{
return $this->slotFinish;
}
/**
* Set slotFinish
*
* @param Datetime $slotFinish
*/
public function setSlotFinish($slotFinish)
{
$this->slotFinish = $slotFinish;
}
/**
* @param Supplier $supplier
*
* @return SupplierCustomer|false
*/
public function getSupplierCustomerBySupplier(Supplier $supplier)
{
return $this->supplierCustomers->filter(function (SupplierCustomer $supplierCustomer) use ($supplier) {
return $supplierCustomer->getSupplier()->getId() == $supplier->getId();
})->first();
}
/**
* @param $supplierId
*
* @return mixed
*/
public function getSupplierCustomerBySupplierId($supplierId)
{
return $this->supplierCustomers->filter(function (SupplierCustomer $supplierCustomer) use ($supplierId) {
return $supplierCustomer->getSupplier()->getId() == $supplierId;
})->first();
}
/**
* @return string
*/
public function getDeliveryComment()
{
return $this->deliveryComment;
}
/**
* @param string $deliveryComment
*/
public function setDeliveryComment($deliveryComment)
{
$this->deliveryComment = $deliveryComment;
}
/**
* @return mixed
*/
public function getSalePerson()
{
return $this->salePerson;
}
/**
* @param mixed $salePerson
*/
public function setSalePerson($salePerson)
{
$this->salePerson = $salePerson;
}
/**
* @return mixed
*/
public function getSuggestedSuppliers()
{
return $this->suggestedSuppliers;
}
/**
* @param mixed $suggestedSuppliers
*/
public function setSuggestedSuppliers($suggestedSuppliers)
{
$this->suggestedSuppliers = $suggestedSuppliers;
}
public function addSuggestedSupplier($supplier)
{
$this->suggestedSuppliers->add($supplier);
}
public function removeSuggestedSupplier($supplier)
{
$this->suggestedSuppliers->removeElement($supplier);
}
/**
* @return mixed
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* @param mixed $latitude
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
}
/**
* @return mixed
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* @param mixed $longitude
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
}
/**
* @return CustomerGroup
*/
public function getCustomerGroup()
{
return $this->customerGroup;
}
/**
* @param CustomerGroup $customerGroup
*/
public function setCustomerGroup(CustomerGroup $customerGroup)
{
$this->customerGroup = $customerGroup;
}
/**
* @return ArrayCollection
*/
public function getTeamMembers()
{
return $this->teamMembers;
}
/**
* @param ArrayCollection $teamMembers
*/
// public function setTeamMembers( ArrayCollection $teamMembers ): void {
// $this->teamMembers = $teamMembers;
// }
public function addTeamMember(User $user)
{
if ($this->teamMembers->contains($user)) {
return;
}
$this->teamMembers->add($user);
$user->addShop($this);
}
public function removeTeamMember(User $user)
{
if (! $this->teamMembers->contains($user)) {
return;
}
$this->teamMembers->removeElement($user);
$user->removeShop($this);
}
/**
* @return string
*/
public function getSourceSystem(): ?string
{
return $this->sourceSystem;
}
/**
* @param string $sourceSystem
*/
public function setSourceSystem(?string $sourceSystem): void
{
$this->sourceSystem = $sourceSystem;
}
}