/**
  * Function called when a Dolibarrr business event is done.
  * All functions "run_trigger" are triggered if file
  * is inside directory core/triggers
  *
  * 	@param		string		$action		Event action code
  * 	@param		Object		$object		Object
  * 	@param		User		$user		Object user
  * 	@param		Translate	$langs		Object langs
  * 	@param		conf		$conf		Object conf
  * 	@return		int						<0 if KO, 0 if no triggered ran, >0 if OK
  */
 public function run_trigger($action, $object, $user, $langs, $conf)
 {
     global $conf, $user, $db;
     // Put here code you want to execute when a Dolibarr business events occurs.
     // Data and type of action are stored into $object and $action
     // Users
     $db =& $this->db;
     //echo $action.'<br>';
     if ($action == 'PROPAL_VALIDATE' && $conf->global->PROPALEHISTORY_AUTO_ARCHIVE) {
         define('INC_FROM_DOLIBARR', true);
         dol_include_once("/propalehistory/config.php");
         dol_include_once("/comm/propal/class/propal.class.php");
         dol_include_once('/propalehistory/class/propaleHist.class.php');
         if (isset($_REQUEST['mesg'])) {
             setEventMessage($_REQUEST['mesg']);
         }
         $ATMdb = new TPDOdb();
         TPropaleHist::archiverPropale($ATMdb, $object);
         dol_syslog("Trigger '" . $this->name . "' for action '{$action}' launched by " . __FILE__ . ". id=" . $object->id);
     }
     return 0;
 }
    function doActions($parameters, &$object, &$action, $hookmanager)
    {
        global $langs, $db, $user;
        define('INC_FROM_DOLIBARR', true);
        dol_include_once("/propalehistory/config.php");
        dol_include_once("/comm/propal/class/propal.class.php");
        dol_include_once('/propalehistory/class/propaleHist.class.php');
        if (isset($_REQUEST['mesg'])) {
            setEventMessage($_REQUEST['mesg']);
        }
        $ATMdb = new TPDOdb();
        if ($_REQUEST['action'] == 'delete') {
            global $db;
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "propale_history";
            $sql .= " WHERE fk_propale = " . $_REQUEST['id'];
            $resql = $db->query($sql);
        }
        if (isset($_REQUEST['actionATM'])) {
            $actionATM = $_REQUEST['actionATM'];
        } else {
            $actionATM = '';
        }
        if ($actionATM == 'viewVersion') {
            $version = new TPropaleHist();
            $version->load($ATMdb, $_REQUEST['idVersion']);
            $propal = $version->getObject();
            //pre($propal,true);
            $object = new PropalHist($db, $object->socid);
            foreach ($propal as $k => $v) {
                $object->{$k} = $v;
            }
            foreach ($object->lines as &$line) {
                $line->description = $line->desc;
                $line->db = $db;
                //$line->fetch_optionals();
            }
            //pre($object,true);
            $object->id = $_REQUEST['id'];
            $object->db = $db;
        } elseif ($actionATM == 'createVersion') {
            TPropaleHist::archiverPropale($ATMdb, $object);
        } elseif ($actionATM == 'restaurer') {
            TPropaleHist::restaurerPropale($ATMdb, $object);
        } elseif ($actionATM == 'supprimer') {
            $version = new TPropaleHist();
            $version->load($ATMdb, $_REQUEST['idVersion']);
            $version->delete($ATMdb);
            ?>
				<script language="javascript">
					document.location.href="<?php 
            echo $_SERVER['PHP_SELF'];
            ?>
?id=<?php 
            echo $_REQUEST['id'];
            ?>
&mesg=<?php 
            echo $langs->transnoentities('HistoryVersionSuccessfullDelete');
            ?>
";
				</script>
			<?php 
        }
    }
 /**
  * Function called when module is enabled.
  * The init function add constants, boxes, permissions and menus
  * (defined in constructor) into Dolibarr database.
  * It also creates data directories
  *
  * 	@param		string	$options	Options when enabling module ('', 'noboxes')
  * 	@return		int					1 if OK, 0 if KO
  */
 public function init($options = '')
 {
     $sql = array();
     $result = $this->loadTables();
     define('INC_FROM_DOLIBARR', true);
     dol_include_once('/propalehistory/config.php');
     dol_include_once('/comm/propal/class/propal.class.php');
     // TODO don't think is needed, please remove it
     dol_include_once('/propalehistory/class/propaleHist.class.php');
     $PDOdb = new TPDOdb();
     $o = new TPropaleHist();
     $o->init_db_by_vars($PDOdb);
     return $this->_init($sql, $options);
 }
 static function restaurerPropale(&$ATMdb, &$object)
 {
     global $db, $user, $langs;
     $versionPropale = new TPropaleHist();
     $versionPropale->load($ATMdb, $_REQUEST['idVersion']);
     $propale = $versionPropale->getObject();
     $propale->statut = 0;
     $object->statut = 0;
     foreach ($object->lines as $line) {
         $object->deleteline($line->rowid) . "<br />";
     }
     foreach ($propale->lines as $line) {
         $object->addline($line->desc, $line->subprice, $line->qty, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, $line->fk_product, $line->remise_percent, 'HT', $line->subprice, $line->info_bits, $line->product_type, $line->rang, $line->special_code, $line->fk_parent_line, $line->fk_fournprice, $line->pa_ht, $line->label, $line->date_start, $line->date_end, $line->array_options);
     }
     $object->set_draft($user);
     // Pour pouvoir modifier les dates, le statut doit être à 0
     $object->set_availability($user, $propale->availability_id);
     $object->set_date($user, $propale->date);
     $object->set_date_livraison($user, $propale->date_livraison);
     $object->set_echeance($user, $propale->fin_validite);
     $object->set_ref_client($user, $propale->ref_client);
     $object->set_demand_reason($user, $propale->demand_reason_id);
     $object->setPaymentMethods($propale->mode_reglement_id);
     $object->setPaymentTerms($propale->cond_reglement_id);
     $object->valid($user);
     header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $_REQUEST['id'] . '&mesg=' . $langs->transnoentities('HistoryVersionSuccessfullRestored'));
 }