I don’t know if we will have the same term in mind, but here in our country we call it Product Unit of Measure or UOM. For example, we have a “candy” product. Possible unit of measures could be: pieces, packs and boxes.
The purpose of this post is to solve the problem of converting product quantity or inventory values in different unit of measures. I think SAP has this functionality. Just in case you want to have the same in your current program, keep on reading!
The following questions might give you some more ideas about this post:
How many box are 132 pieces?
How many box are 309 packs?
How much of a box are 33 pieces?
How much of a box are 5 packs?
…and many other combinations. So let’s get started!
For example we have a product with 3 unit of measures.
Product code: COF_A1
UOMs: box, pack, pcs
In this post, when I say “upload”, it generally refers to saving the value in the system database. Anyways, when working with unit of measure conversion, the user usually deals with three objects:
1. Product (above)
2. Unit of Measures (UOM)
3. UOM Conversion Values
Tell the user to upload their UOMs from biggest to smallest. It will look like this in the list or CSV:
COF_A1,box
COF_A1,pack
COF_A1,pcs
When uploading UOM conversion, the bigger UOM/s are on the left side and the smallest UOM is always on the right side. For example:
1 box = 24 pcs
1 pack = 12 pcs
Now apply it in the following input based on the above UOM conversion.
8 pcs = ? box:
8 pcs = 8 pcs (since pcs is already the smallest UOM, no need to multiply to any value)
8 / [24] = [0.34 box]
8 pcs = 0.34 box
15 pcs = ? pack:
15 pcs = 15 pcs (since pcs is already the smallest UOM)
15 / [12] = [1.25 pack]
15 pcs = 1.25 pack:
9 pack = ? pcs:
9 pack = 9 * [12] = [108 pcs]
9 pack = 108 pcs
11 pack = ? box:
11 pack = 11 * [12] = [132 pcs]
132 / [24] = [5.5 box]
11 pack = 5.5 box
3 box = ? pcs:
11 box = 11 * [24] = [264 pcs]
11 box = 264 pcs
7 box = ? pack:
7 box = 7 * [24] = [168 pcs]
168 / [12] = [14 pack]
7 box = 14 pack
2 box and 8 pack = ? pcs
2 box = 2 * [24] = [48] pcs
8 pack = 8 * [12] = [96] pcs
48 pcs + 96 pcs = [144 pcs]
2 box and 8 pack = 144 pcs
4 box and 18 pcs = ? pack
4 box = 4 * [24] = [96] pcs
18 pcs = [18] pcs (as is since pcs is the smallest UOM)
96 + 18 = [114 pcs]
114 pcs / [12] = [9.5 pack] (yes, we divide this time!)
4 box and 18 pcs = 9.5 pack
3 pack and 13 pcs = ? box
3 pack = 3 * [12] = 36 pcs
13 pcs = [13] pcs (as is since pcs is the smallest UOM)
36 pcs + 13 pcs = 49 pcs
49 pcs / [24] = [2.04 box]
3 pack and 13 pcs = 2.04 box
Here are some noticeable rules during the process:
1. When converting to smallest UOM, use multiplication.
2. Smallest UOM does not need to be converted.
3. When converting from smallest UOM to bigger UOM, use division.
Of course, all the examples above can be automated. The first is step is making a pseudo code.
Problem: 3 pack and 13 pcs is how many box?
// initialize converted value converted_value = 0; // set the product code product_code = 'prod-1' // set the target uom target_uom = 'box'; // prepare objects (should be in loop and retrieved from database) obj_1 = new obj(); obj_1.val = 3; obj_1.uom = 'pack'; obj_1 = new obj(); obj_1.val = 3; obj_1.uom = 'pack'; // put in array (if you're using PHP, you can use array_push while looping values from database) input_obj_arr = { obj_1, obj_2 } // variable for accumulated smallest uom accumulated_smallest_uom_val = 0; // loop through the array foreach(input_obj_arr as input){ // get the conversion value uom_conversion = getConversionValue(product_code, input.uom); // convert to smallest uom if(uom_conversion==null){ // if uom_conversion is null, it means it is already the smallest uom // uom_conversion will be null since getConversionValue() method DO NOT get the smallest uom conversion value } else{ // use multiplication to convert to smallest uom smallest_uom_val = input.val * uom_conversion.va; // accumulate smallest_uom accumulated_smallest_uom_val += smallest_uom_val; } } // now convert to the target uom // getConversionValue() method will return null if it tries to get the smallest uom target_uom_conversion = getConversionValue(product_code, target_uom); // if target_uom_conversion is null, the accumulated_smallest_uom_val is as is the converted_value if(target_uom_conversion==null){ converted_value = accumulated_smallest_uom_val; } // compute the converted value by division else{ converted_value = accumulated_smallest_uom_val / target_uom_conversion.val; }
3 pack and 13 pcs is 2.04 box (See example 5.9 above)
If you have any other way of solving this unit of measure conversion problem, please tell us in the comment section below. I am willing to change my current solution if yours is better.
Thanks for reading this post about unit of measure conversion pseudocode. If it helps you, please like or share it. Or if you have a friend or colleague that needs this type of solution, please share this to them, you’ll be happy you did. It’s always nice to help others.