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\Parts;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2023-10-03 16:29:04 +02:00
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
|
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
|
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
|
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\RangeFilter;
|
2023-09-03 23:58:09 +02:00
|
|
|
use ApiPlatform\Metadata\ApiFilter;
|
2023-09-18 23:38:06 +02:00
|
|
|
use ApiPlatform\Metadata\ApiProperty;
|
2023-09-03 23:58:09 +02:00
|
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
|
|
use ApiPlatform\Metadata\Delete;
|
|
|
|
use ApiPlatform\Metadata\Get;
|
|
|
|
use ApiPlatform\Metadata\GetCollection;
|
|
|
|
use ApiPlatform\Metadata\Link;
|
|
|
|
use ApiPlatform\Metadata\Patch;
|
|
|
|
use ApiPlatform\Metadata\Post;
|
|
|
|
use ApiPlatform\Serializer\Filter\PropertyFilter;
|
2023-10-02 00:22:15 +02:00
|
|
|
use App\ApiPlatform\DocumentedAPIProperty;
|
2023-10-03 21:37:58 +02:00
|
|
|
use App\ApiPlatform\Filter\EntityFilter;
|
2023-10-03 16:29:04 +02:00
|
|
|
use App\ApiPlatform\Filter\LikeFilter;
|
2023-10-03 21:59:33 +02:00
|
|
|
use App\ApiPlatform\Filter\PartStoragelocationFilter;
|
2023-07-19 22:49:40 +02:00
|
|
|
use App\Entity\Attachments\AttachmentTypeAttachment;
|
2023-11-12 21:53:45 +01:00
|
|
|
use App\Entity\Parts\PartTraits\AssociationTrait;
|
2023-06-11 14:55:06 +02:00
|
|
|
use App\Repository\PartRepository;
|
|
|
|
use Doctrine\DBAL\Types\Types;
|
2019-09-24 16:09:54 +02:00
|
|
|
use App\Entity\Attachments\Attachment;
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Attachments\AttachmentContainingDBElement;
|
2020-03-29 15:59:00 +02:00
|
|
|
use App\Entity\Attachments\PartAttachment;
|
2022-12-29 13:27:33 +01:00
|
|
|
use App\Entity\Parts\PartTraits\ProjectTrait;
|
2020-03-15 13:56:31 +01:00
|
|
|
use App\Entity\Parameters\ParametersTrait;
|
2020-03-11 21:48:47 +01:00
|
|
|
use App\Entity\Parameters\PartParameter;
|
2019-09-16 21:40:47 +02:00
|
|
|
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;
|
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;
|
2023-11-12 21:53:45 +01:00
|
|
|
use Jfcherng\Diff\Utility\Arr;
|
2022-12-04 02:28:47 +01:00
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
2023-03-12 01:12:35 +01:00
|
|
|
use Symfony\Component\Serializer\Annotation\Groups;
|
2019-03-13 13:23:12 +01:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
2023-01-28 21:36:19 +01:00
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
2019-09-16 13:27:53 +02:00
|
|
|
* Part class.
|
|
|
|
*
|
2019-09-16 21:40:47 +02:00
|
|
|
* The class properties are split over various traits in directory PartTraits.
|
2023-04-15 23:14:53 +02:00
|
|
|
* Otherwise, this class would be too big, to be maintained.
|
2023-06-11 15:02:59 +02:00
|
|
|
* @see \App\Tests\Entity\Parts\PartTest
|
2023-06-18 00:00:58 +02:00
|
|
|
* @extends AttachmentContainingDBElement<PartAttachment>
|
|
|
|
* @template-use ParametersTrait<PartParameter>
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[UniqueEntity(fields: ['ipn'], message: 'part.ipn.must_be_unique')]
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Entity(repositoryClass: PartRepository::class)]
|
2023-05-28 01:51:13 +02:00
|
|
|
#[ORM\Table('`parts`')]
|
|
|
|
#[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'])]
|
2023-09-03 23:58:09 +02:00
|
|
|
#[ApiResource(
|
|
|
|
operations: [
|
2023-09-18 23:38:06 +02:00
|
|
|
new Get(normalizationContext: ['groups' => ['part:read', 'provider_reference:read', 'api:basic:read', 'part_lot:read',
|
2023-09-19 11:44:22 +02:00
|
|
|
'orderdetail:read', 'pricedetail:read', 'parameter:read', 'attachment:read'],
|
|
|
|
'openapi_definition_name' => 'Read',
|
|
|
|
], security: 'is_granted("read", object)'),
|
2023-09-03 23:58:09 +02:00
|
|
|
new GetCollection(security: 'is_granted("@parts.read")'),
|
|
|
|
new Post(securityPostDenormalize: 'is_granted("create", object)'),
|
|
|
|
new Patch(security: 'is_granted("edit", object)'),
|
|
|
|
new Delete(security: 'is_granted("delete", object)'),
|
|
|
|
],
|
2023-09-18 21:31:55 +02:00
|
|
|
normalizationContext: ['groups' => ['part:read', 'provider_reference:read', 'api:basic:read', 'part_lot:read'], 'openapi_definition_name' => 'Read'],
|
2023-09-03 23:58:09 +02:00
|
|
|
denormalizationContext: ['groups' => ['part:write', 'api:basic:write'], 'openapi_definition_name' => 'Write'],
|
|
|
|
)]
|
|
|
|
#[ApiFilter(PropertyFilter::class)]
|
2023-10-03 21:37:58 +02:00
|
|
|
#[ApiFilter(EntityFilter::class, properties: ["category", "footprint", "manufacturer", "partUnit"])]
|
2023-10-03 21:59:33 +02:00
|
|
|
#[ApiFilter(PartStoragelocationFilter::class, properties: ["storage_location"])]
|
2023-10-03 16:29:04 +02:00
|
|
|
#[ApiFilter(LikeFilter::class, properties: ["name", "comment", "description", "ipn", "tags", "manufacturer_product_number"])]
|
2023-10-03 21:37:58 +02:00
|
|
|
#[ApiFilter(BooleanFilter::class, properties: ["favorite" , "needs_review"])]
|
2023-10-03 16:29:04 +02:00
|
|
|
#[ApiFilter(RangeFilter::class, properties: ["mass", "minamount"])]
|
|
|
|
#[ApiFilter(DateFilter::class, strategy: DateFilter::EXCLUDE_NULL)]
|
|
|
|
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
|
2023-10-02 00:22:15 +02:00
|
|
|
#[DocumentedAPIProperty(schemaName: 'Part-Read', property: 'total_instock', type: 'number', nullable: false,
|
|
|
|
description: 'The total amount of this part in stock (sum of all part lots).')]
|
2019-02-23 22:41:13 +01:00
|
|
|
class Part extends AttachmentContainingDBElement
|
|
|
|
{
|
2019-09-16 21:40:47 +02:00
|
|
|
use AdvancedPropertyTrait;
|
2019-09-24 13:39:49 +02:00
|
|
|
//use MasterAttachmentTrait;
|
2019-09-16 21:40:47 +02:00
|
|
|
use BasicPropertyTrait;
|
|
|
|
use InstockTrait;
|
|
|
|
use ManufacturerTrait;
|
|
|
|
use OrderTrait;
|
2020-03-11 21:48:47 +01:00
|
|
|
use ParametersTrait;
|
2022-12-29 13:27:33 +01:00
|
|
|
use ProjectTrait;
|
2023-11-12 21:53:45 +01:00
|
|
|
use AssociationTrait;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2020-03-29 23:13:25 +02:00
|
|
|
/** @var Collection<int, PartParameter>
|
2020-03-11 21:48:47 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Valid]
|
2023-09-19 11:44:22 +02:00
|
|
|
#[Groups(['full', 'part:read', 'part:write'])]
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\OneToMany(targetEntity: PartParameter::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
2023-05-28 01:51:13 +02:00
|
|
|
#[ORM\OrderBy(['group' => 'ASC', 'name' => 'ASC'])]
|
2023-04-15 22:25:03 +02:00
|
|
|
protected Collection $parameters;
|
2020-03-11 21:48:47 +01:00
|
|
|
|
2019-03-20 22:30:03 +01:00
|
|
|
|
2020-01-05 22:32:19 +01:00
|
|
|
/** *************************************************************
|
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
|
|
|
****************************************************************/
|
2023-08-01 15:55:21 +02:00
|
|
|
|
2019-09-16 22:04:59 +02:00
|
|
|
/**
|
|
|
|
* @var string The name of this part
|
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected string $name = '';
|
2019-09-16 22:04:59 +02:00
|
|
|
|
2019-03-20 19:11:34 +01:00
|
|
|
/**
|
2020-03-29 23:13:25 +02:00
|
|
|
* @var Collection<int, PartAttachment>
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Valid]
|
2023-09-18 23:38:06 +02:00
|
|
|
#[Groups(['full', 'part:read', 'part:write'])]
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\OneToMany(targetEntity: PartAttachment::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
2023-05-28 01:51:13 +02:00
|
|
|
#[ORM\OrderBy(['name' => 'ASC'])]
|
2023-04-15 22:25:03 +02:00
|
|
|
protected Collection $attachments;
|
2019-08-12 15:47:57 +02:00
|
|
|
|
2019-09-24 16:09:54 +02:00
|
|
|
/**
|
2023-04-15 21:49:19 +02:00
|
|
|
* @var Attachment|null
|
2019-09-24 16:09:54 +02:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Expression('value == null or value.isPicture()', message: 'part.master_attachment.must_be_picture')]
|
2023-07-19 22:49:40 +02:00
|
|
|
#[ORM\ManyToOne(targetEntity: PartAttachment::class)]
|
2023-05-28 01:51:13 +02:00
|
|
|
#[ORM\JoinColumn(name: 'id_preview_attachment', onDelete: 'SET NULL')]
|
2023-09-18 23:38:06 +02:00
|
|
|
#[Groups(['part:read', 'part:write'])]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?Attachment $master_picture_attachment = null;
|
2019-09-24 16:09:54 +02:00
|
|
|
|
2023-10-04 00:08:10 +02:00
|
|
|
#[Groups(['part:read'])]
|
|
|
|
protected ?\DateTimeInterface $addedDate = null;
|
|
|
|
#[Groups(['part:read'])]
|
|
|
|
protected ?\DateTimeInterface $lastModified = null;
|
|
|
|
|
|
|
|
|
2019-08-19 23:41:58 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2023-06-11 14:55:06 +02:00
|
|
|
$this->attachments = new ArrayCollection();
|
2019-08-19 23:41:58 +02:00
|
|
|
parent::__construct();
|
|
|
|
$this->partLots = new ArrayCollection();
|
2019-08-30 14:25:05 +02:00
|
|
|
$this->orderdetails = new ArrayCollection();
|
2020-03-11 21:48:47 +01:00
|
|
|
$this->parameters = new ArrayCollection();
|
2022-12-18 23:58:04 +01:00
|
|
|
$this->project_bom_entries = new ArrayCollection();
|
2023-07-16 01:24:49 +02:00
|
|
|
|
2023-11-12 21:53:45 +01:00
|
|
|
$this->associated_parts_as_owner = new ArrayCollection();
|
|
|
|
$this->associated_parts_as_other = new ArrayCollection();
|
|
|
|
|
2023-07-16 01:24:49 +02:00
|
|
|
//By default, the part has no provider
|
|
|
|
$this->providerReference = InfoProviderReference::noProvider();
|
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);
|
|
|
|
}
|
2020-05-13 21:07:50 +02:00
|
|
|
|
|
|
|
//Deep clone parameters
|
|
|
|
$parameters = $this->parameters;
|
|
|
|
$this->parameters = new ArrayCollection();
|
|
|
|
foreach ($parameters as $parameter) {
|
|
|
|
$this->addParameter(clone $parameter);
|
|
|
|
}
|
2023-07-16 01:24:49 +02:00
|
|
|
|
2023-11-12 21:53:45 +01:00
|
|
|
//Deep clone the owned part associations (the owned ones make not much sense without the owner)
|
|
|
|
$ownedAssociations = $this->associated_parts_as_owner;
|
|
|
|
$this->associated_parts_as_owner = new ArrayCollection();
|
|
|
|
foreach ($ownedAssociations as $association) {
|
|
|
|
$this->addAssociatedPartsAsOwner(clone $association);
|
|
|
|
}
|
|
|
|
|
2023-07-16 01:24:49 +02:00
|
|
|
//Deep clone info provider
|
|
|
|
$this->providerReference = clone $this->providerReference;
|
2020-03-15 13:56:31 +01:00
|
|
|
}
|
|
|
|
parent::__clone();
|
|
|
|
}
|
2023-01-28 21:36:19 +01:00
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Callback]
|
2023-06-18 15:37:42 +02:00
|
|
|
public function validate(ExecutionContextInterface $context, $payload): void
|
2023-01-28 21:36:19 +01:00
|
|
|
{
|
|
|
|
//Ensure that the part name fullfills the regex of the category
|
2023-06-11 14:55:06 +02:00
|
|
|
if ($this->category instanceof Category) {
|
2023-01-28 21:36:19 +01:00
|
|
|
$regex = $this->category->getPartnameRegex();
|
2023-06-11 18:59:07 +02:00
|
|
|
if ($regex !== '' && !preg_match($regex, $this->name)) {
|
2023-06-11 14:15:46 +02:00
|
|
|
$context->buildViolation('part.name.must_match_category_regex')
|
|
|
|
->atPath('name')
|
|
|
|
->setParameter('%regex%', $regex)
|
|
|
|
->addViolation();
|
2023-01-28 21:36:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|