$modele = $_REQUEST['modele'];
 $expedition = new Expedition($db);
 $expedition->fetch($_REQUEST['expedition']);
 $expedition->fetch_lines();
 $TetiquettesVides = array();
 $Tetiquettes = array();
 //création des div vides
 for ($i = 0; $i < $nbVides; $i++) {
     $TetiquettesVides[$i] = array($i);
 }
 $TPDOdb = new TPDOdb();
 //création des div pleines
 foreach ($expedition->lines as $ligne) {
     $TPDOdb->Execute("SELECT rowid FROM " . MAIN_DB_PREFIX . "expeditiondet_asset WHERE fk_expeditiondet = " . $ligne->rowid);
     $TidExepeditiondetAsset = array();
     while ($TPDOdb->Get_line()) {
         $TidExepeditiondetAsset[] = $TPDOdb->Get_field('rowid');
     }
     foreach ($TidExepeditiondetAsset as $idExpeditiondetAsset) {
         $sql = "SELECT p.ref, p.label as nom, p.note as descritpion, eda.tare as tare, a.serial_number as code, a.lot_number as lot, eda.weight_reel as poids, eda.weight_reel_unit as poids_unit, eda.tare_unit as tare_unit\n\t\t\t\t\tFROM " . MAIN_DB_PREFIX . "expeditiondet_asset as eda\n\t\t\t\t\t\tLEFT JOIN " . MAIN_DB_PREFIX . "asset as a ON (a.rowid = eda.fk_asset)\n\t\t\t\t\t\tLEFT JOIN " . MAIN_DB_PREFIX . "product as p ON (p.rowid = a.fk_product)\n\t\t\t\t\tWHERE eda.rowid = " . $idExpeditiondetAsset;
         $TPDOdb->Execute($sql);
         $TPDOdb->Get_line();
         $tare_unit = _unit($TPDOdb->Get_field('tare_unit'));
         $poids_unit = _unit($TPDOdb->Get_field('poids_unit'));
         //On duplique l'étiquette autant de fois que demandé en paramètre
         for ($i = 0; $i < $nbCopies; $i++) {
             $Tetiquettes[] = array("ref" => $TPDOdb->Get_field('ref'), "nom" => $TPDOdb->Get_field('nom'), "description" => (int) $TPDOdb->Get_field('description') != 0 ? $TPDOdb->Get_field('description') : "", "tare" => number_format($TPDOdb->Get_field('tare'), 2, ',', ' '), "tare_unit" => $tare_unit, "code" => $TPDOdb->Get_field('code'), "lot" => $TPDOdb->Get_field('lot'), "poids" => number_format($TPDOdb->Get_field('poids'), 2, ',', ' '), "poids_unit" => $poids_unit);
         }
     }
 }
 if (!empty($_REQUEST['margetop'])) {
        
        
        
        $ideabox = $('<div id="ideabox_widget"><div class="titre center">Ajouter une idée dans une boîte</div>Boîte : <select id="ideabox_widget_select"></select><br /></div>');
        $ideabox.append('Titre : <br /><input id="ideabox_widget_label" type="text" id="ideabox_widget_label" placeholder="Titre de l\'idée" /><br />');
        $ideabox.append('Description : <br /><textarea id="ideabox_widget_description" cols="10" rows="2" type="text" placeholder="Description de l\'idée" /><br />');
        $ideabox.append('<div class="center" style="margin-top:5px;"><a id="ideabox_widget_valid" class="button" style="cursor:pointer;">Ajouter</a>&nbsp;'
                                + '<a id="ideabox_widget_cancel" class="button" style="cursor:pointer;">Annuler</a></div>');
                                
        $('body').append($ideabox);                                
    }
    
    var ideabox = new Array();
    
    <?php 
while ($PDOdb->Get_line()) {
    echo "ideabox.push({id : '" . $PDOdb->Get_field('rowid') . "', name : '" . addslashes($PDOdb->Get_field('label')) . "'});";
}
?>
    if(ideabox.length > 0)
    {
       openIdeaboxWidget();
       $.each(ideabox, function(key, value){
           $('#ideabox_widget_select').append('<option value="'+ value.id +'">'+ value.name +'</option>');
       });
    }
    
    $('#ideabox_widget_cancel').click(function(){
        $('#ideabox_widget_label').empty();
        $('#ideabox_widget_description').empty();
        
 /**
  * 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)
 {
     if ($action === 'ORDER_VALIDATE') {
         global $conf, $db;
         if ($conf->global->CREATE_OF_ON_ORDER_VALIDATE) {
             define('INC_FROM_DOLIBARR', true);
             dol_include_once('/product/class/product.class.php');
             dol_include_once('/of/config.php');
             dol_include_once('/of/class/ordre_fabrication_asset.class.php');
             $PDOdb = new TPDOdb();
             foreach ($object->lines as $line) {
                 // Uniquement si c'est un produit
                 if (!empty($line->fk_product) && $line->fk_product_type == 0) {
                     // On charge le produit pour vérifier son stock
                     $prod = new Product($db);
                     $prod->fetch($line->fk_product);
                     $prod->load_stock();
                     if ($prod->stock_reel < $line->qty) {
                         $assetOF = new TAssetOF();
                         $assetOF->fk_commande = $_REQUEST['id'];
                         $assetOF->fk_soc = $object->socid;
                         $assetOF->addLine($PDOdb, $line->fk_product, 'TO_MAKE', $line->qty);
                         $assetOF->save($PDOdb);
                     }
                 }
             }
         }
     } elseif ($action === 'ORDER_CANCEL') {
         if ($conf->global->DELETE_OF_ON_ORDER_CANCEL) {
             define('INC_FROM_DOLIBARR', true);
             dol_include_once('/of/config.php');
             dol_include_once('/of/class/ordre_fabrication_asset.class.php');
             $PDOdb = new TPDOdb();
             // On récupère les identifiants des of créés à partir de cette commande
             $TID_OF_command = TAssetOF::getTID_OF_command($_REQUEST['id']);
             foreach ($TID_OF_command as $id_of) {
                 $asset = new TAssetOF();
                 $asset->load($PDOdb, $id_of);
                 if ($asset->status == "DRAFT" || $asset->status == "VALID") {
                     $asset->delete($PDOdb);
                 }
             }
         }
     } elseif ($action === 'TASK_TIMESPENT_CREATE') {
         if ($conf->workstation->enabled) {
             define('INC_FROM_DOLIBARR', true);
             dol_include_once('/of/config.php');
             dol_include_once('/of/class/ordre_fabrication_asset.class.php');
             $PDOdb = new TPDOdb();
             $PDOdb->Execute("SELECT rowid \n\t\t\t\t\t\tFROM " . MAIN_DB_PREFIX . "asset_workstation_of \n\t\t\t\t\t\tWHERE fk_project_task=" . $object->id);
             if ($obj = $PDOdb->Get_line()) {
                 $wsof = new TAssetWorkstationOF();
                 $wsof->load($PDOdb, $obj->rowid);
                 $wsof->nb_hour_real = ($object->duration_effective + $object->timespent_duration) / 3600;
                 // Parce que Dolibarr mets le THM à jour après la création de la tâche :/
                 $sql = "UPDATE " . MAIN_DB_PREFIX . "projet_task_time";
                 $sql .= " SET thm = (SELECT thm FROM " . MAIN_DB_PREFIX . "user WHERE rowid = " . $object->timespent_fk_user . ")";
                 // set average hour rate of user
                 $sql .= " WHERE rowid = " . $object->timespent_id;
                 $object->db->query($sql);
                 $wsof->db =& $object->db;
                 $wsof->save($PDOdb);
             }
         }
     } elseif ($action === 'ORDERSUPPLIER_ADD_LIVRAISON') {
         global $db;
         if ($conf->of->enabled) {
             define('INC_FROM_DOLIBARR', true);
             dol_include_once('/of/config.php');
             dol_include_once('/of/class/ordre_fabrication_asset.class.php');
             $resql = $db->query('SELECT fk_statut FROM llx_commande_fournisseur WHERE rowid = ' . $_REQUEST['id']);
             $res = $db->fetch_object($resql);
             if ($res->fk_statut == 5) {
                 // La livraison est totale
                 //On cherche l'OF lié
                 $resql = $db->query("SELECT fk_source \n\t\t\t\t\t\t\t\t\t\t\tFROM " . MAIN_DB_PREFIX . "element_element \n\t\t\t\t\t\t\t\t\t\t\tWHERE fk_target = " . $_REQUEST['id'] . " \n\t\t\t\t\t\t\t\t\t\t\t\tAND sourcetype = 'ordre_fabrication' \n\t\t\t\t\t\t\t\t\t\t\t\tAND targettype = 'order_supplier'");
                 $res = $db->fetch_object($resql);
                 $id_of = $res->fk_source;
                 if ($id_of > 0) {
                     $of = new TAssetOF();
                     $of->load($PDOdb, $id_of);
                     if ($of->status != 'CLOSE') {
                         $of->closeOF($PDOdb);
                         setEventMessage($langs->trans('OFAttachedClosedAutomatically', '<a href="' . dol_buildpath('/of/fiche_of.php?id=' . $id_of, 2) . '">' . $of->numero . '</a>'));
                     }
                 }
             }
         }
     }
     return 0;
 }