Calculate mail freight for various products

Asked

Viewed 3,943 times

18

I am trying to make the freight calculation of various products using the Webservice of the post office, but I am having doubts/difficulty to perform the calculation when I have several products.

I am using the PHP code to call the webservice and receive the return in XML. This call is correct. I can call and receive values correctly. My question is more in the logic of how to calculate and process when I own a shopping cart with more than one product.

I know that I must pay attention to the calculation based on the dimensions (height, width, length and weight), I also know that in this case the calculation is based on cubic weight, as documented.

However, the parameters that I must fill in to perform the calculation do not provide a field to send more than one product. In this case, how should the calculation be made?

Do I sum the dimensions in a "raw" way? Ex.: 2 products of dimensions:

  • A: 10cm, L: 15cm, C: 22cm
  • A: 8cm, L: 31cm, C: 18cm
  • Total: A: 18cm, L: 46cm, C: 40cm

And I send these sums?

When I do this I can even get the result, but it doesn’t seem right, being that sometimes the difference in freight is very big. Ex.:

  • Shipping product A: R$18,00
  • Shipping product B: R$21,00
  • Freight sum A+B: R$88,00

Does anyone have experience with this kind of service that can help me?


In order to facilitate understanding, the data concerning the product(s) received by the postal service website are::

  • nVlPeso: ex: 4 (for 4kg);
  • nVlCompriment: ex: 10 (for 10cm);
  • nVlAltura: ex: 15 (for 15cm);
  • nVlLargura: ex: 20 (for 20cm);
  • nVlDiametro: *non-compulsory

There is, for example, an option for more than one item, only this data.

  • The best is to try to assemble in 3D (or 2D for didactic purposes) as the product would look together. You will see that adding up in the three dimensions is having too much empty space

  • Is using mail packaging or own?

  • @Randrade At first using only own packaging. Some products already have factory packaging.

  • What happens when the product already has its own packaging? You would group it into a single package or send it in separate packages?

  • @Randrade is difficult to answer, because it depends a little. Some cases can only be "tied up", or, for example, wrapped with brown paper. Some, from what I’ve seen, don’t go by the post office rules, so it would have to be separate packages. Already others are small and you can put several in one box.

  • @Randrade to put in context, are petshop products. From medicines, toys, feed bags (1,3,5,10kg), etc..

Show 1 more comment

4 answers

21

About the packaging

The main difficulty point I saw in your question was when sending the package dimensions. I will do in 2D to simplify.

Imagine you want to send two Sugar Union:

açúcar união

It has dimension two fingers by five fingers, for 5g of content:

dois dedos cinco dedos

Add up the dimensions the way you were doing:

pondo de maneira equivocada

Or so:

pondo de maneira equivocada 2

In the first misguided way, of the 49 square fingers (7 fingers per 7 fingers) of the package, only 20 square fingers in fact are being used. Note the square with nothing larger than 25 square fingers, and the smallest of 4 square fingers.

In the second packaging, the total area is 40 square fingers (4 fingers per 10 fingers). Note that for each pack of 10 square fingers, it has an empty area right next to the same size.

The best packaging for you would be:

4 dedos por 5 dedos

Or this one:

2 dedos por 10 dedos

I don’t know if the square post office takes into account the area only, or if it takes into account the individual dimensions. I would ask the webservice of the square post office both options if I could send these requisitions.

Now, imagine you wish to send a Sugar Union is a Sal Sosal:

sosal

Sal measures 2 fingers by 3 fingers for 1g (didactic and approximate measurements, I will not post the photo here).

So you can pack these four ways sugar and salt:

4x5 5x5 8x2 7x3
4 por 5 5 por 5 8 por 2 7 por 3

Note that 5x5 is not the best choice, it is 100% dominated by 4x5 in all dimensions considered. Now, 8x2 has total area of 16 square fingers, being an option of smaller total area than 4x5.

Later I put an algorithm on how to do this packaging in 2D, but I think for 3D or higher dimensions is very different

  • It is. An alternative would be a more complex calculation (I presume). Identify the larger product, e.g.: Height, and add only the other values. It would not be ideal, but it would be better than this method of "packaging" that I have. The idea of your method I can understand, but I don’t know how to apply it so that the calculation is done correctly. I even updated the question to show the data that Webservice receives.

  • Mais tarde eu coloco um algoritmo de como fazer esse empacotamento em 2D Good luck, because this problem is full NP.

  • 2

    Since it is a search algorithm (more specifically search for enumeration, not search for counting), not decision algorithm, it does not fit exactly in the classification in P/NP or another conventional complexity class (such as RE or PSPACE or AM). Nor did I promise efficiency. And the strategy I’m using is more similar to EXPTIME than NP.

7


After some time reading much online material and researching thoroughly on the subject, I think I managed to arrive at a satisfactory result.

As already discussed here (and also a question concerning the goal) I know that the packaging 2D or 3D will be something extremely difficult (or impossible), but, as stated in the question, my biggest doubt was given to the fact of how to calculate the freight for one or more products so that it is not made one of the following proposals:

  • Sum of Frete A + Frete B;
  • Sum of the measures of Produto A + Produto B and calculation of freight;

Another problem found was how to add the information of various products and the webservice Post accepts only one value for each of the fields.

In this way I managed to arrive at a calculation where, until now, I believe I am returning values much closer to the actual values of freight than with the method previously used.

Note: I did not get to test officially by performing the calculation by code and checking at the post office counter at the time of sending the product.


The logic behind the calculation is as follows:

  • Calculate the value in cm³ for each product (Width x Height x Length x Quantity);
  • Add up the value of cm³ and weight of all products;
  • Extraction of the cubic root from the sum of cm³ of all products;

For these calculations, I’m using this code:

$total_peso = 0;
$total_cm_cubico = 0;

/**
 * $produto = lista de produtos no carrinho de compras. Deve possuir,
 * obrigatoriamente, os campos largura, altura, comprimento e quantidade.
 */
foreach ($produto as $row) {
    $row_peso = $row['peso'] * $row['quantidade'];
    $row_cm = ($row['altura'] * $row['largura'] * $row['comprimento']) * $row['quantidade'];

    $total_peso += $row_peso;
    $total_cm_cubico += $row_cm;
}

$raiz_cubica = round(pow($total_cm_cubico, 1/3), 2);
$resposta = array(
    'total_peso' => $total_peso,
    'raiz_cubica' => $raiz_cubica,
);

So, when filling in the data to be sent to the Post Office, I fill in the following way:

// Os valores 16, 2, 11 e 0.3 são os valores mínimos determinados pelo serviço dos Correios
$comprimento =  $raiz_cubica < 16 ? 16 : $raiz_cubica;
$altura = $raiz_cubica < 2 ? 2 : $raiz_cubica;
$largura = $raiz_cubica < 11 ? 11 : $raiz_cubica;
$peso = $total_peso < 0.3 ? 0.3 : $total_peso;
$diametro = hypot($comprimento, $largura); // Calculando a hipotenusa pois minhas encomendas são retangulares

'nVlPeso'        => $peso,
'nVlComprimento' => $comprimento,
'nVlAltura'      => $altura,
'nVlLargura'     => $largura,
'nVlDiametro'    => $diametro, // não obrigatório

With this calculation I was able to achieve results where calculating the individual freight value of 2 products I obtained the values:

  • Product A: R$17,10
  • Product B: R$20,40

And when running the two products together I got R $ 23,60 (considering that the measures and type of package did not get less than R $ 17,10 - should be minimum rate).

This type of result was repeated (similarly) in several different products and different quantities, that is, there was no sum of freight but a gradual increase according to the measures and weight of the products.

Recalling that there are some pending functionalities, such as the validation of the measures so that it does not exceed the maximum values and, if it exceeds, divide the freight in more than one "box".

  • sensational! I had arrived at the formula of the cubic root but had not thought of the hypotenuse of this value, strange the api of them consider the diameter if the request considers that the packaging is a box. Regardless of this, it worked in a first test in which the product was 50x25x98cm and 10,740kg. I will add a log to our system to save the requests with this proposed formula and compare the results (A/B test) to see how they look. Today we actually only use the sum of weights and it works for most cases

0

Good evening, resurrecting deceased, rsrsrsrs but I will post my contribution q may be to help someone in the future.

I’m developing a system for a natural products company, they have teas, encapsulated soluble products. well varied. I was able to solve this difficulty by converting the measurement of each product to cm³(cubic centimeter Height x Width x Length) Then we are the measure of each product. At the end I take the square root of the result and step as height, width and length. Hit-and-run

Example: Product with C15 A13 L8 then this product has 1560cm³ If send 3 units will 4680m³ another product with C20 15 l13 it will have 1300cm³ sending 2 units will 2600m³

Adding the two subtotals to 7280 the cubic root of it is 19.38, as the box is always bigger than the volume of products, I added 10% to this value that gives the value of 21.32 cm. Then I make the calculation about 21,32 of height, 21,32 of width and 21,32 of length.

Obs put an IF for each measure to meet the minimum standard of the post office. For example if the result is less than 2, the height variable will receive the value of 2 which is the minimum height required by mail.

I hope it helps if someone.

0

You need to add up only one dimension, for example height, and take the higher value of the other two.

A: 10cm, L: 15cm, C: 22cm

A: 8cm, L: 31cm, C: 18cm

Total: A: 18cm, L: 31cm, C: 22cm

Suppose you pack one box on top of the other. The height will be added up, but the maximum width and maximum length remain the same. Adding everything up, as in the initial reasoning, the volume of the products rises to the cube. Instead of calculating the 2 packages, it would be calculating the volume of 8 packages.

  • This does not answer the question. When you have reputation enough, you’ll be able to leave comments on any post but until then, write only answer that no depend on more information of who asked. - Of Revision

  • I’m sorry, but answer yes. It indicates the correct logic and simplifies the path of the solution presented so far which, in my opinion, makes unnecessary turns. I would also like you to clarify the reason why my answer depends on more information from those who asked, I do not understand. I only posted because it would be a useful reference for those who consult the issue in the future, but if it hurts any rules of the site, they can delete without problems.

  • I had already tested this way, unfortunately, the results were not close to what was expected. The difference in values when adding up freight is very high. I believe that one of the reasons for this is the fact that the calculation of freight, by the post office, takes into account the m³, as explained in a link in the question.

  • @user105586 , the logic for this is at best incomplete. This applies to two products, but for 3 or more products the situation is distinct, because the concave solid shape coming from the packaging of 2 cobblestones allows more solids to fit in the middle. There is also the problem of stacking (since some products cannot be stacked because they are fragile)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.