Merge pull request #5871 from atm-florian/dev_clonecontract

NEW Add clone contract feature
This commit is contained in:
Laurent Destailleur 2016-10-15 02:21:35 +02:00 committed by GitHub
commit 6aeed34d30
2 changed files with 167 additions and 20 deletions

View File

@ -967,6 +967,28 @@ if (empty($reshook))
} }
} }
} }
// Action clone object
if ($action == 'confirm_clone' && $confirm == 'yes')
{
if (! GETPOST('socid', 3))
{
setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
}
else
{
if ($object->id > 0) {
$result = $object->createFromClone($socid);
if ($result > 0) {
header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $result);
exit();
} else {
if (count($object->errors) > 0) setEventMessages($object->error, $object->errors, 'errors');
$action = '';
}
}
}
}
} }
/* /*
@ -1265,6 +1287,12 @@ else
print '<input type="hidden" name="action" value="setremise">'; print '<input type="hidden" name="action" value="setremise">';
} }
// Clone confirmation
if ($action == 'clone') {
$formquestion = array(array('type' => 'other','name' => 'socid','label' => $langs->trans("SelectThirdParty"),'value' => $form->select_company(GETPOST('socid', 'int'), 'socid', '(s.client=1 OR s.client=2 OR s.client=3)')));
print $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('CloneContract'), $langs->trans('ConfirmCloneContract', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
}
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';
$linkback = '<a href="'.DOL_URL_ROOT.'/contrat/list.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>'; $linkback = '<a href="'.DOL_URL_ROOT.'/contrat/list.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
@ -1965,6 +1993,11 @@ else
else print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="'.$langs->trans("NotEnoughPermissions").'">'.$langs->trans("CreateOrder").'</a></div>'; else print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="'.$langs->trans("NotEnoughPermissions").'">'.$langs->trans("CreateOrder").'</a></div>';
} }
// Clone
if ($user->rights->contrat->creer) {
print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&amp;socid=' . $object->socid . '&amp;action=clone&amp;object=' . $object->element . '">' . $langs->trans("ToClone") . '</a></div>';
}
if ($object->nbofservicesclosed < $nbofservices) if ($object->nbofservicesclosed < $nbofservices)
{ {
//if (! $numactive) //if (! $numactive)

View File

@ -2263,6 +2263,120 @@ class Contrat extends CommonObject
return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables); return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
} }
/**
* Load an object from its id and create a new one in database
*
* @param int $socid Id of thirdparty
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers
* @return int New id of clone
*/
function createFromClone($socid = 0, $notrigger=0) {
global $db, $user, $langs, $conf, $hookmanager;
dol_include_once('/projet/class/project.class.php');
$this->context['createfromclone'] = 'createfromclone';
$error = 0;
$now = dol_now();
$this->fetch($this->id);
// Load dest object
$clonedObj = clone $this;
$this->db->begin();
$objsoc = new Societe($this->db);
$objsoc->fetch($clonedObj->socid);
// $clonedObj->id=0;
$clonedObj->statut = 0;
if (empty($conf->global->CONTRACT_ADDON) || ! is_readable(DOL_DOCUMENT_ROOT . "/core/modules/contract/" . $conf->global->CONTRACT_ADDON . ".php")) {
$this->error = 'ErrorSetupNotComplete';
dol_syslog($this->error);
return - 1;
}
// Set ref
require_once DOL_DOCUMENT_ROOT . "/core/modules/contract/" . $conf->global->CONTRACT_ADDON . '.php';
$obj = $conf->global->CONTRACT_ADDON;
$modContract = new $obj();
$clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
// get extrafields so they will be clone
foreach ( $this->lines as $line ) {
$line->fetch_optionals($line->rowid);
}
// Create clone
$result = $clonedObj->create($user);
if ($result < 0) {
$error ++;
$this->error = $clonedObj->error;
$this->errors[] = $clonedObj->error;
} else {
// copy internal contacts
if ($clonedObj->copy_linked_contact($this, 'internal') < 0)
$error ++;
// copy external contacts if same company
elseif ($this->socid == $clonedObj->socid) {
if ($clonedObj->copy_linked_contact($this, 'external') < 0)
$error ++;
}
}
if (! $error) {
foreach ( $this->lines as $line ) {
$result = $clonedObj->addline($line->desc, $line->subprice, $line->qty, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, $line->fk_product, $line->remise_percent, $line->date_ouverture, $line->date_cloture, 'HT', 0, $line->info_bits, $line->fk_fournprice, $line->pa_ht, $line->array_options, $line->fk_unit);
if ($result < 0) {
$error ++;
$this->error = $clonedObj->error;
$this->errors[] = $clonedObj->error;
}
}
}
if (! $error) {
// Hook of thirdparty module
if (is_object($hookmanager)) {
$parameters = array (
'objFrom' => $this,
'clonedObj' => $clonedObj
);
$action = '';
$reshook = $hookmanager->executeHooks('createFrom', $parameters, $clonedObj, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0)
$error ++;
}
}
if (! $notrigger && empty($error))
{
// Call trigger
$clonedObj->old_copy=$this;
$result = $clonedObj->call_trigger('CONTRACT_CLONE', $user);
if ($result < 0) {
$error ++;
}
// End call triggers
}
unset($this->context['createfromclone']);
// End
if (! $error) {
$this->db->commit();
return $clonedObj->id;
} else {
$this->db->rollback();
return - 1;
}
}
} }