Skip to content

Commit 20c4f79

Browse files
authored
Merge pull request #14 from kinimodmeyer/several-warehouses
Work in Progress - #13
2 parents 1a0286e + d668a39 commit 20c4f79

5 files changed

Lines changed: 171 additions & 4 deletions

File tree

examples/stock.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,20 @@
3232
$catalog->close();
3333

3434
/*
35-
* update stock
35+
* one centralised warehouse
3636
*/
3737
$stockHandler->updateStock([
38-
(new Tradebyte\Stock\Model\Stock())->setArticleNumber('12345')->setStock(6)
38+
(new Tradebyte\Stock\Model\StockUpdate())
39+
->setArticleNumber('12345')
40+
->setStock(6)
3941
]);
42+
43+
/*
44+
* several warehouses
45+
*/
46+
$stockHandler->updateStock([
47+
(new Tradebyte\Stock\Model\StockUpdate())
48+
->setArticleNumber('12345')
49+
->addStockForWarehouse('zentrallager', 10)
50+
->addStockForWarehouse('aussenlager', 5)
51+
]);

src/Stock/Handler.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Tradebyte\Stock;
66

77
use Tradebyte\Client;
8+
use Tradebyte\Stock\Model\Stock;
9+
use Tradebyte\Stock\Model\StockUpdate;
810
use XMLWriter;
911

1012
class Handler
@@ -31,11 +33,19 @@ public function downloadStockList(string $filePath, array $filter = []): bool
3133
return $this->client->getRestClient()->downloadFile($filePath, 'stock/', $filter);
3234
}
3335

36+
/**
37+
* @deprecated Don´t use this method, will be removed in next major release.
38+
*/
3439
public function updateStockFromStockList(string $filePath): string
3540
{
3641
return $this->client->getRestClient()->postXMLFile($filePath, 'articles/stock');
3742
}
3843

44+
/**
45+
* Don´t use Stock[] anymore as object-array, support will be removed in next major release.
46+
*
47+
* @param Stock[]|StockUpdate[] $stockArray
48+
*/
3949
public function updateStock(array $stockArray): string
4050
{
4151
$writer = new XMLWriter();
@@ -46,7 +56,23 @@ public function updateStock(array $stockArray): string
4656
foreach ($stockArray as $stock) {
4757
$writer->startElement('ARTICLE');
4858
$writer->writeElement('A_NR', $stock->getArticleNumber());
49-
$writer->writeElement('A_STOCK', (string)$stock->getStock());
59+
60+
if ($stock instanceof StockUpdate) {
61+
if ($stock->getStock() !== null) {
62+
$writer->writeElement('A_STOCK', (string)$stock->getStock());
63+
}
64+
65+
foreach ($stock->getStockForWarehouses() as $warehouseStock) {
66+
$writer->startElement('A_STOCK');
67+
$writer->writeAttribute('identifier', $warehouseStock['identifier']);
68+
$writer->writeAttribute('key', $warehouseStock['key']);
69+
$writer->text((string)$warehouseStock['stock']);
70+
$writer->endElement();
71+
}
72+
} else {
73+
$writer->writeElement('A_STOCK', (string)$stock->getStock());
74+
}
75+
5076
$writer->endElement();
5177
}
5278

src/Stock/Model/StockUpdate.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tradebyte\Stock\Model;
6+
7+
class StockUpdate
8+
{
9+
private ?string $articleNumber = null;
10+
11+
private ?int $stock = null;
12+
13+
private array $stockForWarehouses = [];
14+
15+
public function getArticleNumber(): ?string
16+
{
17+
return $this->articleNumber;
18+
}
19+
20+
public function setArticleNumber(string $articleNumber): StockUpdate
21+
{
22+
$this->articleNumber = $articleNumber;
23+
return $this;
24+
}
25+
26+
public function getStock(): ?int
27+
{
28+
return $this->stock;
29+
}
30+
31+
public function setStock(int $stock): StockUpdate
32+
{
33+
$this->stock = $stock;
34+
return $this;
35+
}
36+
37+
public function getStockForWarehouses(): array
38+
{
39+
return $this->stockForWarehouses;
40+
}
41+
42+
public function addStockForWarehouse(string $warehouseKey, int $stock): StockUpdate
43+
{
44+
$this->stockForWarehouses[] = [
45+
'identifier' => 'key',
46+
'key' => $warehouseKey,
47+
'stock' => $stock,
48+
];
49+
return $this;
50+
}
51+
52+
public function getRawData(): array
53+
{
54+
return [
55+
'article_number' => $this->getArticleNumber(),
56+
'stock' => $this->getStock(),
57+
'stockForWarehouses' => $this->getStockForWarehouses(),
58+
];
59+
}
60+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Tradebyte\Client;
99
use Tradebyte\Stock\Model\Stock;
1010

11-
class StockTest extends Base
11+
class StockListTest extends Base
1212
{
1313
public function testGetStockListFromFile(): void
1414
{

tests/Stock/StockUpdateTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tradebyte\Stock;
6+
7+
use Tradebyte\Base;
8+
use Tradebyte\Client;
9+
use Tradebyte\Stock\Model\StockUpdate;
10+
11+
class StockUpdateTest extends Base
12+
{
13+
public function testUpdateStock(): void
14+
{
15+
$expectedXML = '<TBCATALOG><ARTICLEDATA><ARTICLE><A_NR>1234</A_NR><A_STOCK>5</A_STOCK>'
16+
. '<A_STOCK identifier="key" key="test">6</A_STOCK>'
17+
. '<A_STOCK identifier="key" key="test2">7</A_STOCK></ARTICLE></ARTICLEDATA></TBCATALOG>';
18+
$mockRestClient = $this->getMockBuilder(Client\Rest::class)
19+
->disableOriginalConstructor()
20+
->onlyMethods(['postXML'])
21+
->getMock();
22+
$mockRestClient->expects($this->once())
23+
->method('postXML')
24+
->with('articles/stock', $expectedXML);
25+
$mockClient = $this->getMockBuilder(Client::class)
26+
->disableOriginalConstructor()
27+
->onlyMethods(['getRestClient'])
28+
->getMock();
29+
$mockClient->expects($this->any())
30+
->method('getRestClient')
31+
->willReturn($mockRestClient);
32+
$stockHandler = $mockClient->getStockHandler();
33+
$stockHandler->updateStock(
34+
[
35+
(new StockUpdate())
36+
->setArticleNumber('1234')
37+
->setStock(5)
38+
->addStockForWarehouse('test', 6)
39+
->addStockForWarehouse('test2', 7)
40+
]
41+
);
42+
}
43+
44+
public function testStockObjectGetRawData(): void
45+
{
46+
$stock = new StockUpdate();
47+
$stock->setStock(20);
48+
$stock->addStockForWarehouse('warehouse', 20);
49+
$stock->addStockForWarehouse('warehouse2', 40);
50+
$stock->setArticleNumber('123456');
51+
$this->assertSame([
52+
'article_number' => '123456',
53+
'stock' => 20,
54+
'stockForWarehouses' => [
55+
0 => [
56+
'identifier' => 'key',
57+
'key' => 'warehouse',
58+
'stock' => 20
59+
],
60+
1 => [
61+
62+
'identifier' => 'key',
63+
'key' => 'warehouse2',
64+
'stock' => 40
65+
]
66+
]
67+
], $stock->getRawData());
68+
}
69+
}

0 commit comments

Comments
 (0)