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

174 lines
6.2 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
namespace App\Entity\Parts;
2019-02-23 22:41:13 +01:00
2019-09-24 16:09:54 +02:00
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentContainingDBElement;
2020-03-29 15:59:00 +02:00
use App\Entity\Attachments\PartAttachment;
use App\Entity\Parts\PartTraits\ProjectTrait;
use App\Entity\ProjectSystem\Project;
2020-03-15 13:56:31 +01:00
use App\Entity\Parameters\ParametersTrait;
use App\Entity\Parameters\PartParameter;
use App\Entity\Parts\PartTraits\AdvancedPropertyTrait;
use App\Entity\Parts\PartTraits\BasicPropertyTrait;
use App\Entity\Parts\PartTraits\InstockTrait;
use App\Entity\Parts\PartTraits\ManufacturerTrait;
use App\Entity\Parts\PartTraits\OrderTrait;
use App\Entity\ProjectSystem\ProjectBOMEntry;
2020-01-05 22:49:00 +01:00
use DateTime;
2019-08-19 23:41:58 +02:00
use Doctrine\Common\Collections\ArrayCollection;
2020-03-29 15:59:00 +02:00
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;
2023-03-12 01:12:35 +01:00
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
2019-02-23 22:41:13 +01:00
/**
* Part class.
*
* The class properties are split over various traits in directory PartTraits.
* Otherwise this class would be too big, to be maintained.
*
* @ORM\Entity(repositoryClass="App\Repository\PartRepository")
* @ORM\Table("`parts`", indexes={
* @ORM\Index(name="parts_idx_datet_name_last_id_needs", columns={"datetime_added", "name", "last_modified", "id", "needs_review"}),
* @ORM\Index(name="parts_idx_name", columns={"name"}),
* @ORM\Index(name="parts_idx_ipn", columns={"ipn"}),
* })
* @UniqueEntity(fields={"ipn"}, message="part.ipn.must_be_unique")
2019-02-23 22:41:13 +01:00
*/
class Part extends AttachmentContainingDBElement
{
use AdvancedPropertyTrait;
//use MasterAttachmentTrait;
use BasicPropertyTrait;
use InstockTrait;
use ManufacturerTrait;
use OrderTrait;
use ParametersTrait;
use ProjectTrait;
2019-02-23 22:41:13 +01:00
2020-03-29 23:13:25 +02:00
/** @var Collection<int, PartParameter>
* @Assert\Valid()
* @ORM\OneToMany(targetEntity="App\Entity\Parameters\PartParameter", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"group" = "ASC" ,"name" = "ASC"})
2023-03-12 01:12:35 +01:00
* @Groups({"full"})
*/
protected $parameters;
/**
* @ORM\Column(type="datetime", name="datetime_added", options={"default"="CURRENT_TIMESTAMP"})
*/
2022-09-18 22:59:31 +02:00
protected ?DateTime $addedDate = null;
/** *************************************************************
2019-11-10 14:00:56 +01:00
* Overridden properties
2020-01-05 22:49:00 +01:00
* (They are defined here and not in a trait, to avoid conflicts).
2019-09-16 22:04:59 +02:00
****************************************************************/
/**
* @var string The name of this part
* @ORM\Column(type="string")
*/
2022-09-18 22:59:31 +02:00
protected string $name = '';
2019-09-16 22:04:59 +02:00
/**
2020-03-29 23:13:25 +02:00
* @var Collection<int, PartAttachment>
* @ORM\OneToMany(targetEntity="App\Entity\Attachments\PartAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"name" = "ASC"})
* @Assert\Valid()
2023-03-12 01:12:35 +01:00
* @Groups({"full"})
2019-02-23 22:41:13 +01:00
*/
protected $attachments;
2020-01-05 22:49:00 +01:00
/**
* @var DateTime the date when this element was modified the last time
* @ORM\Column(type="datetime", name="last_modified", options={"default"="CURRENT_TIMESTAMP"})
*/
2022-09-18 22:59:31 +02:00
protected ?DateTime $lastModified = null;
2020-01-05 22:49:00 +01:00
2019-09-24 16:09:54 +02:00
/**
* @var Attachment
* @ORM\ManyToOne(targetEntity="App\Entity\Attachments\Attachment")
* @ORM\JoinColumn(name="id_preview_attachment", referencedColumnName="id", onDelete="SET NULL", nullable=true)
2019-09-24 16:09:54 +02:00
* @Assert\Expression("value == null or value.isPicture()", message="part.master_attachment.must_be_picture")
*/
2022-09-18 22:59:31 +02:00
protected ?Attachment $master_picture_attachment = null;
2019-09-24 16:09:54 +02:00
2019-08-19 23:41:58 +02:00
public function __construct()
{
parent::__construct();
$this->partLots = new ArrayCollection();
$this->orderdetails = new ArrayCollection();
$this->parameters = new ArrayCollection();
$this->project_bom_entries = new ArrayCollection();
2019-08-19 23:41:58 +02:00
}
2020-03-15 13:56:31 +01:00
public function __clone()
{
if ($this->id) {
//Deep clone part lots
$lots = $this->partLots;
$this->partLots = new ArrayCollection();
foreach ($lots as $lot) {
$this->addPartLot(clone $lot);
}
//Deep clone order details
$orderdetails = $this->orderdetails;
$this->orderdetails = new ArrayCollection();
foreach ($orderdetails as $orderdetail) {
$this->addOrderdetail(clone $orderdetail);
}
//Deep clone parameters
$parameters = $this->parameters;
$this->parameters = new ArrayCollection();
foreach ($parameters as $parameter) {
$this->addParameter(clone $parameter);
}
2020-03-15 13:56:31 +01:00
}
parent::__clone();
}
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context, $payload)
{
//Ensure that the part name fullfills the regex of the category
if ($this->category) {
$regex = $this->category->getPartnameRegex();
if (!empty($regex)) {
if (!preg_match($regex, $this->name)) {
$context->buildViolation('part.name.must_match_category_regex')
->atPath('name')
->setParameter('%regex%', $regex)
->addViolation();
}
}
}
}
}