diff --git a/htdocs/comm/propal/card.php b/htdocs/comm/propal/card.php
index 559ceba1548..1cd02b05755 100644
--- a/htdocs/comm/propal/card.php
+++ b/htdocs/comm/propal/card.php
@@ -1128,7 +1128,7 @@ if (empty($reshook))
setEventMessages($mesg, null, 'errors');
} else {
// Insert line
- $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $price_base_type, $pu_ttc, $info_bits, $type, $rank, 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $date_start, $date_end, $array_options, $fk_unit, '', 0, $pu_ht_devise);
+ $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $price_base_type, $pu_ttc, $info_bits, $type, min($rank, count($object->lines) + 1), 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $date_start, $date_end, $array_options, $fk_unit, '', 0, $pu_ht_devise);
if ($result > 0) {
$db->commit();
diff --git a/htdocs/commande/card.php b/htdocs/commande/card.php
index a241222ae73..eabdea93e01 100644
--- a/htdocs/commande/card.php
+++ b/htdocs/commande/card.php
@@ -954,7 +954,7 @@ if (empty($reshook))
setEventMessages($mesg, null, 'errors');
} else {
// Insert line
- $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $info_bits, 0, $price_base_type, $pu_ttc, $date_start, $date_end, $type, $rank, 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $array_options, $fk_unit, '', 0, $pu_ht_devise);
+ $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $info_bits, 0, $price_base_type, $pu_ttc, $date_start, $date_end, $type, min($rank, count($object->lines) + 1), 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $array_options, $fk_unit, '', 0, $pu_ht_devise);
if ($result > 0) {
$ret = $object->fetch($object->id); // Reload to get new records
diff --git a/htdocs/compta/facture/card.php b/htdocs/compta/facture/card.php
index 62dcfaa5ddc..db984666449 100644
--- a/htdocs/compta/facture/card.php
+++ b/htdocs/compta/facture/card.php
@@ -2229,7 +2229,7 @@ if (empty($reshook))
setEventMessages($mesg, null, 'errors');
} else {
// Insert line
- $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $date_start, $date_end, 0, $info_bits, '', $price_base_type, $pu_ttc, $type, $rank, $special_code, '', 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $array_options, $_POST['progress'], '', $fk_unit, $pu_ht_devise);
+ $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $date_start, $date_end, 0, $info_bits, '', $price_base_type, $pu_ttc, $type, min($rank, count($object->lines) + 1), $special_code, '', 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $array_options, $_POST['progress'], '', $fk_unit, $pu_ht_devise);
if ($result > 0)
{
diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php
index 153f1b26775..5e0a3bbbec8 100644
--- a/htdocs/core/tpl/objectline_create.tpl.php
+++ b/htdocs/core/tpl/objectline_create.tpl.php
@@ -298,14 +298,7 @@ if ($nolinesbefore) {
}
if(!empty($conf->global->MAIN_VIEW_LINE_NUMBER)) {
- $tab = array(-1 => $langs->trans('AtTheEnd'));
- if (!empty($object->lines)) {
- $langs->load('admin');
- foreach ($object->lines as $k => $v) {
- $tab[$v->rang] = $langs->trans('OnLine') . ' ' . ($k + 1);
- }
- }
- echo '
'.$langs->trans('Position').' : '.$form->selectarray('rank', $tab);
+ echo '
'.$langs->trans('AddLineOnPosition').' : ';
}
if (is_object($hookmanager) && empty($senderissupplier))
diff --git a/htdocs/fourn/commande/card.php b/htdocs/fourn/commande/card.php
index 4dee5059223..4fd29a872d8 100644
--- a/htdocs/fourn/commande/card.php
+++ b/htdocs/fourn/commande/card.php
@@ -534,7 +534,7 @@ if (empty($reshook))
$pu_ht_devise,
'',
0,
- $rank
+ min($rank, count($object->lines) + 1)
);
}
if ($idprod == -99 || $idprod == 0)
diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php
index b60ee380cf4..0d8f59aa33d 100644
--- a/htdocs/fourn/facture/card.php
+++ b/htdocs/fourn/facture/card.php
@@ -1380,7 +1380,7 @@ if (empty($reshook))
$tva_npr,
$price_base_type,
$type,
- $rank,
+ min($rank, count($object->lines) + 1),
0,
$array_options,
$productsupplier->fk_unit,
diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang
index 3f21e47bc5f..1c7a1d47ebb 100644
--- a/htdocs/langs/en_US/main.lang
+++ b/htdocs/langs/en_US/main.lang
@@ -1074,5 +1074,4 @@ AmountMustBePositive=Amount must be positive
ByStatus=By status
InformationMessage=Information
Used=Used
-AtTheEnd=At the end
-OnLine=On line
+AddLineOnPosition=Add line on position (at the end if empty)
diff --git a/htdocs/supplier_proposal/card.php b/htdocs/supplier_proposal/card.php
index 1fc8723951c..1fce863ed84 100644
--- a/htdocs/supplier_proposal/card.php
+++ b/htdocs/supplier_proposal/card.php
@@ -679,7 +679,7 @@ if (empty($reshook))
$pu_ttc,
$tva_npr,
$type,
- $rank,
+ min($rank, count($object->lines) + 1),
0,
GETPOST('fk_parent_line'),
$fournprice,