vendor/crosiersource/crosierlib-radx/src/Entity/Financeiro/Fatura.php line 80

Open in your IDE?
  1. <?php
  2. namespace CrosierSource\CrosierLibRadxBundle\Entity\Financeiro;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  10. use CrosierSource\CrosierLibBaseBundle\Doctrine\Annotations\EntityHandler;
  11. use CrosierSource\CrosierLibBaseBundle\Doctrine\Annotations\NotUppercase;
  12. use CrosierSource\CrosierLibBaseBundle\Entity\EntityId;
  13. use CrosierSource\CrosierLibBaseBundle\Entity\EntityIdTrait;
  14. use CrosierSource\CrosierLibBaseBundle\Utils\DateTimeUtils\DateTimeUtils;
  15. use CrosierSource\CrosierLibBaseBundle\Utils\StringUtils\StringUtils;
  16. use DateTime;
  17. use Doctrine\Common\Collections\ArrayCollection;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. use Symfony\Component\Serializer\Annotation\SerializedName;
  21. /**
  22.  * Entidade 'Fatura'.
  23.  *
  24.  * Agrupa diversas movimentações que são pagas com referência a um documento fiscal.
  25.  *
  26.  * @ApiResource(
  27.  *     normalizationContext={"groups"={"fatura","entityId"},"enable_max_depth"=true},
  28.  *     denormalizationContext={"groups"={"fatura"},"enable_max_depth"=true},
  29.  *
  30.  *     itemOperations={
  31.  *          "get"={"path"="/fin/fatura/{id}", "security"="is_granted('ROLE_FINAN')"},
  32.  *          "put"={"path"="/fin/fatura/{id}", "security"="is_granted('ROLE_FINAN')"},
  33.  *          "delete"={"path"="/fin/fatura/{id}", "security"="is_granted('ROLE_FINAN_ADMIN')"}
  34.  *     },
  35.  *     collectionOperations={
  36.  *          "get"={"path"="/fin/fatura", "security"="is_granted('ROLE_FINAN')"},
  37.  *          "post"={"path"="/fin/fatura", "security"="is_granted('ROLE_FINAN')"}
  38.  *     },
  39.  *
  40.  *     attributes={
  41.  *          "pagination_items_per_page"=10,
  42.  *          "formats"={"jsonld", "csv"={"text/csv"}}
  43.  *     }
  44.  *
  45.  * )
  46.  * @ApiFilter(PropertyFilter::class)
  47.  *
  48.  * @ApiFilter(DateFilter::class, properties={"dtFatura"})
  49.  *
  50.  * @ApiFilter(BooleanFilter::class, properties={
  51.  *     "fechada",
  52.  *     "quitada",
  53.  *     "cancelada"
  54.  * })
  55.  *
  56.  * @ApiFilter(SearchFilter::class, properties={
  57.  *     "sacadoDocumento": "exact",
  58.  *     "sacadoNome": "partial",
  59.  *     "cedenteDocumento": "exact",
  60.  *     "cedenteNome": "partial"
  61.  * })
  62.  *
  63.  * @ApiFilter(OrderFilter::class, properties={
  64.  *     "id",
  65.  *     "updated",
  66.  *     "dtFatura"
  67.  * }, arguments={"orderParameterName"="order"})
  68.  *
  69.  * @EntityHandler(entityHandlerClass="CrosierSource\CrosierLibRadxBundle\EntityHandler\Financeiro\FaturaEntityHandler")
  70.  *
  71.  * @ORM\Entity(repositoryClass="CrosierSource\CrosierLibRadxBundle\Repository\Financeiro\FaturaRepository")
  72.  * @ORM\Table(name="fin_fatura")
  73.  *
  74.  * @author Carlos Eduardo Pauluk
  75.  */
  76. class Fatura implements EntityId
  77. {
  78.     use EntityIdTrait;
  79.     /**
  80.      * @ORM\Column(name="descricao", type="string", nullable=true)
  81.      * @Groups("fatura")
  82.      */
  83.     public ?string $descricao null;
  84.     /**
  85.      * CPF/CNPJ de quem paga esta movimentação.
  86.      *
  87.      * @ORM\Column(name="sacado_documento", type="string", nullable=true)
  88.      * @Groups("fatura")
  89.      */
  90.     public ?string $sacadoDocumento null;
  91.     /**
  92.      * Razão Social / Nome de quem paga esta movimentação.
  93.      *
  94.      * @ORM\Column(name="sacado_nome", type="string", nullable=true)
  95.      * @Groups("fatura")
  96.      */
  97.     public ?string $sacadoNome null;
  98.     /**
  99.      * CPF/CNPJ de quem recebe esta movimentação.
  100.      *
  101.      * @ORM\Column(name="cedente_documento", type="string", nullable=true)
  102.      * @Groups("fatura")
  103.      */
  104.     public ?string $cedenteDocumento null;
  105.     /**
  106.      * Razão Social / Nome de quem recebe esta movimentação.
  107.      *
  108.      * @ORM\Column(name="cedente_nome", type="string", nullable=true)
  109.      * @Groups("fatura")
  110.      */
  111.     public ?string $cedenteNome null;
  112.     /**
  113.      * Data em que a movimentação efetivamente aconteceu.
  114.      *
  115.      * @ORM\Column(name="dt_fatura", type="datetime")
  116.      * @Groups("fatura")
  117.      *
  118.      * @var DateTime|null
  119.      */
  120.     public ?DateTime $dtFatura null;
  121.     /**
  122.      * Data em que a movimentação efetivamente aconteceu.
  123.      *
  124.      * @ORM\Column(name="dt_vencto", type="datetime", nullable=true)
  125.      * @Groups("fatura")
  126.      *
  127.      * @var DateTime|null
  128.      */
  129.     public ?DateTime $dtVencto null;
  130.     /**
  131.      *
  132.      * Se for fechada, não é possível incluir outras movimentações na fatura.
  133.      *
  134.      * @ORM\Column(name="fechada", type="boolean")
  135.      * @Groups("fatura")
  136.      *
  137.      * @var bool|null
  138.      */
  139.     public ?bool $fechada false;
  140.     /**
  141.      *
  142.      * @ORM\Column(name="quitada", type="boolean")
  143.      * @Groups("fatura")
  144.      *
  145.      * @var bool|null
  146.      */
  147.     public ?bool $quitada false;
  148.     /**
  149.      *
  150.      * @ORM\Column(name="cancelada", type="boolean")
  151.      * @Groups("fatura")
  152.      *
  153.      * @var bool|null
  154.      */
  155.     public ?bool $cancelada false;
  156.     /**
  157.      * @ORM\Column(name="obs", type="string", nullable=true)
  158.      * @Groups("fatura")
  159.      */
  160.     public ?string $obs null;
  161.     /**
  162.      *
  163.      * @ORM\Column(name="json_data", type="json")
  164.      * @var null|array
  165.      * @NotUppercase()
  166.      * @Groups("fatura")
  167.      */
  168.     public ?array $jsonData null;
  169.     /**
  170.      *
  171.      * @var Movimentacao[]|ArrayCollection|null
  172.      *
  173.      * @ORM\OneToMany(targetEntity="Movimentacao", mappedBy="fatura")
  174.      */
  175.     public $movimentacoes null;
  176.     public function __construct()
  177.     {
  178.         $this->movimentacoes = new ArrayCollection();
  179.     }
  180.     
  181.     /**
  182.      * @Groups("fatura")
  183.      * @return null|string
  184.      */
  185.     public function getSacado(): ?string {
  186.         if ($this->sacadoDocumento && $this->sacadoNome) {
  187.             return StringUtils::mascararCnpjCpf($this->sacadoDocumento) . ' - ' $this->sacadoNome;   
  188.         }
  189.         return null;
  190.     }
  191.     /**
  192.      * @Groups("fatura")
  193.      * @return null|string
  194.      */
  195.     public function getCedente(): ?string {
  196.         if ($this->cedenteDocumento && $this->cedenteNome) {
  197.             return StringUtils::mascararCnpjCpf($this->cedenteDocumento) . ' - ' $this->cedenteNome;
  198.         }
  199.         return null;
  200.     }
  201.     
  202.     /**
  203.      * @return Movimentacao[]|ArrayCollection|null
  204.      */
  205.     public function getMovimentacoes()
  206.     {
  207.         return $this->movimentacoes;
  208.     }
  209.     /**
  210.      * @param Movimentacao[]|ArrayCollection|null $movimentacoes
  211.      * @return Fatura
  212.      */
  213.     public function setMovimentacoes($movimentacoes): Fatura
  214.     {
  215.         $this->movimentacoes $movimentacoes;
  216.         return $this;
  217.     }
  218.     /**
  219.      * @param Movimentacao $movimentacao
  220.      * @return $this
  221.      */
  222.     public function addMovimentacao(Movimentacao $movimentacao): self
  223.     {
  224.         if (!$this->movimentacoes->contains($movimentacao)) {
  225.             $this->movimentacoes[] = $movimentacao;
  226.             $movimentacao->fatura $this;
  227.         }
  228.         return $this;
  229.     }
  230.     /**
  231.      * @param Movimentacao $movimentacao
  232.      * @return $this
  233.      */
  234.     public function removeMovimentacao(Movimentacao $movimentacao): self
  235.     {
  236.         if ($this->movimentacoes->contains($movimentacao)) {
  237.             $this->movimentacoes->removeElement($movimentacao);
  238.             if ($movimentacao->fatura === $this) {
  239.                 $movimentacao->fatura null;
  240.             }
  241.         }
  242.         return $this;
  243.     }
  244.     /**
  245.      * @param int $codigo
  246.      * @return Movimentacao|null
  247.      */
  248.     public function getPrimeiraMovimentacaoByCategoriaCodigo(int $codigo): ?Movimentacao
  249.     {
  250.         foreach ($this->getMovimentacoes() as $m) {
  251.             if ($m->categoria->codigo === $codigo) {
  252.                 return $m;
  253.             }
  254.         }
  255.         return null;
  256.     }
  257.     /**
  258.      * @Groups("fatura")
  259.      * @SerializedName("dtQuitacao")
  260.      * @return null|\DateTime
  261.      */
  262.     public function getDtQuitacao(): ?\DateTime
  263.     {
  264.         $maior null;
  265.         if ($this->quitada) {
  266.             if ($this->movimentacoes) {
  267.                 foreach ($this->movimentacoes as $movimentacao) {
  268.                     if (!$maior || ($movimentacao->dtPagto && DateTimeUtils::diffInDias($movimentacao->dtPagto$maior) > 0)) {
  269.                         $maior = clone $movimentacao->dtPagto;
  270.                     }
  271.                 }
  272.             }
  273.         }
  274.         return $maior;
  275.     }
  276.     public function getValorTotalCobrancaFatura(?bool $apenasRealizadas false): float
  277.     {
  278.         $total 0.0;
  279.         if ($this->movimentacoes) {
  280.             $statuss $apenasRealizadas ? ['REALIZADA'] : ['ABERTA''REALIZADA'];
  281.             foreach ($this->movimentacoes as $movimentacao) {
  282.                 if (in_array($movimentacao->status$statusstrue) &&
  283.                     in_array($movimentacao->categoria->codigo, [110210], true)) {
  284.                     $total bcadd($total$movimentacao->valorTotal2);
  285.                 }
  286.             }
  287.         }
  288.         return $total;
  289.     }
  290.     /**
  291.      * @Groups("fatura")
  292.      * @var float
  293.      */
  294.     public function getValorTotal(): float
  295.     {
  296.         $total 0.0;
  297.         if ($this->movimentacoes) {
  298.             foreach ($this->movimentacoes as $movimentacao) {
  299.                 if (!in_array($movimentacao->categoria->codigo, [110210], true)) {
  300.                     $total bcadd($total$movimentacao->valorTotal2);
  301.                 }
  302.             }
  303.         }
  304.         return $total;
  305.     }
  306.     /**
  307.      * @Groups("fatura")
  308.      * @var float
  309.      */
  310.     public function getSaldo(): float {
  311.         return (float)bcsub($this->getValorTotal(), $this->getValorTotalCobrancaFatura(true), 2);
  312.     }
  313. }