Skip to content

Commit

Permalink
added addStockItem
Browse files Browse the repository at this point in the history
  • Loading branch information
segy committed Nov 23, 2023
1 parent 690c883 commit 304afce
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
21 changes: 21 additions & 0 deletions spec/Pohoda/StockSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ public function it_can_set_action_type()
$this->getXML()->asXML()->shouldReturn('<stk:stock version="2.0"><stk:actionType><stk:update><ftr:filter><ftr:code>CODE</ftr:code><ftr:store><typ:ids>STORAGE</typ:ids></ftr:store></ftr:filter></stk:update></stk:actionType><stk:stockHeader>' . $this->_defaultHeader() . '</stk:stockHeader></stk:stock>');
}

public function it_can_add_stock_items()
{
$this->addStockItem([
'storage' => ['ids' => 'MATERIÁL'],
'code' => 'B03',
'name' => 'Spojovacia doska',
'count' => 88,
'quantity' => 1,
'stockPriceItem' => [
[
'stockPrice' => ['ids' => 'Cena 1', 'price' => 294]
],
[
'stockPrice' => ['ids' => 'MOC', 'price' => 393.3]
]
]
]);

$this->getXML()->asXML()->shouldReturn('<stk:stock version="2.0"><stk:stockHeader>' . $this->_defaultHeader() . '</stk:stockHeader><stk:stockDetail><stk:stockItem><stk:storage><typ:ids>MATERIÁL</typ:ids></stk:storage><stk:code>B03</stk:code><stk:name>Spojovacia doska</stk:name><stk:count>88</stk:count><stk:quantity>1</stk:quantity><stk:stockPriceItem><stk:stockPrice><typ:ids>Cena 1</typ:ids><typ:price>294</typ:price></stk:stockPrice><stk:stockPrice><typ:ids>MOC</typ:ids><typ:price>393.3</typ:price></stk:stockPrice></stk:stockPriceItem></stk:stockItem></stk:stockDetail></stk:stock>');
}

public function it_can_set_prices()
{
$this->addPrice('Price1', 20.43);
Expand Down
25 changes: 22 additions & 3 deletions src/Pohoda/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Riesenia\Pohoda\Common\OptionsResolver;
use Riesenia\Pohoda\Stock\Header;
use Riesenia\Pohoda\Stock\Price;
use Riesenia\Pohoda\Stock\StockItem;

class Stock extends Agenda
{
Expand All @@ -37,6 +38,24 @@ public function __construct(array $data, string $ico, bool $resolveOptions = tru
parent::__construct($data, $ico, $resolveOptions);
}

/**
* Add stock item.
*
* @param array<string,mixed> $data
*
* @return $this
*/
public function addStockItem(array $data): self
{
if (!isset($this->_data['stockDetail'])) {
$this->_data['stockDetail'] = [];
}

$this->_data['stockDetail'][] = new StockItem($data, $this->_ico);

return $this;
}

/**
* Add price.
*
Expand All @@ -52,8 +71,8 @@ public function addPrice(string $code, float $value): self
}

$this->_data['stockPriceItem'][] = new Price([
'code' => $code,
'value' => $value
'ids' => $code,
'price' => $value
], $this->_ico);

return $this;
Expand Down Expand Up @@ -112,7 +131,7 @@ public function getXML(): \SimpleXMLElement
$xml = $this->_createXML()->addChild('stk:stock', '', $this->_namespace('stk'));
$xml->addAttribute('version', '2.0');

$this->_addElements($xml, ['actionType', 'header', 'stockPriceItem'], 'stk');
$this->_addElements($xml, ['actionType', 'header', 'stockDetail', 'stockPriceItem'], 'stk');

return $xml;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pohoda/Stock/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getXML(): \SimpleXMLElement
{
$xml = $this->_createXML()->addChild('stk:stockPriceItem', '', $this->_namespace('stk'));

return $this->_addRefElement($xml, 'stk:stockPrice', ['ids' => $this->_data['code'], 'price' => $this->_data['value']]);
return $this->_addRefElement($xml, 'stk:stockPrice', $this->_data);
}

/**
Expand All @@ -31,6 +31,6 @@ public function getXML(): \SimpleXMLElement
protected function _configureOptions(OptionsResolver $resolver)
{
// available options
$resolver->setDefined(['code', 'value']);
$resolver->setDefined(['ids', 'price']);
}
}
63 changes: 63 additions & 0 deletions src/Pohoda/Stock/StockItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* This file is part of riesenia/pohoda package.
*
* Licensed under the MIT License
* (c) RIESENIA.com
*/

declare(strict_types=1);

namespace Riesenia\Pohoda\Stock;

use Riesenia\Pohoda\Agenda;
use Riesenia\Pohoda\Common\OptionsResolver;

class StockItem extends Agenda
{
/** @var string[] */
protected $_refElements = ['stockInfo', 'storage'];

/** @var string[] */
protected $_elements = ['id', 'stockInfo', 'storage', 'code', 'name', 'count', 'quantity', 'stockPriceItem'];

/**
* {@inheritdoc}
*/
public function __construct(array $data, string $ico, bool $resolveOptions = true)
{
// process stockPriceItem
if (isset($data['stockPriceItem'])) {
$data['stockPriceItem'] = \array_map(function ($stockPriceItem) use ($ico, $resolveOptions) {
return new Price($stockPriceItem['stockPrice'], $ico, $resolveOptions);
}, $data['stockPriceItem']);
}

parent::__construct($data, $ico, $resolveOptions);
}

/**
* {@inheritdoc}
*/
public function getXML(): \SimpleXMLElement
{
$xml = $this->_createXML()->addChild('stk:stockItem', '', $this->_namespace('stk'));

$this->_addElements($xml, $this->_elements, 'stk');

return $xml;
}

/**
* {@inheritdoc}
*/
protected function _configureOptions(OptionsResolver $resolver)
{
// available options
$resolver->setDefined($this->_elements);

$resolver->setNormalizer('id', $resolver->getNormalizer('int'));
$resolver->setNormalizer('count', $resolver->getNormalizer('float'));
$resolver->setNormalizer('quantity', $resolver->getNormalizer('float'));
}
}

0 comments on commit 304afce

Please sign in to comment.