vendor/crosiersource/crosierlib-radx/src/Entity/CRM/Cliente.php line 52

Open in your IDE?
  1. <?php
  2. namespace CrosierSource\CrosierLibRadxBundle\Entity\CRM;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  8. use CrosierSource\CrosierLibBaseBundle\Doctrine\Annotations\EntityHandler;
  9. use CrosierSource\CrosierLibBaseBundle\Doctrine\Annotations\NotUppercase;
  10. use CrosierSource\CrosierLibBaseBundle\Doctrine\Annotations\TrackedEntity;
  11. use CrosierSource\CrosierLibBaseBundle\Entity\EntityId;
  12. use CrosierSource\CrosierLibBaseBundle\Entity\EntityIdTrait;
  13. use CrosierSource\CrosierLibBaseBundle\Utils\StringUtils\StringUtils;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18.  * @ApiResource(
  19.  *     normalizationContext={"groups"={"cliente","entityId"},"enable_max_depth"=true},
  20.  *     denormalizationContext={"groups"={"cliente"},"enable_max_depth"=true},
  21.  *
  22.  *     itemOperations={
  23.  *          "get"={"path"="/crm/cliente/{id}", "security"="is_granted('ROLE_FINAN')"},
  24.  *          "put"={"path"="/crm/cliente/{id}", "security"="is_granted('ROLE_FINAN')"},
  25.  *          "delete"={"path"="/crm/cliente/{id}", "security"="is_granted('ROLE_ADMIN')"}
  26.  *     },
  27.  *     collectionOperations={
  28.  *          "get"={"path"="/crm/cliente", "security"="is_granted('ROLE_FINAN')"},
  29.  *          "post"={"path"="/crm/cliente", "security"="is_granted('ROLE_FINAN')"}
  30.  *     },
  31.  *
  32.  *     attributes={
  33.  *          "pagination_items_per_page"=10,
  34.  *          "formats"={"jsonld", "csv"={"text/csv"}}
  35.  *     }
  36.  * )
  37.  * @ApiFilter(PropertyFilter::class)
  38.  *
  39.  * @ApiFilter(SearchFilter::class, properties={"nome": "partial", "documento": "exact", "id": "exact"})
  40.  * @ApiFilter(OrderFilter::class, properties={"id", "documento", "nome", "updated"}, arguments={"orderParameterName"="order"})
  41.  *
  42.  * @EntityHandler(entityHandlerClass="CrosierSource\CrosierLibRadxBundle\EntityHandler\CRM\ClienteEntityHandler")
  43.  * @ORM\Entity(repositoryClass="CrosierSource\CrosierLibRadxBundle\Repository\CRM\ClienteRepository")
  44.  * @ORM\Table(name="crm_cliente")
  45.  * @TrackedEntity
  46.  *
  47.  * @author Carlos Eduardo Pauluk
  48.  */
  49. class Cliente implements EntityId
  50. {
  51.     use EntityIdTrait;
  52.     /**
  53.      * @ORM\Column(name="codigo", type="string")
  54.      * @var null|string
  55.      * @Groups("cliente")
  56.      */
  57.     public ?string $codigo null;
  58.     /**
  59.      * @ORM\Column(name="nome", type="string", nullable="false", length=255)
  60.      * @Groups("cliente")
  61.      */
  62.     public ?string $nome null;
  63.     /**
  64.      * @ORM\Column(name="documento", type="string", nullable="true", length=14)
  65.      * @Groups("cliente")
  66.      */
  67.     public ?string $documento null;
  68.     /**
  69.      * PF/PJ
  70.      * @ORM\Column(name="tipo_pessoa", type="string", nullable="true", length=2)
  71.      * @Groups("cliente")
  72.      */
  73.     public ?string $tipoPessoa null;
  74.     /**
  75.      * @ORM\Column(name="nome_fantasia", type="string", nullable="true")
  76.      * @Groups("cliente")
  77.      */
  78.     public ?string $nomeFantasia null;
  79.     /**
  80.      * @ORM\Column(name="ie", type="string", nullable="true", length=30)
  81.      * @Groups("cliente")
  82.      */
  83.     public ?string $ie null;
  84.     /**
  85.      * @ORM\Column(name="dt_nascimento", type="datetime", nullable=true)
  86.      * @Groups("cliente")
  87.      * @Assert\Type("\DateTime")
  88.      */
  89.     public ?\DateTime $dtNascimento null;
  90.     /**
  91.      * @ORM\Column(name="fone1", type="string", nullable=true, length=50)
  92.      * @Groups("cliente")
  93.      * @var string|null
  94.      */
  95.     public ?string $fone1 null;
  96.     /**
  97.      * @ORM\Column(name="fone2", type="string", nullable=true, length=50)
  98.      * @Groups("cliente")
  99.      * @var string|null
  100.      */
  101.     public ?string $fone2 null;
  102.     /**
  103.      * @ORM\Column(name="fone3", type="string", nullable=true, length=50)
  104.      * @Groups("cliente")
  105.      * @var string|null
  106.      */
  107.     public ?string $fone3 null;
  108.     /**
  109.      * @ORM\Column(name="fone4", type="string", nullable=true, length=50)
  110.      * @Groups("cliente")
  111.      * @var string|null
  112.      */
  113.     public ?string $fone4 null;
  114.     /**
  115.      * @ORM\Column(name="cep", type="string", nullable=true, length=8)
  116.      * @var string|null
  117.      * @Groups("cliente")
  118.      */
  119.     public ?string $cep null;
  120.     /**
  121.      *
  122.      * @ORM\Column(name="logradouro", type="string", nullable=true, length=100)
  123.      * @var string|null
  124.      * @Groups("cliente")
  125.      */
  126.     public ?string $logradouro null;
  127.     /**
  128.      * @ORM\Column(name="numero", type="string", nullable=true, length=60)
  129.      * @var string|null
  130.      * @Groups("cliente")
  131.      */
  132.     public ?string $numero null;
  133.     /**
  134.      * @ORM\Column(name="complemento", type="string", nullable=true, length=60)
  135.      * @var string|null
  136.      * @Groups("cliente")
  137.      */
  138.     public ?string $complemento null;
  139.     /**
  140.      * @ORM\Column(name="bairro", type="string", nullable=true, length=60)
  141.      * @var string|null
  142.      * @Groups("cliente")
  143.      */
  144.     public ?string $bairro null;
  145.     /**
  146.      * @ORM\Column(name="cidade", type="string", nullable=true, length=60)
  147.      * @var string|null
  148.      * @Groups("cliente")
  149.      */
  150.     public ?string $cidade null;
  151.     /**
  152.      * @ORM\Column(name="estado", type="string", nullable=true, length=2)
  153.      * @var string|null
  154.      * @Groups("cliente")
  155.      */
  156.     public ?string $estado null;
  157.     /**
  158.      * @ORM\Column(name="ativo", type="boolean")
  159.      * @var null|bool
  160.      *
  161.      * @Groups("cliente")
  162.      */
  163.     public ?bool $ativo null;
  164.     
  165.     /**
  166.      * @ORM\Column(name="json_data", type="json")
  167.      * @NotUppercase()
  168.      * @Groups("cliente")
  169.      */
  170.     public ?array $jsonData null;
  171.     /**
  172.      * @return string
  173.      * @Groups("cliente")
  174.      */
  175.     public function getNomeMontadoComDocumento(): string
  176.     {
  177.         $r StringUtils::mascararCnpjCpf($this->documento) . ' - ';
  178.         if ($this->jsonData['nomeFantasia'] ?? false) {
  179.             $r .= $this->nome ' (' $this->jsonData['nomeFantasia'] . ')';
  180.         } else {
  181.             $r .= $this->nome;
  182.         }
  183.         return $r;
  184.     }
  185.     /**
  186.      * @return string
  187.      * @Groups("cliente")
  188.      */
  189.     public function getIdDocumentoNome(): string
  190.     {
  191.         $r '(' str_pad($this->id7'0'STR_PAD_LEFT) . ') ';
  192.         if (!($this->documento ?: false)) {
  193.             $r .= ' [DOCUMENTO_ND]';
  194.         } else {
  195.             $r .= StringUtils::mascararCnpjCpf($this->documento);
  196.         }
  197.         if ($this->tipoPessoa === 'PJ') {
  198.             $r .= ' - ' . ($this->nomeFantasia ?: $this->nome);
  199.         } else {
  200.             $r .= ' - ' $this->nome;
  201.         }
  202.         return $r;
  203.     }
  204.     /**
  205.      * @param string $tipo
  206.      * @return array|null
  207.      */
  208.     public function getEnderecoByTipo(string $tipo): ?array
  209.     {
  210.         $enderecos $this->jsonData['enderecos'] ?? null;
  211.         if ($enderecos) {
  212.             foreach ($enderecos as $endereco) {
  213.                 if (strpos($endereco['tipo'], $tipo) !== FALSE) {
  214.                     return $endereco;
  215.                 }
  216.             }
  217.         }
  218.         return null;
  219.     }
  220.     /**
  221.      * Insere somente se já não existir.
  222.      *
  223.      * @param array $novoEndereco
  224.      */
  225.     public function inserirNovoEndereco(array $novoEndereco)
  226.     {
  227.         // Verifica os endereços do cliente
  228.         $enderecoJaSalvo false;
  229.         if (($this->jsonData['enderecos'] ?? false) && count($this->jsonData['enderecos']) > 0) {
  230.             foreach ($this->jsonData['enderecos'] as $endereco) {
  231.                 if (
  232.                     (($endereco['logradouro'] ?? '') === ($novoEndereco['logradouro'] ?? '')) &&
  233.                     (($endereco['numero'] ?? '') === ($novoEndereco['numero'] ?? '')) &&
  234.                     (($endereco['complemento'] ?? '') === ($novoEndereco['complemento'] ?? '')) &&
  235.                     (($endereco['bairro'] ?? '') === ($novoEndereco['bairro'] ?? '')) &&
  236.                     (($endereco['cep'] ?? '') === ($novoEndereco['cep'] ?? '')) &&
  237.                     (($endereco['cidade'] ?? '') === ($novoEndereco['cidade'] ?? '')) &&
  238.                     (($endereco['estado'] ?? '') === ($novoEndereco['estado'] ?? ''))) {
  239.                     $enderecoJaSalvo true;
  240.                 }
  241.             }
  242.         }
  243.         if (!$enderecoJaSalvo) {
  244.             if (!isset($this->jsonData['enderecos'])) {
  245.                 $this->jsonData['enderecos'] = [];
  246.             }
  247.             $this->jsonData['enderecos'][] = [
  248.                 'tipo' => $novoEndereco['tipo'] ?? '',
  249.                 'logradouro' => $novoEndereco['logradouro'] ?? '',
  250.                 'numero' => $novoEndereco['numero'] ?? '',
  251.                 'complemento' => $novoEndereco['complemento'] ?? '',
  252.                 'bairro' => $novoEndereco['bairro'] ?? '',
  253.                 'cep' => $novoEndereco['cep'] ?? '',
  254.                 'cidade' => $novoEndereco['cidade'] ?? '',
  255.                 'estado' => $novoEndereco['estado'] ?? '',
  256.             ];
  257.         }
  258.     }
  259. }