From a1ac285a25c9dda1c5caffe4b4ba45bc84e43eb4 Mon Sep 17 00:00:00 2001 From: Paul Dermody Date: Mon, 1 Jan 2018 15:03:08 -0600 Subject: [PATCH] Fixed automated computation of surface and volume to use correct units. It was failing for inches and feet. --- htdocs/core/lib/product.lib.php | 39 ++++++++++++++++++++++++++ htdocs/product/class/product.class.php | 4 +-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/htdocs/core/lib/product.lib.php b/htdocs/core/lib/product.lib.php index b01637d7b08..dd756830f41 100644 --- a/htdocs/core/lib/product.lib.php +++ b/htdocs/core/lib/product.lib.php @@ -477,3 +477,42 @@ function measuring_units_string($unit,$measuring_style='') return $measuring_units[$unit]; } + +/** + * Transform a given unit into the square of that unit, if known + * + * @param int $unit Unit key (-3,-2,-1,0,98,99...) + * @return int Squared unit key (-6,-4,-2,0,98,99...) + * @see formproduct->load_measuring_units + */ +function measuring_units_squared($unit) +{ + $measuring_units=array(); + $measuring_units[0] = 0; // m -> m3 + $measuring_units[-1] = -2; // dm-> dm2 + $measuring_units[-2] = -4; // cm -> cm2 + $measuring_units[-3] = -6; // mm -> mm2 + $measuring_units[98] = 98; // foot -> foot2 + $measuring_units[99] = 99; // inch -> inch2 + return $measuring_units[$unit]; +} + + +/** + * Transform a given unit into the cube of that unit, if known + * + * @param int $unit Unit key (-3,-2,-1,0,98,99...) + * @return int Cubed unit key (-9,-6,-3,0,88,89...) + * @see formproduct->load_measuring_units + */ +function measuring_units_cubed($unit) +{ + $measuring_units=array(); + $measuring_units[0] = 0; // m -> m2 + $measuring_units[-1] = -3; // dm-> dm3 + $measuring_units[-2] = -6; // cm -> cm3 + $measuring_units[-3] = -9; // mm -> mm3 + $measuring_units[98] = 88; // foot -> foot3 + $measuring_units[99] = 89; // inch -> inch3 + return $measuring_units[$unit]; +} diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index ed5813c2998..0b40f815816 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -745,12 +745,12 @@ class Product extends CommonObject if (empty($this->surface) && !empty($this->length) && !empty($this->width) && $this->length_units == $this->width_units) { $this->surface = $this->length * $this->width; - $this->surface_units = $this->length_units + $this->width_units; + $this->surface_units = measuring_units_squared($this->length_units); } if (empty($this->volume) && !empty($this->surface_units) && !empty($this->height) && $this->length_units == $this->height_units) { $this->volume = $this->surface * $this->height; - $this->volume_units = $this->surface_units + $this->height_units; + $this->volume_units = measuring_units_cubed($this->height_units); } $this->surface = price2num($this->surface);