From c1a5bb83a0d97bb28e219edb539c49745cc45ba2 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Fri, 19 Apr 2024 19:33:40 +0200 Subject: [PATCH 1/3] init commit of several stock for warehouses --- examples/stock.php | 10 ++++++++++ src/Stock/Handler.php | 18 +++++++++++++++++- src/Stock/Model/Stock.php | 17 +++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/examples/stock.php b/examples/stock.php index 5570c37..4affa6b 100644 --- a/examples/stock.php +++ b/examples/stock.php @@ -37,3 +37,13 @@ $stockHandler->updateStock([ (new Tradebyte\Stock\Model\Stock())->setArticleNumber('12345')->setStock(6) ]); + +/* + * update stock several warehouse + */ +$stockHandler->updateStock([ + (new Tradebyte\Stock\Model\Stock()) + ->setArticleNumber('12345') + ->addStockForWarehouse('zentrallager', 10) + ->addStockForWarehouse('aussenlager', 5) +]); \ No newline at end of file diff --git a/src/Stock/Handler.php b/src/Stock/Handler.php index 67c9502..57d1b53 100644 --- a/src/Stock/Handler.php +++ b/src/Stock/Handler.php @@ -5,6 +5,7 @@ namespace Tradebyte\Stock; use Tradebyte\Client; +use Tradebyte\Stock\Model\Stock; use XMLWriter; class Handler @@ -36,6 +37,9 @@ public function updateStockFromStockList(string $filePath): string return $this->client->getRestClient()->postXMLFile($filePath, 'articles/stock'); } + /** + * @param Stock[] $stockArray + */ public function updateStock(array $stockArray): string { $writer = new XMLWriter(); @@ -46,7 +50,19 @@ public function updateStock(array $stockArray): string foreach ($stockArray as $stock) { $writer->startElement('ARTICLE'); $writer->writeElement('A_NR', $stock->getArticleNumber()); - $writer->writeElement('A_STOCK', (string)$stock->getStock()); + + if ($stock->getStock() !== null) { + $writer->writeElement('A_STOCK', (string)$stock->getStock()); + } + + foreach ($stock->getStockForWarehouses() as $warehouseStock) { + $writer->startElement('A_STOCK'); + $writer->writeAttribute('identifier', $warehouseStock['identifier']); + $writer->writeAttribute('key', $warehouseStock['key']); + $writer->text((string)$warehouseStock['stock']); + $writer->endElement(); + } + $writer->endElement(); } diff --git a/src/Stock/Model/Stock.php b/src/Stock/Model/Stock.php index 633998f..5d7ae94 100644 --- a/src/Stock/Model/Stock.php +++ b/src/Stock/Model/Stock.php @@ -12,6 +12,8 @@ class Stock private ?int $stock = null; + private array $stockForWarehouses = []; + public function getArticleNumber(): ?string { return $this->articleNumber; @@ -34,6 +36,21 @@ public function setStock(int $stock): Stock return $this; } + public function getStockForWarehouses(): array + { + return $this->stockForWarehouses; + } + + public function addStockForWarehouse(string $warehouseKey, int $stock): Stock + { + $this->stockForWarehouses[] = [ + 'identifier' => 'key', + 'key' => $warehouseKey, + 'stock' => $stock, + ]; + return $this; + } + public function fillFromSimpleXMLElement(SimpleXMLElement $xmlElement): void { $this->setArticleNumber((string)$xmlElement->A_NR); From c2d177d2877a3a1b4fa5a061bae601c9315cb3a4 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Sun, 21 Apr 2024 11:25:31 +0200 Subject: [PATCH 2/3] adding StockUpdate model. --- examples/stock.php | 10 +-- src/Stock/Handler.php | 30 ++++---- src/Stock/Model/Stock.php | 17 ----- src/Stock/Model/StockUpdate.php | 60 ++++++++++++++++ .../{StockTest.php => StockListTest.php} | 2 +- tests/Stock/StockUpdateTest.php | 69 +++++++++++++++++++ 6 files changed, 151 insertions(+), 37 deletions(-) create mode 100644 src/Stock/Model/StockUpdate.php rename tests/Stock/{StockTest.php => StockListTest.php} (96%) create mode 100644 tests/Stock/StockUpdateTest.php diff --git a/examples/stock.php b/examples/stock.php index 4affa6b..e6c8458 100644 --- a/examples/stock.php +++ b/examples/stock.php @@ -32,17 +32,19 @@ $catalog->close(); /* - * update stock + * one centralised warehouse */ $stockHandler->updateStock([ - (new Tradebyte\Stock\Model\Stock())->setArticleNumber('12345')->setStock(6) + (new Tradebyte\Stock\Model\StockUpdate()) + ->setArticleNumber('12345') + ->setStock(6) ]); /* - * update stock several warehouse + * several warehouses */ $stockHandler->updateStock([ - (new Tradebyte\Stock\Model\Stock()) + (new Tradebyte\Stock\Model\StockUpdate()) ->setArticleNumber('12345') ->addStockForWarehouse('zentrallager', 10) ->addStockForWarehouse('aussenlager', 5) diff --git a/src/Stock/Handler.php b/src/Stock/Handler.php index 57d1b53..56edb61 100644 --- a/src/Stock/Handler.php +++ b/src/Stock/Handler.php @@ -6,6 +6,7 @@ use Tradebyte\Client; use Tradebyte\Stock\Model\Stock; +use Tradebyte\Stock\Model\StockUpdate; use XMLWriter; class Handler @@ -32,13 +33,8 @@ public function downloadStockList(string $filePath, array $filter = []): bool return $this->client->getRestClient()->downloadFile($filePath, 'stock/', $filter); } - public function updateStockFromStockList(string $filePath): string - { - return $this->client->getRestClient()->postXMLFile($filePath, 'articles/stock'); - } - /** - * @param Stock[] $stockArray + * @param StockUpdate[] $stockArray */ public function updateStock(array $stockArray): string { @@ -51,18 +47,22 @@ public function updateStock(array $stockArray): string $writer->startElement('ARTICLE'); $writer->writeElement('A_NR', $stock->getArticleNumber()); - if ($stock->getStock() !== null) { + if ($stock instanceof StockUpdate) { + if ($stock->getStock() !== null) { + $writer->writeElement('A_STOCK', (string)$stock->getStock()); + } + + foreach ($stock->getStockForWarehouses() as $warehouseStock) { + $writer->startElement('A_STOCK'); + $writer->writeAttribute('identifier', $warehouseStock['identifier']); + $writer->writeAttribute('key', $warehouseStock['key']); + $writer->text((string)$warehouseStock['stock']); + $writer->endElement(); + } + } else { $writer->writeElement('A_STOCK', (string)$stock->getStock()); } - foreach ($stock->getStockForWarehouses() as $warehouseStock) { - $writer->startElement('A_STOCK'); - $writer->writeAttribute('identifier', $warehouseStock['identifier']); - $writer->writeAttribute('key', $warehouseStock['key']); - $writer->text((string)$warehouseStock['stock']); - $writer->endElement(); - } - $writer->endElement(); } diff --git a/src/Stock/Model/Stock.php b/src/Stock/Model/Stock.php index 5d7ae94..633998f 100644 --- a/src/Stock/Model/Stock.php +++ b/src/Stock/Model/Stock.php @@ -12,8 +12,6 @@ class Stock private ?int $stock = null; - private array $stockForWarehouses = []; - public function getArticleNumber(): ?string { return $this->articleNumber; @@ -36,21 +34,6 @@ public function setStock(int $stock): Stock return $this; } - public function getStockForWarehouses(): array - { - return $this->stockForWarehouses; - } - - public function addStockForWarehouse(string $warehouseKey, int $stock): Stock - { - $this->stockForWarehouses[] = [ - 'identifier' => 'key', - 'key' => $warehouseKey, - 'stock' => $stock, - ]; - return $this; - } - public function fillFromSimpleXMLElement(SimpleXMLElement $xmlElement): void { $this->setArticleNumber((string)$xmlElement->A_NR); diff --git a/src/Stock/Model/StockUpdate.php b/src/Stock/Model/StockUpdate.php new file mode 100644 index 0000000..f8961f9 --- /dev/null +++ b/src/Stock/Model/StockUpdate.php @@ -0,0 +1,60 @@ +articleNumber; + } + + public function setArticleNumber(string $articleNumber): StockUpdate + { + $this->articleNumber = $articleNumber; + return $this; + } + + public function getStock(): ?int + { + return $this->stock; + } + + public function setStock(int $stock): StockUpdate + { + $this->stock = $stock; + return $this; + } + + public function getStockForWarehouses(): array + { + return $this->stockForWarehouses; + } + + public function addStockForWarehouse(string $warehouseKey, int $stock): StockUpdate + { + $this->stockForWarehouses[] = [ + 'identifier' => 'key', + 'key' => $warehouseKey, + 'stock' => $stock, + ]; + return $this; + } + + public function getRawData(): array + { + return [ + 'article_number' => $this->getArticleNumber(), + 'stock' => $this->getStock(), + 'stockForWarehouses' => $this->getStockForWarehouses(), + ]; + } +} diff --git a/tests/Stock/StockTest.php b/tests/Stock/StockListTest.php similarity index 96% rename from tests/Stock/StockTest.php rename to tests/Stock/StockListTest.php index 6d8da98..8637c5d 100644 --- a/tests/Stock/StockTest.php +++ b/tests/Stock/StockListTest.php @@ -8,7 +8,7 @@ use Tradebyte\Client; use Tradebyte\Stock\Model\Stock; -class StockTest extends Base +class StockListTest extends Base { public function testGetStockListFromFile(): void { diff --git a/tests/Stock/StockUpdateTest.php b/tests/Stock/StockUpdateTest.php new file mode 100644 index 0000000..8c93a55 --- /dev/null +++ b/tests/Stock/StockUpdateTest.php @@ -0,0 +1,69 @@ +
12345' + . '6' + . '7
'; + $mockRestClient = $this->getMockBuilder(Client\Rest::class) + ->disableOriginalConstructor() + ->onlyMethods(['postXML']) + ->getMock(); + $mockRestClient->expects($this->once()) + ->method('postXML') + ->with('articles/stock', $expectedXML); + $mockClient = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->onlyMethods(['getRestClient']) + ->getMock(); + $mockClient->expects($this->any()) + ->method('getRestClient') + ->willReturn($mockRestClient); + $stockHandler = $mockClient->getStockHandler(); + $stockHandler->updateStock( + [ + (new StockUpdate()) + ->setArticleNumber('1234') + ->setStock(5) + ->addStockForWarehouse('test', 6) + ->addStockForWarehouse('test2', 7) + ] + ); + } + + public function testStockObjectGetRawData(): void + { + $stock = new StockUpdate(); + $stock->setStock(20); + $stock->addStockForWarehouse('warehouse', 20); + $stock->addStockForWarehouse('warehouse2', 40); + $stock->setArticleNumber('123456'); + $this->assertSame([ + 'article_number' => '123456', + 'stock' => 20, + 'stockForWarehouses' => [ + 0 => [ + 'identifier' => 'key', + 'key' => 'warehouse', + 'stock' => 20 + ], + 1 => [ + + 'identifier' => 'key', + 'key' => 'warehouse2', + 'stock' => 40 + ] + ] + ], $stock->getRawData()); + } +} From d668a391a750a065afb5e3212d6b11d13822a29d Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Sun, 21 Apr 2024 11:37:23 +0200 Subject: [PATCH 3/3] highlight some deprecations. --- src/Stock/Handler.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Stock/Handler.php b/src/Stock/Handler.php index 56edb61..e0db5f6 100644 --- a/src/Stock/Handler.php +++ b/src/Stock/Handler.php @@ -34,7 +34,17 @@ public function downloadStockList(string $filePath, array $filter = []): bool } /** - * @param StockUpdate[] $stockArray + * @deprecated Don´t use this method, will be removed in next major release. + */ + public function updateStockFromStockList(string $filePath): string + { + return $this->client->getRestClient()->postXMLFile($filePath, 'articles/stock'); + } + + /** + * Don´t use Stock[] anymore as object-array, support will be removed in next major release. + * + * @param Stock[]|StockUpdate[] $stockArray */ public function updateStock(array $stockArray): string {