2006-06-18 17:06:29 +02:00
< ? php
2011-04-08 12:10:08 +02:00
/* Copyright ( C ) 2006 - 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2005 - 2011 Regis Houssin < regis @ dolibarr . fr >
* Copyright ( C ) 2010 - 2011 Juanjo Menent < jmenent @ 2 byte . es >
2006-06-18 17:06:29 +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
2011-08-01 01:45:11 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2006-06-18 17:06:29 +02:00
*/
/**
2010-06-08 01:52:43 +02:00
* \file htdocs / core / class / commonobject . class . php
2009-06-19 09:32:39 +02:00
* \ingroup core
2011-06-22 12:58:22 +02:00
* \brief File of parent class of all other business classes ( invoices , contracts , proposals , orders , ... )
2009-02-20 23:53:15 +01:00
*/
2006-06-18 17:06:29 +02:00
/**
2009-06-19 09:32:39 +02:00
* \class CommonObject
2011-08-23 00:04:21 +02:00
* \brief Parent class of all other business classes ( invoices , contracts , proposals , orders , ... )
2009-02-20 23:53:15 +01:00
*/
2006-06-18 17:06:29 +02:00
2011-08-23 00:04:21 +02:00
abstract class CommonObject
2006-06-18 17:06:29 +02:00
{
2010-09-22 08:30:32 +02:00
var $db ;
2011-02-20 21:53:26 +01:00
2011-08-23 00:04:21 +02:00
var $canvas ; // Contains canvas name if it is
2010-09-28 20:00:59 +02:00
2011-08-23 00:04:21 +02:00
// No constructor as it is an abstract class
2010-08-18 09:28:12 +02:00
2010-05-04 20:33:22 +02:00
/**
2011-09-12 19:08:02 +02:00
* Check if ref is used .
2011-08-28 14:30:15 +02:00
*
2011-09-12 19:08:02 +02:00
* @ return int < 0 if KO , 0 if not found , > 0 if found
2010-05-04 20:33:22 +02:00
*/
function verifyNumRef ()
{
global $conf ;
$sql = " SELECT rowid " ;
2010-10-03 20:49:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element ;
2010-05-04 20:33:22 +02:00
$sql .= " WHERE ref = ' " . $this -> ref . " ' " ;
$sql .= " AND entity = " . $conf -> entity ;
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::verifyNumRef sql= " . $sql , LOG_DEBUG );
2010-05-04 20:33:22 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
return $num ;
}
else
{
$this -> error = $this -> db -> lasterror ();
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::verifyNumRef " . $this -> error , LOG_ERR );
2010-05-04 20:33:22 +02:00
return - 1 ;
}
}
2006-06-18 17:06:29 +02:00
/**
2011-09-12 19:08:02 +02:00
* Add a link between element $this -> element and a contact
2011-08-28 14:30:15 +02:00
*
2011-09-12 19:08:02 +02:00
* @ param fk_socpeople Id of contact to link
* @ param type_contact Type of contact ( code or id )
* @ param source external = Contact extern ( llx_socpeople ), internal = Contact intern ( llx_user )
* @ param notrigger Disable all triggers
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
2010-05-28 11:37:12 +02:00
function add_contact ( $fk_socpeople , $type_contact , $source = 'external' , $notrigger = 0 )
2006-06-18 17:06:29 +02:00
{
2010-05-28 20:13:17 +02:00
global $user , $conf , $langs ;
2006-06-18 17:06:29 +02:00
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::add_contact $fk_socpeople , $type_contact , $source " );
2006-06-18 17:06:29 +02:00
2010-05-28 20:13:17 +02:00
// Check parameters
2006-06-18 17:06:29 +02:00
if ( $fk_socpeople <= 0 )
{
$this -> error = $langs -> trans ( " ErrorWrongValueForParameter " , " 1 " );
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::add_contact " . $this -> error , LOG_ERR );
2006-06-18 17:06:29 +02:00
return - 1 ;
}
if ( ! $type_contact )
{
$this -> error = $langs -> trans ( " ErrorWrongValueForParameter " , " 2 " );
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::add_contact " . $this -> error , LOG_ERR );
2006-06-18 17:06:29 +02:00
return - 2 ;
}
$id_type_contact = 0 ;
if ( is_numeric ( $type_contact ))
{
$id_type_contact = $type_contact ;
}
else
{
// On recherche id type_contact
$sql = " SELECT tc.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
$sql .= " WHERE element=' " . $this -> element . " ' " ;
$sql .= " AND source=' " . $source . " ' " ;
$sql .= " AND code=' " . $type_contact . " ' AND active=1 " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$id_type_contact = $obj -> rowid ;
}
}
2010-05-28 20:13:17 +02:00
$datecreate = dol_now ();
2006-06-18 17:06:29 +02:00
2009-02-20 23:53:15 +01:00
// Insertion dans la base
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " element_contact " ;
$sql .= " (element_id, fk_socpeople, datecreate, statut, fk_c_type_contact) " ;
$sql .= " VALUES ( " . $this -> id . " , " . $fk_socpeople . " , " ;
2006-06-18 17:06:29 +02:00
$sql .= $this -> db -> idate ( $datecreate );
$sql .= " , 4, ' " . $id_type_contact . " ' " ;
2009-02-20 23:53:15 +01:00
$sql .= " ) " ;
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::add_contact sql= " . $sql );
2006-06-18 17:06:29 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2009-02-20 23:53:15 +01:00
{
2010-05-28 11:37:12 +02:00
if ( ! $notrigger )
{
// Call triggers
include_once ( DOL_DOCUMENT_ROOT . " /core/class/interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( strtoupper ( $this -> element ) . '_ADD_CONTACT' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// End call triggers
}
2010-05-28 20:13:17 +02:00
2009-02-20 23:53:15 +01:00
return 1 ;
}
else
{
2007-10-31 13:55:01 +01:00
if ( $this -> db -> errno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' )
{
$this -> error = $this -> db -> errno ();
return - 2 ;
}
else
{
2010-05-28 20:13:17 +02:00
$this -> error = $this -> db -> error ();
2009-02-20 23:53:15 +01:00
dol_syslog ( $this -> error , LOG_ERR );
2007-10-31 13:55:01 +01:00
return - 1 ;
}
2009-02-20 23:53:15 +01:00
}
2006-06-18 17:06:29 +02:00
}
2009-02-20 23:53:15 +01:00
/**
2011-07-04 11:36:29 +02:00
* Update a link to contact line
2011-08-28 14:30:15 +02:00
*
2011-07-04 11:36:29 +02:00
* @ param rowid Id of line contact - element
* @ param statut New status of link
2011-08-14 05:13:50 +02:00
* @ param type_contact_id Id of contact type ( not modified if 0 )
2011-07-04 11:36:29 +02:00
* @ return int < 0 if KO , >= 0 if OK
2009-02-20 23:53:15 +01:00
*/
2011-08-14 05:13:50 +02:00
function update_contact ( $rowid , $statut , $type_contact_id = 0 )
2006-06-18 17:06:29 +02:00
{
2009-02-20 23:53:15 +01:00
// Insertion dans la base
$sql = " UPDATE " . MAIN_DB_PREFIX . " element_contact set " ;
2011-08-14 05:13:50 +02:00
$sql .= " statut = " . $statut ;
if ( $type_contact_id ) $sql .= " , fk_c_type_contact = ' " . $type_contact_id . " ' " ;
2009-02-20 23:53:15 +01:00
$sql .= " where rowid = " . $rowid ;
2011-08-14 05:13:50 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2009-02-20 23:53:15 +01:00
{
return 0 ;
}
else
{
2011-08-14 05:13:50 +02:00
$this -> error = $this -> db -> lasterror ();
2009-02-20 23:53:15 +01:00
return - 1 ;
}
}
2006-06-18 17:06:29 +02:00
/**
2011-07-04 11:36:29 +02:00
* Delete a link to contact line
2011-08-28 14:30:15 +02:00
*
2011-07-04 11:36:29 +02:00
* @ param rowid Id of contact link line to delete
* @ param notrigger Disable all triggers
* @ return int > 0 if OK , < 0 if KO
2009-02-20 23:53:15 +01:00
*/
2011-08-14 05:13:50 +02:00
function delete_contact ( $rowid , $notrigger = 0 )
2009-02-20 23:53:15 +01:00
{
2010-05-28 20:13:17 +02:00
global $user , $langs , $conf ;
2009-02-20 23:53:15 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_contact " ;
$sql .= " WHERE rowid = " . $rowid ;
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_contact sql= " . $sql );
2009-02-20 23:53:15 +01:00
if ( $this -> db -> query ( $sql ))
{
2010-05-28 11:37:12 +02:00
if ( ! $notrigger )
{
// Call triggers
include_once ( DOL_DOCUMENT_ROOT . " /core/class/interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( strtoupper ( $this -> element ) . '_DELETE_CONTACT' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// End call triggers
}
2010-05-28 20:13:17 +02:00
2009-02-20 23:53:15 +01:00
return 1 ;
}
else
{
2008-04-04 12:37:58 +02:00
$this -> error = $this -> db -> lasterror ();
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_contact error= " . $this -> error , LOG_ERR );
2009-02-20 23:53:15 +01:00
return - 1 ;
}
}
2010-05-04 20:33:22 +02:00
2010-04-29 17:35:52 +02:00
/**
2011-05-18 14:34:48 +02:00
* Delete all links between an object $this and all its contacts
2011-08-28 14:30:15 +02:00
*
2011-05-18 14:34:48 +02:00
* @ return int > 0 if OK , < 0 if KO
2010-04-29 17:35:52 +02:00
*/
function delete_linked_contact ()
{
$temp = array ();
2010-05-28 20:13:17 +02:00
$typeContact = $this -> liste_type_contact ( '' );
2010-05-04 20:33:22 +02:00
2010-04-29 17:35:52 +02:00
foreach ( $typeContact as $key => $value )
{
array_push ( $temp , $key );
}
$listId = implode ( " , " , $temp );
2010-05-04 20:33:22 +02:00
2010-04-29 17:35:52 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_contact " ;
$sql .= " WHERE element_id = " . $this -> id ;
$sql .= " AND fk_c_type_contact IN ( " . $listId . " ) " ;
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_linked_contact sql= " . $sql );
2010-04-29 17:35:52 +02:00
if ( $this -> db -> query ( $sql ))
{
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_linked_contact error= " . $this -> error , LOG_ERR );
2010-04-29 17:35:52 +02:00
return - 1 ;
}
}
2009-02-20 23:53:15 +01:00
/**
2011-04-26 15:49:41 +02:00
* Get array of all contacts for an object
2011-08-28 14:30:15 +02:00
*
2011-09-12 19:08:02 +02:00
* @ param int $statut Status of lines to get ( - 1 = all )
* @ param string $source Source of contact : external or thirdparty ( llx_socpeople ) or internal ( llx_user )
* @ param int $list 0 : Return array contains all properties , 1 : Return array contains just id
* @ return array Array of contacts
2009-02-20 23:53:15 +01:00
*/
2011-04-26 15:49:41 +02:00
function liste_contact ( $statut =- 1 , $source = 'external' , $list = 0 )
2009-02-20 23:53:15 +01:00
{
global $langs ;
$tab = array ();
2010-01-26 14:28:25 +01:00
$sql = " SELECT ec.rowid, ec.statut, ec.fk_socpeople as id " ;
if ( $source == 'internal' ) $sql .= " , '-1' as socid " ;
2010-04-24 15:39:16 +02:00
if ( $source == 'external' || $source == 'thirdparty' ) $sql .= " , t.fk_soc as socid " ;
2011-07-10 18:50:40 +02:00
$sql .= " , t.civilite as civility, t.name as lastname, t.firstname, t.email " ;
2010-01-26 14:28:25 +01:00
$sql .= " , tc.source, tc.element, tc.code, tc.libelle " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_type_contact tc " ;
$sql .= " , " . MAIN_DB_PREFIX . " element_contact ec " ;
2009-02-20 23:53:15 +01:00
if ( $source == 'internal' ) $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " user t on ec.fk_socpeople = t.rowid " ;
2010-04-24 15:39:16 +02:00
if ( $source == 'external' || $source == 'thirdparty' ) $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " socpeople t on ec.fk_socpeople = t.rowid " ;
2009-02-20 23:53:15 +01:00
$sql .= " WHERE ec.element_id = " . $this -> id ;
$sql .= " AND ec.fk_c_type_contact=tc.rowid " ;
$sql .= " AND tc.element=' " . $this -> element . " ' " ;
if ( $source == 'internal' ) $sql .= " AND tc.source = 'internal' " ;
2010-04-24 15:39:16 +02:00
if ( $source == 'external' || $source == 'thirdparty' ) $sql .= " AND tc.source = 'external' " ;
2009-02-20 23:53:15 +01:00
$sql .= " AND tc.active=1 " ;
if ( $statut >= 0 ) $sql .= " AND ec.statut = ' " . $statut . " ' " ;
$sql .= " ORDER BY t.name ASC " ;
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::liste_contact sql= " . $sql );
2009-02-20 23:53:15 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
2011-04-26 15:49:41 +02:00
if ( ! $list )
{
$transkey = " TypeContact_ " . $obj -> element . " _ " . $obj -> source . " _ " . $obj -> code ;
$libelle_type = ( $langs -> trans ( $transkey ) != $transkey ? $langs -> trans ( $transkey ) : $obj -> libelle );
2011-07-10 18:50:40 +02:00
$tab [ $i ] = array ( 'source' => $obj -> source , 'socid' => $obj -> socid , 'id' => $obj -> id ,
'nom' => $obj -> lastname , // For backward compatibility
'civility' => $obj -> civility , 'lastname' => $obj -> lastname , 'firstname' => $obj -> firstname , 'email' => $obj -> email ,
2011-04-26 15:49:41 +02:00
'rowid' => $obj -> rowid , 'code' => $obj -> code , 'libelle' => $libelle_type , 'status' => $obj -> statut );
}
else
{
$tab [ $i ] = $obj -> id ;
}
2011-05-18 14:34:48 +02:00
2009-02-20 23:53:15 +01:00
$i ++ ;
}
2011-05-18 14:34:48 +02:00
2009-02-20 23:53:15 +01:00
return $tab ;
}
else
{
$this -> error = $this -> db -> error ();
dol_print_error ( $this -> db );
return - 1 ;
}
}
2011-08-14 05:13:50 +02:00
/**
* Update status of a contact linked to object
*
* @ param $rowid Id of link between object and contact
* @ return int < 0 if KO , >= 0 if OK
*/
function swapContactStatus ( $rowid )
2009-02-20 23:53:15 +01:00
{
$sql = " SELECT ec.datecreate, ec.statut, ec.fk_socpeople, ec.fk_c_type_contact, " ;
2011-08-14 05:13:50 +02:00
$sql .= " tc.code, tc.libelle " ;
//$sql.= ", s.fk_soc";
2009-02-20 23:53:15 +01:00
$sql .= " FROM ( " . MAIN_DB_PREFIX . " element_contact as ec, " . MAIN_DB_PREFIX . " c_type_contact as tc) " ;
2011-08-14 05:13:50 +02:00
//$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as s ON ec.fk_socpeople=s.rowid"; // Si contact de type external, alors il est lie a une societe
2009-02-20 23:53:15 +01:00
$sql .= " WHERE ec.rowid = " . $rowid ;
$sql .= " AND ec.fk_c_type_contact=tc.rowid " ;
$sql .= " AND tc.element = ' " . $this -> element . " ' " ;
2011-08-14 05:13:50 +02:00
dol_syslog ( get_class ( $object ) . " ::swapContactStatus sql= " . $sql );
2009-02-20 23:53:15 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
2011-08-14 05:13:50 +02:00
$newstatut = ( $obj -> statut == 4 ) ? 5 : 4 ;
$result = $this -> update_contact ( $rowid , $newstatut );
$this -> db -> free ( $resql );
return $result ;
2009-02-20 23:53:15 +01:00
}
else
{
$this -> error = $this -> db -> error ();
dol_print_error ( $this -> db );
2011-08-14 05:13:50 +02:00
return - 1 ;
2009-02-20 23:53:15 +01:00
}
2011-08-14 05:13:50 +02:00
2009-02-20 23:53:15 +01:00
}
/**
2010-12-13 14:16:02 +01:00
* Return array with list of possible values for type of contacts
2011-08-28 14:30:15 +02:00
*
2010-12-13 14:16:02 +01:00
* @ param source internal , external or all if not defined
* @ param order Sort order by : code or rowid
2011-07-13 18:55:25 +02:00
* @ param option 0 = Return array id -> label , 1 = Return array code -> label
* @ return array Array list of type of contacts ( id -> label if option = 0 , code -> label if option = 1 )
2009-02-20 23:53:15 +01:00
*/
2011-07-13 18:55:25 +02:00
function liste_type_contact ( $source = 'internal' , $order = 'code' , $option = 0 )
2009-02-20 23:53:15 +01:00
{
global $langs ;
$tab = array ();
2010-01-25 23:24:40 +01:00
$sql = " SELECT DISTINCT tc.rowid, tc.code, tc.libelle " ;
2009-02-20 23:53:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
2010-01-25 23:24:40 +01:00
$sql .= " WHERE tc.element=' " . $this -> element . " ' " ;
2010-05-28 20:13:17 +02:00
if ( ! empty ( $source )) $sql .= " AND tc.source=' " . $source . " ' " ;
2010-01-25 23:24:40 +01:00
$sql .= " ORDER by tc. " . $order ;
2009-02-20 23:53:15 +01:00
2010-12-13 14:16:02 +01:00
//print "sql=".$sql;
2009-02-20 23:53:15 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
$transkey = " TypeContact_ " . $this -> element . " _ " . $source . " _ " . $obj -> code ;
$libelle_type = ( $langs -> trans ( $transkey ) != $transkey ? $langs -> trans ( $transkey ) : $obj -> libelle );
2011-07-13 18:55:25 +02:00
if ( empty ( $option )) $tab [ $obj -> rowid ] = $libelle_type ;
else $tab [ $obj -> code ] = $libelle_type ;
2009-02-20 23:53:15 +01:00
$i ++ ;
}
return $tab ;
}
else
{
2010-12-13 14:16:02 +01:00
$this -> error = $this -> db -> lasterror ();
//dol_print_error($this->db);
2009-02-20 23:53:15 +01:00
return null ;
}
}
/**
2010-11-15 20:21:29 +01:00
* Return id of contacts for a source and a contact code .
* Example : contact client de facturation ( 'external' , 'BILLING' )
* Example : contact client de livraison ( 'external' , 'SHIPPING' )
* Example : contact interne suivi paiement ( 'internal' , 'SALESREPFOLL' )
2011-08-28 14:30:15 +02:00
*
2010-11-15 20:21:29 +01:00
* @ param source 'external' or 'internal'
* @ param code 'BILLING' , 'SHIPPING' , 'SALESREPFOLL' , ...
* @ param status limited to a certain status
* @ return array List of id for such contacts
2009-02-20 23:53:15 +01:00
*/
2010-04-01 10:47:41 +02:00
function getIdContact ( $source , $code , $status = 0 )
2009-02-20 23:53:15 +01:00
{
2010-11-15 22:29:26 +01:00
global $conf ;
2010-12-13 14:16:02 +01:00
2009-02-20 23:53:15 +01:00
$result = array ();
$i = 0 ;
$sql = " SELECT ec.fk_socpeople " ;
2010-06-15 21:03:21 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " element_contact as ec, " ;
2010-11-23 09:46:15 +01:00
if ( $source == 'internal' ) $sql .= " " . MAIN_DB_PREFIX . " user as c, " ;
if ( $source == 'external' ) $sql .= " " . MAIN_DB_PREFIX . " socpeople as c, " ;
2010-06-15 21:03:21 +02:00
$sql .= " " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
2009-02-20 23:53:15 +01:00
$sql .= " WHERE ec.element_id = " . $this -> id ;
2010-11-15 22:29:26 +01:00
$sql .= " AND ec.fk_socpeople = c.rowid " ;
2011-01-15 10:31:09 +01:00
$sql .= " AND c.entity IN (0, " . $conf -> entity . " ) " ;
2010-04-01 10:47:41 +02:00
$sql .= " AND ec.fk_c_type_contact = tc.rowid " ;
2009-02-20 23:53:15 +01:00
$sql .= " AND tc.element = ' " . $this -> element . " ' " ;
$sql .= " AND tc.source = ' " . $source . " ' " ;
$sql .= " AND tc.code = ' " . $code . " ' " ;
$sql .= " AND tc.active = 1 " ;
2010-04-01 10:47:41 +02:00
if ( $status ) $sql .= " AND ec.statut = " . $status ;
2009-02-20 23:53:15 +01:00
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::getIdContact sql= " . $sql );
2009-02-20 23:53:15 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$result [ $i ] = $obj -> fk_socpeople ;
$i ++ ;
}
}
else
{
$this -> error = $this -> db -> error ();
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::getIdContact " . $this -> error , LOG_ERR );
2009-02-20 23:53:15 +01:00
return null ;
}
return $result ;
}
/**
2011-08-28 14:30:15 +02:00
* Charge le contact d ' id $id dans this -> contact
*
* @ param contactid Id du contact
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
function fetch_contact ( $contactid )
{
2010-04-28 09:55:43 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /contact/class/contact.class.php " );
2008-02-24 18:01:48 +01:00
$contact = new Contact ( $this -> db );
$result = $contact -> fetch ( $contactid );
$this -> contact = $contact ;
2007-09-14 23:02:13 +02:00
return $result ;
2009-02-20 23:53:15 +01:00
}
/**
2010-11-11 01:59:31 +01:00
* Load the third party of object from id $this -> socid into this -> thirdpary
2011-08-28 14:30:15 +02:00
*
2010-11-11 01:59:31 +01:00
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
2010-05-12 13:58:58 +02:00
function fetch_thirdparty ()
2009-02-20 23:53:15 +01:00
{
2009-06-19 09:32:39 +02:00
global $conf ;
2009-07-07 18:19:37 +02:00
2010-05-05 20:08:42 +02:00
if ( empty ( $this -> socid )) return 0 ;
2010-11-11 01:59:31 +01:00
$thirdparty = new Societe ( $this -> db );
$result = $thirdparty -> fetch ( $this -> socid );
$this -> client = $thirdparty ; // deprecated
$this -> thirdparty = $thirdparty ;
2009-06-19 09:32:39 +02:00
// Use first price level if level not defined for third party
2010-11-11 01:59:31 +01:00
if ( $conf -> global -> PRODUIT_MULTIPRICES && empty ( $this -> thirdparty -> price_level ))
{
$this -> client -> price_level = 1 ; // deprecated
$this -> thirdparty -> price_level = 1 ;
}
2009-07-07 18:19:37 +02:00
2007-09-14 23:02:13 +02:00
return $result ;
2009-02-20 23:53:15 +01:00
}
/**
2011-08-28 14:30:15 +02:00
* Charge le projet d ' id $this -> fk_project dans this -> projet
*
* @ return int < 0 if KO , >= 0 if OK
2009-02-20 23:53:15 +01:00
*/
function fetch_projet ()
{
2010-05-05 20:08:42 +02:00
if ( empty ( $this -> fk_project )) return 0 ;
2010-03-11 15:31:16 +01:00
$project = new Project ( $this -> db );
$result = $project -> fetch ( $this -> fk_project );
$this -> projet = $project ;
2007-09-14 23:02:13 +02:00
return $result ;
2009-02-20 23:53:15 +01:00
}
2007-05-04 20:56:49 +02:00
2008-02-24 18:01:48 +01:00
/**
2011-08-28 14:30:15 +02:00
* Charge le user d ' id userid dans this -> user
*
* @ param userid Id du contact
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
function fetch_user ( $userid )
{
2010-04-28 09:31:34 +02:00
$user = new User ( $this -> db );
$result = $user -> fetch ( $userid );
2008-02-24 18:01:48 +01:00
$this -> user = $user ;
2007-09-14 23:02:13 +02:00
return $result ;
2009-02-20 23:53:15 +01:00
}
2008-02-24 18:01:48 +01:00
2007-09-14 23:02:13 +02:00
/**
2011-08-20 17:11:31 +02:00
* Load delivery adresse id into $this -> fk_address
2011-08-28 14:30:15 +02:00
*
2011-08-20 17:11:31 +02:00
* @ param fk_address Id of address
2011-01-15 10:31:09 +01:00
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
2011-01-15 10:31:09 +01:00
function fetch_address ( $fk_address )
2007-09-14 23:02:13 +02:00
{
2011-01-15 10:31:09 +01:00
$object = new Societe ( $this -> db );
$result = $object -> fetch_address ( $fk_address );
$this -> deliveryaddress = $object ; // TODO obsolete
$this -> adresse = $object ; // TODO obsolete
$this -> address = $object ;
2007-09-14 23:02:13 +02:00
return $result ;
}
2009-12-15 12:46:54 +01:00
2009-12-15 11:39:22 +01:00
/**
2010-09-09 16:06:15 +02:00
* Read linked origin object
2009-12-15 11:39:22 +01:00
*/
2010-09-09 16:06:15 +02:00
function fetch_origin ()
2009-12-15 11:39:22 +01:00
{
2010-03-13 16:52:30 +01:00
// TODO uniformise code
2010-09-09 16:06:15 +02:00
if ( $this -> origin == 'shipping' ) $this -> origin = 'expedition' ;
if ( $this -> origin == 'delivery' ) $this -> origin = 'livraison' ;
2010-09-15 15:29:17 +02:00
2010-09-09 16:06:15 +02:00
$object = $this -> origin ;
2010-04-01 12:21:48 +02:00
2010-04-09 09:55:37 +02:00
$classname = ucfirst ( $object );
$this -> $object = new $classname ( $this -> db );
2009-12-15 11:39:22 +01:00
$this -> $object -> fetch ( $this -> origin_id );
}
2008-02-24 18:01:48 +01:00
2011-05-17 20:28:54 +02:00
/**
2011-05-24 11:50:51 +02:00
* Load object from specific field
2011-08-28 14:30:15 +02:00
*
2011-05-17 20:28:54 +02:00
* @ param table Table element or element line
2011-05-24 11:50:51 +02:00
* @ param field Field selected
2011-05-17 20:28:54 +02:00
* @ param key Import key
* @ return int < 0 if KO , > 0 if OK
*/
2011-05-24 11:50:51 +02:00
function fetchObjectFrom ( $table , $field , $key )
2011-05-17 20:28:54 +02:00
{
global $conf ;
2011-05-18 14:34:48 +02:00
2011-05-17 20:28:54 +02:00
$result = false ;
2011-05-18 14:34:48 +02:00
2011-05-17 20:28:54 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . $table ;
2011-05-24 11:50:51 +02:00
$sql .= " WHERE " . $field . " = ' " . $key . " ' " ;
2011-05-17 20:28:54 +02:00
$sql .= " AND entity = " . $conf -> entity ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$result = $this -> fetch ( $row [ 0 ]);
}
2011-05-18 14:34:48 +02:00
2011-05-17 20:28:54 +02:00
return $result ;
}
2011-06-22 11:17:21 +02:00
2011-06-20 08:59:36 +02:00
/**
* Update a specific field from an object
2011-08-28 14:30:15 +02:00
*
2011-06-20 08:59:36 +02:00
* @ param table Table element or element line
* @ param id Object id
* @ param field Field to update
* @ param value New value
* @ return int < 0 if KO , > 0 if OK
*/
function updateObjectField ( $table , $id , $field , $value )
{
global $conf ;
$sql = " UPDATE " . MAIN_DB_PREFIX . $table . " SET " ;
$sql .= $field . " = ' " . $value . " ' " ;
$sql .= " WHERE rowid = " . $id ;
2011-06-22 11:17:21 +02:00
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::updateObjectField sql= " . $sql , LOG_DEBUG );
2011-06-20 08:59:36 +02:00
$resql = $this -> db -> query ( $sql );
2011-06-29 21:29:26 +02:00
if ( $resql )
{
return 1 ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
2011-06-20 08:59:36 +02:00
}
2007-07-29 13:09:04 +02:00
2007-11-05 23:37:41 +01:00
/**
2011-08-28 14:30:15 +02:00
* Load properties id_previous and id_next
*
* @ param filter Optional filter
* @ param fieldid Name of field to use for the select MAX and MIN
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
2007-11-05 23:37:41 +01:00
function load_previous_next_ref ( $filter = '' , $fieldid )
{
2009-11-28 13:06:11 +01:00
global $conf , $user ;
2009-10-04 14:16:45 +02:00
2007-11-05 23:37:41 +01:00
if ( ! $this -> table_element )
{
2011-09-10 20:51:39 +02:00
dol_print_error ( '' , get_class ( $this ) . " ::load_previous_next_ref was called on objet with property table_element not defined " , LOG_ERR );
2007-11-05 23:37:41 +01:00
return - 1 ;
}
2007-10-29 21:36:02 +01:00
2009-10-04 14:16:45 +02:00
// this->ismultientitymanaged contains
// 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
2009-11-28 15:14:21 +01:00
$alias = 's' ;
if ( $this -> element == 'societe' ) $alias = 'te' ;
2009-10-04 14:16:45 +02:00
2009-11-28 13:06:11 +01:00
$sql = " SELECT MAX(te. " . $fieldid . " ) " ;
2009-10-04 14:16:45 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as te " ;
2011-06-22 11:17:21 +02:00
if ( $this -> ismultientitymanaged == 2 || ( $this -> element != 'societe' && empty ( $this -> isnolinkedbythird ) && empty ( $user -> rights -> societe -> client -> voir ))) $sql .= " , " . MAIN_DB_PREFIX . " societe as s " ; // If we need to link to societe to limit select to entity
2011-06-09 01:10:42 +02:00
if ( empty ( $this -> isnolinkedbythird ) && ! $user -> rights -> societe -> client -> voir ) $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as sc ON " . $alias . " .rowid = sc.fk_soc " ;
2011-02-24 19:11:12 +01:00
$sql .= " WHERE te. " . $fieldid . " < ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
2011-06-09 01:10:42 +02:00
if ( empty ( $this -> isnolinkedbythird ) && ! $user -> rights -> societe -> client -> voir ) $sql .= " AND sc.fk_user = " . $user -> id ;
2010-09-09 01:50:52 +02:00
if ( ! empty ( $filter )) $sql .= " AND " . $filter ;
2011-06-09 01:10:42 +02:00
if ( $this -> ismultientitymanaged == 2 || ( $this -> element != 'societe' && empty ( $this -> isnolinkedbythird ) && ! $user -> rights -> societe -> client -> voir )) $sql .= ' AND te.fk_soc = s.rowid' ; // If we need to link to societe to limit select to entity
2011-04-18 12:49:39 +02:00
if ( $this -> ismultientitymanaged == 1 ) $sql .= ' AND te.entity IN (0,' . ( ! empty ( $conf -> entities [ $this -> element ]) ? $conf -> entities [ $this -> element ] : $conf -> entity ) . ')' ;
2008-02-24 15:41:07 +01:00
2009-11-28 15:21:10 +01:00
//print $sql."<br>";
2007-11-05 23:37:41 +01:00
$result = $this -> db -> query ( $sql ) ;
if ( ! $result )
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
$row = $this -> db -> fetch_row ( $result );
$this -> ref_previous = $row [ 0 ];
2008-02-24 15:41:07 +01:00
2008-02-24 18:01:48 +01:00
2009-11-28 13:06:11 +01:00
$sql = " SELECT MIN(te. " . $fieldid . " ) " ;
2009-10-04 14:16:45 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as te " ;
2011-06-09 01:10:42 +02:00
if ( $this -> ismultientitymanaged == 2 || ( $this -> element != 'societe' && empty ( $this -> isnolinkedbythird ) && ! $user -> rights -> societe -> client -> voir )) $sql .= " , " . MAIN_DB_PREFIX . " societe as s " ; // If we need to link to societe to limit select to entity
if ( empty ( $this -> isnolinkedbythird ) && ! $user -> rights -> societe -> client -> voir ) $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as sc ON " . $alias . " .rowid = sc.fk_soc " ;
2011-02-24 19:11:12 +01:00
$sql .= " WHERE te. " . $fieldid . " > ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
2011-06-09 01:10:42 +02:00
if ( empty ( $this -> isnolinkedbythird ) && ! $user -> rights -> societe -> client -> voir ) $sql .= " AND sc.fk_user = " . $user -> id ;
if ( ! empty ( $filter )) $sql .= " AND " . $filter ;
if ( $this -> ismultientitymanaged == 2 || ( $this -> element != 'societe' && empty ( $this -> isnolinkedbythird ) && ! $user -> rights -> societe -> client -> voir )) $sql .= ' AND te.fk_soc = s.rowid' ; // If we need to link to societe to limit select to entity
2011-04-18 12:49:39 +02:00
if ( $this -> ismultientitymanaged == 1 ) $sql .= ' AND te.entity IN (0,' . ( ! empty ( $conf -> entities [ $this -> element ]) ? $conf -> entities [ $this -> element ] : $conf -> entity ) . ')' ;
2009-10-04 14:16:45 +02:00
// Rem: Bug in some mysql version: SELECT MIN(rowid) FROM llx_socpeople WHERE rowid > 1 when one row in database with rowid=1, returns 1 instead of null
2008-02-24 15:41:07 +01:00
2009-04-27 22:37:50 +02:00
//print $sql."<br>";
2007-11-05 23:37:41 +01:00
$result = $this -> db -> query ( $sql ) ;
if ( ! $result )
{
$this -> error = $this -> db -> error ();
return - 2 ;
}
$row = $this -> db -> fetch_row ( $result );
$this -> ref_next = $row [ 0 ];
2008-02-24 18:01:48 +01:00
2007-11-05 23:37:41 +01:00
return 1 ;
}
2008-02-24 18:01:48 +01:00
2009-02-20 23:53:15 +01:00
/**
2011-08-28 14:30:15 +02:00
* Return list of id of contacts of project
*
* @ param source Source of contact : external ( llx_socpeople ) or internal ( llx_user ) or thirdparty ( llx_societe )
* @ return array Array of id of contacts ( if source = external or internal )
2010-04-24 15:39:16 +02:00
* Array of id of third parties with at least one contact on project ( if source = thirdparty )
2009-02-20 23:53:15 +01:00
*/
function getListContactId ( $source = 'external' )
{
$contactAlreadySelected = array ();
$tab = $this -> liste_contact ( - 1 , $source );
$num = sizeof ( $tab );
$i = 0 ;
while ( $i < $num )
{
2010-04-24 15:39:16 +02:00
if ( $source == 'thirdparty' ) $contactAlreadySelected [ $i ] = $tab [ $i ][ 'socid' ];
else $contactAlreadySelected [ $i ] = $tab [ $i ][ 'id' ];
2009-02-20 23:53:15 +01:00
$i ++ ;
}
return $contactAlreadySelected ;
}
2007-11-05 23:37:41 +01:00
2008-02-24 18:01:48 +01:00
2008-02-24 16:51:06 +01:00
/**
2011-08-28 14:30:15 +02:00
* Link element with a project
*
2011-09-12 19:08:02 +02:00
* @ param int $projectid Project id to link element to
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
2010-03-11 15:31:16 +01:00
function setProject ( $projectid )
2008-02-24 16:51:06 +01:00
{
if ( ! $this -> table_element )
{
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::setProject was called on objet with property table_element not defined " , LOG_ERR );
2008-02-24 16:51:06 +01:00
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2010-03-11 15:31:16 +01:00
if ( $projectid ) $sql .= ' SET fk_projet = ' . $projectid ;
2008-02-24 16:51:06 +01:00
else $sql .= ' SET fk_projet = NULL' ;
$sql .= ' WHERE rowid = ' . $this -> id ;
2008-02-24 18:01:48 +01:00
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::setProject sql= " . $sql );
2008-02-24 16:51:06 +01:00
if ( $this -> db -> query ( $sql ))
{
2010-03-11 15:31:16 +01:00
$this -> fk_project = $projectid ;
2008-02-24 16:51:06 +01:00
return 1 ;
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-02-24 16:51:06 +01:00
return - 1 ;
}
}
2008-02-24 18:01:48 +01:00
2008-02-24 17:16:34 +01:00
/**
2011-08-28 14:30:15 +02:00
* Set last model used by doc generator
*
* @ param user User object that make change
* @ param modelpdf Modele name
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
2008-02-24 17:16:34 +01:00
function setDocModel ( $user , $modelpdf )
{
if ( ! $this -> table_element )
{
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::setDocModel was called on objet with property table_element not defined " , LOG_ERR );
2008-02-24 17:16:34 +01:00
return - 1 ;
}
2011-02-20 21:53:26 +01:00
$newmodelpdf = dol_trunc ( $modelpdf , 255 );
2008-02-24 17:16:34 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
2011-02-20 21:53:26 +01:00
$sql .= " SET model_pdf = ' " . $this -> db -> escape ( $newmodelpdf ) . " ' " ;
2008-02-24 17:16:34 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
// if ($this->element == 'facture') $sql.= " AND fk_statut < 2";
// if ($this->element == 'propal') $sql.= " AND fk_statut = 0";
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::setDocModel sql= " . $sql );
2008-02-24 17:16:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> modelpdf = $modelpdf ;
return 1 ;
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-02-24 17:16:34 +01:00
return 0 ;
}
}
2008-02-24 18:01:48 +01:00
2008-02-24 17:46:42 +01:00
/**
2011-09-10 19:52:21 +02:00
* Stocke un numero de rang pour toutes les lignes de detail d 'un element qui n' en ont pas .
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param boolean $renum true to renum all already ordered lines , false to renum only not already ordered lines .
* @ param string $rowidorder ASC or DESC
2009-02-20 23:53:15 +01:00
*/
2011-04-09 17:59:45 +02:00
function line_order ( $renum = false , $rowidorder = 'ASC' )
2008-02-24 17:46:42 +01:00
{
2010-09-06 23:10:45 +02:00
if ( ! $this -> table_element_line )
{
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::line_order was called on objet with property table_element_line not defined " , LOG_ERR );
2010-09-06 23:10:45 +02:00
return - 1 ;
}
if ( ! $this -> fk_element )
{
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::line_order was called on objet with property fk_element not defined " , LOG_ERR );
2010-09-06 23:10:45 +02:00
return - 1 ;
}
$sql = 'SELECT count(rowid) FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . '=' . $this -> id ;
2010-01-05 01:04:45 +01:00
if ( ! $renum ) $sql .= ' AND rang = 0' ;
2010-07-02 17:38:32 +02:00
if ( $renum ) $sql .= ' AND rang <> 0' ;
2008-02-24 17:46:42 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$nl = $row [ 0 ];
}
if ( $nl > 0 )
{
2010-09-06 23:10:45 +02:00
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2011-04-09 17:59:45 +02:00
$sql .= ' ORDER BY rang ASC, rowid ' . $rowidorder ;
2008-02-24 17:46:42 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$row = $this -> db -> fetch_row ( $resql );
2011-04-09 17:59:45 +02:00
$this -> updateRangOfLine ( $row [ 0 ], ( $i + 1 ));
2008-02-24 17:46:42 +01:00
$i ++ ;
}
}
}
}
2010-08-20 18:22:30 +02:00
/**
2011-09-10 19:52:21 +02:00
* Update a line to have a lower rank
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param int $rowid
2010-08-20 18:22:30 +02:00
*/
2008-02-24 17:46:42 +01:00
function line_up ( $rowid )
{
$this -> line_order ();
2010-08-20 17:56:33 +02:00
// Get rang of line
$rang = $this -> getRangOfLine ( $rowid );
2008-02-24 17:46:42 +01:00
2010-08-20 18:22:30 +02:00
// Update position of line
$this -> updateLineUp ( $rowid , $rang );
}
/**
2011-09-10 19:52:21 +02:00
* Update a line to have a higher rank
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param int $rowid
2010-08-20 18:22:30 +02:00
*/
function line_down ( $rowid )
{
$this -> line_order ();
// Get rang of line
$rang = $this -> getRangOfLine ( $rowid );
// Get max value for rang
$max = $this -> line_max ();
// Update position of line
$this -> updateLineDown ( $rowid , $rang , $max );
}
2010-08-22 15:40:44 +02:00
2010-08-20 19:57:25 +02:00
/**
2011-08-28 14:30:15 +02:00
* Update position of line ( rang )
*
2011-09-10 19:52:21 +02:00
* @ param int $rowid
* @ param int $rang
2010-08-20 19:57:25 +02:00
*/
function updateRangOfLine ( $rowid , $rang )
{
2010-09-06 23:10:45 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET rang = ' . $rang ;
2010-08-20 19:57:25 +02:00
$sql .= ' WHERE rowid = ' . $rowid ;
if ( ! $this -> db -> query ( $sql ) )
{
dol_print_error ( $this -> db );
}
}
2010-09-28 20:00:59 +02:00
2010-09-18 09:52:17 +02:00
/**
2011-09-10 19:52:21 +02:00
* Update position of line with ajax ( rang )
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param int $roworder
2010-09-18 09:52:17 +02:00
*/
function line_ajaxorder ( $roworder )
2010-09-28 20:00:59 +02:00
{
2010-09-18 09:52:17 +02:00
$rows = explode ( ',' , $roworder );
2010-09-18 11:18:37 +02:00
$num = count ( $rows );
2010-09-28 20:00:59 +02:00
2010-09-18 09:52:17 +02:00
for ( $i = 0 ; $i < $num ; $i ++ )
{
$this -> updateRangOfLine ( $rows [ $i ], ( $i + 1 ));
}
}
2010-08-22 15:40:44 +02:00
2010-08-20 18:22:30 +02:00
/**
2011-09-10 19:52:21 +02:00
* Update position of line up ( rang )
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param int $rowid
* @ param int $rang
2010-08-20 18:22:30 +02:00
*/
function updateLineUp ( $rowid , $rang )
{
2008-02-24 17:46:42 +01:00
if ( $rang > 1 )
{
2010-09-06 23:10:45 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET rang = ' . $rang ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2008-02-24 17:46:42 +01:00
$sql .= ' AND rang = ' . ( $rang - 1 );
if ( $this -> db -> query ( $sql ) )
{
2010-09-06 23:10:45 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET rang = ' . ( $rang - 1 );
$sql .= ' WHERE rowid = ' . $rowid ;
2008-02-24 17:46:42 +01:00
if ( ! $this -> db -> query ( $sql ) )
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-02-24 17:46:42 +01:00
}
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-02-24 17:46:42 +01:00
}
}
}
2010-08-22 15:40:44 +02:00
2010-08-20 18:22:30 +02:00
/**
2011-09-10 19:52:21 +02:00
* Update position of line down ( rang )
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param int $rowid
* @ param int $rang
* @ param int $max
2010-08-20 18:22:30 +02:00
*/
function updateLineDown ( $rowid , $rang , $max )
2008-02-24 17:46:42 +01:00
{
if ( $rang < $max )
{
2010-09-06 23:10:45 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET rang = ' . $rang ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$sql .= ' AND rang = ' . ( $rang + 1 );
2008-02-24 17:46:42 +01:00
if ( $this -> db -> query ( $sql ) )
{
2010-09-06 23:10:45 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET rang = ' . ( $rang + 1 );
$sql .= ' WHERE rowid = ' . $rowid ;
2008-02-24 17:46:42 +01:00
if ( ! $this -> db -> query ( $sql ) )
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-02-24 17:46:42 +01:00
}
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-02-24 17:46:42 +01:00
}
}
2008-02-24 18:01:48 +01:00
}
2010-08-22 15:40:44 +02:00
2010-08-20 17:56:33 +02:00
/**
2011-09-10 19:52:21 +02:00
* Get position of line ( rang )
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param int $rowid Id of line
* @ return int Value of rang in table of lines
2010-08-20 17:56:33 +02:00
*/
function getRangOfLine ( $rowid )
{
2010-09-06 23:10:45 +02:00
$sql = 'SELECT rang FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE rowid =' . $rowid ;
2010-08-20 17:56:33 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
2010-08-22 15:40:44 +02:00
2010-08-20 19:09:07 +02:00
/**
2011-09-10 19:52:21 +02:00
* Get rowid of the line relative to its position
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param int $rang Rang value
* @ return int Rowid of the line
2010-08-20 19:09:07 +02:00
*/
function getIdOfLine ( $rang )
{
2010-09-06 23:10:45 +02:00
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2010-08-20 19:09:07 +02:00
$sql .= ' AND rang = ' . $rang ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
2010-07-29 17:30:18 +02:00
2010-07-02 20:16:27 +02:00
/**
2011-09-10 19:52:21 +02:00
* Get max value used for position of line ( rang )
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param int $fk_parent_line Parent line id
* @ return int Max value of rang in table of lines
2010-07-02 20:16:27 +02:00
*/
2011-04-09 17:59:45 +02:00
function line_max ( $fk_parent_line = 0 )
2010-07-02 20:16:27 +02:00
{
2011-04-09 17:59:45 +02:00
// Search the last rang with fk_parent_line
if ( $fk_parent_line )
2010-07-02 20:16:27 +02:00
{
2011-04-09 17:59:45 +02:00
$sql = 'SELECT max(rang) FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$sql .= ' AND fk_parent_line = ' . $fk_parent_line ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
if ( ! empty ( $row [ 0 ])) {
return $row [ 0 ];
} else {
return $this -> getRangOfLine ( $fk_parent_line );
}
}
}
// If not, search the last rang of element
else
{
$sql = 'SELECT max(rang) FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
2010-07-02 20:16:27 +02:00
}
}
2008-02-24 18:01:48 +01:00
/**
2011-09-10 19:52:21 +02:00
* Update private note of element
2011-08-28 14:30:15 +02:00
*
2011-09-10 19:52:21 +02:00
* @ param string $note New value for note
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
2008-02-24 18:01:48 +01:00
function update_note ( $note )
{
if ( ! $this -> table_element )
{
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note was called on objet with property table_element not defined " , LOG_ERR );
2008-02-24 18:01:48 +01:00
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2010-02-03 17:57:23 +01:00
// TODO uniformize fields note_private
if ( $this -> table_element == 'fichinter' || $this -> table_element == 'projet' || $this -> table_element == 'projet_task' )
{
2011-02-24 19:11:12 +01:00
$sql .= " SET note_private = ' " . $this -> db -> escape ( $note ) . " ' " ;
2010-02-03 17:57:23 +01:00
}
else
{
2011-02-24 19:11:12 +01:00
$sql .= " SET note = ' " . $this -> db -> escape ( $note ) . " ' " ;
2010-02-03 17:57:23 +01:00
}
2008-02-24 18:01:48 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note sql= " . $sql , LOG_DEBUG );
2008-02-24 18:01:48 +01:00
if ( $this -> db -> query ( $sql ))
{
$this -> note = $note ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note error= " . $this -> error , LOG_ERR );
2008-02-24 18:01:48 +01:00
return - 1 ;
}
}
/**
2011-08-28 14:30:15 +02:00
* Update public note of element
* @ param note_public New value for note
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
2008-02-24 18:01:48 +01:00
function update_note_public ( $note_public )
{
if ( ! $this -> table_element )
{
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note_public was called on objet with property table_element not defined " , LOG_ERR );
2008-02-24 18:01:48 +01:00
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2011-02-24 19:11:12 +01:00
$sql .= " SET note_public = ' " . $this -> db -> escape ( $note_public ) . " ' " ;
2008-02-24 18:01:48 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note_public sql= " . $sql );
2008-02-24 18:01:48 +01:00
if ( $this -> db -> query ( $sql ))
{
$this -> note_public = $note_public ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2008-03-07 11:34:16 +01:00
/**
2011-04-07 14:32:29 +02:00
* Update total_ht , total_ttc and total_vat for an object ( sum of lines )
2011-08-28 14:30:15 +02:00
*
2011-04-11 10:34:26 +02:00
* @ param exclspec Exclude special product ( product_type = 9 )
* @ param roundingadjust - 1 = Use default method ( MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND or 0 ), 0 = Use total of rounding , 1 = Use rounding of total
* @ return int < 0 if KO , > 0 if OK
2009-02-20 23:53:15 +01:00
*/
2011-04-11 10:34:26 +02:00
function update_price ( $exclspec = 0 , $roundingadjust =- 1 )
2008-03-07 11:34:16 +01:00
{
include_once ( DOL_DOCUMENT_ROOT . '/lib/price.lib.php' );
2009-02-16 22:37:28 +01:00
2011-04-11 10:34:26 +02:00
if ( $roundingadjust < 0 && isset ( $conf -> global -> MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND )) $roundingadjust = $conf -> global -> MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND ;
if ( $roundingadjust < 0 ) $roundingadjust = 0 ;
2008-03-07 11:34:16 +01:00
$err = 0 ;
// List lines to sum
$fieldtva = 'total_tva' ;
2010-06-17 21:36:46 +02:00
$fieldlocaltax1 = 'total_localtax1' ;
$fieldlocaltax2 = 'total_localtax2' ;
2011-04-11 10:34:26 +02:00
if ( $this -> element == 'facture_fourn' || $this -> element == 'invoice_supplier' ) $fieldtva = 'tva' ;
2010-07-29 17:30:18 +02:00
2011-04-11 10:34:26 +02:00
$sql = 'SELECT qty, total_ht, ' . $fieldtva . ' as total_tva, ' . $fieldlocaltax1 . ' as total_localtax1, ' . $fieldlocaltax2 . ' as total_localtax2, total_ttc,' ;
$sql .= ' tva_tx as vatrate' ;
2008-03-07 11:34:16 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2011-04-07 14:32:29 +02:00
if ( $exclspec ) $sql .= ' AND product_type <> 9' ;
2008-03-07 11:34:16 +01:00
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::update_price sql= " . $sql );
2008-03-07 11:34:16 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> total_ht = 0 ;
$this -> total_tva = 0 ;
2010-06-17 19:12:34 +02:00
$this -> total_localtax1 = 0 ;
$this -> total_localtax2 = 0 ;
2008-03-07 11:34:16 +01:00
$this -> total_ttc = 0 ;
2009-02-16 22:37:28 +01:00
2008-03-07 11:34:16 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
2011-04-11 01:25:49 +02:00
$this -> total_ht += $obj -> total_ht ;
$this -> total_tva += $obj -> total_tva ;
2010-06-17 19:12:34 +02:00
$this -> total_localtax1 += $obj -> total_localtax1 ;
$this -> total_localtax2 += $obj -> total_localtax2 ;
2011-04-11 01:25:49 +02:00
$this -> total_ttc += $obj -> total_ttc ;
2008-03-07 11:34:16 +01:00
2011-04-11 10:34:26 +02:00
// TODO Also fill array by vat rate
$varates [ $this -> vatrate ][] = array ( 'total_ht' => $obj -> total_ht , 'total_tva' => $obj -> total_tva , 'total_ttc' => $obj -> total_ttc ,
'total_localtax1' => $obj -> total_localtax1 , 'total_localtax2' => $obj -> total_localtax2 );
2008-03-07 11:34:16 +01:00
$i ++ ;
}
$this -> db -> free ( $resql );
2011-04-11 10:34:26 +02:00
// TODO
if ( $roundingadjust )
{
// For each vatrate, calculate if two method of calculation differs
// If it differs
if ( 1 == 2 )
{
// Adjust a line and update it
}
}
2008-03-07 11:34:16 +01:00
// Now update field total_ht, total_ttc and tva
$fieldht = 'total_ht' ;
$fieldtva = 'tva' ;
2010-06-17 21:36:46 +02:00
$fieldlocaltax1 = 'localtax1' ;
$fieldlocaltax2 = 'localtax2' ;
2008-03-19 01:31:03 +01:00
$fieldttc = 'total_ttc' ;
2011-04-11 01:25:49 +02:00
if ( $this -> element == 'facture' || $this -> element == 'facturerec' ) $fieldht = 'total' ;
if ( $this -> element == 'facture_fourn' || $this -> element == 'invoice_supplier' ) $fieldtva = 'total_tva' ;
if ( $this -> element == 'propal' ) $fieldttc = 'total' ;
2009-02-16 22:37:28 +01:00
2008-03-07 11:34:16 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . ' SET' ;
$sql .= " " . $fieldht . " =' " . price2num ( $this -> total_ht ) . " ', " ;
$sql .= " " . $fieldtva . " =' " . price2num ( $this -> total_tva ) . " ', " ;
2010-06-17 19:12:34 +02:00
$sql .= " " . $fieldlocaltax1 . " =' " . price2num ( $this -> total_localtax1 ) . " ', " ;
$sql .= " " . $fieldlocaltax2 . " =' " . price2num ( $this -> total_localtax2 ) . " ', " ;
2008-03-19 01:31:03 +01:00
$sql .= " " . $fieldttc . " =' " . price2num ( $this -> total_ttc ) . " ' " ;
2008-03-07 11:34:16 +01:00
$sql .= ' WHERE rowid = ' . $this -> id ;
2008-03-19 01:31:03 +01:00
//print "xx".$sql;
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::update_price sql= " . $sql );
2008-03-07 11:34:16 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::update_price error= " . $this -> error , LOG_ERR );
2008-03-07 11:34:16 +01:00
return - 1 ;
}
}
else
{
$this -> error = $this -> db -> error ();
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::update_price error= " . $this -> error , LOG_ERR );
2008-03-07 11:34:16 +01:00
return - 1 ;
}
2009-02-16 22:37:28 +01:00
}
2009-12-15 12:46:54 +01:00
2009-12-11 11:17:58 +01:00
/**
2010-12-13 14:16:02 +01:00
* Add objects linked in llx_element_element .
2011-08-28 14:30:15 +02:00
*
2010-12-13 14:16:02 +01:00
* @ return int <= 0 if KO , > 0 if OK
2009-12-11 11:17:58 +01:00
*/
2009-12-11 17:31:42 +01:00
function add_object_linked ()
2009-12-11 11:17:58 +01:00
{
2009-12-11 17:31:42 +01:00
$this -> db -> begin ();
2009-12-15 12:46:54 +01:00
2009-12-11 11:17:58 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " element_element ( " ;
$sql .= " fk_source " ;
$sql .= " , sourcetype " ;
$sql .= " , fk_target " ;
$sql .= " , targettype " ;
$sql .= " ) VALUES ( " ;
2009-12-11 17:31:42 +01:00
$sql .= $this -> origin_id ;
$sql .= " , ' " . $this -> origin . " ' " ;
$sql .= " , " . $this -> id ;
$sql .= " , ' " . $this -> element . " ' " ;
$sql .= " ) " ;
2009-12-15 12:46:54 +01:00
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::add_object_linked sql= " . $sql );
2009-12-11 17:31:42 +01:00
if ( $this -> db -> query ( $sql ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
2010-12-13 14:16:02 +01:00
$this -> error = $this -> db -> lasterror ();
2009-12-11 17:31:42 +01:00
$this -> db -> rollback ();
return 0 ;
}
2009-12-11 11:17:58 +01:00
}
2009-06-04 21:42:34 +02:00
/**
2011-05-06 20:27:18 +02:00
* Fetch array of objects linked to current object . Links are loaded into this -> linked_object array .
2011-08-28 14:30:15 +02:00
*
2011-04-11 10:34:26 +02:00
* @ param sourceid
* @ param sourcetype
* @ param targetid
* @ param targettype
2011-05-06 10:28:28 +02:00
* @ param clause OR , AND
2009-06-04 21:42:34 +02:00
*/
2011-05-06 20:27:18 +02:00
function fetchObjectLinked ( $sourceid = '' , $sourcetype = '' , $targetid = '' , $targettype = '' , $clause = 'OR' )
2009-06-04 21:42:34 +02:00
{
2011-05-06 20:27:18 +02:00
global $conf ;
2011-05-18 14:34:48 +02:00
2011-05-06 20:27:18 +02:00
$this -> linkedObjectsIds = array ();
$this -> linkedObjects = array ();
2011-05-18 14:34:48 +02:00
2011-05-06 10:28:28 +02:00
$justsource = false ;
$justtarget = false ;
2011-05-18 14:34:48 +02:00
2011-05-06 10:28:28 +02:00
if ( ! empty ( $sourceid ) && ! empty ( $sourcetype ) && empty ( $targetid ) && empty ( $targettype )) $justsource = true ;
if ( empty ( $sourceid ) && empty ( $sourcetype ) && ! empty ( $targetid ) && ! empty ( $targettype )) $justtarget = true ;
2009-12-15 12:46:54 +01:00
2011-05-06 20:27:18 +02:00
$sourceid = ( ! empty ( $sourceid ) ? $sourceid : $this -> id );
$targetid = ( ! empty ( $targetid ) ? $targetid : $this -> id );
$sourcetype = ( ! empty ( $sourcetype ) ? $sourcetype : ( ! empty ( $this -> origin ) ? $this -> origin : $this -> element ) );
$targettype = ( ! empty ( $targettype ) ? $targettype : $this -> element );
2009-06-04 21:42:34 +02:00
// Links beetween objects are stored in this table
2009-12-11 11:17:58 +01:00
$sql = 'SELECT fk_source, sourcetype, fk_target, targettype' ;
2009-06-04 21:42:34 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'element_element' ;
2011-05-06 10:28:28 +02:00
$sql .= " WHERE " ;
if ( $justsource || $justtarget )
{
if ( $justsource ) $sql .= " fk_source = ' " . $sourceid . " ' AND sourcetype = ' " . $sourcetype . " ' " ;
if ( $justtarget ) $sql .= " fk_target = ' " . $targetid . " ' AND targettype = ' " . $targettype . " ' " ;
}
else
{
$sql .= " (fk_source = ' " . $sourceid . " ' AND sourcetype = ' " . $sourcetype . " ') " ;
$sql .= " " . $clause . " (fk_target = ' " . $targetid . " ' AND targettype = ' " . $targettype . " ') " ;
}
2011-05-06 20:27:18 +02:00
//print $sql;
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::fetchObjectLink sql= " . $sql );
2009-06-04 21:42:34 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
2009-12-12 21:41:27 +01:00
if ( $obj -> fk_source == $sourceid )
2009-06-04 21:42:34 +02:00
{
2011-05-06 20:27:18 +02:00
$this -> linkedObjectsIds [ $obj -> targettype ][] = $obj -> fk_target ;
2009-06-04 21:42:34 +02:00
}
2009-12-12 21:41:27 +01:00
if ( $obj -> fk_target == $targetid )
2009-06-04 21:42:34 +02:00
{
2011-05-06 20:27:18 +02:00
$this -> linkedObjectsIds [ $obj -> sourcetype ][] = $obj -> fk_source ;
2009-06-04 21:42:34 +02:00
}
$i ++ ;
}
2011-05-18 14:34:48 +02:00
2011-05-06 20:27:18 +02:00
if ( ! empty ( $this -> linkedObjectsIds ))
{
foreach ( $this -> linkedObjectsIds as $objecttype => $objectids )
2011-05-06 10:28:28 +02:00
{
2011-05-06 20:27:18 +02:00
// Parse element/subelement (ex: project_task)
$module = $element = $subelement = $objecttype ;
if ( preg_match ( '/^([^_]+)_([^_]+)/i' , $objecttype , $regs ))
2011-05-06 10:28:28 +02:00
{
2011-05-06 20:27:18 +02:00
$module = $element = $regs [ 1 ];
$subelement = $regs [ 2 ];
2011-05-06 10:28:28 +02:00
}
2011-05-18 14:34:48 +02:00
2011-05-06 20:27:18 +02:00
$classpath = $element . '/class' ;
2011-05-18 14:34:48 +02:00
2011-05-06 20:27:18 +02:00
// To work with non standard path
2011-07-01 18:58:09 +02:00
if ( $objecttype == 'facture' ) { $classpath = 'compta/facture/class' ; }
if ( $objecttype == 'propal' ) { $classpath = 'comm/propal/class' ; }
if ( $objecttype == 'shipping' ) { $classpath = 'expedition/class' ; $subelement = 'expedition' ; $module = 'expedition_bon' ; }
if ( $objecttype == 'delivery' ) { $classpath = 'livraison/class' ; $subelement = 'livraison' ; $module = 'livraison_bon' ; }
if ( $objecttype == 'invoice_supplier' ) { $classpath = 'fourn/class' ; }
if ( $objecttype == 'order_supplier' ) { $classpath = 'fourn/class' ; }
2011-07-02 01:05:39 +02:00
if ( $objecttype == 'fichinter' ) { $classpath = 'fichinter/class' ; $subelement = 'fichinter' ; $module = 'ficheinter' ; }
2011-05-18 14:34:48 +02:00
2011-05-06 20:27:18 +02:00
$classfile = strtolower ( $subelement ); $classname = ucfirst ( $subelement );
if ( $objecttype == 'invoice_supplier' ) { $classfile = 'fournisseur.facture' ; $classname = 'FactureFournisseur' ; }
if ( $objecttype == 'order_supplier' ) { $classfile = 'fournisseur.commande' ; $classname = 'CommandeFournisseur' ; }
2011-05-18 14:34:48 +02:00
2011-05-06 20:27:18 +02:00
if ( $conf -> $module -> enabled && $element != $this -> element )
{
dol_include_once ( '/' . $classpath . '/' . $classfile . '.class.php' );
2011-05-18 14:34:48 +02:00
2011-05-06 20:27:18 +02:00
$num = sizeof ( $objectids );
2011-05-18 14:34:48 +02:00
2011-05-06 20:27:18 +02:00
for ( $i = 0 ; $i < $num ; $i ++ )
{
$object = new $classname ( $this -> db );
$ret = $object -> fetch ( $objectids [ $i ]);
if ( $ret >= 0 )
{
$this -> linkedObjects [ $objecttype ][ $i ] = $object ;
}
}
}
2011-05-06 10:28:28 +02:00
}
2011-05-06 20:27:18 +02:00
}
}
else
{
dol_print_error ( $this -> db );
2011-05-06 10:28:28 +02:00
}
}
2011-05-18 14:34:48 +02:00
2009-12-15 09:15:11 +01:00
/**
2011-04-11 10:34:26 +02:00
* Set statut of an object
2011-08-28 14:30:15 +02:00
*
2011-04-11 10:34:26 +02:00
* @ param statut Statut to set
2011-07-04 11:36:29 +02:00
* @ param elementId Id of element to force ( use this -> id by default )
* @ param elementType Type of element to force ( use -> this -> element by default )
2011-04-11 10:34:26 +02:00
* @ return int < 0 if ko , > 0 if ok
2009-12-15 09:15:11 +01:00
*/
function setStatut ( $statut , $elementId = '' , $elementType = '' )
{
$elementId = ( ! empty ( $elementId ) ? $elementId : $this -> id );
2010-05-13 01:03:33 +02:00
$elementTable = ( ! empty ( $elementType ) ? $elementType : $this -> table_element );
2009-12-15 12:46:54 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $elementTable ;
2009-12-15 09:15:11 +01:00
$sql .= " SET fk_statut = " . $statut ;
$sql .= " WHERE rowid= " . $elementId ;
2009-12-15 12:46:54 +01:00
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::setStatut sql= " . $sql , LOG_DEBUG );
2009-12-15 09:15:11 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
2009-12-15 12:46:54 +01:00
$this -> error = $this -> db -> lasterror ();
2011-09-10 20:51:39 +02:00
dol_syslog ( get_class ( $this ) . " ::setStatut " . $this -> error , LOG_ERR );
2009-12-15 09:15:11 +01:00
return - 1 ;
}
2009-12-15 12:46:54 +01:00
2009-12-15 09:15:11 +01:00
return 1 ;
}
2010-04-24 15:39:16 +02:00
2010-08-18 09:28:12 +02:00
2010-09-02 23:08:29 +02:00
/**
2011-08-23 00:04:21 +02:00
* Load type of canvas of an object if it exists
*
2011-09-10 20:51:39 +02:00
* @ param int $id Record id
* @ param string $ref Record ref
* @ return int < 0 if KO , 0 if nothing done , > 0 if OK
2010-09-02 23:08:29 +02:00
*/
2010-10-03 15:13:41 +02:00
function getCanvas ( $id = 0 , $ref = '' )
2010-09-02 23:08:29 +02:00
{
global $conf ;
2010-10-03 20:49:15 +02:00
2011-08-23 00:04:21 +02:00
if ( empty ( $id ) && empty ( $ref )) return 0 ;
if ( ! empty ( $conf -> global -> MAIN_DISABLE_CANVAS )) return 0 ; // To increase speed. Not enabled by default.
// Clean parameters
2010-10-03 15:13:41 +02:00
$ref = trim ( $ref );
2010-09-02 23:08:29 +02:00
$sql = " SELECT rowid, canvas " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " WHERE entity = " . $conf -> entity ;
2011-08-23 00:04:21 +02:00
if ( ! empty ( $id )) $sql .= " AND rowid = " . $id ;
2010-10-03 15:13:41 +02:00
if ( ! empty ( $ref )) $sql .= " AND ref = ' " . $ref . " ' " ;
2010-09-02 23:08:29 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
2011-08-23 00:04:21 +02:00
if ( $obj )
{
$this -> id = $obj -> rowid ;
$this -> canvas = $obj -> canvas ;
return 1 ;
}
else return 0 ;
2010-09-02 23:08:29 +02:00
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
2011-04-13 09:09:59 +02:00
/**
* Get special code of line
2011-08-28 14:30:15 +02:00
*
2011-04-13 09:09:59 +02:00
* @ param lineid Id of line
*/
function getSpecialCode ( $lineid )
{
$sql = 'SELECT special_code FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE rowid = ' . $lineid ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
2010-08-18 09:28:12 +02:00
2010-09-02 23:08:29 +02:00
2011-06-22 12:58:22 +02:00
/**
* Function to get extra fields of a member into $this -> array_options
2011-08-28 14:30:15 +02:00
*
2011-06-22 12:58:22 +02:00
* @ param rowid
* @ param optionsArray Array resulting of call of extrafields -> fetch_name_optionals_label ()
*/
function fetch_optionals ( $rowid , $optionsArray = '' )
{
if ( ! is_array ( $optionsArray ))
{
// optionsArray not already loaded, so we load it
require_once ( DOL_DOCUMENT_ROOT . " /core/class/extrafields.class.php " );
$extrafields = new ExtraFields ( $this -> db );
$optionsArray = $extrafields -> fetch_name_optionals_label ();
}
2010-09-02 23:08:29 +02:00
2011-06-22 12:58:22 +02:00
// Request to get complementary values
if ( sizeof ( $optionsArray ) > 0 )
{
$sql = " SELECT rowid " ;
foreach ( $optionsArray as $name => $label )
{
$sql .= " , " . $name ;
}
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields " ;
$sql .= " WHERE fk_object = " . $rowid ;
dol_syslog ( get_class ( $this ) . " ::fetch_optionals sql= " . $sql , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$tab = $this -> db -> fetch_array ( $resql );
foreach ( $tab as $key => $value )
{
if ( $key != 'rowid' && $key != 'tms' && $key != 'fk_member' )
{
// we can add this attribute to adherent object
$this -> array_options [ " options_ $key " ] = $value ;
}
}
}
$this -> db -> free ( $resql );
}
else
{
dol_print_error ( $this -> db );
}
}
}
2010-12-14 10:03:50 +01:00
2011-06-22 12:58:22 +02:00
2011-08-28 14:30:15 +02:00
/**
* Function to check if an object is used by others
*
* @ param id Id of object
* @ return int < 0 if KO , 0 if not used , > 0 if already used
*/
function isObjectUsed ( $id )
{
// Check parameters
if ( ! isset ( $this -> childtables ) || ! is_array ( $this -> childtables ) || count ( $this -> childtables ) == 0 )
{
dol_print_error ( 'Called isObjectUsed on a class with property this->childtables not defined' );
return - 1 ;
}
// Test if child exists
$haschild = 0 ;
foreach ( $this -> childtables as $table )
{
// Check if third party can be deleted
$nb = 0 ;
$sql = " SELECT COUNT(*) as nb from " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE " . $this -> fk_element . " = " . $id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$haschild += $obj -> nb ;
//print 'Found into table '.$table;
if ( $haschild ) break ; // We found at least on, we stop here
}
else
{
$this -> error = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . " ::delete error -1 " . $this -> error , LOG_ERR );
return - 1 ;
}
}
if ( $haschild > 0 )
{
$this -> error = " ErrorRecordHasChildren " ;
return $haschild ;
}
else return 0 ;
}
2011-06-22 12:58:22 +02:00
// --------------------
2011-04-11 10:34:26 +02:00
// TODO: All functions here must be redesigned and moved as they are not business functions but output functions
2011-06-22 12:58:22 +02:00
// --------------------
2010-09-02 23:08:29 +02:00
2011-06-04 15:12:44 +02:00
/**
2011-06-06 13:37:59 +02:00
*
2011-06-04 15:12:44 +02:00
* Enter description here ...
* @ param unknown_type $objectid
* @ param unknown_type $objecttype
* @ param unknown_type $withpicto
* @ param unknown_type $option
*/
function getElementUrl ( $objectid , $objecttype , $withpicto = 0 , $option = '' )
{
global $conf ;
2011-06-06 13:37:59 +02:00
2011-06-04 15:12:44 +02:00
// Parse element/subelement (ex: project_task)
$module = $element = $subelement = $objecttype ;
if ( preg_match ( '/^([^_]+)_([^_]+)/i' , $objecttype , $regs ))
{
$module = $element = $regs [ 1 ];
$subelement = $regs [ 2 ];
}
2011-06-06 13:37:59 +02:00
2011-06-04 15:12:44 +02:00
$classpath = $element . '/class' ;
2011-06-06 13:37:59 +02:00
2011-06-04 15:12:44 +02:00
// To work with non standard path
2011-06-06 13:37:59 +02:00
if ( $objecttype == 'facture' || $objecttype == 'invoice' ) { $classpath = 'compta/facture/class' ; $module = 'facture' ; $subelement = 'facture' ; }
if ( $objecttype == 'commande' || $objecttype == 'order' ) { $classpath = 'commande/class' ; $module = 'commande' ; $subelement = 'commande' ; }
2011-06-04 15:12:44 +02:00
if ( $objecttype == 'propal' ) { $classpath = 'comm/propal/class' ; }
if ( $objecttype == 'shipping' ) { $classpath = 'expedition/class' ; $subelement = 'expedition' ; $module = 'expedition_bon' ; }
if ( $objecttype == 'delivery' ) { $classpath = 'livraison/class' ; $subelement = 'livraison' ; $module = 'livraison_bon' ; }
if ( $objecttype == 'invoice_supplier' ) { $classpath = 'fourn/class' ; }
if ( $objecttype == 'order_supplier' ) { $classpath = 'fourn/class' ; }
2011-06-06 13:37:59 +02:00
if ( $objecttype == 'contract' ) { $classpath = 'contrat/class' ; $module = 'contrat' ; $subelement = 'contrat' ; }
2011-06-06 13:45:25 +02:00
if ( $objecttype == 'member' ) { $classpath = 'adherents/class' ; $module = 'adherent' ; $subelement = 'adherent' ; }
2011-06-06 13:37:59 +02:00
2011-06-06 13:45:25 +02:00
//print "objecttype=".$objecttype." module=".$module." subelement=".$subelement;
2011-06-06 13:37:59 +02:00
$classfile = strtolower ( $subelement ); $classname = ucfirst ( $subelement );
2011-06-04 15:12:44 +02:00
if ( $objecttype == 'invoice_supplier' ) { $classfile = 'fournisseur.facture' ; $classname = 'FactureFournisseur' ; }
if ( $objecttype == 'order_supplier' ) { $classfile = 'fournisseur.commande' ; $classname = 'CommandeFournisseur' ; }
2011-06-06 13:37:59 +02:00
2011-06-04 15:12:44 +02:00
if ( $conf -> $module -> enabled )
{
dol_include_once ( '/' . $classpath . '/' . $classfile . '.class.php' );
2011-06-06 13:37:59 +02:00
2011-06-04 15:12:44 +02:00
$object = new $classname ( $this -> db );
$ret = $object -> fetch ( $objectid );
if ( $ret > 0 ) return $object -> getNomUrl ( $withpicto , $option );
}
}
2011-06-06 13:37:59 +02:00
2011-04-11 10:34:26 +02:00
/* This is to show linked object block */
2010-09-02 23:08:29 +02:00
2011-04-11 10:34:26 +02:00
/**
* Show linked object block
* TODO Move this into html . class . php
2011-06-22 12:58:22 +02:00
* But for the moment we don 't know if it' s possible as we keep a method available on overloaded objects .
2011-04-11 10:34:26 +02:00
*/
2011-05-06 20:27:18 +02:00
function showLinkedObjectBlock ()
2011-04-11 10:34:26 +02:00
{
global $langs , $bc ;
2011-05-18 14:34:48 +02:00
2011-05-06 20:27:18 +02:00
$this -> fetchObjectLinked ();
2011-04-11 10:34:26 +02:00
2011-05-06 15:35:15 +02:00
$num = sizeof ( $this -> linkedObjects );
2011-05-06 20:27:18 +02:00
2011-05-06 15:35:15 +02:00
foreach ( $this -> linkedObjects as $objecttype => $objects )
{
$tplpath = $element = $subelement = $objecttype ;
2011-05-18 14:34:48 +02:00
2011-05-06 15:35:15 +02:00
if ( preg_match ( '/^([^_]+)_([^_]+)/i' , $objecttype , $regs ))
{
$element = $regs [ 1 ];
$subelement = $regs [ 2 ];
$tplpath = $element . '/' . $subelement ;
}
2011-05-18 14:34:48 +02:00
2011-05-06 15:35:15 +02:00
// To work with non standard path
2011-08-23 00:04:21 +02:00
if ( $objecttype == 'facture' ) { $tplpath = 'compta/' . $element ; }
if ( $objecttype == 'propal' ) { $tplpath = 'comm/' . $element ; }
if ( $objecttype == 'shipping' ) { $tplpath = 'expedition' ; }
if ( $objecttype == 'delivery' ) { $tplpath = 'livraison' ; }
2011-05-06 15:35:15 +02:00
if ( $objecttype == 'invoice_supplier' ) { $tplpath = 'fourn/facture' ; }
if ( $objecttype == 'order_supplier' ) { $tplpath = 'fourn/commande' ; }
2011-05-18 14:34:48 +02:00
2011-08-23 00:04:21 +02:00
global $linkedObjectBlock ;
$linkedObjectBlock = $objects ;
2011-05-18 14:34:48 +02:00
2011-05-06 15:35:15 +02:00
dol_include_once ( '/' . $tplpath . '/tpl/linkedobjectblock.tpl.php' );
}
2011-05-18 14:34:48 +02:00
2011-05-06 15:35:15 +02:00
return $num ;
2011-04-11 10:34:26 +02:00
}
/* This is to show add lines */
/**
2010-08-05 11:12:07 +02:00
* Show add predefined products / services form
2011-04-11 10:34:26 +02:00
* TODO Edit templates to use global variables and include them directly in controller call
* But for the moment we don 't know if it' s possible as we keep a method available on overloaded objects .
2011-09-12 19:08:02 +02:00
*
* @ param int $dateSelector 1 = Show also date range input fields
* @ param Societe $seller Object thirdparty who sell
* @ param Societe $buyer Object thirdparty who buy
* @ param HookManager $hookmanager Hook manager instance
2010-08-05 11:12:07 +02:00
*/
2011-08-10 12:55:34 +02:00
function formAddPredefinedProduct ( $dateSelector , $seller , $buyer , $hookmanager = false )
2010-08-05 11:12:07 +02:00
{
2011-04-06 09:32:54 +02:00
global $conf , $langs , $object ;
2010-09-14 09:05:31 +02:00
global $html , $bcnd , $var ;
2010-08-05 11:12:07 +02:00
2010-09-15 15:29:17 +02:00
// Use global variables + $dateSelector + $seller and $buyer
2010-09-08 18:09:59 +02:00
include ( DOL_DOCUMENT_ROOT . '/core/tpl/predefinedproductline_create.tpl.php' );
2010-08-05 11:12:07 +02:00
}
2010-08-18 09:28:12 +02:00
2010-08-06 18:10:03 +02:00
/**
* Show add free products / services form
2011-04-11 10:34:26 +02:00
* TODO Edit templates to use global variables and include them directly in controller call
2010-09-08 23:56:39 +02:00
* But for the moment we don 't know if it' st possible as we keep a method available on overloaded objects .
2011-09-12 19:08:02 +02:00
*
* @ param int $dateSelector 1 = Show also date range input fields
* @ param Societe $seller Object thirdparty who sell
* @ param Societe $buyer Object thirdparty who buy
* @ param HookManager $hookmanager Hook manager instance
2010-09-08 23:56:39 +02:00
*/
2011-08-10 12:55:34 +02:00
function formAddFreeProduct ( $dateSelector , $seller , $buyer , $hookmanager = false )
2010-08-06 18:10:03 +02:00
{
2011-04-06 09:32:54 +02:00
global $conf , $langs , $object ;
2010-09-14 09:05:31 +02:00
global $html , $bcnd , $var ;
2011-04-11 01:25:49 +02:00
2010-09-15 15:29:17 +02:00
// Use global variables + $dateSelector + $seller and $buyer
2010-09-08 18:09:59 +02:00
include ( DOL_DOCUMENT_ROOT . '/core/tpl/freeproductline_create.tpl.php' );
2010-08-06 18:10:03 +02:00
}
2010-04-24 15:39:16 +02:00
2011-02-20 21:53:26 +01:00
2010-04-24 15:39:16 +02:00
2011-04-11 10:34:26 +02:00
/* This is to show array of line of details */
2011-03-02 11:38:32 +01:00
2010-08-18 09:28:12 +02:00
2010-08-10 00:39:20 +02:00
/**
2011-04-11 10:34:26 +02:00
* Return HTML table for object lines
* TODO Move this into an output class file ( htmlline . class . php )
* If lines are into a template , title must also be into a template
* But for the moment we don 't know if it' st possible as we keep a method available on overloaded objects .
2011-07-04 11:36:29 +02:00
* @ param $action Action code
* @ param $seller Object of seller third party
* @ param $buyer Object of buyer third party
* @ param $selected Object line selected
* @ param $dateSelector 1 = Show also date range input fields
2010-08-10 00:39:20 +02:00
*/
2011-08-10 19:40:42 +02:00
function printObjectLines ( $action = 'viewline' , $seller , $buyer , $selected = 0 , $dateSelector = 0 , $hookmanager = false )
2010-08-10 00:39:20 +02:00
{
global $conf , $langs ;
2011-05-18 14:34:48 +02:00
2011-04-15 17:47:42 +02:00
// TODO test using div instead of tables
2011-04-15 17:05:28 +02:00
/*
2011-04-16 10:05:01 +02:00
print '<div class="table" id="tablelines">' ;
2011-04-15 17:47:42 +02:00
print '<div class="thead">' ;
2011-04-15 16:45:41 +02:00
print '<div class="tr">' ;
2011-04-16 10:05:01 +02:00
print '<div class="td firstcol">' . $langs -> trans ( 'Description' ) . '</div>' ;
2011-04-15 16:45:41 +02:00
print '<div class="td">' . $langs -> trans ( 'VAT' ) . '</div>' ;
print '<div class="td">' . $langs -> trans ( 'PriceUHT' ) . '</div>' ;
print '<div class="td">' . $langs -> trans ( 'Qty' ) . '</div>' ;
print '<div class="td">' . $langs -> trans ( 'ReductionShort' ) . '</div>' ;
print '<div class="td">' . $langs -> trans ( 'TotalHTShort' ) . '</div>' ;
2011-04-16 10:05:01 +02:00
print '<div class="td endcol"> </div>' ;
print '<div class="td endcol"> </div>' ;
2011-04-15 16:45:41 +02:00
print '<div class="td end"> </div>' ;
2011-04-15 17:47:42 +02:00
print '</div></div>' ;
2011-04-15 17:05:28 +02:00
*/
2011-04-11 10:34:26 +02:00
2010-09-14 09:05:31 +02:00
print '<tr class="liste_titre nodrag nodrop">' ;
2010-08-10 00:39:20 +02:00
print '<td>' . $langs -> trans ( 'Description' ) . '</td>' ;
print '<td align="right" width="50">' . $langs -> trans ( 'VAT' ) . '</td>' ;
print '<td align="right" width="80">' . $langs -> trans ( 'PriceUHT' ) . '</td>' ;
print '<td align="right" width="50">' . $langs -> trans ( 'Qty' ) . '</td>' ;
print '<td align="right" width="50">' . $langs -> trans ( 'ReductionShort' ) . '</td>' ;
print '<td align="right" width="50">' . $langs -> trans ( 'TotalHTShort' ) . '</td>' ;
2010-09-27 18:23:58 +02:00
print '<td width="10"> </td>' ;
print '<td width="10"> </td>' ;
2010-09-28 20:21:01 +02:00
print '<td nowrap="nowrap"> </td>' ; // No width to allow autodim
2010-08-10 00:39:20 +02:00
print " </tr> \n " ;
2011-04-11 10:34:26 +02:00
2010-09-07 15:07:32 +02:00
$num = count ( $this -> lines );
2010-08-10 00:39:20 +02:00
$var = true ;
$i = 0 ;
2011-05-18 14:34:48 +02:00
2011-04-16 10:05:01 +02:00
//print '<div class="tbody">';
2011-05-18 14:34:48 +02:00
2010-09-07 15:07:32 +02:00
foreach ( $this -> lines as $line )
2010-08-10 00:39:20 +02:00
{
$var =! $var ;
2010-08-18 09:28:12 +02:00
2011-08-10 12:55:34 +02:00
if ( is_object ( $hookmanager ) && ( ( $line -> product_type == 9 && ! empty ( $line -> special_code )) || ! empty ( $line -> fk_parent_line ) ) )
2010-09-07 17:14:18 +02:00
{
2011-06-30 15:27:20 +02:00
if ( empty ( $line -> fk_parent_line ))
{
2011-08-10 12:55:34 +02:00
$parameters = array ( 'line' => $line , 'var' => $var , 'num' => $num , 'i' => $i , 'dateSelector' => $dateSelector , 'seller' => $seller , 'buyer' => $buyer , 'selected' => $selected );
2011-08-11 00:47:33 +02:00
$reshook = $hookmanager -> executeHooks ( 'printObjectLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2011-06-30 15:27:20 +02:00
}
2010-09-07 17:14:18 +02:00
}
else
{
2011-08-10 12:55:34 +02:00
$this -> printLine ( $action , $line , $var , $num , $i , $dateSelector , $seller , $buyer , $selected , $hookmanager );
2010-09-07 17:14:18 +02:00
}
2010-08-18 09:28:12 +02:00
2010-08-10 00:39:20 +02:00
$i ++ ;
}
2011-05-18 14:34:48 +02:00
2011-04-16 10:05:01 +02:00
//print '</div></div>';
2010-08-10 00:39:20 +02:00
}
2010-08-18 09:28:12 +02:00
2010-08-10 00:39:20 +02:00
/**
2011-04-11 10:34:26 +02:00
* Return HTML content of a detail line
* TODO Move this into an output class file ( htmlline . class . php )
* If lines are into a template , title must also be into a template
* But for the moment we don 't know if it' s possible as we keep a method available on overloaded objects .
2011-04-16 11:09:04 +02:00
* @ param $action GET / POST action
2010-09-08 23:56:39 +02:00
* @ param $line Selected object line to output
* @ param $var Is it a an odd line
* @ param $num Number of line
* @ param $i
* @ param $dateSelector 1 = Show also date range input fields
2010-09-15 15:29:17 +02:00
* @ param $seller Object of seller third party
* @ param $buyer Object of buyer third party
2011-04-16 10:57:06 +02:00
* @ param $selected Object line selected
2010-08-10 00:39:20 +02:00
*/
2011-08-10 12:55:34 +02:00
function printLine ( $action = 'viewline' , $line , $var = true , $num = 0 , $i = 0 , $dateSelector = 0 , $seller , $buyer , $selected = 0 , $hookmanager = false )
2010-08-10 00:39:20 +02:00
{
global $conf , $langs , $user ;
2010-10-02 22:47:55 +02:00
global $html , $bc , $bcdd ;
2010-08-18 09:28:12 +02:00
2010-08-10 00:39:20 +02:00
$element = $this -> element ;
2010-09-08 23:56:39 +02:00
if ( $element == 'propal' ) $element = 'propale' ; // To work with non standard path
2010-08-18 09:28:12 +02:00
2010-08-10 00:39:20 +02:00
// Show product and description
$type = $line -> product_type ? $line -> product_type : $line -> fk_product_type ;
// Try to enhance type detection using date_start and date_end for free lines where type
// was not saved.
if ( ! empty ( $line -> date_start )) $type = 1 ;
if ( ! empty ( $line -> date_end )) $type = 1 ;
2010-08-18 09:28:12 +02:00
2010-08-10 00:39:20 +02:00
// Ligne en mode visu
2011-04-16 11:09:04 +02:00
if ( $action != 'editline' || $selected != $line -> id )
2010-08-10 00:39:20 +02:00
{
// Produit
if ( $line -> fk_product > 0 )
{
$product_static = new Product ( $db );
2010-09-08 23:56:39 +02:00
2010-09-08 19:02:24 +02:00
$product_static -> type = $line -> fk_product_type ;
$product_static -> id = $line -> fk_product ;
$product_static -> ref = $line -> ref ;
$product_static -> libelle = $line -> product_label ;
$text = $product_static -> getNomUrl ( 1 );
$text .= ' - ' . $line -> product_label ;
$description = ( $conf -> global -> PRODUIT_DESC_IN_FORM ? '' : dol_htmlentitiesbr ( $line -> description ));
2010-09-08 23:56:39 +02:00
2010-09-15 15:29:17 +02:00
// Use global variables + $seller and $buyer
2010-09-08 18:09:59 +02:00
include ( DOL_DOCUMENT_ROOT . '/core/tpl/predefinedproductline_view.tpl.php' );
2011-04-16 10:05:01 +02:00
//include(DOL_DOCUMENT_ROOT.'/core/tpl/predefinedproductlinediv_view.tpl.php');
2010-08-10 00:39:20 +02:00
}
else
{
2010-09-15 15:29:17 +02:00
// Use global variables + $dateSelector + $seller and $buyer
include ( DOL_DOCUMENT_ROOT . '/core/tpl/freeproductline_view.tpl.php' );
2010-08-10 00:39:20 +02:00
}
}
2010-08-18 09:28:12 +02:00
2010-08-10 00:39:20 +02:00
// Ligne en mode update
2011-04-16 11:09:04 +02:00
if ( $this -> statut == 0 && $action == 'editline' && $selected == $line -> id )
2010-08-10 00:39:20 +02:00
{
if ( $line -> fk_product > 0 )
{
2010-09-15 15:29:17 +02:00
// Use global variables + $dateSelector + $seller and $buyer
include ( DOL_DOCUMENT_ROOT . '/core/tpl/predefinedproductline_edit.tpl.php' );
2010-08-10 00:39:20 +02:00
}
2010-09-08 19:02:24 +02:00
else
2010-08-10 00:39:20 +02:00
{
2010-09-15 15:29:17 +02:00
// Use global variables + $dateSelector + $seller and $buyer
include ( DOL_DOCUMENT_ROOT . '/core/tpl/freeproductline_edit.tpl.php' );
2010-08-10 00:39:20 +02:00
}
}
}
2010-12-14 10:03:50 +01:00
2011-04-11 10:34:26 +02:00
/* This is to show array of line of details of source object */
2010-12-17 11:08:31 +01:00
/**
2011-04-11 10:34:26 +02:00
* Return HTML table table of source object lines
* TODO Move this and previous function into output html class file ( htmlline . class . php ) .
* If lines are into a template , title must also be into a template
* But for the moment we don 't know if it' s possible as we keep a method available on overloaded objects .
2010-12-17 11:08:31 +01:00
*/
2011-08-11 17:14:50 +02:00
function printOriginLinesList ( $hookmanager = false )
2010-12-17 11:08:31 +01:00
{
global $langs ;
2011-04-11 10:34:26 +02:00
2010-12-17 11:08:31 +01:00
print '<tr class="liste_titre">' ;
print '<td>' . $langs -> trans ( 'Ref' ) . '</td>' ;
print '<td>' . $langs -> trans ( 'Description' ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( 'VAT' ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( 'PriceUHT' ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( 'Qty' ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( 'ReductionShort' ) . '</td></tr>' ;
2011-04-11 10:34:26 +02:00
2010-12-17 11:08:31 +01:00
$num = count ( $this -> lines );
$var = true ;
$i = 0 ;
foreach ( $this -> lines as $line )
{
$var =! $var ;
2011-08-11 17:14:50 +02:00
if ( is_object ( $hookmanager ) && ( ( $line -> product_type == 9 && ! empty ( $line -> special_code )) || ! empty ( $line -> fk_parent_line ) ) )
2010-12-17 11:08:31 +01:00
{
2011-06-30 15:27:20 +02:00
if ( empty ( $line -> fk_parent_line ))
{
2011-08-11 17:14:50 +02:00
$parameters = array ( 'line' => $line , 'var' => $var , 'i' => $i );
$reshook = $hookmanager -> executeHooks ( 'printOriginObjectLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2011-06-30 15:27:20 +02:00
}
2010-12-17 11:08:31 +01:00
}
else
{
$this -> printOriginLine ( $line , $var );
}
$i ++ ;
}
}
/**
2011-04-11 10:34:26 +02:00
* Return HTML with a line of table array of source object lines
* TODO Move this and previous function into output html class file ( htmlline . class . php ) .
* If lines are into a template , title must also be into a template
* But for the moment we don 't know if it' s possible as we keep a method available on overloaded objects .
* @ param line
* @ param var
2010-12-17 11:08:31 +01:00
*/
function printOriginLine ( $line , $var )
{
global $langs , $bc ;
2011-02-20 21:53:26 +01:00
2010-12-17 11:08:31 +01:00
//var_dump($line);
$date_start = $line -> date_debut_prevue ;
if ( $line -> date_debut_reel ) $date_start = $line -> date_debut_reel ;
$date_end = $line -> date_fin_prevue ;
if ( $line -> date_fin_reel ) $date_end = $line -> date_fin_reel ;
2011-04-11 01:25:49 +02:00
2011-04-09 22:54:02 +02:00
$this -> tpl [ 'label' ] = '' ;
if ( ! empty ( $line -> fk_parent_line )) $this -> tpl [ 'label' ] .= img_picto ( '' , 'rightarrow' );
2011-04-11 01:25:49 +02:00
2011-04-11 10:34:26 +02:00
if (( $line -> info_bits & 2 ) == 2 ) // TODO Not sure this is used for source object
2010-12-17 11:08:31 +01:00
{
$discount = new DiscountAbsolute ( $db );
$discount -> fk_soc = $this -> socid ;
2011-04-09 22:54:02 +02:00
$this -> tpl [ 'label' ] .= $discount -> getNomUrl ( 0 , 'discount' );
2010-12-17 11:08:31 +01:00
}
else if ( $line -> fk_product )
{
$productstatic = new Product ( $this -> db );
$productstatic -> id = $line -> fk_product ;
$productstatic -> ref = $line -> ref ;
$productstatic -> type = $line -> fk_product_type ;
2011-04-09 22:54:02 +02:00
$this -> tpl [ 'label' ] .= $productstatic -> getNomUrl ( 1 );
2010-12-17 11:08:31 +01:00
$this -> tpl [ 'label' ] .= $line -> label ? ' - ' . $line -> label : '' ;
// Dates
2011-06-29 21:29:26 +02:00
if ( $line -> product_type == 1 && ( $date_start || $date_end ))
2010-12-17 11:08:31 +01:00
{
$this -> tpl [ 'label' ] .= get_date_range ( $date_start , $date_end );
}
}
else
{
2011-04-09 22:54:02 +02:00
$this -> tpl [ 'label' ] .= ( $line -> product_type == - 1 ? ' ' : ( $line -> product_type == 1 ? img_object ( $langs -> trans ( '' ), 'service' ) : img_object ( $langs -> trans ( '' ), 'product' )));
2010-12-17 11:08:31 +01:00
$this -> tpl [ 'label' ] .= ( $line -> label ? ' ' . $line -> label : '' );
// Dates
2011-06-29 21:29:26 +02:00
if ( $line -> product_type == 1 && ( $date_start || $date_end ))
2010-12-17 11:08:31 +01:00
{
$this -> tpl [ 'label' ] .= get_date_range ( $date_start , $date_end );
}
}
if ( $line -> desc )
{
2011-04-11 10:34:26 +02:00
if ( $line -> desc == '(CREDIT_NOTE)' ) // TODO Not sure this is used for source object
2010-12-17 11:08:31 +01:00
{
$discount = new DiscountAbsolute ( $this -> db );
$discount -> fetch ( $line -> fk_remise_except );
$this -> tpl [ 'description' ] = $langs -> transnoentities ( " DiscountFromCreditNote " , $discount -> getNomUrl ( 0 ));
}
2011-04-11 10:34:26 +02:00
elseif ( $line -> desc == '(DEPOSIT)' ) // TODO Not sure this is used for source object
2010-12-17 11:08:31 +01:00
{
$discount = new DiscountAbsolute ( $this -> db );
$discount -> fetch ( $line -> fk_remise_except );
$this -> tpl [ 'description' ] = $langs -> transnoentities ( " DiscountFromDeposit " , $discount -> getNomUrl ( 0 ));
}
else
{
$this -> tpl [ 'description' ] = dol_trunc ( $line -> desc , 60 );
}
}
else
{
$this -> tpl [ 'description' ] = ' ' ;
}
2011-04-10 08:10:42 +02:00
$this -> tpl [ 'vat_rate' ] = vatrate ( $line -> tva_tx , true );
2010-12-17 11:08:31 +01:00
$this -> tpl [ 'price' ] = price ( $line -> subprice );
$this -> tpl [ 'qty' ] = (( $line -> info_bits & 2 ) != 2 ) ? $line -> qty : ' ' ;
2011-04-10 08:10:42 +02:00
$this -> tpl [ 'remise_percent' ] = (( $line -> info_bits & 2 ) != 2 ) ? vatrate ( $line -> remise_percent , true ) : ' ' ;
2011-04-11 01:25:49 +02:00
2010-12-17 11:08:31 +01:00
include ( DOL_DOCUMENT_ROOT . '/core/tpl/originproductline.tpl.php' );
}
2011-06-24 09:10:55 +02:00
2011-08-10 02:50:16 +02:00
2011-06-24 09:10:55 +02:00
/**
* Add / Update extra fields
*/
2011-08-20 17:30:38 +02:00
function insertExtraFields ()
2011-06-24 09:10:55 +02:00
{
2011-08-20 17:30:38 +02:00
if ( sizeof ( $this -> array_options ) > 0 )
2011-06-24 09:10:55 +02:00
{
$this -> db -> begin ();
2011-08-20 17:30:38 +02:00
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields WHERE fk_object = " . $this -> id ;
dol_syslog ( get_class ( $this ) . " ::insertExtraFields delete sql= " . $sql_del );
2011-06-24 09:10:55 +02:00
$this -> db -> query ( $sql_del );
2011-08-20 17:30:38 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields (fk_object " ;
foreach ( $this -> array_options as $key => $value )
2011-06-24 09:10:55 +02:00
{
// Add field of attribut
$sql .= " , " . substr ( $key , 8 ); // Remove 'options_' prefix
}
2011-08-20 17:30:38 +02:00
$sql .= " ) VALUES ( " . $this -> id ;
foreach ( $this -> array_options as $key => $value )
2011-06-24 09:10:55 +02:00
{
// Add field o fattribut
2011-08-20 17:30:38 +02:00
if ( $this -> array_options [ $key ] != '' )
2011-06-24 09:10:55 +02:00
{
2011-08-20 17:30:38 +02:00
$sql .= " ,' " . $this -> array_options [ $key ] . " ' " ;
2011-06-24 09:10:55 +02:00
}
else
{
$sql .= " ,null " ;
}
}
$sql .= " ) " ;
2011-08-20 17:30:38 +02:00
dol_syslog ( get_class ( $this ) . " ::insertExtraFields insert sql= " . $sql );
2011-06-24 09:10:55 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
2011-08-20 17:30:38 +02:00
dol_syslog ( get_class ( $this ) . " ::update " . $this -> error , LOG_ERR );
2011-06-24 09:10:55 +02:00
$this -> db -> rollback ();
return - 1 ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
else return 0 ;
}
2006-06-18 17:06:29 +02:00
}
?>