Error 500 when loading part: RoundingNecessaryException
Describe the bug I edited a existing part and added a second vendor. If I now open the part again I get an error 500. The log file shows the following:
[2021-06-03T11:23:17.602415+02:00] request.CRITICAL: Uncaught PHP Exception Twig\Error\RuntimeError: "An exception has been thrown during the rendering of a template ("Rounding is necessary to represent the result of the operation at this scale.")." at /var/www/partdb/templates/Parts/info/_main_infos.html.twig line 51 {"exception":"[object] (Twig\\Error\\RuntimeError(code: 0): An exception has been thrown during the rendering of a template (\"Rounding is necessary to represent the result of the operation at this scale.\"). at /var/www/partdb/templates/Parts/info/_main_infos.html.twig:51)\n[previous exception] [object] (Brick\\Math\\Exception\\RoundingNecessaryException(code: 0): Rounding is necessary to represent the result of the operation at this scale. at /var/www/partdb/vendor/brick/math/src/Exception/RoundingNecessaryException.php:19)"} []
This was the content of the DB when the error occoured:
MariaDB [partdb]> select * from orderdetails o, pricedetails p where part_id = 169 and p.orderdetails_id = o.id ;
+-----+---------+-------------+----------------+----------+--------------------------------------------------------------------------------------------------+---------------------+---------------------+-----+-----------------+-------------+---------+------------------------+-----------------------+--------------+---------------------+---------------------+
| id | part_id | id_supplier | supplierpartnr | obsolete | supplier_product_url | last_modified | datetime_added | id | orderdetails_id | id_currency | price | price_related_quantity | min_discount_quantity | manual_input | last_modified | datetime_added |
+-----+---------+-------------+----------------+----------+--------------------------------------------------------------------------------------------------+---------------------+---------------------+-----+-----------------+-------------+---------+------------------------+-----------------------+--------------+---------------------+---------------------+
| 126 | 169 | 1 | HEBL 25 | 0 | | 2020-06-12 14:46:01 | 2020-06-12 14:46:01 | 230 | 126 | 1 | 0.55000 | 1 | 1 | 1 | 2020-06-12 14:46:01 | 2020-06-12 14:46:01 |
| 325 | 169 | 5 | | 0 | | 2021-06-03 09:22:10 | 2021-06-03 09:21:39 | 433 | 325 | NULL | 6.58000 | 15 | 5 | 1 | 2021-06-03 09:22:10 | 2021-06-03 09:22:10 |
+-----+---------+-------------+----------------+----------+--------------------------------------------------------------------------------------------------+---------------------+---------------------+-----+-----------------+-------------+---------+------------------------+-----------------------+--------------+---------------------+---------------------+
I manually updated the second price from 6,58 to 6,00 to get rid of the error message.
I think the error occours here: https://github.com/Part-DB/Part-DB-symfony/blob/7bdf4536a90ef3eda6c748a38d64062703bbf8c8/src/Services/PricedetailHelper.php#L191 It should be similar to this: https://github.com/Part-DB/Part-DB-symfony/blob/7bdf4536a90ef3eda6c748a38d64062703bbf8c8/src/Services/PricedetailHelper.php#L234
But it seems the exception is thrown in the dividedBy function... the the following line should be enough:
return $avg->dividedBy($count, Pricedetail::PRICE_PRECISION, RoundingMode::HALF_UP);
Should be fixed now. Thanks for the suggestion.