vendor/novosga/core/Entity/AbstractAtendimento.php line 517

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Novo SGA project.
  4.  *
  5.  * (c) Rogerio Lino <rogeriolino@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Novosga\Entity;
  11. use DateTime;
  12. use DateInterval;
  13. use Novosga\Entity\Cliente;
  14. use Novosga\Entity\Senha;
  15. /**
  16.  * AbstractAtendimento.
  17.  *
  18.  * @author Rogerio Lino <rogeriolino@gmail.com>
  19.  */
  20. abstract class AbstractAtendimento implements \JsonSerializable
  21. {
  22.     /**
  23.      * @var mixed
  24.      */
  25.     protected $id;
  26.     
  27.     /**
  28.      * @var Unidade
  29.      */
  30.     protected $unidade;
  31.     /**
  32.      * @var Servico
  33.      */
  34.     protected $servico;
  35.     /**
  36.      * @var Usuario
  37.      */
  38.     protected $usuario;
  39.     /**
  40.      * @var Usuario
  41.      */
  42.     protected $usuarioTriagem;
  43.     /**
  44.      * @var Local
  45.      */
  46.     protected $local;
  47.     /**
  48.      * @var int
  49.      */
  50.     protected $numeroLocal;
  51.     /**
  52.      * @var Prioridade
  53.      */
  54.     private $prioridade;
  55.     /**
  56.      * @var \DateTime
  57.      */
  58.     protected $dataAgendamento;
  59.     /**
  60.      * @var \DateTime
  61.      */
  62.     protected $dataChegada;
  63.     /**
  64.      * @var \DateTime
  65.      */
  66.     protected $dataChamada;
  67.     /**
  68.      * @var \DateTime
  69.      */
  70.     private $dataInicio;
  71.     /**
  72.      * @var \DateTime
  73.      */
  74.     private $dataFim;
  75.     
  76.     /**
  77.      * @var int
  78.      */
  79.     private $tempoEspera;
  80.     
  81.     /**
  82.      * @var int
  83.      */
  84.     private $tempoPermanencia;
  85.     
  86.     /**
  87.      * @var int
  88.      */
  89.     private $tempoAtendimento;
  90.     
  91.     /**
  92.      * @var int
  93.      */
  94.     private $tempoDeslocamento;
  95.     /**
  96.      * @var string
  97.      */
  98.     protected $status;
  99.     
  100.     /**
  101.      * @var string
  102.      */
  103.     protected $resolucao;
  104.     /**
  105.      * @var Cliente
  106.      */
  107.     protected $cliente;
  108.     /**
  109.      * @var Senha
  110.      */
  111.     protected $senha;
  112.     /**
  113.      * @var Atendimento
  114.      */
  115.     protected $pai;
  116.     /**
  117.      * @var string
  118.      */
  119.     protected $observacao;
  120.     
  121.     /**
  122.      * @var ArquivoAtendimento[]
  123.      */
  124.     private $arquivos;
  125.     
  126.     public function __construct()
  127.     {
  128.         $this->senha = new Senha();
  129.         $this->arquivos = new \Doctrine\Common\Collections\ArrayCollection();
  130.     }
  131.     
  132.     public function getId()
  133.     {
  134.         return $this->id;
  135.     }
  136.     public function setId($id): self
  137.     {
  138.         $this->id $id;
  139.         return $this;
  140.     }
  141.     
  142.     public function getUnidade(): ?Unidade
  143.     {
  144.         return $this->unidade;
  145.     }
  146.     public function setUnidade(?Unidade $unidade): self
  147.     {
  148.         $this->unidade $unidade;
  149.         return $this;
  150.     }
  151.     public function getServico(): ?Servico
  152.     {
  153.         return $this->servico;
  154.     }
  155.     public function setServico(?Servico $servico): self
  156.     {
  157.         $this->servico $servico;
  158.         return $this;
  159.     }
  160.     public function getUsuario(): ?Usuario
  161.     {
  162.         return $this->usuario;
  163.     }
  164.     public function setUsuario(?Usuario $usuario): self
  165.     {
  166.         $this->usuario $usuario;
  167.         return $this;
  168.     }
  169.     public function setUsuarioTriagem(?Usuario $usuario): self
  170.     {
  171.         $this->usuarioTriagem $usuario;
  172.         return $this;
  173.     }
  174.     public function getUsuarioTriagem(): ?Usuario
  175.     {
  176.         return $this->usuarioTriagem;
  177.     }
  178.     public function getLocal(): ?Local
  179.     {
  180.         return $this->local;
  181.     }
  182.     public function getNumeroLocal(): ?int
  183.     {
  184.         return $this->numeroLocal;
  185.     }
  186.     public function setLocal(?Local $local): self
  187.     {
  188.         $this->local $local;
  189.         return $this;
  190.     }
  191.     public function setNumeroLocal(?int $numeroLocal): self
  192.     {
  193.         $this->numeroLocal $numeroLocal;
  194.         return $this;
  195.     }
  196.     
  197.     public function getDataAgendamento(): ?DateTime
  198.     {
  199.         return $this->dataAgendamento;
  200.     }
  201.     public function setDataAgendamento(?DateTime $dataAgendamento): self
  202.     {
  203.         $this->dataAgendamento $dataAgendamento;
  204.         
  205.         return $this;
  206.     }
  207.     /**
  208.      * @return DateTime
  209.      */
  210.     public function getDataChegada(): ?DateTime
  211.     {
  212.         return $this->dataChegada;
  213.     }
  214.     public function setDataChegada(?DateTime $dataChegada): self
  215.     {
  216.         $this->dataChegada $dataChegada;
  217.         return $this;
  218.     }
  219.     /**
  220.      * @return DateTime
  221.      */
  222.     public function getDataChamada(): ?DateTime
  223.     {
  224.         return $this->dataChamada;
  225.     }
  226.     public function setDataChamada(?DateTime $dataChamada): self
  227.     {
  228.         $this->dataChamada $dataChamada;
  229.         return $this;
  230.     }
  231.     /**
  232.      * @return DateTime
  233.      */
  234.     public function getDataInicio(): ?DateTime
  235.     {
  236.         return $this->dataInicio;
  237.     }
  238.     public function setDataInicio(?DateTime $dataInicio): self
  239.     {
  240.         $this->dataInicio $dataInicio;
  241.         return $this;
  242.     }
  243.     /**
  244.      * @return DateTime
  245.      */
  246.     public function getDataFim(): ?DateTime
  247.     {
  248.         return $this->dataFim;
  249.     }
  250.     public function setDataFim(?DateTime $dataFim): self
  251.     {
  252.         $this->dataFim $dataFim;
  253.         return $this;
  254.     }
  255.     public function getStatus()
  256.     {
  257.         return $this->status;
  258.     }
  259.     
  260.     public function setStatus($status): self
  261.     {
  262.         $this->status $status;
  263.         return $this;
  264.     }
  265.     
  266.     public function getResolucao()
  267.     {
  268.         return $this->resolucao;
  269.     }
  270.     public function setResolucao($resolucao): self
  271.     {
  272.         $this->resolucao $resolucao;
  273.         
  274.         return $this;
  275.     }
  276.         
  277.     public function setCliente(Cliente $cliente): self
  278.     {
  279.         $this->cliente $cliente;
  280.         return $this;
  281.     }
  282.     
  283.     public function getPai(): ?self
  284.     {
  285.         return $this->pai;
  286.     }
  287.     public function setPai(?self $pai): self
  288.     {
  289.         $this->pai $pai;
  290.         return $this;
  291.     }
  292.     
  293.     /**
  294.      * @param DateInterval $tempoEspera
  295.      * @return $this
  296.      */
  297.     public function setTempoEspera(DateInterval $tempoEspera): self
  298.     {
  299.         $this->tempoEspera $this->dateIntervalToSeconds($tempoEspera);
  300.         return $this;
  301.     }
  302.     /**
  303.      * Retorna o tempo de espera do cliente até ser atendido.
  304.      * A diferença entre a data de chegada até a data de chamada (ou atual).
  305.      *
  306.      * @return DateInterval
  307.      */
  308.     public function getTempoEspera()
  309.     {
  310.         if ($this->tempoEspera) {
  311.             return $this->secondsToDateInterval($this->tempoEspera);
  312.         }
  313.         
  314.         $now = new DateTime();
  315.         $interval $now->diff($this->getDataChegada());
  316.         return $interval;
  317.     }
  318.     
  319.     /**
  320.      * @param DateInterval $tempoPermanencia
  321.      * @return $this
  322.      */
  323.     public function setTempoPermanencia(DateInterval $tempoPermanencia): self
  324.     {
  325.         $this->tempoPermanencia $this->dateIntervalToSeconds($tempoPermanencia);
  326.         return $this;
  327.     }
  328.     
  329.     /**
  330.      * Retorna o tempo de permanência do cliente na unidade.
  331.      * A diferença entre a data de chegada até a data de fim de atendimento.
  332.      *
  333.      * @return DateInterval
  334.      */
  335.     public function getTempoPermanencia()
  336.     {
  337.         if ($this->tempoPermanencia) {
  338.             return $this->secondsToDateInterval($this->tempoPermanencia);
  339.         }
  340.         
  341.         $interval = new DateInterval('P0M');
  342.         if ($this->getDataFim()) {
  343.             $interval $this->getDataFim()->diff($this->getDataChegada());
  344.         }
  345.         return $interval;
  346.     }
  347.     
  348.     /**
  349.      * @param DateInterval $tempoAtendimento
  350.      * @return $this
  351.      */
  352.     public function setTempoAtendimento(DateInterval $tempoAtendimento null): self
  353.     {
  354.         if ($tempoAtendimento) {
  355.             $this->tempoAtendimento $this->dateIntervalToSeconds($tempoAtendimento);
  356.         } else {
  357.             $this->tempoAtendimento 0;
  358.         }
  359.         
  360.         return $this;
  361.     }
  362.     /**
  363.      * Retorna o tempo total do atendimento.
  364.      * A diferença entre a data de início e fim do atendimento.
  365.      *
  366.      * @return DateInterval
  367.      */
  368.     public function getTempoAtendimento()
  369.     {
  370.         if ($this->tempoAtendimento) {
  371.             return $this->secondsToDateInterval($this->tempoAtendimento);
  372.         }
  373.         
  374.         $interval = new DateInterval('P0M');
  375.         if ($this->getDataFim()) {
  376.             $interval $this->getDataFim()->diff($this->getDataInicio());
  377.         }
  378.         return $interval;
  379.     }
  380.     
  381.     /**
  382.      * @param DateInterval $tempoEspera
  383.      * @return $this
  384.      */
  385.     public function setTempoDeslocamento(DateInterval $tempoDeslocamento): self
  386.     {
  387.         $this->tempoDeslocamento $this->dateIntervalToSeconds($tempoDeslocamento);
  388.         return $this;
  389.     }
  390.     /**
  391.      * Retorna o tempo de deslocamento do cliente.
  392.      * A diferença entre a data de chamada até a data de início.
  393.      *
  394.      * @return \DateInterval
  395.      */
  396.     public function getTempoDeslocamento()
  397.     {
  398.         if ($this->tempoDeslocamento) {
  399.             return $this->secondsToDateInterval($this->tempoDeslocamento);
  400.         }
  401.         
  402.         $interval = new \DateInterval('P0M');
  403.         if ($this->getDataChamada()) {
  404.             $interval $this->getDataInicio()->diff($this->getDataChamada());
  405.         }
  406.         return $interval;
  407.     }
  408.     /**
  409.      * @return Cliente
  410.      */
  411.     public function getCliente()
  412.     {
  413.         return $this->cliente;
  414.     }
  415.     /**
  416.      * @return Senha
  417.      */
  418.     public function getSenha()
  419.     {
  420.         return $this->senha;
  421.     }
  422.     
  423.     public function getPrioridade()
  424.     {
  425.         return $this->prioridade;
  426.     }
  427.     public function setPrioridade(Prioridade $prioridade): self
  428.     {
  429.         $this->prioridade $prioridade;
  430.         return $this;
  431.     }
  432.     
  433.     public function getObservacao()
  434.     {
  435.         return $this->observacao;
  436.     }
  437.     public function setObservacao($observacao): self
  438.     {
  439.         $this->observacao $observacao;
  440.         return $this;
  441.     }
  442.     
  443.     public function jsonSerialize()
  444.     {
  445.         return [
  446.             'id'       => $this->getId(),
  447.             'senha'    => $this->getSenha(),
  448.             'servico'  => [
  449.                 'id'   => $this->getServico()->getId(),
  450.                 'nome' => $this->getServico()->getNome(),
  451.             ],
  452.             'observacao'      => $this->getObservacao(),
  453.             'dataChegada'     => $this->getDataChegada()->format('Y-m-d\TH:i:s'),
  454.             'dataChamada'     => $this->getDataChamada() ? $this->getDataChamada()->format('Y-m-d\TH:i:s') : null,
  455.             'dataInicio'      => $this->getDataInicio() ? $this->getDataInicio()->format('Y-m-d\TH:i:s') : null,
  456.             'dataFim'         => $this->getDataFim() ? $this->getDataFim()->format('Y-m-d\TH:i:s') : null,
  457.             'dataAgendamento' => $this->getDataAgendamento() ? $this->getDataAgendamento()->format('Y-m-d\TH:i:s') : null,
  458.             'tempoEspera'     => $this->getTempoEspera()->format('%H:%I:%S'),
  459.             'prioridade'      => $this->getPrioridade(),
  460.             'status'          => $this->getStatus(),
  461.             'resolucao'       => $this->getResolucao(),
  462.             'cliente'         => $this->getCliente(),
  463.             'triagem'         => $this->getUsuarioTriagem() ? $this->getUsuarioTriagem()->getUsername() : null,
  464.             'usuario'         => $this->getUsuario() ? $this->getUsuario()->getUsername() : null,
  465.         ];
  466.     }
  467.     public function __toString()
  468.     {
  469.         return $this->getSenha()->toString();
  470.     }
  471.     
  472.     private function dateIntervalToSeconds(\DateInterval $d): int
  473.     {
  474.         $seconds $d->+ ($d->60) + ($d->3600) + ($d->86400) + ($d->2592000);
  475.         
  476.         return $seconds;
  477.     }
  478.     
  479.     private function secondsToDateInterval(int $s)
  480.     {
  481.         $dt1 = new \DateTime("@0");
  482.         $dt2 = new \DateTime("@{$s}");
  483.         
  484.         return $dt1->diff($dt2);
  485.     }
  486.     public function getArquivos()
  487.     {
  488.         return $this->arquivos;
  489.     }
  490.     public function setArquivos($arquivos)
  491.     {
  492.         $this->arquivos $arquivos;
  493.         return $this;
  494.     }
  495.     public function getArquivosArray() : array
  496.     {
  497.         $array = [];
  498.         foreach ($this->getArquivos() as $arquivo) {
  499.             if ($arquivo->getDeletedAt()) continue;
  500.             $array[] = $arquivo->jsonSerialize();
  501.         }
  502.         return $array;
  503.     }
  504.     public function setSenha(Senha $senha)
  505.     {
  506.         $this->senha $senha;
  507.         return $this;
  508.     }
  509. }