2
0
forked from Wavyzz/dolibarr

FIX #11861 Not consistent code to manage measuring units

This commit is contained in:
Laurent Destailleur
2019-09-27 19:45:33 +02:00
parent 18055bc425
commit d0d884742f
7 changed files with 67 additions and 45 deletions

View File

@@ -235,6 +235,7 @@ class CUnits // extends CommonObject
$sql.= " t.label,";
$sql.= " t.short_label,";
$sql.= " t.unit_type,";
$sql.= " t.scale,";
$sql.= " t.active";
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'c_units as t';
// Manage filter
@@ -279,6 +280,7 @@ class CUnits // extends CommonObject
$record->label = $obj->label;
$record->short_label = $obj->short_label;
$record->unit_type = $obj->unit_type;
$record->scale = $obj->scale;
$record->active = $obj->active;
$this->records[$record->id] = $record;
}

View File

@@ -4522,7 +4522,7 @@ function price2num($amount, $rounding = '', $alreadysqlnb = 0)
* Output a dimension with best unit
*
* @param float $dimension Dimension
* @param int $unit Unit of dimension (Example: 0=kg, -3=g, 98=ounce, 99=pound, ...)
* @param int $unit Unit scale of dimension (Example: 0=kg, -3=g, -6=mg, 98=ounce, 99=pound, ...)
* @param string $type 'weight', 'volume', ...
* @param Translate $outputlangs Translate language object
* @param int $round -1 = non rounding, x = number of decimal
@@ -4566,7 +4566,7 @@ function showDimensionInBestUnit($dimension, $unit, $type, $outputlangs, $round
$unit = $forceunitoutput;
}*/
$ret=price($dimension, 0, $outputlangs, 0, 0, $round).' '.measuring_units_string($unit, $type);
$ret=price($dimension, 0, $outputlangs, 0, 0, $round).' '.measuring_units_string(0, $type, $unit);
return $ret;
}

View File

@@ -485,25 +485,41 @@ function show_stats_for_company($product, $socid)
*
* @param int $unit ID of unit (rowid in llx_c_units table)
* @param string $measuring_style Style of unit: weight, volume,...
* @param string $scale Scale of unit: '0', '-3', '6', ...
* @param int $use_short_label 1=Use short label ('g' instead of 'gram'). Short labels are not translated.
* @return string Unit string
* @see formproduct->selectMeasuringUnits
*/
function measuring_units_string($unit, $measuring_style = '')
function measuring_units_string($unit, $measuring_style = '', $scale = '', $use_short_label = 0)
{
global $langs, $db;
require_once DOL_DOCUMENT_ROOT.'/core/class/cunits.class.php';
$measuringUnits= new CUnits($db);
$result = $measuringUnits->fetchAll('', '', 0, 0, array(
if ($scale !== '')
{
$arrayforfilter = array(
't.scale' => $scale,
't.unit_type' => $measuring_style,
't.active' => 1
);
}
else
{
$arrayforfilter = array(
't.rowid' => $unit,
't.unit_type' => $measuring_style,
't.active' => 1
));
);
}
$result = $measuringUnits->fetchAll('', '', 0, 0, $arrayforfilter);
if ($result<0) {
return -1;
} else {
if (is_array($measuringUnits->records) && count($measuringUnits->records)>0) {
return $langs->transnoentitiesnoconv($measuringUnits->records[key($measuringUnits->records)]->label);
if ($use_short_label) return $measuringUnits->records[key($measuringUnits->records)]->short_label;
else return $langs->transnoentitiesnoconv($measuringUnits->records[key($measuringUnits->records)]->label);
} else {
return '';
}

View File

@@ -392,6 +392,7 @@ class modProduct extends DolibarrModules
));
}
/* Disabled: The value of fields xxx_units is the scale for reference unit, not the rowid in table llx_c_unit !!!
$this->import_convertvalue_array[$r] = array(
'p.weight_units' => array(
'rule' => 'fetchidfromcodeunits',
@@ -441,7 +442,7 @@ class modProduct extends DolibarrModules
'units' => 'volume',
'dict' => 'DictionaryMeasuringUnits'
)
);
);*/
if (! empty($conf->fournisseur->enabled) || !empty($conf->margin->enabled)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r], array('p.cost_price'=>'CostPrice'));
if (is_object($mysoc) && $mysoc->useNPR()) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r], array('p.recuperableonly'=>'NPR'));
@@ -501,17 +502,17 @@ class modProduct extends DolibarrModules
'p.accountancy_code_sell_export' => "",
'p.accountancy_code_buy' => "",
'p.weight' => "",
'p.weight_units' => 'use a unit of measure from the dictionary. g/Kg/T etc....matches field "Short Label" for unit type "weight" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.weight_units' => 'use a unit of measure from the dictionary. g/Kg/T etc....matches field "Scale" for unit type "weight" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.length' => "",
'p.length_units' => 'use a unit of measure from the dictionary. m/cm/mm etc....matches field "Short Label" for unit type "size" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.length_units' => 'use a unit of measure from the dictionary. m/cm/mm etc....matches field "Scale" for unit type "size" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.width' => "",
'p.width_units' => 'use a unit of measure from the dictionary. m/cm/mm etc....matches field "Short Label" for unit type "size" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.width_units' => 'use a unit of measure from the dictionary. m/cm/mm etc....matches field "Scale" for unit type "size" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.height' => "",
'p.height_units' => 'use a unit of measure from the dictionary. m/cm/mm etc....matches field "Short Label" for unit type "size" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.height_units' => 'use a unit of measure from the dictionary. m/cm/mm etc....matches field "Scale" for unit type "size" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.surface' => "",
'p.surface_units' => 'use a unit of measure from the dictionary. m2/cm2/mm2 etc....matches field "Short Label" for unit type "surface" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.surface_units' => 'use a unit of measure from the dictionary. m2/cm2/mm2 etc....matches field "Scale" for unit type "surface" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.volume' => "",
'p.volume_units' => 'use a unit of measure from the dictionary. m3/cm3/mm3 etc....matches field "Short Label" for unit type "volume" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.volume_units' => 'use a unit of measure from the dictionary. m3/cm3/mm3 etc....matches field "Scale" for unit type "volume" in table "' . MAIN_DB_PREFIX . 'c_units',
'p.finished' => '0 (raw material) / 1 (finished goods)'
);
//clauses copied from import_fields_array

View File

@@ -178,14 +178,14 @@ INSERT INTO llx_c_units (code, scale, label, short_label, unit_type, active) VAL
INSERT INTO llx_c_units (code, scale, label, short_label, unit_type, active) VALUES ('Y','31557600','year','y', 'time', 1);
UPDATE llx_c_units SET short_label = 'i' WHERE code = 'MI';
UPDATE llx_c_units SET unit_type = 'weight', short_label = 'kg' WHERE code = 'KG';
UPDATE llx_c_units SET unit_type = 'weight', short_label = 'g' WHERE code = 'G';
UPDATE llx_c_units SET unit_type = 'weight', short_label = 'kg', scale = 0 WHERE code = 'KG';
UPDATE llx_c_units SET unit_type = 'weight', short_label = 'g', scale = -3 WHERE code = 'G';
UPDATE llx_c_units SET unit_type = 'time' WHERE code IN ('S','H','D');
UPDATE llx_c_units SET unit_type = 'size' WHERE code IN ('M','LM');
UPDATE llx_c_units SET label = 'SizeUnitm' WHERE code IN ('M');
UPDATE llx_c_units SET active = 0 WHERE code IN ('LM');
UPDATE llx_c_units SET unit_type = 'surface' WHERE code IN ('M2');
UPDATE llx_c_units SET unit_type = 'volume' WHERE code IN ('M3','L');
UPDATE llx_c_units SET label = 'SizeUnitm', scale = 0 WHERE code IN ('M');
UPDATE llx_c_units SET active = 0, scale = 0 WHERE code IN ('LM');
UPDATE llx_c_units SET unit_type = 'surface', scale = 0 WHERE code IN ('M2');
UPDATE llx_c_units SET unit_type = 'volume', scale = 0 WHERE code IN ('M3','L');
UPDATE llx_c_units SET scale = -3, active = 0 WHERE code IN ('L');
UPDATE llx_c_units SET label = 'VolumeUnitm3' WHERE code IN ('M3');
UPDATE llx_c_units SET label = 'SurfaceUnitm2' WHERE code IN ('M2');

View File

@@ -300,17 +300,17 @@ if (empty($reshook))
$object->desiredstock = GETPOST('desiredstock')?GETPOST('desiredstock'):0;
$object->canvas = GETPOST('canvas');
$object->weight = GETPOST('weight');
$object->weight_units = GETPOST('weight_units');
$object->weight_units = GETPOST('weight_units'); // This is not the fk_unit but the power of unit
$object->length = GETPOST('size');
$object->length_units = GETPOST('size_units');
$object->length_units = GETPOST('size_units'); // This is not the fk_unit but the power of unit
$object->width = GETPOST('sizewidth');
$object->height = GETPOST('sizeheight');
$object->surface = GETPOST('surface');
$object->surface_units = GETPOST('surface_units');
$object->surface_units = GETPOST('surface_units'); // This is not the fk_unit but the power of unit
$object->volume = GETPOST('volume');
$object->volume_units = GETPOST('volume_units');
$object->volume_units = GETPOST('volume_units'); // This is not the fk_unit but the power of unit
$object->finished = GETPOST('finished', 'alpha');
$object->fk_unit = GETPOST('units', 'alpha');
$object->fk_unit = GETPOST('units', 'alpha'); // This is the fk_unit of sale
$accountancy_code_sell = GETPOST('accountancy_code_sell', 'alpha');
$accountancy_code_sell_intra = GETPOST('accountancy_code_sell_intra', 'alpha');
@@ -404,16 +404,16 @@ if (empty($reshook))
$object->canvas = GETPOST('canvas');
$object->weight = GETPOST('weight');
$object->weight_units = GETPOST('weight_units');
$object->weight_units = GETPOST('weight_units'); // This is not the fk_unit but the power of unit
$object->length = GETPOST('size');
$object->length_units = GETPOST('size_units');
$object->length_units = GETPOST('size_units'); // This is not the fk_unit but the power of unit
$object->width = GETPOST('sizewidth');
$object->height = GETPOST('sizeheight');
$object->surface = GETPOST('surface');
$object->surface_units = GETPOST('surface_units');
$object->surface_units = GETPOST('surface_units'); // This is not the fk_unit but the power of unit
$object->volume = GETPOST('volume');
$object->volume_units = GETPOST('volume_units');
$object->volume_units = GETPOST('volume_units'); // This is not the fk_unit but the power of unit
$object->finished = GETPOST('finished', 'alpha');
$units = GETPOST('units', 'int');
@@ -1044,7 +1044,7 @@ else
// Weight
print '<tr><td>'.$langs->trans("Weight").'</td><td colspan="3">';
print '<input name="weight" size="4" value="'.GETPOST('weight').'">';
print $formproduct->selectMeasuringUnits("weight_units", "weight", (empty($conf->global->MAIN_WEIGHT_DEFAULT_UNIT)?0:$conf->global->MAIN_WEIGHT_DEFAULT_UNIT));
print $formproduct->selectMeasuringUnits("weight_units", "weight", GETPOSTISSET('weight_units')?GETPOST('weight_units', 'alpha'):(empty($conf->global->MAIN_WEIGHT_DEFAULT_UNIT)?0:$conf->global->MAIN_WEIGHT_DEFAULT_UNIT), 0, 2);
print '</td></tr>';
// Length
if (empty($conf->global->PRODUCT_DISABLE_SIZE))
@@ -1053,7 +1053,7 @@ else
print '<input name="size" size="4" value="'.GETPOST('size').'"> x ';
print '<input name="sizewidth" size="4" value="'.GETPOST('sizewidth').'"> x ';
print '<input name="sizeheight" size="4" value="'.GETPOST('sizeheight').'">';
print $formproduct->selectMeasuringUnits("size_units", "size");
print $formproduct->selectMeasuringUnits("size_units", "size", GETPOSTISSET('size_units')?GETPOST('size_units', 'alpha'):'0', 0, 2);
print '</td></tr>';
}
if (empty($conf->global->PRODUCT_DISABLE_SURFACE))
@@ -1061,7 +1061,7 @@ else
// Surface
print '<tr><td>'.$langs->trans("Surface").'</td><td colspan="3">';
print '<input name="surface" size="4" value="'.GETPOST('surface').'">';
print $formproduct->selectMeasuringUnits("surface_units", "surface");
print $formproduct->selectMeasuringUnits("surface_units", "surface", GETPOSTISSET('surface_units')?GETPOST('surface_units', 'alpha'):'0', 0, 2);
print '</td></tr>';
}
if (empty($conf->global->PRODUCT_DISABLE_VOLUME))
@@ -1069,7 +1069,7 @@ else
// Volume
print '<tr><td>'.$langs->trans("Volume").'</td><td colspan="3">';
print '<input name="volume" size="4" value="'.GETPOST('volume').'">';
print $formproduct->selectMeasuringUnits("volume_units", "volume");
print $formproduct->selectMeasuringUnits("volume_units", "volume", GETPOSTISSET('volume_units')?GETPOST('volume_units', 'alpha'):'0', 0, 2);
print '</td></tr>';
}
}
@@ -1417,7 +1417,7 @@ else
// Weight
print '<tr><td>'.$langs->trans("Weight").'</td><td colspan="3">';
print '<input name="weight" size="5" value="'.$object->weight.'"> ';
print $formproduct->selectMeasuringUnits("weight_units", "weight", $object->weight_units);
print $formproduct->selectMeasuringUnits("weight_units", "weight", $object->weight_units, 0, 2);
print '</td></tr>';
if (empty($conf->global->PRODUCT_DISABLE_SIZE))
{
@@ -1426,7 +1426,7 @@ else
print '<input name="size" size="5" value="'.$object->length.'">x';
print '<input name="sizewidth" size="5" value="'.$object->width.'">x';
print '<input name="sizeheight" size="5" value="'.$object->height.'"> ';
print $formproduct->selectMeasuringUnits("size_units", "size", $object->length_units);
print $formproduct->selectMeasuringUnits("size_units", "size", $object->length_units, 0, 2);
print '</td></tr>';
}
if (empty($conf->global->PRODUCT_DISABLE_SURFACE))
@@ -1434,7 +1434,7 @@ else
// Surface
print '<tr><td>'.$langs->trans("Surface").'</td><td colspan="3">';
print '<input name="surface" size="5" value="'.$object->surface.'"> ';
print $formproduct->selectMeasuringUnits("surface_units", "surface", $object->surface_units);
print $formproduct->selectMeasuringUnits("surface_units", "surface", $object->surface_units, 0, 2);
print '</td></tr>';
}
if (empty($conf->global->PRODUCT_DISABLE_VOLUME))
@@ -1442,7 +1442,7 @@ else
// Volume
print '<tr><td>'.$langs->trans("Volume").'</td><td colspan="3">';
print '<input name="volume" size="5" value="'.$object->volume.'"> ';
print $formproduct->selectMeasuringUnits("volume_units", "volume", $object->volume_units);
print $formproduct->selectMeasuringUnits("volume_units", "volume", $object->volume_units, 0, 2);
print '</td></tr>';
}
}
@@ -1818,11 +1818,12 @@ else
print '<tr><td class="titlefield">'.$langs->trans("Nature").'</td><td colspan="2">';
print $object->getLibFinished();
print '</td></tr>';
// Weight
print '<tr><td class="titlefield">'.$langs->trans("Weight").'</td><td colspan="2">';
if ($object->weight != '')
{
print $object->weight." ".measuring_units_string($object->weight_units, "weight");
print $object->weight." ".measuring_units_string(0, "weight", $object->weight_units);
}
else
{
@@ -1838,7 +1839,7 @@ else
print $object->length;
if ($object->width) print " x ".$object->width;
if ($object->height) print " x ".$object->height;
print ' '.measuring_units_string($object->length_units, "size");
print ' '.measuring_units_string(0, "size", $object->length_units);
}
else
{
@@ -1852,7 +1853,7 @@ else
print '<tr><td>'.$langs->trans("Surface").'</td><td colspan="2">';
if ($object->surface != '')
{
print $object->surface." ".measuring_units_string($object->surface_units, "surface");
print $object->surface." ".measuring_units_string(0, "surface", $object->surface_units);
}
else
{
@@ -1866,7 +1867,7 @@ else
print '<tr><td>'.$langs->trans("Volume").'</td><td colspan="2">';
if ($object->volume != '')
{
print $object->volume." ".measuring_units_string($object->volume_units, "volume");
print $object->volume." ".measuring_units_string(0, "volume", $object->volume_units);
}
else
{

View File

@@ -308,7 +308,7 @@ class FormProduct
* @param string $measuring_style Unit to show: weight, size, surface, volume, time
* @param string $default Preselected value
* @param int $adddefault Add empty unit called "Default"
* @param int $mode 1=Use short label as value, 0=Use rowid
* @param int $mode 1=Use short label as value, 0=Use rowid, 2=Use scale (power)
* @return string
*/
public function selectMeasuringUnits($name = 'measuring_units', $measuring_style = '', $default = '0', $adddefault = 0, $mode = 0)
@@ -345,10 +345,12 @@ class FormProduct
{
$return .= '<option value="';
if ($mode == 1) $return .= $lines->short_label;
elseif ($mode == 2) $return .= $lines->scale;
else $return .= $lines->id;
$return .= '"';
if ($mode == 1 && $lines->short_label == $default) $return .= ' selected';
if ($mode == 0 && $lines->id == $default) $return .= ' selected';
elseif ($mode == 2 && $lines->scale == $default) $return .= ' selected';
elseif ($mode == 0 && $lines->id == $default) $return .= ' selected';
$return .= '>';
if ($measuring_style == 'time') $return.= $langs->trans(ucfirst($lines->label));
else $return .= $langs->trans($lines->label);