From f10fac11747c0686f9720092098828bc2275bb8c Mon Sep 17 00:00:00 2001 From: Charles Delachapelle Evarisk Date: Thu, 29 Aug 2024 16:10:23 +0200 Subject: [PATCH] #30 [Competitorprice] add: clone system --- class/competitorprice.class.php | 56 +++++++++++++++++++++++++++++++-- view/competitorprice_card.php | 20 +++++++----- 2 files changed, 67 insertions(+), 9 deletions(-) diff --git a/class/competitorprice.class.php b/class/competitorprice.class.php index 6f70a59..622cece 100644 --- a/class/competitorprice.class.php +++ b/class/competitorprice.class.php @@ -24,10 +24,13 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; +//Load saturne libraries +require_once __DIR__ . '/../../saturne/class/saturneobject.class.php'; + /** * Class for CompetitorPrice */ -class CompetitorPrice extends CommonObject +class CompetitorPrice extends SaturneObject { /** * @var string ID of module. @@ -306,6 +309,55 @@ public function delete(User $user, bool $notrigger = false): int return $this->deleteCommon($user, $notrigger); } + /** + * Clone an object into another one. + * + * @param User $user User that creates + * @param int $fromID ID of object to clone. + * @return int New object created, <0 if KO. + * @throws Exception + */ + public function createFromClone(User $user, int $fromID): int + { + dol_syslog(__METHOD__, LOG_DEBUG); + + $object = new self($this->db); + $this->db->begin(); + + // Reset some properties + unset($object->id); + unset($object->fk_user_creat); + + // Load source object + $object->fetchCommon($fromID); + + // Clear fields + if (property_exists($object, 'date_creation')) { + $object->date_creation = dol_now(); + } + if (property_exists($object, 'competitor_date')) { + $object->competitor_date = dol_now(); + } + if (property_exists($object, 'ref')) { + $object->ref = $this->getNextNumRef(); + } + + // Create clone + $object->context = 'createfromclone'; + $competitorPriceID = $object->create($user); + + unset($object->context); + + // End + if ($competitorPriceID > 0) { + $this->db->commit(); + return $competitorPriceID; + } else { + $this->db->rollback(); + return -1; + } + } + /** * Return a link to the object card (with optionaly the picto) * @@ -499,4 +551,4 @@ public function initAsSpecimen(): void { $this->initAsSpecimenCommon(); } -} \ No newline at end of file +} diff --git a/view/competitorprice_card.php b/view/competitorprice_card.php index cd93f8e..f6d6813 100644 --- a/view/competitorprice_card.php +++ b/view/competitorprice_card.php @@ -87,10 +87,10 @@ // Default sort order (if not yet defined by previous GETPOST) if (!$sortfield) { - $sortfield = 't.fk_soc'; + $sortfield = 't.competitor_date'; } if (!$sortorder) { - $sortorder = 'ASC'; + $sortorder = 'DESC'; } // Definition of array of fields for columns @@ -185,7 +185,13 @@ $object->ref = $refCompetitorPriceMod->getNextValue($object); } - include DOL_DOCUMENT_ROOT . '/core/actions_addupdatedelete.inc.php'; + $noback = 1; + require_once DOL_DOCUMENT_ROOT . '/core/actions_addupdatedelete.inc.php'; + + if ($action == 'confirm_clone' && $permissiontoadd) { + header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $id); // Open record of new object + exit; + } //Tricks to use common template $object = $product; @@ -213,7 +219,7 @@ $formconfirm = ''; if (($action == 'deleteProductCompetitorPrice' && (empty($conf->use_javascript_ajax) || !empty($conf->dol_use_jmobile))) // Output when action = clone if jmobile or no js - || (!empty($conf->use_javascript_ajax) && empty($conf->dol_use_jmobile))) { // Always output when not jmobile nor js + || (!empty($conf->use_javascript_ajax) && empty($conf->dol_use_jmobile))) {// Always output when not jmobile nor js $formconfirm .= $form->formconfirm($_SERVER['PHP_SELF'] . '?id=' . $object->id . '&rowid=' . $competitorPrice->id, $langs->trans('DeleteProductCompetitorPrice'), $langs->trans('ConfirmDeleteProductCompetitorPrice'), 'confirm_delete', '', 'yes', 1); } @@ -457,10 +463,10 @@ // Modify-Remove print ''; if ($permissiontoadd) { - print '' . img_edit() . ''; - print ' '; + print '' . img_edit() . ''; + print '' . img_picto($langs->trans('Clone'), 'clone') . ''; print '' . img_picto($langs->trans('Remove'), 'delete') . ''; - } + } print ''; print ''; }