<?php
namespace App\Entity;
use App\Exception\InvalidArgumentException;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(
* repositoryClass="App\Repository\PackingListRepository"
* )
* @ORM\Table(
* name="packing_list",
* options={"collate"="utf8_swedish_ci"}
* )
* @ORM\HasLifecycleCallbacks
*/
class PackingList implements EntityInterface, LoggableInterface, SalesDocumentInterface
{
use DocumentTrait;
use LoggableTrait;
const NUMBER_PREFIX = 'PL';
const ORDER_NUMBER_PREFIX = 9;
const STATUS_DRAFT = 'draft';
const STATUS_SENT = 'sent';
const STATUS_REJECTED = 'rejected';
/**
* @ORM\Id
* @ORM\Column(
* type="integer"
* )
* @ORM\GeneratedValue(
* strategy="AUTO"
* )
*
* @var int|null
*/
protected ?int $id = null;
/**
* @ORM\Column(
* name="`case`",
* type="string",
* length=30,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $case = null;
/**
* @ORM\OneToOne(
* targetEntity="CompanyDocumentAddress",
* cascade={"persist","remove"},
* orphanRemoval=true
* )
* @ORM\JoinColumn(
* name="delivery_address_id"
* )
*
* @Assert\Valid
*
* @var CompanyDocumentAddress|null
*/
protected ?CompanyDocumentAddress $deliveryAddress = null;
/**
* @ORM\OneToMany(
* targetEntity="DocumentVersionPackingList",
* mappedBy="packingList",
* cascade={"persist","remove"}
* )
* @ORM\OrderBy({"created" = "DESC"})
*
* @var Collection<DocumentVersionPackingList>
*/
protected Collection $documentVersions;
/**
* @ORM\Column(
* type="integer",
* )
* @Assert\Range(
* min=0
* )
*
* @var int
*/
protected int $height = 0;
/**
* @ORM\Column(
* type="integer",
* )
* @Assert\Range(
* min=0
* )
*
* @var int
*/
protected int $length = 0;
/**
* @ORM\ManyToMany(
* targetEntity="LogEntry",
* cascade={"persist","remove"},
* orphanRemoval=true
* )
* @ORM\JoinTable(
* name="packing_list_log_entry",
* joinColumns={
* @ORM\JoinColumn(
* name="packing_list_id",
* referencedColumnName="id",
* onDelete="cascade"
* )
* },
* inverseJoinColumns={
* @ORM\JoinColumn(
* name="log_entry_id",
* referencedColumnName="id",
* unique=true,
* onDelete="cascade"
* )
* }
* )
* @ORM\OrderBy({"time" = "ASC"})
*
* @Assert\Valid
*
* @var Collection<LogEntry>
*/
protected $logEntries;
/**
* @ORM\Column(
* type="string",
* length=500,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $marks = null;
/**
* @ORM\Column(
* type="string",
* length=5000,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $notes = null;
/**
* @ORM\Column(
* name="order_number",
* type="integer",
* nullable=true
* )
*
* @var int|null
*/
protected ?int $orderNumber = null;
/**
* @ORM\OneToMany(
* targetEntity="Parcel",
* mappedBy="packingList",
* cascade={"persist","remove"},
* orphanRemoval=true
* )
* @ORM\OrderBy({"number" = "ASC"})
*
* @Assert\Valid
*
* @var Collection<Parcel>
*/
protected Collection $parcels;
/**
* @ORM\ManyToOne(
* targetEntity="SalesCase",
* inversedBy="packingLists"
* )
* @ORM\JoinColumn(
* name="sales_case_id",
* referencedColumnName="id",
* onDelete="cascade"
* )
*
* @Assert\NotNull
*
* @var SalesCase
*/
protected SalesCase $salesCase;
/**
* @ORM\ManyToMany(
* targetEntity="SalesItem",
* cascade={"persist","remove"},
* orphanRemoval=true
* )
* @ORM\JoinTable(
* name="packing_list_sales_item",
* joinColumns={
* @ORM\JoinColumn(
* name="packing_list_id",
* referencedColumnName="id",
* onDelete="cascade"
* )
* },
* inverseJoinColumns={
* @ORM\JoinColumn(
* name="sales_item_id",
* referencedColumnName="id",
* unique=true,
* onDelete="cascade"
* )
* }
* )
* @ORM\OrderBy({"position" = "ASC"})
*
* @Assert\Valid
*
* @var Collection<SalesItem>
*/
protected Collection $salesItems;
/**
* @ORM\Column(
* name="total_weight_gross",
* type="integer",
* nullable=true
* )
*
* @var int|null
*/
protected ?int $totalWeightGross = null;
/**
* @ORM\Column(
* name="total_weight_net",
* type="integer",
* nullable=true
* )
*
* @var int|null
*/
protected ?int $totalWeightNet = null;
/**
* @ORM\Column(
* type="integer",
* )
* @Assert\Range(
* min=0
* )
*
* @var int
*/
protected int $width = 0;
/**
* @param SalesCase $salesCase
*/
public function __construct(SalesCase $salesCase)
{
$this->salesCase = $salesCase;
$this->documentVersions = new ArrayCollection();
$this->logEntries = new ArrayCollection();
$this->parcels = new ArrayCollection();
$this->salesItems = new ArrayCollection();
}
/**
* @param DocumentVersionPackingList $documentVersion
*
* @throws InvalidArgumentException
*/
public function addDocumentVersion(DocumentVersionPackingList $documentVersion)
{
if ($documentVersion->getPackingList() !== $this) {
throw new InvalidArgumentException('Document version has mismatching packing list document.');
}
$this->documentVersions->add($documentVersion);
}
/**
* @param Parcel $parcel
*/
public function addParcel(Parcel $parcel): void
{
$this->parcels->add($parcel);
}
/**
* @param SalesItem $item
*/
public function addSalesItem(SalesItem $item)
{
$this->salesItems->add($item);
}
/**
* @return null|string
*/
public function getCase(): ?string
{
return $this->case;
}
/**
* @return CompanyDocumentAddress|null
*/
public function getDeliveryAddress(): ?CompanyDocumentAddress
{
return $this->deliveryAddress;
}
/**
* @return Collection<DocumentVersionPackingList>
*/
public function getDocumentVersions(): Collection
{
return $this->documentVersions;
}
/**
* @return int
*/
public function getHeight(): int
{
return $this->height;
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return int
*/
public function getLength(): int
{
return $this->length;
}
/**
* @return null|string
*/
public function getMarks(): ?string
{
return $this->marks;
}
/**
* @return null|string
*/
public function getNotes(): ?string
{
return $this->notes;
}
/**
* @return string
*/
public function getNumber(): string
{
return self::NUMBER_PREFIX . str_pad($this->salesCase->getId(), 6, '0', STR_PAD_LEFT)
. '-'
. ($this->isDraft() ? 'DRAFT' : self::ORDER_NUMBER_PREFIX . str_pad($this->getOrderNumber(), 5, '0', STR_PAD_LEFT));
}
/**
* @return int|null
*/
public function getOrderNumber(): ?int
{
return $this->orderNumber;
}
/**
* @return Collection
*/
public function getParcels(): Collection
{
return $this->parcels;
}
/**
* @return string
*/
public function getPreviewFileName(): string
{
return 'Packing List - ' . $this->getNumber() . ' - Preview ' . date('Y-m-d') . '.pdf';
}
/**
* @return SalesCase
*/
public function getSalesCase(): SalesCase
{
return $this->salesCase;
}
/**
* @return Collection<SalesItem>
*/
public function getSalesItems(): Collection
{
return $this->salesItems;
}
/**
* @return array<string>
*/
public static function getStatusChoices(): array
{
return [
self::STATUS_DRAFT,
self::STATUS_SENT,
self::STATUS_REJECTED,
];
}
/**
* @return int
*/
public function getTotalVolume(): int
{
return $this->length * $this->width * $this->height;
}
/**
* @return int|null
*/
public function getTotalWeightGross(): ?int
{
return $this->totalWeightGross;
}
/**
* @return int|null
*/
public function getTotalWeightNet(): ?int
{
return $this->totalWeightNet;
}
/**
* @return int
*/
public function getWidth(): int
{
return $this->width;
}
/**
* @return bool
*/
public function isDraft(): bool
{
return $this->getStatus() == self::STATUS_DRAFT;
}
/**
* @return bool
*/
public function isRejected(): bool
{
return $this->getStatus() == self::STATUS_REJECTED;
}
/**
* @param Parcel $parcel
*/
public function removeParcel(Parcel $parcel): void
{
$this->parcels->removeElement($parcel);
}
/**
* @param SalesItem $item
*/
public function removeSalesItem(SalesItem $item)
{
$this->salesItems->removeElement($item);
}
/**
* @param string|null $case
*/
public function setCase(?string $case)
{
$this->case = $case;
}
/**
* @param CompanyDocumentAddress|null $deliveryAddress
*/
public function setDeliveryAddress(?CompanyDocumentAddress $deliveryAddress)
{
$this->deliveryAddress = $deliveryAddress;
}
/**
* @param int $height
*/
public function setHeight(int $height)
{
$this->height = $height;
}
/**
* @param int $length
*/
public function setLength(int $length)
{
$this->length = $length;
}
/**
* @param string|null $marks
*/
public function setMarks(?string $marks)
{
$this->marks = $marks;
}
/**
* @param string|null $notes
*/
public function setNotes(?string $notes)
{
$this->notes = $notes;
}
/**
* @param int|null $orderNumber
*/
public function setOrderNumber(?int $orderNumber)
{
$this->orderNumber = $orderNumber;
}
/**
* @param int|null $totalWeightGross
*/
public function setTotalWeightGross(?int $totalWeightGross)
{
$this->totalWeightGross = $totalWeightGross;
}
/**
* @param int|null $totalWeightNet
*/
public function setTotalWeightNet(?int $totalWeightNet)
{
$this->totalWeightNet = $totalWeightNet;
}
/**
* @param int $width
*/
public function setWidth(int $width)
{
$this->width = $width;
}
}