mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-20 17:15:51 +02:00
Use BigDecimal object type for Supplier shipping costs, instead of bcmath string.
This commit is contained in:
parent
ae23a82105
commit
08267b88b0
10 changed files with 312 additions and 14 deletions
|
@ -11,6 +11,7 @@
|
|||
"ext-json": "*",
|
||||
"ext-mbstring": "*",
|
||||
"beberlei/doctrineextensions": "^1.2",
|
||||
"brick/math": "^0.8.15",
|
||||
"doctrine/annotations": "^1.6",
|
||||
"doctrine/doctrine-bundle": "^2.0",
|
||||
"dompdf/dompdf": "^0.8.5",
|
||||
|
|
48
composer.lock
generated
48
composer.lock
generated
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "19cd1b3160aed1bd1f07a776339b462d",
|
||||
"content-hash": "23b9810991f322cda669c851011f7bbf",
|
||||
"packages": [
|
||||
{
|
||||
"name": "beberlei/assert",
|
||||
|
@ -122,6 +122,52 @@
|
|||
],
|
||||
"time": "2019-12-05T09:49:04+00:00"
|
||||
},
|
||||
{
|
||||
"name": "brick/math",
|
||||
"version": "0.8.15",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/brick/math.git",
|
||||
"reference": "9b08d412b9da9455b210459ff71414de7e6241cd"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/brick/math/zipball/9b08d412b9da9455b210459ff71414de7e6241cd",
|
||||
"reference": "9b08d412b9da9455b210459ff71414de7e6241cd",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-json": "*",
|
||||
"php": "^7.1|^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"php-coveralls/php-coveralls": "^2.2",
|
||||
"phpunit/phpunit": "^7.5.15|^8.5",
|
||||
"vimeo/psalm": "^3.5"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Brick\\Math\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"description": "Arbitrary-precision arithmetic library",
|
||||
"keywords": [
|
||||
"Arbitrary-precision",
|
||||
"BigInteger",
|
||||
"BigRational",
|
||||
"arithmetic",
|
||||
"bigdecimal",
|
||||
"bignum",
|
||||
"brick",
|
||||
"math"
|
||||
],
|
||||
"time": "2020-04-15T15:59:35+00:00"
|
||||
},
|
||||
{
|
||||
"name": "doctrine/annotations",
|
||||
"version": "1.10.2",
|
||||
|
|
|
@ -11,6 +11,8 @@ doctrine:
|
|||
class: App\Helpers\UTCDateTimeType
|
||||
date:
|
||||
class: App\Helpers\UTCDateTimeType
|
||||
big_decimal:
|
||||
class: App\Helpers\BigDecimalType
|
||||
|
||||
schema_filter: ~^(?!internal)~
|
||||
profiling_collect_backtrace: true
|
||||
|
|
|
@ -54,7 +54,10 @@ use App\Entity\Attachments\SupplierAttachment;
|
|||
use App\Entity\Base\AbstractCompany;
|
||||
use App\Entity\Parameters\SupplierParameter;
|
||||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Validator\Constraints\BigDecimal\BigDecimalPositiveOrZero;
|
||||
use App\Validator\Constraints\Selectable;
|
||||
use Brick\Math\BigDecimal;
|
||||
use Brick\Math\BigNumber;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
@ -94,9 +97,9 @@ class Supplier extends AbstractCompany
|
|||
protected $default_currency;
|
||||
|
||||
/**
|
||||
* @var string|null the shipping costs that have to be paid, when ordering via this supplier
|
||||
* @ORM\Column(name="shipping_costs", nullable=true, type="decimal", precision=11, scale=5)
|
||||
* @Assert\PositiveOrZero()
|
||||
* @var BigDecimal|null the shipping costs that have to be paid, when ordering via this supplier
|
||||
* @ORM\Column(name="shipping_costs", nullable=true, type="big_decimal", precision=11, scale=5)
|
||||
* @BigDecimalPositiveOrZero()
|
||||
*/
|
||||
protected $shipping_costs;
|
||||
|
||||
|
@ -140,9 +143,9 @@ class Supplier extends AbstractCompany
|
|||
/**
|
||||
* Gets the shipping costs for an order with this supplier, given in base currency.
|
||||
*
|
||||
* @return string|null A bcmath string with the shipping costs
|
||||
* @return BigDecimal|null A BigDecimal with the shipping costs
|
||||
*/
|
||||
public function getShippingCosts(): ?string
|
||||
public function getShippingCosts(): ?BigDecimal
|
||||
{
|
||||
return $this->shipping_costs;
|
||||
}
|
||||
|
@ -150,17 +153,13 @@ class Supplier extends AbstractCompany
|
|||
/**
|
||||
* Sets the shipping costs for an order with this supplier.
|
||||
*
|
||||
* @param string|null $shipping_costs a bcmath string with the shipping costs
|
||||
* @param string|null $shipping_costs a BigDecimal with the shipping costs
|
||||
*
|
||||
* @return Supplier
|
||||
*/
|
||||
public function setShippingCosts(?string $shipping_costs): self
|
||||
public function setShippingCosts(?BigDecimal $shipping_costs): self
|
||||
{
|
||||
/* Just a little hack to ensure that price has 5 digits after decimal point,
|
||||
so that DB does not detect changes, when something like 0.4 is passed
|
||||
Third parameter must have the scale value of decimal column. */
|
||||
$this->shipping_costs = bcmul($shipping_costs, '1.0', 5);
|
||||
|
||||
$this->shipping_costs = $shipping_costs;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,7 @@ namespace App\Form\AdminPages;
|
|||
|
||||
use App\Entity\Base\AbstractNamedDBElement;
|
||||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Form\Type\BigDecimalMoneyType;
|
||||
use App\Form\Type\StructuralEntityType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
@ -73,7 +74,7 @@ class SupplierForm extends CompanyForm
|
|||
'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'move', $entity),
|
||||
]);
|
||||
|
||||
$builder->add('shipping_costs', MoneyType::class, [
|
||||
$builder->add('shipping_costs', BigDecimalMoneyType::class, [
|
||||
'required' => false,
|
||||
'currency' => $this->default_currency,
|
||||
'scale' => 3,
|
||||
|
|
65
src/Form/Type/BigDecimalMoneyType.php
Normal file
65
src/Form/Type/BigDecimalMoneyType.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 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 Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Form\Type;
|
||||
|
||||
|
||||
use Brick\Math\BigDecimal;
|
||||
use Brick\Math\BigNumber;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class BigDecimalMoneyType extends AbstractType implements DataTransformerInterface
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->addModelTransformer($this);
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return MoneyType::class;
|
||||
}
|
||||
|
||||
public function transform($value)
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($value instanceof BigDecimal) {
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return BigDecimal::of($value);
|
||||
}
|
||||
}
|
74
src/Helpers/BigDecimalType.php
Normal file
74
src/Helpers/BigDecimalType.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 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 Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
|
||||
use Brick\Math\BigDecimal;
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
|
||||
class BigDecimalType extends Type
|
||||
{
|
||||
public const BIG_DECIMAL = 'big_decimal';
|
||||
|
||||
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
|
||||
{
|
||||
return $platform->getDecimalTypeDeclarationSQL($fieldDeclaration);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @param AbstractPlatform $platform
|
||||
* @return BigDecimal|\Brick\Math\BigNumber|mixed
|
||||
*/
|
||||
public function convertToPHPValue($value, AbstractPlatform $platform)
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return BigDecimal::of($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BigDecimal $value
|
||||
* @param AbstractPlatform $platform
|
||||
* @return mixed
|
||||
*/
|
||||
public function convertToDatabaseValue($value, AbstractPlatform $platform)
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return self::BIG_DECIMAL;
|
||||
}
|
||||
|
||||
public function requiresSQLCommentHint(AbstractPlatform $platform)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 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 Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Validator\Constraints\BigDecimal;
|
||||
|
||||
|
||||
use Brick\Math\BigDecimal;
|
||||
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;
|
||||
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
|
||||
|
||||
/**
|
||||
* Validates values are greater than or equal to the previous (>=).
|
||||
*
|
||||
* @author Daniel Holmes <daniel@danielholmes.org>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class BigDecimalGreaterThenOrEqualValidator extends AbstractComparisonValidator
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function compareValues($value1, $value2)
|
||||
{
|
||||
if ($value1 instanceof BigDecimal) {
|
||||
$value1 = (string) $value1;
|
||||
}
|
||||
|
||||
if ($value2 instanceof BigDecimal) {
|
||||
$value2 = (string) $value2;
|
||||
}
|
||||
|
||||
return null === $value2 || $value1 >= $value2;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getErrorCode()
|
||||
{
|
||||
return GreaterThanOrEqual::TOO_LOW_ERROR;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 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 Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Validator\Constraints\BigDecimal;
|
||||
|
||||
|
||||
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
|
||||
use Symfony\Component\Validator\Constraints\NumberConstraintTrait;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
|
||||
*
|
||||
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
|
||||
*/
|
||||
class BigDecimalPositiveOrZero extends GreaterThanOrEqual
|
||||
{
|
||||
use NumberConstraintTrait;
|
||||
|
||||
public $message = 'This value should be either positive or zero.';
|
||||
|
||||
public function __construct($options = null)
|
||||
{
|
||||
parent::__construct($this->configureNumberConstraintOptions($options));
|
||||
}
|
||||
|
||||
public function validatedBy(): string
|
||||
{
|
||||
return BigDecimalGreaterThenOrEqualValidator::class;
|
||||
}
|
||||
}
|
|
@ -11,6 +11,9 @@
|
|||
"beberlei/doctrineextensions": {
|
||||
"version": "v1.2.6"
|
||||
},
|
||||
"brick/math": {
|
||||
"version": "0.8.15"
|
||||
},
|
||||
"composer/semver": {
|
||||
"version": "1.5.0"
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue