<?php
namespace App\Containers\CrmSection\BonusSystemContainer\Entities;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Doctrine\Orm\Filter\NumericFilter;
use ApiPlatform\Doctrine\Orm\Filter\RangeFilter;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\DBAL\Types\Types;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use App\Containers\CrmSection\BonusSystemContainer\Data\Repositories\TransactionRepository;
use App\Containers\SettingSection\ListContainer\Entities\BonusTypeEntity;
use App\Containers\SettingSection\ListContainer\Entities\BonusTransactionTypeEntity;
use App\Containers\CrmSection\ClientContainer\Entities\ClientEntity;
use App\Containers\CrmSection\ContractContainer\Entities\ContractEntity;
use App\Ship\ApiPlatform\Filter\SimpleSearchFilter;
/**
* Транзакции
*/
#[ApiResource(
routePrefix: '/crm',
normalizationContext: [
'groups' => 'read',
'enable_max_depth' => true
],
operations: [
new Get(
uriTemplate: '/bonus_transactions/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT') or is_granted('ROLE_READONLY')"
),
new Patch(
uriTemplate: '/bonus_transactions/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN')"
),
// new Put(
// uriTemplate: '/bonus_transactions/{id}', requirements: ['id' => '\d+'],
// security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT')"
// ),
new Delete(
uriTemplate: '/bonus_transactions/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN')"
),
new Post(
uriTemplate: '/bonus_transactions',
security: "is_granted('ROLE_ADMIN')"
),
new GetCollection(
uriTemplate: '/bonus_transactions',
security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT') or is_granted('ROLE_READONLY')"
),
],
shortName: 'CRM_BonusSystem'
)]
#[ApiFilter(SearchFilter::class, properties: [
'id' => 'exact',
'externalCode' => 'partial',
'bonusType' => 'exact',
'client' => 'exact',
'referrer' => 'exact',
'type' => 'exact',
'contract' => 'exact',
'comment' => 'partial',
'type.id' => 'exact',
'type.name' => 'partial',
'type.code' => 'exact',
'bonusType.id' => 'exact',
'bonusType.name' => 'partial',
'bonusType.code' => 'exact',
])]
#[ApiFilter(NumericFilter::class, properties: ['id', 'bonuses', 'bonusType.id', 'type.id', 'client.id', 'referrer.id', 'contract.id'])]
#[ApiFilter(RangeFilter::class, properties: ['id', 'bonuses', 'bonusType.id', 'type.id', 'client.id', 'referrer.id', 'contract.id'])]
#[ApiFilter(DateFilter::class, properties: ['date', 'createdAt', 'updatedAt'])]
#[ApiFilter(OrderFilter::class)]
#[ApiFilter(filterClass: SimpleSearchFilter::class, properties: ['comment'])]
#[ORM\Entity(repositoryClass: TransactionRepository::class)]
#[ORM\Table(name: '`crm_bonus_transactions`')]
class TransactionEntity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
#[Groups(['read'])]
private ?int $id = null;
// Внешний код
#[ORM\Column(type: Types::STRING, length: 255, nullable: true, unique: true)]
#[Groups(['read'])]
private ?string $externalCode = null;
// Тип бонуса
#[ORM\ManyToOne(targetEntity: BonusTypeEntity::class)]
#[ORM\JoinColumn(name: 'bonus_type_id', referencedColumnName: 'id', nullable: true)]
#[Groups(['read'])]
#[MaxDepth(1)]
private ?BonusTypeEntity $bonusType = null;
// Клиент/Реферал
#[ORM\ManyToOne(targetEntity: ClientEntity::class)]
#[ORM\JoinColumn(name: 'client_id', referencedColumnName: 'id', nullable: false)]
#[Groups(['read'])]
#[MaxDepth(2)]
private ClientEntity $client;
// Реферер (вербующий клиент)
#[ORM\ManyToOne(targetEntity: ClientEntity::class)]
#[ORM\JoinColumn(name: 'referrer_id', referencedColumnName: 'id', nullable: true)]
#[Groups(['read'])]
#[MaxDepth(2)]
private ?ClientEntity $referrer = null;
// Тип транзакций
#[ORM\ManyToOne(targetEntity: BonusTransactionTypeEntity::class)]
#[ORM\JoinColumn(name: 'type_id', referencedColumnName: 'id', nullable: false)]
#[Groups(['read'])]
#[MaxDepth(1)]
private BonusTransactionTypeEntity $type;
// Дата транзакции
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
#[Context(
normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s'],
denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s']
)]
#[Groups(['read'])]
private ?\DateTimeInterface $date = null;
// Количество бонусов
#[ORM\Column(type: Types::INTEGER, length: 10, options: ['default' => 0])]
#[Groups(['read'])]
private ?int $bonuses = 0;
// Договор
#[ORM\ManyToOne(targetEntity: ContractEntity::class)]
#[ORM\JoinColumn(name: 'contract_id', referencedColumnName: 'id', nullable: true)]
#[Groups(['read'])]
#[MaxDepth(2)]
private ?ContractEntity $contract = null;
// Комментарий
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
#[Groups(['read'])]
private ?string $comment = null;
#[Gedmo\Timestampable(on: 'create')]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
#[Groups(['read'])]
protected $createdAt;
#[Gedmo\Timestampable(on: 'update')]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
#[Groups(['read'])]
protected $updatedAt;
public function getId(): ?int
{
return $this->id;
}
public function getExternalCode()
{
return $this->externalCode;
}
public function setExternalCode($code)
{
$this->externalCode = $code;
return $this;
}
public function getBonusType()
{
return $this->bonusType;
}
public function setBonusType($type)
{
$this->bonusType = $type;
return $this;
}
public function getClient()
{
return $this->client;
}
public function setClient($client)
{
$this->client = $client;
return $this;
}
public function getReferrer()
{
return $this->referrer;
}
public function setReferrer($referrer)
{
$this->referrer = $referrer;
return $this;
}
public function getType()
{
return $this->type;
}
public function setType($type)
{
$this->type = $type;
return $this;
}
public function setDate(\DateTimeImmutable|null $date)
{
$this->date = $date;
return $this;
}
public function getDate()
{
return $this->date;
}
public function getBonuses()
{
return $this->bonuses;
}
public function setBonuses($bonuses)
{
$this->bonuses = $bonuses;
return $this;
}
public function getContract()
{
return $this->contract;
}
public function setContract($contract)
{
$this->contract = $contract;
return $this;
}
public function getComment()
{
return $this->comment;
}
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
public function getCreatedAt()
{
return $this->createdAt;
}
public function getUpdatedAt()
{
return $this->updatedAt;
}
}