<?php
namespace App\Entity;
use App\Enum\EstadoOperacionEnum;
use App\Enum\EstadoRelojEnum;
use App\Enum\TipoOperacionEnum;
use App\Exception\ConfirmadaOperacionException;
use App\Twig\NumeroALetrasExtension;
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\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use App\Validator as PerseoAssert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\OperacionRepository")
* @ORM\Table(name="operacion", schema="perseo",
* indexes={
* @ORM\Index(name="idx_operacion_valoracion", columns={"valoracion_id", "deleted_at"})
* }
* )
* @ORM\EntityListeners({
* "App\EntityListener\Operacion\CalcularIDPerseoListener",
* "App\EntityListener\Operacion\CalcularTipoOperacionListener",
* "App\EntityListener\Operacion\UpdateDatesListener",
* "App\EntityListener\Operacion\ControlTipoOperacionListener"
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=true)
* @Vich\Uploadable
* @PerseoAssert\ContraintsValidarEntidadDetalleCompra()
* @PerseoAssert\ContraintsValidarEntidadDetalleVenta()
*/
class Operacion
{
/**
* @ORM\Id
* @ORM\Column(type="bigint", options={"unsigned":true})
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(
* type="string",
* nullable=true,
* options={"comment":"valores a tomar (Compra, Gestión, Permuta, Venta)"}
* )
*/
protected $tipo;
/**
* @ORM\Column(type="string", nullable=true, name="id_perseo")
*/
protected $IDperseo;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $fecha;
/**
* @ORM\Column(type="boolean", nullable=true, options={"comment":"0 => particular, 1 => empresa"})
*/
protected $tipoCliente;
/**
* @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="datetime", nullable=true, name="fecha_tramitacion")
*/
protected $fechaTramitacion;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_tramitada")
*/
protected $fechaTramitada;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_confirmada")
*/
protected $fechaConfirmada;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_asentada")
*/
protected $fechaAsentada;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_finalizada")
*/
private $fechaFinalizada;
/**
* @ORM\Column(type="datetime", nullable=true, name="fecha_cancelada")
*/
protected $fechaCancelada;
/**
* @ORM\Column(type="string", nullable=true, name="forma_pago")
*/
protected $formaPago;
/**
* @ORM\Column(type="float", nullable=true, precision=2, name="precio_pagar", options={"comment":"Costes compra + Costes venta"})
*/
protected $precioPagar;
/**
* @ORM\Column(type="text", nullable=true, name="comentario_contrato")
*/
protected $comentarioContrato;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $comentario;
/**
* @ORM\Column(type="string", nullable=true, name="contract_signed")
*/
protected $contractSigned;
/**
* @Assert\File(
* mimeTypes={
* "application/pdf",
* }
* )
* @Vich\UploadableField(mapping="operacion", fileNameProperty="contractSigned")
* @var File
*/
protected $contractSignedFile;
/**
* @ORM\Column(type="boolean", nullable=true, name="can_deleted", options={"default":true})
*/
protected $canDeleted;
/**
* @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\Compra", inversedBy="operacion", cascade={"persist"})
* @ORM\JoinColumn(name="compra_id", referencedColumnName="id", unique=true)
*/
protected $compra;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Venta", inversedBy="operacion", cascade={"persist"})
* @ORM\JoinColumn(name="venta_id", referencedColumnName="id", unique=true)
*/
protected $venta;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Valoracion", inversedBy="operacion")
* @ORM\JoinColumn(name="valoracion_id", referencedColumnName="id", unique=true)
*/
protected $valoracion;
/**
* @ORM\OneToMany(targetEntity=\App\Entity\Reloj::class, mappedBy="operacionGestion")
*/
private $relojesGestion;
/**
* @ORM\OneToMany(targetEntity=\App\Entity\Reloj::class, mappedBy="operacionVenta")
*/
private $relojesVenta;
/**
* @ORM\OneToMany(targetEntity=\App\Entity\Reloj::class, mappedBy="operacionCompra")
*/
private $relojesCompra;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Canal", inversedBy="operaciones")
* @ORM\JoinColumn(name="canal_id", referencedColumnName="id")
*/
protected $canal;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Cliente", inversedBy="operaciones")
* @ORM\JoinColumn(name="cliente_id", referencedColumnName="id")
*/
protected $cliente;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\EstadoOperacion", inversedBy="operaciones")
* @ORM\JoinColumn(name="estado_id", referencedColumnName="id")
*/
protected $estado;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Banco", inversedBy="operaciones")
* @ORM\JoinColumn(name="banco_id", referencedColumnName="id")
*/
protected $banco;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Firmante", inversedBy="operaciones")
* @ORM\JoinColumn(name="firmante_id", referencedColumnName="id")
*/
protected $firmante;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Intercambio", inversedBy="operaciones")
* @ORM\JoinColumn(name="intercambio_id", referencedColumnName="id")
*/
protected $intercambio;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\UnidadNegocio", inversedBy="operaciones")
* @ORM\JoinColumn(name="unidad_negocio_id", referencedColumnName="id")
*/
protected $unidadNegocio;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Usuario", inversedBy="operaciones")
* @ORM\JoinColumn(name="usuario_id", referencedColumnName="id")
*/
protected $usuario;
public function __construct()
{
$this->venta = new Venta();
$this->compra = new Compra();
$this->cliente = new Cliente();
$this->relojesGestion = new ArrayCollection();
$this->relojesVenta = new ArrayCollection();
$this->relojesCompra = new ArrayCollection();
}
public function __toString(): string
{
return $this->IDperseo??'---';
}
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 getFechaTramitacion(): ?DateTimeInterface
{
return $this->fechaTramitacion;
}
public function setFechaTramitacion(?DateTimeInterface $fechaTramitacion): self
{
$this->fechaTramitacion = $fechaTramitacion;
return $this;
}
public function getFechaTramitada(): ?DateTimeInterface
{
return $this->fechaTramitada;
}
public function setFechaTramitada(?DateTimeInterface $fechaTramitada): self
{
$this->fechaTramitada = $fechaTramitada;
return $this;
}
public function getFechaConfirmada(): ?DateTimeInterface
{
return $this->fechaConfirmada;
}
public function setFechaConfirmada(?DateTimeInterface $fechaConfirmada): self
{
$this->fechaConfirmada = $fechaConfirmada;
return $this;
}
public function getFechaAsentada(): ?DateTimeInterface
{
return $this->fechaAsentada;
}
public function setFechaAsentada(?DateTimeInterface $fechaAsentada): self
{
$this->fechaAsentada = $fechaAsentada;
return $this;
}
public function getFechaCancelada(): ?DateTimeInterface
{
return $this->fechaCancelada;
}
public function setFechaCancelada(?DateTimeInterface $fechaCancelada): self
{
$this->fechaCancelada = $fechaCancelada;
return $this;
}
public function getFormaPago(): ?string
{
return $this->formaPago;
}
public function setFormaPago(?string $formaPago): self
{
$this->formaPago = $formaPago;
return $this;
}
public function getPrecioPagarStr(): ?string
{
$string = new NumeroALetrasExtension();
return $string->numeroALetras($this->getPrecioPagar());
}
public function getPrecioPagarAbsStr(): ?string
{
$string = new NumeroALetrasExtension();
return $string->numeroALetras(abs($this->getPrecioPagar()));
}
public function getPrecioPagar(): ?float
{
return $this->precioPagar;
}
public function setPrecioPagar(?float $precioPagar): self
{
$this->precioPagar = $precioPagar;
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;
}
public function getCanal(): ?Canal
{
return $this->canal;
}
public function setCanal(?Canal $canal): self
{
$this->canal = $canal;
return $this;
}
public function getCliente(): ?Cliente
{
return $this->cliente;
}
public function setCliente(?Cliente $cliente): self
{
$this->cliente = $cliente;
return $this;
}
public function getVenta(): ?Venta
{
return $this->venta;
}
public function setVenta(?Venta $venta): self
{
$this->venta = $venta;
return $this;
}
public function getCompra(): ?Compra
{
return $this->compra;
}
public function setCompra(?Compra $compra): self
{
$this->compra = $compra;
return $this;
}
public function getTipoCliente(): ?bool
{
return $this->tipoCliente;
}
public function setTipoCliente(?bool $tipoCliente): self
{
$this->tipoCliente = $tipoCliente;
return $this;
}
public function getExportacionStr():string
{
return $this->isExportacion() ? 'Exportacion' : 'Europa';
}
public function getExportacion(): ?bool
{
return $this->exportacion;
}
public function setExportacion(?bool $exportacion): self
{
$this->exportacion = $exportacion;
return $this;
}
public function getComentarioContrato(): ?string
{
return $this->comentarioContrato;
}
public function setComentarioContrato(?string $comentarioContrato): self
{
$this->comentarioContrato = $comentarioContrato;
return $this;
}
public function getComentario(): ?string
{
return $this->comentario;
}
public function setComentario(?string $comentario): self
{
$this->comentario = $comentario;
return $this;
}
public function getValoracion(): ?Valoracion
{
return $this->valoracion;
}
public function setValoracion(?Valoracion $valoracion): self
{
$this->valoracion = $valoracion;
return $this;
}
public function getEstado(): ?EstadoOperacion
{
return $this->estado;
}
public function setEstado(?EstadoOperacion $estado): self
{
$this->estado = $estado;
return $this;
}
public function getBanco(): ?Banco
{
return $this->banco;
}
public function setBanco(?Banco $banco): self
{
$this->banco = $banco;
return $this;
}
public function getFirmante(): ?Firmante
{
return $this->firmante;
}
public function setFirmante(?Firmante $firmante): self
{
$this->firmante = $firmante;
return $this;
}
public function getIntercambio(): ?Intercambio
{
return $this->intercambio;
}
public function setIntercambio(?Intercambio $intercambio): self
{
$this->intercambio = $intercambio;
return $this;
}
public function getFecha(): ?DateTimeInterface
{
return $this->fecha;
}
public function setFecha(?DateTimeInterface $fecha): self
{
$this->fecha = $fecha;
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 getIdioma(): ?string
{
return $this->idioma;
}
public function setIdioma(?string $idioma): self
{
$this->idioma = $idioma;
return $this;
}
public function isTipoCliente(): ?bool
{
return $this->tipoCliente;
}
public function isExportacion(): ?bool
{
return $this->exportacion;
}
public function getContractSigned(): ?string
{
return $this->contractSigned;
}
public function setContractSigned(?string $contractSigned): static
{
$this->contractSigned = $contractSigned;
return $this;
}
public function getContractSignedFile(): ?File
{
return $this->contractSignedFile;
}
public function setContractSignedFile(?File $contractSignedFile): self
{
$this->contractSignedFile = $contractSignedFile;
if ($contractSignedFile) {
// if 'updatedAt' is not defined in your entity, use another property
$this->setUpdatedAt(new DateTime('now'));
}
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function getTipo(): ?string
{
return $this->tipo;
}
public function setTipo(string $tipo): static
{
$this->tipo = $tipo;
return $this;
}
/**
* @return DateTime
*/
public function getEstadoFecha(): DateTime
{
switch($this->getEstado()?->getKey())
{
case EstadoOperacionEnum::ESTADO_EN_TRAMITACION:
$fecha = $this->getFechaTramitacion();
break;
case EstadoOperacionEnum::ESTADO_TRAMITADA:
$fecha = $this->getFechaTramitada();
break;
case EstadoOperacionEnum::ESTADO_CONFIRMADA:
$fecha = $this->getFechaConfirmada();
break;
case EstadoOperacionEnum::ESTADO_CANCELADA:
$fecha = $this->getFechaCancelada();
break;
case EstadoOperacionEnum::ESTADO_ASENTADA:
$fecha = $this->getFechaAsentada();
break;
default:
$fecha = null;
break;
}
return $fecha;
}
public function isCanDeleted(): ?bool
{
return $this->canDeleted;
}
public function setCanDeleted(?bool $canDeleted): static
{
$this->canDeleted = $canDeleted;
return $this;
}
/**
* @return Collection<int, Reloj>
*/
public function getRelojesGestion(): Collection
{
return $this->relojesGestion;
}
public function addRelojesGestion(Reloj $relojesGestion): static
{
if (!$this->relojesGestion->contains($relojesGestion)) {
$this->relojesGestion->add($relojesGestion);
$relojesGestion->setOperacionGestion($this);
}
return $this;
}
public function removeRelojesGestion(Reloj $relojesGestion): static
{
if ($this->relojesGestion->removeElement($relojesGestion)) {
// set the owning side to null (unless already changed)
if ($relojesGestion->getOperacionGestion() === $this) {
$relojesGestion->setOperacionGestion(null);
}
}
return $this;
}
/**
* @return Collection<int, Reloj>
*/
public function getRelojesVenta(): Collection
{
return $this->relojesVenta;
}
public function addRelojesVentum(Reloj $relojesVentum): static
{
if (!$this->relojesVenta->contains($relojesVentum)) {
$this->relojesVenta->add($relojesVentum);
$relojesVentum->setOperacionVenta($this);
}
return $this;
}
public function removeRelojesVentum(Reloj $relojesVentum): static
{
if ($this->relojesVenta->removeElement($relojesVentum)) {
// set the owning side to null (unless already changed)
if ($relojesVentum->getOperacionVenta() === $this) {
$relojesVentum->setOperacionVenta(null);
}
}
return $this;
}
/**
* @return Collection<int, Reloj>
*/
public function getRelojesCompra(): Collection
{
return $this->relojesCompra;
}
public function addRelojesCompra(Reloj $relojesCompra): static
{
if (!$this->relojesCompra->contains($relojesCompra)) {
$this->relojesCompra->add($relojesCompra);
$relojesCompra->setOperacionCompra($this);
}
return $this;
}
public function removeRelojesCompra(Reloj $relojesCompra): static
{
if ($this->relojesCompra->removeElement($relojesCompra)) {
// set the owning side to null (unless already changed)
if ($relojesCompra->getOperacionCompra() === $this) {
$relojesCompra->setOperacionCompra(null);
}
}
return $this;
}
public function getFechaFinalizada(): ?\DateTimeInterface
{
return $this->fechaFinalizada;
}
public function setFechaFinalizada(?\DateTimeInterface $fechaFinalizada): static
{
$this->fechaFinalizada = $fechaFinalizada;
return $this;
}
public function canBeProcessed(): ?bool
{
// Solo EN_TRAMITACION o CONFIRMADA
if (!in_array($this->getEstado()?->getKey(), [
EstadoOperacionEnum::ESTADO_EN_TRAMITACION,
EstadoOperacionEnum::ESTADO_CONFIRMADA
])) {
return false;
}
// Si está CONFIRMADA y es tipo GESTION, no puede tramitarse
if ($this->getEstado()?->getKey() === EstadoOperacionEnum::ESTADO_CONFIRMADA
&& $this->getTipo() === TipoOperacionEnum::OPERACION_GESTION
) {
return false;
}
foreach ($this->getVenta()->getDetalle() as $detalle)
{
if(in_array($detalle->getReloj()->getEstado()?->getKey(), [ EstadoRelojEnum::ESTADO_GESTION_CANCELADA])) return false;
}
return $this->getFechaTramitada() ? true : false;
}
public function canBeConfirmed(): ?bool
{
if ($this->isCompraGestion()) return true;
if ($this->getEstado()?->getKey() !== EstadoOperacionEnum::ESTADO_TRAMITADA) {
return false;
}
foreach ($this->getVenta()->getDetalle() as $detalle)
{
if(in_array($detalle->getReloj()->getEstado()?->getKey(), [ EstadoRelojEnum::ESTADO_GESTION_CANCELADA])) return false;
}
return $this->getFechaConfirmada() ? true : false;
}
public function isCompraGestion(): ?bool
{
$detalles = $this->getCompra()?->getDetalle();
if($this->getTipo() === TipoOperacionEnum::OPERACION_COMPRA &&
$detalles->count() === 1 &&
$detalles->first()?->getReloj()?->getOperacionGestion())
{
return true;
}
return false;
}
}