Browse Source

Merge pull request #24978 from OPEN-DSI/develop_fix_clone_product

FIX: Fix error managment of product clone
Laurent Destailleur 2 years ago
parent
commit
ca54766617
1 changed files with 48 additions and 52 deletions
  1. 48 52
      htdocs/product/card.php

+ 48 - 52
htdocs/product/card.php

@@ -876,92 +876,88 @@ if (empty($reshook)) {
 		if (!GETPOST('clone_content') && !GETPOST('clone_prices')) {
 			setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
 		} else {
-			$db->begin();
-
-			$originalId = $id;
 			if ($object->id > 0) {
-				$object->ref = GETPOST('clone_ref', 'alphanohtml');
-				$object->status = 0;
-				$object->status_buy = 0;
-				$object->id = null;
-				$object->barcode = -1;
-
-				if ($object->check()) {
-					$object->context['createfromclone'] = 'createfromclone';
-					$id = $object->create($user);
+				$error = 0;
+				$clone = dol_clone($object, 2);
+
+				$clone->id = null;
+				$clone->ref = GETPOST('clone_ref', 'alphanohtml');
+				$clone->status = 0;
+				$clone->status_buy = 0;
+				$clone->barcode = -1;
+
+				if ($clone->check()) {
+					$db->begin();
+
+					$clone->context['createfromclone'] = 'createfromclone';
+					$id = $clone->create($user);
 					if ($id > 0) {
 						if (GETPOST('clone_composition')) {
-							$result = $object->clone_associations($originalId, $id);
-
+							$result = $clone->clone_associations($object->id, $id);
 							if ($result < 1) {
-								$db->rollback();
 								setEventMessages($langs->trans('ErrorProductClone'), null, 'errors');
-								header("Location: ".$_SERVER["PHP_SELF"]."?id=".$originalId);
-								exit;
+								setEventMessages($clone->error, $clone->errors, 'errors');
+								$error++;
 							}
 						}
 
-						if (GETPOST('clone_categories')) {
-							$result = $object->cloneCategories($originalId, $id);
-
+						if (!$error && GETPOST('clone_categories')) {
+							$result = $clone->cloneCategories($object->id, $id);
 							if ($result < 1) {
-								$db->rollback();
 								setEventMessages($langs->trans('ErrorProductClone'), null, 'errors');
-								header("Location: ".$_SERVER["PHP_SELF"]."?id=".$originalId);
-								exit;
+								setEventMessages($clone->error, $clone->errors, 'errors');
+								$error++;
 							}
 						}
 
-						if (GETPOST('clone_prices')) {
-							$result = $object->clone_price($originalId, $id);
-
+						if (!$error && GETPOST('clone_prices')) {
+							$result = $clone->clone_price($object->id, $id);
 							if ($result < 1) {
-								$db->rollback();
 								setEventMessages($langs->trans('ErrorProductClone'), null, 'errors');
-								header('Location: '.$_SERVER['PHP_SELF'].'?id='.$originalId);
-								exit();
+								setEventMessages($clone->error, $clone->errors, 'errors');
+								$error++;
 							}
 						}
 
-						// $object->clone_fournisseurs($originalId, $id);
-
-						$db->commit();
-						$db->close();
-
-						header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
-						exit;
+						// $clone->clone_fournisseurs($object->id, $id);
 					} else {
-						$id = $originalId;
-
-						if ($object->error == 'ErrorProductAlreadyExists') {
-							$db->rollback();
-
+						if ($clone->error == 'ErrorProductAlreadyExists') {
 							$refalreadyexists++;
 							$action = "";
 
-							$mesg = $langs->trans("ErrorProductAlreadyExists", $object->ref);
-							$mesg .= ' <a href="'.$_SERVER["PHP_SELF"].'?ref='.$object->ref.'">'.$langs->trans("ShowCardHere").'</a>.';
+							$mesg = $langs->trans("ErrorProductAlreadyExists", $clone->ref);
+							$mesg .= ' <a href="' . $_SERVER["PHP_SELF"] . '?ref=' . $clone->ref . '">' . $langs->trans("ShowCardHere") . '</a>.';
 							setEventMessages($mesg, null, 'errors');
-							$object->fetch($id);
 						} else {
-							$db->rollback();
-							if (count($object->errors)) {
-								setEventMessages($object->error, $object->errors, 'errors');
-								dol_print_error($db, $object->errors);
+							if (count($clone->errors)) {
+								setEventMessages($clone->error, $clone->errors, 'errors');
+								dol_print_error($db, $clone->errors);
 							} else {
-								setEventMessages($langs->trans($object->error), null, 'errors');
-								dol_print_error($db, $object->error);
+								setEventMessages($langs->trans($clone->error), null, 'errors');
+								dol_print_error($db, $clone->error);
 							}
 						}
+						$error++;
 					}
 
-					unset($object->context['createfromclone']);
+					unset($clone->context['createfromclone']);
+
+					if ($error) {
+						$db->rollback();
+					} else {
+						$db->commit();
+						$db->close();
+						header("Location: " . $_SERVER["PHP_SELF"] . "?id=" . $id);
+						exit;
+					}
+				} else {
+					setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("NewRefForClone")), null, 'errors');
 				}
 			} else {
-				$db->rollback();
 				dol_print_error($db, $object->error);
 			}
 		}
+		$action = 'clone';
 	}
 
 	// Delete a product