diff --git a/htdocs/bom/tpl/objectline_view.tpl.php b/htdocs/bom/tpl/objectline_view.tpl.php
index 6f78641a60b..4fa7cf622d4 100644
--- a/htdocs/bom/tpl/objectline_view.tpl.php
+++ b/htdocs/bom/tpl/objectline_view.tpl.php
@@ -318,14 +318,12 @@ if ($resql) {
}
// Qty
- $label = $sub_bom_product->getLabelOfUnit('long');
+ $label = $sub_bom_product->getLabelOfUnit('long', $langs);
if ($sub_bom_line->qty_frozen > 0) {
print '
'.price($sub_bom_line->qty, 0, '', 0, 0).' | ';
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
print '';
- if ($label !== '') {
- print $langs->trans($label);
- }
+ print $label;
print ' | ';
}
print ''.$langs->trans('Yes').' | ';
@@ -333,9 +331,7 @@ if ($resql) {
print ''.price($sub_bom_line->qty * (float) $line->qty, 0, '', 0, 0).' | ';
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
print '';
- if ($label !== '') {
- print $langs->trans($label);
- }
+ print $label;
print ' | ';
}
diff --git a/htdocs/contrat/card.php b/htdocs/contrat/card.php
index 65be9ff5e68..161d7752501 100644
--- a/htdocs/contrat/card.php
+++ b/htdocs/contrat/card.php
@@ -1739,7 +1739,7 @@ if ($action == 'create') {
print ''.$objp->qty.' | ';
// Unit
if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
- print ''.$langs->trans($object->lines[$cursorline - 1]->getLabelOfUnit()).' | ';
+ print ''.$object->lines[$cursorline - 1]->getLabelOfUnit('long', $langs).' | ';
}
// Discount
if ($objp->remise_percent > 0) {
diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php
index 311de5a3e95..bfc80694946 100644
--- a/htdocs/core/class/commondocgenerator.class.php
+++ b/htdocs/core/class/commondocgenerator.class.php
@@ -943,8 +943,9 @@ abstract class CommonDocGenerator
// Units
if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
- $resarray['line_unit'] = $outputlangs->trans($line->getLabelOfUnit('long'));
- $resarray['line_unit_short'] = $outputlangs->trans($line->getLabelOfUnit('short'));
+ $resarray['line_unit'] = $line->getLabelOfUnit('long', $outputlangs);
+ $resarray['line_unit_short'] = $line->getLabelOfUnit('short', $outputlangs);
+ //$resarray['line_unit_code'] = $line->getLabelOfUnit('code', $outputlangs);
}
// Retrieve extrafields
diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php
index 3b74b7fb9ef..b941f1e06e3 100644
--- a/htdocs/core/class/commonobject.class.php
+++ b/htdocs/core/class/commonobject.class.php
@@ -5732,7 +5732,9 @@ abstract class CommonObject
$this->tpl['multicurrency_price'] = price($line->multicurrency_subprice);
$this->tpl['qty'] = (($line->info_bits & 2) != 2) ? $line->qty : ' ';
if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
- $this->tpl['unit'] = $langs->transnoentities($line->getLabelOfUnit('long'));
+ $this->tpl['unit'] = $line->getLabelOfUnit('long', $langs);
+ $this->tpl['unit_short'] = $line->getLabelOfUnit('short', $langs);
+ //$this->tpl['unit_code'] = $line->getLabelOfUnit('code');
}
$this->tpl['remise_percent'] = (($line->info_bits & 2) != 2) ? vatrate((string) $line->remise_percent, true) : ' ';
diff --git a/htdocs/core/class/commonobjectline.class.php b/htdocs/core/class/commonobjectline.class.php
index ea63be80aea..da0173728df 100644
--- a/htdocs/core/class/commonobjectline.class.php
+++ b/htdocs/core/class/commonobjectline.class.php
@@ -295,45 +295,61 @@ abstract class CommonObjectLine extends CommonObject
}
/**
- * Returns the label, short_label or code found in units dictionary from ->fk_unit.
- * A langs->trans() must be called on result to get translated value.
+ * Reads the units dictionary to return the translation code of a unit (if type='code'), or translated long label (if type='long') or short label (if type='short').
+ * TODO Duplicate of getLabelOfUnit() in product.class.php
*
- * @param string $type Label type ('long', 'short' or 'code'). This can be a translation key.
- * @return string|int<-1,1> Return integer <0 if KO, label if OK (Example: 'long', 'short' or 'unitCODE')
+ * @param string $type Code type ('code', 'long' or 'short')
+ * @param Translate|null $outputlangs Language to use for long label translation
+ * @param int $noentities No entities
+ * @return string|int Return integer <0 if KO, code or label of unit if OK.
*/
- public function getLabelOfUnit($type = 'long')
+ public function getLabelOfUnit($type = 'long', $outputlangs = null, $noentities = 0)
{
global $langs;
if (empty($this->fk_unit)) {
return '';
}
-
- $langs->load('products');
-
- $label_type = 'label';
- if ($type == 'short') {
- $label_type = 'short_label';
- } elseif ($type == 'code') {
- $label_type = 'code';
+ if (empty($outputlangs)) {
+ $outputlangs = $langs;
}
- $sql = "SELECT ".$label_type.", code from ".$this->db->prefix()."c_units where rowid = ".((int) $this->fk_unit);
+ $outputlangs->load('products');
+ $label = '';
+
+ $sql = "SELECT code, label, short_label FROM ".$this->db->prefix()."c_units where rowid = ".((int) $this->fk_unit);
$resql = $this->db->query($sql);
- if ($resql && $this->db->num_rows($resql) > 0 && $res = $this->db->fetch_array($resql)) {
- if ($label_type == 'code') {
- $label = 'unit'.$res['code'];
- } else {
- $label = $res[$label_type];
- }
- $this->db->free($resql);
- return $label;
- } else {
- $this->error = $this->db->lasterror();
+ if (!$resql) {
+ $this->error = $this->db->error();
dol_syslog(get_class($this)."::getLabelOfUnit Error ".$this->error, LOG_ERR);
return -1;
+ } elseif ($this->db->num_rows($resql) > 0 && $res = $this->db->fetch_array($resql)) {
+ if ($type == 'short') {
+ if ($noentities) {
+ $label = $outputlangs->transnoentitiesnoconv($res['short_label']);
+ } else {
+ $label = $outputlangs->trans($res['short_label']);
+ }
+ } elseif ($type == 'code') {
+ $label = $res['code'];
+ } else {
+ if ($outputlangs->trans('unit'.$res['code']) == 'unit'.$res['code']) {
+ // No translation available
+ $label = $res['label'];
+ } else {
+ // Return the translated value
+ if ($noentities) {
+ $label = $outputlangs->transnoentitiesnoconv('unit'.$res['code']);
+ } else {
+ $label = $outputlangs->trans('unit'.$res['code']);
+ }
+ }
+ }
}
+ $this->db->free($resql);
+
+ return $label;
}
/**
diff --git a/htdocs/core/lib/pdf.lib.php b/htdocs/core/lib/pdf.lib.php
index 21bf6a2516f..dcb5eafe065 100644
--- a/htdocs/core/lib/pdf.lib.php
+++ b/htdocs/core/lib/pdf.lib.php
@@ -2323,7 +2323,7 @@ function pdf_getlineqty_keeptoship($object, $i, $outputlangs, $hidedetails = 0)
*/
function pdf_getlineunit($object, $i, $outputlangs, $hidedetails = 0)
{
- global $hookmanager, $langs;
+ global $hookmanager;
$reshook = 0;
$result = '';
@@ -2348,7 +2348,7 @@ function pdf_getlineunit($object, $i, $outputlangs, $hidedetails = 0)
}
if (empty($reshook)) {
if (empty($hidedetails) || $hidedetails > 1) {
- $result .= $langs->transnoentitiesnoconv($object->lines[$i]->getLabelOfUnit('short'));
+ $result .= $object->lines[$i]->getLabelOfUnit('short', $outputlangs, 1);
}
}
return $result;
diff --git a/htdocs/core/tpl/objectline_view.tpl.php b/htdocs/core/tpl/objectline_view.tpl.php
index b0929b26969..7f85b8d3155 100644
--- a/htdocs/core/tpl/objectline_view.tpl.php
+++ b/htdocs/core/tpl/objectline_view.tpl.php
@@ -437,10 +437,8 @@ print '';
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
print '';
- $label = $line->getLabelOfUnit('short');
- if ($label !== '') {
- print $langs->trans($label);
- }
+ $label = $line->getLabelOfUnit('short', $langs);
+ print $label;
print ' | ';
}
if (!empty($line->remise_percent) && $line->special_code != 3) {
diff --git a/htdocs/product/card.php b/htdocs/product/card.php
index 3dfae650581..7c96ee4bbd5 100644
--- a/htdocs/product/card.php
+++ b/htdocs/product/card.php
@@ -2966,12 +2966,10 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($canvasdisplayactio
// Unit
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
- $unit = $object->getLabelOfUnit();
+ $unit = $object->getLabelOfUnit('long', $langs);
print '| '.$langs->trans('DefaultUnitToShow').' | ';
- if ($unit !== '') {
- print $langs->trans($unit);
- }
+ print $unit;
print ' |
';
}
diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php
index 8d2ac1d2b5e..f71054ad52c 100644
--- a/htdocs/product/class/product.class.php
+++ b/htdocs/product/class/product.class.php
@@ -6939,27 +6939,29 @@ class Product extends CommonObject
}
/**
- * Returns the text label from units dictionary
+ * Reads the units dictionary to return the translation code of a unit (if type='code'), or translated long label (if type='long') or short label (if type='short').
+ * TODO Duplicate of getLabelOfUnit() in commonobjectline.class.php
*
- * @param string $type Label type (long or short)
- * @return string|int Return integer <0 if ko, label if ok
+ * @param string $type Code type ('code', 'long' or 'short')
+ * @param Translate|null $outputlangs Language to use for long label translation
+ * @param int $noentities No entities
+ * @return string|int Return integer <0 if KO, code or label of unit if OK.
*/
- public function getLabelOfUnit($type = 'long')
+ public function getLabelOfUnit($type = 'long', $outputlangs = null, $noentities = 0)
{
global $langs;
- if (!$this->fk_unit) {
+ if (empty($this->fk_unit)) {
return '';
}
-
- $langs->load('products');
- $label = '';
- $label_type = 'label';
- if ($type == 'short') {
- $label_type = 'short_label';
+ if (empty($outputlangs)) {
+ $outputlangs = $langs;
}
- $sql = "SELECT ".$label_type.", code from ".$this->db->prefix()."c_units where rowid = ".((int) $this->fk_unit);
+ $outputlangs->load('products');
+ $label = '';
+
+ $sql = "SELECT code, label, short_label FROM ".$this->db->prefix()."c_units where rowid = ".((int) $this->fk_unit);
$resql = $this->db->query($sql);
if (!$resql) {
@@ -6967,7 +6969,27 @@ class Product extends CommonObject
dol_syslog(get_class($this)."::getLabelOfUnit Error ".$this->error, LOG_ERR);
return -1;
} elseif ($this->db->num_rows($resql) > 0 && $res = $this->db->fetch_array($resql)) {
- $label = ($label_type == 'short_label' ? $res[$label_type] : 'unit'.$res['code']);
+ if ($type == 'short') {
+ if ($noentities) {
+ $label = $outputlangs->transnoentitiesnoconv($res['short_label']);
+ } else {
+ $label = $outputlangs->trans($res['short_label']);
+ }
+ } elseif ($type == 'code') {
+ $label = $res['code'];
+ } else {
+ if ($outputlangs->trans('unit'.$res['code']) == 'unit'.$res['code']) {
+ // No translation available
+ $label = $res['label'];
+ } else {
+ // Return the translated value
+ if ($noentities) {
+ $label = $outputlangs->transnoentitiesnoconv('unit'.$res['code']);
+ } else {
+ $label = $outputlangs->trans('unit'.$res['code']);
+ }
+ }
+ }
}
$this->db->free($resql);
diff --git a/htdocs/product/price_suppliers.php b/htdocs/product/price_suppliers.php
index 6d961fb2c62..8d21413b1f3 100644
--- a/htdocs/product/price_suppliers.php
+++ b/htdocs/product/price_suppliers.php
@@ -602,9 +602,9 @@ if ($id > 0 || $ref) {
}
// Units
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
- $unit = $object->getLabelOfUnit();
+ $unit = $object->getLabelOfUnit('long', $langs);
if ($unit !== '') {
- print ' '.$langs->trans($unit);
+ print ' '.$unit;
}
}
print '';
@@ -620,9 +620,9 @@ if ($id > 0 || $ref) {
// Units
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
- $unit = $object->getLabelOfUnit();
+ $unit = $object->getLabelOfUnit('long', $langs);
if ($unit !== '') {
- print ' '.$langs->trans($unit);
+ print ' '.$unit;
}
}
}
@@ -1182,12 +1182,12 @@ if ($id > 0 || $ref) {
// Quantity
if (!empty($arrayfields['pfp.quantity']['checked'])) {
print '';
- print $productfourn->fourn_qty;
+ print dolPrintHTML($productfourn->fourn_qty);
// Units
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
- $unit = $object->getLabelOfUnit();
+ $unit = $object->getLabelOfUnit('long', $langs);
if ($unit !== '') {
- print ' '.$langs->trans($unit);
+ print ' '.$unit;
}
}
print ' | ';
diff --git a/htdocs/product/stock/card.php b/htdocs/product/stock/card.php
index c4570804ad9..ed79e7907d3 100644
--- a/htdocs/product/stock/card.php
+++ b/htdocs/product/stock/card.php
@@ -829,7 +829,7 @@ if ($action == 'create') {
if (is_null($productstatic->fk_unit)) {
$productstatic->fk_unit = 1;
}
- print $langs->trans($productstatic->getLabelOfUnit());
+ print $productstatic->getLabelOfUnit('long', $langs);
print '';
}
@@ -892,7 +892,7 @@ if ($action == 'create') {
$totalarray['val']['totalunit'] = $totalunit;
$totalarray['val']['totalvalue'] = price2num($totalvalue, 'MT');
$totalarray['val']['totalvaluesell'] = price2num($totalvaluesell, 'MT');
- $totalarray['val']['units'] = $langs->trans($productstatic->getLabelOfUnit());
+ $totalarray['val']['units'] = $productstatic->getLabelOfUnit('long', $langs);
$parameters = array('context' => 'warehousecard', 'totalarray' => &$totalarray);
// Note that $action and $object may have been modified by hook
diff --git a/htdocs/product/stock/stocktransfer/stocktransfer_card.php b/htdocs/product/stock/stocktransfer/stocktransfer_card.php
index 569997df752..af2727778d6 100644
--- a/htdocs/product/stock/stocktransfer/stocktransfer_card.php
+++ b/htdocs/product/stock/stocktransfer/stocktransfer_card.php
@@ -863,9 +863,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
print '';
$label = $productstatic->getLabelOfUnit('short');
- if ($label !== '') {
- print $langs->trans($label);
- }
+ print $label;
print ' | ';
}