News:

Support the VirtueMart project and become a member

Main Menu

convertDimensionUnit not correct in shopfunctions.php

Started by averkroost, January 14, 2013, 19:52:00 PM

Previous topic - Next topic

averkroost

The conversion from dimension unit in function convertDimensionUnit is not correct.


static function convertDimensionUnit ($value, $from, $to) {

$from = strtoupper($from);
$to = strtoupper($to);
$value = (float)str_replace (',', '.', $value);
if ($from === $to) {
return $value;
}
$meter = 1 * $value;

// transform $value in meters
switch ($from) {
case 'CM':
$meter = (float)(0.01 * $value);
break;
case 'MM':
$meter = (float)(0.001 * $value);
break;
case 'YD':
$meter = (float)(1.0936 * $value);
break;
case 'FT':
$meter = (float)(3.28083 * $value);
break;
case 'IN':
$meter =(float) (39.37 * $value);
break;
}
switch ($to) {
case 'CM' :
$value = (float)($meter * 0.01);
break;
case 'MM' :
$value = (float)($meter * 0.001);
break;
case 'YD' :
$value =(float) ($meter * 0.9144);
break;
case 'FT' :
$value = (float)($meter * 0.3048);
break;
case 'IN' :
$value = (float)($meter * 0.0254);
break;
}
return $value;
}


The fist step converts the old metric value to meter. In de next step de value in meter must been converted to the new format. But de conversion wont work right.
If you started with 10 CM en you want MM you get 0,0001 MM while the correct output had to be 100 MM

If the code is changed to:


/**
* Convert Metric Unit
*
* @author Florian Voutzinos
*/
static function convertDimensionUnit ($value, $from, $to) {

$from = strtoupper($from);
$to = strtoupper($to);
$value = (float)str_replace (',', '.', $value);
if ($from === $to) {
return $value;
}
$meter = 1 * $value;

// transform $value in meters
switch ($from) {
case 'CM':
$meter = (float)(0.01 * $value);
break;
case 'MM':
$meter = (float)(0.001 * $value);
break;
case 'YD' :
$meter =(float) (0.9144 * $value);
break;
case 'FT' :
$meter = (float)(0.3048 * $value);
break;
case 'IN' :
$meter = (float)(0.0254 * $value);
break;
}
switch ($to) {
case 'CM':
$value = (float)(($meter / 0.01);
break;
case 'MM':
$value = (float)(($meter / 0.001);
break;
case 'YD' :
$value =(float) (($meter / 0.9144);
break;
case 'FT' :
$value = (float)(($meter / 0.3048);
break;
case 'IN' :
$value = (float)(($meter / 0.0254);
                break;
}
return $value;
}


After de change de function works correct.

I'll hope it can be fixed in the next release update. I'll have the problem in VM 2.0.16 and 2.0.18a

Milbo

Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Milbo

Checked and understood.

Thanks. also noticed that we do not use it. Would be interesting to know what you are working on.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/