diff --git a/htdocs/includes/triggers/interface_fraisport.class.php-NORUN b/htdocs/includes/triggers/interface_fraisport.class.php-NORUN new file mode 100644 index 00000000000..1bb6fd394ed --- /dev/null +++ b/htdocs/includes/triggers/interface_fraisport.class.php-NORUN @@ -0,0 +1,273 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + * $Source$ + */ + +/** + \file htdocs/includes/triggers/interface_demo.class.php + \ingroup core + \brief Fichier de demo de personalisation des actions du workflow + \remarks Son propre fichier d'actions peut etre créé par recopie de celui-ci: + - Le nom du fichier doit etre interface_xxx.class.php + - Le fichier doit rester stocké dans includes/triggers + - Le nom de la classe doit etre InterfaceXxx +*/ + + +/** + \class InterfaceEditeur + \brief Classe des fonctions triggers des actions personalisées du workflow +*/ + +class InterfaceFraisport +{ + var $db; + var $error; + + /** + * \brief Constructeur. + * \param DB Handler d'accès base + */ + function InterfaceFraisport($DB) + { + $this->db = $DB ; + + $this->name = "Frais de port"; + $this->family = "facture"; + $this->description = "Les triggers de ce composant calculent les frais de port."; + $this->revision = explode(' ','$Revision$'); + $this->version = $this->revision[1]; + } + + /** + * \brief Renvoi nom du lot de triggers + * \return string Nom du lot de triggers + */ + function getName() + { + return $this->name; + } + + /** + * \brief Renvoi descriptif du lot de triggers + * \return string Descriptif du lot de triggers + */ + function getDesc() + { + return $this->description; + } + /** + * \brief Renvoi version du lot de triggers + * \return string Version du lot de triggers + */ + function getVersion() + { + global $langs; + $langs->load("admin"); + + if ($this->version == 'experimental') return $langs->trans("Experimental"); + elseif ($this->version == 'dolibarr') return DOL_VERSION; + elseif ($this->version) return $this->version; + else return $langs->trans("Unknown"); + } + + /** + * \brief Fonction appelée lors du déclenchement d'un évènement Dolibarr. + * D'autres fonctions run_trigger peuvent etre présentes dans includes/triggers + * \param action Code de l'evenement + * \param object Objet concern + * \param user Objet user + * \param lang Objet lang + * \param conf Objet conf + * \return int <0 si ko, 0 si aucune action faite, >0 si ok + */ + function run_trigger($action,$object,$user,$langs,$conf) + { + if ($action == 'LINEORDER_INSERT') + { + return $this->_add_line_haulier($action,$object,$user,$langs,$conf); + } + if ($action == 'LINEORDER_DELETE') + { + return $this->_add_line_haulier($action,$object,$user,$langs,$conf); + } + } + + + function _add_line_haulier($action,$object,$user,$langs,$conf) + { + // Calcul les frais de port automatiquement + // + // + if (1 == 1) + { + dolibarr_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->fk_commande); + /* + * Calcul le poids du colis + * + */ + $sql = "SELECT p.weight, p.weight_units"; + $sql.= " FROM ".MAIN_DB_PREFIX."product as p,".MAIN_DB_PREFIX."commandedet as fd"; + $sql.= " WHERE fd.fk_product = p.rowid"; + $sql.= " AND fd.fk_commande = '".$object->fk_commande."'"; + + $resql = $this->db->query($sql) ; + + if ($resql) + { + $weights = array(); + $i = 0; + while ( $row = $this->db->fetch_row($resql) ) + { + $weights[$i] = $row[0]; + $weights_units[$i] = $row[1]; + $i++; + } + } + else + { + dolibarr_syslog("Trigger '".$this->name."' in action '$action' [2] SQL ERROR "); + } + /* + * + * + */ + $total = 0; + foreach($weights as $id => $weight) + { + $unite = $weights_units[$id]; + $w = weight_convert($weight, $unite, 0); + $total = $total + $w; + } + + /* + * Détecte si la ligne existe + * + */ + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."commandedet "; + $sql.= " WHERE fk_commande = '".$object->fk_commande."'"; + $sql.= " AND special_code = 1;"; + + $resql = $this->db->query($sql) ; + + if ($resql) + { + $exists = $this->db->num_rows($resql); + $this->db->free($resql); + } + else + { + dolibarr_syslog("Trigger '".$this->name."' in action '$action' [3] SQL ERROR $sql"); + } + /* + * Nombre de ligne de commande + * + */ + $sql = "SELECT MAX(rang) FROM ".MAIN_DB_PREFIX."commandedet"; + $sql.= " WHERE fk_commande=".$object->fk_commande.";"; + + $resql = $this->db->query($sql) ; + $num_lignes=0; + if ($resql) + { + while ( $row = $this->db->fetch_row($resql) ) + { + $num_lignes = $row[0]; + } + } + else + { + dolibarr_syslog("Trigger '".$this->name."' in action '$action' [4] SQL ERROR "); + } + /* + * Calcul les frais de port + * + */ + $desc = "Frais de port (".$total." kg)"; + $txtva = 19.6; + $rang = $num_lignes + 1; + + $total_ht = 1 * $total; + $total_tva = round($total_ht * $txtva / 100,2); + $total_ttc = $total_ht + $total_tva; + + if ($exists > 0) + { + // Mets à jour la ligne + $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet"; + $sql.= " SET description ='".addslashes($desc)."'"; + $sql.= ", rang='".$rang."'"; + $sql.= " WHERE fk_commande=".$object->fk_commande; + $sql.= " AND special_code=1;"; + + $resql = $this->db->query($sql) ; + + if ($resql) + { + + } + else + { + dolibarr_syslog("Trigger '".$this->name."' in action '$action' [5] SQL ERROR $sql"); + } + } + else + { + // Insertion dans base de la ligne + $sql = "INSERT INTO ".MAIN_DB_PREFIX."commandedet"; + $sql.= " (fk_commande, description, qty, tva_tx,"; + $sql.= " fk_product, remise_percent, subprice, price, remise, fk_remise_except,"; + $sql.= " coef,rang,info_bits, total_ht, total_tva, total_ttc,special_code)"; + $sql.= " VALUES (".$object->fk_commande.","; + $sql.= " '".addslashes($desc)."',"; + $sql.= " '1',"; + $sql.= " '".price2num($txtva)."',"; + $sql.= 'null,'; + $sql.= " '0',"; + $sql.= " '".price2num($total_ht)."',"; + $sql.= " '".price2num($total_ht)."',"; + $sql.= " 0,0,0,"; + $sql.= ' '.$rang.',0,'; + $sql.= " '".price2num($total_ht)."',"; + $sql.= " '".price2num($total_tva)."',"; + $sql.= " '".price2num($total_ttc)."'"; + $sql.= ",1);"; + + $resql = $this->db->query($sql) ; + + if ($resql) + { + + } + else + { + dolibarr_syslog("Trigger '".$this->name."' in action '$action' [5] SQL ERROR $sql"); + } + } + + } + else + { + dolibarr_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + return -1; + } + return 0; + } +} +?>