Skip to content

Commit

Permalink
adding StockUpdate model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Meyer authored and Dominik Meyer committed Apr 21, 2024
1 parent c1a5bb8 commit c2d177d
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 37 deletions.
10 changes: 6 additions & 4 deletions examples/stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions src/Stock/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Tradebyte\Client;
use Tradebyte\Stock\Model\Stock;
use Tradebyte\Stock\Model\StockUpdate;
use XMLWriter;

class Handler
Expand All @@ -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
{
Expand All @@ -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();
}

Expand Down
17 changes: 0 additions & 17 deletions src/Stock/Model/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class Stock

private ?int $stock = null;

private array $stockForWarehouses = [];

public function getArticleNumber(): ?string
{
return $this->articleNumber;
Expand All @@ -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);
Expand Down
60 changes: 60 additions & 0 deletions src/Stock/Model/StockUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Tradebyte\Stock\Model;

class StockUpdate
{
private ?string $articleNumber = null;

private ?int $stock = null;

private array $stockForWarehouses = [];

public function getArticleNumber(): ?string
{
return $this->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(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Tradebyte\Client;
use Tradebyte\Stock\Model\Stock;

class StockTest extends Base
class StockListTest extends Base
{
public function testGetStockListFromFile(): void
{
Expand Down
69 changes: 69 additions & 0 deletions tests/Stock/StockUpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Tradebyte\Stock;

use Tradebyte\Base;
use Tradebyte\Client;
use Tradebyte\Stock\Model\StockUpdate;

class StockUpdateTest extends Base
{
public function testUpdateStock(): void
{
$expectedXML = '<TBCATALOG><ARTICLEDATA><ARTICLE><A_NR>1234</A_NR><A_STOCK>5</A_STOCK>'
. '<A_STOCK identifier="key" key="test">6</A_STOCK>'
. '<A_STOCK identifier="key" key="test2">7</A_STOCK></ARTICLE></ARTICLEDATA></TBCATALOG>';
$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());
}
}

0 comments on commit c2d177d

Please sign in to comment.