<?php

        global $conf, $db, $langs, $user, $hookmanager;

        if (empty($conf->global->DOLITOOLS_CHECK_ON_CLONE)) return;

        if (! in_array('propalcard',$hookmanager->contextarray) && ! in_array('ordercard',$hookmanager->contextarray) && ! in_array('invoicecard',$hookmanager->contextarray)) return;

        $langs->load('checkonclone@dolitools');

  		$element = $object->element;

		// TODO uniformiser
		if ($element == 'propale') $element = 'propal';

		if ( $element == 'propal' || $element=='commande' || $element == 'facture' )
		{
			if ($object->context['createfromclone'] == 'createfromclone')
			{
				// maj ref client
				$customer_ref =  GETPOST('customer_ref');

				$sql = 'UPDATE '.MAIN_DB_PREFIX.$element." SET ref_client='".$customer_ref."' WHERE rowid=".$object->id;
				dol_syslog(get_class($this)."::create sql=".$sql);

				$resql=$this->db->query($sql);
				if (! $resql)
				{
					$this->errors[] = $langs->trans("UpdateFailed");
					return -1;
				}

				// maj contact client
				$contactid =  GETPOST('contactid');
				$tab = $object->liste_type_contact('external', 'position', 1, 1);
				foreach ($tab as $key => $dummy) {
					$type_contact = $key;
					break;
				}
				$object->delete_linked_contact('external');
				$res = $object->add_contact($contactid, $type_contact, 'external');
				if ($res < 0)
				{
					$this->errors[] = $object->error;
					return -1;
				}

	if ($conf->visuals->enabled) { //TODO : move these extrafields into altapdf or dolitools or ???
				$object->fetch_optionals();
				$object->array_options['options_shipping_site'] = GETPOST('options_shipping_site');
				$object->array_options['options_manual_shipping_site'] = GETPOST('options_manual_shipping_site');
				$object->insertExtraFields();
	}
			}

			return 1;

		}
