2023-03-12 01:41:44 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2023 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Serializer;
|
|
|
|
|
|
|
|
use App\Entity\Parts\Part;
|
2023-03-13 00:22:46 +01:00
|
|
|
use App\Entity\Parts\PartLot;
|
|
|
|
use App\Entity\Parts\Storelocation;
|
2023-03-13 01:04:49 +01:00
|
|
|
use App\Entity\Parts\Supplier;
|
|
|
|
use App\Entity\PriceInformations\Orderdetail;
|
|
|
|
use App\Entity\PriceInformations\Pricedetail;
|
|
|
|
use Brick\Math\BigDecimal;
|
2023-03-13 00:35:31 +01:00
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
2023-03-12 01:41:44 +01:00
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
|
|
|
|
2023-03-13 00:35:31 +01:00
|
|
|
class PartNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
|
2023-03-12 01:41:44 +01:00
|
|
|
{
|
2023-03-13 00:44:05 +01:00
|
|
|
private const DENORMALIZE_KEY_MAPPING = [
|
|
|
|
'notes' => 'comment',
|
|
|
|
'quantity' => 'instock',
|
|
|
|
'amount' => 'instock',
|
2023-03-13 00:52:22 +01:00
|
|
|
'mpn' => 'manufacturer_product_number',
|
2023-03-13 01:04:49 +01:00
|
|
|
'spn' => 'supplier_part_number',
|
2023-03-14 00:02:40 +01:00
|
|
|
'supplier_product_number' => 'supplier_part_number',
|
|
|
|
'storage_location' => 'storelocation',
|
2023-03-13 00:44:05 +01:00
|
|
|
];
|
2023-03-12 01:41:44 +01:00
|
|
|
|
2023-03-13 00:44:05 +01:00
|
|
|
private ObjectNormalizer $normalizer;
|
2023-03-13 00:22:46 +01:00
|
|
|
private StructuralElementFromNameDenormalizer $locationDenormalizer;
|
2023-03-12 01:41:44 +01:00
|
|
|
|
2023-03-13 00:22:46 +01:00
|
|
|
public function __construct(ObjectNormalizer $normalizer, StructuralElementFromNameDenormalizer $locationDenormalizer)
|
2023-03-12 01:41:44 +01:00
|
|
|
{
|
|
|
|
$this->normalizer = $normalizer;
|
2023-03-13 00:22:46 +01:00
|
|
|
$this->locationDenormalizer = $locationDenormalizer;
|
2023-03-12 01:41:44 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 00:35:31 +01:00
|
|
|
public function supportsNormalization($data, string $format = null): bool
|
2023-03-12 01:41:44 +01:00
|
|
|
{
|
|
|
|
return $data instanceof Part;
|
|
|
|
}
|
|
|
|
|
2023-04-15 21:18:11 +02:00
|
|
|
public function normalize($object, string $format = null, array $context = []): array
|
2023-03-12 01:41:44 +01:00
|
|
|
{
|
|
|
|
if (!$object instanceof Part) {
|
|
|
|
throw new \InvalidArgumentException('This normalizer only supports Part objects!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->normalizer->normalize($object, $format, $context);
|
|
|
|
|
|
|
|
//Remove type field for CSV export
|
2023-03-13 00:22:46 +01:00
|
|
|
if ($format === 'csv') {
|
2023-03-12 01:41:44 +01:00
|
|
|
unset($data['type']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['total_instock'] = $object->getAmountSum();
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2023-03-13 00:22:46 +01:00
|
|
|
|
2023-03-13 00:35:31 +01:00
|
|
|
public function supportsDenormalization($data, string $type, string $format = null): bool
|
2023-03-13 00:22:46 +01:00
|
|
|
{
|
|
|
|
return is_array($data) && is_a($type, Part::class, true);
|
|
|
|
}
|
|
|
|
|
2023-03-13 00:44:05 +01:00
|
|
|
private function normalizeKeys(array &$data): array
|
|
|
|
{
|
|
|
|
//Rename keys based on the mapping, while leaving the data untouched
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
if (isset(self::DENORMALIZE_KEY_MAPPING[$key])) {
|
|
|
|
$data[self::DENORMALIZE_KEY_MAPPING[$key]] = $value;
|
|
|
|
unset($data[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2023-04-15 21:18:11 +02:00
|
|
|
public function denormalize($data, string $type, string $format = null, array $context = []): ?Part
|
2023-03-13 00:22:46 +01:00
|
|
|
{
|
2023-03-13 00:44:05 +01:00
|
|
|
$this->normalizeKeys($data);
|
|
|
|
|
2023-03-13 01:04:49 +01:00
|
|
|
//Empty IPN should be null, or we get a constraint error
|
2023-03-14 00:17:13 +01:00
|
|
|
if (isset($data['ipn']) && $data['ipn'] === '') {
|
2023-03-13 01:04:49 +01:00
|
|
|
$data['ipn'] = null;
|
|
|
|
}
|
|
|
|
|
2023-03-15 21:33:18 +01:00
|
|
|
//Fill empty needs_review and needs_review_comment fields with false
|
|
|
|
if (empty($data['needs_review'])) {
|
|
|
|
$data['needs_review'] = false;
|
|
|
|
}
|
|
|
|
if (empty($data['favorite'])) {
|
|
|
|
$data['favorite'] = false;
|
|
|
|
}
|
|
|
|
if (empty($data['minamount'])) {
|
|
|
|
$data['minamount'] = 0.0;
|
|
|
|
}
|
|
|
|
|
2023-03-13 00:22:46 +01:00
|
|
|
$object = $this->normalizer->denormalize($data, $type, $format, $context);
|
|
|
|
|
|
|
|
if (!$object instanceof Part) {
|
|
|
|
throw new \InvalidArgumentException('This normalizer only supports Part objects!');
|
|
|
|
}
|
|
|
|
|
2023-03-15 21:33:18 +01:00
|
|
|
if ((isset($data['instock']) && trim($data['instock']) !== "") || (isset($data['storelocation']) && trim($data['storelocation']) !== "")) {
|
2023-03-13 00:22:46 +01:00
|
|
|
$partLot = new PartLot();
|
|
|
|
|
|
|
|
if (isset($data['instock']) && $data['instock'] !== "") {
|
|
|
|
//Replace comma with dot
|
|
|
|
$instock = (float) str_replace(',', '.', $data['instock']);
|
|
|
|
|
|
|
|
$partLot->setAmount($instock);
|
|
|
|
} else {
|
|
|
|
$partLot->setInstockUnknown(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($data['storelocation']) && $data['storelocation'] !== "") {
|
|
|
|
$location = $this->locationDenormalizer->denormalize($data['storelocation'], Storelocation::class, $format, $context);
|
|
|
|
$partLot->setStorageLocation($location);
|
|
|
|
}
|
|
|
|
|
|
|
|
$object->addPartLot($partLot);
|
|
|
|
}
|
|
|
|
|
2023-03-13 01:04:49 +01:00
|
|
|
if (isset($data['supplier']) && $data['supplier'] !== "") {
|
|
|
|
$supplier = $this->locationDenormalizer->denormalize($data['supplier'], Supplier::class, $format, $context);
|
|
|
|
|
|
|
|
if ($supplier) {
|
|
|
|
$orderdetail = new Orderdetail();
|
|
|
|
$orderdetail->setSupplier($supplier);
|
|
|
|
|
|
|
|
if (isset($data['supplier_part_number']) && $data['supplier_part_number'] !== "") {
|
|
|
|
$orderdetail->setSupplierpartnr($data['supplier_part_number']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$object->addOrderdetail($orderdetail);
|
|
|
|
|
|
|
|
if (isset($data['price']) && $data['price'] !== "") {
|
|
|
|
$pricedetail = new Pricedetail();
|
|
|
|
$pricedetail->setMinDiscountQuantity(1);
|
|
|
|
$pricedetail->setPriceRelatedQuantity(1);
|
|
|
|
$price = BigDecimal::of(str_replace(',', '.', $data['price']));
|
|
|
|
$pricedetail->setPrice($price);
|
|
|
|
|
|
|
|
$orderdetail->addPricedetail($pricedetail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 00:22:46 +01:00
|
|
|
return $object;
|
|
|
|
}
|
2023-03-13 00:35:31 +01:00
|
|
|
|
|
|
|
public function hasCacheableSupportsMethod(): bool
|
|
|
|
{
|
|
|
|
//Must be false, because we rely on is_array($data) in supportsDenormalization()
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-12 01:41:44 +01:00
|
|
|
}
|