src/Entity/PackingList.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Exception\InvalidArgumentException;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. /**
  9.  * @ORM\Entity(
  10.  *     repositoryClass="App\Repository\PackingListRepository"
  11.  * )
  12.  * @ORM\Table(
  13.  *     name="packing_list",
  14.  *     options={"collate"="utf8_swedish_ci"}
  15.  * )
  16.  * @ORM\HasLifecycleCallbacks
  17.  */
  18. class PackingList implements EntityInterfaceLoggableInterfaceSalesDocumentInterface
  19. {
  20.     use DocumentTrait;
  21.     use LoggableTrait;
  22.     const NUMBER_PREFIX 'PL';
  23.     const ORDER_NUMBER_PREFIX 9;
  24.     const STATUS_DRAFT 'draft';
  25.     const STATUS_SENT 'sent';
  26.     const STATUS_REJECTED 'rejected';
  27.     /**
  28.      * @ORM\Id
  29.      * @ORM\Column(
  30.      *     type="integer"
  31.      * )
  32.      * @ORM\GeneratedValue(
  33.      *     strategy="AUTO"
  34.      * )
  35.      *
  36.      * @var int|null
  37.      */
  38.     protected ?int $id null;
  39.     /**
  40.      * @ORM\Column(
  41.      *     name="`case`",
  42.      *     type="string",
  43.      *     length=30,
  44.      *     nullable=true
  45.      * )
  46.      *
  47.      * @var string|null
  48.      */
  49.     protected ?string $case null;
  50.     /**
  51.      * @ORM\OneToOne(
  52.      *     targetEntity="CompanyDocumentAddress",
  53.      *     cascade={"persist","remove"},
  54.      *     orphanRemoval=true
  55.      * )
  56.      * @ORM\JoinColumn(
  57.      *     name="delivery_address_id"
  58.      * )
  59.      *
  60.      * @Assert\Valid
  61.      *
  62.      * @var CompanyDocumentAddress|null
  63.      */
  64.     protected ?CompanyDocumentAddress $deliveryAddress null;
  65.     /**
  66.      * @ORM\OneToMany(
  67.      *     targetEntity="DocumentVersionPackingList",
  68.      *     mappedBy="packingList",
  69.      *     cascade={"persist","remove"}
  70.      * )
  71.      * @ORM\OrderBy({"created" = "DESC"})
  72.      *
  73.      * @var Collection<DocumentVersionPackingList>
  74.      */
  75.     protected Collection $documentVersions;
  76.     /**
  77.      * @ORM\Column(
  78.      *     type="integer",
  79.      * )
  80.      * @Assert\Range(
  81.      *     min=0
  82.      * )
  83.      *
  84.      * @var int
  85.      */
  86.     protected int $height 0;
  87.     /**
  88.      * @ORM\Column(
  89.      *     type="integer",
  90.      * )
  91.      * @Assert\Range(
  92.      *     min=0
  93.      * )
  94.      *
  95.      * @var int
  96.      */
  97.     protected int $length 0;
  98.     /**
  99.      * @ORM\ManyToMany(
  100.      *     targetEntity="LogEntry",
  101.      *     cascade={"persist","remove"},
  102.      *     orphanRemoval=true
  103.      * )
  104.      * @ORM\JoinTable(
  105.      *     name="packing_list_log_entry",
  106.      *     joinColumns={
  107.      *         @ORM\JoinColumn(
  108.      *             name="packing_list_id",
  109.      *             referencedColumnName="id",
  110.      *             onDelete="cascade"
  111.      *         )
  112.      *     },
  113.      *     inverseJoinColumns={
  114.      *         @ORM\JoinColumn(
  115.      *             name="log_entry_id",
  116.      *             referencedColumnName="id",
  117.      *             unique=true,
  118.      *             onDelete="cascade"
  119.      *         )
  120.      *     }
  121.      * )
  122.      * @ORM\OrderBy({"time" = "ASC"})
  123.      *
  124.      * @Assert\Valid
  125.      *
  126.      * @var Collection<LogEntry>
  127.      */
  128.     protected $logEntries;
  129.     /**
  130.      * @ORM\Column(
  131.      *     type="string",
  132.      *     length=500,
  133.      *     nullable=true
  134.      * )
  135.      *
  136.      * @var string|null
  137.      */
  138.     protected ?string $marks null;
  139.     /**
  140.      * @ORM\Column(
  141.      *     type="string",
  142.      *     length=5000,
  143.      *     nullable=true
  144.      * )
  145.      *
  146.      * @var string|null
  147.      */
  148.     protected ?string $notes null;
  149.     /**
  150.      * @ORM\Column(
  151.      *     name="order_number",
  152.      *     type="integer",
  153.      *     nullable=true
  154.      * )
  155.      *
  156.      * @var int|null
  157.      */
  158.     protected ?int $orderNumber null;
  159.     /**
  160.      * @ORM\OneToMany(
  161.      *     targetEntity="Parcel",
  162.      *     mappedBy="packingList",
  163.      *     cascade={"persist","remove"},
  164.      *     orphanRemoval=true
  165.      * )
  166.      * @ORM\OrderBy({"number" = "ASC"})
  167.      *
  168.      * @Assert\Valid
  169.      *
  170.      * @var Collection<Parcel>
  171.      */
  172.     protected Collection $parcels;
  173.     /**
  174.      * @ORM\ManyToOne(
  175.      *     targetEntity="SalesCase",
  176.      *     inversedBy="packingLists"
  177.      * )
  178.      * @ORM\JoinColumn(
  179.      *     name="sales_case_id",
  180.      *     referencedColumnName="id",
  181.      *     onDelete="cascade"
  182.      * )
  183.      *
  184.      * @Assert\NotNull
  185.      *
  186.      * @var SalesCase
  187.      */
  188.     protected SalesCase $salesCase;
  189.     /**
  190.      * @ORM\ManyToMany(
  191.      *     targetEntity="SalesItem",
  192.      *     cascade={"persist","remove"},
  193.      *     orphanRemoval=true
  194.      * )
  195.      * @ORM\JoinTable(
  196.      *     name="packing_list_sales_item",
  197.      *     joinColumns={
  198.      *         @ORM\JoinColumn(
  199.      *             name="packing_list_id",
  200.      *             referencedColumnName="id",
  201.      *             onDelete="cascade"
  202.      *         )
  203.      *     },
  204.      *     inverseJoinColumns={
  205.      *         @ORM\JoinColumn(
  206.      *             name="sales_item_id",
  207.      *             referencedColumnName="id",
  208.      *             unique=true,
  209.      *             onDelete="cascade"
  210.      *         )
  211.      *     }
  212.      * )
  213.      * @ORM\OrderBy({"position" = "ASC"})
  214.      *
  215.      * @Assert\Valid
  216.      *
  217.      * @var Collection<SalesItem>
  218.      */
  219.     protected Collection $salesItems;
  220.     /**
  221.      * @ORM\Column(
  222.      *     name="total_weight_gross",
  223.      *     type="integer",
  224.      *     nullable=true
  225.      * )
  226.      *
  227.      * @var int|null
  228.      */
  229.     protected ?int $totalWeightGross null;
  230.     /**
  231.      * @ORM\Column(
  232.      *     name="total_weight_net",
  233.      *     type="integer",
  234.      *     nullable=true
  235.      * )
  236.      *
  237.      * @var int|null
  238.      */
  239.     protected ?int $totalWeightNet null;
  240.     /**
  241.      * @ORM\Column(
  242.      *     type="integer",
  243.      * )
  244.      * @Assert\Range(
  245.      *     min=0
  246.      * )
  247.      *
  248.      * @var int
  249.      */
  250.     protected int $width 0;
  251.     /**
  252.      * @param SalesCase $salesCase
  253.      */
  254.     public function __construct(SalesCase $salesCase)
  255.     {
  256.         $this->salesCase $salesCase;
  257.         $this->documentVersions = new ArrayCollection();
  258.         $this->logEntries = new ArrayCollection();
  259.         $this->parcels = new ArrayCollection();
  260.         $this->salesItems = new ArrayCollection();
  261.     }
  262.     /**
  263.      * @param DocumentVersionPackingList $documentVersion
  264.      *
  265.      * @throws InvalidArgumentException
  266.      */
  267.     public function addDocumentVersion(DocumentVersionPackingList $documentVersion)
  268.     {
  269.         if ($documentVersion->getPackingList() !== $this) {
  270.             throw new InvalidArgumentException('Document version has mismatching packing list document.');
  271.         }
  272.         $this->documentVersions->add($documentVersion);
  273.     }
  274.     /**
  275.      * @param Parcel $parcel
  276.      */
  277.     public function addParcel(Parcel $parcel): void
  278.     {
  279.         $this->parcels->add($parcel);
  280.     }
  281.     /**
  282.      * @param SalesItem $item
  283.      */
  284.     public function addSalesItem(SalesItem $item)
  285.     {
  286.         $this->salesItems->add($item);
  287.     }
  288.     /**
  289.      * @return null|string
  290.      */
  291.     public function getCase(): ?string
  292.     {
  293.         return $this->case;
  294.     }
  295.     /**
  296.      * @return CompanyDocumentAddress|null
  297.      */
  298.     public function getDeliveryAddress(): ?CompanyDocumentAddress
  299.     {
  300.         return $this->deliveryAddress;
  301.     }
  302.     /**
  303.      * @return Collection<DocumentVersionPackingList>
  304.      */
  305.     public function getDocumentVersions(): Collection
  306.     {
  307.         return $this->documentVersions;
  308.     }
  309.     /**
  310.      * @return int
  311.      */
  312.     public function getHeight(): int
  313.     {
  314.         return $this->height;
  315.     }
  316.     /**
  317.      * @return int|null
  318.      */
  319.     public function getId(): ?int
  320.     {
  321.         return $this->id;
  322.     }
  323.     /**
  324.      * @return int
  325.      */
  326.     public function getLength(): int
  327.     {
  328.         return $this->length;
  329.     }
  330.     /**
  331.      * @return null|string
  332.      */
  333.     public function getMarks(): ?string
  334.     {
  335.         return $this->marks;
  336.     }
  337.     /**
  338.      * @return null|string
  339.      */
  340.     public function getNotes(): ?string
  341.     {
  342.         return $this->notes;
  343.     }
  344.     /**
  345.      * @return string
  346.      */
  347.     public function getNumber(): string
  348.     {
  349.         return self::NUMBER_PREFIX str_pad($this->salesCase->getId(), 6'0'STR_PAD_LEFT)
  350.             . '-'
  351.             . ($this->isDraft() ? 'DRAFT' self::ORDER_NUMBER_PREFIX str_pad($this->getOrderNumber(), 5'0'STR_PAD_LEFT));
  352.     }
  353.     /**
  354.      * @return int|null
  355.      */
  356.     public function getOrderNumber(): ?int
  357.     {
  358.         return $this->orderNumber;
  359.     }
  360.     /**
  361.      * @return Collection
  362.      */
  363.     public function getParcels(): Collection
  364.     {
  365.         return $this->parcels;
  366.     }
  367.     /**
  368.      * @return string
  369.      */
  370.     public function getPreviewFileName(): string
  371.     {
  372.         return 'Packing List - ' $this->getNumber() . ' - Preview ' date('Y-m-d') . '.pdf';
  373.     }
  374.     /**
  375.      * @return SalesCase
  376.      */
  377.     public function getSalesCase(): SalesCase
  378.     {
  379.         return $this->salesCase;
  380.     }
  381.     /**
  382.      * @return Collection<SalesItem>
  383.      */
  384.     public function getSalesItems(): Collection
  385.     {
  386.         return $this->salesItems;
  387.     }
  388.     /**
  389.      * @return array<string>
  390.      */
  391.     public static function getStatusChoices(): array
  392.     {
  393.         return [
  394.             self::STATUS_DRAFT,
  395.             self::STATUS_SENT,
  396.             self::STATUS_REJECTED,
  397.         ];
  398.     }
  399.     /**
  400.      * @return int
  401.      */
  402.     public function getTotalVolume(): int
  403.     {
  404.         return $this->length $this->width $this->height;
  405.     }
  406.     /**
  407.      * @return int|null
  408.      */
  409.     public function getTotalWeightGross(): ?int
  410.     {
  411.         return $this->totalWeightGross;
  412.     }
  413.     /**
  414.      * @return int|null
  415.      */
  416.     public function getTotalWeightNet(): ?int
  417.     {
  418.         return $this->totalWeightNet;
  419.     }
  420.     /**
  421.      * @return int
  422.      */
  423.     public function getWidth(): int
  424.     {
  425.         return $this->width;
  426.     }
  427.     /**
  428.      * @return bool
  429.      */
  430.     public function isDraft(): bool
  431.     {
  432.         return $this->getStatus() == self::STATUS_DRAFT;
  433.     }
  434.     /**
  435.      * @return bool
  436.      */
  437.     public function isRejected(): bool
  438.     {
  439.         return $this->getStatus() == self::STATUS_REJECTED;
  440.     }
  441.     /**
  442.      * @param Parcel $parcel
  443.      */
  444.     public function removeParcel(Parcel $parcel): void
  445.     {
  446.         $this->parcels->removeElement($parcel);
  447.     }
  448.     /**
  449.      * @param SalesItem $item
  450.      */
  451.     public function removeSalesItem(SalesItem $item)
  452.     {
  453.         $this->salesItems->removeElement($item);
  454.     }
  455.     /**
  456.      * @param string|null $case
  457.      */
  458.     public function setCase(?string $case)
  459.     {
  460.         $this->case $case;
  461.     }
  462.     /**
  463.      * @param CompanyDocumentAddress|null $deliveryAddress
  464.      */
  465.     public function setDeliveryAddress(?CompanyDocumentAddress $deliveryAddress)
  466.     {
  467.         $this->deliveryAddress $deliveryAddress;
  468.     }
  469.     /**
  470.      * @param int $height
  471.      */
  472.     public function setHeight(int $height)
  473.     {
  474.         $this->height $height;
  475.     }
  476.     /**
  477.      * @param int $length
  478.      */
  479.     public function setLength(int $length)
  480.     {
  481.         $this->length $length;
  482.     }
  483.     /**
  484.      * @param string|null $marks
  485.      */
  486.     public function setMarks(?string $marks)
  487.     {
  488.         $this->marks $marks;
  489.     }
  490.     /**
  491.      * @param string|null $notes
  492.      */
  493.     public function setNotes(?string $notes)
  494.     {
  495.         $this->notes $notes;
  496.     }
  497.     /**
  498.      * @param int|null $orderNumber
  499.      */
  500.     public function setOrderNumber(?int $orderNumber)
  501.     {
  502.         $this->orderNumber $orderNumber;
  503.     }
  504.     /**
  505.      * @param int|null $totalWeightGross
  506.      */
  507.     public function setTotalWeightGross(?int $totalWeightGross)
  508.     {
  509.         $this->totalWeightGross $totalWeightGross;
  510.     }
  511.     /**
  512.      * @param int|null $totalWeightNet
  513.      */
  514.     public function setTotalWeightNet(?int $totalWeightNet)
  515.     {
  516.         $this->totalWeightNet $totalWeightNet;
  517.     }
  518.     /**
  519.      * @param int $width
  520.      */
  521.     public function setWidth(int $width)
  522.     {
  523.         $this->width $width;
  524.     }
  525. }