Files
Part-DB-server/src/Entity/PriceInformations/Orderdetail.php
Sebastian Almberg b9d940ae33 Enhance KiCad integration: API v2, batch EDA editing, field export control (#1241)
* Add stock quantity, datasheet URL, and HTTP caching to KiCad API

- Add Stock field showing total available quantity across all part lots
- Add Storage Location field when parts have stored locations
- Resolve actual datasheet PDF from attachments (by type name, attachment
  name, or first PDF) instead of always linking to Part-DB page
- Keep Part-DB page URL as separate "Part-DB URL" field
- Add ETag and Cache-Control headers to all KiCad API endpoints
- Support conditional requests (If-None-Match) returning 304
- Categories/part lists cached 5 min, part details cached 1 min

* Add KiCadHelper unit tests and fix PDF detection for external URLs

- Add comprehensive KiCadHelperTest with 14 test cases covering:
  - Stock quantity calculation (zero, single lot, multiple lots)
  - Stock exclusion of expired and unknown-quantity lots
  - Storage location display (present, absent, multiple)
  - Datasheet URL resolution by type name, attachment name, PDF extension
  - Datasheet fallback to Part-DB URL when no match
  - "Data sheet" (with space) name variant matching
- Fix PDF extension detection for external attachments (getExtension()
  returns null for external-only attachments, now also parses URL path)

* Fix 304 response body, parse_url safety, and location/stock consistency

- Use empty Response instead of JsonResponse(null) for 304 Not Modified
  to avoid sending "null" as response body
- Guard parse_url() result with is_string() since it can return false
  for malformed URLs
- Move storage location tracking inside the availability check so
  expired and unknown-quantity lots don't contribute locations

* Fix testPartDetailsPart2 to actually test Part 2

The test was requesting /parts/1.json instead of /parts/2.json and had
Part 1's expected data. Now tests Part 2 which inherits EDA info from
its category and footprint, verifying the inheritance behavior.

* Use Symfony's built-in ETag handling for HTTP caching

Replace manual If-None-Match comparison with Response::setEtag() and
Response::isNotModified(), which properly handles ETag quoting, weak
vs strong comparison, and 304 response cleanup. Fixes PHPStan return
type error and CI test failures.

* Add configurable KiCad field export for part parameters

Add a kicad_export checkbox to parameters, allowing users to control
which specifications appear as fields in the KiCad HTTP library API.
Parameters with kicad_export enabled are included using their formatted
value, without overwriting hardcoded fields like description or Stock.

* Add partdb:kicad:populate command for bulk KiCad path assignment

Console command that populates KiCad footprint/symbol paths on Footprint
and Category entities based on name-to-library mappings. Supports dry-run,
force overwrite, and list modes. Includes 130+ footprint mappings and 30+
category symbol mappings for KiCad 9.x standard libraries.

* Add CSV import support for EDA/KiCad fields

Add user-friendly column aliases (kicad_symbol, kicad_footprint,
kicad_reference, kicad_value, eda_exclude_bom, etc.) to the CSV import
system. Users can now bulk-set KiCad symbols, footprints, and other EDA
metadata via CSV/Excel import without knowing the internal dot notation.

* Add batch EDA field editing from parts table

Users can now select multiple parts in any parts table and batch-edit
their EDA/KiCad fields (symbol, footprint, reference prefix, value,
visibility, exclude from BOM/board/sim). Each field has an "Apply"
checkbox so users control exactly which fields are changed.

* Remove unused counter variable in BatchEdaController

* Fix PHPStan errors in PopulateKicadCommand and BatchEdaController

Add @var type annotations for Doctrine repository findAll() calls so
PHPStan can resolve getEdaInfo() on Footprint/Category entities. Fix
array return type for numeric-string keys and add explicit callback to
array_filter to satisfy strict rules.

* Fix batch EDA edit: required validation and pre-populate shared values

- Add required=false to TriStateCheckboxType fields so HTML5 validation
  doesn't force users to check visibility/BOM/board checkboxes
- Pre-populate form fields when all selected parts share the same EDA
  value, so users can see current state before editing

* Add KiCad API v2, orderdetail export control, EDA status indicator, BOM improvements

- Add KiCad API v2 endpoints (/kicad-api/v2) with volatile field support
  for stock and storage location (shown but not saved to schematic)
- Add kicad_export flag to Orderdetail entity for per-supplier SPN control
  (backward compatible: if no flag set, all SPNs exported as before)
- Add EDA completeness indicator column in parts datatable (bolt icon)
- Add ?minimal=true query param for faster category parts loading
- Improve category descriptions (use comment instead of URL when available)
- Improve BOM importer multi-footprint support: merge entries by Part-DB
  part ID when linked, tracking footprint variants in comments
- Fix KiCost manf/manf# fields always present (not conditional on orderdetails)
- Fix duplicate getEdaInfo() call in shouldPartBeVisible
- Consolidate supplier SPN and KiCost field generation into single loop

* Fix kicad_export column default for SQLite compatibility

Add options default to ORM column definition so schema:update
works correctly on SQLite test databases.

* Make EDA status bolt icon clickable to open EDA settings tab

* Fix EDA bolt link to correctly open EDA tab via data-turbo=false

* Add configurable datasheet URL mode for KiCad API

New setting "Datasheet field links to PDF" in KiCad EDA settings.
When enabled (default), the datasheet field resolves to the actual
PDF attachment URL. When disabled, it links to the Part-DB page
(old behavior). Configurable via settings UI or EDA_KICAD_DATASHEET_AS_PDF env var.

* Fix settings crash when upgrading: make datasheetAsPdf nullable

The settings bundle stores values in the database. When upgrading from
a version without datasheetAsPdf, the stored JSON lacks this key,
causing a TypeError when assigning null to a non-nullable bool.
Making it nullable with a fallback in KiCadHelper fixes the upgrade path.

* Add functional tests for KiCad API v2 and batch EDA controller

- KiCadApiV2ControllerTest: root, categories, parts, volatile fields,
  v1 vs v2 comparison, cache headers, 304 conditional request, auth
- BatchEdaControllerTest: page load, empty redirect, form submission

* Fix test failures: correct ids format and anonymous access assertion

* Improve test coverage for BatchEdaController

Add tests for: applying all EDA fields at once, custom redirect URL,
and verifying unchecked fields are skipped.

* Address PR review: rename to eda_visibility, merge migrations, API versioning

Changes based on jbtronics' review of PR #1241:

- Rename kicad_export -> eda_visibility (entities, forms, templates,
  translations, tests) with nullable bool for system default support
- Merge two database migrations into one (Version20260211000000)
- Rename createCachedJsonResponse -> createCacheableJsonResponse
- Change bool $apiV2 -> int $apiVersion with version validation
- EDA visibility field only shown for part parameters, not other entities
- PopulateKicadCommand: check alternative names of footprints/categories
- PopulateKicadCommand: support external JSON mapping file (--mapping-file)
- Ship default mappings JSON at contrib/kicad-populate/default_mappings.json
- Add system-wide defaultEdaVisibility setting in KiCadEDASettings
- Add KiCad HTTP Library v2 spec link in controller docs

* Fix duplicate loadMappingFile method causing PHP fatal error

* Add tests for mapping file and alternative name matching, update populate command docs

Add 5 new tests for PopulateKicadCommand covering:
- Custom mapping file overriding defaults
- Invalid JSON mapping file error handling
- Missing mapping file error handling
- Footprint alternative name matching
- Category alternative name matching

Update contrib README to document --mapping-file option,
alternative name matching, and custom JSON mapping format.

* Split out KiCad API v2 into separate PR as requested by maintainer

Remove v2 controller, tests, and volatile field support from this PR.
The v2 API will be submitted as a separate PR for focused discussion.

* Improve test coverage for KiCadHelper and PopulateKicadCommand

KiCadHelper: Add tests for orderdetail eda_visibility filtering,
backward compatibility when no flags set, manufacturer/KiCost fields,
and parameter with empty name skipping.

PopulateKicadCommand: Add tests for mapping file with both footprints
and categories sections, and mapping file with only categories.

* Load populate Kicad default mappings from json file

* Moved kicad:populate documentation to central docs

* Added introduced column to PartTableColumns to make it configurable in the settings

* Use TristateCheckboxes for parameter and orderdetail types

* Fixed translation keys

* Split up default eda visibility for parameters and purchase infos

---------

Co-authored-by: Jan Böhmer <mail@jan-boehmer.de>
2026-03-01 22:10:13 +01:00

448 lines
14 KiB
PHP

<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 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 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/>.
*/
declare(strict_types=1);
namespace App\Entity\PriceInformations;
use Doctrine\Common\Collections\Criteria;
use ApiPlatform\Doctrine\Common\Filter\DateFilterInterface;
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Metadata\ApiFilter;
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\OpenApi\Model\Operation;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use App\ApiPlatform\Filter\LikeFilter;
use Doctrine\DBAL\Types\Types;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\TimestampTrait;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\TimeStampableInterface;
use App\Entity\Parts\Part;
use App\Entity\Parts\Supplier;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Length;
/**
* Class Orderdetail.
*/
#[UniqueEntity(['supplierpartnr', 'supplier', 'part'])]
#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
#[ORM\Table('`orderdetails`')]
#[ORM\Index(columns: ['supplierpartnr'], name: 'orderdetails_supplier_part_nr')]
#[ApiResource(
operations: [
new Get(security: 'is_granted("read", object)'),
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)'),
],
normalizationContext: ['groups' => ['orderdetail:read', 'orderdetail:read:standalone', 'api:basic:read', 'pricedetail:read'], 'openapi_definition_name' => 'Read'],
denormalizationContext: ['groups' => ['orderdetail:write', 'api:basic:write'], 'openapi_definition_name' => 'Write'],
)]
#[ApiResource(
uriTemplate: '/parts/{id}/orderdetails.{_format}',
operations: [
new GetCollection(
openapi: new Operation(summary: 'Retrieves the orderdetails of a part.'),
security: 'is_granted("@parts.read")'
)
],
uriVariables: [
'id' => new Link(toProperty: 'part', fromClass: Part::class)
],
normalizationContext: ['groups' => ['orderdetail:read', 'pricedetail:read', 'api:basic:read'], 'openapi_definition_name' => 'Read']
)]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(LikeFilter::class, properties: ["supplierpartnr", "supplier_product_url"])]
#[ApiFilter(BooleanFilter::class, properties: ["obsolete"])]
#[ApiFilter(DateFilter::class, strategy: DateFilterInterface::EXCLUDE_NULL)]
#[ApiFilter(OrderFilter::class, properties: ['supplierpartnr', 'id', 'addedDate', 'lastModified'])]
class Orderdetail extends AbstractDBElement implements TimeStampableInterface, NamedElementInterface
{
use TimestampTrait;
/**
* @var Collection<int, Pricedetail>
*/
#[Assert\Valid]
#[Groups(['extended', 'full', 'import', 'orderdetail:read', 'orderdetail:write'])]
#[ORM\OneToMany(mappedBy: 'orderdetail', targetEntity: Pricedetail::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['min_discount_quantity' => Criteria::ASC])]
protected Collection $pricedetails;
/**
* @var string The order number of the part at the supplier
*/
#[Groups(['extended', 'full', 'import', 'orderdetail:read', 'orderdetail:write'])]
#[ORM\Column(type: Types::STRING)]
#[Length(max: 255)]
protected string $supplierpartnr = '';
/**
* @var bool True if this part is obsolete/not available anymore at the supplier
*/
#[Groups(['extended', 'full', 'import', 'orderdetail:read', 'orderdetail:write'])]
#[ORM\Column(type: Types::BOOLEAN)]
protected bool $obsolete = false;
/**
* @var bool|null Whether this orderdetail's supplier part number should be exported as an EDA field. Null means use system default.
*/
#[Groups(['full', 'import', 'orderdetail:read', 'orderdetail:write'])]
#[ORM\Column(type: Types::BOOLEAN, nullable: true, options: ['default' => null])]
protected ?bool $eda_visibility = null;
/**
* @var string The URL to the product on the supplier's website
*/
#[Assert\Url(requireTld: false)]
#[Groups(['full', 'import', 'orderdetail:read', 'orderdetail:write'])]
#[ORM\Column(type: Types::TEXT)]
protected string $supplier_product_url = '';
/**
* @var Part|null The part with which this orderdetail is associated
*/
#[Assert\NotNull]
#[ORM\ManyToOne(targetEntity: Part::class, inversedBy: 'orderdetails')]
#[Groups(['orderdetail:read:standalone', 'orderdetail:write'])]
#[ORM\JoinColumn(name: 'part_id', nullable: false, onDelete: 'CASCADE')]
protected ?Part $part = null;
/**
* @var Supplier|null The supplier of this orderdetail
*/
#[Assert\NotNull(message: 'validator.orderdetail.supplier_must_not_be_null')]
#[Groups(['extended', 'full', 'import', 'orderdetail:read', 'orderdetail:write'])]
#[ORM\ManyToOne(targetEntity: Supplier::class, inversedBy: 'orderdetails')]
#[ORM\JoinColumn(name: 'id_supplier')]
protected ?Supplier $supplier = null;
/**
* @var bool|null Whether the prices includes VAT or not. Null means, that it is not specified, if the prices includes VAT or not.
*/
#[ORM\Column(type: Types::BOOLEAN, nullable: true)]
#[Groups(['extended', 'full', 'import', 'orderdetail:read', 'orderdetail:write'])]
protected ?bool $prices_includes_vat = null;
public function __construct()
{
$this->pricedetails = new ArrayCollection();
}
public function __clone()
{
if ($this->id) {
$this->addedDate = null;
$pricedetails = $this->pricedetails;
$this->pricedetails = new ArrayCollection();
//Set master attachment is needed
foreach ($pricedetails as $pricedetail) {
$this->addPricedetail(clone $pricedetail);
}
}
parent::__clone();
}
/**
* Helper for updating the timestamp. It is automatically called by doctrine before persisting.
*/
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updateTimestamps(): void
{
$this->lastModified = new DateTimeImmutable('now');
if (!$this->addedDate instanceof \DateTimeInterface) {
$this->addedDate = new DateTimeImmutable('now');
}
if ($this->part instanceof Part) {
$this->part->updateTimestamps();
}
}
/********************************************************************************
*
* Getters
*
*********************************************************************************/
/**
* Get the part.
*
* @return Part|null the part of this orderdetails
*/
public function getPart(): ?Part
{
return $this->part;
}
/**
* Get the supplier.
*
* @return Supplier the supplier of this orderdetails
*/
public function getSupplier(): ?Supplier
{
return $this->supplier;
}
/**
* Get the supplier part-nr.
*
* @return string the part-nr
*/
public function getSupplierPartNr(): string
{
return $this->supplierpartnr;
}
/**
* Get if this orderdetails is obsolete.
*
* "Orderdetails is obsolete" means that the part with that supplier-part-nr
* is no longer available from the supplier of that orderdetails.
*
* @return bool * true if this part is obsolete at that supplier
* * false if this part isn't obsolete at that supplier
*/
public function getObsolete(): bool
{
return $this->obsolete;
}
public function isObsolete(): bool
{
return $this->getObsolete();
}
/**
* Get the link to the website of the article on the supplier's website.
*
* @param bool $no_automatic_url Set this to true, if you only want to get the local set product URL for this Orderdetail
* and not an automatic generated one, based from the Supplier
*
* @return string the link to the article
*/
public function getSupplierProductUrl(bool $no_automatic_url = false): string
{
if ($no_automatic_url || '' !== $this->supplier_product_url) {
return $this->supplier_product_url;
}
if (!$this->getSupplier() instanceof Supplier) {
return '';
}
return $this->getSupplier()->getAutoProductUrl($this->supplierpartnr); // maybe an automatic url is available...
}
/**
* Get all pricedetails.
*
* @return Collection<int, Pricedetail>
*/
public function getPricedetails(): Collection
{
return $this->pricedetails;
}
/**
* Adds a price detail to this orderdetail.
*
* @param Pricedetail $pricedetail The pricedetail to add
*/
public function addPricedetail(Pricedetail $pricedetail): self
{
$pricedetail->setOrderdetail($this);
$this->pricedetails->add($pricedetail);
return $this;
}
/**
* Removes a price detail from this orderdetail.
*/
public function removePricedetail(Pricedetail $pricedetail): self
{
$this->pricedetails->removeElement($pricedetail);
return $this;
}
/**
* Find the pricedetail that is correct for the desired amount (the one with the greatest discount value with a
* minimum order amount of the wished quantity).
*
* @param float $quantity this is the quantity to choose the correct pricedetails
*
* @return Pricedetail|null the price as a bcmath string. Null if there are no orderdetails for the given quantity
*/
public function findPriceForQty(float $quantity = 1.0): ?Pricedetail
{
if ($quantity <= 0) {
return null;
}
$all_pricedetails = $this->getPricedetails();
$correct_pricedetail = null;
foreach ($all_pricedetails as $pricedetail) {
// choose the correct pricedetails for the chosen quantity ($quantity)
if ($quantity < $pricedetail->getMinDiscountQuantity()) {
break;
}
$correct_pricedetail = $pricedetail;
}
return $correct_pricedetail;
}
/********************************************************************************
*
* Setters
*
*********************************************************************************/
/**
* Sets a new part with which this orderdetail is associated.
*/
public function setPart(Part $part): self
{
$this->part = $part;
return $this;
}
/**
* Sets the new supplier associated with this orderdetail.
*/
public function setSupplier(Supplier $new_supplier): self
{
$this->supplier = $new_supplier;
return $this;
}
/**
* Set the supplier part-nr.
*
* @param string $new_supplierpartnr the new supplier-part-nr
*/
public function setSupplierpartnr(string $new_supplierpartnr): self
{
$this->supplierpartnr = $new_supplierpartnr;
return $this;
}
/**
* Set if the part is obsolete at the supplier of that orderdetails.
*
* @param bool $new_obsolete true means that this part is obsolete
*/
public function setObsolete(bool $new_obsolete): self
{
$this->obsolete = $new_obsolete;
return $this;
}
/**
* Sets the custom product supplier URL for this order detail.
* Set this to "", if the function getSupplierProductURL should return the automatic generated URL.
*
* @param string $new_url The new URL for the supplier URL
*/
public function setSupplierProductUrl(string $new_url): self
{
//Only change the internal URL if it is not the auto generated one
if ($this->supplier && $new_url === $this->supplier->getAutoProductUrl($this->getSupplierPartNr())) {
return $this;
}
$this->supplier_product_url = $new_url;
return $this;
}
/**
* Checks if the prices of this orderdetail include VAT. Null means, that it is not specified, if the prices includes
* VAT or not.
* @return bool|null
*/
public function getPricesIncludesVAT(): ?bool
{
return $this->prices_includes_vat;
}
/**
* Sets whether the prices of this orderdetail include VAT.
* @param bool|null $includesVat
* @return $this
*/
public function setPricesIncludesVAT(?bool $includesVat): self
{
$this->prices_includes_vat = $includesVat;
return $this;
}
public function isEdaVisibility(): ?bool
{
return $this->eda_visibility;
}
/**
* @return $this
*/
public function setEdaVisibility(?bool $eda_visibility): self
{
$this->eda_visibility = $eda_visibility;
return $this;
}
public function getName(): string
{
return $this->getSupplierPartNr();
}
}