Skip to content

Commit

Permalink
#30 [Competitorprice] add: clone system
Browse files Browse the repository at this point in the history
  • Loading branch information
evarisk-charles committed Aug 29, 2024
1 parent 4b21f2a commit f10fac1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
56 changes: 54 additions & 2 deletions class/competitorprice.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
*
Expand Down Expand Up @@ -499,4 +551,4 @@ public function initAsSpecimen(): void
{
$this->initAsSpecimenCommon();
}
}
}
20 changes: 13 additions & 7 deletions view/competitorprice_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -457,10 +463,10 @@
// Modify-Remove
print '<td class="center nowraponall">';
if ($permissiontoadd) {
print '<a class="editfielda" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&socid=' . $competitorPriceDetail->fk_soc . '&action=update_competitor_price&rowid=' . $competitorPriceDetail->id . '&token=' . newToken() . '">' . img_edit() . '</a>';
print ' ';
print '<a class="editfielda marginrightonly" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&socid=' . $competitorPriceDetail->fk_soc . '&action=update_competitor_price&rowid=' . $competitorPriceDetail->id . '&token=' . newToken() . '">' . img_edit() . '</a>';
print '<a class="marginrightonly" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&socid=' . $competitorPriceDetail->fk_soc . '&action=confirm_clone&confirm=yes&rowid=' . $competitorPriceDetail->id . '&token=' . newToken() . '">' . img_picto($langs->trans('Clone'), 'clone') . '</a>';
print '<a href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&socid=' . $competitorPriceDetail->fk_soc . '&action=deleteProductCompetitorPrice&rowid=' . $competitorPriceDetail->id . '&token=' . newToken() . '">' . img_picto($langs->trans('Remove'), 'delete') . '</a>';
}
}
print '</td>';
print '</tr>';
}
Expand Down

0 comments on commit f10fac1

Please sign in to comment.