Part-DB-server/src/Entity/PriceInformations/Orderdetail.php

355 lines
9.9 KiB
PHP
Raw Normal View History

<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
*
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.
*
* 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.
*
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/>.
*/
declare(strict_types=1);
2019-03-05 14:37:41 +01:00
2019-02-23 22:41:13 +01:00
namespace App\Entity\PriceInformations;
2019-02-23 22:41:13 +01:00
use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\TimestampTrait;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\TimeStampableInterface;
use App\Entity\Parts\Part;
use App\Entity\Parts\Supplier;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
2019-02-23 22:41:13 +01:00
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
2019-02-23 22:41:13 +01:00
/**
* Class Orderdetail.
2019-02-23 22:41:13 +01:00
*
* @ORM\Table("`orderdetails`", indexes={
* @ORM\Index(name="orderdetails_supplier_part_nr", columns={"supplierpartnr"}),
* })
2019-02-23 22:41:13 +01:00
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity({"supplierpartnr", "supplier", "part"})
2019-02-23 22:41:13 +01:00
*/
class Orderdetail extends AbstractDBElement implements TimeStampableInterface, NamedElementInterface
2019-02-23 22:41:13 +01:00
{
use TimestampTrait;
2019-02-23 22:41:13 +01:00
/**
* @ORM\OneToMany(targetEntity="Pricedetail", mappedBy="orderdetail", cascade={"persist", "remove"}, orphanRemoval=true)
* @Assert\Valid()
* @ORM\OrderBy({"min_discount_quantity" = "ASC"})
2019-02-23 22:41:13 +01:00
*/
protected $pricedetails;
/**
* @var string
* @ORM\Column(type="string")
*/
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")
*/
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")
* @Assert\Url()
2019-02-23 22:41:13 +01:00
*/
2022-09-18 22:59:31 +02:00
protected string $supplier_product_url = '';
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")
* @Assert\NotNull(message="validator.orderdetail.supplier_must_not_be_null")
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
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();
}
/**
* 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
*
*********************************************************************************/
/**
* Get the part.
2019-02-23 22:41:13 +01:00
*
* @return Part|null the part of this orderdetails
2019-02-23 22:41:13 +01:00
*/
public function getPart(): ?Part
2019-02-23 22:41:13 +01:00
{
return $this->part;
}
/**
* Get the supplier.
2019-02-23 22:41:13 +01:00
*
* @return Supplier the supplier of this orderdetails
2019-02-23 22:41:13 +01: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
*/
public function getSupplierPartNr(): string
2019-02-23 22:41:13 +01:00
{
return $this->supplierpartnr;
}
/**
* 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.
*
* @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
*/
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
*
* @return string the link to the article
2019-02-23 22:41:13 +01:00
*/
public function getSupplierProductUrl(bool $no_automatic_url = false): string
2019-02-23 22:41:13 +01:00
{
if ($no_automatic_url || '' !== $this->supplier_product_url) {
return $this->supplier_product_url;
2019-03-20 22:53:06 +01:00
}
if (null === $this->getSupplier()) {
2019-09-16 22:04:59 +02:00
return '';
}
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
}
/**
* Get all pricedetails.
2019-02-23 22:41:13 +01:00
*
* @return Pricedetail[]|Collection all pricedetails as a one-dimensional array of Pricedetails objects,
* sorted by minimum discount quantity
2019-02-23 22:41:13 +01:00
*/
public function getPricedetails(): Collection
2019-02-23 22:41:13 +01:00
{
return $this->pricedetails;
}
/**
2023-02-05 03:01:25 +01:00
* Adds a price detail to this orderdetail.
*
* @param Pricedetail $pricedetail The pricedetail to add
*
* @return Orderdetail
*/
public function addPricedetail(Pricedetail $pricedetail): self
{
$pricedetail->setOrderdetail($this);
$this->pricedetails->add($pricedetail);
return $this;
}
/**
2023-02-05 03:01:25 +01:00
* Removes a price detail from this orderdetail.
*
* @return Orderdetail
*/
public function removePricedetail(Pricedetail $pricedetail): self
{
$this->pricedetails->removeElement($pricedetail);
return $this;
}
2019-02-23 22:41:13 +01:00
/**
* Find the pricedetail that is correct for the desired amount (the one with the greatest discount value with a
* 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
{
if ($quantity <= 0) {
return null;
2019-02-23 22:41:13 +01:00
}
$all_pricedetails = $this->getPricedetails();
$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)
if ($quantity < $pricedetail->getMinDiscountQuantity()) {
2019-02-23 22:41:13 +01:00
break;
}
$correct_pricedetail = $pricedetail;
2019-02-23 22:41:13 +01:00
}
return $correct_pricedetail;
2019-02-23 22:41:13 +01:00
}
/********************************************************************************
*
* Setters
*
*********************************************************************************/
/**
* Sets a new part with which this orderdetail is associated.
*
2019-09-16 22:04:59 +02:00
* @return Orderdetail
*/
public function setPart(Part $part): self
{
$this->part = $part;
2019-09-16 22:04:59 +02:00
return $this;
}
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-08-20 18:39:57 +02:00
* @return Orderdetail
2019-02-23 22:41:13 +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;
return $this;
2019-02-23 22:41:13 +01:00
}
/**
* Set the supplier part-nr.
*
* @param string $new_supplierpartnr the new supplier-part-nr
*
2019-08-20 18:39:57 +02:00
* @return Orderdetail
* @return Orderdetail
2019-02-23 22:41:13 +01:00
*/
public function setSupplierpartnr(string $new_supplierpartnr): self
2019-02-23 22:41:13 +01:00
{
$this->supplierpartnr = $new_supplierpartnr;
return $this;
2019-02-23 22:41:13 +01:00
}
/**
* Set if the part is obsolete at the supplier of that orderdetails.
*
* @param bool $new_obsolete true means that this part is obsolete
*
2019-08-20 18:39:57 +02:00
* @return Orderdetail
* @return Orderdetail
2019-02-23 22:41:13 +01:00
*/
public function setObsolete(bool $new_obsolete): self
2019-02-23 22:41:13 +01:00
{
$this->obsolete = $new_obsolete;
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.
*
2020-01-05 15:46:58 +01:00
* @param string $new_url The new URL for the supplier URL
*
2019-08-20 18:39:57 +02:00
* @return Orderdetail
2019-02-23 22:41:13 +01:00
*/
public function setSupplierProductUrl(string $new_url): self
2019-02-23 22:41:13 +01: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())) {
return $this;
}
$this->supplier_product_url = $new_url;
return $this;
2019-02-23 22:41:13 +01:00
}
public function getName(): string
{
return $this->getSupplierPartNr();
}
}