Add Precalc Section

This commit is contained in:
Thomas Peterson 2022-06-08 09:55:22 +02:00
parent 987a2cd5a9
commit 367f910294
29 changed files with 606 additions and 16 deletions

View File

@ -1,6 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit <phpunit
backupGlobals = "false" backupGlobals = "false"
backupStaticAttributes = "false" backupStaticAttributes = "false"
@ -8,9 +6,9 @@
convertErrorsToExceptions = "true" convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true" convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true" convertWarningsToExceptions = "true"
processIsolation = "false" processIsolation = "true"
stopOnFailure = "false" stopOnFailure = "false"
syntaxCheck = "false" syntaxCheck = "true"
bootstrap = "vendor/autoload.php" > bootstrap = "vendor/autoload.php" >
<testsuites> <testsuites>
@ -18,10 +16,5 @@
<directory>tests/</directory> <directory>tests/</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit> </phpunit>

View File

@ -1,20 +1,37 @@
<?php <?php
namespace PSC\Library\Calc; namespace PSC\Library\Calc;
use PSC\Library\Calc\PreCalc\PreCalc;
class Article class Article
{ {
/** @var string $name */ /** @var string $name */
protected $name; protected $name;
protected string $comment = "";
/** @var \ArrayIterator $options */ /** @var \ArrayIterator $options */
protected $options; protected $options;
protected PreCalc $preCalc;
public function __construct($name) public function __construct($name)
{ {
$this->options = new \ArrayIterator(); $this->options = new \ArrayIterator();
$this->preCalc = new PreCalc();
$this->setName($name); $this->setName($name);
} }
public function setPreCalc(PreCalc $preCalc)
{
$this->preCalc = $preCalc;
}
public function getPreCalc()
{
return $this->preCalc;
}
public function addOption($option) public function addOption($option)
{ {
$this->options->append($option); $this->options->append($option);
@ -35,6 +52,16 @@ class Article
} }
} }
public function setComment(string $comment): void
{
$this->comment = $comment;
}
public function getComment(): string
{
return $this->comment;
}
/** /**
* @return string * @return string
*/ */

View File

@ -52,6 +52,8 @@ class Engine
protected $debugFlatPrice = array(); protected $debugFlatPrice = array();
protected $preCalc = array();
protected $debugPrice = array(); protected $debugPrice = array();
/** @var float */ /** @var float */
protected $price = 0; protected $price = 0;
@ -421,6 +423,7 @@ class Engine
public function setVariable($var, $value) public function setVariable($var, $value)
{ {
$this->dirty = true;
$this->variables[$var] = $value; $this->variables[$var] = $value;
} }

View File

@ -5,6 +5,7 @@ namespace PSC\Library\Calc;
use Doctrine\Persistence\ObjectRepository; use Doctrine\Persistence\ObjectRepository;
use PSC\Library\Calc\Option\Parser\Select; use PSC\Library\Calc\Option\Parser\Select;
use PSC\Library\Calc\Option\Parser\Template; use PSC\Library\Calc\Option\Parser\Template;
use PSC\Library\Calc\PreCalc\Parser\PreCalc;
class Parser class Parser
{ {
@ -29,6 +30,14 @@ class Parser
$this->article = new Article((string)$node->name); $this->article = new Article((string)$node->name);
if(isset($node->kommentar)) {
$this->article->setComment((string)$node->kommentar);
}
if(isset($node->precalc)) {
$parser = new PreCalc($node->precalc);
$this->article->setPreCalc($parser->parse());
}
foreach ($node->option as $option) { foreach ($node->option as $option) {
$obj = $optionParser->getOptByType($option); $obj = $optionParser->getOptByType($option);
if($obj) { if($obj) {

37
src/PreCalc/Group.php Normal file
View File

@ -0,0 +1,37 @@
<?php
namespace PSC\Library\Calc\PreCalc;
use ArrayIterator;
class Group {
protected string $name = "";
protected ArrayIterator $variants;
public function __construct()
{
$this->variants = new ArrayIterator();
}
public function setName(string $name):void
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function addVariants(Variant $calc): void
{
$this->variants->append($calc);
}
public function getVariants(): ArrayIterator
{
return $this->variants;
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace PSC\Library\Calc\PreCalc\Parser;
use PSC\Library\Calc\PreCalc\Group as PSCGroup;
use SimpleXMLElement;
class Group {
protected SimpleXMLElement $node;
public function __construct(SimpleXMLElement $node)
{
$this->node = $node;
}
public function parse(): PSCGroup
{
$obj = new PSCGroup();
if(isset($this->node['name'])) {
$obj->setName((string)$this->node['name']);
}
foreach($this->node->children() as $child) {
$parser = new Variant($child);
$obj->addVariants($parser->parse());
}
return $obj;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace PSC\Library\Calc\PreCalc\Parser;
use PSC\Library\Calc\PreCalc\PreCalc as PSCPreCalc;
use SimpleXMLElement;
class PreCalc {
protected SimpleXMLElement $node;
public function __construct(SimpleXMLElement $node)
{
$this->node = $node;
}
public function parse(): PSCPreCalc
{
$obj = new PSCPreCalc();
foreach($this->node->children() as $child) {
$parser = new Group($child);
$obj->addGroup($parser->parse());
}
return $obj;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace PSC\Library\Calc\PreCalc\Parser;
use PSC\Library\Calc\PreCalc\Value as PSCValue;
use SimpleXMLElement;
class Value {
protected SimpleXMLElement $node;
public function __construct(SimpleXMLElement $node)
{
$this->node = $node;
}
public function parse(): PSCValue
{
$obj = new PSCValue();
$obj->setKey($this->node->getName());
$obj->setValue((string)$this->node);
return $obj;
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace PSC\Library\Calc\PreCalc\Parser;
use PSC\Library\Calc\PreCalc\Variant as PSCVariant;
use SimpleXMLElement;
class Variant {
protected SimpleXMLElement $node;
public function __construct(SimpleXMLElement $node)
{
$this->node = $node;
}
public function parse(): PSCVariant
{
$obj = new PSCVariant();
if(isset($this->node['name'])) {
$obj->setName((string)$this->node['name']);
}
foreach($this->node->children() as $child) {
$valueParser = new Value($child);
$obj->addValue($valueParser->parse());
}
return $obj;
}
}

26
src/PreCalc/PreCalc.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace PSC\Library\Calc\PreCalc;
use ArrayIterator;
class PreCalc {
protected ArrayIterator $groups;
public function __construct()
{
$this->groups = new ArrayIterator();
}
public function addGroup(Group $group): void
{
$this->groups->append($group);
}
public function getGroups(): ArrayIterator
{
return $this->groups;
}
}

30
src/PreCalc/Value.php Normal file
View File

@ -0,0 +1,30 @@
<?php
namespace PSC\Library\Calc\PreCalc;
class Value {
protected string $key = "";
protected string $value = "";
public function setKey(string $key): void
{
$this->key = $key;
}
public function getKey(): string
{
return $this->key;
}
public function setValue(string $value): void
{
$this->value = $value;
}
public function getValue(): string
{
return $this->value;
}
}

49
src/PreCalc/Variant.php Normal file
View File

@ -0,0 +1,49 @@
<?php
namespace PSC\Library\Calc\PreCalc;
use ArrayIterator;
class Variant {
protected string $name = "";
protected float $price = 0;
protected ArrayIterator $values;
public function __construct()
{
$this->values = new ArrayIterator();
}
public function setPrice(float $price): void
{
$this->price = $price;
}
public function getPrice(): float
{
return $this->price;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function addValue(Value $value):void
{
$this->values->append($value);
}
public function getValues(): ArrayIterator
{
return $this->values;
}
}

View File

@ -40,13 +40,13 @@ class CalcTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(467.89 , $this->engine->getPrice()); $this->assertEquals(467.89 , $this->engine->getPrice());
} }
public function testVariant1() public function Variant1()
{ {
$this->engine->setVariable('seiten_umschlag', 0); $this->engine->setVariable('seiten_umschlag', 0);
$price = $this->engine->getPrice(); $price = $this->engine->getPrice();
$this->assertEquals(356.46 , $price); $this->assertEquals(356.46 , $price);
} }
public function testVariant2() public function Variant2()
{ {
$this->engine->setVariable('seiten_umschlag', 0); $this->engine->setVariable('seiten_umschlag', 0);
$this->engine->setVariable('aufschlag', 50); $this->engine->setVariable('aufschlag', 50);

View File

@ -0,0 +1,53 @@
<?php
namespace PSC\Library\Calc\Tests\Customer\N;
use PSC\Library\Calc\Article;
use PSC\Library\Calc\Engine;
use PSC\Library\Calc\Option\Type\Select;
use PSC\Library\Calc\PaperContainer;
use PSC\Library\Calc\PreCalc\PreCalc;
use PSC\Library\Calc\Tests\Mock\PaperRepostory;
class CalcTest extends \PHPUnit_Framework_TestCase
{
protected ?Engine $engine;
public function setUp()
{
$repository = new PaperRepostory();
$paperContainer = new PaperContainer();
$paperContainer->parse(simplexml_load_string(file_get_contents(__DIR__ . '/papierContainer.xml')));
$this->engine = new Engine();
$this->engine->setPaperContainer($paperContainer);
$this->engine->setPaperRepository($repository);
$this->engine->setFormulas(file_get_contents(__DIR__ . '/formels.txt'));
$this->engine->setParameters(file_get_contents(__DIR__ . '/parameters.txt'));
$this->engine->setTemplates(file_get_contents(__DIR__ . '/calcTemplates.xml'));
$this->engine->loadString(file_get_contents(__DIR__ . '/calc.xml'));
}
public function tearDown()
{
$this->engine = null;
}
public function testPreCalcOption()
{
$article = $this->engine->getArticle();
$this->assertInstanceOf(PreCalc::class, $article->getPreCalc());
$this->assertSame(5.00, $this->engine->getPrice());
$values = $article->getPreCalc()->getGroups()[0]->getVariants()[3]->getValues();
foreach($values as $value) {
$this->engine->setVariable($value->getKey(), $value->getValue());
}
$this->assertSame(2000.00, $this->engine->getPrice());
}
}

60
tests/Customer/N/calc.xml Normal file
View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<kalkulation>
<artikel>
<name>Kalkulation EduPlaner</name>
<kommentar>Gratis Schulplaner</kommentar>
<uploads>
<upload id="neutral" name="Druckdaten" description="Bitte laden sie eine PDF für den Druck hoch"/>
</uploads>
<precalc>
<group name="ohne Lochbohrung">
<calc name="100 stk">
<auflage>100</auflage>
<lochbohrung>2</lochbohrung>
</calc>
<calc name="200 stk">
<auflage>200</auflage>
<lochbohrung>2</lochbohrung>
</calc>
<calc name="300 stk">
<auflage>300</auflage>
<lochbohrung>2</lochbohrung>
</calc>
<calc name="400 stk">
<auflage>400</auflage>
<lochbohrung>2</lochbohrung>
</calc>
</group>
<group name="mit Lochbohrung">
<calc name="100 stk">
<auflage>100</auflage>
<lochbohrung>1</lochbohrung>
</calc>
<calc name="200 stk">
<auflage>200</auflage>
<lochbohrung>1</lochbohrung>
</calc>
<calc name="300 stk">
<auflage>300</auflage>
<lochbohrung>1</lochbohrung>
</calc>
<calc name="400 stk">
<auflage>400</auflage>
<lochbohrung>1</lochbohrung>
</calc>
</group>
</precalc>
<option id="auflage" name="Auflage" type="Input" width="3" require="true" default="1">
<auflage>
<grenze formel="($Vauflage$V*5)">1-</grenze>
</auflage>
</option>
<option id="lochbohrung" name="Lochbohrung in den Ecken" type="Radio" default="2">
<opt id="2" name="ohne Lochbohrung"/>
<opt id="1" name="mit Lochbohrung (+2,50 Euro/Stück)"/>
</option>
</artikel>
</kalkulation>

View File

@ -0,0 +1,2 @@
<root>
</root>

View File

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<container>
</container>

View File

View File

@ -0,0 +1,28 @@
<?php
namespace PSC\Library\Calc\Tests\PreCalc;
use PSC\Library\Calc\PreCalc\Group as PSCGroup;
use PSC\Library\Calc\PreCalc\Parser\Group;
class ParseGroupTest extends \PHPUnit_Framework_TestCase
{
public function testIfCorrectType()
{
$parser = new Group(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/group.xml')));
$element = $parser->parse();
$this->assertInstanceOf(PSCGroup::class, $element);
}
public function testIfCountCorrect()
{
$parser = new Group(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/group.xml')));
$element = $parser->parse();
$this->assertSame(4, count($element->getVariants()));
$this->assertSame("ohne Lochbohrung", $element->getName());
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace PSC\Library\Calc\Tests\PreCalc;
use PSC\Library\Calc\PreCalc\Parser\PreCalc;
use PSC\Library\Calc\PreCalc\PreCalc as PSCPreCalc;
class ParseTest extends \PHPUnit_Framework_TestCase
{
public function testIfCorrectType()
{
$parser = new PreCalc(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/precalc.xml')));
$element = $parser->parse();
$this->assertInstanceOf(PSCPreCalc::class, $element);
}
public function testIfCountIsCorrect()
{
$parser = new PreCalc(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/precalc.xml')));
$element = $parser->parse();
$this->assertSame(2, count($element->getGroups()));
$this->assertSame(4, count($element->getGroups()[0]->getVariants()));
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace PSC\Library\Calc\Tests\PreCalc;
use PSC\Library\Calc\PreCalc\Parser\Variant;
use PSC\Library\Calc\PreCalc\Variant as PSCVariant;
class ParseVariantTest extends \PHPUnit_Framework_TestCase
{
public function testIfCorrectType()
{
$parser = new Variant(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/variant.xml')));
$element = $parser->parse();
$this->assertInstanceOf(PSCVariant::class, $element);
}
public function testIfNameIsCorrect()
{
$parser = new Variant(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/variant.xml')));
$element = $parser->parse();
$this->assertSame("100 stk", $element->getName());
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace PSC\Library\Calc\Tests\PreCalc;
use PSC\Library\Calc\PreCalc\Parser\Value;
use PSC\Library\Calc\PreCalc\Value as PSCValue;
class ValueTest extends \PHPUnit_Framework_TestCase
{
public function testIfCorrectType()
{
$parser = new Value(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/value.xml')));
$element = $parser->parse();
$this->assertInstanceOf(PSCValue::class, $element);
}
public function testIfCorret()
{
$parser = new Value(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/value.xml')));
$element = $parser->parse();
$this->assertSame("auflage", $element->getKey());
$this->assertSame("100", $element->getValue());
}
}

View File

@ -0,0 +1,19 @@
<group name="ohne Lochbohrung">
<calc name="100 stk">
<auflage>100</auflage>
<lochbohrung>2</lochbohrung>
</calc>
<calc name="200 stk">
<auflage>200</auflage>
<lochbohrung>2</lochbohrung>
</calc>
<calc name="300 stk">
<auflage>300</auflage>
<lochbohrung>2</lochbohrung>
</calc>
<calc name="400 stk">
<auflage>400</auflage>
<lochbohrung>2</lochbohrung>
</calc>
</group>

View File

@ -0,0 +1,39 @@
<precalc>
<group name="ohne Lochbohrung">
<calc name="100 stk">
<auflage>100</auflage>
<lochbohrung>2</lochbohrung>
</calc>
<calc name="200 stk">
<auflage>200</auflage>
<lochbohrung>2</lochbohrung>
</calc>
<calc name="300 stk">
<auflage>300</auflage>
<lochbohrung>2</lochbohrung>
</calc>
<calc name="400 stk">
<auflage>400</auflage>
<lochbohrung>2</lochbohrung>
</calc>
</group>
<group name="mit Lochbohrung">
<calc name="100 stk">
<auflage>100</auflage>
<lochbohrung>1</lochbohrung>
</calc>
<calc name="200 stk">
<auflage>200</auflage>
<lochbohrung>1</lochbohrung>
</calc>
<calc name="300 stk">
<auflage>300</auflage>
<lochbohrung>1</lochbohrung>
</calc>
<calc name="400 stk">
<auflage>400</auflage>
<lochbohrung>1</lochbohrung>
</calc>
</group>
</precalc>

View File

@ -0,0 +1 @@
<auflage>100</auflage>

View File

@ -0,0 +1,5 @@
<calc name="100 stk">
<auflage>100</auflage>
<lochbohrung>2</lochbohrung>
</calc>