diff --git a/htdocs/product/card.php b/htdocs/product/card.php index f1729b153b9..3f2833fde52 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -88,8 +88,8 @@ $mesg = ''; $error = 0; $errors = array(); $refalreadyexists = 0; $id = GETPOST('id', 'int'); -$ref = GETPOST('ref', 'alpha'); -$type = (GETPOST('type', 'int') !== '') ? GETPOST('type', 'int') : Product::TYPE_PRODUCT; +$ref = (GETPOSTISSET('ref') ? GETPOST('ref', 'alpha') : null); +$type = (GETPOSTISSET('type') ? GETPOST('type', 'int') : Product::TYPE_PRODUCT); $action = (GETPOST('action', 'alpha') ? GETPOST('action', 'alpha') : 'view'); $cancel = GETPOST('cancel', 'alpha'); $backtopage = GETPOST('backtopage', 'alpha'); @@ -112,6 +112,16 @@ if (!empty($user->socid)) { $socid = $user->socid; } +// Load object modCodeProduct +$module = (!empty($conf->global->PRODUCT_CODEPRODUCT_ADDON) ? $conf->global->PRODUCT_CODEPRODUCT_ADDON : 'mod_codeproduct_leopard'); +if (substr($module, 0, 16) == 'mod_codeproduct_' && substr($module, -3) == 'php') { + $module = substr($module, 0, dol_strlen($module) - 4); +} +$result = dol_include_once('/core/modules/product/'.$module.'.php'); +if ($result > 0) { + $modCodeProduct = new $module(); +} + $object = new Product($db); $object->type = $type; // so test later to fill $usercancxxx is correct $extrafields = new ExtraFields($db); @@ -247,9 +257,11 @@ if (empty($reshook)) { $error++; } if (empty($ref)) { - setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Ref')), null, 'errors'); - $action = "create"; - $error++; + if (empty($conf->global->PRODUCT_GENERATE_REF_AFTER_FORM)) { + setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Ref')), null, 'errors'); + $action = "create"; + $error++; + } } if (!empty($duration_value) && empty($duration_unit)) { setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Unit')), null, 'errors'); @@ -431,6 +443,11 @@ if (empty($reshook)) { $error++; } + if (!$ref && !empty($conf->global->PRODUCT_GENERATE_REF_AFTER_FORM)) { + // Generate ref... + $ref = $modCodeProduct->getNextValue($object, $type); + } + if (!$error) { $id = $object->create($user); } @@ -470,7 +487,9 @@ if (empty($reshook)) { if ($object->id > 0) { $object->oldcopy = clone $object; - $object->ref = $ref; + if (empty($conf->global->PRODUCT_GENERATE_REF_AFTER_FORM)) { + $object->ref = $ref; + } $object->label = GETPOST('label', $label_security_check); $desc = dol_htmlcleanlastbr(preg_replace('/ $/', '', GETPOST('desc', 'restricthtml'))); @@ -1079,16 +1098,18 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action)) { print '
| '.$langs->trans("Ref").' | '; + if ($refalreadyexists) { + print $langs->trans("RefAlreadyExists"); + } + print ' | '.$langs->trans("Ref").' | '; - if ($refalreadyexists) { - print $langs->trans("RefAlreadyExists"); - } - print ' | '; // Label print '
| '.$langs->trans("Label").' |