diff --git a/htdocs/contrat/card.php b/htdocs/contrat/card.php index 69a1f55f2d65254708ce78f08d6296af60e8d8b2..e8b8a99041740e6c2f7db029bab1c70fa97512d8 100644 --- a/htdocs/contrat/card.php +++ b/htdocs/contrat/card.php @@ -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">'; } + // 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%">'; $linkback = '<a href="'.DOL_URL_ROOT.'/contrat/list.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>'; @@ -1964,6 +1992,11 @@ else if ($user->rights->facture->creer) print '<div class="inline-block divButAction"><a class="butAction" href="'.DOL_URL_ROOT.'/commande/card.php?action=create&origin='.$object->element.'&originid='.$object->id.'&socid='.$object->thirdparty->id.'">'.$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 . '&socid=' . $object->socid . '&action=clone&object=' . $object->element . '">' . $langs->trans("ToClone") . '</a></div>'; + } if ($object->nbofservicesclosed < $nbofservices) { diff --git a/htdocs/contrat/class/contrat.class.php b/htdocs/contrat/class/contrat.class.php index eb08bb52ba87eee19754f5cc35c9b6149da0e0e0..dd53828a638c0b8cfe72cab044ef9d3ee5110f77 100644 --- a/htdocs/contrat/class/contrat.class.php +++ b/htdocs/contrat/class/contrat.class.php @@ -56,7 +56,7 @@ class Contrat extends CommonObject * @var string */ var $ref_customer; - + /** * Supplier reference of the contract * @var string @@ -536,7 +536,7 @@ class Contrat extends CommonObject return -1; } } - + /** * Load a contract from database * @@ -904,7 +904,7 @@ class Contrat extends CommonObject } if (! $paramsok) return -1; - + $this->db->begin(); $now=dol_now(); @@ -969,7 +969,7 @@ class Contrat extends CommonObject } } } - + // Insert contacts commerciaux ('SALESREPSIGN','contrat') if (! $error) { @@ -996,8 +996,8 @@ class Contrat extends CommonObject { dol_print_error($this->db); $error++; - } - + } + if (! empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN)) { $originforcontact = $origin; @@ -1018,10 +1018,10 @@ class Contrat extends CommonObject } } } - + $sqlcontact = "SELECT ctc.code, ctc.source, ec.fk_socpeople FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as ctc"; $sqlcontact.= " WHERE element_id = ".$originidforcontact." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$originforcontact."'"; - + $resqlcontact = $this->db->query($sqlcontact); if ($resqlcontact) { @@ -1034,11 +1034,11 @@ class Contrat extends CommonObject } } else dol_print_error($resqlcontact); - } + } } } } - + if (! $error) { // Call trigger @@ -1381,7 +1381,7 @@ class Contrat extends CommonObject $localtaxes_type=getLocalTaxesFromRate($txtva, 0, $this->societe, $mysoc); $txtva = preg_replace('/\s*\(.*\)/','',$txtva); // Remove code into vatrate. - + $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1,$mysoc, $localtaxes_type); $total_ht = $tabprice[0]; $total_tva = $tabprice[1]; @@ -1404,9 +1404,9 @@ class Contrat extends CommonObject if (empty($pa_ht)) $pa_ht=0; - + // if buy price not defined, define buyprice as configured in margin admin - if ($this->pa_ht == 0) + if ($this->pa_ht == 0) { if (($result = $this->defineBuyPrice($pu_ht, $remise_percent, $fk_product)) < 0) { @@ -1565,7 +1565,7 @@ class Contrat extends CommonObject $localtaxes_type=getLocalTaxesFromRate($tvatx, 0, $this->societe, $mysoc); $tvatx = preg_replace('/\s*\(.*\)/','',$tvatx); // Remove code into vatrate. - + $tabprice=calcul_price_total($qty, $pu, $remise_percent, $tvatx, $localtax1tx, $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type); $total_ht = $tabprice[0]; $total_tva = $tabprice[1]; @@ -1589,7 +1589,7 @@ class Contrat extends CommonObject if (empty($pa_ht)) $pa_ht=0; // if buy price not defined, define buyprice as configured in margin admin - if ($this->pa_ht == 0) + if ($this->pa_ht == 0) { if (($result = $this->defineBuyPrice($pu_ht, $remise_percent)) < 0) { @@ -2263,6 +2263,119 @@ class Contrat extends CommonObject 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 + $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; + } + } } @@ -2273,7 +2386,7 @@ class ContratLigne extends CommonObjectLine { public $element='contratdet'; public $table_element='contratdet'; - + var $id; var $ref; var $tms; @@ -2291,10 +2404,10 @@ class ContratLigne extends CommonObjectLine public $libelle; var $description; - + var $product_ref; var $product_label; - + var $date_commande; var $date_ouverture_prevue; // date start planned var $date_ouverture; // date start real @@ -2435,7 +2548,7 @@ class ContratLigne extends CommonObjectLine $result=''; $label=$langs->trans("ShowContractOfService").': '.$this->label; if (empty($label)) $label=$this->description; - + $link = '<a href="'.DOL_URL_ROOT.'/contrat/card.php?id='.$this->fk_contrat.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">'; $linkend='</a>'; @@ -2631,7 +2744,7 @@ class ContratLigne extends CommonObjectLine if (empty($this->pa_ht)) $this->pa_ht=0; // if buy price not defined, define buyprice as configured in margin admin - if ($this->pa_ht == 0) + if ($this->pa_ht == 0) { if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {