Part-DB.Part-DB-server/src/Entity/Parts/PartTraits/OrderTrait.php

195 lines
6.1 KiB
PHP
Raw Normal View History

<?php
2020-02-22 18:14:36 +01:00
/**
* 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
* GNU Affero General Public License for more details.
*
* 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/>.
*/
2020-01-05 15:46:58 +01:00
declare(strict_types=1);
namespace App\Entity\Parts\PartTraits;
2023-06-11 14:55:06 +02:00
use Doctrine\DBAL\Types\Types;
use App\Entity\PriceInformations\Orderdetail;
2023-03-12 01:12:35 +01:00
use Symfony\Component\Serializer\Annotation\Groups;
2022-08-14 19:32:53 +02:00
use Symfony\Component\Validator\Constraints as Assert;
2020-01-05 22:49:00 +01:00
use function count;
use Doctrine\Common\Collections\Collection;
2020-04-10 13:05:08 +02:00
use Doctrine\ORM\Mapping as ORM;
/**
* This trait collects all aspects of a part related to orders and priceinformations.
*/
trait OrderTrait
{
/**
2020-01-04 20:24:09 +01:00
* @var Orderdetail[]|Collection the details about how and where you can order this part
*/
2023-05-28 01:21:05 +02:00
#[Assert\Valid]
#[Groups(['extended', 'full', 'import'])]
2023-06-11 14:55:06 +02:00
#[ORM\OneToMany(targetEntity: Orderdetail::class, mappedBy: 'part', cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['supplierpartnr' => 'ASC'])]
2023-06-11 14:55:06 +02:00
protected Collection $orderdetails;
/**
* @var int
*/
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::INTEGER)]
2022-09-18 22:59:31 +02:00
protected int $order_quantity = 0;
/**
* @var bool
*/
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::BOOLEAN)]
2022-09-18 22:59:31 +02:00
protected bool $manual_order = false;
/**
2023-04-15 21:49:19 +02:00
* @var Orderdetail|null
*/
2023-06-11 14:55:06 +02:00
#[ORM\OneToOne(targetEntity: Orderdetail::class)]
#[ORM\JoinColumn(name: 'order_orderdetails_id')]
protected ?Orderdetail $order_orderdetail = null;
/**
* Get the selected order orderdetails of this part.
*
* @return Orderdetail the selected order orderdetails
*/
public function getOrderOrderdetails(): ?Orderdetail
{
return $this->order_orderdetail;
}
/**
* Get the order quantity of this part.
*
* @return int the order quantity
*/
public function getOrderQuantity(): int
{
return $this->order_quantity;
}
/**
* Check if this part is marked for manual ordering.
*
* @return bool the "manual_order" attribute
*/
public function isMarkedForManualOrder(): bool
{
return $this->manual_order;
}
/**
* Get all orderdetails of this part.
*
* @param bool $hide_obsolete If true, obsolete orderdetails will NOT be returned
*
* @return Collection|Orderdetail[] * all orderdetails as a one-dimensional array of Orderdetails objects
* (empty array if there are no ones)
* * the array is sorted by the suppliers names / minimum order quantity
*/
public function getOrderdetails(bool $hide_obsolete = false): Collection
{
//If needed hide the obsolete entries
if ($hide_obsolete) {
return $this->orderdetails->filter(fn(Orderdetail $orderdetail) => ! $orderdetail->getObsolete());
}
return $this->orderdetails;
}
/**
* Adds the given orderdetail to list of orderdetails.
* The orderdetail is assigned to this part.
*
2020-01-04 20:24:09 +01:00
* @param Orderdetail $orderdetail the orderdetail that should be added
2020-03-15 13:56:31 +01:00
*
2020-02-01 17:00:03 +01:00
* @return $this
*/
public function addOrderdetail(Orderdetail $orderdetail): self
{
$orderdetail->setPart($this);
$this->orderdetails->add($orderdetail);
return $this;
}
/**
* Removes the given orderdetail from the list of orderdetails.
2020-03-15 13:56:31 +01:00
*
2020-02-01 17:00:03 +01:00
* @return $this
*/
public function removeOrderdetail(Orderdetail $orderdetail): self
{
$this->orderdetails->removeElement($orderdetail);
return $this;
}
/**
* Set the "manual_order" attribute.
*
* @param bool $new_manual_order the new "manual_order" attribute
* @param int $new_order_quantity the new order quantity
2019-09-16 22:04:59 +02:00
* @param Orderdetail|null $new_order_orderdetail * the ID of the new order orderdetails
* * or Zero for "no order orderdetails"
* * or NULL for automatic order orderdetails
* (if the part has exactly one orderdetails,
* set this orderdetails as order orderdetails.
* Otherwise, set "no order orderdetails")
2020-03-15 13:56:31 +01:00
*
2020-02-01 17:00:03 +01:00
* @return $this
*/
2019-09-16 22:04:59 +02:00
public function setManualOrder(bool $new_manual_order, int $new_order_quantity = 1, ?Orderdetail $new_order_orderdetail = null): self
{
//Assert::greaterThan($new_order_quantity, 0, 'The new order quantity must be greater zero. Got %s!');
$this->manual_order = $new_manual_order;
//TODO;
$this->order_orderdetail = $new_order_orderdetail;
$this->order_quantity = $new_order_quantity;
return $this;
}
/**
* Check if this part is obsolete.
*
* A Part is marked as "obsolete" if all their orderdetails are marked as "obsolete".
* If a part has no orderdetails, the part isn't marked as obsolete.
*
* @return bool true, if this part is obsolete. false, if this part isn't obsolete
*/
public function isObsolete(): bool
{
$all_orderdetails = $this->getOrderdetails();
2020-01-05 22:49:00 +01:00
if (0 === count($all_orderdetails)) {
return false;
}
foreach ($all_orderdetails as $orderdetails) {
2020-08-21 21:36:22 +02:00
if (!$orderdetails->getObsolete()) {
return false;
}
}
return true;
}
}