diff --git a/htdocs/bom/ajax/ajax.php b/htdocs/bom/ajax/ajax.php index 2ad12cc60a3..fac4fee6ef4 100644 --- a/htdocs/bom/ajax/ajax.php +++ b/htdocs/bom/ajax/ajax.php @@ -53,6 +53,7 @@ if (!defined('NOBROWSERNOTIF')) { include_once '../../main.inc.php'; // Load $user and permissions require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/cunits.class.php'; $action = GETPOST('action', 'aZ09'); @@ -69,10 +70,9 @@ if ($action == 'getDurationUnitByProduct') { $product = new Product($db); $res = $product->fetch($idproduct); - if ($res > 0) { - $return = $product->duration_unit; - } + $cUnit = new CUnits($db); + $fk_unit = $cUnit->getUnitFromCode($product->duration_unit, 'short_label','time'); - echo json_encode($return); + echo json_encode($fk_unit); exit(); } diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php index ce9a284200b..026f055fb8c 100644 --- a/htdocs/bom/bom_card.php +++ b/htdocs/bom/bom_card.php @@ -167,7 +167,7 @@ if (empty($reshook)) { $qty_frozen = price2num(GETPOST('qty_frozen', 'alpha'), 'MS'); $disable_stock_change = GETPOST('disable_stock_change', 'int'); $efficiency = price2num(GETPOST('efficiency', 'alpha')); - $duration_unit = GETPOST('duration_unit', 'alphanohtml'); + $fk_unit = GETPOST('fk_unit', 'alphanohtml'); if ($qty == '') { setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), null, 'errors'); $error++; @@ -191,7 +191,7 @@ if (empty($reshook)) { $bomline->qty_frozen = (int) $qty_frozen; $bomline->disable_stock_change = (int) $disable_stock_change; $bomline->efficiency = $efficiency; - $bomline->duration_unit = $duration_unit; + $bomline->fk_unit = $fk_unit; // Rang to use $rangmax = $object->line_max(0); @@ -227,7 +227,7 @@ if (empty($reshook)) { $qty_frozen = price2num(GETPOST('qty_frozen', 'alpha'), 'MS'); $disable_stock_change = GETPOST('disable_stock_change', 'int'); $efficiency = price2num(GETPOST('efficiency', 'alpha')); - $duration_unit = GETPOST('duration_unit', 'alphanohtml'); + $fk_unit = GETPOST('fk_unit', 'alphanohtml'); if ($qty == '') { setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), null, 'errors'); @@ -240,7 +240,9 @@ if (empty($reshook)) { $bomline->qty_frozen = (int) $qty_frozen; $bomline->disable_stock_change = (int) $disable_stock_change; $bomline->efficiency = $efficiency; - $bomline->duration_unit = $duration_unit; + + require_once DOL_DOCUMENT_ROOT.'/core/class/cunits.class.php'; + $bomline->fk_unit = $fk_unit; $result = $bomline->update($user); if ($result <= 0) { diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index e3aa0180cf5..f74a1d69263 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1128,7 +1128,8 @@ class BOM extends CommonObject } } else { //Convert qty to hour - $qty = convertDurationtoHour($line->qty, $line->duration_unit); + $unit = measuringUnitString($line->fk_unit); + $qty = convertDurationtoHour($line->qty, $unit); if ($conf->workstation->enabled) { if ($tmpproduct->fk_default_workstation) { @@ -1293,7 +1294,7 @@ class BOMLine extends CommonObjectLine 'qty_frozen' => array('type'=>'smallint', 'label'=>'QuantityFrozen', 'enabled'=>1, 'visible'=>1, 'default'=>0, 'position'=>105, 'css'=>'maxwidth50imp', 'help'=>'QuantityConsumedInvariable'), 'disable_stock_change' => array('type'=>'smallint', 'label'=>'DisableStockChange', 'enabled'=>1, 'visible'=>1, 'default'=>0, 'position'=>108, 'css'=>'maxwidth50imp', 'help'=>'DisableStockChangeHelp'), 'efficiency' => array('type'=>'double(24,8)', 'label'=>'ManufacturingEfficiency', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'position'=>110, 'notnull'=>1, 'css'=>'maxwidth50imp', 'help'=>'ValueOfEfficiencyConsumedMeans'), - 'duration_unit' => array('type'=>'varchar(6)', 'label'=>'Unit', 'enabled'=>1, 'visible'=>1, 'position'=>120, 'notnull'=>-1,), + 'fk_unit' => array('type'=>'integer', 'label'=>'Unit', 'enabled'=>1, 'visible'=>1, 'position'=>120, 'notnull'=>-1,), 'position' => array('type'=>'integer', 'label'=>'Rank', 'enabled'=>1, 'visible'=>0, 'default'=>0, 'position'=>200, 'notnull'=>1,), 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000, 'notnull'=>-1,), ); diff --git a/htdocs/bom/tpl/objectline_create.tpl.php b/htdocs/bom/tpl/objectline_create.tpl.php index ff257d6ab72..f8fa20e1db4 100644 --- a/htdocs/bom/tpl/objectline_create.tpl.php +++ b/htdocs/bom/tpl/objectline_create.tpl.php @@ -160,8 +160,11 @@ if ($filtertype != 1) { print ''; } else { $coldisplay++; + require_once DOL_DOCUMENT_ROOT.'/core/class/cunits.class.php'; + $cUnit = new CUnits($this->db); + $fk_unit_default = $cUnit->getUnitFromCode('h', 'short_label','time'); print ''; - print $formproduct->selectMeasuringUnits("duration_unit", "time", 'h', 0, 1); + print $formproduct->selectMeasuringUnits("fk_unit", "time", $fk_unit_default, 0, 0); print ''; $coldisplay++; @@ -212,7 +215,8 @@ jQuery(document).ready(function() { //change unit selected if we change service selected $('#idprodservice').change(function(){ - var idproduct = $(this).val(); + var idproduct = $(this).val(); + $.ajax({ url : "" ,type: 'POST' @@ -221,8 +225,10 @@ jQuery(document).ready(function() { ,'idproduct' : idproduct } }).done(function(data) { + + console.log(data); var data = JSON.parse(data); - $('#duration_unit').val(data).change();; + $("#fk_unit").val(data).change(); }); }); diff --git a/htdocs/bom/tpl/objectline_edit.tpl.php b/htdocs/bom/tpl/objectline_edit.tpl.php index b803277f359..d7505207f4c 100644 --- a/htdocs/bom/tpl/objectline_edit.tpl.php +++ b/htdocs/bom/tpl/objectline_edit.tpl.php @@ -141,7 +141,7 @@ if ($filtertype != 1) { $coldisplay++; print ''; - print $formproduct->selectMeasuringUnits("duration_unit", "time", ($line->duration_unit) ? $line->duration_unit : '', 0, 1); + print $formproduct->selectMeasuringUnits("fk_unit", "time", ($line->fk_unit) ? $line->fk_unit : '', 0, 0); print ''; $coldisplay++; diff --git a/htdocs/bom/tpl/objectline_view.tpl.php b/htdocs/bom/tpl/objectline_view.tpl.php index 2c9758f2176..7c436d5721e 100644 --- a/htdocs/bom/tpl/objectline_view.tpl.php +++ b/htdocs/bom/tpl/objectline_view.tpl.php @@ -132,16 +132,14 @@ if ($filtertype != 1) { //Unité print ''; $coldisplay++; - if ($line->qty > 1) { - $dur = array("s"=>$langs->trans("Seconds"), "i"=>$langs->trans("Minutes"), "h"=>$langs->trans("Hours"), "d"=>$langs->trans("Days"), "w"=>$langs->trans("Weeks"), "m"=>$langs->trans("Months"), "y"=>$langs->trans("Years")); - } elseif ($tmpproduct->duration_value > 0) { - $dur = array("s"=>$langs->trans("Second"), "i"=>$langs->trans("Minute"), "h"=>$langs->trans("Hour"), "d"=>$langs->trans("Day"), "w"=>$langs->trans("Week"), "m"=>$langs->trans("Month"), "y"=>$langs->trans("Year")); - } - if (!empty($line->duration_unit)) { - print (isset($dur[$line->duration_unit]) ? " ".$langs->trans($dur[$line->duration_unit])." " : ''); - } else { - print (!empty($tmpproduct->duration_unit) && isset($dur[$tmpproduct->duration_unit]) ? " " . $langs->trans($dur[$tmpproduct->duration_unit]) . " " : ''); + + if (!empty($line->fk_unit)) { + require_once DOL_DOCUMENT_ROOT.'/core/class/cunits.class.php'; + $unit = new CUnits($this->db); + $unit->fetch($line->fk_unit); + print (isset($unit->label) ? " ".$langs->trans(ucwords($unit->label))." " : ''); } + print ''; //Poste de travail diff --git a/htdocs/core/class/cunits.class.php b/htdocs/core/class/cunits.class.php index dfc22fd37ec..9ad1f77e84a 100644 --- a/htdocs/core/class/cunits.class.php +++ b/htdocs/core/class/cunits.class.php @@ -420,15 +420,16 @@ class CUnits // extends CommonObject * Get unit from code * @param string $code code of unit * @param string $mode 0= id , short_label=Use short label as value, code=use code + * @param string $unit_type weight,size,surface,volume,qty,time... * @return int <0 if KO, Id of code if OK */ - public function getUnitFromCode($code, $mode = 'code') + public function getUnitFromCode($code, $mode = 'code', $unit_type='') { if ($mode == 'short_label') { - return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid'); + return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid',0, ' AND unit_type = "'.$unit_type.'"'); } elseif ($mode == 'code') { - return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid'); + return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid',0, ' AND unit_type = "'. $unit_type .'"'); } return $code; diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 381280289ee..8f08d77c6fb 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -8504,10 +8504,11 @@ function dol_osencode($str) * @param string $fieldkey Field to search the key into * @param string $fieldid Field to get * @param int $entityfilter Filter by entity + * @param string $filters Filter on other fields * @return int <0 if KO, Id of code if OK * @see $langs->getLabelFromKey */ -function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = 'id', $entityfilter = 0) +function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = 'id', $entityfilter = 0, $filters = array()) { global $cache_codes; @@ -8529,6 +8530,9 @@ function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = if (!empty($entityfilter)) { $sql .= " AND entity IN (".getEntity($tablename).")"; } + if ($filters) { + $sql .= $filters; + } $resql = $db->query($sql); if ($resql) { diff --git a/htdocs/install/mysql/migration/16.0.0-17.0.0.sql b/htdocs/install/mysql/migration/16.0.0-17.0.0.sql index fb1ba0a8da2..051889a3dde 100644 --- a/htdocs/install/mysql/migration/16.0.0-17.0.0.sql +++ b/htdocs/install/mysql/migration/16.0.0-17.0.0.sql @@ -57,4 +57,4 @@ ALTER TABLE llx_facture_fourn ADD COLUMN close_missing_amount double(24, 8) afte ALTER TABLE llx_adherent_type ADD COLUMN caneditamount integer DEFAULT 0 AFTER amount; ALTER TABLE llx_product ADD COLUMN fk_default_workstation integer DEFAULT NULL; -ALTER TABLE llx_bom_bomline ADD COLUMN duration_unit varchar(6) DEFAULT NULL; +ALTER TABLE llx_bom_bomline ADD COLUMN fk_unit integer DEFAULT NULL; diff --git a/htdocs/install/mysql/tables/llx_bom_bomline.sql b/htdocs/install/mysql/tables/llx_bom_bomline.sql index c93484db92a..eae1f6c6662 100644 --- a/htdocs/install/mysql/tables/llx_bom_bomline.sql +++ b/htdocs/install/mysql/tables/llx_bom_bomline.sql @@ -25,7 +25,7 @@ CREATE TABLE llx_bom_bomline( qty_frozen smallint DEFAULT 0, disable_stock_change smallint DEFAULT 0, efficiency double(24,8) NOT NULL DEFAULT 1, - duration_unit varchar(6) NULL, + fk_unit integer NULL, position integer NOT NULL DEFAULT 0 -- END MODULEBUILDER FIELDS ) ENGINE=innodb;