2019-03-20 23:16:07 +01:00
|
|
|
<?php
|
2019-08-12 15:47:57 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-08-12 15:47:57 +02:00
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2019-08-12 15:47:57 +02:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2019-08-12 15:47:57 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-02-22 18:14:36 +01:00
|
|
|
* GNU Affero General Public License for more details.
|
2019-08-12 15:47:57 +02:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-08-12 15:47:57 +02:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-03-05 14:37:41 +01:00
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
namespace App\Entity\PriceInformations;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractDBElement;
|
2019-08-29 13:06:04 +02:00
|
|
|
use App\Entity\Base\TimestampTrait;
|
2020-03-28 18:55:02 +01:00
|
|
|
use App\Entity\Contracts\NamedElementInterface;
|
2020-02-16 23:48:57 +01:00
|
|
|
use App\Entity\Contracts\TimeStampableInterface;
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
use App\Entity\Parts\Supplier;
|
2020-05-20 23:36:44 +02:00
|
|
|
use DateTime;
|
2019-08-30 14:25:05 +02:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
|
|
use Doctrine\Common\Collections\Collection;
|
2019-02-23 22:41:13 +01:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2020-03-30 15:21:21 +02:00
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
2023-03-12 01:12:35 +01:00
|
|
|
use Symfony\Component\Serializer\Annotation\Groups;
|
2019-08-29 13:06:04 +02:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Class Orderdetail.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2022-09-25 18:33:13 +02:00
|
|
|
* @ORM\Table("`orderdetails`", indexes={
|
|
|
|
* @ORM\Index(name="orderdetails_supplier_part_nr", columns={"supplierpartnr"}),
|
|
|
|
* })
|
2019-02-23 22:41:13 +01:00
|
|
|
* @ORM\Entity()
|
2019-08-29 13:06:04 +02:00
|
|
|
* @ORM\HasLifecycleCallbacks()
|
2020-03-30 15:21:21 +02:00
|
|
|
* @UniqueEntity({"supplierpartnr", "supplier", "part"})
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2020-03-28 18:55:02 +01:00
|
|
|
class Orderdetail extends AbstractDBElement implements TimeStampableInterface, NamedElementInterface
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-08-29 13:06:04 +02:00
|
|
|
use TimestampTrait;
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2019-08-30 14:25:05 +02:00
|
|
|
* @ORM\OneToMany(targetEntity="Pricedetail", mappedBy="orderdetail", cascade={"persist", "remove"}, orphanRemoval=true)
|
|
|
|
* @Assert\Valid()
|
2019-08-31 12:07:16 +02:00
|
|
|
* @ORM\OrderBy({"min_discount_quantity" = "ASC"})
|
2023-03-13 00:52:22 +01:00
|
|
|
* @Groups({"extended", "full", "import"})
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
|
|
|
protected $pricedetails;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* @ORM\Column(type="string")
|
2023-03-13 00:52:22 +01:00
|
|
|
* @Groups({"extended", "full", "import"})
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected string $supplierpartnr = '';
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
* @ORM\Column(type="boolean")
|
2023-03-13 00:52:22 +01:00
|
|
|
* @Groups({"extended", "full", "import"})
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected bool $obsolete = false;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* @ORM\Column(type="string")
|
2019-08-29 13:06:04 +02:00
|
|
|
* @Assert\Url()
|
2023-03-13 00:52:22 +01:00
|
|
|
* @Groups({"full", "import"})
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected string $supplier_product_url = '';
|
2019-08-02 12:17:56 +02:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
/**
|
|
|
|
* @var Part
|
|
|
|
* @ORM\ManyToOne(targetEntity="App\Entity\Parts\Part", inversedBy="orderdetails")
|
|
|
|
* @ORM\JoinColumn(name="part_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
|
|
|
|
* @Assert\NotNull()
|
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?Part $part = null;
|
2020-01-05 22:49:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Supplier
|
|
|
|
* @ORM\ManyToOne(targetEntity="App\Entity\Parts\Supplier", inversedBy="orderdetails")
|
|
|
|
* @ORM\JoinColumn(name="id_supplier", referencedColumnName="id")
|
2022-07-23 21:41:54 +02:00
|
|
|
* @Assert\NotNull(message="validator.orderdetail.supplier_must_not_be_null")
|
2023-03-13 00:52:22 +01:00
|
|
|
* @Groups({"extended", "full", "import"})
|
2020-01-05 22:49:00 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?Supplier $supplier = null;
|
2020-01-05 22:49:00 +01:00
|
|
|
|
2019-08-30 14:25:05 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->pricedetails = new ArrayCollection();
|
|
|
|
}
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
if ($this->id) {
|
|
|
|
$this->addedDate = null;
|
|
|
|
$pricedetails = $this->pricedetails;
|
|
|
|
$this->pricedetails = new ArrayCollection();
|
|
|
|
//Set master attachment is needed
|
|
|
|
foreach ($pricedetails as $pricedetail) {
|
|
|
|
$this->addPricedetail(clone $pricedetail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent::__clone();
|
|
|
|
}
|
|
|
|
|
2020-05-20 23:36:44 +02:00
|
|
|
/**
|
|
|
|
* Helper for updating the timestamp. It is automatically called by doctrine before persisting.
|
|
|
|
*
|
|
|
|
* @ORM\PrePersist
|
|
|
|
* @ORM\PreUpdate
|
|
|
|
*/
|
|
|
|
public function updateTimestamps(): void
|
|
|
|
{
|
|
|
|
$this->lastModified = new DateTime('now');
|
|
|
|
if (null === $this->addedDate) {
|
|
|
|
$this->addedDate = new DateTime('now');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->part instanceof Part) {
|
|
|
|
$this->part->updateTimestamps();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/********************************************************************************
|
|
|
|
*
|
|
|
|
* Getters
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get the part.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-11-09 00:31:42 +01:00
|
|
|
* @return Part|null the part of this orderdetails
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-11-09 00:31:42 +01:00
|
|
|
public function getPart(): ?Part
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
return $this->part;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get the supplier.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @return Supplier the supplier of this orderdetails
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-29 13:06:04 +02:00
|
|
|
public function getSupplier(): ?Supplier
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
return $this->supplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the supplier part-nr.
|
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @return string the part-nr
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function getSupplierPartNr(): string
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
return $this->supplierpartnr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get if this orderdetails is obsolete.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
|
|
|
* "Orderdetails is obsolete" means that the part with that supplier-part-nr
|
|
|
|
* is no longer available from the supplier of that orderdetails.
|
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @return bool * true if this part is obsolete at that supplier
|
|
|
|
* * false if this part isn't obsolete at that supplier
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function getObsolete(): bool
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2022-12-18 19:45:04 +01:00
|
|
|
return $this->obsolete;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-05 03:01:25 +01:00
|
|
|
* Get the link to the website of the article on the supplier's website.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2020-01-05 15:46:58 +01:00
|
|
|
* @param bool $no_automatic_url Set this to true, if you only want to get the local set product URL for this Orderdetail
|
2023-02-05 03:01:25 +01:00
|
|
|
* and not an automatic generated one, based from the Supplier
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @return string the link to the article
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function getSupplierProductUrl(bool $no_automatic_url = false): string
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-08-02 12:17:56 +02:00
|
|
|
if ($no_automatic_url || '' !== $this->supplier_product_url) {
|
|
|
|
return $this->supplier_product_url;
|
2019-03-20 22:53:06 +01:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $this->getSupplier()) {
|
2019-09-16 22:04:59 +02:00
|
|
|
return '';
|
2019-08-31 13:43:41 +02:00
|
|
|
}
|
|
|
|
|
2019-03-20 22:53:06 +01:00
|
|
|
return $this->getSupplier()->getAutoProductUrl($this->supplierpartnr); // maybe an automatic url is available...
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get all pricedetails.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-09-01 13:56:14 +02:00
|
|
|
* @return Pricedetail[]|Collection all pricedetails as a one-dimensional array of Pricedetails objects,
|
2019-11-09 00:47:20 +01:00
|
|
|
* sorted by minimum discount quantity
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-30 14:25:05 +02:00
|
|
|
public function getPricedetails(): Collection
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
return $this->pricedetails;
|
|
|
|
}
|
|
|
|
|
2019-08-30 14:25:05 +02:00
|
|
|
/**
|
2023-02-05 03:01:25 +01:00
|
|
|
* Adds a price detail to this orderdetail.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-30 14:25:05 +02:00
|
|
|
* @param Pricedetail $pricedetail The pricedetail to add
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-30 14:25:05 +02:00
|
|
|
* @return Orderdetail
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function addPricedetail(Pricedetail $pricedetail): self
|
2019-08-30 14:25:05 +02:00
|
|
|
{
|
|
|
|
$pricedetail->setOrderdetail($this);
|
|
|
|
$this->pricedetails->add($pricedetail);
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-30 14:25:05 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-05 03:01:25 +01:00
|
|
|
* Removes a price detail from this orderdetail.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-30 14:25:05 +02:00
|
|
|
* @return Orderdetail
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function removePricedetail(Pricedetail $pricedetail): self
|
2019-08-30 14:25:05 +02:00
|
|
|
{
|
|
|
|
$this->pricedetails->removeElement($pricedetail);
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-30 14:25:05 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2019-11-09 00:31:42 +01:00
|
|
|
* Find the pricedetail that is correct for the desired amount (the one with the greatest discount value with a
|
2019-11-09 00:47:20 +01:00
|
|
|
* minimum order amount of the wished quantity).
|
|
|
|
*
|
|
|
|
* @param float $quantity this is the quantity to choose the correct pricedetails
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2023-02-05 03:01:25 +01:00
|
|
|
* @return Pricedetail|null the price as a bcmath string. Null if there are no orderdetails for the given quantity
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2020-02-01 17:00:03 +01:00
|
|
|
public function findPriceForQty(float $quantity = 1.0): ?Pricedetail
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-09-01 18:52:22 +02:00
|
|
|
if ($quantity <= 0) {
|
|
|
|
return null;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$all_pricedetails = $this->getPricedetails();
|
|
|
|
|
2019-09-01 18:52:22 +02:00
|
|
|
$correct_pricedetail = null;
|
|
|
|
foreach ($all_pricedetails as $pricedetail) {
|
2019-11-10 14:00:56 +01:00
|
|
|
// choose the correct pricedetails for the chosen quantity ($quantity)
|
2019-09-01 18:52:22 +02:00
|
|
|
if ($quantity < $pricedetail->getMinDiscountQuantity()) {
|
2019-02-23 22:41:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:52:22 +02:00
|
|
|
$correct_pricedetail = $pricedetail;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
2019-09-01 18:52:22 +02:00
|
|
|
return $correct_pricedetail;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************
|
|
|
|
*
|
|
|
|
* Setters
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-08-30 14:25:05 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Sets a new part with which this orderdetail is associated.
|
|
|
|
*
|
2019-09-16 22:04:59 +02:00
|
|
|
* @return Orderdetail
|
2019-08-30 14:25:05 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setPart(Part $part): self
|
2019-08-30 14:25:05 +02:00
|
|
|
{
|
|
|
|
$this->part = $part;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-16 22:04:59 +02:00
|
|
|
return $this;
|
2019-08-30 14:25:05 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2019-08-20 18:39:57 +02:00
|
|
|
* Sets the new supplier associated with this orderdetail.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-20 18:39:57 +02:00
|
|
|
* @return Orderdetail
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setSupplier(Supplier $new_supplier): self
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-08-20 18:39:57 +02:00
|
|
|
$this->supplier = $new_supplier;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-02-24 12:54:11 +01:00
|
|
|
return $this;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the supplier part-nr.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @param string $new_supplierpartnr the new supplier-part-nr
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-20 18:39:57 +02:00
|
|
|
* @return Orderdetail
|
|
|
|
* @return Orderdetail
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function setSupplierpartnr(string $new_supplierpartnr): self
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
$this->supplierpartnr = $new_supplierpartnr;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-02-24 12:54:11 +01:00
|
|
|
return $this;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Set if the part is obsolete at the supplier of that orderdetails.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @param bool $new_obsolete true means that this part is obsolete
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-20 18:39:57 +02:00
|
|
|
* @return Orderdetail
|
|
|
|
* @return Orderdetail
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function setObsolete(bool $new_obsolete): self
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
$this->obsolete = $new_obsolete;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-02-24 12:54:11 +01:00
|
|
|
return $this;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the custom product supplier URL for this order detail.
|
|
|
|
* Set this to "", if the function getSupplierProductURL should return the automatic generated URL.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-05 15:46:58 +01:00
|
|
|
* @param string $new_url The new URL for the supplier URL
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-20 18:39:57 +02:00
|
|
|
* @return Orderdetail
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setSupplierProductUrl(string $new_url): self
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-08-30 14:25:05 +02:00
|
|
|
//Only change the internal URL if it is not the auto generated one
|
2019-09-16 22:04:59 +02:00
|
|
|
if ($new_url === $this->supplier->getAutoProductUrl($this->getSupplierPartNr())) {
|
2019-08-30 14:25:05 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-02-24 12:54:11 +01:00
|
|
|
$this->supplier_product_url = $new_url;
|
|
|
|
|
|
|
|
return $this;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
2020-03-28 18:55:02 +01:00
|
|
|
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->getSupplierPartNr();
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|