/**
  *	\brief     	Insert line in database
  *	\param      notrigger		1 no triggers
  *	\return		int				<0 if KO, >0 if OK
  */
 function insert($notrigger = 0)
 {
     global $langs, $user, $conf;
     dol_syslog("TicketLigne::Insert rang=" . $this->rang, LOG_DEBUG);
     // Clean parameters
     $this->desc = trim($this->desc);
     if (empty($this->tva_tx)) {
         $this->tva_tx = 0;
     }
     if (empty($this->localtax1_tx)) {
         $this->localtax1_tx = 0;
     }
     if (empty($this->localtax2_tx)) {
         $this->localtax2_tx = 0;
     }
     if (empty($this->total_localtax1)) {
         $this->total_localtax1 = 0;
     }
     if (empty($this->total_localtax2)) {
         $this->total_localtax2 = 0;
     }
     if (empty($this->rang)) {
         $this->rang = 0;
     }
     if (empty($this->remise)) {
         $this->remise = 0;
     }
     if (empty($this->remise_percent)) {
         $this->remise_percent = 0;
     }
     if (empty($this->info_bits)) {
         $this->info_bits = 0;
     }
     if (empty($this->subprice)) {
         $this->subprice = 0;
     }
     if (empty($this->price)) {
         $this->price = 0;
     }
     //if (empty($this->special_code)) $this->special_code=0;
     //if (empty($this->fk_parent_line)) $this->fk_parent_line=0;
     // Check parameters
     if ($this->product_type < 0) {
         return -1;
     }
     $this->db->begin();
     // Insertion dans base de la ligne
     $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'pos_ticketdet';
     $sql .= ' (fk_ticket, fk_parent_line, description, qty, tva_tx, localtax1_tx, localtax2_tx,';
     $sql .= ' fk_product, product_type, remise_percent, subprice, price, remise, fk_remise_except,';
     $sql .= ' date_start, date_end, fk_code_ventilation, fk_export_compta, ';
     $sql .= ' rang,';
     $sql .= ' info_bits, total_ht, total_tva, total_ttc, total_localtax1, total_localtax2, note)';
     $sql .= " VALUES (" . $this->fk_ticket . ",";
     $sql .= " " . ($this->fk_parent_line > 0 ? "'" . $this->fk_parent_line . "'" : "null") . ",";
     $sql .= " '" . $this->db->escape($this->desc) . "',";
     $sql .= " " . price2num($this->qty) . ",";
     $sql .= " " . price2num($this->tva_tx) . ",";
     $sql .= " " . price2num($this->localtax1_tx) . ",";
     $sql .= " " . price2num($this->localtax2_tx) . ",";
     if ($this->fk_product) {
         $sql .= "'" . $this->fk_product . "',";
     } else {
         $sql .= 'null,';
     }
     $sql .= " " . $this->product_type . ",";
     $sql .= " " . price2num($this->remise_percent) . ",";
     $sql .= " " . price2num($this->subprice) . ",";
     $sql .= " " . price2num($this->price) . ",";
     $sql .= " " . ($this->remise ? price2num($this->remise) : '0') . ",";
     // Deprecated
     if ($this->fk_remise_except) {
         $sql .= $this->fk_remise_except . ",";
     } else {
         $sql .= 'null,';
     }
     if ($this->date_start) {
         $sql .= "'" . $this->db->idate($this->date_start) . "',";
     } else {
         $sql .= 'null,';
     }
     if ($this->date_end) {
         $sql .= "'" . $this->db->idate($this->date_end) . "',";
     } else {
         $sql .= 'null,';
     }
     $sql .= ' ' . $this->fk_code_ventilation . ',';
     $sql .= ' ' . $this->fk_export_compta . ',';
     $sql .= ' ' . $this->rang . ',';
     //$sql.= ' '.$this->special_code.',';
     $sql .= " '" . $this->info_bits . "',";
     $sql .= " " . price2num($this->total_ht) . ",";
     $sql .= " " . price2num($this->total_tva) . ",";
     $sql .= " " . price2num($this->total_ttc) . ",";
     $sql .= " " . price2num($this->total_localtax1) . ",";
     $sql .= " " . price2num($this->total_localtax2) . ",";
     $sql .= $this->note ? "'" . $this->db->escape($this->note) . "'" : "null";
     $sql .= ')';
     dol_syslog("TicketLigne::insert sql=" . $sql);
     $resql = $this->db->query($sql);
     if ($resql) {
         $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX . 'pos_ticketdet');
         // Si fk_remise_except defini, on lie la remise a la ticket
         // ce qui la flague comme "consommee".
         if ($this->fk_remise_except) {
             $discount = new DiscountAbsolute($this->db);
             $result = $discount->fetch($this->fk_remise_except);
             if ($result >= 0) {
                 // Check if discount was found
                 if ($result > 0) {
                     // Check if discount not already affected to another ticket
                     if ($discount->fk_ticket) {
                         $this->error = $langs->trans("ErrorDiscountAlreadyUsed", $discount->id);
                         dol_syslog("TicketLigne::insert Error " . $this->error, LOG_ERR);
                         $this->db->rollback();
                         return -3;
                     } else {
                         $result = $discount->link_to_ticket($this->rowid, 0);
                         if ($result < 0) {
                             $this->error = $discount->error;
                             dol_syslog("TicketLigne::insert Error " . $this->error, LOG_ERR);
                             $this->db->rollback();
                             return -3;
                         }
                     }
                 } else {
                     $this->error = $langs->trans("ErrorADiscountThatHasBeenRemovedIsIncluded");
                     dol_syslog("TicketLigne::insert Error " . $this->error, LOG_ERR);
                     $this->db->rollback();
                     return -3;
                 }
             } else {
                 $this->error = $discount->error;
                 dol_syslog("TicketLigne::insert Error " . $this->error, LOG_ERR);
                 $this->db->rollback();
                 return -3;
             }
         }
         if (!$notrigger) {
             // Appel des triggers
             include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
             $interface = new Interfaces($this->db);
             $result = $interface->run_triggers('LINEBILL_INSERT', $this, $user, $langs, $conf);
             if ($result < 0) {
                 $error++;
                 $this->errors = $interface->errors;
             }
             // Fin appel triggers
         }
         $this->db->commit();
         return $this->rowid;
     } else {
         $this->error = $this->db->error();
         dol_syslog("TicketLigne::insert Error " . $this->error, LOG_ERR);
         $this->db->rollback();
         return -2;
     }
 }