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-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-02-16 23:48:57 +01:00
|
|
|
use App\Entity\Contracts\TimeStampableInterface;
|
2020-05-20 22:02:07 +02:00
|
|
|
use App\Validator\Constraints\BigDecimal\BigDecimalPositive;
|
2019-08-12 21:47:25 +02:00
|
|
|
use App\Validator\Constraints\Selectable;
|
2020-05-20 22:02:07 +02:00
|
|
|
use Brick\Math\BigDecimal;
|
|
|
|
use Brick\Math\RoundingMode;
|
2020-05-20 23:36:44 +02:00
|
|
|
use DateTime;
|
2019-02-23 22:41:13 +01:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2019-08-12 15:47:57 +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-12 15:47:57 +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 Pricedetail.
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[UniqueEntity(fields: ['min_discount_quantity', 'orderdetail'])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Entity]
|
|
|
|
#[ORM\HasLifecycleCallbacks]
|
|
|
|
#[ORM\Table('`pricedetails`')]
|
|
|
|
#[ORM\Index(name: 'pricedetails_idx_min_discount', columns: ['min_discount_quantity'])]
|
|
|
|
#[ORM\Index(name: 'pricedetails_idx_min_discount_price_qty', columns: ['min_discount_quantity', 'price_related_quantity'])]
|
2020-02-16 23:48:57 +01:00
|
|
|
class Pricedetail extends AbstractDBElement implements TimeStampableInterface
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-08-29 13:06:04 +02:00
|
|
|
use TimestampTrait;
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
public const PRICE_PRECISION = 5;
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2020-10-02 12:42:15 +02:00
|
|
|
* @var BigDecimal The price related to the detail. (Given in the selected currency)
|
2020-05-20 22:02:07 +02:00
|
|
|
* @BigDecimalPositive()
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Groups(['extended', 'full'])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Column(type: 'big_decimal', precision: 11, scale: 5)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected BigDecimal $price;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
/**
|
|
|
|
* @var ?Currency The currency used for the current price information.
|
2019-11-09 00:47:20 +01:00
|
|
|
* If this is null, the global base unit is assumed.
|
2019-08-12 21:47:25 +02:00
|
|
|
* @Selectable()
|
2019-08-12 15:47:57 +02:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Groups(['extended', 'full', 'import'])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\ManyToOne(targetEntity: 'Currency', inversedBy: 'pricedetails')]
|
|
|
|
#[ORM\JoinColumn(name: 'id_currency')]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?Currency $currency = null;
|
2019-08-12 15:47:57 +02:00
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2019-08-31 13:08:02 +02:00
|
|
|
* @var float
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Positive]
|
|
|
|
#[Groups(['extended', 'full', 'import'])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::FLOAT)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected float $price_related_quantity = 1.0;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-31 13:08:02 +02:00
|
|
|
* @var float
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Positive]
|
|
|
|
#[Groups(['extended', 'full', 'import'])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::FLOAT)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected float $min_discount_quantity = 1.0;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected bool $manual_input = true;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
/**
|
2020-02-01 19:42:28 +01:00
|
|
|
* @var Orderdetail|null
|
2020-01-05 22:49:00 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\NotNull]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\ManyToOne(targetEntity: 'Orderdetail', inversedBy: 'pricedetails')]
|
|
|
|
#[ORM\JoinColumn(name: 'orderdetails_id', nullable: false, onDelete: 'CASCADE')]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?Orderdetail $orderdetail = null;
|
2020-01-05 22:49:00 +01:00
|
|
|
|
2019-09-01 13:56:14 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2020-05-20 22:02:07 +02:00
|
|
|
$this->price = BigDecimal::zero()->toScale(self::PRICE_PRECISION);
|
2019-09-01 13:56:14 +02:00
|
|
|
}
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
if ($this->id) {
|
|
|
|
$this->addedDate = null;
|
|
|
|
}
|
|
|
|
parent::__clone();
|
|
|
|
}
|
|
|
|
|
2020-05-20 23:36:44 +02:00
|
|
|
/**
|
|
|
|
* Helper for updating the timestamp. It is automatically called by doctrine before persisting.
|
|
|
|
*/
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\PrePersist]
|
|
|
|
#[ORM\PreUpdate]
|
2020-05-20 23:36:44 +02:00
|
|
|
public function updateTimestamps(): void
|
|
|
|
{
|
|
|
|
$this->lastModified = new DateTime('now');
|
|
|
|
if (null === $this->addedDate) {
|
|
|
|
$this->addedDate = new DateTime('now');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->orderdetail instanceof Orderdetail) {
|
|
|
|
$this->orderdetail->updateTimestamps();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/********************************************************************************
|
|
|
|
*
|
|
|
|
* Getters
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2020-03-29 23:13:25 +02:00
|
|
|
* Get the orderdetail to which this pricedetail belongs to this pricedetails.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2020-03-29 23:13:25 +02:00
|
|
|
* @return Orderdetail|null the orderdetail this price belongs to
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2020-03-29 23:13:25 +02:00
|
|
|
public function getOrderdetail(): ?Orderdetail
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
return $this->orderdetail;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:34:11 +02:00
|
|
|
/**
|
|
|
|
* Returns the price associated with this pricedetail.
|
|
|
|
* It is given in current currency and for the price related quantity.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-05-20 22:02:07 +02:00
|
|
|
* @return BigDecimal the price as BigDecimal object, like returned raw from DB
|
2019-09-01 12:34:11 +02:00
|
|
|
*/
|
2020-05-20 22:02:07 +02:00
|
|
|
public function getPrice(): BigDecimal
|
2019-09-01 12:34:11 +02:00
|
|
|
{
|
|
|
|
return $this->price;
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2020-03-29 23:13:25 +02:00
|
|
|
* Get the price for a single unit in the currency associated with this price detail.
|
|
|
|
*
|
2020-05-20 22:02:07 +02:00
|
|
|
* @param float|string|BigDecimal $multiplier The returned price (float or string) will be multiplied
|
2020-03-29 23:13:25 +02:00
|
|
|
* with this multiplier.
|
|
|
|
*
|
|
|
|
* You will get the price for $multiplier parts. If you want the price which is stored
|
|
|
|
* in the database, you have to pass the "price_related_quantity" count as $multiplier.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2020-05-20 22:02:07 +02:00
|
|
|
* @return BigDecimal the price as a bcmath string
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2020-05-20 22:02:07 +02:00
|
|
|
public function getPricePerUnit($multiplier = 1.0): BigDecimal
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2020-05-20 22:02:07 +02:00
|
|
|
$tmp = BigDecimal::of($multiplier);
|
|
|
|
$tmp = $tmp->multipliedBy($this->price);
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2020-05-20 22:02:07 +02:00
|
|
|
return $tmp->dividedBy($this->price_related_quantity, static::PRICE_PRECISION, RoundingMode::HALF_UP);
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get the price related quantity.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
|
|
|
* This is the quantity, for which the price is valid.
|
2019-08-31 13:08:02 +02:00
|
|
|
* The amount is measured in part unit.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-08-31 13:08:02 +02:00
|
|
|
* @return float the price related quantity
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-08-12 15:47:57 +02:00
|
|
|
* @see Pricedetail::setPriceRelatedQuantity()
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-31 13:08:02 +02:00
|
|
|
public function getPriceRelatedQuantity(): float
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if ($this->orderdetail && $this->orderdetail->getPart() && !$this->orderdetail->getPart()->useFloatAmount()) {
|
2019-09-16 22:04:59 +02:00
|
|
|
$tmp = round($this->price_related_quantity);
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2022-12-18 19:45:04 +01:00
|
|
|
return max($tmp, 1);
|
2019-08-31 13:08:02 +02:00
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-03-05 17:02:48 +01:00
|
|
|
return $this->price_related_quantity;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get the minimum discount quantity.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
|
|
|
* "Minimum discount quantity" means the minimum order quantity for which the price
|
|
|
|
* of this orderdetails is valid.
|
|
|
|
*
|
2019-08-31 13:08:02 +02:00
|
|
|
* The amount is measured in part unit.
|
|
|
|
*
|
2019-09-16 22:04:59 +02:00
|
|
|
* @return float the minimum discount quantity
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-08-12 15:47:57 +02:00
|
|
|
* @see Pricedetail::setMinDiscountQuantity()
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-31 13:08:02 +02:00
|
|
|
public function getMinDiscountQuantity(): float
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if ($this->orderdetail && $this->orderdetail->getPart() && !$this->orderdetail->getPart()->useFloatAmount()) {
|
2019-09-16 22:04:59 +02:00
|
|
|
$tmp = round($this->min_discount_quantity);
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2022-08-14 19:32:53 +02:00
|
|
|
return max($tmp, 1);
|
2019-08-31 13:08:02 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 17:02:48 +01:00
|
|
|
return $this->min_discount_quantity;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
/**
|
|
|
|
* Returns the currency associated with this price information.
|
|
|
|
* Returns null, if no specific currency is selected and the global base currency should be assumed.
|
|
|
|
*/
|
|
|
|
public function getCurrency(): ?Currency
|
|
|
|
{
|
|
|
|
return $this->currency;
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/********************************************************************************
|
|
|
|
*
|
|
|
|
* Setters
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-08-30 14:25:05 +02:00
|
|
|
/**
|
|
|
|
* Sets the orderdetail to which this pricedetail belongs to.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-30 14:25:05 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setOrderdetail(Orderdetail $orderdetail): self
|
2019-08-30 14:25:05 +02:00
|
|
|
{
|
|
|
|
$this->orderdetail = $orderdetail;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-30 14:25:05 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
/**
|
2023-02-05 03:01:25 +01:00
|
|
|
* Sets the currency associated with the price information.
|
2019-08-12 15:47:57 +02:00
|
|
|
* Set to null, to use the global base currency.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-12 15:47:57 +02:00
|
|
|
* @return Pricedetail
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setCurrency(?Currency $currency): self
|
2019-08-12 15:47:57 +02:00
|
|
|
{
|
|
|
|
$this->currency = $currency;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-20 18:39:57 +02:00
|
|
|
return $this;
|
2019-08-12 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Set the price.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2020-05-20 22:02:07 +02:00
|
|
|
* @param BigDecimal $new_price the new price as a float number
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2023-04-15 23:14:53 +02:00
|
|
|
* This is the price for "price_related_quantity" parts!!
|
|
|
|
* Example: if "price_related_quantity" is 10,
|
|
|
|
* you have to set here the price for 10 parts!
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-02-02 14:05:36 +01:00
|
|
|
* @return $this
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2020-05-20 22:02:07 +02:00
|
|
|
public function setPrice(BigDecimal $new_price): self
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2020-05-20 22:45:41 +02:00
|
|
|
$tmp = $new_price->toScale(self::PRICE_PRECISION, RoundingMode::HALF_UP);
|
|
|
|
//Only change the object, if the value changes, so that doctrine does not detect it as changed.
|
|
|
|
if ((string) $tmp !== (string) $this->price) {
|
|
|
|
$this->price = $tmp;
|
|
|
|
}
|
2020-08-21 21:36:22 +02: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 the price related quantity.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
|
|
|
* This is the quantity, for which the price is valid.
|
|
|
|
*
|
2019-04-28 12:30:33 +02:00
|
|
|
* Example:
|
2019-02-23 22:41:13 +01:00
|
|
|
* If 100pcs costs 20$, you have to set the price to 20$ and the price related
|
|
|
|
* quantity to 100. The single price (20$/100 = 0.2$) will be calculated automatically.
|
|
|
|
*
|
2019-09-16 22:04:59 +02:00
|
|
|
* @param float $new_price_related_quantity the price related quantity
|
2020-01-05 22:49:00 +01:00
|
|
|
*
|
2020-01-05 15:55:16 +01:00
|
|
|
* @return $this
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-31 13:08:02 +02:00
|
|
|
public function setPriceRelatedQuantity(float $new_price_related_quantity): self
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
$this->price_related_quantity = $new_price_related_quantity;
|
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 the minimum discount quantity.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
|
|
|
* "Minimum discount quantity" means the minimum order quantity for which the price
|
|
|
|
* of this orderdetails is valid. This way, you're able to use different prices
|
|
|
|
* for different order quantities (quantity discount!).
|
|
|
|
*
|
2019-04-28 12:30:33 +02:00
|
|
|
* Example:
|
2019-02-23 22:41:13 +01:00
|
|
|
* - 1-9pcs costs 10$: set price to 10$/pcs and minimum discount quantity to 1
|
|
|
|
* - 10-99pcs costs 9$: set price to 9$/pcs and minimum discount quantity to 10
|
|
|
|
* - 100pcs or more costs 8$: set price/pcs to 8$ and minimum discount quantity to 100
|
|
|
|
*
|
|
|
|
* (Each of this examples would be an own Pricedetails-object.
|
|
|
|
* So the orderdetails would have three Pricedetails for one supplier.)
|
|
|
|
*
|
2019-09-16 22:04:59 +02:00
|
|
|
* @param float $new_min_discount_quantity the minimum discount quantity
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-02-02 14:05:36 +01:00
|
|
|
* @return $this
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-31 13:08:02 +02:00
|
|
|
public function setMinDiscountQuantity(float $new_min_discount_quantity): self
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
$this->min_discount_quantity = $new_min_discount_quantity;
|
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
|
|
|
}
|