diff --git a/migrations/Version20260208131116.php b/migrations/Version20260208131116.php index 7ffc47a8..d05d3e4c 100644 --- a/migrations/Version20260208131116.php +++ b/migrations/Version20260208131116.php @@ -22,7 +22,7 @@ final class Version20260208131116 extends AbstractMultiPlatformMigration $this->addSql('ALTER TABLE part_lots ADD last_stocktake_at DATETIME DEFAULT NULL'); $this->addSql('ALTER TABLE parts ADD gtin VARCHAR(255) DEFAULT NULL'); $this->addSql('CREATE INDEX parts_idx_gtin ON parts (gtin)'); - $this->addSql('ALTER TABLE pricedetails ADD include_vat TINYINT DEFAULT NULL'); + $this->addSql('ALTER TABLE orderdetails ADD prices_includes_vat TINYINT DEFAULT NULL'); } public function mySQLDown(Schema $schema): void @@ -32,7 +32,7 @@ final class Version20260208131116 extends AbstractMultiPlatformMigration $this->addSql('DROP INDEX parts_idx_gtin ON `parts`'); $this->addSql('ALTER TABLE `parts` DROP gtin'); $this->addSql('ALTER TABLE part_lots DROP last_stocktake_at'); - $this->addSql('ALTER TABLE `pricedetails` DROP include_vat'); + $this->addSql('ALTER TABLE `orderdetails` DROP prices_includes_vat'); } public function sqLiteUp(Schema $schema): void @@ -57,7 +57,7 @@ final class Version20260208131116 extends AbstractMultiPlatformMigration $this->addSql('CREATE INDEX IDX_6940A7FE1ECB93AE ON parts (id_manufacturer)'); $this->addSql('CREATE INDEX IDX_6940A7FEA3ED1215 ON parts (id_part_custom_state)'); $this->addSql('CREATE INDEX parts_idx_gtin ON parts (gtin)'); - $this->addSql('ALTER TABLE pricedetails ADD COLUMN include_vat BOOLEAN DEFAULT NULL'); + $this->addSql('ALTER TABLE orderdetails ADD COLUMN prices_includes_vat BOOLEAN DEFAULT NULL'); } public function sqLiteDown(Schema $schema): void @@ -99,15 +99,14 @@ final class Version20260208131116 extends AbstractMultiPlatformMigration $this->addSql('CREATE INDEX parts_idx_datet_name_last_id_needs ON "parts" (datetime_added, name, last_modified, id, needs_review)'); $this->addSql('CREATE INDEX parts_idx_name ON "parts" (name)'); $this->addSql('CREATE INDEX parts_idx_ipn ON "parts" (ipn)'); - $this->addSql('CREATE TEMPORARY TABLE __temp__pricedetails AS SELECT id, price, price_related_quantity, min_discount_quantity, manual_input, last_modified, datetime_added, id_currency, orderdetails_id FROM "pricedetails"'); - $this->addSql('DROP TABLE "pricedetails"'); - $this->addSql('CREATE TABLE "pricedetails" (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, price NUMERIC(11, 5) NOT NULL, price_related_quantity DOUBLE PRECISION NOT NULL, min_discount_quantity DOUBLE PRECISION NOT NULL, manual_input BOOLEAN NOT NULL, last_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, datetime_added DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, id_currency INTEGER DEFAULT NULL, orderdetails_id INTEGER NOT NULL, CONSTRAINT FK_C68C4459398D64AA FOREIGN KEY (id_currency) REFERENCES currencies (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_C68C44594A01DDC7 FOREIGN KEY (orderdetails_id) REFERENCES "orderdetails" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)'); - $this->addSql('INSERT INTO "pricedetails" (id, price, price_related_quantity, min_discount_quantity, manual_input, last_modified, datetime_added, id_currency, orderdetails_id) SELECT id, price, price_related_quantity, min_discount_quantity, manual_input, last_modified, datetime_added, id_currency, orderdetails_id FROM __temp__pricedetails'); - $this->addSql('DROP TABLE __temp__pricedetails'); - $this->addSql('CREATE INDEX IDX_C68C4459398D64AA ON "pricedetails" (id_currency)'); - $this->addSql('CREATE INDEX IDX_C68C44594A01DDC7 ON "pricedetails" (orderdetails_id)'); - $this->addSql('CREATE INDEX pricedetails_idx_min_discount ON "pricedetails" (min_discount_quantity)'); - $this->addSql('CREATE INDEX pricedetails_idx_min_discount_price_qty ON "pricedetails" (min_discount_quantity, price_related_quantity)'); + $this->addSql('CREATE TEMPORARY TABLE __temp__orderdetails AS SELECT id, supplierpartnr, obsolete, supplier_product_url, last_modified, datetime_added, part_id, id_supplier FROM "orderdetails"'); + $this->addSql('DROP TABLE "orderdetails"'); + $this->addSql('CREATE TABLE "orderdetails" (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, supplierpartnr VARCHAR(255) NOT NULL, obsolete BOOLEAN NOT NULL, supplier_product_url CLOB NOT NULL, last_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, datetime_added DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, part_id INTEGER NOT NULL, id_supplier INTEGER DEFAULT NULL, CONSTRAINT FK_489AFCDC4CE34BEC FOREIGN KEY (part_id) REFERENCES "parts" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_489AFCDCCBF180EB FOREIGN KEY (id_supplier) REFERENCES "suppliers" (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('INSERT INTO "orderdetails" (id, supplierpartnr, obsolete, supplier_product_url, last_modified, datetime_added, part_id, id_supplier) SELECT id, supplierpartnr, obsolete, supplier_product_url, last_modified, datetime_added, part_id, id_supplier FROM __temp__orderdetails'); + $this->addSql('DROP TABLE __temp__orderdetails'); + $this->addSql('CREATE INDEX IDX_489AFCDC4CE34BEC ON "orderdetails" (part_id)'); + $this->addSql('CREATE INDEX IDX_489AFCDCCBF180EB ON "orderdetails" (id_supplier)'); + $this->addSql('CREATE INDEX orderdetails_supplier_part_nr ON "orderdetails" (supplierpartnr)'); } public function postgreSQLUp(Schema $schema): void @@ -116,7 +115,7 @@ final class Version20260208131116 extends AbstractMultiPlatformMigration $this->addSql('ALTER TABLE part_lots ADD last_stocktake_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL'); $this->addSql('ALTER TABLE parts ADD gtin VARCHAR(255) DEFAULT NULL'); $this->addSql('CREATE INDEX parts_idx_gtin ON parts (gtin)'); - $this->addSql('ALTER TABLE pricedetails ADD include_vat BOOLEAN DEFAULT NULL'); + $this->addSql('ALTER TABLE orderdetails ADD prices_includes_vat BOOLEAN DEFAULT NULL'); } public function postgreSQLDown(Schema $schema): void @@ -125,6 +124,6 @@ final class Version20260208131116 extends AbstractMultiPlatformMigration $this->addSql('ALTER TABLE part_lots DROP last_stocktake_at'); $this->addSql('DROP INDEX parts_idx_gtin'); $this->addSql('ALTER TABLE "parts" DROP gtin'); - $this->addSql('ALTER TABLE "pricedetails" DROP include_vat'); + $this->addSql('ALTER TABLE "orderdetails" DROP prices_includes_vat'); } } diff --git a/src/Entity/PriceInformations/Orderdetail.php b/src/Entity/PriceInformations/Orderdetail.php index 9a9a2823..58f69598 100644 --- a/src/Entity/PriceInformations/Orderdetail.php +++ b/src/Entity/PriceInformations/Orderdetail.php @@ -148,6 +148,13 @@ class Orderdetail extends AbstractDBElement implements TimeStampableInterface, N #[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(); @@ -390,45 +397,23 @@ class Orderdetail extends AbstractDBElement implements TimeStampableInterface, N } /** - * Checks if the prices of this orderdetail include VAT. This is determined by checking the pricedetails of this - * orderdetail. If there are no pricedetails or if the pricedetails have conflicting values, null is returned. + * 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 */ - #[Groups(['orderdetail:read'])] - #[SerializedName('prices_include_vat')] public function getPricesIncludesVAT(): ?bool { - $value = null; - //We determine that via the pricedetails - foreach ($this->getPricedetails() as $pricedetail) { - /** @var Pricedetail $pricedetail */ - if ($pricedetail->getIncludesVat() === null) { - return null; // If any pricedetail doesn't specify this, we can't determine it - } - - if ($value === null) { - $value = $pricedetail->getIncludesVat(); // Set initial value - } elseif ($value !== $pricedetail->getIncludesVat()) { - return null; // If there are conflicting values, we can't determine it - } - } - - return $value; + return $this->prices_includes_vat; } /** - * Sets whether the prices of this orderdetail include VAT. This is set for all pricedetails of this orderdetail. + * Sets whether the prices of this orderdetail include VAT. * @param bool|null $includesVat * @return $this */ - #[Groups(['orderdetail:write'])] - #[SerializedName('prices_include_vat')] public function setPricesIncludesVAT(?bool $includesVat): self { - foreach ($this->getPricedetails() as $pricedetail) { - /** @var Pricedetail $pricedetail */ - $pricedetail->setIncludesVat($includesVat); - } + $this->prices_includes_vat = $includesVat; return $this; } diff --git a/src/Entity/PriceInformations/Pricedetail.php b/src/Entity/PriceInformations/Pricedetail.php index 7deb64f9..553b07a3 100644 --- a/src/Entity/PriceInformations/Pricedetail.php +++ b/src/Entity/PriceInformations/Pricedetail.php @@ -121,12 +121,7 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface #[Groups(['pricedetail:read:standalone', 'pricedetail:write'])] protected ?Orderdetail $orderdetail = null; - /** - * @var bool|null Whether the price includes VAT or not. Null means, that it is not specified, if the price includes VAT or not. - */ - #[ORM\Column(name: "include_vat", type: Types::BOOLEAN, nullable: true)] - #[Groups(['extended', 'full', 'import', 'pricedetail:read', 'pricedetail:write'])] - protected ?bool $includes_vat = null; + public function __construct() { @@ -277,7 +272,7 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface */ public function getIncludesVat(): ?bool { - return $this->includes_vat; + return $this->orderdetail?->getPricesIncludesVAT(); } /******************************************************************************** @@ -376,15 +371,4 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface return $this; } - - /** - * Set whether the price includes VAT or not. Null means, that it is not specified, if the price includes VAT or not. - * @param bool|null $includes_vat - * @return $this - */ - public function setIncludesVat(?bool $includes_vat): self - { - $this->includes_vat = $includes_vat; - return $this; - } } diff --git a/src/Services/InfoProviderSystem/DTOs/PriceDTO.php b/src/Services/InfoProviderSystem/DTOs/PriceDTO.php index 2acf3e57..cf1f577d 100644 --- a/src/Services/InfoProviderSystem/DTOs/PriceDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/PriceDTO.php @@ -39,7 +39,9 @@ readonly class PriceDTO public string $price, /** @var string The currency of the used ISO code of this price detail */ public ?string $currency_iso_code, - /** @var bool If the price includes tax */ + /** @var bool If the price includes tax + * @deprecated Use the prices_include_vat property of the PurchaseInfoDTO instead, as this property is not reliable if there are multiple prices with different values for includes_tax + */ public ?bool $includes_tax = true, /** @var float the price related quantity */ public ?float $price_related_quantity = 1.0, diff --git a/src/Services/InfoProviderSystem/DTOs/PurchaseInfoDTO.php b/src/Services/InfoProviderSystem/DTOs/PurchaseInfoDTO.php index 9ac142ff..446d04dc 100644 --- a/src/Services/InfoProviderSystem/DTOs/PurchaseInfoDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/PurchaseInfoDTO.php @@ -29,6 +29,9 @@ namespace App\Services\InfoProviderSystem\DTOs; */ readonly class PurchaseInfoDTO { + /** @var bool|null If the prices contain VAT or not. Null if state is unknown. */ + public ?bool $prices_include_vat; + public function __construct( public string $distributor_name, public string $order_number, @@ -36,6 +39,7 @@ readonly class PurchaseInfoDTO public array $prices, /** @var string|null An url to the product page of the vendor */ public ?string $product_url = null, + ?bool $prices_include_vat = null, ) { //Ensure that the prices are PriceDTO instances @@ -44,5 +48,17 @@ readonly class PurchaseInfoDTO throw new \InvalidArgumentException('The prices array must only contain PriceDTO instances'); } } + + //If no prices_include_vat information is given, try to deduct it from the prices + if ($prices_include_vat === null) { + $vatValues = array_unique(array_map(fn(PriceDTO $price) => $price->includes_tax, $this->prices)); + if (count($vatValues) === 1) { + $this->prices_include_vat = $vatValues[0]; //Use the value of the prices if they are all the same + } else { + $this->prices_include_vat = null; //If there are different values for the prices, we cannot determine if the prices include VAT or not + } + } else { + $this->prices_include_vat = $prices_include_vat; + } } } diff --git a/src/Services/InfoProviderSystem/DTOtoEntityConverter.php b/src/Services/InfoProviderSystem/DTOtoEntityConverter.php index 1a93b111..c7c15673 100644 --- a/src/Services/InfoProviderSystem/DTOtoEntityConverter.php +++ b/src/Services/InfoProviderSystem/DTOtoEntityConverter.php @@ -100,8 +100,6 @@ final class DTOtoEntityConverter $entity->setCurrency(null); } - $entity->setIncludesVat($dto->includes_tax); - return $entity; } @@ -118,6 +116,8 @@ final class DTOtoEntityConverter $entity->addPricedetail($this->convertPrice($price)); } + $entity->setPricesIncludesVAT($dto->prices_include_vat); + return $entity; } diff --git a/tests/Entity/PriceSystem/OrderdetailTest.php b/tests/Entity/PriceSystem/OrderdetailTest.php index 7eae93aa..df86ab34 100644 --- a/tests/Entity/PriceSystem/OrderdetailTest.php +++ b/tests/Entity/PriceSystem/OrderdetailTest.php @@ -62,76 +62,17 @@ class OrderdetailTest extends TestCase $this->assertSame($price5, $orderdetail->findPriceForQty(10000)); } - public function testGetPricesIncludesVAT(): void + public function testGetSetPricesIncludesVAT(): void { $orderdetail = new Orderdetail(); //By default, the pricesIncludesVAT property should be null for empty orderdetails $this->assertNull($orderdetail->getPricesIncludesVAT()); - $price0 = (new Pricedetail())->setMinDiscountQuantity(0.23); - $price1 = (new Pricedetail())->setMinDiscountQuantity(1); - $price5 = (new Pricedetail())->setMinDiscountQuantity(5.3); - - $orderdetail->addPricedetail($price0)->addPricedetail($price1)->addPricedetail($price5); - - //With empty pricedetails, the pricesIncludesVAT property should still be null - $this->assertNull($orderdetail->getPricesIncludesVAT()); - - //If all of the pricedetails have the same value for includesVAT, the pricesIncludesVAT property should return this value - $price0->setIncludesVAT(true); - $price1->setIncludesVAT(true); - $price5->setIncludesVAT(true); - $this->assertTrue($orderdetail->getPricesIncludesVAT()); - - $price0->setIncludesVAT(false); - $price1->setIncludesVAT(false); - $price5->setIncludesVAT(false); - $this->assertFalse($orderdetail->getPricesIncludesVAT()); - - //If the pricedetails have different values for includesVAT, the pricesIncludesVAT property should return null - $price0->setIncludesVAT(true); - $price1->setIncludesVAT(false); - $price5->setIncludesVAT(true); - $this->assertNull($orderdetail->getPricesIncludesVAT()); - - //If the pricedetails have different values for includesVAT, the pricesIncludesVAT property should return null, even if one of them is null - $price0->setIncludesVAT(null); - $price1->setIncludesVAT(false); - $price5->setIncludesVAT(false); - $this->assertNull($orderdetail->getPricesIncludesVAT()); - } - - public function testSetPricesIncludesVAT(): void - { - $orderdetail = new Orderdetail(); - $price0 = (new Pricedetail())->setMinDiscountQuantity(0.23); - $price1 = (new Pricedetail())->setMinDiscountQuantity(1); - $price5 = (new Pricedetail())->setMinDiscountQuantity(5.3); - - $orderdetail->addPricedetail($price0)->addPricedetail($price1)->addPricedetail($price5); - - $this->assertNull($orderdetail->getPricesIncludesVAT()); - $orderdetail->setPricesIncludesVAT(true); $this->assertTrue($orderdetail->getPricesIncludesVAT()); - //Ensure that the pricesIncludesVAT property is correctly propagated to the pricedetails - foreach ($orderdetail->getPricedetails() as $pricedetail) { - $this->assertTrue($pricedetail->getIncludesVAT()); - } $orderdetail->setPricesIncludesVAT(false); $this->assertFalse($orderdetail->getPricesIncludesVAT()); - //Ensure that the pricesIncludesVAT property is correctly propagated to the pricedetails - foreach ($orderdetail->getPricedetails() as $pricedetail) { - $this->assertFalse($pricedetail->getIncludesVAT()); - } - - $orderdetail->setPricesIncludesVAT(null); - $this->assertNull($orderdetail->getPricesIncludesVAT()); - //Ensure that the pricesIncludesVAT property is correctly propagated to the pricedetails - foreach ($orderdetail->getPricedetails() as $pricedetail) { - $this->assertNull($pricedetail->getIncludesVAT()); - } } } diff --git a/tests/Services/InfoProviderSystem/DTOs/PurchaseInfoDTOTest.php b/tests/Services/InfoProviderSystem/DTOs/PurchaseInfoDTOTest.php index 14a3c03f..1c909e67 100644 --- a/tests/Services/InfoProviderSystem/DTOs/PurchaseInfoDTOTest.php +++ b/tests/Services/InfoProviderSystem/DTOs/PurchaseInfoDTOTest.php @@ -22,6 +22,7 @@ declare(strict_types=1); */ namespace App\Tests\Services\InfoProviderSystem\DTOs; +use App\Services\InfoProviderSystem\DTOs\PriceDTO; use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO; use PHPUnit\Framework\TestCase; @@ -33,4 +34,40 @@ class PurchaseInfoDTOTest extends TestCase $this->expectExceptionMessage('The prices array must only contain PriceDTO instances'); new PurchaseInfoDTO('test', 'test', [new \stdClass()]); } + + public function testPricesIncludesVATHandling(): void + { + $pricesTrue = [ + new PriceDTO(minimum_discount_amount: 1, price: '10.00', currency_iso_code: 'USD', includes_tax: true), + new PriceDTO(minimum_discount_amount: 5, price: '9.00', currency_iso_code: 'USD', includes_tax: true), + ]; + $pricesFalse = [ + new PriceDTO(minimum_discount_amount: 1, price: '10.00', currency_iso_code: 'USD', includes_tax: false), + new PriceDTO(minimum_discount_amount: 5, price: '9.00', currency_iso_code: 'USD', includes_tax: false), + ]; + $pricesMixed = [ + new PriceDTO(minimum_discount_amount: 1, price: '10.00', currency_iso_code: 'USD', includes_tax: true), + new PriceDTO(minimum_discount_amount: 5, price: '9.00', currency_iso_code: 'USD', includes_tax: false), + ]; + $pricesNull = [ + new PriceDTO(minimum_discount_amount: 1, price: '10.00', currency_iso_code: 'USD', includes_tax: null), + new PriceDTO(minimum_discount_amount: 5, price: '9.00', currency_iso_code: 'USD', includes_tax: null), + ]; + + //If the prices_include_vat parameter is given, use it: + $dto = new PurchaseInfoDTO('test', 'test', $pricesMixed, prices_include_vat: true); + $this->assertTrue($dto->prices_include_vat); + $dto = new PurchaseInfoDTO('test', 'test', $pricesMixed, prices_include_vat: false); + $this->assertFalse($dto->prices_include_vat); + + //If the prices_include_vat parameter is not given, try to deduct it from the prices: + $dto = new PurchaseInfoDTO('test', 'test', $pricesTrue); + $this->assertTrue($dto->prices_include_vat); + $dto = new PurchaseInfoDTO('test', 'test', $pricesFalse); + $this->assertFalse($dto->prices_include_vat); + $dto = new PurchaseInfoDTO('test', 'test', $pricesMixed); + $this->assertNull($dto->prices_include_vat); + $dto = new PurchaseInfoDTO('test', 'test', $pricesNull); + $this->assertNull($dto->prices_include_vat); + } } diff --git a/tests/Services/InfoProviderSystem/DTOtoEntityConverterTest.php b/tests/Services/InfoProviderSystem/DTOtoEntityConverterTest.php index 54878bbf..45ea9984 100644 --- a/tests/Services/InfoProviderSystem/DTOtoEntityConverterTest.php +++ b/tests/Services/InfoProviderSystem/DTOtoEntityConverterTest.php @@ -94,15 +94,12 @@ class DTOtoEntityConverterTest extends WebTestCase minimum_discount_amount: 5, price: "10.0", currency_iso_code: 'EUR', - includes_tax: true, ); $entity = $this->service->convertPrice($dto); //For base currencies, the currency field is null $this->assertNull($entity->getCurrency()); - - $this->assertTrue($entity->getIncludesVat()); } public function testConvertPurchaseInfo(): void @@ -117,6 +114,7 @@ class DTOtoEntityConverterTest extends WebTestCase order_number: 'TestOrderNumber', prices: $prices, product_url: 'https://example.com', + prices_include_vat: true, ); $entity = $this->service->convertPurchaseInfo($dto); @@ -124,6 +122,7 @@ class DTOtoEntityConverterTest extends WebTestCase $this->assertSame($dto->distributor_name, $entity->getSupplier()->getName()); $this->assertSame($dto->order_number, $entity->getSupplierPartNr()); $this->assertEquals($dto->product_url, $entity->getSupplierProductUrl()); + $this->assertTrue($dto->prices_include_vat); } public function testConvertFileWithName(): void