Files
dolibarr/htdocs/includes/triggers/interface_modCommande_Fraisport.class.php-NORUN
2009-04-16 18:13:52 +00:00

269 lines
7.0 KiB
Plaintext

<?php
/* Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
*
* 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.
*/
/**
\file htdocs/includes/triggers/interface_modCommande_fraisport.class.php
\ingroup core
\brief Fichier trigger pour ajout frais port
\version $Id$
*/
include_once(DOL_DOCUMENT_ROOT.'/lib/functions2.lib.php');
/**
\class InterfaceFraisport
\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 = eregi_replace('Interface','',get_class($this));
$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_carrier($action,$object,$user,$langs,$conf);
}
if ($action == 'LINEORDER_DELETE')
{
return $this->_add_line_carrier($action,$object,$user,$langs,$conf);
}
// Renvoi 0 car aucune action de faite
return 0;
}
function _add_line_carrier($action,$object,$user,$langs,$conf)
{
// Ajoute une ligne de frais de port automatiquement
//
//
if ($conf->global->COMMANDE_ADD_DELIVERY_COST_LINE)
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->fk_commande);
/*
* Calcul le poids du colis
*
*/
$sql = "SELECT (p.weight*fd.qty), 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
{
dol_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
{
dol_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
{
dol_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;
// Tout a 0 tant que l'on ne sait pas calculer
$total_ht = 0;
$total_tva = 0;
$total_ttc = 0;
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)
{
return 2;
}
else
{
dol_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.= " 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,";
$sql.= ' '.$rang.',0,';
$sql.= " '".price2num($total_ht)."',";
$sql.= " '".price2num($total_tva)."',";
$sql.= " '".price2num($total_ttc)."'";
$sql.= ",1);";
$resql = $this->db->query($sql) ;
if ($resql)
{
return 1;
}
else
{
dol_syslog("Trigger '".$this->name."' in action '$action' [5] SQL ERROR $sql");
}
}
}
else
{
// Aucune action car l'option est desactivee
return 0;
}
}
}
?>