2004-10-20 23:06:45 +02:00
< ? php
2006-12-06 16:24:54 +01:00
/* Copyright ( C ) 2002 - 2006 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2009-02-02 03:04:26 +01:00
* Copyright ( C ) 2004 - 2009 Laurent Destailleur < eldy @ users . sourceforge . net >
2004-11-09 16:00:38 +01:00
* Copyright ( C ) 2004 Eric Seigne < eric . seigne @ ryxeo . com >
* Copyright ( C ) 2003 Brian Fraval < brian @ fraval . org >
2006-03-16 02:03:55 +01:00
* Copyright ( C ) 2006 Andre Cianfarani < acianfa @ free . fr >
2009-04-27 22:37:50 +02:00
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis @ dolibarr . fr >
2009-09-14 00:24:46 +02:00
* Copyright ( C ) 2008 Patrick Raguin < patrick . raguin @ auguria . net >
2005-10-05 15:05:30 +02:00
*
2002-04-30 12:51:35 +02:00
* 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 .
*/
2004-12-31 18:29:28 +01:00
/**
2008-10-25 23:18:53 +02:00
* \file htdocs / societe . class . php
* \ingroup societe
2009-05-05 03:16:04 +02:00
* \brief File for third party class
2008-10-25 23:18:53 +02:00
* \version $Id $
2008-07-19 18:53:56 +02:00
*/
2008-02-24 15:41:07 +01:00
require_once ( DOL_DOCUMENT_ROOT . " /commonobject.class.php " );
2004-08-28 14:19:06 +02:00
2005-01-17 13:34:00 +01:00
/**
2008-10-25 23:18:53 +02:00
* \class Societe
2009-05-05 03:16:04 +02:00
* \brief Class to manage third parties objects ( customers , suppliers , prospects ... )
2008-07-19 18:53:56 +02:00
*/
2008-02-24 15:41:07 +01:00
class Societe extends CommonObject
2006-03-16 02:03:55 +01:00
{
var $db ;
2007-11-28 21:58:53 +01:00
var $error ;
var $errors = array ();
2008-07-19 18:53:56 +02:00
var $table_element = 'societe' ;
2009-10-04 20:28:24 +02:00
var $ismultientitymanaged = 1 ; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
2008-04-10 01:07:13 +02:00
2006-03-16 02:03:55 +01:00
var $id ;
var $nom ;
2009-01-23 16:26:55 +01:00
var $nom_particulier ;
2007-04-03 18:37:09 +02:00
var $prenom ;
var $particulier ;
2006-03-16 02:03:55 +01:00
var $adresse ;
var $cp ;
var $ville ;
var $departement_id ;
var $pays_id ;
var $pays_code ;
var $tel ;
var $fax ;
2006-06-10 21:03:50 +02:00
var $email ;
2006-03-16 02:03:55 +01:00
var $url ;
2008-08-28 01:55:24 +02:00
var $gencod ;
2007-02-10 14:43:49 +01:00
2007-04-03 18:37:09 +02:00
// 4 identifiants professionnels (leur utilisation depend du pays)
2007-02-10 14:43:49 +01:00
var $siren ; // IdProf1
var $siret ; // IdProf2
var $ape ; // IdProf3
var $idprof4 ; // IdProf4
2008-07-19 18:53:56 +02:00
2006-03-16 02:03:55 +01:00
var $prefix_comm ;
2008-07-19 18:53:56 +02:00
2006-03-16 02:03:55 +01:00
var $tva_assuj ;
var $tva_intra ;
2008-07-19 18:53:56 +02:00
2006-03-16 02:03:55 +01:00
var $capital ;
var $typent_id ;
2009-01-04 21:40:02 +01:00
var $typent_code ;
2006-03-16 02:03:55 +01:00
var $effectif_id ;
var $forme_juridique_code ;
var $forme_juridique ;
2008-07-19 18:53:56 +02:00
2006-03-16 02:03:55 +01:00
var $remise_client ;
2006-06-04 13:57:38 +02:00
var $mode_reglement ;
var $cond_reglement ;
2008-07-19 18:53:56 +02:00
2009-01-31 16:23:05 +01:00
var $client ; // 0=no customer, 1=customer, 2=prospect
var $prospect ; // 0=no prospect, 1=prospect
var $fournisseur ; // =0no supplier, 1=supplier
2008-07-19 18:53:56 +02:00
2007-11-07 00:30:38 +01:00
var $prefixCustomerIsRequired ;
var $prefixSupplierIsRequired ;
2006-03-16 02:03:55 +01:00
var $code_client ;
var $code_fournisseur ;
var $code_compta ;
var $code_compta_fournisseur ;
2008-07-19 18:53:56 +02:00
2006-03-16 02:03:55 +01:00
var $note ;
2008-07-19 18:53:56 +02:00
//! code statut prospect
var $stcomm_id ;
var $statut_commercial ;
var $price_level ;
var $commercial_id ; //Id du commercial affecte
2009-05-05 03:16:04 +02:00
var $import_key ;
2009-05-05 16:54:17 +02:00
2009-09-14 00:24:46 +02:00
var $logo ;
var $logo_small ;
var $logo_mini ;
2008-07-19 18:53:56 +02:00
/**
* \brief Constructeur de la classe
2009-07-08 15:56:42 +02:00
* \param DB handler acces base de donnees
2008-07-19 18:53:56 +02:00
* \param id id societe ( 0 par defaut )
*/
function Societe ( $DB , $id = 0 )
{
global $conf ;
$this -> db = $DB ;
$this -> id = $id ;
$this -> client = 0 ;
$this -> prospect = 0 ;
$this -> fournisseur = 0 ;
$this -> typent_id = 0 ;
$this -> effectif_id = 0 ;
$this -> forme_juridique_code = 0 ;
$this -> prefixCustomerIsRequired = 0 ;
$this -> prefixSupplierIsRequired = 0 ;
$this -> tva_assuj = 1 ;
return 1 ;
}
/**
* \brief Create third party in database
* \param user Object of user that ask creation
* \return int >= 0 if OK , < 0 if KO
*/
function create ( $user = '' )
{
global $langs , $conf ;
2009-01-23 16:26:55 +01:00
2009-03-25 20:12:35 +01:00
// Clean parameters
2008-07-19 18:53:56 +02:00
$this -> nom = trim ( $this -> nom );
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::create " . $this -> nom );
2008-07-19 18:53:56 +02:00
$this -> db -> begin ();
2009-07-06 09:58:26 +02:00
// For automatic creation during create action (not used by Dolibarr GUI, can be used by scripts)
2009-01-31 16:23:05 +01:00
if ( $this -> code_client == - 1 ) $this -> get_codeclient ( $this -> prefix_comm , 0 );
2008-07-19 18:53:56 +02:00
if ( $this -> code_fournisseur == - 1 ) $this -> get_codefournisseur ( $this -> prefix_comm , 1 );
2009-07-06 09:58:26 +02:00
// Check more parameters
2008-07-19 18:53:56 +02:00
$result = $this -> verify ();
if ( $result >= 0 )
{
2009-04-27 22:37:50 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " societe (nom, entity, datec, datea, fk_user_creat) " ;
$sql .= " VALUES (' " . addslashes ( $this -> nom ) . " ', " . $conf -> entity . " , " . $this -> db -> idate ( gmmktime ()) . " , " . $this -> db -> idate ( gmmktime ()) . " , " ;
2008-07-19 18:53:56 +02:00
$sql .= " " . ( $user -> id > 0 ? " ' " . $user -> id . " ' " : " null " );
$sql .= " ) " ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::create sql= " . $sql );
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " societe " );
$ret = $this -> update ( $this -> id , $user , 0 , 1 , 1 );
// si un commercial cree un client il lui est affecte automatiquement
if ( ! $user -> rights -> societe -> client -> voir )
{
$this -> add_commercial ( $user , $user -> id );
}
// Ajout du commercial affecte
else if ( $this -> commercial_id != '' && $this -> commercial_id != - 1 )
{
$this -> add_commercial ( $user , $this -> commercial_id );
}
2005-10-05 15:05:30 +02:00
2007-01-28 23:11:42 +01:00
// si le fournisseur est classe on l'ajoute
$this -> AddFournisseurInCategory ( $this -> fournisseur_categorie );
2006-12-06 16:24:54 +01:00
2008-07-19 18:53:56 +02:00
if ( $ret >= 0 )
{
$this -> use_webcal = ( $conf -> global -> PHPWEBCALENDAR_COMPANYCREATE == 'always' ? 1 : 0 );
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'COMPANY_CREATE' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// Fin appel triggers
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Create success id= " . $this -> id );
2008-07-19 18:53:56 +02:00
$this -> db -> commit ();
return 0 ;
}
else
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Create echec update " );
2008-07-19 18:53:56 +02:00
$this -> db -> rollback ();
return - 3 ;
}
}
else
{
if ( $this -> db -> errno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' )
{
$this -> error = $langs -> trans ( " ErrorCompanyNameAlreadyExists " , $this -> nom );
}
else
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Create echec insert sql= " . $sql );
2008-07-19 18:53:56 +02:00
}
$this -> db -> rollback ();
return - 2 ;
}
}
else
{
$this -> db -> rollback ();
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Create echec verify sql= " . $sql );
2008-07-19 18:53:56 +02:00
return - 1 ;
}
}
2004-08-28 14:19:06 +02:00
2006-11-17 03:44:10 +01:00
/**
2009-07-06 09:58:26 +02:00
* \brief Check properties of third party are ok ( like name , third party codes , ... )
2009-04-06 10:34:42 +02:00
* \return int 0 if OK , < 0 if KO
2008-07-19 18:53:56 +02:00
*/
2005-12-05 13:58:58 +01:00
function verify ()
{
2007-11-28 21:58:53 +01:00
$this -> errors = array ();
2008-07-19 18:53:56 +02:00
2005-12-05 13:58:58 +01:00
$result = 0 ;
2007-01-29 23:41:35 +01:00
$this -> nom = trim ( $this -> nom );
2005-12-05 13:58:58 +01:00
if ( ! $this -> nom )
{
2007-11-28 21:58:53 +01:00
$this -> errors [] = 'ErrorBadThirdPartyName' ;
2005-12-05 13:58:58 +01:00
$result = - 2 ;
}
2009-04-29 20:02:50 +02:00
2007-01-29 23:41:35 +01:00
if ( $this -> client && $this -> codeclient_modifiable ())
2005-12-05 13:58:58 +01:00
{
2007-11-28 21:58:53 +01:00
// On ne verifie le code client que si la societe est un client / prospect et que le code est modifiable
// Si il n'est pas modifiable il n'est pas mis a jour lors de l'update
2007-01-28 23:11:42 +01:00
$rescode = $this -> check_codeclient ();
2005-12-05 13:58:58 +01:00
if ( $rescode <> 0 )
{
if ( $rescode == - 1 )
{
2007-11-28 21:58:53 +01:00
$this -> errors [] = 'ErrorBadCustomerCodeSyntax' ;
2005-12-05 13:58:58 +01:00
}
if ( $rescode == - 2 )
{
2007-11-28 21:58:53 +01:00
$this -> errors [] = 'ErrorCustomerCodeRequired' ;
2005-12-05 13:58:58 +01:00
}
if ( $rescode == - 3 )
{
2007-11-28 21:58:53 +01:00
$this -> errors [] = 'ErrorCustomerCodeAlreadyUsed' ;
2005-12-05 13:58:58 +01:00
}
2007-11-06 22:30:56 +01:00
if ( $rescode == - 4 )
{
2007-11-28 21:58:53 +01:00
$this -> errors [] = 'ErrorPrefixRequired' ;
2007-11-06 22:30:56 +01:00
}
2005-12-05 13:58:58 +01:00
$result = - 3 ;
}
}
2009-04-29 20:02:50 +02:00
2007-01-29 23:41:35 +01:00
if ( $this -> fournisseur && $this -> codefournisseur_modifiable ())
2007-01-28 23:11:42 +01:00
{
2007-11-28 21:58:53 +01:00
// On ne verifie le code fournisseur que si la societe est un fournisseur et que le code est modifiable
// Si il n'est pas modifiable il n'est pas mis a jour lors de l'update
2007-01-28 23:11:42 +01:00
$rescode = $this -> check_codefournisseur ();
if ( $rescode <> 0 )
{
if ( $rescode == - 1 )
{
2007-11-28 21:58:53 +01:00
$this -> errors [] = 'ErrorBadSupplierCodeSyntax' ;
2007-01-28 23:11:42 +01:00
}
if ( $rescode == - 2 )
{
2007-11-28 21:58:53 +01:00
$this -> errors [] = 'ErrorSupplierCodeRequired' ;
2007-01-28 23:11:42 +01:00
}
if ( $rescode == - 3 )
{
2007-11-28 21:58:53 +01:00
$this -> errors [] = 'ErrorSupplierCodeAlreadyUsed' ;
2007-01-28 23:11:42 +01:00
}
2007-11-07 00:30:38 +01:00
if ( $rescode == - 5 )
2007-11-06 22:30:56 +01:00
{
2007-11-28 21:58:53 +01:00
$this -> errors [] = 'ErrorprefixRequired' ;
2007-11-06 22:30:56 +01:00
}
2007-01-28 23:11:42 +01:00
$result = - 3 ;
}
2008-07-19 18:53:56 +02:00
}
2009-04-29 20:02:50 +02:00
2005-12-05 13:58:58 +01:00
return $result ;
}
2004-10-08 10:04:20 +02:00
2008-07-19 18:53:56 +02:00
/**
* \brief Update parameters of third party
* \param id id societe
* \param user Utilisateur qui demande la mise a jour
* \param call_trigger 0 = non , 1 = oui
2007-02-10 14:43:49 +01:00
* \param allowmodcodeclient Autorise modif code client
* \param allowmodcodefournisseur Autorise modif code fournisseur
2008-07-19 18:53:56 +02:00
* \return int < 0 si ko , >= 0 si ok
*/
function update ( $id , $user = '' , $call_trigger = 1 , $allowmodcodeclient = 0 , $allowmodcodefournisseur = 0 )
{
2009-04-06 10:34:42 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /lib/functions2.lib.php " );
2009-04-29 20:02:50 +02:00
2008-07-19 18:53:56 +02:00
global $langs , $conf ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Update id= " . $id . " call_trigger= " . $call_triger . " allowmodcodeclient= " . $allowmodcodeclient . " allowmodcodefournisseur= " . $allowmodcodefournisseur );
2008-07-19 18:53:56 +02:00
// Clean parameters
$this -> id = $id ;
$this -> capital = trim ( $this -> capital );
$this -> nom = trim ( $this -> nom );
$this -> adresse = trim ( $this -> adresse );
$this -> cp = trim ( $this -> cp );
$this -> ville = trim ( $this -> ville );
$this -> departement_id = trim ( $this -> departement_id );
$this -> pays_id = trim ( $this -> pays_id );
$this -> tel = trim ( $this -> tel );
$this -> fax = trim ( $this -> fax );
2009-10-21 20:14:00 +02:00
$this -> tel = preg_replace ( " / \ s/ " , " " , $this -> tel );
$this -> tel = preg_replace ( " / \ ./ " , " " , $this -> tel );
$this -> fax = preg_replace ( " / \ s/ " , " " , $this -> fax );
$this -> fax = preg_replace ( " / \ ./ " , " " , $this -> fax );
2008-07-19 18:53:56 +02:00
$this -> email = trim ( $this -> email );
$this -> url = $this -> url ? clean_url ( $this -> url , 0 ) : '' ;
$this -> siren = trim ( $this -> siren );
$this -> siret = trim ( $this -> siret );
$this -> ape = trim ( $this -> ape );
$this -> idprof4 = trim ( $this -> idprof4 );
$this -> prefix_comm = trim ( $this -> prefix_comm );
$this -> tva_assuj = trim ( $this -> tva_assuj );
2009-04-29 20:02:50 +02:00
$this -> tva_intra = dol_sanitizeFileName ( $this -> tva_intra , '' );
2008-07-19 18:53:56 +02:00
$this -> capital = trim ( $this -> capital );
if ( strlen ( $this -> capital ) == 0 ) $this -> capital = 0 ;
$this -> effectif_id = trim ( $this -> effectif_id );
$this -> forme_juridique_code = trim ( $this -> forme_juridique_code );
2009-01-23 16:26:55 +01:00
2008-08-28 01:55:24 +02:00
//Gencod
$this -> gencod = trim ( $this -> gencod );
2008-07-19 18:53:56 +02:00
2009-01-31 16:23:05 +01:00
// For automatic creation (not used by Dolibarr)
2008-07-19 18:53:56 +02:00
if ( $this -> code_client == - 1 ) $this -> get_codeclient ( $this -> prefix_comm , 0 );
if ( $this -> code_fournisseur == - 1 ) $this -> get_codefournisseur ( $this -> prefix_comm , 1 );
// Check name is required and codes are ok or unique.
2007-12-11 21:42:49 +01:00
// If error, this->errors[] is filled
$result = $this -> verify ();
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
if ( $result >= 0 )
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Update verify ok " );
2008-07-19 18:53:56 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe " ;
$sql .= " SET nom = ' " . addslashes ( $this -> nom ) . " ' " ; // Champ obligatoire
2008-08-27 20:53:27 +02:00
$sql .= " ,datea = " . $this -> db -> idate ( mktime ());
2008-07-19 18:53:56 +02:00
$sql .= " ,address = ' " . addslashes ( $this -> adresse ) . " ' " ;
$sql .= " ,cp = " . ( $this -> cp ? " ' " . $this -> cp . " ' " : " null " );
$sql .= " ,ville = " . ( $this -> ville ? " ' " . addslashes ( $this -> ville ) . " ' " : " null " );
$sql .= " ,fk_departement = ' " . ( $this -> departement_id ? $this -> departement_id : '0' ) . " ' " ;
$sql .= " ,fk_pays = ' " . ( $this -> pays_id ? $this -> pays_id : '0' ) . " ' " ;
$sql .= " ,tel = " . ( $this -> tel ? " ' " . addslashes ( $this -> tel ) . " ' " : " null " );
$sql .= " ,fax = " . ( $this -> fax ? " ' " . addslashes ( $this -> fax ) . " ' " : " null " );
$sql .= " ,email = " . ( $this -> email ? " ' " . addslashes ( $this -> email ) . " ' " : " null " );
$sql .= " ,url = " . ( $this -> url ? " ' " . addslashes ( $this -> url ) . " ' " : " null " );
$sql .= " ,siren = ' " . addslashes ( $this -> siren ) . " ' " ;
$sql .= " ,siret = ' " . addslashes ( $this -> siret ) . " ' " ;
$sql .= " ,ape = ' " . addslashes ( $this -> ape ) . " ' " ;
$sql .= " ,idprof4 = ' " . addslashes ( $this -> idprof4 ) . " ' " ;
2007-12-11 21:42:49 +01:00
$sql .= " ,tva_assuj = " . ( $this -> tva_assuj >= 0 ? " ' " . $this -> tva_assuj . " ' " : " null " );
2008-07-19 18:53:56 +02:00
$sql .= " ,tva_intra = ' " . addslashes ( $this -> tva_intra ) . " ' " ;
$sql .= " ,capital = ' " . addslashes ( $this -> capital ) . " ' " ;
2009-01-23 16:26:55 +01:00
2008-07-19 18:53:56 +02:00
$sql .= " ,prefix_comm = " . ( $this -> prefix_comm ? " ' " . addslashes ( $this -> prefix_comm ) . " ' " : " null " );
$sql .= " ,fk_effectif = " . ( $this -> effectif_id ? " ' " . $this -> effectif_id . " ' " : " null " );
$sql .= " ,fk_typent = " . ( $this -> typent_id ? " ' " . $this -> typent_id . " ' " : " 0 " );
$sql .= " ,fk_forme_juridique = " . ( $this -> forme_juridique_code ? " ' " . $this -> forme_juridique_code . " ' " : " null " );
$sql .= " ,client = " . $this -> client ;
$sql .= " ,fournisseur = " . $this -> fournisseur ;
2008-08-28 01:55:24 +02:00
$sql .= " ,gencod = " . ( $this -> gencod ? " ' " . $this -> gencod . " ' " : " null " );
2009-01-23 16:26:55 +01:00
2008-07-19 18:53:56 +02:00
if ( $allowmodcodeclient )
{
2009-01-31 16:23:05 +01:00
//$this->check_codeclient();
2008-07-19 18:53:56 +02:00
$sql .= " , code_client = " . ( $this -> code_client ? " ' " . addslashes ( $this -> code_client ) . " ' " : " null " );
// Attention get_codecompta peut modifier le code suivant le module utilise
$this -> get_codecompta ( 'customer' );
$sql .= " , code_compta = " . ( $this -> code_compta ? " ' " . addslashes ( $this -> code_compta ) . " ' " : " null " );
}
if ( $allowmodcodefournisseur )
{
2009-01-31 16:23:05 +01:00
//$this->check_codefournisseur();
2008-07-19 18:53:56 +02:00
$sql .= " , code_fournisseur = " . ( $this -> code_fournisseur ? " ' " . addslashes ( $this -> code_fournisseur ) . " ' " : " null " );
// Attention get_codecompta peut modifier le code suivant le module utilise
$this -> get_codecompta ( 'supplier' );
$sql .= " , code_compta_fournisseur = " . ( $this -> code_compta_fournisseur ? " ' " . addslashes ( $this -> code_compta_fournisseur ) . " ' " : " null " );
}
$sql .= " , fk_user_modif = " . ( $user -> id > 0 ? " ' " . $user -> id . " ' " : " null " );
$sql .= " WHERE rowid = ' " . $id . " ' " ;
2009-01-23 16:26:55 +01:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::update sql= " . $sql );
2008-07-19 18:53:56 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2008-04-21 14:47:23 +02:00
// Si le fournisseur est classe on l'ajoute
$this -> AddFournisseurInCategory ( $this -> fournisseur_categorie );
2006-12-10 14:22:36 +01:00
2008-07-19 18:53:56 +02:00
if ( $call_trigger )
{
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'COMPANY_MODIFY' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// Fin appel triggers
}
$result = 1 ;
}
else
{
if ( $this -> db -> errno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' )
{
// Doublon
$this -> error = $langs -> trans ( " ErrorDuplicateField " );
$result = - 1 ;
}
else
{
$this -> error = $langs -> trans ( " Error sql= " . $sql );
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Update echec sql= " . $sql );
2008-07-19 18:53:56 +02:00
$result = - 2 ;
}
}
}
return $result ;
}
/**
* \brief Load a third party from database into memory
* \param socid Id third party to load
2008-07-28 00:58:45 +02:00
* \param user User object
2009-05-05 03:16:04 +02:00
* \return int > 0 if OK , < 0 if KO
2008-07-19 18:53:56 +02:00
*/
function fetch ( $socid , $user = 0 )
{
2005-10-05 15:05:30 +02:00
global $langs ;
2006-02-03 17:42:39 +01:00
global $conf ;
2008-07-19 18:53:56 +02:00
2008-03-02 20:55:43 +01:00
// Init data for telephonie module
2008-05-16 13:05:45 +02:00
if ( $conf -> telephonie -> enabled && $user && $user -> id )
2005-10-05 15:05:30 +02:00
{
2008-03-02 20:55:43 +01:00
/* Lecture des permissions */
2005-10-05 15:05:30 +02:00
$sql = " SELECT p.pread, p.pwrite, p.pperms " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_perms as p " ;
$sql .= " WHERE p.fk_user = ' " . $user -> id . " ' " ;
$sql .= " AND p.fk_soc = ' " . $socid . " '; " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $row = $this -> db -> fetch_row ( $resql ))
{
$this -> perm_read = $row [ 0 ];
$this -> perm_write = $row [ 1 ];
$this -> perm_perms = $row [ 2 ];
}
}
}
2009-04-27 22:37:50 +02:00
$sql = 'SELECT s.rowid, s.nom, s.entity, s.address,' . $this -> db -> pdate ( 's.datec' ) . ' as dc, s.prefix_comm' ;
2009-02-15 18:33:02 +01:00
$sql .= ', s.price_level' ;
2005-10-05 15:05:30 +02:00
$sql .= ',' . $this -> db -> pdate ( 's.tms' ) . ' as date_update' ;
2008-03-02 20:55:43 +01:00
$sql .= ', s.tel, s.fax, s.email, s.url, s.cp, s.ville, s.note, s.client, s.fournisseur' ;
2007-02-10 14:43:49 +01:00
$sql .= ', s.siren, s.siret, s.ape, s.idprof4' ;
2009-02-01 15:40:06 +01:00
$sql .= ', s.capital, s.tva_intra' ;
2005-10-05 15:05:30 +02:00
$sql .= ', s.fk_typent as typent_id' ;
2008-03-02 20:55:43 +01:00
$sql .= ', s.fk_effectif as effectif_id' ;
$sql .= ', s.fk_forme_juridique as forme_juridique_code' ;
2008-08-28 01:55:24 +02:00
$sql .= ', s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur, s.parent, s.gencod' ;
2006-02-20 17:29:55 +01:00
$sql .= ', s.fk_departement, s.fk_pays, s.fk_stcomm, s.remise_client, s.mode_reglement, s.cond_reglement, s.tva_assuj' ;
2008-03-02 20:55:43 +01:00
$sql .= ', s.fk_prospectlevel' ;
2009-05-05 03:16:04 +02:00
$sql .= ', s.import_key' ;
2008-03-02 20:55:43 +01:00
$sql .= ', fj.libelle as forme_juridique' ;
$sql .= ', e.libelle as effectif' ;
2005-10-05 15:05:30 +02:00
$sql .= ', p.code as pays_code, p.libelle as pays' ;
$sql .= ', d.code_departement as departement_code, d.nom as departement' ;
$sql .= ', st.libelle as stcomm' ;
2007-07-24 00:57:21 +02:00
$sql .= ', te.code as typent_code' ;
2005-10-05 15:05:30 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'societe as s' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_effectif as e ON s.fk_effectif = e.id' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_pays as p ON s.fk_pays = p.rowid' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_stcomm as st ON s.fk_stcomm = st.id' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_forme_juridique as fj ON s.fk_forme_juridique = fj.code' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_departements as d ON s.fk_departement = d.rowid' ;
2007-07-24 00:57:21 +02:00
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_typent as te ON s.fk_typent = te.id' ;
2007-06-12 00:51:47 +02:00
$sql .= ' WHERE s.rowid = ' . $socid ;
2009-02-15 18:33:02 +01:00
2005-10-05 15:05:30 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
2007-06-12 00:51:47 +02:00
$this -> id = $obj -> rowid ;
2008-02-24 15:41:07 +01:00
$this -> ref = $obj -> rowid ;
2009-04-27 22:37:50 +02:00
$this -> entity = $obj -> entity ;
2005-10-05 15:05:30 +02:00
$this -> date_update = $obj -> date_update ;
2008-11-06 02:06:45 +01:00
$this -> nom = $obj -> nom ;
$this -> adresse = $obj -> address ;
2005-10-05 15:05:30 +02:00
$this -> cp = $obj -> cp ;
2008-11-06 02:06:45 +01:00
$this -> ville = $obj -> ville ;
$this -> adresse_full = $obj -> address . " \n " . $obj -> cp . ' ' . $obj -> ville ;
2005-10-05 15:05:30 +02:00
$this -> pays_id = $obj -> fk_pays ;
$this -> pays_code = $obj -> fk_pays ? $obj -> pays_code : '' ;
2008-11-06 02:06:45 +01:00
$this -> pays = $obj -> fk_pays ? ( $langs -> trans ( 'Country' . $obj -> pays_code ) != 'Country' . $obj -> pays_code ? $langs -> trans ( 'Country' . $obj -> pays_code ) : $obj -> pays ) : '' ;
2005-10-05 15:05:30 +02:00
$this -> departement_id = $obj -> fk_departement ;
$this -> departement = $obj -> fk_departement ? $obj -> departement : '' ;
$transcode = $langs -> trans ( 'StatusProspect' . $obj -> fk_stcomm );
$libelle = ( $transcode != 'StatusProspect' . $obj -> fk_stcomm ? $transcode : $obj -> stcomm );
$this -> stcomm_id = $obj -> fk_stcomm ; // id statut commercial
$this -> statut_commercial = $libelle ; // libelle statut commercial
2006-06-10 21:03:50 +02:00
$this -> email = $obj -> email ;
2005-10-05 15:05:30 +02:00
$this -> url = $obj -> url ;
$this -> tel = $obj -> tel ;
$this -> fax = $obj -> fax ;
$this -> parent = $obj -> parent ;
$this -> siren = $obj -> siren ;
$this -> siret = $obj -> siret ;
$this -> ape = $obj -> ape ;
2007-02-10 14:43:49 +01:00
$this -> idprof4 = $obj -> idprof4 ;
2005-10-05 15:05:30 +02:00
$this -> capital = $obj -> capital ;
$this -> code_client = $obj -> code_client ;
$this -> code_fournisseur = $obj -> code_fournisseur ;
$this -> code_compta = $obj -> code_compta ;
$this -> code_compta_fournisseur = $obj -> code_compta_fournisseur ;
2008-08-28 01:55:24 +02:00
$this -> gencod = $obj -> gencod ;
2006-03-16 02:03:55 +01:00
$this -> tva_assuj = $obj -> tva_assuj ;
2005-10-05 15:05:30 +02:00
$this -> tva_intra = $obj -> tva_intra ;
$this -> tva_intra_code = substr ( $obj -> tva_intra , 0 , 2 );
$this -> tva_intra_num = substr ( $obj -> tva_intra , 2 );
$this -> typent_id = $obj -> typent_id ;
2007-07-24 00:57:21 +02:00
$this -> typent_code = $obj -> typent_code ;
2008-07-19 18:53:56 +02:00
2005-10-05 15:05:30 +02:00
$this -> effectif_id = $obj -> effectif_id ;
$this -> effectif = $obj -> effectif_id ? $obj -> effectif : '' ;
$this -> forme_juridique_code = $obj -> forme_juridique_code ;
$this -> forme_juridique = $obj -> forme_juridique_code ? $obj -> forme_juridique : '' ;
2008-03-02 20:55:43 +01:00
$this -> fk_prospectlevel = $obj -> fk_prospectlevel ;
2005-10-05 15:05:30 +02:00
$this -> prefix_comm = $obj -> prefix_comm ;
$this -> remise_client = $obj -> remise_client ;
2006-02-14 12:11:15 +01:00
$this -> mode_reglement = $obj -> mode_reglement ;
2006-02-14 15:47:57 +01:00
$this -> cond_reglement = $obj -> cond_reglement ;
2005-10-05 15:05:30 +02:00
$this -> client = $obj -> client ;
$this -> fournisseur = $obj -> fournisseur ;
$this -> note = $obj -> note ;
2006-02-03 17:42:39 +01:00
// multiprix
2009-01-10 02:11:17 +01:00
$this -> price_level = $obj -> price_level ;
2005-10-05 15:05:30 +02:00
2009-05-05 03:16:04 +02:00
$this -> import_key = $obj -> import_key ;
2009-05-05 16:54:17 +02:00
2005-10-05 15:05:30 +02:00
$result = 1 ;
}
else
{
2009-02-20 23:53:15 +01:00
dol_syslog ( 'Erreur Societe::Fetch aucune societe avec id=' . $this -> id . ' - ' . $sql );
2005-10-05 15:05:30 +02:00
$this -> error = 'Erreur Societe::Fetch aucune societe avec id=' . $this -> id . ' - ' . $sql ;
$result = - 2 ;
}
$this -> db -> free ( $resql );
}
else
{
2009-05-05 16:54:17 +02:00
dol_syslog ( 'Erreur Societe::Fetch ' . $this -> db -> error (), LOG_ERR );
2005-10-05 15:05:30 +02:00
$this -> error = $this -> db -> error ();
$result = - 3 ;
}
2009-06-19 09:32:39 +02:00
// Use first price level if level not defined for third party
if ( $conf -> global -> PRODUIT_MULTIPRICES && empty ( $this -> price_level )) $this -> price_level = 1 ;
2005-10-05 15:05:30 +02:00
return $result ;
}
2008-07-19 18:53:56 +02:00
/**
*
* Lit une adresse de livraison
*
*/
function fetch_adresse_livraison ( $id )
{
global $conf , $langs ;
$sql = " SELECT l.rowid, l.label, l.fk_societe, l.nom, l.address, l.cp " ;
$sql .= " , " . $this -> db -> pdate ( " l.tms " ) . " as dm, " . $this -> db -> pdate ( " l.datec " ) . " as dc " ;
$sql .= " , l.ville, l.fk_pays, l.note, l.tel, l.fax " ;
$sql .= " , p.libelle as pays, p.code as pays_code, s.nom as socname " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_adresse_livraison as l " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_pays as p ON l.fk_pays = p.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON l.fk_societe = s.rowid " ;
$sql .= " WHERE l.rowid = " . $id ;
$result = $this -> db -> query ( $sql ) ;
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
$this -> datec = $obj -> dc ;
$this -> datem = $obj -> dm ;
$this -> label = $obj -> label ;
$this -> socid = $obj -> fk_societe ;
$this -> societe = $obj -> socname ;
$this -> nom = $obj -> nom ;
$this -> address = $obj -> address ;
$this -> adresse = $obj -> address ; //Todo: uniformiser le nom des champs
$this -> cp = $obj -> cp ;
$this -> ville = $obj -> ville ;
$this -> pays_id = $obj -> fk_pays ;
$this -> pays_code = $obj -> fk_pays ? $obj -> pays_code : '' ;
2008-11-06 02:06:45 +01:00
$this -> pays = $obj -> fk_pays ? ( $langs -> trans ( 'Country' . $obj -> pays_code ) != 'Country' . $obj -> pays_code ? $langs -> trans ( 'Country' . $obj -> pays_code ) : $obj -> pays ) : '' ;
2008-07-19 18:53:56 +02:00
$this -> tel = $obj -> tel ;
$this -> fax = $obj -> fax ;
$this -> note = $obj -> note ;
$this -> db -> free ( $result );
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
/**
2009-04-27 22:37:50 +02:00
* \brief Suppression d ' une societe de la base avec ses dependances ( contacts , rib ... )
* \param id id de la societe a supprimer
2008-07-19 18:53:56 +02:00
*/
function delete ( $id )
{
2008-04-10 01:07:13 +02:00
global $user , $langs , $conf ;
2008-07-19 18:53:56 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Delete " , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
$sqr = 0 ;
2005-10-05 15:05:30 +02:00
2008-04-22 20:02:55 +02:00
// Check if third party can be deleted
2009-01-23 16:26:55 +01:00
$nbpropal = 0 ;
$sql = " SELECT COUNT(*) as nb from " . MAIN_DB_PREFIX . " propal " ;
$sql .= " WHERE fk_soc = " . $id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$nbpropal = $obj -> nb ;
if ( $nbpropal > 0 )
{
$this -> error = " ErrorRecordHasChildren " ;
return - 1 ;
}
}
else
{
$this -> error .= $this -> db -> lasterror ();
2009-03-12 23:49:05 +01:00
dol_syslog ( " Societe::Delete erreur -1 " . $this -> error , LOG_ERR );
2009-01-23 16:26:55 +01:00
return - 1 ;
}
2008-07-19 18:53:56 +02:00
2008-10-01 21:37:54 +02:00
if ( $this -> db -> begin ())
2008-07-19 18:53:56 +02:00
{
2008-10-01 21:37:54 +02:00
// Added by Matelli (see http://matelli.fr/showcases/patchs-dolibarr/fix-third-party-deleting.html)
// Removing every "categorie" link with this company
require_once ( DOL_DOCUMENT_ROOT . " /categories/categorie.class.php " );
2009-01-23 16:26:55 +01:00
2008-10-01 21:37:54 +02:00
$static_cat = new Categorie ( $this -> db );
$toute_categs = array ();
2009-01-23 16:26:55 +01:00
2008-10-01 21:37:54 +02:00
// Fill $toute_categs array with an array of (type => array of ("Categorie" instance))
if ( $this -> client || $this -> prospect )
{
$toute_categs [ 'societe' ] = $static_cat -> containing ( $this -> id , 'societe' , 2 );
}
if ( $this -> fournisseur )
2009-01-23 16:26:55 +01:00
{
2008-10-01 21:37:54 +02:00
$toute_categs [ 'fournisseur' ] = $static_cat -> containing ( $this -> id , 'fournisseur' , 1 );
}
2009-01-23 16:26:55 +01:00
2008-10-01 21:37:54 +02:00
// Remove each "Categorie"
foreach ( $toute_categs as $type => $categs_type )
{
foreach ( $categs_type as $cat )
{
$cat -> del_type ( $this , $type );
}
}
2009-01-23 16:26:55 +01:00
2008-10-01 21:37:54 +02:00
// Remove contacts
2008-07-19 18:53:56 +02:00
$sql = " DELETE from " . MAIN_DB_PREFIX . " socpeople " ;
$sql .= " WHERE fk_soc = " . $id ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Delete sql= " . $sql , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
if ( $this -> db -> query ( $sql ))
{
$sqr ++ ;
}
else
{
$this -> error .= $this -> db -> lasterror ();
2009-03-12 23:49:05 +01:00
dol_syslog ( " Societe::Delete erreur -1 " . $this -> error , LOG_ERR );
2008-07-19 18:53:56 +02:00
}
2009-02-02 03:04:26 +01:00
// Update link in member table
$sql = " UPDATE " . MAIN_DB_PREFIX . " adherent " ;
$sql .= " SET fk_soc = NULL where fk_soc = " . $id ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Delete sql= " . $sql , LOG_DEBUG );
2009-02-02 03:04:26 +01:00
if ( $this -> db -> query ( $sql ))
{
$sqr ++ ;
}
else
{
$this -> error .= $this -> db -> lasterror ();
2009-03-12 23:49:05 +01:00
dol_syslog ( " Societe::Delete erreur -1 " . $this -> error , LOG_ERR );
2009-02-02 03:04:26 +01:00
}
2009-02-15 18:33:02 +01:00
2009-02-02 03:04:26 +01:00
// Remove ban
2008-07-19 18:53:56 +02:00
$sql = " DELETE from " . MAIN_DB_PREFIX . " societe_rib " ;
$sql .= " WHERE fk_soc = " . $id ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Delete sql= " . $sql , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
if ( $this -> db -> query ( $sql ))
{
$sqr ++ ;
}
else
{
$this -> error = $this -> db -> lasterror ();
2009-03-12 23:49:05 +01:00
dol_syslog ( " Societe::Delete erreur -2 " . $this -> error , LOG_ERR );
2008-07-19 18:53:56 +02:00
}
2009-02-02 03:04:26 +01:00
// Remove third party
2008-07-19 18:53:56 +02:00
$sql = " DELETE from " . MAIN_DB_PREFIX . " societe " ;
$sql .= " WHERE rowid = " . $id ;
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::Delete sql= " . $sql , LOG_DEBUG );
2008-07-19 18:53:56 +02:00
if ( $this -> db -> query ( $sql ))
{
$sqr ++ ;
}
else
{
$this -> error = $this -> db -> lasterror ();
2009-03-12 23:49:05 +01:00
dol_syslog ( " Societe::Delete erreur -3 " . $this -> error , LOG_ERR );
2008-07-19 18:53:56 +02:00
}
2009-02-02 03:04:26 +01:00
if ( $sqr == 4 )
2008-07-19 18:53:56 +02:00
{
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'COMPANY_DELETE' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// Fin appel triggers
$this -> db -> commit ();
2008-11-17 01:02:57 +01:00
// Suppression du repertoire document
2008-07-19 18:53:56 +02:00
$docdir = $conf -> societe -> dir_output . " / " . $id ;
if ( file_exists ( $docdir ))
{
dol_delete_dir_recursive ( $docdir );
}
return 0 ;
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
}
}
/**
2009-04-27 22:37:50 +02:00
* \brief Retournes les factures impayees de la societe
* \return array tableau des id de factures impayees
2008-07-19 18:53:56 +02:00
*
*/
function factures_impayes ()
{
$facimp = array ();
/*
* Lignes
*/
$sql = " SELECT f.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f WHERE f.fk_soc = ' " . $this -> id . " ' " ;
$sql .= " AND f.fk_statut = '1' AND f.paye = '0' " ;
if ( $this -> db -> query ( $sql ))
{
$num = $this -> db -> num_rows ();
$i = 0 ;
while ( $i < $num )
2004-12-15 11:35:59 +01:00
{
2008-07-19 18:53:56 +02:00
$objp = $this -> db -> fetch_object ();
$array_push ( $facimp , $objp -> rowid );
$i ++ ;
print $i ;
2004-12-15 11:35:59 +01:00
}
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
$this -> db -> free ();
}
return $facimp ;
}
2004-08-28 14:19:06 +02:00
2006-06-24 21:47:36 +02:00
/**
2009-04-27 22:37:50 +02:00
* \brief Attribut le prefix de la societe en base
2006-06-24 21:47:36 +02:00
*
*/
function attribute_prefix ()
{
2009-04-27 22:37:50 +02:00
global $conf ;
2009-04-29 20:02:50 +02:00
2007-06-12 00:51:47 +02:00
$sql = " SELECT nom FROM " . MAIN_DB_PREFIX . " societe WHERE rowid = ' " . $this -> id . " ' " ;
2006-06-24 21:47:36 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
$nom = preg_replace ( " /[[:punct:]]/ " , " " , $obj -> nom );
$this -> db -> free ();
2008-07-19 18:53:56 +02:00
2006-06-24 21:47:36 +02:00
$prefix = $this -> genprefix ( $nom , 4 );
2008-07-19 18:53:56 +02:00
2009-04-27 22:37:50 +02:00
$sql = " SELECT count(*) as nb FROM " . MAIN_DB_PREFIX . " societe " ;
$sql .= " WHERE prefix_comm = ' " . $prefix . " ' " ;
$sql .= " AND entity = " . $conf -> entity ;
2009-04-29 20:02:50 +02:00
2006-06-24 21:47:36 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$this -> db -> free ( $resql );
if ( ! $obj -> nb )
{
2007-06-12 00:51:47 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe set prefix_comm=' " . $prefix . " ' WHERE rowid=' " . $this -> id . " ' " ;
2008-07-19 18:53:56 +02:00
2006-06-24 21:47:36 +02:00
if ( $this -> db -> query ( $sql ) )
{
2008-07-19 18:53:56 +02:00
2006-06-24 21:47:36 +02:00
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2006-06-24 21:47:36 +02:00
}
}
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2006-06-24 21:47:36 +02:00
}
}
}
2004-12-15 11:35:59 +01:00
else
2006-06-24 21:47:36 +02:00
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2006-06-24 21:47:36 +02:00
}
return $prefix ;
}
2005-10-05 15:05:30 +02:00
2006-06-24 21:47:36 +02:00
/**
2009-04-27 22:37:50 +02:00
* \brief Genere le prefix de la societe
* \param nom nom de la societe
* \param taille taille du prefix a retourner
* \param mot l ' indice du mot a utiliser
2006-06-24 21:47:36 +02:00
*/
function genprefix ( $nom , $taille = 4 , $mot = 0 )
2004-09-11 12:02:40 +02:00
{
2006-06-24 21:47:36 +02:00
$retour = " " ;
$tab = explode ( " " , $nom );
if ( $mot < count ( $tab ))
{
$prefix = strtoupper ( substr ( $tab [ $mot ], 0 , $taille ));
2009-04-29 20:02:50 +02:00
2009-04-27 22:37:50 +02:00
// On verifie que ce prefix n'a pas deja ete pris ...
$sql = " SELECT count(*) as nb FROM " . MAIN_DB_PREFIX . " societe " ;
$sql .= " WHERE prefix_comm = ' " . $prefix . " ' " ;
$sql .= " AND entity = " . $conf -> entity ;
2009-04-29 20:02:50 +02:00
2006-06-24 21:47:36 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj -> nb )
{
$this -> db -> free ();
$retour = $this -> genprefix ( $nom , $taille , $mot + 1 );
}
else
{
$retour = $prefix ;
}
}
}
return $retour ;
2004-09-11 12:02:40 +02:00
}
2008-07-19 18:53:56 +02:00
/**
2008-10-24 22:16:36 +02:00
* \brief Define third party as a customer
* \return int < 0 if KO , > 0 if OK
2008-07-19 18:53:56 +02:00
*/
function set_as_client ()
{
if ( $this -> id )
{
2008-10-24 22:16:36 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe " ;
$sql .= " SET client = 1 " ;
$sql .= " WHERE rowid = " . $this -> id ;
2008-07-19 18:53:56 +02:00
2008-10-24 22:16:36 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) return 1 ;
else return - 1 ;
2008-07-19 18:53:56 +02:00
}
2008-10-24 22:16:36 +02:00
return 0 ;
2008-07-19 18:53:56 +02:00
}
2004-08-28 14:19:06 +02:00
2006-05-21 02:33:21 +02:00
/**
2009-07-08 15:56:42 +02:00
* \brief Definit la societe comme un client
2006-05-21 02:33:21 +02:00
* \param remise Valeur en % de la remise
* \param note Note / Motif de modification de la remise
2009-07-08 15:56:42 +02:00
* \param user Utilisateur qui definie la remise
2006-05-21 02:33:21 +02:00
* \return int < 0 si ko , > 0 si ok
*/
function set_remise_client ( $remise , $note , $user )
{
global $langs ;
2008-07-19 18:53:56 +02:00
2006-05-21 02:33:21 +02:00
// Nettoyage parametres
$note = trim ( $note );
if ( ! $note )
{
$this -> error = $langs -> trans ( " ErrorFieldRequired " , $langs -> trans ( " Note " ));
return - 2 ;
}
2004-08-20 11:11:28 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::set_remise_client " . $remise . " , " . $note . " , " . $user -> id );
2005-10-05 15:05:30 +02:00
2006-05-21 02:33:21 +02:00
if ( $this -> id )
{
$this -> db -> begin ();
2009-01-23 16:26:55 +01:00
2006-05-21 02:33:21 +02:00
// Positionne remise courante
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe " ;
$sql .= " SET remise_client = ' " . $remise . " ' " ;
2007-06-12 00:51:47 +02:00
$sql .= " WHERE rowid = " . $this -> id . " ; " ;
2006-05-21 02:33:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 1 ;
}
2008-07-19 18:53:56 +02:00
2006-05-21 02:33:21 +02:00
// Ecrit trace dans historique des remises
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " societe_remise " ;
$sql .= " (datec, fk_soc, remise_client, note, fk_user_author) " ;
2008-08-27 20:53:27 +02:00
$sql .= " VALUES ( " . $this -> db -> idate ( mktime ()) . " , " . $this -> id . " , ' " . $remise . " ', " ;
2006-05-21 02:33:21 +02:00
$sql .= " ' " . addslashes ( $note ) . " ', " ;
$sql .= " " . $user -> id ;
$sql .= " ) " ;
2004-08-20 11:11:28 +02:00
2006-05-21 02:33:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 1 ;
}
2008-07-19 18:53:56 +02:00
2006-05-21 02:33:21 +02:00
$this -> db -> commit ();
return 1 ;
}
}
2005-10-01 17:57:05 +02:00
2006-04-02 14:15:56 +02:00
/**
2009-08-07 02:49:43 +02:00
* \brief Add a discount for third party
2006-04-02 14:15:56 +02:00
* \param remise Montant de la remise
* \param user Utilisateur qui accorde la remise
* \param desc Motif de l ' avoir
2006-06-03 13:32:51 +02:00
* \return int < 0 si ko , id de la ligne de remise si ok
2006-04-02 14:15:56 +02:00
*/
2007-04-03 01:45:50 +02:00
function set_remise_except ( $remise , $user , $desc , $tva_tx = 0 )
2006-04-02 14:15:56 +02:00
{
2006-06-03 13:32:51 +02:00
global $langs ;
2008-07-19 18:53:56 +02:00
2006-06-03 13:32:51 +02:00
// Nettoyage des parametres
$remise = price2num ( $remise );
$desc = trim ( $desc );
2008-07-19 18:53:56 +02:00
2007-12-02 20:50:58 +01:00
// Check parameters
2006-06-03 13:32:51 +02:00
if ( ! $remise > 0 )
{
$this -> error = $langs -> trans ( " ErrorWrongValueForParameter " , " 1 " );
return - 1 ;
}
if ( ! $desc )
{
$this -> error = $langs -> trans ( " ErrorWrongValueForParameter " , " 3 " );
return - 2 ;
}
2008-07-19 18:53:56 +02:00
2006-04-02 14:15:56 +02:00
if ( $this -> id )
{
2007-09-09 13:16:33 +02:00
require_once ( DOL_DOCUMENT_ROOT . '/discount.class.php' );
2007-03-25 05:07:46 +02:00
$discount = new DiscountAbsolute ( $this -> db );
$discount -> fk_soc = $this -> id ;
2007-07-05 00:07:53 +02:00
$discount -> amount_ht = price2num ( $remise , 'MT' );
$discount -> amount_tva = price2num ( $remise * $tva_tx / 100 , 'MT' );
$discount -> amount_ttc = price2num ( $discount -> amount_ht + $discount -> amount_tva , 'MT' );
$discount -> tva_tx = price2num ( $tva_tx , 'MT' );
2009-08-07 02:49:43 +02:00
$discount -> description = $desc ;
2007-03-25 05:07:46 +02:00
$result = $discount -> create ( $user );
if ( $result > 0 )
2006-04-02 14:15:56 +02:00
{
2007-03-25 05:07:46 +02:00
return $result ;
2006-04-02 14:15:56 +02:00
}
else
{
2007-03-25 05:07:46 +02:00
$this -> error = $discount -> error ;
return - 3 ;
2006-04-02 14:15:56 +02:00
}
}
else return 0 ;
}
2005-10-05 15:05:30 +02:00
2006-04-02 20:57:50 +02:00
/**
2008-08-09 00:12:27 +02:00
* \brief Renvoie montant TTC des reductions / avoirs en cours disponibles de la societe
2007-11-26 03:54:16 +01:00
* \param user Filtre sur un user auteur des remises
* \param filter Filtre autre
2008-08-09 00:12:27 +02:00
* \param maxvalue Filter on max value for discount
2007-12-02 20:50:58 +01:00
* \return int < 0 if KO , Credit note amount otherwise
2006-04-02 20:57:50 +02:00
*/
2008-08-09 00:12:27 +02:00
function getAvailableDiscounts ( $user = '' , $filter = '' , $maxvalue = 0 )
2006-04-02 20:57:50 +02:00
{
2007-12-02 20:50:58 +01:00
require_once ( DOL_DOCUMENT_ROOT . '/discount.class.php' );
2008-07-19 18:53:56 +02:00
$discountstatic = new DiscountAbsolute ( $this -> db );
2008-08-09 00:12:27 +02:00
$result = $discountstatic -> getAvailableDiscounts ( $this , $user , $filter , $maxvalue );
2007-12-02 20:50:58 +01:00
if ( $result >= 0 )
{
return $result ;
}
else
{
2007-12-02 21:55:23 +01:00
$this -> error = $discountstatic -> error ;
2007-12-02 20:50:58 +01:00
return - 1 ;
}
2006-04-02 20:57:50 +02:00
}
2009-01-23 16:26:55 +01:00
2008-08-09 00:12:27 +02:00
/**
* Enter description here ...
*
* @ param unknown_type $price_level
* @ param unknown_type $user
*/
2006-04-02 14:15:56 +02:00
function set_price_level ( $price_level , $user )
2006-04-02 20:57:50 +02:00
{
2008-07-19 18:53:56 +02:00
if ( $this -> id )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe " ;
$sql .= " SET price_level = ' " . $price_level . " ' " ;
$sql .= " WHERE rowid = " . $this -> id . " ; " ;
2006-02-03 17:42:39 +01:00
2008-07-19 18:53:56 +02:00
$this -> db -> query ( $sql );
2006-02-03 17:42:39 +01:00
2008-07-19 18:53:56 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " societe_prices " ;
$sql .= " ( datec, fk_soc, price_level, fk_user_author ) " ;
2008-08-27 20:53:27 +02:00
$sql .= " VALUES ( " . $this -> db -> idate ( mktime ()) . " , " . $this -> id . " ,' " . $price_level . " ', " . $user -> id . " ) " ;
2006-02-03 17:42:39 +01:00
2008-07-19 18:53:56 +02:00
if ( ! $this -> db -> query ( $sql ) )
2006-02-03 17:42:39 +01:00
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2006-02-03 17:42:39 +01:00
}
2008-07-19 18:53:56 +02:00
}
}
2005-10-01 17:57:05 +02:00
2008-07-19 18:53:56 +02:00
/**
*
*
*/
function add_commercial ( $user , $commid )
{
if ( $this -> id > 0 && $commid > 0 )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " societe_commerciaux " ;
2009-04-27 22:37:50 +02:00
$sql .= " WHERE fk_soc = " . $this -> id . " AND fk_user = " . $commid ;
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
$this -> db -> query ( $sql );
2005-01-25 13:25:49 +01:00
2008-07-19 18:53:56 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " societe_commerciaux " ;
$sql .= " ( fk_soc, fk_user ) " ;
$sql .= " VALUES ( " . $this -> id . " , " . $commid . " ) " ;
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
if ( ! $this -> db -> query ( $sql ) )
2005-01-25 13:25:49 +01:00
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::add_commercial Erreur " );
2005-01-25 13:25:49 +01:00
}
2008-07-19 18:53:56 +02:00
}
}
/**
*
*
*
*/
function del_commercial ( $user , $commid )
{
if ( $this -> id > 0 && $commid > 0 )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " societe_commerciaux " ;
2009-04-27 22:37:50 +02:00
$sql .= " WHERE fk_soc = " . $this -> id . " AND fk_user = " . $commid ;
2008-07-19 18:53:56 +02:00
if ( ! $this -> db -> query ( $sql ) )
2005-01-25 13:25:49 +01:00
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::del_commercial Erreur " );
2005-01-25 13:25:49 +01:00
}
2004-08-07 01:39:22 +02:00
2008-07-19 18:53:56 +02:00
}
}
2005-10-01 17:57:05 +02:00
2006-06-03 16:47:05 +02:00
/**
* \brief Renvoie nom clicable ( avec eventuellement le picto )
* \param withpicto Inclut le picto dans le lien
2006-06-03 16:59:59 +02:00
* \param option Sur quoi pointe le lien
2007-04-22 22:08:19 +02:00
* \param maxlen Longueur max libelle
2006-06-03 16:47:05 +02:00
* \return string Chaine avec URL
*/
2008-10-19 22:41:23 +02:00
function getNomUrl ( $withpicto = 0 , $option = 'customer' , $maxlen = 0 )
2006-06-03 16:47:05 +02:00
{
global $langs ;
2008-07-19 18:53:56 +02:00
2006-06-03 16:47:05 +02:00
$result = '' ;
2008-10-26 20:09:39 +01:00
$lien = $lienfin = '' ;
2008-07-19 18:53:56 +02:00
2008-10-19 22:41:23 +02:00
if ( $option == 'customer' )
2006-06-03 16:47:05 +02:00
{
2008-10-19 22:41:23 +02:00
if ( $this -> client == 1 )
{
$lien = '<a href="' . DOL_URL_ROOT . '/comm/fiche.php?socid=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
elseif ( $this -> client == 2 )
{
$lien = '<a href="' . DOL_URL_ROOT . '/comm/prospect/fiche.php?socid=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
2006-06-03 16:47:05 +02:00
}
2008-10-19 22:41:23 +02:00
if ( $option == 'supplier' )
2006-06-03 16:47:05 +02:00
{
2008-10-19 22:41:23 +02:00
$lien = '<a href="' . DOL_URL_ROOT . '/fourn/fiche.php?socid=' . $this -> id . '">' ;
2006-06-03 16:47:05 +02:00
$lienfin = '</a>' ;
}
2006-06-03 16:59:59 +02:00
if ( $option == 'compta' )
{
$lien = '<a href="' . DOL_URL_ROOT . '/compta/fiche.php?socid=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
2009-01-23 16:26:55 +01:00
2008-10-26 20:09:39 +01:00
if ( empty ( $lien ))
{
$lien = '<a href="' . DOL_URL_ROOT . '/soc.php?socid=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
2007-03-28 14:17:28 +02:00
if ( $withpicto ) $result .= ( $lien . img_object ( $langs -> trans ( " ShowCompany " ) . ': ' . $this -> nom , 'company' ) . $lienfin . ' ' );
2009-02-20 23:53:15 +01:00
$result .= $lien . ( $maxlen ? dol_trunc ( $this -> nom , $maxlen ) : $this -> nom ) . $lienfin ;
2008-10-26 20:09:39 +01:00
2006-06-03 16:47:05 +02:00
return $result ;
}
2008-07-19 18:53:56 +02:00
/**
* \brief Renvoie le nom d 'une societe a partir d' un id
* \param id id company we search for name
* \return string Third party name
*/
function get_nom ( $id )
{
2002-04-30 12:51:35 +02:00
2008-07-19 18:53:56 +02:00
$sql = " SELECT nom FROM " . MAIN_DB_PREFIX . " societe WHERE rowid=' " . $id . " '; " ;
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
$result = $this -> db -> query ( $sql );
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
if ( $result )
{
if ( $this -> db -> num_rows ())
{
$obj = $this -> db -> fetch_object ( $result );
return $obj -> nom ;
}
$this -> db -> free ();
}
else {
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
}
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
}
2004-08-07 01:39:22 +02:00
2005-10-05 15:05:30 +02:00
2006-11-29 23:03:28 +01:00
/**
2009-04-27 22:37:50 +02:00
* \brief Renvoie la liste des contacts emails existant pour la societe
2008-07-19 18:53:56 +02:00
* \return array tableau des contacts emails
*/
2006-11-29 23:03:28 +01:00
function thirdparty_and_contact_email_array ()
{
global $langs ;
2008-07-19 18:53:56 +02:00
2006-11-29 23:03:28 +01:00
$contact_email = $this -> contact_email_array ();
if ( $this -> email )
{
2007-12-03 22:40:46 +01:00
// TODO: Tester si email non deja present dans tableau contact
2009-02-20 23:53:15 +01:00
$contact_email [ - 1 ] = $langs -> trans ( " ThirdParty " ) . ': ' . dol_trunc ( $this -> nom , 16 ) . " < " . $this -> email . " > " ;
2006-11-29 23:03:28 +01:00
}
return $contact_email ;
}
2008-07-19 18:53:56 +02:00
2006-11-29 23:03:28 +01:00
/**
2009-04-27 22:37:50 +02:00
* \brief Renvoie la liste des contacts emails existant pour la societe
2008-07-19 18:53:56 +02:00
* \return array tableau des contacts emails
*/
2006-11-29 23:03:28 +01:00
function contact_email_array ()
{
$contact_email = array ();
2008-07-19 18:53:56 +02:00
2007-06-12 00:51:47 +02:00
$sql = " SELECT rowid, email, name, firstname " ;
2006-11-29 23:03:28 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople " ;
$sql .= " WHERE fk_soc = ' " . $this -> id . " ' " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$nump = $this -> db -> num_rows ( $resql );
if ( $nump )
{
$i = 0 ;
while ( $i < $nump )
{
$obj = $this -> db -> fetch_object ( $resql );
2007-06-12 00:51:47 +02:00
$contact_email [ $obj -> rowid ] = trim ( $obj -> firstname . " " . $obj -> name ) . " < " . $obj -> email . " > " ;
2006-11-29 23:03:28 +01:00
$i ++ ;
}
}
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2006-11-29 23:03:28 +01:00
}
return $contact_email ;
}
2008-07-19 18:53:56 +02:00
2006-11-29 23:03:28 +01:00
/**
2009-04-27 22:37:50 +02:00
* \brief Renvoie la liste des contacts de cette societe
2008-07-19 18:53:56 +02:00
* \return array tableau des contacts
*/
2006-11-29 23:03:28 +01:00
function contact_array ()
{
$contacts = array ();
2008-07-19 18:53:56 +02:00
2007-06-12 00:51:47 +02:00
$sql = " SELECT rowid, name, firstname FROM " . MAIN_DB_PREFIX . " socpeople WHERE fk_soc = ' " . $this -> id . " ' " ;
2006-11-29 23:03:28 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$nump = $this -> db -> num_rows ( $resql );
if ( $nump )
{
$i = 0 ;
while ( $i < $nump )
{
$obj = $this -> db -> fetch_object ( $resql );
2007-06-12 00:51:47 +02:00
$contacts [ $obj -> rowid ] = $obj -> firstname . " " . $obj -> name ;
2006-11-29 23:03:28 +01:00
$i ++ ;
}
}
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2006-11-29 23:03:28 +01:00
}
return $contacts ;
}
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
/**
* \brief Renvoie l 'email d' un contact depuis son id
* \param rowid id du contact
* \return string email du contact
*/
function contact_get_email ( $rowid )
{
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
$sql = " SELECT rowid, email, name, firstname FROM " . MAIN_DB_PREFIX . " socpeople WHERE rowid = ' " . $rowid . " ' " ;
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
if ( $this -> db -> query ( $sql ) )
{
$nump = $this -> db -> num_rows ();
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
if ( $nump )
{
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
$obj = $this -> db -> fetch_object ();
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
$contact_email = " $obj->firstname $obj->name < $obj->email > " ;
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
}
return $contact_email ;
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
}
2005-10-05 15:05:30 +02:00
2008-07-19 18:53:56 +02:00
}
2003-02-02 16:01:56 +01:00
2004-08-28 14:19:06 +02:00
2007-01-29 23:41:35 +01:00
/**
2008-07-19 18:53:56 +02:00
* \brief Affiche le rib
*/
2007-01-29 23:41:35 +01:00
function display_rib ()
{
global $langs ;
2005-10-05 15:05:30 +02:00
2007-01-29 23:41:35 +01:00
require_once DOL_DOCUMENT_ROOT . " /companybankaccount.class.php " ;
2005-10-05 15:05:30 +02:00
2008-11-24 20:17:09 +01:00
$bac = new CompanyBankAccount ( $this -> db );
$bac -> socid = $this -> id ;
$bac -> fetch ( $this -> id );
2004-09-13 10:17:48 +02:00
2007-01-29 23:41:35 +01:00
if ( $bac -> code_banque || $bac -> code_guichet || $bac -> number || $bac -> cle_rib )
{
2008-11-24 20:17:09 +01:00
$rib = $bac -> code_banque . " " . $bac -> code_guichet . " " . $bac -> number ;
$rib .= ( $bac -> cle_rib ? " ( " . $bac -> cle_rib . " ) " : " " );
2007-01-29 23:41:35 +01:00
}
else
{
$rib = $langs -> trans ( " NoRIB " );
}
return $rib ;
}
2004-09-21 17:05:40 +02:00
2008-11-24 20:17:09 +01:00
/**
* Load this -> bank_account attribut
*/
function load_ban ()
2007-01-29 23:41:35 +01:00
{
require_once DOL_DOCUMENT_ROOT . " /companybankaccount.class.php " ;
2005-10-05 15:05:30 +02:00
2007-01-29 23:41:35 +01:00
$bac = new CompanyBankAccount ( $this -> db , $this -> id );
2008-11-24 20:17:09 +01:00
$bac -> fetch ( $this -> id );
2004-09-21 17:05:40 +02:00
2007-01-29 23:41:35 +01:00
$this -> bank_account = $bac ;
return 1 ;
}
2004-09-13 10:17:48 +02:00
2004-12-31 18:29:28 +01:00
2007-01-29 23:41:35 +01:00
function verif_rib ()
{
2008-11-24 20:17:09 +01:00
$this -> load_ban ();
2007-01-29 23:41:35 +01:00
return $this -> bank_account -> verif ();
}
2008-07-19 18:53:56 +02:00
/**
2009-04-27 22:37:50 +02:00
* \brief Attribut un code client a partir du module de controle des codes .
2007-11-06 22:30:56 +01:00
* \return code_client Code client automatique
*/
function get_codeclient ( $objsoc = 0 , $type = 0 )
{
global $conf ;
if ( $conf -> global -> SOCIETE_CODECLIENT_ADDON )
{
require_once DOL_DOCUMENT_ROOT . '/includes/modules/societe/' . $conf -> global -> SOCIETE_CODECLIENT_ADDON . '.php' ;
$var = $conf -> global -> SOCIETE_CODECLIENT_ADDON ;
$mod = new $var ;
2009-01-23 16:26:55 +01:00
2007-11-06 22:30:56 +01:00
$this -> code_client = $mod -> getNextValue ( $objsoc , $type );
2007-11-07 00:30:38 +01:00
$this -> prefixCustomerIsRequired = $mod -> prefixIsRequired ;
2007-11-06 22:30:56 +01:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::get_codeclient code_client= " . $this -> code_client . " module= " . $var );
2007-11-06 22:30:56 +01:00
}
}
2008-07-19 18:53:56 +02:00
2007-11-06 22:30:56 +01:00
/**
2009-04-27 22:37:50 +02:00
* \brief Attribut un code fournisseur a partir du module de controle des codes .
2007-11-06 22:30:56 +01:00
* \return code_fournisseur Code fournisseur automatique
*/
function get_codefournisseur ( $objsoc = 0 , $type = 1 )
{
global $conf ;
if ( $conf -> global -> SOCIETE_CODEFOURNISSEUR_ADDON )
{
require_once DOL_DOCUMENT_ROOT . '/includes/modules/societe/' . $conf -> global -> SOCIETE_CODEFOURNISSEUR_ADDON . '.php' ;
$var = $conf -> global -> SOCIETE_CODEFOURNISSEUR_ADDON ;
$mod = new $var ;
2009-01-23 16:26:55 +01:00
2007-11-06 22:30:56 +01:00
$this -> code_fournisseur = $mod -> getNextValue ( $objsoc , $type );
2007-11-07 00:30:38 +01:00
$this -> prefixSupplierIsRequired = $mod -> prefixIsRequired ;
2009-01-23 16:26:55 +01:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::get_codefournisseur code_fournisseur= " . $this -> code_fournisseur . " module= " . $var );
2007-11-06 22:30:56 +01:00
}
}
2007-01-29 23:41:35 +01:00
/**
2007-10-30 20:39:50 +01:00
* \brief Verifie si un code client est modifiable en fonction des parametres
* du module de controle des codes .
2007-01-29 23:41:35 +01:00
* \return int 0 = Non , 1 = Oui
*/
function codeclient_modifiable ()
{
global $conf ;
if ( $conf -> global -> SOCIETE_CODECLIENT_ADDON )
{
require_once DOL_DOCUMENT_ROOT . '/includes/modules/societe/' . $conf -> global -> SOCIETE_CODECLIENT_ADDON . '.php' ;
2008-07-19 18:53:56 +02:00
2007-01-29 23:41:35 +01:00
$var = $conf -> global -> SOCIETE_CODECLIENT_ADDON ;
2008-07-19 18:53:56 +02:00
2007-01-29 23:41:35 +01:00
$mod = new $var ;
2008-07-19 18:53:56 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::codeclient_modifiable code_client= " . $this -> code_client . " module= " . $var );
2008-07-19 18:53:56 +02:00
if ( $mod -> code_modifiable_null && ! $this -> code_client ) return 1 ;
2007-01-29 23:41:35 +01:00
if ( $mod -> code_modifiable_invalide && $this -> check_codeclient () < 0 ) return 1 ;
2007-02-10 14:43:49 +01:00
if ( $mod -> code_modifiable ) return 1 ; // A mettre en dernier
2007-01-29 23:41:35 +01:00
return 0 ;
}
else
{
return 0 ;
}
}
/**
2007-02-10 14:43:49 +01:00
* \brief Verifie si un code fournisseur est modifiable dans configuration du module de controle des codes
2007-01-29 23:41:35 +01:00
* \return int 0 = Non , 1 = Oui
*/
function codefournisseur_modifiable ()
{
global $conf ;
if ( $conf -> global -> SOCIETE_CODEFOURNISSEUR_ADDON )
{
require_once DOL_DOCUMENT_ROOT . '/includes/modules/societe/' . $conf -> global -> SOCIETE_CODEFOURNISSEUR_ADDON . '.php' ;
2008-07-19 18:53:56 +02:00
2007-01-29 23:41:35 +01:00
$var = $conf -> global -> SOCIETE_CODEFOURNISSEUR_ADDON ;
2008-07-19 18:53:56 +02:00
2007-01-29 23:41:35 +01:00
$mod = new $var ;
2008-07-19 18:53:56 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::codefournisseur_modifiable code_founisseur= " . $this -> code_fournisseur . " module= " . $var );
2008-07-19 18:53:56 +02:00
if ( $mod -> code_modifiable_null && ! $this -> code_fournisseur ) return 1 ;
2007-01-29 23:41:35 +01:00
if ( $mod -> code_modifiable_invalide && $this -> check_codefournisseur () < 0 ) return 1 ;
2007-02-10 14:43:49 +01:00
if ( $mod -> code_modifiable ) return 1 ; // A mettre en dernier
2007-01-29 23:41:35 +01:00
return 0 ;
}
else
{
return 0 ;
}
}
2004-12-31 18:29:28 +01:00
2005-10-05 15:05:30 +02:00
2006-03-12 16:28:54 +01:00
/**
2009-07-06 09:58:26 +02:00
* \brief Check customer code
* \return int 0 if OK
* - 1 ErrorBadCustomerCodeSyntax
* - 2 ErrorCustomerCodeRequired
* - 3 ErrorCustomerCodeAlreadyUsed
* - 4 ErrorPrefixRequired
2006-03-12 16:28:54 +01:00
*/
function check_codeclient ()
{
2007-01-28 23:11:42 +01:00
global $conf ;
2006-03-12 16:28:54 +01:00
if ( $conf -> global -> SOCIETE_CODECLIENT_ADDON )
{
require_once DOL_DOCUMENT_ROOT . '/includes/modules/societe/' . $conf -> global -> SOCIETE_CODECLIENT_ADDON . '.php' ;
2008-07-19 18:53:56 +02:00
2006-03-12 16:28:54 +01:00
$var = $conf -> global -> SOCIETE_CODECLIENT_ADDON ;
2008-07-19 18:53:56 +02:00
2006-03-12 16:28:54 +01:00
$mod = new $var ;
2008-07-19 18:53:56 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::check_codeclient code_client= " . $this -> code_client . " module= " . $var );
2007-11-07 00:50:32 +01:00
$result = $mod -> verif ( $this -> db , $this -> code_client , $this , 0 );
return $result ;
2006-03-12 16:28:54 +01:00
}
else
{
return 0 ;
}
}
2008-07-19 18:53:56 +02:00
2007-01-28 23:11:42 +01:00
/**
2009-07-06 09:58:26 +02:00
* \brief Check supplier code
* \return int 0 if OK
* - 1 ErrorBadCustomerCodeSyntax
* - 2 ErrorCustomerCodeRequired
* - 3 ErrorCustomerCodeAlreadyUsed
* - 4 ErrorPrefixRequired
2007-01-28 23:11:42 +01:00
*/
2006-03-12 16:28:54 +01:00
function check_codefournisseur ()
{
2007-01-28 23:11:42 +01:00
global $conf ;
if ( $conf -> global -> SOCIETE_CODEFOURNISSEUR_ADDON )
2006-03-12 16:28:54 +01:00
{
2007-01-28 23:11:42 +01:00
require_once DOL_DOCUMENT_ROOT . '/includes/modules/societe/' . $conf -> global -> SOCIETE_CODEFOURNISSEUR_ADDON . '.php' ;
2008-07-19 18:53:56 +02:00
2007-01-28 23:11:42 +01:00
$var = $conf -> global -> SOCIETE_CODEFOURNISSEUR_ADDON ;
2008-07-19 18:53:56 +02:00
2006-03-12 16:28:54 +01:00
$mod = new $var ;
2007-01-28 23:11:42 +01:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::check_codefournisseur code_fournisseur= " . $this -> code_fournisseur . " module= " . $var );
2007-11-07 00:50:32 +01:00
$result = $mod -> verif ( $this -> db , $this -> code_fournisseur , $this , 1 );
return $result ;
2006-03-12 16:28:54 +01:00
}
else
{
return 0 ;
}
}
2005-10-05 15:05:30 +02:00
2006-03-12 16:28:54 +01:00
/**
* \brief Renvoie un code compta , suivant le module de code compta .
2009-04-27 22:37:50 +02:00
* Peut etre identique a celui saisit ou genere automatiquement .
* A ce jour seule la generation automatique est implementee
2006-03-12 16:28:54 +01:00
* \param type Type de tiers ( 'customer' ou 'supplier' )
2006-11-17 03:44:10 +01:00
* \return string Code compta si ok , 0 si aucun , < 0 si ko
2006-03-12 16:28:54 +01:00
*/
function get_codecompta ( $type )
{
global $conf ;
2008-07-19 18:53:56 +02:00
2006-03-12 16:28:54 +01:00
if ( $conf -> global -> SOCIETE_CODECOMPTA_ADDON )
{
require_once DOL_DOCUMENT_ROOT . '/includes/modules/societe/' . $conf -> global -> SOCIETE_CODECOMPTA_ADDON . '.php' ;
2008-07-19 18:53:56 +02:00
2006-03-12 16:28:54 +01:00
$var = $conf -> global -> SOCIETE_CODECOMPTA_ADDON ;
2008-07-19 18:53:56 +02:00
2006-03-12 16:28:54 +01:00
$mod = new $var ;
2008-07-19 18:53:56 +02:00
2006-11-17 03:44:10 +01:00
// Defini code compta dans $mod->code
2006-03-12 16:28:54 +01:00
$result = $mod -> get_code ( $this -> db , $this , $type );
2009-01-23 16:26:55 +01:00
2006-03-12 16:28:54 +01:00
if ( $type == 'customer' ) $this -> code_compta = $mod -> code ;
if ( $type == 'supplier' ) $this -> code_compta_fournisseur = $mod -> code ;
2008-07-19 18:53:56 +02:00
2006-03-12 16:28:54 +01:00
return $result ;
}
else
{
if ( $type == 'customer' ) $this -> code_compta = '' ;
if ( $type == 'supplier' ) $this -> code_compta_fournisseur = '' ;
2005-10-05 15:05:30 +02:00
2006-03-12 16:28:54 +01:00
return 0 ;
}
}
2005-10-05 15:05:30 +02:00
2008-03-07 02:50:56 +01:00
/**
2009-04-27 22:37:50 +02:00
* \brief Defini la societe mere pour les filiales
* \param id id compagnie mere a positionner
2008-07-19 18:53:56 +02:00
* \return int < 0 si ko , > 0 si ok
*/
function set_parent ( $id )
{
if ( $this -> id )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe " ;
$sql .= " SET parent = " . $id ;
$sql .= " WHERE rowid = " . $this -> id . " ; " ;
if ( $this -> db -> query ( $sql ) )
{
return 1 ;
}
else
{
return - 1 ;
}
}
}
2005-11-11 17:37:53 +01:00
2008-03-07 02:50:56 +01:00
/**
2009-04-27 22:37:50 +02:00
* \brief Supprime la societe mere
* \param id id compagnie mere a effacer
2008-07-19 18:53:56 +02:00
* \return int < 0 si ko , > 0 si ok
*/
function remove_parent ( $id )
{
if ( $this -> id )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe " ;
$sql .= " SET parent = null " ;
$sql .= " WHERE rowid = " . $this -> id . " ; " ;
if ( $this -> db -> query ( $sql ) )
{
return 1 ;
}
else
{
return - 1 ;
}
}
}
/**
* \brief Verifie la validite d ' un identifiant professionnel en
* fonction du pays de la societe ( siren , siret , ... )
* \param idprof 1 , 2 , 3 , 4 ( Exemple : 1 = siren , 2 = siret , 3 = naf , 4 = rcs / rm )
* \param soc Objet societe
* \return int < 0 si ko , > 0 si ok
*/
function id_prof_check ( $idprof , $soc )
{
$ok = 1 ;
// Verifie SIREN si pays FR
if ( $idprof == 1 && $soc -> pays_code == 'FR' )
{
$chaine = trim ( $this -> siren );
2009-10-21 16:02:14 +02:00
$chaine = preg_replace ( '/(\s)/' , '' , $chaine );
2008-07-19 18:53:56 +02:00
if ( strlen ( $chaine ) != 9 ) return - 1 ;
$sum = 0 ;
for ( $i = 0 ; $i < 10 ; $i = $i + 2 )
{
$sum = $sum + substr ( $this -> siren , ( 8 - $i ), 1 );
}
for ( $i = 1 ; $i < 9 ; $i = $i + 2 )
{
$ps = 2 * substr ( $this -> siren , ( 8 - $i ), 1 );
if ( $ps > 9 )
{
$ps = substr ( $ps , 0 , 1 ) + substr ( $ps , 1 , 1 );
}
$sum = $sum + $ps ;
}
if ( substr ( $sum , - 1 ) != 0 ) return - 1 ;
}
// Verifie SIRET si pays FR
if ( $idprof == 2 && $soc -> pays_code == 'FR' )
{
$chaine = trim ( $this -> siret );
2009-10-21 16:02:14 +02:00
$chaine = preg_replace ( '/(\s)/' , '' , $chaine );
2008-07-19 18:53:56 +02:00
if ( strlen ( $chaine ) != 14 ) return - 1 ;
}
return $ok ;
}
/**
2009-04-27 22:37:50 +02:00
* \brief Renvoi url de verification d ' un identifiant professionnal
2008-07-19 18:53:56 +02:00
* \param idprof 1 , 2 , 3 , 4 ( Exemple : 1 = siren , 2 = siret , 3 = naf , 4 = rcs / rm )
* \param soc Objet societe
* \return string url ou chaine vide si aucune url connue
*/
function id_prof_url ( $idprof , $soc )
{
global $langs ;
$url = '' ;
if ( $idprof == 1 && $soc -> pays_code == 'FR' ) $url = 'http://www.societe.com/cgi-bin/recherche?rncs=' . $soc -> siren ;
if ( $idprof == 1 && $soc -> pays_code == 'GB' ) $url = 'http://www.companieshouse.gov.uk/WebCHeck/findinfolink/' ;
if ( $url ) return '<a target="_blank" href="' . $url . '">[' . $langs -> trans ( " Check " ) . ']</a>' ;
return '' ;
}
/**
2009-04-27 22:37:50 +02:00
* \brief Indique si la societe a des projets
* \return bool true si la societe a des projets , false sinon
2008-07-19 18:53:56 +02:00
*/
function has_projects ()
{
$sql = 'SELECT COUNT(*) as numproj FROM ' . MAIN_DB_PREFIX . 'projet WHERE fk_soc = ' . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$nump = $this -> db -> num_rows ( $resql );
$obj = $this -> db -> fetch_object ();
$count = $obj -> numproj ;
}
else
{
$count = 0 ;
print $this -> db -> error ();
}
$this -> db -> free ( $resql );
return ( $count > 0 );
}
function AddPerms ( $user_id , $read , $write , $perms )
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " societe_perms " ;
$sql .= " (fk_soc, fk_user, pread, pwrite, pperms) " ;
$sql .= " VALUES ( " . $this -> id . " , " . $user_id . " , " . $read . " , " . $write . " , " . $perms . " ); " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
}
}
/**
* \brief Charge les informations d 'ordre info dans l' objet societe
* \param id id de la societe a charger
*/
function info ( $id )
{
$sql = " SELECT s.rowid, s.nom, " . $this -> db -> pdate ( " datec " ) . " as datec, " . $this -> db -> pdate ( " datea " ) . " as datea, " ;
$sql .= " fk_user_creat, fk_user_modif " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe as s " ;
$sql .= " WHERE s.rowid = " . $id ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
if ( $this -> db -> num_rows ( $result ))
{
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
if ( $obj -> fk_user_creat ) {
$cuser = new User ( $this -> db , $obj -> fk_user_creat );
$cuser -> fetch ();
$this -> user_creation = $cuser ;
}
if ( $obj -> fk_user_modif ) {
$muser = new User ( $this -> db , $obj -> fk_user_modif );
$muser -> fetch ();
$this -> user_modification = $muser ;
}
$this -> ref = $obj -> nom ;
$this -> date_creation = $obj -> datec ;
$this -> date_modification = $obj -> datea ;
}
$this -> db -> free ( $result );
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-19 18:53:56 +02:00
}
}
/**
2009-04-27 22:37:50 +02:00
* \brief Renvoi si pays appartient a CEE
2008-07-19 18:53:56 +02:00
* \param boolean true = pays dans CEE , false = pays hors CEE
*/
function isInEEC ()
{
2008-03-07 02:50:56 +01:00
// List of all country codes that are in europe for european vat rules
2009-07-31 14:07:49 +02:00
// List found on http://ec.europa.eu/taxation_customs/vies/lang.do?fromWhichPage=vieshome
2008-03-07 02:50:56 +01:00
$country_code_in_EEC = array (
'AT' , // Austria
'BE' , // Belgium
'BG' , // Bulgaria
'CY' , // Cyprus
'CZ' , // Czech republic
2009-07-31 14:07:49 +02:00
'DE' , // Germany
2008-03-07 02:50:56 +01:00
'DK' , // Danemark
'EE' , // Estonia
2009-07-31 14:07:49 +02:00
'ES' , // Spain
2008-03-07 02:50:56 +01:00
'FI' , // Finland
'FR' , // France
'GB' , // Royaume-uni
'GR' , // Greece
'NL' , // Holland
'HU' , // Hungary
'IE' , // Ireland
'IT' , // Italy
'LT' , // Lithuania
'LU' , // Luxembourg
2009-07-31 14:07:49 +02:00
'LV' , // Latvia
'MO' , // Monaco Seems to use same IntraVAT than France (http://www.gouv.mc/devwww/wwwnew.nsf/c3241c4782f528bdc1256d52004f970b/9e370807042516a5c1256f81003f5bb3!OpenDocument)
2008-03-07 02:50:56 +01:00
'MT' , // Malta
2009-07-31 14:07:49 +02:00
//'NO', // Norway
2008-03-07 02:50:56 +01:00
'PL' , // Poland
'PT' , // Portugal
'RO' , // Romania
2009-07-31 14:07:49 +02:00
'SE' , // Sweden
2008-03-07 02:50:56 +01:00
'SK' , // Slovakia
'SI' , // Slovenia
2009-07-31 14:07:49 +02:00
//'CH', // Switzerland - No. Swizerland in not in EEC
2008-07-19 18:53:56 +02:00
);
2008-03-07 02:50:56 +01:00
//print "dd".$this->pays_code;
return in_array ( $this -> pays_code , $country_code_in_EEC );
2008-07-19 18:53:56 +02:00
}
2008-03-07 02:50:56 +01:00
/**
2008-07-19 18:53:56 +02:00
* \brief Charge la liste des categories fournisseurs
* \return 0 in success , <> 0 in error
*/
function LoadSupplierCateg ()
{
$this -> SupplierCategories = array ();
$sql = " SELECT rowid, label " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " categorie " ;
$sql .= " WHERE type = 1 " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2006-12-06 16:24:54 +01:00
while ( $obj = $this -> db -> fetch_object ( $resql ) )
2008-07-19 18:53:56 +02:00
{
$this -> SupplierCategories [ $obj -> rowid ] = $obj -> label ;
}
2006-12-06 16:24:54 +01:00
return 0 ;
2008-07-19 18:53:56 +02:00
}
else
{
2006-12-06 16:24:54 +01:00
return - 1 ;
2008-07-19 18:53:56 +02:00
}
2006-12-06 16:24:54 +01:00
}
2008-07-19 18:53:56 +02:00
/*
* \brief Charge la liste des categories fournisseurs
* \return 0 in success , <> 0 in error
*/
function AddFournisseurInCategory ( $categorie_id )
{
if ( $categorie_id > 0 )
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " categorie_fournisseur (fk_categorie, fk_societe) " ;
$sql .= " VALUES (' " . $categorie_id . " ',' " . $this -> id . " '); " ;
if ( $resql = $this -> db -> query ( $sql ))
2006-12-06 16:24:54 +01:00
return 0 ;
2008-07-19 18:53:56 +02:00
}
else
{
return 0 ;
}
return - 1 ;
}
2007-10-09 15:45:08 +02:00
2009-09-15 21:42:48 +02:00
/**
* Add a line in log table to save status change .
*
* @ param unknown_type $id_status
*/
2007-10-09 15:45:08 +02:00
function set_status ( $id_status )
2008-07-19 18:53:56 +02:00
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " societe_log (datel, fk_soc, fk_statut, fk_user, author, label) " ;
2009-04-27 22:37:50 +02:00
$sql .= " VALUES (' " . $dateaction . " ', " . $socid . " , " . $id_status . " , " ;
2007-10-09 15:45:08 +02:00
$sql .= " ' " . $user -> id . " ', " ;
$sql .= " ' " . addslashes ( $user -> login ) . " ', " ;
2009-04-27 22:37:50 +02:00
$sql .= " 'Change statut from " . $oldstcomm . " to " . $stcommid . " ' " ;
2007-10-09 15:45:08 +02:00
$sql .= " ) " ;
2008-07-19 18:53:56 +02:00
$result = $db -> query ( $sql );
if ( $result )
{
2009-04-27 22:37:50 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe SET fk_stcomm = " . $stcommid . " WHERE rowid= " . $socid ;
2008-07-19 18:53:56 +02:00
$result = $db -> query ( $sql );
}
else
{
2007-10-09 15:45:08 +02:00
$errmesg = $db -> error ;
2008-07-19 18:53:56 +02:00
}
2007-10-09 15:45:08 +02:00
}
2008-07-19 18:53:56 +02:00
2008-01-10 18:12:07 +01:00
/**
2008-07-19 18:53:56 +02:00
* \brief Retourne le formulaire de saisie d ' un identifiant professionnel ( siren , siret , etc ... )
* \param idprof 1 , 2 , 3 , 4 ( Exemple : 1 = siren , 2 = siret , 3 = naf , 4 = rcs / rm )
* \param htmlname Nom de la zone input
* \param preselected Default value to show
*/
function show_input_id_prof ( $idprof , $htmlname , $preselected )
{
global $langs ;
$formlength = 16 ;
if ( $this -> pays_code == 'FR' )
2008-01-10 18:12:07 +01:00
{
if ( $idprof == 1 ) $formlength = 9 ;
if ( $idprof == 2 ) $formlength = 14 ;
2008-02-12 04:58:13 +01:00
if ( $idprof == 3 ) $formlength = 5 ; // 4 chiffres et 1 lettre depuis janvier
2008-01-10 18:12:07 +01:00
if ( $idprof == 4 ) $formlength = 12 ;
}
2008-05-14 01:55:08 +02:00
$selected = $preselected ;
2008-07-19 18:53:56 +02:00
if ( ! $selected && $idprof == 1 ) $selected = $this -> siren ;
if ( ! $selected && $idprof == 2 ) $selected = $this -> siret ;
if ( ! $selected && $idprof == 3 ) $selected = $this -> ape ;
if ( ! $selected && $idprof == 4 ) $selected = $this -> idprof4 ;
2008-01-10 18:12:07 +01:00
2008-07-19 18:53:56 +02:00
print '<input type="text" name="' . $htmlname . '" size="' . ( $formlength + 1 ) . '" maxlength="' . $formlength . '" value="' . $selected . '">' ;
}
2009-02-15 18:33:02 +01:00
2009-02-02 03:04:26 +01:00
/**
* \brief Cree en base un utilisateur depuis l ' objet adherent
* \param member Objet adherent source
* \param login Login to force
* \return int Si erreur < 0 , si ok renvoie id compte cree
*/
function create_from_member ( $member , $login = '' )
{
global $conf , $user , $langs ;
$name = $member -> societe ;
if ( empty ( $name )) $name = strtolower ( $member -> nom . ' ' . $member -> prenom );
// Positionne parametres
$this -> email = $member -> email ;
$this -> nom = $name ;
$this -> code_client = - 1 ;
$this -> code_fournisseur = - 1 ;
$this -> adresse = $member -> adresse ;
$this -> cp = $member -> cp ;
$this -> ville = $member -> ville ;
$this -> pays_code = $member -> pays_code ;
$this -> pays_id = $member -> pays_id ;
2009-02-15 18:33:02 +01:00
$this -> tel = $member -> phone ; // Prof phone
2009-02-02 03:04:26 +01:00
$this -> db -> begin ();
// Cree et positionne $this->id
$result = $this -> create ( $user );
if ( $result >= 0 )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " adherent " ;
$sql .= " SET fk_soc= " . $this -> id ;
$sql .= " WHERE rowid= " . $member -> id ;
2009-02-15 18:33:02 +01:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::create_from_member sql= " . $sql , LOG_DEBUG );
2009-02-02 03:04:26 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
$this -> error = $this -> db -> error ();
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::create_from_member - 1 - " . $this -> error , LOG_ERR );
2009-02-02 03:04:26 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
else
{
// $this->error deja positionne
2009-02-20 23:53:15 +01:00
dol_syslog ( " Societe::create_from_member - 2 - " . $this -> error , LOG_ERR );
2009-02-02 03:04:26 +01:00
$this -> db -> rollback ();
return $result ;
}
}
2009-02-15 18:33:02 +01:00
2002-04-30 12:51:35 +02:00
}
2002-07-29 12:26:12 +02:00
2002-04-30 12:51:35 +02:00
?>