<?php
namespace App\Entity;
use App\Repository\PurchaseOrderRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: PurchaseOrderRepository::class)]
#[ORM\HasLifecycleCallbacks]
class PurchaseOrder extends BaseEntity
{
public const STATUS_DRAFT = 'DRAFT';
public const STATUS_SENT = 'SENT';
public const STATUS_PARTIAL_RECEIVED = 'PARTIAL_RECEIVED';
public const STATUS_COMPLETED = 'COMPLETED';
public const STATUS_CANCELLED = 'CANCELLED';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255, unique: true)]
private ?string $poNumber = null;
#[ORM\ManyToOne(inversedBy: 'purchaseOrders')]
#[ORM\JoinColumn(nullable: false)]
private ?Supplier $supplier = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?Warehouse $warehouse = null;
#[ORM\Column(length: 50)]
private ?string $status = self::STATUS_DRAFT;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $orderDate = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $expectedDate = null;
#[ORM\Column(type: Types::DECIMAL, precision: 19, scale: 4)]
private ?string $totalAmount = '0.00';
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $note = null;
public const DELIVERY_TERM_FACTORY = 'FABRIKA_TESLIM';
public const DELIVERY_TERM_PORT = 'LIMAN_TESLIM';
#[ORM\Column(length: 50, nullable: true)]
private ?string $deliveryTerm = null;
#[ORM\OneToMany(mappedBy: 'purchaseOrder', targetEntity: PurchaseOrderItem::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $items;
#[ORM\OneToMany(mappedBy: 'purchaseOrder', targetEntity: PurchaseOrderNote::class, cascade: ['persist', 'remove'])]
private Collection $notes;
#[ORM\ManyToOne(targetEntity: Company::class)]
#[ORM\JoinColumn(nullable: true)]
private ?Company $company = null;
#[ORM\ManyToMany(targetEntity: User::class)]
private Collection $assignedUsers;
public function __construct()
{
$this->items = new ArrayCollection();
$this->notes = new ArrayCollection();
$this->assignedUsers = new ArrayCollection();
$this->orderDate = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getPoNumber(): ?string
{
return $this->poNumber;
}
public function setPoNumber(string $poNumber): self
{
$this->poNumber = $poNumber;
return $this;
}
public function getSupplier(): ?Supplier
{
return $this->supplier;
}
public function setSupplier(?Supplier $supplier): self
{
$this->supplier = $supplier;
return $this;
}
public function getWarehouse(): ?Warehouse
{
return $this->warehouse;
}
public function setWarehouse(?Warehouse $warehouse): self
{
$this->warehouse = $warehouse;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getOrderDate(): ?\DateTimeInterface
{
return $this->orderDate;
}
public function setOrderDate(\DateTimeInterface $orderDate): self
{
$this->orderDate = $orderDate;
return $this;
}
public function getExpectedDate(): ?\DateTimeInterface
{
return $this->expectedDate;
}
public function setExpectedDate(?\DateTimeInterface $expectedDate): self
{
$this->expectedDate = $expectedDate;
return $this;
}
public function getTotalAmount(): ?string
{
return $this->totalAmount;
}
public function setTotalAmount(string $totalAmount): self
{
$this->totalAmount = $totalAmount;
return $this;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): self
{
$this->note = $note;
return $this;
}
/**
* @return Collection<int, PurchaseOrderItem>
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(PurchaseOrderItem $item): self
{
if (!$this->items->contains($item)) {
$this->items->add($item);
$item->setPurchaseOrder($this);
$this->calculateTotal();
}
return $this;
}
public function removeItem(PurchaseOrderItem $item): self
{
if ($this->items->removeElement($item)) {
// set the owning side to null (unless already changed)
if ($item->getPurchaseOrder() === $this) {
$item->setPurchaseOrder(null);
}
$this->calculateTotal();
}
return $this;
}
/**
* @return Collection<int, PurchaseOrderNote>
*/
public function getNotes(): Collection
{
return $this->notes;
}
public function addNote(PurchaseOrderNote $note): self
{
if (!$this->notes->contains($note)) {
$this->notes->add($note);
$note->setPurchaseOrder($this);
}
return $this;
}
public function removeNote(PurchaseOrderNote $note): self
{
if ($this->notes->removeElement($note)) {
if ($note->getPurchaseOrder() === $this) {
$note->setPurchaseOrder(null);
}
}
return $this;
}
/**
* @return Collection<int, User>
*/
public function getAssignedUsers(): Collection
{
return $this->assignedUsers;
}
public function addAssignedUser(User $user): self
{
if (!$this->assignedUsers->contains($user)) {
$this->assignedUsers->add($user);
}
return $this;
}
public function removeAssignedUser(User $user): self
{
$this->assignedUsers->removeElement($user);
return $this;
}
#[ORM\OneToOne(mappedBy: 'purchaseOrder', targetEntity: ExportProcess::class, cascade: ['persist', 'remove'])]
private ?ExportProcess $exportProcess = null;
public function getExportProcess(): ?ExportProcess
{
return $this->exportProcess;
}
public function setExportProcess(ExportProcess $exportProcess): self
{
// set the owning side of the relation if necessary
if ($exportProcess->getPurchaseOrder() !== $this) {
$exportProcess->setPurchaseOrder($this);
}
$this->exportProcess = $exportProcess;
return $this;
}
public function getDeliveryTerm(): ?string
{
return $this->deliveryTerm;
}
public function setDeliveryTerm(?string $deliveryTerm): self
{
$this->deliveryTerm = $deliveryTerm;
return $this;
}
public function calculateTotal(): void
{
$total = 0.0;
foreach ($this->items as $item) {
$total += (float) $item->getTotalPrice();
}
$this->totalAmount = (string) $total;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
}