Split Part entity class into multiple traits.

The part class has become very big and clumsy, that way it should be easier to maintain this entity...
This commit is contained in:
Jan Böhmer 2019-09-16 21:40:47 +02:00
parent 3ecbe19fd6
commit f7c2f1032f
8 changed files with 1041 additions and 774 deletions

View file

@ -0,0 +1,208 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Entity\Parts\PartTraits;
use App\Entity\Parts\Part;
use App\Entity\PriceInformations\Orderdetail;
use App\Security\Annotations\ColumnSecurity;
use Doctrine\Common\Collections\Collection;
/**
* This trait collects all aspects of a part related to orders and priceinformations.
* @package App\Entity\Parts\PartTraits
*/
trait OrderTrait
{
/**
* @var Orderdetail[] The details about how and where you can order this part.
* @ORM\OneToMany(targetEntity="App\Entity\PriceInformations\Orderdetail", mappedBy="part", cascade={"persist", "remove"}, orphanRemoval=false)
* @Assert\Valid()
* @ColumnSecurity(prefix="orderdetails", type="collection")
*/
protected $orderdetails;
/**
* @var int
* @ORM\Column(type="integer")
* @ColumnSecurity(prefix="order", type="integer")
*/
protected $order_quantity = 0;
/**
* @var bool
* @ORM\Column(type="boolean")
* @ColumnSecurity(prefix="order", type="boolean")
*/
protected $manual_order = false;
/**
* @var Orderdetail
* @ORM\OneToOne(targetEntity="App\Entity\PriceInformations\Orderdetail")
* @ORM\JoinColumn(name="order_orderdetails_id", referencedColumnName="id")
*
* @ColumnSecurity(prefix="order", type="object")
*/
protected $order_orderdetail;
/**
* 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) {
$orderdetails = $this->orderdetails;
foreach ($orderdetails as $key => $details) {
if ($details->getObsolete()) {
unset($orderdetails[$key]);
}
}
return $orderdetails;
}
return $this->orderdetails;
}
/**
* Adds the given orderdetail to list of orderdetails.
* The orderdetail is assigned to this part.
* @param Orderdetail $orderdetail The orderdetail that should be added.
* @return self
*/
public function addOrderdetail(Orderdetail $orderdetail) : self
{
$orderdetail->setPart($this);
$this->orderdetails->add($orderdetail);
return $this;
}
/**
* Removes the given orderdetail from the list of orderdetails.
* @param Orderdetail $orderdetail
* @return OrderTrait
*/
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
* @param int|null $new_order_orderdetails_id * 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")
*
* @return self
*/
public function setManualOrder(bool $new_manual_order, int $new_order_quantity = 1, ?Orderdetail $new_order_orderdetail = null): Part
{
//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();
if (0 === count($all_orderdetails)) {
return false;
}
foreach ($all_orderdetails as $orderdetails) {
if (!$orderdetails->getObsolete()) {
return false;
}
}
return true;
}
}