diff --git a/htdocs/product/price.php b/htdocs/product/price.php index b2eb7227265..d58e0dcd8b2 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -11,7 +11,7 @@ * Copyright (C) 2015-2023 Alexandre Spangaro * Copyright (C) 2015 Marcos García * Copyright (C) 2016 Ferran Marcet - * Copyright (C) 2018-2025 Frédéric France + * Copyright (C) 2018-2026 Frédéric France * Copyright (C) 2018 Nicolas ZABOURI * Copyright (C) 2024-2025 MDW * Copyright (C) 2025 Mélina Joum @@ -66,6 +66,7 @@ $langs->loadLangs(array('products', 'bills', 'companies', 'other')); $error = 0; $errors = array(); +$rowid = 0; $id = GETPOSTINT('id'); $ref = GETPOST('ref', 'alpha'); @@ -546,18 +547,19 @@ if (empty($reshook)) { $db->free($resql); } if (!empty($lineid->rowid)) { + require_once DOL_DOCUMENT_ROOT . '/core/class/genericobject.class.php'; + $genericObject = new GenericObject($db); + // We need to force table to update product_price extrafields + $genericObject->id = $lineid->rowid; + $genericObject->table_element = 'product_price'; foreach ($price_extralabels as $code => $label) { - $code_array = GETPOST($code, 'array'); - $object->array_options['options_'.$code] = $code_array[$key]; + $extrafield_values = $extrafields->getOptionalsFromPost('product_price', (string) $key); + $genericObject->array_options['options_'.$code] = $extrafield_values['options_'.$code]; } - // We need to force table to update product_price and not product extrafields - $object->id = $lineid->rowid; - $object->table_element = 'product_price'; - $result = $object->insertExtraFields(); - // Back to product table - $object->id = $id; - $object->table_element = 'product'; + $result = $genericObject->insertExtraFields(); + if ($result < 0) { + setEventMessages($genericObject->error, $genericObject->errors, 'errors'); $error++; } } @@ -689,7 +691,7 @@ if (empty($reshook)) { if ($action == 'delete_all_price_by_qty' && $permissiontoadd) { $priceid = GETPOSTINT('priceid'); - if (!empty($rowid)) { + if (!empty($priceid)) { $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_price_by_qty"; $sql .= " WHERE fk_product_price = ".((int) $priceid); @@ -1186,7 +1188,7 @@ if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUS print ''; print $langs->trans("PriceLevel"); if ($user->admin) { - print ' id.'">'.img_edit($langs->trans('EditSellingPriceLabel'), 0).''; + print ' 'editlabelsellingprice', 'id' => $object->id], true).'">'.img_edit($langs->trans('EditSellingPriceLabel'), 0).''; } print ''; print ''.$langs->trans("SellingPrice").''; @@ -1263,6 +1265,10 @@ if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUS } print ''; if (!empty($extralabels)) { + require_once DOL_DOCUMENT_ROOT . '/core/class/genericobject.class.php'; + $genericObject = new GenericObject($db); + // We need to force table to fetch optionals product_price extrafields + $genericObject->table_element = 'product_price'; $sql1 = "SELECT rowid"; $sql1 .= " FROM ".$object->db->prefix()."product_price"; $sql1 .= " WHERE entity IN (".getEntity('productprice').")"; @@ -1271,35 +1277,16 @@ if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUS $sql1 .= " ORDER BY date_price DESC, rowid DESC"; $sql1 .= " LIMIT 1"; $resql1 = $object->db->query($sql1); - if ($resql1) { - $lineid = $object->db->fetch_object($resql1); + if ($resql1 && $lineid = $object->db->fetch_object($resql1)) { + $genericObject->id = $lineid->rowid; + $genericObject->fetch_optionals(); } - $sql2 = "SELECT"; - $sql2 .= " fk_object"; foreach ($extralabels as $key => $value) { - $sql2 .= ", ".$db->sanitize($key); - } - $sql2 .= " FROM ".MAIN_DB_PREFIX."product_price_extrafields"; - $sql2 .= " WHERE fk_object = ".((int) $lineid->rowid); - $resql2 = $db->query($sql2); - if ($resql2) { - if ($db->num_rows($resql2) != 1) { - foreach ($extralabels as $key => $value) { - if (!empty($extrafields->attributes["product_price"]['list'][$key]) && $extrafields->attributes["product_price"]['list'][$key] != 3) { - print ''; - } - } - } else { - $obj = $db->fetch_object($resql2); - foreach ($extralabels as $key => $value) { - if (!empty($extrafields->attributes["product_price"]['list'][$key]) && $extrafields->attributes["product_price"]['list'][$key] != 3) { - print ''.$extrafields->showOutputField($key, $obj->{$key}, '', 'product_price').""; - } - } + if (!empty($extrafields->attributes["product_price"]['list'][$key]) && $extrafields->attributes["product_price"]['list'][$key] != 3) { + print ''.$extrafields->showOutputField($key, $genericObject->array_options['options_' . $key], '', 'product_price').""; } - $db->free($resql1); - $db->free($resql2); } + $db->free($resql1); } print ''; @@ -1895,6 +1882,10 @@ if (($action == 'edit_price' || $action == 'edit_level_price') && $object->getRi print ''; if (!empty($extralabels)) { + require_once DOL_DOCUMENT_ROOT . '/core/class/genericobject.class.php'; + $genericObject = new GenericObject($db); + // We need to force table to fetch optionals product_price extrafields + $genericObject->table_element = 'product_price'; $sql1 = "SELECT rowid"; $sql1 .= " FROM ".$object->db->prefix()."product_price"; $sql1 .= " WHERE entity IN (".getEntity('productprice').")"; @@ -1903,44 +1894,23 @@ if (($action == 'edit_price' || $action == 'edit_level_price') && $object->getRi $sql1 .= " ORDER BY date_price DESC, rowid DESC"; $sql1 .= " LIMIT 1"; $resql1 = $object->db->query($sql1); - if ($resql1) { - $lineid = $object->db->fetch_object($resql1); + if ($resql1 && $lineid = $object->db->fetch_object($resql1)) { + $genericObject->id = $lineid->rowid; + $genericObject->fetch_optionals(); } - if (empty($lineid->rowid)) { - foreach ($extralabels as $key => $value) { - if (!empty($extrafields->attributes["product_price"]['list'][$key]) && ($extrafields->attributes["product_price"]['list'][$key] == 1 || $extrafields->attributes["product_price"]['list'][$key] == 3 || ($action == "edit_level_price" && $extrafields->attributes["product_price"]['list'][$key] == 4))) { - if (!empty($extrafields->attributes["product_price"]['langfile'][$key])) { - $langs->load($extrafields->attributes["product_price"]['langfile'][$key]); - } - - $extravalue = GETPOSTISSET('options_'.$key) ? $extrafield_values['options_'.$key] : $obj->{$key}; - print ''; + foreach ($extralabels as $key => $value) { + if (!empty($extrafields->attributes["product_price"]['list'][$key]) && ($extrafields->attributes["product_price"]['list'][$key] == 1 || $extrafields->attributes["product_price"]['list'][$key] == 3 || ($action == "edit_level_price" && $extrafields->attributes["product_price"]['list'][$key] == 4))) { + if (!empty($extrafields->attributes["product_price"]['langfile'][$key])) { + $langs->load($extrafields->attributes["product_price"]['langfile'][$key]); } - } - } else { - $sql = "SELECT"; - $sql .= " fk_object"; - foreach ($extralabels as $key => $value) { - $sql .= ", ".$db->sanitize($key); - } - $sql .= " FROM ".MAIN_DB_PREFIX."product_price_extrafields"; - $sql .= " WHERE fk_object = ".((int) $lineid->rowid); - $resql = $db->query($sql); - if ($resql) { - $obj = $db->fetch_object($resql); - foreach ($extralabels as $key => $value) { - if (!empty($extrafields->attributes["product_price"]['list'][$key]) && ($extrafields->attributes["product_price"]['list'][$key] == 1 || $extrafields->attributes["product_price"]['list'][$key] == 3 || ($action == "edit_level_price" && $extrafields->attributes["product_price"]['list'][$key] == 4))) { - if (!empty($extrafields->attributes["product_price"]['langfile'][$key])) { - $langs->load($extrafields->attributes["product_price"]['langfile'][$key]); - } - $extravalue = (GETPOSTISSET('options_'.$key) ? $extrafield_values['options_'.$key] : $obj->{$key} ?? ''); - print ''; - } - } - $db->free($resql); + // $extravalue = (GETPOSTISSET('options_'.$key) ? $extrafield_values['options_'.$key] : $genericObject->array_options['options_' . $key] ?? ''); + print ''; + print $extrafields->showInputField($key, $genericObject->array_options['options_' . $key], '', (string) $i, '', '', $genericObject, 'product_price'); + print ''; } } + $db->free($resql1); } print ''; }