diff --git a/examples/stock.php b/examples/stock.php index 5570c37..e6c8458 100644 --- a/examples/stock.php +++ b/examples/stock.php @@ -32,8 +32,20 @@ $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) ]); + +/* + * several warehouses + */ +$stockHandler->updateStock([ + (new Tradebyte\Stock\Model\StockUpdate()) + ->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..e0db5f6 100644 --- a/src/Stock/Handler.php +++ b/src/Stock/Handler.php @@ -5,6 +5,8 @@ namespace Tradebyte\Stock; use Tradebyte\Client; +use Tradebyte\Stock\Model\Stock; +use Tradebyte\Stock\Model\StockUpdate; use XMLWriter; class Handler @@ -31,11 +33,19 @@ public function downloadStockList(string $filePath, array $filter = []): bool return $this->client->getRestClient()->downloadFile($filePath, 'stock/', $filter); } + /** + * @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 { $writer = new XMLWriter(); @@ -46,7 +56,23 @@ 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 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()); + } + $writer->endElement(); } 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()); + } +}