I have the following situation, and I reflect the image to locate them, in addition to the code.
In the following list of packages, the last column reflects the price of each package that a certain client wants to send in a container. The price of each package is rounded to 2 decimal places and is the volume of the package rounded to 2 decimal places by the price established for the cubic meter, but the total amount (framed in red at the end of the column) is reflected to the customer as a integer value, so that if the product of the volume occupied by the customer times the price per cubic meter gives a value with a decimal, it is rounded up to the next higher integer, regardless of whether the rounding is due to excess or deficiency; to do this the option is applied "ceil"
to the round
view function twig
. This is the code that generates the table with footer
where the total amount is:
<tbody>
{% set volumenTotal = 0 %}
{% set pesoVolTotal = 0 %}
{% set peso = 0 %}
{% set importeTotal = 0 %}
{% for bulto in bultos %}
<tr>
<td class="text-center">{{bulto.numero}}</td>
<td class="text-justify">{{bulto.descripcion}}</td>
<td class="text-center">{{bulto.dimensionLargo}}</td>
<td class="text-center">{{bulto.dimensionAncho}}</td>
<td class="text-center">{{bulto.dimensionAlto}}</td>
<td class="text-center">{{bulto.volumen|number_format('6','.',',')}} ({{bulto.volumen|number_format('2','.',',')}})</td>
<td class="text-center">{{bulto.pesoVolumetrico}}</td>
<td class="text-center">{{bulto.peso}}</td>
{% set precioBulto = bulto.precioACostometrocubicoActual %}
<td class="text-right">{{precioBulto|number_format('2','.',',')}}</td>
</tr>
{% set volumenTotal = volumenTotal + (bulto.volumen|round(2)) %}
{% set pesoVolTotal = pesoVolTotal+bulto.pesoVolumetrico %}
{% set peso = peso + bulto.peso %}
{% set importeTotal = importeTotal + precioBulto %}
{% endfor %}
</tbody>
<tfoot>
{% if bultos is empty %}
<tr><td colspan="9"></td></tr>
{% endif %}
<tr class="bg-primary">
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<th class="text-center text-bold">{{volumenTotal|number_format('2','.',',')}}</th>
<th class="text-center text-bold">{{pesoVolTotal|number_format('2','.',',')}}</th>
<th class="text-center text-bold">{{peso|number_format('2','.',',')}}</th>
<th class="text-right text-bold">{{(volumenTotal*colaborador.menaje.precioMetroCubico)|round(0,'ceil')|number_format(2,'.',',')}}</th>
</tr>
</tfoot>
The way the amount is calculated in the view, as the parameter is being passed ceil
to the function round
, Twig
if the result of the total amount gives an integer value, the round function is adding 1 more integer. Let's say that the amount is 450.00, then it rises to 451.00 (incorrect case), but if the amount were 449.10 or 449.75, which is the case in the image, then the amount would be 450.00 (correct case).
How do I approach this calculation so that it gives the appropriate value. At the end of the table there are two notes to the user, which express how the final amount is calculated, which in short is what I want to achieve.
The solution found is the following, by implementing a filter
Twig
.The 'importe_formatter' filter checks if the value of the amount is an integer, in that case it only formats to 2 decimal places; otherwise, round to the next higher integer and format to 2 decimal places.
and in the Twig view, in the footer of the table it is used in the last column: