mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-13 11:54:32 +02:00
Fixed an exception in getmaxDiscountAmount().
This should fixes an 500 error code, that showed up on some pages. Also added a test method to test this function.
This commit is contained in:
parent
da6b74dfdd
commit
a516879258
2 changed files with 112 additions and 5 deletions
|
@ -25,6 +25,8 @@ namespace App\Services;
|
||||||
use App\Entity\Parts\Part;
|
use App\Entity\Parts\Part;
|
||||||
use App\Entity\PriceInformations\Currency;
|
use App\Entity\PriceInformations\Currency;
|
||||||
use App\Entity\PriceInformations\Pricedetail;
|
use App\Entity\PriceInformations\Pricedetail;
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
use Doctrine\ORM\PersistentCollection;
|
||||||
use Locale;
|
use Locale;
|
||||||
|
|
||||||
class PricedetailHelper
|
class PricedetailHelper
|
||||||
|
@ -53,20 +55,32 @@ class PricedetailHelper
|
||||||
foreach ($orderdetails as $orderdetail) {
|
foreach ($orderdetails as $orderdetail) {
|
||||||
$pricedetails = $orderdetail->getPricedetails();
|
$pricedetails = $orderdetail->getPricedetails();
|
||||||
//The orderdetail must have pricedetails, otherwise this will not work!
|
//The orderdetail must have pricedetails, otherwise this will not work!
|
||||||
if (empty($pricedetails)) {
|
if (count($pricedetails) === 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pricedetails in orderdetails are ordered by min discount quantity,
|
if ($pricedetails instanceof PersistentCollection) {
|
||||||
so our first object is our min order amount for the current orderdetail */
|
/* Pricedetails in orderdetails are ordered by min discount quantity,
|
||||||
$max_amount = $pricedetails->last()->getMinDiscountQuantity();
|
so our first object is our min order amount for the current orderdetail */
|
||||||
|
$max_amount = $pricedetails->last()->getMinDiscountQuantity();
|
||||||
|
} else {
|
||||||
|
// We have to sort the pricedetails manually
|
||||||
|
$array = $pricedetails->map(
|
||||||
|
function (Pricedetail $pricedetail) {
|
||||||
|
return $pricedetail->getMinDiscountQuantity();
|
||||||
|
}
|
||||||
|
)->toArray();
|
||||||
|
sort($array);
|
||||||
|
$max_amount = end($array);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if ($max_amount > $max) {
|
if ($max_amount > $max) {
|
||||||
$max = $max_amount;
|
$max = $max_amount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($max > 0) {
|
if ($max > 0.0) {
|
||||||
return $max;
|
return $max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
93
tests/Services/PricedetailHelperTest.php
Normal file
93
tests/Services/PricedetailHelperTest.php
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony)
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Tests\Services;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Entity\Parts\Part;
|
||||||
|
use App\Entity\PriceInformations\Orderdetail;
|
||||||
|
use App\Entity\PriceInformations\Pricedetail;
|
||||||
|
use App\Services\AmountFormatter;
|
||||||
|
use App\Services\PricedetailHelper;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
|
||||||
|
class PricedetailHelperTest extends WebTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var AmountFormatter
|
||||||
|
*/
|
||||||
|
protected $service;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
//Get an service instance.
|
||||||
|
self::bootKernel();
|
||||||
|
$this->service = self::$container->get(PricedetailHelper::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function maxDiscountAmountDataProvider()
|
||||||
|
{
|
||||||
|
$part = new Part();
|
||||||
|
yield [$part, null, 'Part without any orderdetails failed!'];
|
||||||
|
|
||||||
|
//Part with empty orderdetails
|
||||||
|
$part = new Part();
|
||||||
|
$orderdetail = new Orderdetail();
|
||||||
|
$part->addOrderdetail($orderdetail);
|
||||||
|
yield [$part, null, 'Part with one empty orderdetail failed!'];
|
||||||
|
|
||||||
|
$part = new Part();
|
||||||
|
$orderdetail = new Orderdetail();
|
||||||
|
$part->addOrderdetail($orderdetail);
|
||||||
|
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1));
|
||||||
|
yield [$part, 1.0, 'Part with one pricedetail failed!'];
|
||||||
|
|
||||||
|
$part = new Part();
|
||||||
|
$orderdetail = new Orderdetail();
|
||||||
|
$part->addOrderdetail($orderdetail);
|
||||||
|
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1));
|
||||||
|
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(2));
|
||||||
|
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1.5));
|
||||||
|
yield [$part, 2.0, 'Part with multiple pricedetails failed!'];
|
||||||
|
|
||||||
|
$part = new Part();
|
||||||
|
$orderdetail = new Orderdetail();
|
||||||
|
$orderdetail2 = new Orderdetail();
|
||||||
|
$part->addOrderdetail($orderdetail);
|
||||||
|
$part->addOrderdetail($orderdetail2);
|
||||||
|
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1));
|
||||||
|
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(2));
|
||||||
|
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1.5));
|
||||||
|
$orderdetail2->addPricedetail((new Pricedetail())->setMinDiscountQuantity(10));
|
||||||
|
|
||||||
|
yield [$part, 10.0, 'Part with multiple orderdetails failed'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider maxDiscountAmountDataProvider
|
||||||
|
*/
|
||||||
|
public function testGetMaxDiscountAmount(Part $part, ?float $expected_result, string $message)
|
||||||
|
{
|
||||||
|
$this->assertEquals($expected_result, $this->service->getMaxDiscountAmount($part), $message);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue