<?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)]
class PurchaseOrder
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\ManyToOne(targetEntity: Currency::class, inversedBy: 'purchaseOrders')]
#[ORM\JoinColumn(nullable: false)]
private $currency;
#[ORM\Column(type: 'float')]
private $amount;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $description;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $filepath;
#[ORM\Column(type: 'integer')]
private $closed = 0;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'purchaseOrders')]
#[ORM\JoinColumn(nullable: false)]
private $createdBy;
#[ORM\Column(type: 'datetime')]
private $createdAt;
#[ORM\Column(type: 'datetime', nullable: true)]
private $updatedAt;
#[ORM\ManyToOne(targetEntity: User::class)]
private $updatedBy;
#[ORM\Column(type: 'string', length: 20)]
private $PurchaseOrderNo;
#[ORM\OneToMany(targetEntity: SalesOrder::class, mappedBy: 'purchaseOrder')]
private $salesOrder;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $filename;
#[ORM\Column(type: 'string', length: 64, nullable: true)]
private $filetype;
#[ORM\OneToMany(targetEntity: SalesOrderPurchaseOrder::class, mappedBy: 'PurchaseOrder')]
private $salesOrderPurchaseOrders;
#[ORM\Column(type: 'date', nullable: true)]
private $date;
#[ORM\ManyToOne(targetEntity: Client::class, inversedBy: 'projects')]
#[ORM\JoinColumn(nullable: true)]
private $client;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $cancelledAt = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $cancelNote = null;
public function __construct()
{
$this->salesOrder = new ArrayCollection();
$this->salesOrderPurchaseOrders = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCurrency(): ?Currency
{
return $this->currency;
}
public function setCurrency(?Currency $currency): self
{
$this->currency = $currency;
return $this;
}
public function getAmount(): ?float
{
return $this->amount;
}
public function getAmountUsd(): ?float
{
global $kernel;
$currencyService = $kernel->getContainer()->get('CurrencyService');
$dateStr = $this->createdAt->format('Y-m-d');
return $currencyService->convertAtDate($dateStr, $this->currency->getIso(), 'USD', $this->amount);
}
public function setAmount(float $amount): self
{
$this->amount = $amount;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getFilepath(): ?string
{
return $this->filepath;
}
public function setFilepath(?string $filepath): self
{
$this->filepath = $filepath;
return $this;
}
public function getClosed(): ?int
{
return $this->closed;
}
public function setClosed(int $closed): self
{
$this->closed = $closed;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getUpdatedBy(): ?User
{
return $this->updatedBy;
}
public function setUpdatedBy(?User $updatedBy): self
{
$this->updatedBy = $updatedBy;
return $this;
}
public function getPurchaseOrderNo(): ?string
{
return $this->PurchaseOrderNo;
}
public function setPurchaseOrderNo(string $PurchaseOrderNo): self
{
$this->PurchaseOrderNo = $PurchaseOrderNo;
return $this;
}
/**
* @return Collection<int, SalesOrder>
*/
public function getSalesOrder(): Collection
{
return $this->salesOrder;
}
public function addSalesOrder(SalesOrder $salesOrder): self
{
if (!$this->salesOrder->contains($salesOrder)) {
$this->salesOrder[] = $salesOrder;
$salesOrder->setPurchaseOrder($this);
}
return $this;
}
public function removeSalesOrder(SalesOrder $salesOrder): self
{
if ($this->salesOrder->removeElement($salesOrder)) {
// set the owning side to null (unless already changed)
if ($salesOrder->getPurchaseOrder() === $this) {
$salesOrder->setPurchaseOrder(null);
}
}
return $this;
}
public function getFilename(): ?string
{
return $this->filename;
}
public function setFilename(?string $filename): self
{
$this->filename = $filename;
return $this;
}
public function getFiletype(): ?string
{
return $this->filetype;
}
public function setFiletype(?string $filetype): self
{
$this->filetype = $filetype;
return $this;
}
/**
* @return Collection<int, SalesOrderPurchaseOrder>
*/
public function getSalesOrderPurchaseOrders(): Collection
{
return $this->salesOrderPurchaseOrders;
}
public function getAmountLeft(){
$amountLeft = $this->getAmount();
$salesOrderPurchaseOrders = $this->getSalesOrderPurchaseOrders();
foreach ($salesOrderPurchaseOrders as $salesOrderPurchaseOrder){
$amountLeft -= $salesOrderPurchaseOrder->getAmount();
}
return $amountLeft;
}
public function addSalesOrderPurchaseOrder(SalesOrderPurchaseOrder $salesOrderPurchaseOrder): self
{
if (!$this->salesOrderPurchaseOrders->contains($salesOrderPurchaseOrder)) {
$this->salesOrderPurchaseOrders[] = $salesOrderPurchaseOrder;
$salesOrderPurchaseOrder->setPurchaseOrder($this);
}
return $this;
}
public function removeSalesOrderPurchaseOrder(SalesOrderPurchaseOrder $salesOrderPurchaseOrder): self
{
if ($this->salesOrderPurchaseOrders->removeElement($salesOrderPurchaseOrder)) {
// set the owning side to null (unless already changed)
if ($salesOrderPurchaseOrder->getPurchaseOrder() === $this) {
$salesOrderPurchaseOrder->setPurchaseOrder(null);
}
}
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(?\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
$this->client = $client;
return $this;
}
public function getTotalAllocatedUsd(){
$totalAllocated = 0;
$salesOrderPurchaseOrders = $this->getSalesOrderPurchaseOrders();
foreach ($salesOrderPurchaseOrders as $salesOrderPurchaseOrder){
$totalAllocated += $salesOrderPurchaseOrder->getAmountUsd();
}
return $totalAllocated;
}
public function getAmountLeftUsd(){
return $this->getAmountUsd() - $this->getTotalAllocatedUsd();
}
public function getCancelledAt(): ?\DateTimeImmutable
{
return $this->cancelledAt;
}
public function setCancelledAt(?\DateTimeImmutable $cancelledAt): self
{
$this->cancelledAt = $cancelledAt;
return $this;
}
public function getCancelNote(): ?string
{
return $this->cancelNote;
}
public function setCancelNote(?string $cancelNote): self
{
$this->cancelNote = $cancelNote;
return $this;
}
}