src/Entity/Sales/Contract.php line 101
<?php
namespace App\Entity\Sales;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Post;
use ApiPlatform\OpenApi\Model;
use App\Controller\Upload\CreateAttachmentAction;
use App\Doctrine\Type\Sales\ContractStatus;
use App\Doctrine\Type\Sales\ContractType;
use App\Dto\Sales\ContractInput;
use App\Dto\Sales\ContractStatusInput;
use App\Entity\Common\Blameable;
use App\Entity\Common\Company;
use App\Entity\Common\Trackable;
use App\Entity\File\Upload;
use App\Processor\Sales\ContractInputProcessor;
use App\Processor\Sales\ContractStatusInputProcessor;
use ArrayObject;
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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Table(name: 'contract')]
#[ORM\Index(columns: ['tenant_id'], name: 'contract_tenant_id_idx')]
#[ORM\Index(columns: ['client_id'], name: 'contract_client_id_idx')]
#[ORM\Index(columns: ['created_by'], name: 'contract_created_by_idx')]
#[ORM\Index(columns: ['updated_by'], name: 'contract_updated_by_idx')]
#[ORM\UniqueConstraint(name: 'contract_slug_key', columns: ['slug'])]
#[ORM\UniqueConstraint(name: 'contract_number_key', columns: ['number'])]
#[ORM\Entity]
#[UniqueEntity('number')]
#[ApiResource(
routePrefix: '/sales',
input: ContractInput::class,
processor: ContractInputProcessor::class,
)]
#[ApiResource(
operations: [
new Post(
uriTemplate: '/sales/contracts/{slug}/status',
input: ContractStatusInput::class,
processor: ContractStatusInputProcessor::class,
),
],
)]
#[ApiResource(
uriTemplate: '/sales/assets/{asset}/contracts',
operations: [
new GetCollection(),
],
uriVariables: [
'asset' => new Link(toProperty: 'assets', fromClass: Asset::class),
],
)]
#[ApiResource(
// FIXME : This does not work well with docker
uriTemplate: '/sales/contracts/{slug}/attachments',
operations: [
new Post(
controller: CreateAttachmentAction::class,
openapi: new Model\Operation(
requestBody: new Model\RequestBody(
content: new ArrayObject([
'multipart/form-data' => [
'schema' => [
'type' => 'object',
'properties' => [
'file' => [
'type' => 'string',
'format' => 'binary'
]
]
]
]
])
)
),
shortName: 'Upload',
validationContext: ['groups' => ['Default', 'upload:create']],
deserialize: false
)
],
)]
#[ApiFilter(SearchFilter::class, properties: [
'number' => 'ipartial',
'client.name' => 'exact',
'client.code' => 'exact',
'status' => 'exact',
])]
class Contract
{
use Blameable;
use Trackable;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\SequenceGenerator(sequenceName: 'contract_id_seq')]
#[ORM\Column(type: Types::INTEGER)]
#[ApiProperty(identifier: false)]
private ?int $id = null;
#[Gedmo\Slug(fields: ['number'])]
#[ORM\Column(type: Types::STRING)]
#[ApiProperty(identifier: true)]
private string $slug;
#[Assert\Length(min: 5)]
#[ORM\Column(type: Types::STRING)]
private ?string $number = null;
#[Assert\NotNull]
#[Assert\NotEqualTo(propertyPath: 'client')]
#[ORM\ManyToOne(targetEntity: Company::class)]
#[ORM\JoinColumn(name: 'tenant_id', nullable: false)]
private ?Company $tenant = null;
#[Assert\NotNull]
#[ORM\ManyToOne(targetEntity: Company::class)]
#[ORM\JoinColumn(name: 'client_id', nullable: false)]
#[ApiProperty(readableLink: true)]
private ?Company $client = null;
/** @var Collection<int, Asset> */
#[ORM\ManyToMany(targetEntity: Asset::class, inversedBy: 'contracts')]
#[ORM\JoinTable(name: 'contract_asset_map')]
#[ORM\JoinColumn(name: 'contract_id')]
#[ORM\InverseJoinColumn(name: 'asset_id')]
private Collection $assets;
/** @var Collection<int, WorkOrder> */
#[ApiProperty(readable: false)]
#[ORM\OneToMany(mappedBy: 'contract', targetEntity: WorkOrder::class)]
private Collection $workOrders;
/** @var Collection<int, CallOffOrder> */
#[ORM\ManyToMany(targetEntity: CallOffOrder::class, mappedBy: 'contracts')]
private Collection $callOffOrders;
/** @var Collection<int, Upload> */
#[ORM\ManyToMany(targetEntity: Upload::class)]
#[ORM\JoinTable(name: 'contract_file_map')]
#[ORM\JoinColumn(name: 'contract_id')]
#[ORM\InverseJoinColumn(name: 'file_id')]
#[ApiProperty(readableLink: true)]
private Collection $attachments;
#[Assert\NotNull]
#[ORM\Column(type: Types::DATE_MUTABLE, options: ['default' => 'CURRENT_DATE'])]
private ?DateTimeInterface $startingOn = null;
#[Assert\GreaterThan(propertyPath: 'startingOn')]
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
private ?DateTimeInterface $endingOn = null;
#[Assert\NotNull]
#[ORM\Column(type: Types::STRING, enumType: ContractType::class)]
private ?ContractType $type = null;
#[Assert\NotNull]
#[ORM\Column(type: Types::STRING, enumType: ContractStatus::class, options: ['default' => 'ONGOING'])]
private ?ContractStatus $status = ContractStatus::ONGOING;
public function __construct()
{
$this->assets = new ArrayCollection();
$this->attachments = new ArrayCollection();
$this->callOffOrders = new ArrayCollection();
$this->workOrders = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getNumber(): ?string
{
return $this->number;
}
public function setNumber(?string $number): self
{
$this->number = $number;
return $this;
}
public function getTenant(): ?Company
{
return $this->tenant;
}
public function setTenant(?Company $tenant): self
{
$this->tenant = $tenant;
return $this;
}
public function getClient(): ?Company
{
return $this->client;
}
public function setClient(?Company $client): self
{
$this->client = $client;
return $this;
}
/**
* @return Collection<int, Asset>
*/
public function getAssets(): Collection
{
return $this->assets;
}
public function addAsset(Asset $asset): self
{
if (!$this->assets->contains($asset)) {
$this->assets->add($asset);
}
return $this;
}
public function removeAsset(Asset $asset): self
{
$this->assets->removeElement($asset);
return $this;
}
/** @return Collection<int, WorkOrder> */
public function getWorkOrders(): Collection
{
return $this->workOrders;
}
public function addWorkOrder(WorkOrder $workOrder): static
{
if (!$this->workOrders->contains($workOrder)) {
$this->workOrders[] = $workOrder;
$workOrder->setContract($this);
}
return $this;
}
public function removeWorkOrder(WorkOrder $workOrder): static
{
if ($this->workOrders->removeElement($workOrder)) {
// set the owning side to null (unless already changed)
if ($workOrder->getContract() === $this) {
$workOrder->setContract(null);
}
}
return $this;
}
/**
* @return Collection<int, CallOffOrder>
*/
public function getCallOffOrders(): Collection
{
return $this->callOffOrders;
}
public function addCallOffOrder(CallOffOrder $callOffOrder): self
{
if (!$this->callOffOrders->contains($callOffOrder)) {
$this->callOffOrders->add($callOffOrder);
$callOffOrder->addContract($this);
}
return $this;
}
public function removeCallOffOrder(CallOffOrder $callOffOrder): self
{
if ($this->callOffOrders->removeElement($callOffOrder)) {
$callOffOrder->removeContract($this);
}
return $this;
}
/**
* @return Collection<int, Upload>
*/
public function getAttachments(): Collection
{
return $this->attachments;
}
public function addAttachment(Upload $attachment): self
{
if (!$this->attachments->contains($attachment)) {
$this->attachments->add($attachment);
}
return $this;
}
public function removeAttachment(Upload $attachment): self
{
$this->attachments->removeElement($attachment);
return $this;
}
public function getStartingOn(): DateTimeInterface
{
return $this->startingOn;
}
public function setStartingOn(DateTimeInterface $startingOn): self
{
$this->startingOn = $startingOn;
return $this;
}
public function getEndingOn(): ?DateTimeInterface
{
return $this->endingOn;
}
public function setEndingOn(?DateTimeInterface $endingOn): self
{
$this->endingOn = $endingOn;
return $this;
}
public function getType(): ?ContractType
{
return $this->type;
}
public function setType(ContractType $type): self
{
$this->type = $type;
return $this;
}
public function getStatus(): ?ContractStatus
{
return $this->status;
}
public function setStatus(ContractStatus $status): self
{
$this->status = $status;
return $this;
}
}