VirtueMart Forum

VirtueMart 2 + 3 + 4 => Plugins: Payment, Shipment and others => Topic started by: averkroost on January 14, 2013, 19:52:00 PM

Title: convertDimensionUnit not correct in shopfunctions.php
Post by: averkroost on January 14, 2013, 19:52:00 PM
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
Title: Re: convertDimensionUnit not correct in shopfunctions.php
Post by: Milbo on January 14, 2013, 23:41:44 PM
So you use the Multiplicative inverse ?
Title: Re: convertDimensionUnit not correct in shopfunctions.php
Post by: Milbo on January 31, 2013, 16:22:30 PM
Checked and understood.

Thanks. also noticed that we do not use it. Would be interesting to know what you are working on.