<?php
namespace App\Entity;
use App\Enum\EstadoValoracionEnum;
use App\Validator as PerseoAssert;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\ValoracionRepository")
* @ORM\Table(name="valoracion", schema="perseo",
* indexes={
* @ORM\Index(name="idx_valoracion_fechas_estado", columns={"fecha_enviada", "fecha_aceptacion", "fecha_rechazo", "fecha_tramitacion", "fecha_tramitada"}),
* @ORM\Index(name="idx_valoracion_fecha_deleted", columns={"fecha", "deleted_at"})
* }
* )
* @ORM\EntityListeners({
* "App\EntityListener\Valoracion\CalcularIDPerseoListener"
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=true)
* @PerseoAssert\ContraintsValidarEntidadValoracionStock()
* @PerseoAssert\ContraintsValidarEntidadValoracionSinStock()
*/
class Valoracion
{
static $count = 0;
static $anterior = null;
/**
* @ORM\Id
* @ORM\Column(type="bigint", options={"unsigned":true})
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(
* type="string",
* unique=true,
* nullable=true,
* name="id_perseo",
* options={"comment":"Identificador perseo único generado aleatoriamente combinación letras y números"}
* )
*/
protected $IDperseo;
/**
* @ORM\Column(
* type="boolean",
* nullable=true,
* name="tipo_cliente",
* options={"default":0,"comment":"0 => particular, 1 => empresa"}
* )
*/
protected $tipoCliente;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $fecha;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default":0})
*/
protected $exportacion;
/**
* @ORM\Column(type="string", length=2, nullable=true, options={"default":"es"})
*/
protected $idioma;
/**
* @ORM\Column(type="text", length=255, nullable=true)
*/
protected $comentario;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_enviada")
*/
protected $fechaEnviada;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_aceptacion")
*/
protected $fechaAceptacion;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_rechazo")
*/
protected $fechaRechazo;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_tramitacion")
*/
protected $fechaTramitacion;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_tramitada")
*/
protected $fechaTramitada;
/**
* @ORM\Column(
* type="smallint",
* length=4,
* nullable=true,
* options={"default":0,"unsigned":true,"comment":"Indica las veces que se ha duplicado la valoración."}
* )
*/
protected $duplicados;
/**
* @ORM\Column(type="float", nullable=true, precision=2, name="precio_pagar")
*/
protected $precioPagar;
/**
* @ORM\Column(type="text", nullable=true, name="info_tramitacion")
*/
protected $infoTramitacion;
/**
* @ORM\Column(type="text", nullable=true, name="info_valoracion")
*/
protected $infoValoracion;
/**
* @ORM\Column(
* type="string",
* nullable=true,
* name="tipo_operacion",
* options={"default":"COMPRA","comment":"Gestión o Compra/Permutua"}
* )
*/
protected $tipoOperacion;
/**
* @ORM\Column(type="datetime", nullable=true, name="deleted_at")
*/
protected $deletedAt;
/**
* @ORM\Column(type="datetime", nullable=false, name="updated_at", options={"default":"2022-01-01 00:00:00"})
* @Gedmo\Timestampable(on="update")
*/
protected $updatedAt;
/**
* @ORM\Column(type="datetime", nullable=false, name="created_at", options={"default":"2022-01-01 00:00:00"})
* @Gedmo\Timestampable(on="create")
*/
protected $createdAt;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Operacion", mappedBy="valoracion")
*/
protected $operacion;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Valoracion", mappedBy="clone")
*/
protected $clones;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Canal", inversedBy="valoraciones")
* @ORM\JoinColumn(name="canal_id", referencedColumnName="id")
*/
protected $canal;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ValoracionesRelojes", mappedBy="valoracion", cascade={"persist"})
*/
protected $valoracionesRelojes;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ValoracionesRelojesStock", mappedBy="valoracion", cascade={"persist"})
*/
protected $valoracionesRelojesStocks;
// TODO cambiar cuando se activen los relojes sin stocks, crea una constraint personalizada ... controlando que exista al menos una valoración de reloj ( stock o sin stock )
/**
* @ORM\OneToMany(targetEntity="App\Entity\ValoracionesRelojesSinStock", mappedBy="valoracion", cascade={"persist"})
* @Assert\Count(
* min = 1,
* minMessage = "assert.entidad.valoraciones_relojes"
* )
*/
protected $valoracionesRelojesSinStocks;
/**
* @ORM\OneToMany(targetEntity=\App\Entity\Reloj::class, mappedBy="valoracion")
*/
private $relojes;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\EstadoValoracion", inversedBy="valoraciones")
* @ORM\JoinColumn(name="estado_id", referencedColumnName="id")
*/
protected $estado;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\UnidadNegocio", inversedBy="valoraciones")
* @ORM\JoinColumn(name="unidad_negocio_id", referencedColumnName="id")
*/
protected $unidadNegocio;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Usuario", inversedBy="valoraciones")
* @ORM\JoinColumn(name="usuario_id", referencedColumnName="id")
*/
protected $usuario;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Cliente", inversedBy="valoraciones")
* @ORM\JoinColumn(name="cliente_id", referencedColumnName="id")
*/
protected $cliente;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Valoracion", inversedBy="clones")
* @ORM\JoinColumn(name="clone_id", referencedColumnName="id")
*/
protected $clone;
public function __construct()
{
$this->valoracionesRelojes = new ArrayCollection();
$this->valoracionesRelojesStocks = new ArrayCollection();
$this->valoracionesRelojesSinStocks = new ArrayCollection();
$this->clones = new ArrayCollection();
$this->relojes = new ArrayCollection();
}
public function __clone()
{
$this->setFecha(new DateTime('now'));
$this->setFechaAceptacion(null);
$this->setFechaEnviada(null);
$this->setFechaRechazo(null);
$this->setFechaTramitacion(null);
$this->setFechaTramitada(null);
$this->setEstado(null);
$this->setCreatedAt(new DateTime('now'));
$this->setUpdatedAt(new DateTime('now'));
$this->setDuplicados(null);
$valoracionesRelojes = $this->getValoracionesRelojes();
foreach ($valoracionesRelojes??[] as $valoracion)
{
$valoracionClone = clone $valoracion;
$valoracionClone->setClone($valoracion);
$this->addValoracionesReloje($valoracionClone);
}
}
public function __toString(): string
{
return $this->getIDperseo()??'---';
}
public function getId(): ?string
{
return $this->id;
}
public function getIDperseo(): ?string
{
return $this->IDperseo;
}
public function setIDperseo(?string $IDperseo): self
{
$this->IDperseo = $IDperseo;
return $this;
}
public function getTipoCliente(): ?bool
{
return $this->tipoCliente;
}
public function setTipoCliente(?bool $tipoCliente): self
{
$this->tipoCliente = $tipoCliente;
return $this;
}
public function getFecha(): ?DateTimeInterface
{
return $this->fecha;
}
public function setFecha(?DateTimeInterface $fecha): self
{
$this->fecha = $fecha;
return $this;
}
public function getExportacion(): ?bool
{
return $this->exportacion;
}
public function setExportacion(?bool $exportacion): self
{
$this->exportacion = $exportacion;
return $this;
}
public function getIdioma(): ?string
{
return $this->idioma;
}
public function setIdioma(?string $idioma): self
{
$this->idioma = $idioma;
return $this;
}
public function getComentario(): ?string
{
return $this->comentario;
}
public function setComentario(?string $comentario): self
{
$this->comentario = $comentario;
return $this;
}
public function getFechaEnviada(): ?DateTimeInterface
{
return $this->fechaEnviada;
}
public function setFechaEnviada(?DateTimeInterface $fechaEnviada): self
{
$this->fechaEnviada = $fechaEnviada;
return $this;
}
public function getFechaAceptacion(): ?DateTimeInterface
{
return $this->fechaAceptacion;
}
public function setFechaAceptacion(?DateTimeInterface $fechaAceptacion): self
{
$this->fechaAceptacion = $fechaAceptacion;
return $this;
}
public function getFechaRechazo(): ?DateTimeInterface
{
return $this->fechaRechazo;
}
public function setFechaRechazo(?DateTimeInterface $fechaRechazo): self
{
$this->fechaRechazo = $fechaRechazo;
return $this;
}
public function getFechaTramitacion(): ?DateTimeInterface
{
return $this->fechaTramitacion;
}
public function setFechaTramitacion(?DateTimeInterface $fechaTramitacion): self
{
$this->fechaTramitacion = $fechaTramitacion;
return $this;
}
public function getDuplicados(): ?int
{
return $this->duplicados;
}
public function setDuplicados(?int $duplicados): self
{
$this->duplicados = $duplicados;
return $this;
}
public function getDeletedAt(): ?DateTimeInterface
{
return $this->deletedAt;
}
public function setDeletedAt(?DateTimeInterface $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
public function getUpdatedAt(): ?DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getCreatedAt(): ?DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection|ValoracionesRelojes[]
*/
public function getValoracionesRelojes(): Collection
{
// $iterator = $this->valoracionesRelojes->getIterator();
// $iterator->uasort(function ($a, $b) {
// return ($a->getReloj()->getStock() < $b->getReloj()->getStock()) ? -1 : 1;
// });
//
// $collection = new ArrayCollection(iterator_to_array($iterator));
//
// return $collection;
return $this->valoracionesRelojes;
}
public function addValoracionesReloje(ValoracionesRelojes $valoracionesReloje): self
{
if (!$this->valoracionesRelojes->contains($valoracionesReloje)) {
$this->valoracionesRelojes[] = $valoracionesReloje;
$valoracionesReloje->setValoracion($this);
}
return $this;
}
public function removeValoracionesReloje(ValoracionesRelojes $valoracionesReloje): self
{
if ($this->valoracionesRelojes->removeElement($valoracionesReloje)) {
// set the owning side to null (unless already changed)
if ($valoracionesReloje->getValoracion() === $this) {
//$valoracionesReloje->setValoracion(null);
$valoracionesReloje->setDeletedAt(new DateTime('now'));
}
}
return $this;
}
public function getEstado(): ?EstadoValoracion
{
return $this->estado;
}
public function setEstado(?EstadoValoracion $estado): self
{
$this->estado = $estado;
return $this;
}
public function getUnidadNegocio(): ?UnidadNegocio
{
return $this->unidadNegocio;
}
public function setUnidadNegocio(?UnidadNegocio $unidadNegocio): self
{
$this->unidadNegocio = $unidadNegocio;
return $this;
}
public function getUsuario(): ?Usuario
{
return $this->usuario;
}
public function setUsuario(?Usuario $usuario): self
{
$this->usuario = $usuario;
return $this;
}
public function getCliente(): ?Cliente
{
return $this->cliente;
}
public function setCliente(?Cliente $cliente): self
{
$this->cliente = $cliente;
return $this;
}
public function getPrecioPagar(): ?float
{
return $this->precioPagar;
}
public function setPrecioPagar(?float $precioPagar): self
{
$this->precioPagar = $precioPagar;
return $this;
}
public function getInfoTramitacion(): ?string
{
return $this->infoTramitacion;
}
public function setInfoTramitacion(?string $infoTramitacion): self
{
$this->infoTramitacion = $infoTramitacion;
return $this;
}
public function getInfoValoracion(): ?string
{
return $this->infoValoracion;
}
public function setInfoValoracion(?string $infoValoracion): self
{
$this->infoValoracion = $infoValoracion;
return $this;
}
public function getCanal(): ?Canal
{
return $this->canal;
}
public function setCanal(?Canal $canal): self
{
$this->canal = $canal;
return $this;
}
/**
* @return Collection|ValoracionesRelojesStock[]
*/
public function getValoracionesRelojesStocks(): Collection
{
return $this->valoracionesRelojesStocks;
}
public function addValoracionesRelojesStock(ValoracionesRelojesStock $valoracionesRelojesStock): self
{
if (!$this->valoracionesRelojesStocks->contains($valoracionesRelojesStock)) {
$this->valoracionesRelojesStocks[] = $valoracionesRelojesStock;
$valoracionesRelojesStock->setValoracion($this);
}
return $this;
}
public function removeValoracionesRelojesStock(ValoracionesRelojesStock $valoracionesRelojesStock): self
{
if ($this->valoracionesRelojesStocks->removeElement($valoracionesRelojesStock)) {
// set the owning side to null (unless already changed)
if ($valoracionesRelojesStock->getValoracion() === $this) {
//$valoracionesRelojesStock->setValoracion(null);
$valoracionesRelojesStock->setDeletedAt(new DateTime('now'));
}
}
return $this;
}
/**
* @return Collection|ValoracionesRelojesSinStock[]
*/
public function getValoracionesRelojesSinStocks(): Collection
{
return $this->valoracionesRelojesSinStocks;
}
public function addValoracionesRelojesSinStock(ValoracionesRelojesSinStock $valoracionesRelojesSinStock): self
{
if (!$this->valoracionesRelojesSinStocks->contains($valoracionesRelojesSinStock)) {
$this->valoracionesRelojesSinStocks[] = $valoracionesRelojesSinStock;
$valoracionesRelojesSinStock->setValoracion($this);
}
return $this;
}
public function removeValoracionesRelojesSinStock(ValoracionesRelojesSinStock $valoracionesRelojesSinStock): self
{
if ($this->valoracionesRelojesSinStocks->removeElement($valoracionesRelojesSinStock)) {
// set the owning side to null (unless already changed)
if ($valoracionesRelojesSinStock->getValoracion() === $this) {
//$valoracionesRelojesSinStock->setValoracion(null);
$valoracionesRelojesSinStock->setDeletedAt(new DateTime('now'));
}
}
return $this;
}
public function getExportValoracionesRelojesSinStock()
{
$export = [];
$stocks = $this->getValoracionesRelojesSinStocks();
if(self::$anterior === $this->getIDperseo()) {
self::$count++;
}
else {
self::$count = 0;
self::$anterior = $this->getIDperseo();
}
if($stock = $stocks->get(self::$count))
{
$export['idperso'] = $stock->getIDperseo();
$export['reloj_foto'] = $stock->getRelojFoto();
$export['reloj_marca'] = $stock->getRelojMarca()?->getNombre();
$export['reloj_modelo'] = $stock->getRelojModelo1();
$export['reloj_referencia'] = $stock->getRelojRef1();
$export['reloj_fecha'] = $stock->getRelojFecha();
$export['reloj_foto'] = $stock->getRelojFoto();
$export['reloj_estado'] = $stock->getRelojAspecto();
$export['reloj_papeles'] = $stock->getRelojPapeles();
$export['reloj_caja'] = $stock->getRelojCaja();
$export['precio_referencia'] = $stock->getPrecioReferencia();
$export['precio_promocion'] = $stock->getPrecioPromocion();
$export['precio_min_venta'] = $stock->getPrecioMinVenta();
$export['comision'] = $stock->getComision();
$export['precio_coste_total'] = $stock->getPrecioCosteTotal();
$export['margen_deseado'] = $stock->getMargenDeseado();
$export['margen_promocion'] = $stock->getMargenPromocion();
$export['margen_minimo'] = $stock->getMargenMinimo();
$export['precio_pagar'] = $stock->getPrecioPagar();
$export['informacion'] = $stock->getInfoValoracion();
}
return json_encode($export);
}
public function getExportValoracionesRelojesStock()
{
$export = [];
$stocks = $this->getValoracionesRelojesStocks();
if($stock = $stocks->get(self::$count))
{
$export['reloj'] = $stock->getReloj()?->getIDperseo();
$export['es_precio_chrono24'] = $stock->getIsPrecioChrono24();
$export['precio_promocion'] = $stock->getPrecioPromocion();
$export['precio_coste_total'] = $stock->getPrecioCosteTotal();
$export['descuento'] = $stock->getDescuento();
$export['informacion'] = $stock->getInfoValoracion();
}
return json_encode($export);
}
/**
* @return Collection|Valoracion[]
*/
public function getClones(): Collection
{
return $this->clones;
}
public function addClone(Valoracion $clone): self
{
if (!$this->clones->contains($clone)) {
$this->clones[] = $clone;
$clone->setClone($this);
}
return $this;
}
public function removeClone(Valoracion $clone): self
{
if ($this->clones->removeElement($clone)) {
// set the owning side to null (unless already changed)
if ($clone->getClone() === $this) {
$clone->setClone(null);
}
}
return $this;
}
public function getClone(): ?self
{
return $this->clone;
}
public function setClone(?self $clone): self
{
$this->clone = $clone;
return $this;
}
public function getEstadoFecha()
{
switch($this->getEstado()?->getKey())
{
case EstadoValoracionEnum::ESTADO_ESPERA:
$fecha = $this->getFechaEnviada();
break;
case EstadoValoracionEnum::ESTADO_ACEPTADA:
$fecha = $this->getFechaAceptacion();
break;
case EstadoValoracionEnum::ESTADO_RECHAZADA:
$fecha = $this->getFechaRechazo();
break;
case EstadoValoracionEnum::ESTADO_EN_TRAMITACION:
$fecha = $this->getFechaTramitacion();
break;
default:
$fecha = null;
break;
}
return $fecha;
}
public function getOperacion(): ?Operacion
{
return $this->operacion;
}
public function setOperacion(?Operacion $operacion): self
{
// unset the owning side of the relation if necessary
if ($operacion === null && $this->operacion !== null) {
$this->operacion->setValoracion(null);
}
// set the owning side of the relation if necessary
if ($operacion !== null && $operacion->getValoracion() !== $this) {
$operacion->setValoracion($this);
}
$this->operacion = $operacion;
return $this;
}
public function isTipoCliente(): ?bool
{
return $this->tipoCliente;
}
public function isExportacion(): ?bool
{
return $this->exportacion;
}
public function getTipoOperacion(): ?string
{
return $this->tipoOperacion;
}
public function setTipoOperacion(?string $tipoOperacion): static
{
$this->tipoOperacion = $tipoOperacion;
return $this;
}
public function getFechaString():?string
{
return $this->getFecha()?->format('d/m/Y H:i:s');
}
public function getFechaTramitadaString():?string
{
return $this->getFechaTramitada()?->format('d/m/Y H:i:s');
}
public function getFechaTramitacionString():?string
{
return $this->getFechaTramitacion()?->format('d/m/Y H:i:s');
}
public function getFechaAceptacionString():?string
{
return $this->getFechaAceptacion()?->format('d/m/Y H:i:s');
}
public function getFechaEnviadaString():?string
{
return $this->getFechaEnviada()?->format('d/m/Y H:i:s');
}
public function getFechaRechazoString():?string
{
return $this->getFechaRechazo()?->format('d/m/Y H:i:s');
}
public function getFechaTramitada(): ?\DateTimeInterface
{
return $this->fechaTramitada;
}
public function setFechaTramitada(?\DateTimeInterface $fechaTramitada): static
{
$this->fechaTramitada = $fechaTramitada;
return $this;
}
/**
* @return Collection<int, Reloj>
*/
public function getRelojes(): Collection
{
return $this->relojes;
}
public function addReloje(Reloj $reloje): static
{
if (!$this->relojes->contains($reloje)) {
$this->relojes->add($reloje);
$reloje->setValoracion($this);
}
return $this;
}
public function removeReloje(Reloj $reloje): static
{
if ($this->relojes->removeElement($reloje)) {
// set the owning side to null (unless already changed)
if ($reloje->getValoracion() === $this) {
$reloje->setValoracion(null);
}
}
return $this;
}
}