2006-06-18 17:06:29 +02:00
< ? php
2013-09-10 13:45:39 +02:00
/* Copyright ( C ) 2006 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2013-10-12 11:58:38 +02:00
* Copyright ( C ) 2005 - 2013 Regis Houssin < regis . houssin @ capnetworks . com >
2013-08-20 12:26:39 +02:00
* Copyright ( C ) 2010 - 2013 Juanjo Menent < jmenent @ 2 byte . es >
2012-04-28 18:24:52 +02:00
* Copyright ( C ) 2012 Christophe Battarel < christophe . battarel @ altairis . fr >
2012-01-10 17:16:17 +01:00
* Copyright ( C ) 2010 - 2011 Juanjo Menent < jmenent @ 2 byte . es >
2013-10-30 18:17:37 +01:00
* Copyright ( C ) 2012 - 2013 Christophe Battarel < christophe . battarel @ altairis . fr >
2014-03-05 09:57:36 +01:00
* Copyright ( C ) 2011 - 2014 Philippe Grand < philippe . grand @ atoo - net . com >
2012-10-15 00:49:39 +02:00
* Copyright ( C ) 2012 Marcos GarcÃa < marcosgdf @ gmail . com >
2012-01-10 17:16:17 +01: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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-01-10 17:16:17 +01:00
* ( at your option ) any later version .
2012-02-01 13:21:39 +01:00
*
2012-01-10 17:16:17 +01:00
* 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 .
2012-02-01 13:21:39 +01:00
*
2012-01-10 17:16:17 +01:00
* You should have received a copy of the GNU General Public License
* 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
/**
2012-02-01 13:21:39 +01:00
* Parent class of all other business classes ( invoices , contracts , proposals , orders , ... )
2009-02-20 23:53:15 +01:00
*/
2011-08-23 00:04:21 +02:00
abstract class CommonObject
2006-06-18 17:06:29 +02:00
{
2011-11-07 15:04:02 +01:00
protected $db ;
public $error ;
public $errors ;
public $canvas ; // Contains canvas name if it is
2013-06-27 16:02:52 +02:00
public $name ;
2012-07-09 10:08:56 +02:00
public $lastname ;
public $firstname ;
public $civility_id ;
2013-06-27 16:02:52 +02:00
public $import_key ;
2013-07-04 00:11:40 +02:00
2012-07-25 11:46:33 +02:00
public $array_options = array ();
2014-04-26 17:27:59 +02:00
public $linkedObjectsIds ; // Loaded by ->fetchObjectLinked
public $linkedObjects ; // Loaded by ->fetchObjectLinked
2012-07-25 11:46:33 +02:00
2011-11-07 15:04:02 +01:00
// No constructor as it is an abstract class
2014-05-13 10:12:45 +02:00
/**
* Check an object id / ref exists
* If you don ' t need / want to instantiate object and just need to know if object exists , use this method instead of fetch
*
* @ param string $element String of element ( 'product' , 'facture' , ... )
* @ param int $id Id of object
* @ param string $ref Ref of object to check
* @ param string $ref_ext Ref ext of object to check
* @ return int < 0 if KO , 0 if OK but not found , > 0 if OK and exists
*/
static function isExistingObject ( $element , $id , $ref = '' , $ref_ext = '' )
{
global $db ;
$sql = " SELECT rowid, ref, ref_ext " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $element ;
if ( $id > 0 ) $sql .= " WHERE rowid = " . $db -> escape ( $id );
else if ( $ref ) $sql .= " WHERE ref = ' " . $db -> escape ( $ref ) . " ' " ;
else if ( $ref_ext ) $sql .= " WHERE ref_ext = ' " . $db -> escape ( $ref_ext ) . " ' " ;
else {
$error = 'ErrorWrongParameters' ;
dol_print_error ( get_class () . " ::isExistingObject " . $error , LOG_ERR );
return - 1 ;
}
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class () . " ::isExistingObject " , LOG_DEBUG );
2014-05-13 10:12:45 +02:00
$resql = $db -> query ( $sql );
if ( $resql )
{
$num = $db -> num_rows ( $resql );
if ( $num > 0 ) return 1 ;
else return 0 ;
}
return - 1 ;
}
2013-06-28 12:44:17 +02:00
/**
* Method to output saved errors
2013-07-04 00:11:40 +02:00
*
2013-06-28 12:44:17 +02:00
* @ return string String with errors
*/
function errorsToString ()
{
return $this -> error . ( is_array ( $this -> errors ) ? (( $this -> error != '' ? ' ' : '' ) . join ( ',' , $this -> errors )) : '' );
}
2012-02-01 13:21:39 +01:00
/**
* Return full name ( civility + ' ' + name + ' ' + lastname )
*
* @ param Translate $langs Language object for translation of civility
* @ param int $option 0 = No option , 1 = Add civility
* @ param int $nameorder - 1 = Auto , 0 = Lastname + Firstname , 1 = Firstname + Lastname
* @ param int $maxlen Maximum length
* @ return string String with full name
*/
function getFullName ( $langs , $option = 0 , $nameorder =- 1 , $maxlen = 0 )
{
global $conf ;
2013-06-19 18:08:05 +02:00
//print "lastname=".$this->lastname." name=".$this->name." nom=".$this->nom."<br>\n";
2012-02-01 13:21:39 +01:00
$lastname = $this -> lastname ;
$firstname = $this -> firstname ;
2014-05-01 19:17:45 +02:00
if ( empty ( $lastname )) $lastname = ( isset ( $this -> lastname ) ? $this -> lastname : ( isset ( $this -> name ) ? $this -> name : ( isset ( $this -> nom ) ? $this -> nom : '' )));
2012-02-01 13:21:39 +01:00
$ret = '' ;
2014-04-23 11:48:59 +02:00
if ( $option && $this -> civility_id )
2012-02-01 13:21:39 +01:00
{
2014-04-23 11:48:59 +02:00
if ( $langs -> transnoentitiesnoconv ( " Civility " . $this -> civility_id ) != " Civility " . $this -> civility_id ) $ret .= $langs -> transnoentitiesnoconv ( " Civility " . $this -> civility_id ) . ' ' ;
else $ret .= $this -> civility_id . ' ' ;
2012-02-01 13:21:39 +01:00
}
2013-03-11 15:54:01 +01:00
$ret .= dolGetFirstLastname ( $firstname , $lastname , $nameorder );
2012-02-01 13:21:39 +01:00
return dol_trunc ( $ret , $maxlen );
}
2013-06-05 16:24:32 +02:00
/**
* Return full address of contact
*
* @ param int $withcountry 1 = Add country into address string
* @ param string $sep Separator to use to build string
* @ return string Full address string
*/
function getFullAddress ( $withcountry = 0 , $sep = " \n " )
{
if ( $withcountry && $this -> country_id && ( empty ( $this -> country_code ) || empty ( $this -> country )))
{
require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php' ;
$tmparray = getCountry ( $this -> country_id , 'all' );
$this -> country_code = $tmparray [ 'code' ];
$this -> country = $tmparray [ 'label' ];
}
return dol_format_address ( $this , $withcountry , $sep );
2013-04-07 17:39:08 +02:00
}
2011-11-07 15:04:02 +01:00
/**
* Add a link between element $this -> element and a contact
*
2012-02-01 13:21:39 +01:00
* @ param int $fk_socpeople Id of contact to link
* @ param int $type_contact Type of contact ( code or id )
* @ param int $source external = Contact extern ( llx_socpeople ), internal = Contact intern ( llx_user )
* @ param int $notrigger Disable all triggers
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
function add_contact ( $fk_socpeople , $type_contact , $source = 'external' , $notrigger = 0 )
{
global $user , $conf , $langs ;
2011-12-07 15:06:29 +01:00
2011-11-07 15:04:02 +01:00
dol_syslog ( get_class ( $this ) . " ::add_contact $fk_socpeople , $type_contact , $source " );
// Check parameters
if ( $fk_socpeople <= 0 )
{
$this -> error = $langs -> trans ( " ErrorWrongValueForParameter " , " 1 " );
dol_syslog ( get_class ( $this ) . " ::add_contact " . $this -> error , LOG_ERR );
return - 1 ;
}
if ( ! $type_contact )
{
$this -> error = $langs -> trans ( " ErrorWrongValueForParameter " , " 2 " );
dol_syslog ( get_class ( $this ) . " ::add_contact " . $this -> error , LOG_ERR );
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 ;
}
}
$datecreate = dol_now ();
2014-06-25 16:51:26 +02:00
$this -> db -> begin ();
2014-06-26 23:04:26 +02:00
2011-11-07 15:04:02 +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 . " , " ;
2013-05-17 19:30:48 +02:00
$sql .= " ' " . $this -> db -> idate ( $datecreate ) . " ' " ;
2011-11-07 15:04:02 +01:00
$sql .= " , 4, ' " . $id_type_contact . " ' " ;
$sql .= " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::add_contact " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( ! $notrigger )
{
2014-06-25 16:51:26 +02:00
$result = $this -> call_trigger ( strtoupper ( $this -> element ) . '_ADD_CONTACT' , $user );
if ( $result < 0 ) { $this -> db -> rollback (); return - 1 ; }
2011-11-07 15:04:02 +01:00
}
2014-06-25 16:51:26 +02:00
$this -> db -> commit ();
2011-11-07 15:04:02 +01:00
return 1 ;
}
else
{
if ( $this -> db -> errno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' )
{
$this -> error = $this -> db -> errno ();
2014-07-07 11:32:56 +02:00
$this -> db -> rollback ();
2011-11-07 15:04:02 +01:00
return - 2 ;
}
else
{
$this -> error = $this -> db -> error ();
2014-07-07 11:32:56 +02:00
$this -> db -> rollback ();
2011-11-07 15:04:02 +01:00
return - 1 ;
}
}
}
2014-01-16 11:43:59 +01:00
/**
* Copy contact from one element to current
*
2014-04-23 11:52:48 +02:00
* @ param CommonObject $objFrom Source element
* @ param string $source Nature of contact ( 'internal' or 'external' )
2014-01-24 10:37:19 +01:00
* @ return int > 0 if OK , < 0 if KO
2014-01-16 11:43:59 +01:00
*/
function copy_linked_contact ( $objFrom , $source = 'internal' )
{
global $user , $langs , $conf ;
$contacts = $objFrom -> liste_contact ( - 1 , $source );
foreach ( $contacts as $contact )
{
if ( $this -> add_contact ( $contact [ 'id' ], $contact [ 'fk_c_type_contact' ], $contact [ 'source' ]) < 0 )
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
return 1 ;
}
2011-11-07 15:04:02 +01:00
/**
* Update a link to contact line
*
2012-02-01 13:21:39 +01:00
* @ param int $rowid Id of line contact - element
* @ param int $statut New status of link
* @ param int $type_contact_id Id of contact type ( not modified if 0 )
2012-12-01 15:45:05 +01:00
* @ param int $fk_socpeople Id of soc_people to update ( not modified if 0 )
2012-02-01 13:21:39 +01:00
* @ return int < 0 if KO , >= 0 if OK
2011-11-07 15:04:02 +01:00
*/
2012-12-01 15:45:05 +01:00
function update_contact ( $rowid , $statut , $type_contact_id = 0 , $fk_socpeople = 0 )
2011-11-07 15:04:02 +01:00
{
// Insertion dans la base
$sql = " UPDATE " . MAIN_DB_PREFIX . " element_contact set " ;
$sql .= " statut = " . $statut ;
if ( $type_contact_id ) $sql .= " , fk_c_type_contact = ' " . $type_contact_id . " ' " ;
2012-12-01 15:45:05 +01:00
if ( $fk_socpeople ) $sql .= " , fk_socpeople = ' " . $fk_socpeople . " ' " ;
2011-11-07 15:04:02 +01:00
$sql .= " where rowid = " . $rowid ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
return 0 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
/**
* Delete a link to contact line
*
2012-02-01 13:21:39 +01:00
* @ param int $rowid Id of contact link line to delete
* @ param int $notrigger Disable all triggers
* @ return int > 0 if OK , < 0 if KO
2011-11-07 15:04:02 +01:00
*/
function delete_contact ( $rowid , $notrigger = 0 )
{
global $user , $langs , $conf ;
2011-12-07 15:06:29 +01:00
2014-06-25 16:51:26 +02:00
$this -> db -> begin ();
2011-12-07 15:06:29 +01:00
2011-11-07 15:04:02 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_contact " ;
$sql .= " WHERE rowid = " . $rowid ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_contact " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
if ( $this -> db -> query ( $sql ))
{
if ( ! $notrigger )
{
2014-06-25 16:51:26 +02:00
$result = $this -> call_trigger ( strtoupper ( $this -> element ) . '_DELETE_CONTACT' , $user );
if ( $result < 0 ) { $this -> db -> rollback (); return - 1 ; }
2011-11-07 15:04:02 +01:00
}
2014-06-25 16:51:26 +02:00
$this -> db -> commit ();
2011-11-07 15:04:02 +01:00
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
2014-06-25 16:51:26 +02:00
$this -> db -> rollback ();
2011-11-07 15:04:02 +01:00
return - 1 ;
}
}
/**
* Delete all links between an object $this and all its contacts
*
* @ return int > 0 if OK , < 0 if KO
*/
function delete_linked_contact ()
{
$temp = array ();
$typeContact = $this -> liste_type_contact ( '' );
foreach ( $typeContact as $key => $value )
{
array_push ( $temp , $key );
}
$listId = implode ( " , " , $temp );
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_contact " ;
$sql .= " WHERE element_id = " . $this -> id ;
$sql .= " AND fk_c_type_contact IN ( " . $listId . " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_linked_contact " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
if ( $this -> db -> query ( $sql ))
{
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
/**
* Get array of all contacts for an object
*
2012-02-01 13:21:39 +01: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
2011-11-07 15:04:02 +01:00
*/
function liste_contact ( $statut =- 1 , $source = 'external' , $list = 0 )
{
global $langs ;
$tab = array ();
2014-01-16 11:43:59 +01:00
$sql = " SELECT ec.rowid, ec.statut, ec.fk_socpeople as id, ec.fk_c_type_contact " ; // This field contains id of llx_socpeople or id of llx_user
2011-11-07 15:04:02 +01:00
if ( $source == 'internal' ) $sql .= " , '-1' as socid " ;
if ( $source == 'external' || $source == 'thirdparty' ) $sql .= " , t.fk_soc as socid " ;
2013-03-05 00:14:04 +01:00
$sql .= " , t.civilite as civility, t.lastname as lastname, t.firstname, t.email " ;
2011-11-07 15:04:02 +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 " ;
if ( $source == 'internal' ) $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " user t on ec.fk_socpeople = t.rowid " ;
if ( $source == 'external' || $source == 'thirdparty' ) $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " socpeople t on ec.fk_socpeople = t.rowid " ;
$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' " ;
if ( $source == 'external' || $source == 'thirdparty' ) $sql .= " AND tc.source = 'external' " ;
$sql .= " AND tc.active=1 " ;
if ( $statut >= 0 ) $sql .= " AND ec.statut = ' " . $statut . " ' " ;
2013-03-05 00:14:04 +01:00
$sql .= " ORDER BY t.lastname ASC " ;
2011-11-07 15:04:02 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::liste_contact " , LOG_DEBUG );
2011-11-07 15:04:02 +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 );
if ( ! $list )
{
$transkey = " TypeContact_ " . $obj -> element . " _ " . $obj -> source . " _ " . $obj -> code ;
$libelle_type = ( $langs -> trans ( $transkey ) != $transkey ? $langs -> trans ( $transkey ) : $obj -> libelle );
$tab [ $i ] = array ( 'source' => $obj -> source , 'socid' => $obj -> socid , 'id' => $obj -> id ,
2011-07-10 18:50:40 +02:00
'nom' => $obj -> lastname , // For backward compatibility
'civility' => $obj -> civility , 'lastname' => $obj -> lastname , 'firstname' => $obj -> firstname , 'email' => $obj -> email ,
2014-01-16 11:43:59 +01:00
'rowid' => $obj -> rowid , 'code' => $obj -> code , 'libelle' => $libelle_type , 'status' => $obj -> statut , 'fk_c_type_contact' => $obj -> fk_c_type_contact );
2011-11-07 15:04:02 +01:00
}
else
{
$tab [ $i ] = $obj -> id ;
}
$i ++ ;
}
return $tab ;
}
else
{
$this -> error = $this -> db -> error ();
dol_print_error ( $this -> db );
return - 1 ;
}
}
/**
* Update status of a contact linked to object
*
2012-02-01 13:21:39 +01:00
* @ param int $rowid Id of link between object and contact
* @ return int < 0 if KO , >= 0 if OK
2011-11-07 15:04:02 +01:00
*/
function swapContactStatus ( $rowid )
{
$sql = " SELECT ec.datecreate, ec.statut, ec.fk_socpeople, ec.fk_c_type_contact, " ;
$sql .= " tc.code, tc.libelle " ;
//$sql.= ", s.fk_soc";
$sql .= " FROM ( " . MAIN_DB_PREFIX . " element_contact as ec, " . MAIN_DB_PREFIX . " c_type_contact as tc) " ;
//$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
$sql .= " WHERE ec.rowid = " . $rowid ;
$sql .= " AND ec.fk_c_type_contact=tc.rowid " ;
$sql .= " AND tc.element = ' " . $this -> element . " ' " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::swapContactStatus " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$newstatut = ( $obj -> statut == 4 ) ? 5 : 4 ;
$result = $this -> update_contact ( $rowid , $newstatut );
$this -> db -> free ( $resql );
return $result ;
}
else
{
$this -> error = $this -> db -> error ();
dol_print_error ( $this -> db );
return - 1 ;
}
}
/**
* Return array with list of possible values for type of contacts
*
2012-02-01 13:21:39 +01:00
* @ param string $source 'internal' , 'external' or 'all'
* @ param string $order Sort order by : 'code' or 'rowid'
* @ param string $option 0 = Return array id -> label , 1 = Return array code -> label
2013-09-25 11:14:50 +02:00
* @ param string $activeonly 0 = all type of contact , 1 = only the active
2012-02-01 13:21:39 +01:00
* @ return array Array list of type of contacts ( id -> label if option = 0 , code -> label if option = 1 )
2011-11-07 15:04:02 +01:00
*/
2013-09-25 10:15:32 +02:00
function liste_type_contact ( $source = 'internal' , $order = 'code' , $option = 0 , $activeonly = 0 )
2011-11-07 15:04:02 +01:00
{
global $langs ;
$tab = array ();
$sql = " SELECT DISTINCT tc.rowid, tc.code, tc.libelle " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
$sql .= " WHERE tc.element=' " . $this -> element . " ' " ;
2013-09-25 10:15:32 +02:00
if ( $activeonly == 1 )
$sql .= " AND tc.active=1 " ; // only the active type
2013-10-12 11:58:38 +02:00
2011-11-07 15:04:02 +01:00
if ( ! empty ( $source )) $sql .= " AND tc.source=' " . $source . " ' " ;
$sql .= " ORDER by tc. " . $order ;
//print "sql=".$sql;
$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 );
if ( empty ( $option )) $tab [ $obj -> rowid ] = $libelle_type ;
else $tab [ $obj -> code ] = $libelle_type ;
$i ++ ;
}
return $tab ;
}
else
{
$this -> error = $this -> db -> lasterror ();
//dol_print_error($this->db);
return null ;
}
}
/**
* 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' )
*
2012-02-01 13:21:39 +01:00
* @ param string $source 'external' or 'internal'
* @ param string $code 'BILLING' , 'SHIPPING' , 'SALESREPFOLL' , ...
* @ param int $status limited to a certain status
* @ return array List of id for such contacts
2011-11-07 15:04:02 +01:00
*/
function getIdContact ( $source , $code , $status = 0 )
{
global $conf ;
$result = array ();
$i = 0 ;
$sql = " SELECT ec.fk_socpeople " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " element_contact as ec, " ;
if ( $source == 'internal' ) $sql .= " " . MAIN_DB_PREFIX . " user as c, " ;
if ( $source == 'external' ) $sql .= " " . MAIN_DB_PREFIX . " socpeople as c, " ;
$sql .= " " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
$sql .= " WHERE ec.element_id = " . $this -> id ;
$sql .= " AND ec.fk_socpeople = c.rowid " ;
2012-02-11 08:03:26 +01:00
if ( $source == 'internal' ) $sql .= " AND c.entity IN (0, " . $conf -> entity . " ) " ;
if ( $source == 'external' ) $sql .= " AND c.entity IN ( " . getEntity ( 'societe' , 1 ) . " ) " ;
2011-11-07 15:04:02 +01:00
$sql .= " AND ec.fk_c_type_contact = tc.rowid " ;
$sql .= " AND tc.element = ' " . $this -> element . " ' " ;
$sql .= " AND tc.source = ' " . $source . " ' " ;
$sql .= " AND tc.code = ' " . $code . " ' " ;
$sql .= " AND tc.active = 1 " ;
if ( $status ) $sql .= " AND ec.statut = " . $status ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::getIdContact " , LOG_DEBUG );
2011-11-07 15:04:02 +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 ();
return null ;
}
return $result ;
}
2009-02-20 23:53:15 +01:00
2011-11-07 15:04:02 +01:00
/**
* Charge le contact d ' id $id dans this -> contact
*
2012-02-01 13:21:39 +01:00
* @ param int $contactid Id du contact
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
function fetch_contact ( $contactid )
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php' ;
2011-11-07 15:04:02 +01:00
$contact = new Contact ( $this -> db );
$result = $contact -> fetch ( $contactid );
$this -> contact = $contact ;
return $result ;
}
/**
2014-06-29 04:25:42 +02:00
* Load the third party of object , from id $this -> socid or $this -> fk_soc , into this -> thirdparty
2011-11-07 15:04:02 +01:00
*
2012-02-01 13:21:39 +01:00
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
function fetch_thirdparty ()
{
global $conf ;
2014-05-03 03:10:58 +02:00
if ( empty ( $this -> socid ) && empty ( $this -> fk_soc )) return 0 ;
2011-11-07 15:04:02 +01:00
$thirdparty = new Societe ( $this -> db );
2014-05-03 03:10:58 +02:00
$result = $thirdparty -> fetch ( isset ( $this -> socid ) ? $this -> socid : $this -> fk_soc );
2011-11-07 15:04:02 +01:00
$this -> client = $thirdparty ; // deprecated
$this -> thirdparty = $thirdparty ;
// Use first price level if level not defined for third party
2012-05-29 23:00:12 +02:00
if ( ! empty ( $conf -> global -> PRODUIT_MULTIPRICES ) && empty ( $this -> thirdparty -> price_level ))
2011-11-07 15:04:02 +01:00
{
$this -> client -> price_level = 1 ; // deprecated
$this -> thirdparty -> price_level = 1 ;
}
return $result ;
}
2011-12-07 15:06:29 +01:00
/**
2014-01-04 12:29:20 +01:00
* Load data for barcode into properties -> barcode_type *
2014-05-25 17:14:33 +02:00
* Properties -> barcode_type that is id of barcode type is used to find other properties , but
* if it is not defined , -> element must be defined to know default barcode type .
2011-12-07 15:06:29 +01:00
*
2014-05-25 17:14:33 +02:00
* @ return int < 0 if KO , 0 if can ' t guess type of barcode ( ISBN , EAN13 ... ), > 0 if OK ( all barcode properties loaded )
2011-12-07 15:06:29 +01:00
*/
function fetch_barcode ()
{
global $conf ;
dol_syslog ( get_class ( $this ) . '::fetch_barcode this->element=' . $this -> element . ' this->barcode_type=' . $this -> barcode_type );
2014-05-29 21:01:00 +02:00
2011-12-07 15:06:29 +01:00
$idtype = $this -> barcode_type ;
2014-05-25 17:14:33 +02:00
if ( empty ( $idtype ) && $idtype != '0' ) // If type of barcode no set, we try to guess. If set to '0' it means we forced to have type remain not defined
2011-12-07 15:06:29 +01:00
{
if ( $this -> element == 'product' ) $idtype = $conf -> global -> PRODUIT_DEFAULT_BARCODE_TYPE ;
else if ( $this -> element == 'societe' ) $idtype = $conf -> global -> GENBARCODE_BARCODETYPE_THIRDPARTY ;
2014-05-25 17:14:33 +02:00
else dol_syslog ( 'Call fetch_barcode with barcode_type not defined and cant be guessed' , LOG_WARNING );
2011-12-07 15:06:29 +01:00
}
2014-05-29 21:01:00 +02:00
2011-12-07 15:06:29 +01:00
if ( $idtype > 0 )
{
if ( empty ( $this -> barcode_type ) || empty ( $this -> barcode_type_code ) || empty ( $this -> barcode_type_label ) || empty ( $this -> barcode_type_coder )) // If data not already loaded
{
$sql = " SELECT rowid, code, libelle as label, coder " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_barcode_type " ;
$sql .= " WHERE rowid = " . $idtype ;
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::fetch_barcode' , LOG_DEBUG );
2011-12-07 15:06:29 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$this -> barcode_type = $obj -> rowid ;
$this -> barcode_type_code = $obj -> code ;
$this -> barcode_type_label = $obj -> label ;
$this -> barcode_type_coder = $obj -> coder ;
return 1 ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
}
else return 0 ;
}
2011-11-07 15:04:02 +01:00
/**
* Charge le projet d ' id $this -> fk_project dans this -> projet
*
* @ return int < 0 if KO , >= 0 if OK
*/
function fetch_projet ()
{
if ( empty ( $this -> fk_project )) return 0 ;
$project = new Project ( $this -> db );
$result = $project -> fetch ( $this -> fk_project );
$this -> projet = $project ;
return $result ;
}
/**
* Charge le user d ' id userid dans this -> user
*
2012-02-01 13:21:39 +01:00
* @ param int $userid Id du contact
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
function fetch_user ( $userid )
{
$user = new User ( $this -> db );
$result = $user -> fetch ( $userid );
$this -> user = $user ;
return $result ;
}
/**
2012-02-01 13:21:39 +01:00
* Read linked origin object
*
* @ return void
2011-11-07 15:04:02 +01:00
*/
function fetch_origin ()
{
// TODO uniformise code
if ( $this -> origin == 'shipping' ) $this -> origin = 'expedition' ;
if ( $this -> origin == 'delivery' ) $this -> origin = 'livraison' ;
2013-08-07 15:08:30 +02:00
$origin = $this -> origin ;
2011-11-07 15:04:02 +01:00
2013-08-07 15:08:30 +02:00
$classname = ucfirst ( $origin );
$this -> $origin = new $classname ( $this -> db );
$this -> $origin -> fetch ( $this -> origin_id );
2011-11-07 15:04:02 +01:00
}
/**
* Load object from specific field
*
2012-02-01 13:21:39 +01:00
* @ param string $table Table element or element line
* @ param string $field Field selected
* @ param string $key Import key
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
function fetchObjectFrom ( $table , $field , $key )
{
global $conf ;
$result = false ;
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE " . $field . " = ' " . $key . " ' " ;
$sql .= " AND entity = " . $conf -> entity ;
2012-03-18 17:47:17 +01:00
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::fetchObjectFrom' , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$result = $this -> fetch ( $row [ 0 ]);
}
return $result ;
}
/**
* Load value from specific field
*
* @ param string $table Table of element or element line
* @ param int $id Element id
* @ param string $field Field selected
* @ return int < 0 if KO , > 0 if OK
*/
function getValueFrom ( $table , $id , $field )
{
$result = false ;
$sql = " SELECT " . $field . " FROM " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE rowid = " . $id ;
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::getValueFrom' , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$result = $row [ 0 ];
}
return $result ;
}
/**
2013-07-19 13:38:57 +02:00
* Update a specific field into database
2011-11-07 15:04:02 +01:00
*
* @ param string $field Field to update
* @ param mixte $value New value
2013-07-19 13:38:57 +02:00
* @ param string $table To force other table element or element line ( should not be used )
* @ param int $id To force other object id ( should not be used )
* @ param string $format Data format ( 'text' , 'date' ) . 'text' is used if not defined
* @ param string $id_field To force rowid field name . 'rowid' is used it not defined
* @ param string $user Update last update fields also if user object provided
2011-11-07 15:04:02 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2013-07-19 13:38:57 +02:00
function setValueFrom ( $field , $value , $table = '' , $id = '' , $format = '' , $id_field = '' , $user = '' )
2011-11-07 15:04:02 +01:00
{
global $conf ;
2013-07-19 13:38:57 +02:00
if ( empty ( $table )) $table = $this -> table_element ;
if ( empty ( $id )) $id = $this -> id ;
if ( empty ( $format )) $format = 'text' ;
if ( empty ( $id_field )) $id_field = 'rowid' ;
2011-11-07 15:04:02 +01:00
$this -> db -> begin ();
$sql = " UPDATE " . MAIN_DB_PREFIX . $table . " SET " ;
if ( $format == 'text' ) $sql .= $field . " = ' " . $this -> db -> escape ( $value ) . " ' " ;
else if ( $format == 'date' ) $sql .= $field . " = ' " . $this -> db -> idate ( $value ) . " ' " ;
2013-07-19 13:38:57 +02:00
if ( is_object ( $user )) $sql .= " , fk_user_modif = " . $user -> id ;
2013-03-02 16:17:12 +01:00
$sql .= " WHERE " . $id_field . " = " . $id ;
2011-11-07 15:04:02 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " :: " . __FUNCTION__ . " " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
}
/**
* Load properties id_previous and id_next
*
2012-02-01 13:21:39 +01:00
* @ param string $filter Optional filter
* @ param int $fieldid Name of field to use for the select MAX and MIN
2014-04-09 13:19:31 +02:00
* @ param int $nodbprefix Do not include DB prefix to forge table name
2012-02-01 13:21:39 +01:00
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
2014-04-09 13:19:31 +02:00
function load_previous_next_ref ( $filter , $fieldid , $nodbprefix = 0 )
2011-11-07 15:04:02 +01:00
{
global $conf , $user ;
if ( ! $this -> table_element )
{
dol_print_error ( '' , get_class ( $this ) . " ::load_previous_next_ref was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
// this->ismultientitymanaged contains
// 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
$alias = 's' ;
if ( $this -> element == 'societe' ) $alias = 'te' ;
$sql = " SELECT MAX(te. " . $fieldid . " ) " ;
2014-04-09 13:19:31 +02:00
$sql .= " FROM " . ( empty ( $nodbprefix ) ? MAIN_DB_PREFIX : '' ) . $this -> table_element . " as te " ;
2012-07-10 22:10:12 +02:00
if ( isset ( $this -> ismultientitymanaged ) && $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-11-07 15:04:02 +01: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 " ;
$sql .= " WHERE te. " . $fieldid . " < ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
if ( empty ( $this -> isnolinkedbythird ) && ! $user -> rights -> societe -> client -> voir ) $sql .= " AND sc.fk_user = " . $user -> id ;
if ( ! empty ( $filter )) $sql .= " AND " . $filter ;
2012-07-10 22:10:12 +02:00
if ( isset ( $this -> ismultientitymanaged ) && $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
if ( isset ( $this -> ismultientitymanaged ) && $this -> ismultientitymanaged == 1 ) $sql .= ' AND te.entity IN (' . getEntity ( $this -> element , 1 ) . ')' ;
2011-11-07 15:04:02 +01:00
//print $sql."<br>";
$result = $this -> db -> query ( $sql );
if ( ! $result )
{
2014-04-09 13:19:31 +02:00
$this -> error = $this -> db -> lasterror ();
2011-11-07 15:04:02 +01:00
return - 1 ;
}
$row = $this -> db -> fetch_row ( $result );
$this -> ref_previous = $row [ 0 ];
$sql = " SELECT MIN(te. " . $fieldid . " ) " ;
2014-04-09 13:19:31 +02:00
$sql .= " FROM " . ( empty ( $nodbprefix ) ? MAIN_DB_PREFIX : '' ) . $this -> table_element . " as te " ;
2012-07-10 22:10:12 +02:00
if ( isset ( $this -> ismultientitymanaged ) && $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
2011-11-07 15:04:02 +01: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 " ;
$sql .= " WHERE te. " . $fieldid . " > ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
if ( empty ( $this -> isnolinkedbythird ) && ! $user -> rights -> societe -> client -> voir ) $sql .= " AND sc.fk_user = " . $user -> id ;
if ( ! empty ( $filter )) $sql .= " AND " . $filter ;
2012-07-10 22:10:12 +02:00
if ( isset ( $this -> ismultientitymanaged ) && $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
if ( isset ( $this -> ismultientitymanaged ) && $this -> ismultientitymanaged == 1 ) $sql .= ' AND te.entity IN (' . getEntity ( $this -> element , 1 ) . ')' ;
2011-11-07 15:04:02 +01: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
//print $sql."<br>";
$result = $this -> db -> query ( $sql );
if ( ! $result )
{
2014-04-09 13:19:31 +02:00
$this -> error = $this -> db -> lasterror ();
2011-11-07 15:04:02 +01:00
return - 2 ;
}
$row = $this -> db -> fetch_row ( $result );
$this -> ref_next = $row [ 0 ];
return 1 ;
}
/**
* Return list of id of contacts of project
*
2012-02-01 13:21:39 +01:00
* @ param string $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 )
* Array of id of third parties with at least one contact on project ( if source = thirdparty )
2011-11-07 15:04:02 +01:00
*/
function getListContactId ( $source = 'external' )
{
$contactAlreadySelected = array ();
$tab = $this -> liste_contact ( - 1 , $source );
$num = count ( $tab );
$i = 0 ;
while ( $i < $num )
{
if ( $source == 'thirdparty' ) $contactAlreadySelected [ $i ] = $tab [ $i ][ 'socid' ];
else $contactAlreadySelected [ $i ] = $tab [ $i ][ 'id' ];
$i ++ ;
}
return $contactAlreadySelected ;
}
/**
* Link element with a project
*
* @ param int $projectid Project id to link element to
* @ return int < 0 if KO , > 0 if OK
*/
function setProject ( $projectid )
{
if ( ! $this -> table_element )
{
dol_syslog ( get_class ( $this ) . " ::setProject was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
if ( $projectid ) $sql .= ' SET fk_projet = ' . $projectid ;
else $sql .= ' SET fk_projet = NULL' ;
$sql .= ' WHERE rowid = ' . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::setProject " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
if ( $this -> db -> query ( $sql ))
{
$this -> fk_project = $projectid ;
return 1 ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
2012-03-18 00:59:24 +01:00
2012-04-28 18:24:52 +02:00
/**
* Change the payments methods
*
* @ param int $id Id of new payment method
* @ return int > 0 if OK , < 0 if KO
*/
function setPaymentMethods ( $id )
{
dol_syslog ( get_class ( $this ) . '::setPaymentMethods(' . $id . ')' );
if ( $this -> statut >= 0 || $this -> element == 'societe' )
{
// TODO uniformize field name
$fieldname = 'fk_mode_reglement' ;
if ( $this -> element == 'societe' ) $fieldname = 'mode_reglement' ;
2013-07-30 15:06:57 +02:00
if ( get_class ( $this ) == 'Fournisseur' ) $fieldname = 'mode_reglement_supplier' ;
2012-04-28 18:24:52 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' SET ' . $fieldname . ' = ' . $id ;
$sql .= ' WHERE rowid=' . $this -> id ;
if ( $this -> db -> query ( $sql ))
{
2012-03-13 20:01:10 +01:00
$this -> mode_reglement_id = $id ;
2012-04-28 18:24:52 +02:00
return 1 ;
}
else
{
dol_syslog ( get_class ( $this ) . '::setPaymentMethods Erreur ' . $sql . ' - ' . $this -> db -> error ());
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
else
{
dol_syslog ( get_class ( $this ) . '::setPaymentMethods, status of the object is incompatible' );
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
return - 2 ;
}
2012-03-13 20:01:10 +01:00
}
2012-03-18 00:59:24 +01:00
2012-04-28 18:24:52 +02:00
/**
* Change the payments terms
*
* @ param int $id Id of new payment terms
* @ return int > 0 if OK , < 0 if KO
*/
function setPaymentTerms ( $id )
{
dol_syslog ( get_class ( $this ) . '::setPaymentTerms(' . $id . ')' );
if ( $this -> statut >= 0 || $this -> element == 'societe' )
2012-03-13 19:34:22 +01:00
{
// TODO uniformize field name
$fieldname = 'fk_cond_reglement' ;
if ( $this -> element == 'societe' ) $fieldname = 'cond_reglement' ;
2013-07-30 15:06:57 +02:00
if ( get_class ( $this ) == 'Fournisseur' ) $fieldname = 'cond_reglement_supplier' ;
2012-04-28 18:24:52 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' SET ' . $fieldname . ' = ' . $id ;
2012-03-13 19:24:11 +01:00
$sql .= ' WHERE rowid=' . $this -> id ;
2012-04-28 18:24:52 +02:00
if ( $this -> db -> query ( $sql ))
{
2012-03-13 20:01:10 +01:00
$this -> cond_reglement_id = $id ;
2012-04-28 18:24:52 +02:00
$this -> cond_reglement = $id ; // for compatibility
return 1 ;
}
else
{
dol_syslog ( get_class ( $this ) . '::setPaymentTerms Erreur ' . $sql . ' - ' . $this -> db -> error ());
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
else
{
dol_syslog ( get_class ( $this ) . '::setPaymentTerms, status of the object is incompatible' );
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
return - 2 ;
}
2012-03-13 19:24:11 +01:00
}
2012-03-18 00:59:24 +01:00
2012-04-28 18:24:52 +02:00
/**
* Define delivery address
*
* @ param int $id Address id
* @ return int < 0 si ko , > 0 si ok
*/
function setDeliveryAddress ( $id )
2012-03-14 11:11:46 +01:00
{
2013-02-15 19:56:51 +01:00
$fieldname = 'fk_delivery_address' ;
2012-03-14 11:11:46 +01:00
if ( $this -> element == 'delivery' || $this -> element == 'shipping' ) $fieldname = 'fk_address' ;
2012-03-18 00:59:24 +01:00
2012-03-14 11:11:46 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " SET " . $fieldname . " = " . $id ;
$sql .= " WHERE rowid = " . $this -> id . " AND fk_statut = 0 " ;
2012-03-18 00:59:24 +01:00
2012-03-14 11:11:46 +01:00
if ( $this -> db -> query ( $sql ))
{
$this -> fk_delivery_address = $id ;
2012-04-28 18:24:52 +02:00
return 1 ;
2012-03-14 11:11:46 +01:00
}
else
{
$this -> error = $this -> db -> error ();
dol_syslog ( get_class ( $this ) . '::setDeliveryAddress Erreur ' . $sql . ' - ' . $this -> error );
return - 1 ;
2012-04-28 18:24:52 +02:00
}
2012-03-14 11:11:46 +01:00
}
2012-03-18 00:59:24 +01:00
2014-07-24 18:08:08 +02:00
/**
* Change the shipping method
*
2014-07-25 21:50:21 +02:00
* @ param int $shipping_method_id Id of shipping method
2014-07-24 18:08:08 +02:00
* @ return int 1 if OK , 0 if KO
*/
2014-07-25 21:50:21 +02:00
function setShippingMethod ( $shipping_method_id )
2014-07-24 18:08:08 +02:00
{
if ( ! $this -> table_element ) {
dol_syslog ( get_class ( $this ) . " ::setShippingMethod was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
2014-07-25 21:50:21 +02:00
if ( $shipping_method_id < 0 ) $shipping_method_id = 'NULL' ;
dol_syslog ( get_class ( $this ) . '::setShippingMethod(' . $shipping_method_id . ')' );
2014-07-24 18:08:08 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
2014-07-25 21:50:21 +02:00
$sql .= " SET fk_shipping_method = " . $shipping_method_id ;
2014-07-24 18:08:08 +02:00
$sql .= " WHERE rowid= " . $this -> id ;
if ( $this -> db -> query ( $sql )) {
2014-07-25 21:50:21 +02:00
$this -> shipping_method_id = ( $shipping_method_id == 'NULL' ) ? null : $shipping_method_id ;
2014-07-24 18:08:08 +02:00
return 1 ;
} else {
dol_syslog ( get_class ( $this ) . '::setShippingMethod Error ' , LOG_DEBUG );
$this -> error = $this -> db -> error ();
return 0 ;
}
}
2011-11-07 15:04:02 +01:00
/**
* Set last model used by doc generator
*
2012-02-01 13:21:39 +01:00
* @ param User $user User object that make change
* @ param string $modelpdf Modele name
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
function setDocModel ( $user , $modelpdf )
{
if ( ! $this -> table_element )
{
dol_syslog ( get_class ( $this ) . " ::setDocModel was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
$newmodelpdf = dol_trunc ( $modelpdf , 255 );
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET model_pdf = ' " . $this -> db -> escape ( $newmodelpdf ) . " ' " ;
$sql .= " WHERE rowid = " . $this -> id ;
// if ($this->element == 'facture') $sql.= " AND fk_statut < 2";
// if ($this->element == 'propal') $sql.= " AND fk_statut = 0";
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::setDocModel " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> modelpdf = $modelpdf ;
return 1 ;
}
else
{
dol_print_error ( $this -> db );
return 0 ;
}
}
2014-07-06 20:49:34 +02:00
/**
* Change the bank account
*
* @ param int $fk_account Id of bank account
* @ return int 1 if OK , 0 if KO
*/
function setBankAccount ( $fk_account )
{
2014-07-07 18:40:18 +02:00
if ( ! $this -> table_element ) {
2014-07-06 20:49:34 +02:00
dol_syslog ( get_class ( $this ) . " ::setBankAccount was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
if ( $fk_account < 0 ) $fk_account = 'NULL' ;
dol_syslog ( get_class ( $this ) . '::setBankAccount(' . $fk_account . ')' );
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET fk_account = " . $fk_account ;
$sql .= " WHERE rowid= " . $this -> id ;
2014-07-07 18:40:18 +02:00
if ( $this -> db -> query ( $sql )) {
2014-07-06 20:49:34 +02:00
$this -> fk_account = ( $fk_account == 'NULL' ) ? null : $fk_account ;
return 1 ;
2014-07-07 18:40:18 +02:00
} else {
2014-07-06 20:49:34 +02:00
dol_syslog ( get_class ( $this ) . '::setBankAccount Error ' . $sql . ' - ' . $this -> db -> error ());
$this -> error = $this -> db -> error ();
return 0 ;
}
}
2011-11-07 15:04:02 +01:00
/**
2012-09-15 15:33:00 +02:00
* Save a new position ( field rang ) for details lines .
2013-02-15 15:48:53 +01:00
* You can choose to set position for lines with already a position or lines without any position defined .
2011-11-07 15:04:02 +01:00
*
2013-03-22 19:21:46 +01: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
* @ param boolean $fk_parent_line Table with fk_parent_line field or not
2012-02-01 13:21:39 +01:00
* @ return void
2011-11-07 15:04:02 +01:00
*/
2013-03-22 19:21:46 +01:00
function line_order ( $renum = false , $rowidorder = 'ASC' , $fk_parent_line = true )
2011-11-07 15:04:02 +01:00
{
if ( ! $this -> table_element_line )
{
dol_syslog ( get_class ( $this ) . " ::line_order was called on objet with property table_element_line not defined " , LOG_ERR );
return - 1 ;
}
if ( ! $this -> fk_element )
{
dol_syslog ( get_class ( $this ) . " ::line_order was called on objet with property fk_element not defined " , LOG_ERR );
return - 1 ;
}
2012-09-15 15:33:00 +02:00
// Count number of lines to reorder (according to choice $renum)
$nl = 0 ;
$sql = 'SELECT count(rowid) FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
2010-09-06 23:10:45 +02:00
$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' ;
2011-11-08 00:17:20 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::line_order " , LOG_DEBUG );
2008-02-24 17:46:42 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$nl = $row [ 0 ];
}
2012-09-15 15:33:00 +02:00
else dol_print_error ( $this -> db );
2008-02-24 17:46:42 +01:00
if ( $nl > 0 )
{
2012-09-15 15:33:00 +02:00
// The goal of this part is to reorder all lines, with all children lines sharing the same
// counter that parents.
2012-01-23 22:23:00 +01:00
$rows = array ();
2012-02-01 13:21:39 +01:00
2013-02-15 15:48:53 +01:00
// We first search all lines that are parent lines (for multilevel details lines)
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 ;
2013-05-29 01:30:36 +02:00
if ( $fk_parent_line ) $sql .= ' AND fk_parent_line IS NULL' ;
2011-04-09 17:59:45 +02:00
$sql .= ' ORDER BY rang ASC, rowid ' . $rowidorder ;
2011-11-08 00:17:20 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::line_order search all parent lines " , LOG_DEBUG );
2008-02-24 17:46:42 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2012-01-23 22:23:00 +01:00
$i = 0 ;
2008-02-24 17:46:42 +01:00
$num = $this -> db -> num_rows ( $resql );
while ( $i < $num )
{
$row = $this -> db -> fetch_row ( $resql );
2012-09-15 15:33:00 +02:00
$rows [] = $row [ 0 ]; // Add parent line into array rows
2013-02-15 15:48:53 +01:00
$childrens = $this -> getChildrenOfLine ( $row [ 0 ]);
2013-10-12 11:58:38 +02:00
if ( ! empty ( $childrens ))
2012-01-23 22:23:00 +01:00
{
2013-10-12 11:58:38 +02:00
foreach ( $childrens as $child )
2012-01-23 22:23:00 +01:00
{
array_push ( $rows , $child );
}
}
2008-02-24 17:46:42 +01:00
$i ++ ;
}
2012-01-23 22:23:00 +01:00
2012-09-15 15:33:00 +02:00
// Now we set a new number for each lines (parent and children with children included into parent tree)
2012-01-23 22:23:00 +01:00
if ( ! empty ( $rows ))
{
foreach ( $rows as $key => $row )
{
$this -> updateRangOfLine ( $row , ( $key + 1 ));
}
}
}
2012-09-15 15:33:00 +02:00
else
{
dol_print_error ( $this -> db );
}
2012-01-23 22:23:00 +01:00
}
}
2012-02-01 13:21:39 +01:00
2012-01-23 22:23:00 +01:00
/**
2013-02-15 15:48:53 +01:00
* Get children of line
2012-02-01 13:21:39 +01:00
*
2012-01-23 22:23:00 +01:00
* @ param int $id Id of parent line
2013-02-15 15:48:53 +01:00
* @ return array Array with list of children lines id
2012-01-23 22:23:00 +01:00
*/
2013-02-15 15:48:53 +01:00
function getChildrenOfLine ( $id )
2012-01-23 22:23:00 +01:00
{
$rows = array ();
2012-02-01 13:21:39 +01:00
2012-01-23 22:23:00 +01:00
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$sql .= ' AND fk_parent_line = ' . $id ;
$sql .= ' ORDER BY rang ASC' ;
2012-02-01 13:21:39 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::getChildrenOfLine search children lines for line " . $id . " " , LOG_DEBUG );
2012-01-23 22:23:00 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$i = 0 ;
$num = $this -> db -> num_rows ( $resql );
while ( $i < $num )
{
$row = $this -> db -> fetch_row ( $resql );
$rows [ $i ] = $row [ 0 ];
$i ++ ;
2008-02-24 17:46:42 +01:00
}
}
2012-02-01 13:21:39 +01:00
2012-01-23 22:23:00 +01:00
return $rows ;
2008-02-24 17:46:42 +01:00
}
2011-11-07 15:04:02 +01:00
/**
* Update a line to have a lower rank
*
2013-03-22 19:21:46 +01:00
* @ param int $rowid Id of line
* @ param boolean $fk_parent_line Table with fk_parent_line field or not
2012-02-01 13:21:39 +01:00
* @ return void
2011-11-07 15:04:02 +01:00
*/
2013-03-22 19:21:46 +01:00
function line_up ( $rowid , $fk_parent_line = true )
2011-11-07 15:04:02 +01:00
{
2013-03-22 19:21:46 +01:00
$this -> line_order ( false , 'ASC' , $fk_parent_line );
2011-11-07 15:04:02 +01:00
// Get rang of line
$rang = $this -> getRangOfLine ( $rowid );
// Update position of line
$this -> updateLineUp ( $rowid , $rang );
}
/**
* Update a line to have a higher rank
*
2013-03-22 19:21:46 +01:00
* @ param int $rowid Id of line
* @ param boolean $fk_parent_line Table with fk_parent_line field or not
2012-02-01 13:21:39 +01:00
* @ return void
2011-11-07 15:04:02 +01:00
*/
2013-03-22 19:21:46 +01:00
function line_down ( $rowid , $fk_parent_line = true )
2011-11-07 15:04:02 +01:00
{
2013-03-22 19:21:46 +01:00
$this -> line_order ( false , 'ASC' , $fk_parent_line );
2011-11-07 15:04:02 +01:00
// 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-20 19:57:25 +02:00
/**
2011-08-28 14:30:15 +02:00
* Update position of line ( rang )
*
2012-02-01 13:21:39 +01:00
* @ param int $rowid Id of line
* @ param int $rang Position
* @ return void
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 ;
2011-11-08 00:17:20 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::updateRangOfLine " , LOG_DEBUG );
2011-11-08 00:17:20 +01:00
if ( ! $this -> db -> query ( $sql ))
2010-08-20 19:57:25 +02:00
{
dol_print_error ( $this -> db );
}
}
2011-11-07 15:04:02 +01:00
/**
* Update position of line with ajax ( rang )
*
2012-01-23 22:23:00 +01:00
* @ param array $rows Array of rows
2012-02-01 13:21:39 +01:00
* @ return void
2011-11-07 15:04:02 +01:00
*/
2012-01-23 22:23:00 +01:00
function line_ajaxorder ( $rows )
2011-11-07 15:04:02 +01:00
{
$num = count ( $rows );
for ( $i = 0 ; $i < $num ; $i ++ )
{
$this -> updateRangOfLine ( $rows [ $i ], ( $i + 1 ));
}
}
/**
* Update position of line up ( rang )
*
2012-02-01 13:21:39 +01:00
* @ param int $rowid Id of line
* @ param int $rang Position
* @ return void
2011-11-07 15:04:02 +01:00
*/
function updateLineUp ( $rowid , $rang )
{
if ( $rang > 1 )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET rang = ' . $rang ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$sql .= ' AND rang = ' . ( $rang - 1 );
if ( $this -> db -> query ( $sql ) )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET rang = ' . ( $rang - 1 );
$sql .= ' WHERE rowid = ' . $rowid ;
if ( ! $this -> db -> query ( $sql ) )
{
dol_print_error ( $this -> db );
}
}
else
{
dol_print_error ( $this -> db );
}
}
}
/**
* Update position of line down ( rang )
*
2012-02-01 13:21:39 +01:00
* @ param int $rowid Id of line
* @ param int $rang Position
* @ param int $max Max
* @ return void
2011-11-07 15:04:02 +01:00
*/
function updateLineDown ( $rowid , $rang , $max )
{
if ( $rang < $max )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET rang = ' . $rang ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$sql .= ' AND rang = ' . ( $rang + 1 );
if ( $this -> db -> query ( $sql ) )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET rang = ' . ( $rang + 1 );
$sql .= ' WHERE rowid = ' . $rowid ;
if ( ! $this -> db -> query ( $sql ) )
{
dol_print_error ( $this -> db );
}
}
else
{
dol_print_error ( $this -> db );
}
}
}
/**
* Get position of line ( rang )
*
* @ param int $rowid Id of line
* @ return int Value of rang in table of lines
*/
function getRangOfLine ( $rowid )
{
$sql = 'SELECT rang FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE rowid =' . $rowid ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::getRangOfLine " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
/**
* Get rowid of the line relative to its position
*
* @ param int $rang Rang value
* @ return int Rowid of the line
*/
function getIdOfLine ( $rang )
{
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$sql .= ' AND rang = ' . $rang ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
/**
* Get max value used for position of line ( rang )
*
* @ param int $fk_parent_line Parent line id
* @ return int Max value of rang in table of lines
*/
function line_max ( $fk_parent_line = 0 )
{
// Search the last rang with fk_parent_line
if ( $fk_parent_line )
{
$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 ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::line_max " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$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 ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::line_max " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
}
/**
2012-03-18 02:01:23 +01:00
* Update external ref of element
2011-11-07 15:04:02 +01:00
*
* @ param string $ref_ext Update field ref_ext
* @ return int < 0 if KO , > 0 if OK
*/
function update_ref_ext ( $ref_ext )
{
if ( ! $this -> table_element )
{
dol_syslog ( get_class ( $this ) . " ::update_ref_ext was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET ref_ext = ' " . $this -> db -> escape ( $ref_ext ) . " ' " ;
$sql .= " WHERE " . ( isset ( $this -> table_rowid ) ? $this -> table_rowid : 'rowid' ) . " = " . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update_ref_ext " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
if ( $this -> db -> query ( $sql ))
{
$this -> ref_ext = $ref_ext ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
/**
2013-04-10 10:58:38 +02:00
* Update note of element
2011-11-07 15:04:02 +01:00
*
2013-04-10 10:58:38 +02:00
* @ param string $note New value for note
* @ param string $suffix '' , '_public' or '_private'
* @ return int < 0 if KO , > 0 if OK
2013-04-09 17:18:07 +02:00
*/
2013-04-10 10:58:38 +02:00
function update_note ( $note , $suffix = '' )
2013-04-09 17:18:07 +02:00
{
if ( ! $this -> table_element )
{
dol_syslog ( get_class ( $this ) . " ::update_note was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
2013-04-14 23:42:24 +02:00
if ( ! in_array ( $suffix , array ( '' , '_public' , '_private' )))
{
dol_syslog ( get_class ( $this ) . " ::upate_note Parameter suffix must be empty, '_private' or '_public' " , LOG_ERR );
return - 2 ;
}
2013-04-16 01:13:20 +02:00
2013-04-09 17:18:07 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2013-04-10 10:58:38 +02:00
$sql .= " SET note " . $suffix . " = " . ( ! empty ( $note ) ? ( " ' " . $this -> db -> escape ( $note ) . " ' " ) : " NULL " );
2013-04-09 17:18:07 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
2013-04-10 00:08:55 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note " , LOG_DEBUG );
2013-04-09 17:18:07 +02:00
if ( $this -> db -> query ( $sql ))
{
2013-04-10 11:03:24 +02:00
if ( $suffix == '_public' ) $this -> note_public = $note ;
else if ( $suffix == '_private' ) $this -> note_private = $note ;
else $this -> note = $note ;
2013-04-09 17:18:07 +02:00
return 1 ;
}
else
{
2013-04-10 11:03:24 +02:00
$this -> error = $this -> db -> lasterror ();
2013-04-09 17:18:07 +02:00
return - 1 ;
}
}
2011-11-07 15:04:02 +01:00
/**
2013-04-10 10:58:38 +02:00
* Update public note ( kept for backward compatibility )
2013-04-16 01:13:20 +02:00
*
2013-04-10 10:58:38 +02:00
* @ param string $note New value for note
* @ return int < 0 if KO , > 0 if OK
* @ deprecated
2011-11-07 15:04:02 +01:00
*/
2013-04-10 10:58:38 +02:00
function update_note_public ( $note )
2011-11-07 15:04:02 +01:00
{
2013-04-10 10:58:38 +02:00
return $this -> update_note ( $note , '_public' );
2011-11-07 15:04:02 +01:00
}
2013-04-16 01:13:20 +02:00
2011-11-07 15:04:02 +01:00
/**
2013-07-04 00:11:40 +02:00
* Update total_ht , total_ttc , total_vat , total_localtax1 , total_localtax2 for an object ( sum of lines ) .
2013-09-18 16:46:54 +02:00
* Must be called at end of methods addline or updateline .
2011-11-07 15:04:02 +01:00
*
2014-05-13 13:22:29 +02:00
* @ param int $exclspec > 0 = Exclude special product ( product_type = 9 )
* @ param string $roundingadjust 'none' = Do nothing , 'auto' = Use default method ( MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND if defined , or '0' ), '0' = Force use total of rounding , '1' = Force use rounding of total
2012-04-07 02:24:32 +02:00
* @ param int $nodatabaseupdate 1 = Do not update database . Update only properties of object .
2014-05-25 05:01:14 +02:00
* @ param Societe $seller If roundingadjust is '0' or '1' , it means we recalculate total for lines before calculating total for object . For this , we need seller object .
2012-04-07 02:24:32 +02:00
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
2014-05-13 13:22:29 +02:00
function update_price ( $exclspec = 0 , $roundingadjust = 'none' , $nodatabaseupdate = 0 , $seller = '' )
2011-11-07 15:04:02 +01:00
{
2013-07-04 00:11:40 +02:00
global $conf ;
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php' ;
2011-11-07 15:04:02 +01:00
2014-05-13 13:22:29 +02:00
if ( $roundingadjust == '-1' ) $roundingadjust = 'auto' ; // For backward compatibility
2013-09-13 17:43:26 +02:00
$forcedroundingmode = $roundingadjust ;
2014-05-13 13:22:29 +02:00
if ( $forcedroundingmode == 'auto' && isset ( $conf -> global -> MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND )) $forcedroundingmode = $conf -> global -> MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND ;
if ( $forcedroundingmode == 'auto' ) $forcedroundingmode = '0' ;
2011-11-07 15:04:02 +01:00
2012-04-07 02:24:32 +02:00
$error = 0 ;
2011-11-07 15:04:02 +01:00
// Define constants to find lines to sum
$fieldtva = 'total_tva' ;
$fieldlocaltax1 = 'total_localtax1' ;
$fieldlocaltax2 = 'total_localtax2' ;
2013-09-13 17:43:26 +02:00
$fieldup = 'subprice' ;
2013-09-18 16:46:54 +02:00
if ( $this -> element == 'facture_fourn' || $this -> element == 'invoice_supplier' )
2013-09-13 17:43:26 +02:00
{
$fieldtva = 'tva' ;
$fieldup = 'pu_ht' ;
}
2011-11-07 15:04:02 +01:00
2013-09-13 17:43:26 +02:00
$sql = 'SELECT rowid, qty, ' . $fieldup . ' as up, remise_percent, total_ht, ' . $fieldtva . ' as total_tva, total_ttc, ' . $fieldlocaltax1 . ' as total_localtax1, ' . $fieldlocaltax2 . ' as total_localtax2,' ;
$sql .= ' tva_tx as vatrate, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, info_bits, product_type' ;
2011-11-07 15:04:02 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2012-04-07 02:24:32 +02:00
if ( $exclspec )
{
$product_field = 'product_type' ;
if ( $this -> table_element_line == 'contratdet' ) $product_field = '' ; // contratdet table has no product_type field
if ( $product_field ) $sql .= ' AND ' . $product_field . ' <> 9' ;
}
2013-07-04 00:11:40 +02:00
$sql .= ' ORDER by rowid' ; // We want to be sure to always use same order of line to not change lines differently when option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND is used
2011-11-07 15:04:02 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update_price " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> total_ht = 0 ;
$this -> total_tva = 0 ;
$this -> total_localtax1 = 0 ;
$this -> total_localtax2 = 0 ;
$this -> total_ttc = 0 ;
2013-07-04 00:11:40 +02:00
$total_ht_by_vats = array ();
2013-07-07 13:17:48 +02:00
$total_tva_by_vats = array ();
$total_ttc_by_vats = array ();
2011-11-07 15:04:02 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
2014-05-13 13:22:29 +02:00
// Note: There is no check on detail line and no check on total, if $forcedroundingmode = 'none'
if ( $forcedroundingmode == '0' ) // Check if data on line are consistent. This may solve lines that were not consistent because set with $forcedroundingmode='auto'
2013-09-13 17:43:26 +02:00
{
2013-09-18 16:46:54 +02:00
$localtax_array = array ( $obj -> localtax1_type , $obj -> localtax1_tx , $obj -> localtax2_type , $obj -> localtax2_tx );
$tmpcal = calcul_price_total ( $obj -> qty , $obj -> up , $obj -> remise_percent , $obj -> vatrate , $obj -> localtax1_tx , $obj -> localtax2_tx , 0 , 'HT' , $obj -> info_bits , $obj -> product_type , $seller , $localtax_array );
2013-09-13 17:43:26 +02:00
$diff = price2num ( $tmpcal [ 1 ] - $obj -> total_tva , 'MT' , 1 );
if ( $diff )
{
$sqlfix = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element_line . " SET " . $fieldtva . " = " . $tmpcal [ 1 ] . " , total_ttc = " . $tmpcal [ 2 ] . " WHERE rowid = " . $obj -> rowid ;
2014-05-13 13:22:29 +02:00
dol_syslog ( 'We found unconsistent data into detailed line (difference of ' . $diff . ') for line rowid = ' . $obj -> rowid . " (total vat of line calculated= " . $tmpcal [ 1 ] . " , database= " . $obj -> total_tva . " ). We fix the total_vat and total_ttc of line by running sqlfix = " . $sqlfix );
2013-09-13 17:43:26 +02:00
$resqlfix = $this -> db -> query ( $sqlfix );
if ( ! $resqlfix ) dol_print_error ( $this -> db , 'Failed to update line' );
$obj -> total_tva = $tmpcal [ 1 ];
$obj -> total_ttc = $tmpcal [ 2 ];
//
}
}
2013-09-18 16:46:54 +02:00
2013-09-13 17:43:26 +02:00
$this -> total_ht += $obj -> total_ht ; // The only field visible at end of line detail
2011-11-07 15:04:02 +01:00
$this -> total_tva += $obj -> total_tva ;
$this -> total_localtax1 += $obj -> total_localtax1 ;
$this -> total_localtax2 += $obj -> total_localtax2 ;
$this -> total_ttc += $obj -> total_ttc ;
2014-05-01 19:17:45 +02:00
if ( ! isset ( $total_ht_by_vats [ $obj -> vatrate ])) $total_ht_by_vats [ $obj -> vatrate ] = 0 ;
if ( ! isset ( $total_tva_by_vats [ $obj -> vatrate ])) $total_tva_by_vats [ $obj -> vatrate ] = 0 ;
if ( ! isset ( $total_ttc_by_vats [ $obj -> vatrate ])) $total_ttc_by_vats [ $obj -> vatrate ] = 0 ;
2013-07-04 00:11:40 +02:00
$total_ht_by_vats [ $obj -> vatrate ] += $obj -> total_ht ;
$total_tva_by_vats [ $obj -> vatrate ] += $obj -> total_tva ;
$total_ttc_by_vats [ $obj -> vatrate ] += $obj -> total_ttc ;
2011-11-07 15:04:02 +01:00
2014-05-13 13:22:29 +02:00
if ( $forcedroundingmode == '1' ) // Check if we need adjustement onto line for vat
2012-07-11 18:13:41 +02:00
{
2013-07-04 00:11:40 +02:00
$tmpvat = price2num ( $total_ht_by_vats [ $obj -> vatrate ] * $obj -> vatrate / 100 , 'MT' , 1 );
$diff = price2num ( $total_tva_by_vats [ $obj -> vatrate ] - $tmpvat , 'MT' , 1 );
//print 'Line '.$i.' rowid='.$obj->rowid.' vat_rate='.$obj->vatrate.' total_ht='.$obj->total_ht.' total_tva='.$obj->total_tva.' total_ttc='.$obj->total_ttc.' total_ht_by_vats='.$total_ht_by_vats[$obj->vatrate].' total_tva_by_vats='.$total_tva_by_vats[$obj->vatrate].' (new calculation = '.$tmpvat.') total_ttc_by_vats='.$total_ttc_by_vats[$obj->vatrate].($diff?" => DIFF":"")."<br>\n";
if ( $diff )
{
2014-05-19 15:54:43 +02:00
if ( abs ( $diff ) > 0.1 ) { dol_syslog ( 'A rounding difference was detected into TOTAL but is too high to be corrected' , LOG_WARNING ); exit ; }
2013-07-04 00:11:40 +02:00
$sqlfix = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element_line . " SET " . $fieldtva . " = " . ( $obj -> total_tva - $diff ) . " , total_ttc = " . ( $obj -> total_ttc - $diff ) . " WHERE rowid = " . $obj -> rowid ;
2014-05-13 13:22:29 +02:00
dol_syslog ( 'We found a difference of ' . $diff . ' for line rowid = ' . $obj -> rowid . " . We fix the total_vat and total_ttc of line by running sqlfix = " . $sqlfix );
2013-07-04 00:11:40 +02:00
$resqlfix = $this -> db -> query ( $sqlfix );
if ( ! $resqlfix ) dol_print_error ( $this -> db , 'Failed to update line' );
$this -> total_tva -= $diff ;
2013-07-07 13:17:48 +02:00
$this -> total_ttc -= $diff ;
$total_tva_by_vats [ $obj -> vatrate ] -= $diff ;
$total_ttc_by_vats [ $obj -> vatrate ] -= $diff ;
2013-07-04 00:11:40 +02:00
}
2012-07-11 18:13:41 +02:00
}
2011-11-07 15:04:02 +01:00
$i ++ ;
}
2013-03-30 14:27:13 +01:00
// Add revenue stamp to total
$this -> total_ttc += isset ( $this -> revenuestamp ) ? $this -> revenuestamp : 0 ;
2013-04-07 17:39:08 +02:00
2011-11-07 15:04:02 +01:00
$this -> db -> free ( $resql );
// Now update global field total_ht, total_ttc and tva
$fieldht = 'total_ht' ;
$fieldtva = 'tva' ;
$fieldlocaltax1 = 'localtax1' ;
$fieldlocaltax2 = 'localtax2' ;
$fieldttc = 'total_ttc' ;
2013-07-04 00:11:40 +02:00
// Specific code for backward compatibility with old field names
2011-11-07 15:04:02 +01: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' ;
2012-04-07 02:24:32 +02:00
if ( empty ( $nodatabaseupdate ))
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . ' SET' ;
$sql .= " " . $fieldht . " =' " . price2num ( $this -> total_ht ) . " ', " ;
$sql .= " " . $fieldtva . " =' " . price2num ( $this -> total_tva ) . " ', " ;
$sql .= " " . $fieldlocaltax1 . " =' " . price2num ( $this -> total_localtax1 ) . " ', " ;
$sql .= " " . $fieldlocaltax2 . " =' " . price2num ( $this -> total_localtax2 ) . " ', " ;
$sql .= " " . $fieldttc . " =' " . price2num ( $this -> total_ttc ) . " ' " ;
$sql .= ' WHERE rowid = ' . $this -> id ;
//print "xx".$sql;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update_price " , LOG_DEBUG );
2012-04-07 02:24:32 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$error ++ ;
$this -> error = $this -> db -> error ();
}
}
2011-11-07 15:04:02 +01:00
2012-04-07 02:24:32 +02:00
if ( ! $error )
2011-11-07 15:04:02 +01:00
{
return 1 ;
}
else
{
return - 1 ;
}
}
else
{
2012-04-07 02:24:32 +02:00
dol_print_error ( $this -> db , 'Bad request in update_price' );
2011-11-07 15:04:02 +01:00
return - 1 ;
}
}
2011-08-14 05:13:50 +02:00
/**
2012-02-21 18:16:24 +01:00
* Add objects linked in llx_element_element .
2011-08-14 05:13:50 +02:00
*
2012-02-21 18:16:24 +01:00
* @ param string $origin Linked element type
* @ param int $origin_id Linked element id
* @ return int <= 0 if KO , > 0 if OK
2011-08-14 05:13:50 +02:00
*/
2012-02-21 18:16:24 +01:00
function add_object_linked ( $origin = null , $origin_id = null )
2011-11-07 15:04:02 +01:00
{
2012-02-21 18:16:24 +01:00
$origin = ( ! empty ( $origin ) ? $origin : $this -> origin );
$origin_id = ( ! empty ( $origin_id ) ? $origin_id : $this -> origin_id );
2012-03-18 00:59:24 +01:00
2011-11-07 15:04:02 +01:00
$this -> db -> begin ();
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " element_element ( " ;
$sql .= " fk_source " ;
$sql .= " , sourcetype " ;
$sql .= " , fk_target " ;
$sql .= " , targettype " ;
$sql .= " ) VALUES ( " ;
2012-02-21 18:16:24 +01:00
$sql .= $origin_id ;
$sql .= " , ' " . $origin . " ' " ;
2011-11-07 15:04:02 +01:00
$sql .= " , " . $this -> id ;
$sql .= " , ' " . $this -> element . " ' " ;
$sql .= " ) " ;
2009-02-20 23:53:15 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::add_object_linked " , LOG_DEBUG );
2011-11-09 16:54:34 +01:00
if ( $this -> db -> query ( $sql ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return 0 ;
}
}
2009-12-15 12:46:54 +01:00
2009-12-15 11:39:22 +01:00
/**
2013-08-07 15:08:30 +02:00
* Fetch array of objects linked to current object . Links are loaded into this -> linkedObjects array and this -> linkedObjectsIds
2011-08-28 14:30:15 +02:00
*
2011-11-10 10:13:44 +01:00
* @ param int $sourceid Object source id
* @ param string $sourcetype Object source type
* @ param int $targetid Object target id
* @ param string $targettype Object target type
2014-04-26 17:27:59 +02:00
* @ param string $clause 'OR' or 'AND' clause used when both source id and target id are provided
2011-11-10 10:13:44 +01:00
* @ return void
2011-11-07 15:04:02 +01:00
*/
2014-04-23 17:15:07 +02:00
function fetchObjectLinked ( $sourceid = '' , $sourcetype = '' , $targetid = '' , $targettype = '' , $clause = 'OR' )
2011-11-07 15:04:02 +01:00
{
global $conf ;
2011-04-11 10:34:26 +02:00
2011-11-07 15:04:02 +01:00
$this -> linkedObjectsIds = array ();
$this -> linkedObjects = array ();
2011-10-23 22:10:30 +02:00
2011-11-07 15:04:02 +01:00
$justsource = false ;
$justtarget = false ;
2012-07-08 12:24:13 +02:00
$withtargettype = false ;
$withsourcetype = false ;
2008-03-07 11:34:16 +01:00
2012-07-08 12:18:29 +02:00
if ( ! empty ( $sourceid ) && ! empty ( $sourcetype ) && empty ( $targetid ))
{
$justsource = true ;
if ( ! empty ( $targettype )) $withtargettype = true ;
}
if ( ! empty ( $targetid ) && ! empty ( $targettype ) && empty ( $sourceid ))
{
$justtarget = true ;
2012-07-08 12:24:13 +02:00
if ( ! empty ( $sourcetype )) $withsourcetype = true ;
2012-07-08 12:18:29 +02:00
}
2008-03-07 11:34:16 +01:00
2012-02-07 18:33:27 +01:00
$sourceid = ( ! empty ( $sourceid ) ? $sourceid : $this -> id );
$targetid = ( ! empty ( $targetid ) ? $targetid : $this -> id );
2012-07-02 17:07:37 +02:00
$sourcetype = ( ! empty ( $sourcetype ) ? $sourcetype : $this -> element );
2012-02-07 18:33:27 +01:00
$targettype = ( ! empty ( $targettype ) ? $targettype : $this -> element );
2011-04-11 10:34:26 +02:00
2011-11-07 15:04:02 +01:00
// Links beetween objects are stored in this table
$sql = 'SELECT fk_source, sourcetype, fk_target, targettype' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'element_element' ;
$sql .= " WHERE " ;
if ( $justsource || $justtarget )
{
2012-07-08 12:18:29 +02:00
if ( $justsource )
{
$sql .= " fk_source = ' " . $sourceid . " ' AND sourcetype = ' " . $sourcetype . " ' " ;
if ( $withtargettype ) $sql .= " AND targettype = ' " . $targettype . " ' " ;
}
else if ( $justtarget )
{
$sql .= " fk_target = ' " . $targetid . " ' AND targettype = ' " . $targettype . " ' " ;
2012-07-08 14:18:40 +02:00
if ( $withsourcetype ) $sql .= " AND sourcetype = ' " . $sourcetype . " ' " ;
2012-07-08 12:18:29 +02:00
}
2011-11-07 15:04:02 +01:00
}
else
{
$sql .= " (fk_source = ' " . $sourceid . " ' AND sourcetype = ' " . $sourcetype . " ') " ;
$sql .= " " . $clause . " (fk_target = ' " . $targetid . " ' AND targettype = ' " . $targettype . " ') " ;
}
2013-05-10 18:35:19 +02:00
$sql .= ' ORDER BY sourcetype' ;
2012-02-21 10:47:08 +01:00
//print $sql;
2011-04-11 10:34:26 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetchObjectLink " , LOG_DEBUG );
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2012-02-21 10:47:08 +01:00
$num = $this -> db -> num_rows ( $resql );
2011-11-07 15:04:02 +01:00
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj -> fk_source == $sourceid )
{
$this -> linkedObjectsIds [ $obj -> targettype ][] = $obj -> fk_target ;
}
if ( $obj -> fk_target == $targetid )
{
$this -> linkedObjectsIds [ $obj -> sourcetype ][] = $obj -> fk_source ;
}
$i ++ ;
}
2012-02-21 10:47:08 +01:00
2011-11-07 15:04:02 +01:00
if ( ! empty ( $this -> linkedObjectsIds ))
{
foreach ( $this -> linkedObjectsIds as $objecttype => $objectids )
{
// Parse element/subelement (ex: project_task)
$module = $element = $subelement = $objecttype ;
2012-02-15 19:20:18 +01:00
if ( $objecttype != 'order_supplier' && $objecttype != 'invoice_supplier' && preg_match ( '/^([^_]+)_([^_]+)/i' , $objecttype , $regs ))
2011-11-07 15:04:02 +01:00
{
$module = $element = $regs [ 1 ];
$subelement = $regs [ 2 ];
}
2011-04-11 10:34:26 +02:00
2011-11-07 15:04:02 +01:00
$classpath = $element . '/class' ;
2009-12-15 12:46:54 +01:00
2011-11-07 15:04:02 +01:00
// To work with non standard path
if ( $objecttype == 'facture' ) {
$classpath = 'compta/facture/class' ;
}
2011-11-10 10:13:44 +01:00
else if ( $objecttype == 'propal' ) {
2011-11-07 15:04:02 +01:00
$classpath = 'comm/propal/class' ;
}
2011-11-10 10:13:44 +01:00
else if ( $objecttype == 'shipping' ) {
2011-11-07 15:04:02 +01:00
$classpath = 'expedition/class' ; $subelement = 'expedition' ; $module = 'expedition_bon' ;
}
2011-11-10 10:13:44 +01:00
else if ( $objecttype == 'delivery' ) {
2011-11-07 15:04:02 +01:00
$classpath = 'livraison/class' ; $subelement = 'livraison' ; $module = 'livraison_bon' ;
}
2012-02-15 19:20:18 +01:00
else if ( $objecttype == 'invoice_supplier' || $objecttype == 'order_supplier' ) {
$classpath = 'fourn/class' ; $module = 'fournisseur' ;
2011-11-07 15:04:02 +01:00
}
2011-11-10 10:13:44 +01:00
else if ( $objecttype == 'fichinter' ) {
2011-11-07 15:04:02 +01:00
$classpath = 'fichinter/class' ; $subelement = 'fichinter' ; $module = 'ficheinter' ;
}
// TODO ajout temporaire - MAXIME MANGIN
2011-11-10 10:13:44 +01:00
else if ( $objecttype == 'contratabonnement' ) {
2011-11-07 15:04:02 +01:00
$classpath = 'contrat/class' ; $subelement = 'contrat' ; $module = 'contratabonnement' ;
}
$classfile = strtolower ( $subelement ); $classname = ucfirst ( $subelement );
if ( $objecttype == 'invoice_supplier' ) {
$classfile = 'fournisseur.facture' ; $classname = 'FactureFournisseur' ;
}
2011-11-10 10:13:44 +01:00
else if ( $objecttype == 'order_supplier' ) {
2011-11-07 15:04:02 +01:00
$classfile = 'fournisseur.commande' ; $classname = 'CommandeFournisseur' ;
}
if ( $conf -> $module -> enabled && $element != $this -> element )
{
dol_include_once ( '/' . $classpath . '/' . $classfile . '.class.php' );
$num = count ( $objectids );
for ( $i = 0 ; $i < $num ; $i ++ )
{
$object = new $classname ( $this -> db );
$ret = $object -> fetch ( $objectids [ $i ]);
if ( $ret >= 0 )
{
$this -> linkedObjects [ $objecttype ][ $i ] = $object ;
}
}
}
}
}
}
else
{
dol_print_error ( $this -> db );
}
}
2012-02-13 01:43:35 +01:00
2012-02-07 18:33:27 +01:00
/**
* Update object linked of a current object
2012-02-13 01:43:35 +01:00
*
2012-02-07 18:33:27 +01:00
* @ param int $sourceid Object source id
* @ param string $sourcetype Object source type
* @ param int $targetid Object target id
* @ param string $targettype Object target type
* @ return int > 0 if OK , < 0 if KO
*/
function updateObjectLinked ( $sourceid = '' , $sourcetype = '' , $targetid = '' , $targettype = '' )
{
$updatesource = false ;
$updatetarget = false ;
2012-02-13 01:43:35 +01:00
2012-02-07 18:33:27 +01:00
if ( ! empty ( $sourceid ) && ! empty ( $sourcetype ) && empty ( $targetid ) && empty ( $targettype )) $updatesource = true ;
else if ( empty ( $sourceid ) && empty ( $sourcetype ) && ! empty ( $targetid ) && ! empty ( $targettype )) $updatetarget = true ;
2012-02-13 01:43:35 +01:00
2012-02-07 18:33:27 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " element_element SET " ;
if ( $updatesource )
{
$sql .= " fk_source = " . $sourceid ;
$sql .= " , sourcetype = ' " . $sourcetype . " ' " ;
$sql .= " WHERE fk_target = " . $this -> id ;
$sql .= " AND targettype = ' " . $this -> element . " ' " ;
}
else if ( $updatetarget )
{
$sql .= " fk_target = " . $targetid ;
$sql .= " , targettype = ' " . $targettype . " ' " ;
$sql .= " WHERE fk_source = " . $this -> id ;
$sql .= " AND sourcetype = ' " . $this -> element . " ' " ;
}
2012-02-13 01:43:35 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::updateObjectLinked " , LOG_DEBUG );
2012-02-07 18:33:27 +01:00
if ( $this -> db -> query ( $sql ))
{
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2012-02-13 01:43:35 +01:00
2012-02-07 16:47:46 +01:00
/**
2012-02-07 18:33:27 +01:00
* Delete all links between an object $this
2012-02-07 16:47:46 +01:00
*
2012-07-05 14:31:13 +02:00
* @ param int $sourceid Object source id
* @ param string $sourcetype Object source type
* @ param int $targetid Object target id
* @ param string $targettype Object target type
2012-02-07 18:33:27 +01:00
* @ return int > 0 if OK , < 0 if KO
2012-02-07 16:47:46 +01:00
*/
2012-07-05 14:31:13 +02:00
function deleteObjectLinked ( $sourceid = '' , $sourcetype = '' , $targetid = '' , $targettype = '' )
2012-02-07 16:47:46 +01:00
{
2012-07-05 14:31:13 +02:00
$deletesource = false ;
$deletetarget = false ;
if ( ! empty ( $sourceid ) && ! empty ( $sourcetype ) && empty ( $targetid ) && empty ( $targettype )) $deletesource = true ;
else if ( empty ( $sourceid ) && empty ( $sourcetype ) && ! empty ( $targetid ) && ! empty ( $targettype )) $deletetarget = true ;
$sourceid = ( ! empty ( $sourceid ) ? $sourceid : $this -> id );
$sourcetype = ( ! empty ( $sourcetype ) ? $sourcetype : $this -> element );
$targetid = ( ! empty ( $targetid ) ? $targetid : $this -> id );
$targettype = ( ! empty ( $targettype ) ? $targettype : $this -> element );
2012-02-07 16:47:46 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_element " ;
2012-02-15 19:35:19 +01:00
$sql .= " WHERE " ;
2012-07-05 14:31:13 +02:00
if ( $deletesource )
{
$sql .= " fk_source = " . $sourceid . " AND sourcetype = ' " . $sourcetype . " ' " ;
$sql .= " AND fk_target = " . $this -> id . " AND targettype = ' " . $this -> element . " ' " ;
}
else if ( $deletetarget )
{
$sql .= " fk_target = " . $targetid . " AND targettype = ' " . $targettype . " ' " ;
$sql .= " AND fk_source = " . $this -> id . " AND sourcetype = ' " . $this -> element . " ' " ;
}
else
{
$sql .= " (fk_source = " . $this -> id . " AND sourcetype = ' " . $this -> element . " ') " ;
$sql .= " OR " ;
$sql .= " (fk_target = " . $this -> id . " AND targettype = ' " . $this -> element . " ') " ;
}
2012-02-13 01:43:35 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::deleteObjectLinked " , LOG_DEBUG );
2012-02-07 16:47:46 +01:00
if ( $this -> db -> query ( $sql ))
{
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2012-02-13 01:43:35 +01:00
2011-11-07 15:04:02 +01:00
/**
2012-03-15 06:59:27 +01:00
* Set status of an object
2011-11-07 15:04:02 +01:00
*
2012-03-15 06:59:27 +01:00
* @ param int $status Status to set
2012-02-01 13:21:39 +01:00
* @ param int $elementId Id of element to force ( use this -> id by default )
* @ param string $elementType Type of element to force ( use -> this -> element by default )
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
2012-03-15 06:59:27 +01:00
function setStatut ( $status , $elementId = '' , $elementType = '' )
2011-11-07 15:04:02 +01:00
{
$elementId = ( ! empty ( $elementId ) ? $elementId : $this -> id );
$elementTable = ( ! empty ( $elementType ) ? $elementType : $this -> table_element );
2012-03-18 00:59:24 +01:00
2012-03-15 06:59:27 +01:00
$this -> db -> begin ();
2011-11-07 15:04:02 +01:00
2012-05-05 20:22:46 +02:00
$fieldstatus = " fk_statut " ;
if ( $elementTable == 'user' ) $fieldstatus = " statut " ;
2011-11-07 15:04:02 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $elementTable ;
2012-05-05 20:22:46 +02:00
$sql .= " SET " . $fieldstatus . " = " . $status ;
2011-11-07 15:04:02 +01:00
$sql .= " WHERE rowid= " . $elementId ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::setStatut " , LOG_DEBUG );
2012-04-28 18:24:52 +02:00
if ( $this -> db -> query ( $sql ))
{
2012-03-15 06:59:27 +01:00
$this -> db -> commit ();
2012-04-28 18:24:52 +02:00
$this -> statut = $status ;
return 1 ;
}
else
{
2012-03-15 06:59:27 +01:00
$this -> error = $this -> db -> lasterror ();
2012-04-28 18:24:52 +02:00
$this -> db -> rollback ();
return - 1 ;
2011-11-07 15:04:02 +01:00
}
}
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 ;
2012-04-06 22:39:40 +02:00
$sql .= " WHERE entity IN ( " . getEntity ( $this -> element , 1 ) . " ) " ;
if ( ! empty ( $id )) $sql .= " AND rowid = " . $id ;
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-11-07 15:04:02 +01:00
/**
2014-05-13 10:12:45 +02:00
* Get special code of a line
2011-11-07 15:04:02 +01:00
*
2012-02-01 13:21:39 +01:00
* @ param int $lineid Id of line
* @ return int Special code
2011-11-07 15:04:02 +01:00
*/
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
2013-11-04 16:50:41 +01:00
* This method is in most cases called by method fetch of objects but you can call it separately .
2011-08-28 14:30:15 +02:00
*
2012-02-01 13:21:39 +01:00
* @ param int $rowid Id of line
* @ param array $optionsArray Array resulting of call of extrafields -> fetch_name_optionals_label ()
2014-03-05 09:57:36 +01:00
* @ return int < 0 if error , 0 if no optionals to find nor found , 1 if a line is found and optional loaded
2011-06-22 12:58:22 +02:00
*/
function fetch_optionals ( $rowid , $optionsArray = '' )
{
if ( ! is_array ( $optionsArray ))
{
// optionsArray not already loaded, so we load it
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
2011-06-22 12:58:22 +02:00
$extrafields = new ExtraFields ( $this -> db );
2013-04-26 10:29:14 +02:00
$optionsArray = $extrafields -> fetch_name_optionals_label ( $this -> table_element );
2011-06-22 12:58:22 +02:00
}
2010-09-02 23:08:29 +02:00
2013-05-20 17:37:08 +02:00
2011-06-22 12:58:22 +02:00
// Request to get complementary values
2011-09-17 21:49:50 +02:00
if ( count ( $optionsArray ) > 0 )
2011-06-22 12:58:22 +02:00
{
$sql = " SELECT rowid " ;
foreach ( $optionsArray as $name => $label )
{
$sql .= " , " . $name ;
}
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields " ;
$sql .= " WHERE fk_object = " . $rowid ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch_optionals " , LOG_DEBUG );
2011-09-20 11:40:27 +02:00
$resql = $this -> db -> query ( $sql );
2011-06-22 12:58:22 +02:00
if ( $resql )
{
2013-12-13 18:33:05 +01:00
$numrows = $this -> db -> num_rows ( $resql );
if ( $numrows )
2011-06-22 12:58:22 +02:00
{
$tab = $this -> db -> fetch_array ( $resql );
foreach ( $tab as $key => $value )
{
2014-06-09 15:21:20 +02:00
// Test fetch_array ! is_int($key) because fetch_array seult is a mix table with Key as alpha and Key as int (depend db engine)
2013-04-26 10:51:17 +02:00
if ( $key != 'rowid' && $key != 'tms' && $key != 'fk_member' && ! is_int ( $key ))
2011-06-22 12:58:22 +02:00
{
// we can add this attribute to adherent object
$this -> array_options [ " options_ $key " ] = $value ;
}
}
}
2013-05-20 17:37:08 +02:00
2011-06-22 12:58:22 +02:00
$this -> db -> free ( $resql );
2014-01-04 12:29:20 +01:00
2013-12-13 18:33:05 +01:00
if ( $numrows ) return $numrows ;
else return 0 ;
2011-06-22 12:58:22 +02:00
}
else
{
dol_print_error ( $this -> db );
2013-12-13 18:33:05 +01:00
return - 1 ;
2011-06-22 12:58:22 +02:00
}
}
2013-12-13 18:33:05 +01:00
return 0 ;
2011-06-22 12:58:22 +02:00
}
2010-12-14 10:03:50 +01:00
2012-12-08 12:49:20 +01:00
/**
* Delete all extra fields values for the current object .
2012-12-10 19:06:59 +01:00
*
* @ return int < 0 if KO , > 0 if OK
2012-12-08 12:49:20 +01:00
*/
function deleteExtraFields ()
{
global $langs ;
2012-12-10 19:06:59 +01:00
2012-12-08 12:49:20 +01:00
$error = 0 ;
2012-12-10 19:06:59 +01:00
2012-12-08 12:49:20 +01:00
$this -> db -> begin ();
2012-12-10 19:06:59 +01:00
2012-12-08 12:49:20 +01:00
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields WHERE fk_object = " . $this -> id ;
2014-06-12 13:49:05 +02:00
dol_syslog ( get_class ( $this ) . " ::deleteExtraFields delete " , LOG_DEBUG );
2012-12-10 19:06:59 +01:00
$resql = $this -> db -> query ( $sql_del );
2012-12-08 12:49:20 +01:00
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
2011-06-22 12:58:22 +02:00
2011-09-13 14:12:48 +02:00
/**
2012-02-01 20:44:06 +01:00
* Add / Update all extra fields values for the current object .
* All data to describe values to insert are stored into $this -> array_options = array ( 'keyextrafield' => 'valueextrafieldtoadd' )
2012-02-01 13:21:39 +01:00
*
2013-11-07 21:02:50 +01:00
* @ return int - 1 = error , O = did nothing , 1 = OK
2011-11-07 15:04:02 +01:00
*/
function insertExtraFields ()
{
2013-11-05 13:11:36 +01:00
global $conf , $langs ;
2012-02-01 20:44:06 +01:00
2012-02-07 18:48:50 +01:00
$error = 0 ;
2012-02-13 01:43:35 +01:00
2013-11-05 13:11:36 +01:00
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) return 0 ; // For avoid conflicts if trigger used
2014-01-04 12:29:20 +01:00
2012-07-24 17:52:46 +02:00
if ( ! empty ( $this -> array_options ))
2011-09-13 14:12:48 +02:00
{
2012-02-01 20:44:06 +01:00
// Check parameters
$langs -> load ( 'admin' );
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
2012-02-01 20:44:06 +01:00
$extrafields = new ExtraFields ( $this -> db );
2013-04-26 10:29:14 +02:00
$optionsArray = $extrafields -> fetch_name_optionals_label ( $this -> table_element );
2012-02-01 20:44:06 +01:00
foreach ( $this -> array_options as $key => $value )
{
$attributeKey = substr ( $key , 8 ); // Remove 'options_' prefix
$attributeType = $extrafields -> attribute_type [ $attributeKey ];
$attributeSize = $extrafields -> attribute_size [ $attributeKey ];
$attributeLabel = $extrafields -> attribute_label [ $attributeKey ];
switch ( $attributeType )
{
case 'int' :
if ( ! is_numeric ( $value ) && $value != '' )
{
$error ++ ; $this -> errors [] = $langs -> trans ( " ExtraFieldHasWrongValue " , $attributeLabel );
return - 1 ;
}
elseif ( $value == '' )
{
$this -> array_options [ $key ] = null ;
}
break ;
2013-01-22 16:48:32 +01:00
case 'price' :
2013-01-14 16:13:09 +01:00
$this -> array_options [ $key ] = price2num ( $this -> array_options [ $key ]);
break ;
2013-03-29 14:45:40 +01:00
case 'date' :
2013-04-02 10:52:39 +02:00
$this -> array_options [ $key ] = $this -> db -> idate ( $this -> array_options [ $key ]);
2013-03-29 14:45:40 +01:00
break ;
case 'datetime' :
2013-04-02 10:52:39 +02:00
$this -> array_options [ $key ] = $this -> db -> idate ( $this -> array_options [ $key ]);
2013-03-29 14:45:40 +01:00
break ;
2013-01-14 16:13:09 +01:00
}
2013-04-07 17:39:08 +02:00
}
2011-09-13 14:12:48 +02:00
$this -> db -> begin ();
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields WHERE fk_object = " . $this -> id ;
2014-06-12 13:49:05 +02:00
dol_syslog ( get_class ( $this ) . " ::insertExtraFields delete " , LOG_DEBUG );
2011-09-13 14:12:48 +02:00
$this -> db -> query ( $sql_del );
$sql = " INSERT INTO " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields (fk_object " ;
foreach ( $this -> array_options as $key => $value )
{
2013-03-16 00:00:57 +01:00
$attributeKey = substr ( $key , 8 ); // Remove 'options_' prefix
2011-09-13 14:12:48 +02:00
// Add field of attribut
2013-03-16 00:00:57 +01:00
if ( $extrafields -> attribute_type [ $attributeKey ] != 'separate' ) // Only for other type of separate
2013-03-22 17:10:17 +01:00
$sql .= " , " . $attributeKey ;
2011-09-13 14:12:48 +02:00
}
$sql .= " ) VALUES ( " . $this -> id ;
foreach ( $this -> array_options as $key => $value )
{
2013-03-16 00:00:57 +01:00
$attributeKey = substr ( $key , 8 ); // Remove 'options_' prefix
2011-09-13 14:12:48 +02:00
// Add field o fattribut
2013-03-16 00:00:57 +01:00
if ( $extrafields -> attribute_type [ $attributeKey ] != 'separate' ) // Only for other type of separate)
{
if ( $this -> array_options [ $key ] != '' )
{
2013-05-02 12:14:29 +02:00
$sql .= " ,' " . $this -> db -> escape ( $this -> array_options [ $key ]) . " ' " ;
2013-03-16 00:00:57 +01:00
}
else
{
$sql .= " ,null " ;
}
}
2011-09-13 14:12:48 +02:00
}
$sql .= " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insertExtraFields insert " , LOG_DEBUG );
2011-09-13 14:12:48 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
else return 0 ;
2011-11-07 15:04:02 +01:00
}
2013-04-07 17:39:08 +02:00
2013-04-01 18:59:05 +02:00
/**
2013-03-30 17:43:21 +01:00
* Function to show lines of extrafields with output datas
2013-04-07 17:39:08 +02:00
*
2014-02-16 23:51:29 +01:00
* @ param object $extrafields Extrafield Object
2013-04-07 17:39:08 +02:00
* @ param string $mode Show output ( view ) or input ( edit ) for extrafield
2014-02-16 23:51:29 +01:00
* @ param array $params Optionnal parameters
* @ param string $keyprefix Prefix string to add into name and id of field ( can be used to avoid duplicate names )
2013-04-07 17:39:08 +02:00
*
* @ return string
2013-03-30 17:43:21 +01:00
*/
2014-02-16 23:51:29 +01:00
function showOptionals ( $extrafields , $mode = 'view' , $params = 0 , $keyprefix = '' )
2013-03-30 17:43:21 +01:00
{
2014-04-23 11:57:53 +02:00
global $_POST , $conf ;
2013-04-07 17:39:08 +02:00
2013-04-01 18:59:05 +02:00
$out = '' ;
2013-04-07 17:39:08 +02:00
2013-11-05 13:11:36 +01:00
if ( count ( $extrafields -> attribute_label ) > 0 )
2013-04-01 18:59:05 +02:00
{
$out .= " \n " ;
$out .= '<!-- showOptionalsInput --> ' ;
$out .= " \n " ;
2013-04-07 17:39:08 +02:00
2013-04-01 18:59:05 +02:00
$e = 0 ;
foreach ( $extrafields -> attribute_label as $key => $label )
{
2013-06-10 16:05:41 +02:00
if ( is_array ( $params ) && count ( $params ) > 0 ) {
if ( array_key_exists ( 'colspan' , $params )) {
$colspan = $params [ 'colspan' ];
}
} else {
$colspan = '3' ;
}
2013-08-14 09:56:08 +02:00
switch ( $mode ) {
case " view " :
$value = $this -> array_options [ " options_ " . $key ];
break ;
case " edit " :
$value = ( isset ( $_POST [ " options_ " . $key ]) ? $_POST [ " options_ " . $key ] : $this -> array_options [ " options_ " . $key ]);
break ;
}
2013-04-01 18:59:05 +02:00
if ( $extrafields -> attribute_type [ $key ] == 'separate' )
{
$out .= $extrafields -> showSeparator ( $key );
}
else
{
2013-06-10 16:05:41 +02:00
$csstyle = '' ;
if ( is_array ( $params ) && count ( $params ) > 0 ) {
if ( array_key_exists ( 'style' , $params )) {
$csstyle = $params [ 'style' ];
}
}
2013-04-01 18:59:05 +02:00
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_USE_TWO_COLUMS ) && ( $e % 2 ) == 0 )
{
2013-06-10 16:05:41 +02:00
$out .= '<tr ' . $csstyle . '>' ;
2013-04-01 18:59:05 +02:00
$colspan = '0' ;
}
2013-04-07 17:39:08 +02:00
else
2013-04-01 18:59:05 +02:00
{
2013-06-10 16:05:41 +02:00
$out .= '<tr ' . $csstyle . '>' ;
2013-04-01 18:59:05 +02:00
}
// Convert date into timestamp format
if ( in_array ( $extrafields -> attribute_type [ $key ], array ( 'date' , 'datetime' )))
{
2013-05-10 16:34:14 +02:00
$value = isset ( $_POST [ " options_ " . $key ]) ? dol_mktime ( $_POST [ " options_ " . $key . " hour " ], $_POST [ " options_ " . $key . " min " ], 0 , $_POST [ " options_ " . $key . " month " ], $_POST [ " options_ " . $key . " day " ], $_POST [ " options_ " . $key . " year " ]) : $this -> db -> jdate ( $this -> array_options [ 'options_' . $key ]);
2013-04-01 18:59:05 +02:00
}
2013-07-26 08:51:22 +02:00
if ( $extrafields -> attribute_required [ $key ])
$label = '<span class="fieldrequired">' . $label . '</span>' ;
2013-04-01 18:59:05 +02:00
$out .= '<td>' . $label . '</td>' ;
2014-02-12 00:23:15 +01:00
$out .= '<td' . ( $colspan ? ' colspan="' . $colspan . '"' : '' ) . '>' ;
2013-04-07 17:39:08 +02:00
2013-04-01 18:59:05 +02:00
switch ( $mode ) {
case " view " :
$out .= $extrafields -> showOutputField ( $key , $value );
break ;
case " edit " :
2014-02-16 23:51:29 +01:00
$out .= $extrafields -> showInputField ( $key , $value , '' , $keyprefix );
2013-04-01 18:59:05 +02:00
break ;
}
2013-04-07 17:39:08 +02:00
2013-04-01 18:59:05 +02:00
$out .= '</td>' . " \n " ;
2013-04-07 17:39:08 +02:00
2013-04-01 18:59:05 +02:00
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_USE_TWO_COLUMS ) && (( $e % 2 ) == 1 )) $out .= '</tr>' ;
else $out .= '</tr>' ;
$e ++ ;
}
}
$out .= " \n " ;
$out .= '<!-- /showOptionalsInput --> ' ;
2013-07-18 17:42:09 +02:00
$out .= '
< script type = " text/javascript " >
jQuery ( document ) . ready ( function () {
function showOptions ( child_list , parent_list )
{
var val = $ ( " select[name= \" options_ " + parent_list + " \" ] " ) . val ();
var parentVal = parent_list + " : " + val ;
if ( val > 0 ) {
$ ( " select[name= \" " + child_list + " \" ] option[parent] " ) . hide ();
$ ( " select[name= \" " + child_list + " \" ] option[parent= \" " + parentVal + " \" ] " ) . show ();
} else {
$ ( " select[name= \" " + child_list + " \" ] option " ) . show ();
}
}
function setListDependencies () {
jQuery ( " select option[parent] " ) . parent () . each ( function () {
var child_list = $ ( this ) . attr ( " name " );
var parent = $ ( this ) . find ( " option[parent]:first " ) . attr ( " parent " );
var infos = parent . split ( " : " );
var parent_list = infos [ 0 ];
$ ( " select[name= \" options_ " + parent_list + " \" ] " ) . change ( function () {
showOptions ( child_list , parent_list );
});
});
}
2013-07-19 13:38:57 +02:00
2013-07-18 17:42:09 +02:00
setListDependencies ();
});
</ script > ' ;
2013-04-01 18:59:05 +02:00
}
return $out ;
}
2011-10-05 13:56:32 +02:00
2011-08-28 14:30:15 +02:00
/**
2014-04-26 17:27:59 +02:00
* Function to check if an object is used by others .
* Check is done into this -> childtables . There is no check into llx_element_element .
2011-08-28 14:30:15 +02:00
*
2011-12-07 19:22:12 +01:00
* @ param int $id Id of object
* @ return int < 0 if KO , 0 if not used , > 0 if already used
2011-08-28 14:30:15 +02:00
*/
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 ();
return - 1 ;
}
}
if ( $haschild > 0 )
{
$this -> error = " ErrorRecordHasChildren " ;
return $haschild ;
}
else return 0 ;
}
2011-10-05 13:56:32 +02:00
2011-11-07 15:04:02 +01:00
/**
* Function to say how many lines object contains
*
2013-04-20 00:10:20 +02:00
* @ param int $predefined - 1 = All , 0 = Count free product / service only , 1 = Count predefined product / service only , 2 = Count predefined product , 3 = Count predefined service
2011-11-07 15:04:02 +01:00
* @ return int < 0 if KO , 0 if no predefined products , nb of lines with predefined products if found
*/
function hasProductsOrServices ( $predefined =- 1 )
{
$nb = 0 ;
foreach ( $this -> lines as $key => $val )
{
$qualified = 0 ;
if ( $predefined == - 1 ) $qualified = 1 ;
if ( $predefined == 1 && $val -> fk_product > 0 ) $qualified = 1 ;
if ( $predefined == 0 && $val -> fk_product <= 0 ) $qualified = 1 ;
2013-04-20 00:10:20 +02:00
if ( $predefined == 2 && $val -> fk_product > 0 && $val -> product_type == 0 ) $qualified = 1 ;
if ( $predefined == 3 && $val -> fk_product > 0 && $val -> product_type == 1 ) $qualified = 1 ;
2011-11-07 15:04:02 +01:00
if ( $qualified ) $nb ++ ;
}
dol_syslog ( get_class ( $this ) . '::hasProductsOrServices we found ' . $nb . ' qualified lines of products/servcies' );
return $nb ;
}
2012-03-18 00:59:24 +01:00
2012-10-15 00:49:39 +02:00
/**
2013-01-10 12:58:46 +01:00
* Function that returns the total amount HT of discounts applied for all lines .
2012-12-10 19:06:59 +01:00
*
2013-01-10 13:53:46 +01:00
* @ return float
2012-10-15 00:49:39 +02:00
*/
function getTotalDiscount ()
{
2013-01-10 13:53:46 +01:00
$total_discount = 0.00 ;
2012-10-15 00:49:39 +02:00
2013-01-10 12:58:46 +01:00
$sql = " SELECT subprice as pu_ht, qty, remise_percent, total_ht " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " det " ;
$sql .= " WHERE " . $this -> fk_element . " = " . $this -> id ;
2012-12-10 19:06:59 +01:00
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::getTotalDiscount' , LOG_DEBUG );
2013-01-10 12:58:46 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2012-10-15 00:49:39 +02:00
{
2013-01-10 12:58:46 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
2014-04-23 11:59:33 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2013-01-10 12:58:46 +01:00
$pu_ht = $obj -> pu_ht ;
$qty = $obj -> qty ;
$discount_percent_line = $obj -> remise_percent ;
$total_ht = $obj -> total_ht ;
2013-01-22 16:48:32 +01:00
2013-01-10 12:58:46 +01:00
$total_discount_line = price2num (( $pu_ht * $qty ) - $total_ht , 'MT' );
$total_discount += $total_discount_line ;
2013-01-22 16:48:32 +01:00
2013-01-10 12:58:46 +01:00
$i ++ ;
}
}
2013-01-22 16:48:32 +01:00
//print $total_discount; exit;
2013-01-10 13:53:46 +01:00
return price2num ( $total_discount );
2012-10-15 00:49:39 +02:00
}
2012-04-28 18:24:52 +02:00
/**
2012-03-16 14:32:37 +01:00
* Set extra parameters
*
2012-04-28 18:24:52 +02:00
* @ return void
*/
function setExtraParameters ()
2012-03-16 14:32:37 +01:00
{
$this -> db -> begin ();
2012-04-07 02:24:32 +02:00
2012-03-24 15:17:20 +01:00
$extraparams = ( ! empty ( $this -> extraparams ) ? json_encode ( $this -> extraparams ) : null );
2012-04-28 18:24:52 +02:00
2012-03-24 15:17:20 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET extraparams = " . ( ! empty ( $extraparams ) ? " ' " . $this -> db -> escape ( $extraparams ) . " ' " : " null " );
$sql .= " WHERE rowid = " . $this -> id ;
2012-03-18 00:59:24 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::setExtraParameters " , LOG_DEBUG );
2012-04-28 18:24:52 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
2012-03-16 14:32:37 +01:00
}
2012-03-18 00:59:24 +01:00
2011-06-22 12:58:22 +02:00
2012-04-08 13:05:58 +02:00
/**
* Return if a country is inside the EEC ( European Economic Community )
*
* @ return boolean true = country inside EEC , false = country outside EEC
*/
function isInEEC ()
{
// List of all country codes that are in europe for european vat rules
2013-01-10 14:17:43 +01:00
// List found on http://ec.europa.eu/taxation_customs/common/faq/faq_1179_en.htm#9
2012-04-08 13:05:58 +02:00
$country_code_in_EEC = array (
'AT' , // Austria
'BE' , // Belgium
'BG' , // Bulgaria
'CY' , // Cyprus
'CZ' , // Czech republic
'DE' , // Germany
'DK' , // Danemark
'EE' , // Estonia
'ES' , // Spain
'FI' , // Finland
'FR' , // France
2013-01-10 14:17:43 +01:00
'GB' , // United Kingdom
2012-04-08 13:05:58 +02:00
'GR' , // Greece
'NL' , // Holland
'HU' , // Hungary
'IE' , // Ireland
2013-01-10 14:20:03 +01:00
'IM' , // Isle of Man - Included in UK
2012-04-08 13:05:58 +02:00
'IT' , // Italy
'LT' , // Lithuania
'LU' , // Luxembourg
'LV' , // Latvia
2013-01-10 14:17:43 +01:00
'MC' , // Monaco - Included in France
2012-04-08 13:05:58 +02:00
'MT' , // Malta
//'NO', // Norway
'PL' , // Poland
'PT' , // Portugal
'RO' , // Romania
'SE' , // Sweden
'SK' , // Slovakia
'SI' , // Slovenia
2013-04-24 19:14:59 +02:00
'UK' , // United Kingdom
2012-04-08 13:05:58 +02:00
//'CH', // Switzerland - No. Swizerland in not in EEC
);
//print "dd".$this->country_code;
return in_array ( $this -> country_code , $country_code_in_EEC );
}
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
// --------------------
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 .
2012-02-01 13:21:39 +01:00
*
* @ return void
2011-04-11 10:34:26 +02:00
*/
2013-01-26 10:35:30 +01:00
function showLinkedObjectBlock ()
2011-04-11 10:34:26 +02:00
{
2013-01-25 19:12:54 +01:00
global $conf , $langs , $hookmanager ;
global $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
2012-07-05 14:31:13 +02:00
// Bypass the default method
$hookmanager -> initHooks ( array ( 'commonobject' ));
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'showLinkedObjectBlock' , $parameters , $this , $action ); // Note that $action and $object may have been modified by hook
2013-01-22 17:43:41 +01:00
if ( empty ( $reshook ))
2012-07-05 14:31:13 +02:00
{
$num = count ( $this -> linkedObjects );
foreach ( $this -> linkedObjects as $objecttype => $objects )
{
$tplpath = $element = $subelement = $objecttype ;
if ( preg_match ( '/^([^_]+)_([^_]+)/i' , $objecttype , $regs ))
{
$element = $regs [ 1 ];
$subelement = $regs [ 2 ];
$tplpath = $element . '/' . $subelement ;
}
// To work with non standard path
if ( $objecttype == 'facture' ) {
$tplpath = 'compta/' . $element ;
2012-07-17 14:30:10 +02:00
if ( empty ( $conf -> facture -> enabled )) continue ; // Do not show if module disabled
2012-07-05 14:31:13 +02:00
}
else if ( $objecttype == 'propal' ) {
$tplpath = 'comm/' . $element ;
2012-07-17 14:30:10 +02:00
if ( empty ( $conf -> propal -> enabled )) continue ; // Do not show if module disabled
2012-07-05 14:31:13 +02:00
}
else if ( $objecttype == 'shipping' ) {
$tplpath = 'expedition' ;
2012-07-17 14:30:10 +02:00
if ( empty ( $conf -> expedition -> enabled )) continue ; // Do not show if module disabled
2012-07-05 14:31:13 +02:00
}
else if ( $objecttype == 'delivery' ) {
$tplpath = 'livraison' ;
}
else if ( $objecttype == 'invoice_supplier' ) {
$tplpath = 'fourn/facture' ;
}
else if ( $objecttype == 'order_supplier' ) {
$tplpath = 'fourn/commande' ;
}
global $linkedObjectBlock ;
$linkedObjectBlock = $objects ;
// Output template part (modules that overwrite templates must declare this into descriptor)
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( '/' . $tplpath . '/tpl' ));
foreach ( $dirtpls as $reldir )
{
2012-08-23 02:04:35 +02:00
$res =@ include dol_buildpath ( $reldir . '/linkedobjectblock.tpl.php' );
2012-07-05 14:31:13 +02:00
if ( $res ) break ;
}
}
return $num ;
2011-05-06 15:35:15 +02:00
}
2011-04-11 10:34:26 +02:00
}
2013-10-12 11:58:38 +02:00
2011-04-11 10:34:26 +02:00
/* This is to show add lines */
/**
2012-08-23 13:45:51 +02:00
* Show add free and predefined products / services form
2011-11-07 15:04:02 +01:00
*
2012-02-01 13:21:39 +01: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
* @ return void
2010-09-08 23:56:39 +02:00
*/
2013-01-26 10:35:30 +01:00
function formAddObjectLine ( $dateSelector , $seller , $buyer )
2010-08-06 18:10:03 +02:00
{
2013-01-26 10:35:30 +01:00
global $conf , $user , $langs , $object , $hookmanager ;
2011-11-08 10:18:45 +01:00
global $form , $bcnd , $var ;
2011-04-11 01:25:49 +02:00
2013-06-10 16:05:41 +02:00
//Line extrafield
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafieldsline = new ExtraFields ( $this -> db );
$extralabelslines = $extrafieldsline -> fetch_name_optionals_label ( $this -> table_element_line );
2012-04-29 11:00:28 +02:00
// Output template part (modules that overwrite templates must declare this into descriptor)
2010-09-15 15:29:17 +02:00
// Use global variables + $dateSelector + $seller and $buyer
2012-04-29 11:00:28 +02:00
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( '/core/tpl' ));
foreach ( $dirtpls as $reldir )
2012-04-28 18:24:52 +02:00
{
2014-05-05 14:30:08 +02:00
$tpl = dol_buildpath ( $reldir . '/objectline_create.tpl.php' );
2012-08-23 13:45:51 +02:00
if ( empty ( $conf -> file -> strict_mode )) {
$res =@ include $tpl ;
2012-08-22 17:42:40 +02:00
} else {
2012-08-23 13:45:51 +02:00
$res = include $tpl ; // for debug
2012-08-22 17:42:40 +02:00
}
2012-04-29 11:00:28 +02:00
if ( $res ) break ;
2012-04-28 18:24:52 +02:00
}
2011-11-07 15:04:02 +01: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
2012-04-06 11:38:13 +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 .
*
* @ param string $action Action code
* @ param string $seller Object of seller third party
* @ param string $buyer Object of buyer third party
* @ param string $selected Object line selected
* @ param int $dateSelector 1 = Show also date range input fields
* @ return void
*/
2013-01-26 10:35:30 +01:00
function printObjectLines ( $action , $seller , $buyer , $selected = 0 , $dateSelector = 0 )
2012-04-06 11:38:13 +02:00
{
2014-05-12 12:53:10 +02:00
global $conf , $langs , $user , $object , $hookmanager ;
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
print '<tr class="liste_titre nodrag nodrop">' ;
2013-04-16 01:13:20 +02:00
if ( ! empty ( $conf -> global -> MAIN_VIEW_LINE_NUMBER )) print '<td align="center" width="5"> </td>' ;
// Description
2014-07-10 17:36:39 +02:00
print '<td><label for="">' . $langs -> trans ( 'Description' ) . '</label></td>' ;
2013-04-16 01:13:20 +02:00
// VAT
2014-07-10 17:36:39 +02:00
print '<td align="right" width="50"><label for="tva_tx">' . $langs -> trans ( 'VAT' ) . '</label></td>' ;
2013-04-16 01:13:20 +02:00
// Price HT
2014-07-10 17:36:39 +02:00
print '<td align="right" width="80"><label for="price_ht">' . $langs -> trans ( 'PriceUHT' ) . '</label></td>' ;
2013-04-16 01:13:20 +02:00
if ( $conf -> global -> MAIN_FEATURES_LEVEL > 1 ) print '<td align="right" width="80"> </td>' ;
// Qty
2014-07-10 17:36:39 +02:00
print '<td align="right" width="50"><label for="qty">' . $langs -> trans ( 'Qty' ) . '</label></td>' ;
2013-04-16 01:13:20 +02:00
// Reduction short
2014-07-10 17:36:39 +02:00
print '<td align="right" width="50"><label for="remise_percent">' . $langs -> trans ( 'ReductionShort' ) . '</label></td>' ;
2013-04-16 01:13:20 +02:00
2013-05-20 17:37:08 +02:00
if ( ! empty ( $conf -> margin -> enabled ) && empty ( $user -> societe_id ))
{
2012-08-01 17:36:15 +02:00
if ( $conf -> global -> MARGIN_TYPE == " 1 " )
2012-07-18 15:26:33 +02:00
print '<td align="right" width="80">' . $langs -> trans ( 'BuyingPrice' ) . '</td>' ;
else
2013-01-11 15:33:34 +01:00
print '<td align="right" width="80">' . $langs -> trans ( 'CostPrice' ) . '</td>' ;
2013-04-16 01:13:20 +02:00
2013-08-20 12:11:19 +02:00
if ( ! empty ( $conf -> global -> DISPLAY_MARGIN_RATES ) && $user -> rights -> margins -> liretous )
2012-08-01 17:36:15 +02:00
print '<td align="right" width="50">' . $langs -> trans ( 'MarginRate' ) . '</td>' ;
2013-08-20 12:11:19 +02:00
if ( ! empty ( $conf -> global -> DISPLAY_MARK_RATES ) && $user -> rights -> margins -> liretous )
2012-08-01 17:36:15 +02:00
print '<td align="right" width="50">' . $langs -> trans ( 'MarkRate' ) . '</td>' ;
}
2013-04-16 01:13:20 +02:00
// Total HT
2012-04-06 11:38:13 +02:00
print '<td align="right" width="50">' . $langs -> trans ( 'TotalHTShort' ) . '</td>' ;
2013-04-16 01:13:20 +02:00
2014-05-12 12:53:10 +02:00
print '<td></td>' ; // No width to allow autodim
2013-04-16 01:13:20 +02:00
print '<td width="10"></td>' ;
2014-05-12 12:53:10 +02:00
print '<td width="10"></td>' ;
2013-04-16 01:13:20 +02:00
2012-04-06 11:38:13 +02:00
print " </tr> \n " ;
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
$num = count ( $this -> lines );
$var = true ;
$i = 0 ;
2012-04-07 02:24:32 +02:00
2013-06-10 16:05:41 +02:00
//Line extrafield
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafieldsline = new ExtraFields ( $this -> db );
$extralabelslines = $extrafieldsline -> fetch_name_optionals_label ( $this -> table_element_line );
2013-06-16 21:31:21 +02:00
2012-04-06 11:38:13 +02:00
foreach ( $this -> lines as $line )
{
2013-06-10 16:05:41 +02:00
//Line extrafield
$line -> fetch_optionals ( $line -> id , $extralabelslines );
2012-04-06 11:38:13 +02:00
$var =! $var ;
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
if ( is_object ( $hookmanager ) && (( $line -> product_type == 9 && ! empty ( $line -> special_code )) || ! empty ( $line -> fk_parent_line )))
{
if ( empty ( $line -> fk_parent_line ))
{
2014-02-01 12:55:05 +01:00
$parameters = array ( 'line' => $line , 'var' => $var , 'num' => $num , 'i' => $i , 'dateSelector' => $dateSelector , 'seller' => $seller , 'buyer' => $buyer , 'selected' => $selected , 'extrafieldsline' => $extrafieldsline );
2012-08-01 17:36:15 +02:00
$reshook = $hookmanager -> executeHooks ( 'printObjectLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2012-04-06 11:38:13 +02:00
}
}
else
{
2013-06-10 16:05:41 +02:00
$this -> printObjectLine ( $action , $line , $var , $num , $i , $dateSelector , $seller , $buyer , $selected , $extrafieldsline );
2012-04-06 11:38:13 +02:00
}
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
$i ++ ;
}
}
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
/**
* Return HTML content of a detail line
* TODO Move this into an output class file ( htmlline . class . php )
*
* @ param string $action GET / POST action
* @ param array $line Selected object line to output
* @ param string $var Is it a an odd line ( true )
* @ param int $num Number of line ( 0 )
* @ param int $i I
* @ param int $dateSelector 1 = Show also date range input fields
* @ param string $seller Object of seller third party
* @ param string $buyer Object of buyer third party
* @ param string $selected Object line selected
2013-07-07 13:14:32 +02:00
* @ param object $extrafieldsline Object of extrafield line attribute
2012-04-06 11:38:13 +02:00
* @ return void
2010-08-10 00:39:20 +02:00
*/
2013-06-10 16:05:41 +02:00
function printObjectLine ( $action , $line , $var , $num , $i , $dateSelector , $seller , $buyer , $selected = 0 , $extrafieldsline = 0 )
2010-08-10 00:39:20 +02:00
{
2014-05-12 12:53:10 +02:00
global $conf , $langs , $user , $object , $hookmanager ;
2011-11-08 10:18:45 +01:00
global $form , $bc , $bcdd ;
2012-04-07 02:24:32 +02:00
2012-04-06 12:14:16 +02:00
$element = $this -> element ;
2013-03-22 17:10:17 +01:00
$text = '' ; $description = '' ; $type = 0 ;
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
// Show product and description
2012-08-23 13:45:51 +02:00
$type = ( ! empty ( $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 ; // deprecated
if ( ! empty ( $line -> date_end )) $type = 1 ; // deprecated
2012-04-06 11:38:13 +02:00
// Ligne en mode visu
if ( $action != 'editline' || $selected != $line -> id )
{
2013-03-22 17:10:17 +01:00
// Product
2012-04-06 11:38:13 +02:00
if ( $line -> fk_product > 0 )
{
2013-03-30 14:27:13 +01:00
$product_static = new Product ( $this -> db );
$product_static -> type = $line -> fk_product_type ;
$product_static -> id = $line -> fk_product ;
$product_static -> ref = $line -> ref ;
$text = $product_static -> getNomUrl ( 1 );
2013-03-22 17:10:17 +01:00
2014-02-17 18:27:45 +01:00
// Define output language and label
2014-02-17 18:22:15 +01:00
if ( ! empty ( $conf -> global -> MAIN_MULTILANGS ))
2012-04-06 11:38:13 +02:00
{
2014-02-17 18:27:45 +01:00
if ( ! is_object ( $this -> thirdparty ))
2014-02-17 18:22:15 +01:00
{
2014-02-17 18:27:45 +01:00
dol_print_error ( '' , 'Error: Method printObjectLine was called on an object and object->fetch_thirdparty was not done before' );
return ;
2014-02-17 18:22:15 +01:00
}
2012-08-23 13:45:51 +02:00
$prod = new Product ( $this -> db );
2013-03-22 17:10:17 +01:00
$prod -> fetch ( $line -> fk_product );
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
$outputlangs = $langs ;
$newlang = '' ;
if ( empty ( $newlang ) && GETPOST ( 'lang_id' )) $newlang = GETPOST ( 'lang_id' );
2014-02-17 18:27:45 +01:00
if ( ! empty ( $conf -> global -> PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE ) && empty ( $newlang )) $newlang = $this -> thirdparty -> default_lang ; // For language to language of customer
2012-04-06 11:38:13 +02:00
if ( ! empty ( $newlang ))
{
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
}
2012-04-07 02:24:32 +02:00
2012-08-25 12:07:18 +02:00
$label = ( ! empty ( $prod -> multilangs [ $outputlangs -> defaultlang ][ " label " ])) ? $prod -> multilangs [ $outputlangs -> defaultlang ][ " label " ] : $line -> product_label ;
2012-04-06 11:38:13 +02:00
}
else
{
$label = $line -> product_label ;
}
2012-04-07 02:24:32 +02:00
2012-08-22 17:42:40 +02:00
$text .= ' - ' . ( ! empty ( $line -> label ) ? $line -> label : $label );
2012-09-12 18:47:06 +02:00
$description = ( ! empty ( $conf -> global -> PRODUIT_DESC_IN_FORM ) ? '' : dol_htmlentitiesbr ( $line -> description ));
2012-04-06 11:38:13 +02:00
}
2012-08-23 13:45:51 +02:00
2012-08-25 15:21:34 +02:00
// Output template part (modules that overwrite templates must declare this into descriptor)
// Use global variables + $dateSelector + $seller and $buyer
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( '/core/tpl' ));
foreach ( $dirtpls as $reldir )
2012-04-06 11:38:13 +02:00
{
2012-08-23 13:45:51 +02:00
$tpl = dol_buildpath ( $reldir . '/objectline_view.tpl.php' );
2012-08-25 15:21:34 +02:00
if ( empty ( $conf -> file -> strict_mode )) {
$res =@ include $tpl ;
} else {
$res = include $tpl ; // for debug
}
if ( $res ) break ;
2012-04-06 11:38:13 +02:00
}
}
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
// Ligne en mode update
if ( $this -> statut == 0 && $action == 'editline' && $selected == $line -> id )
{
2012-08-23 13:45:51 +02:00
$label = ( ! empty ( $line -> label ) ? $line -> label : (( $line -> fk_product > 0 ) ? $line -> product_label : '' ));
2012-08-25 15:21:34 +02:00
if ( ! empty ( $conf -> global -> MAIN_HTML5_PLACEHOLDER )) $placeholder = ' placeholder="' . $langs -> trans ( " Label " ) . '"' ;
2012-08-23 13:45:51 +02:00
else $placeholder = ' title="' . $langs -> trans ( " Label " ) . '"' ;
$pu_ttc = price2num ( $line -> subprice * ( 1 + ( $line -> tva_tx / 100 )), 'MU' );
2012-08-25 15:21:34 +02:00
// Output template part (modules that overwrite templates must declare this into descriptor)
// Use global variables + $dateSelector + $seller and $buyer
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( '/core/tpl' ));
foreach ( $dirtpls as $reldir )
2012-04-06 11:38:13 +02:00
{
2012-08-23 13:45:51 +02:00
$tpl = dol_buildpath ( $reldir . '/objectline_edit.tpl.php' );
if ( empty ( $conf -> file -> strict_mode )) {
$res =@ include $tpl ;
} else {
$res = include $tpl ; // for debug
2012-08-25 15:21:34 +02:00
}
if ( $res ) break ;
2012-04-06 11:38:13 +02:00
}
}
}
2010-12-14 10:03:50 +01:00
2011-04-11 10:34:26 +02:00
2011-11-07 15:04:02 +01:00
/* This is to show array of line of details of source object */
2011-04-11 10:34:26 +02:00
2011-11-07 15:04:02 +01:00
/**
* Return HTML table table of source object lines
2011-04-11 10:34:26 +02:00
* 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 .
2012-02-01 13:21:39 +01:00
*
* @ return void
2011-11-07 15:04:02 +01:00
*/
2013-01-26 10:35:30 +01:00
function printOriginLinesList ()
2011-11-07 15:04:02 +01:00
{
2013-01-26 10:35:30 +01:00
global $langs , $hookmanager ;
2011-11-07 15:04:02 +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>' ;
$num = count ( $this -> lines );
$var = true ;
$i = 0 ;
foreach ( $this -> lines as $line )
{
$var =! $var ;
2012-08-25 03:28:30 +02:00
if ( is_object ( $hookmanager ) && (( $line -> product_type == 9 && ! empty ( $line -> special_code )) || ! empty ( $line -> fk_parent_line )))
2011-11-07 15:04:02 +01:00
{
if ( empty ( $line -> fk_parent_line ))
{
$parameters = array ( 'line' => $line , 'var' => $var , 'i' => $i );
2011-11-22 14:00:03 +01:00
$action = '' ;
2011-11-07 15:04:02 +01:00
$reshook = $hookmanager -> executeHooks ( 'printOriginObjectLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
}
}
else
{
2012-03-18 20:23:50 +01:00
$this -> printOriginLine ( $line , $var );
2011-11-07 15:04:02 +01:00
}
$i ++ ;
}
}
/**
* Return HTML with a line of table array of source object lines
2011-04-11 10:34:26 +02:00
* 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 .
2012-02-01 13:21:39 +01:00
*
* @ param array $line Line
* @ param string $var Var
* @ return void
2011-11-07 15:04:02 +01:00
*/
2012-03-18 20:23:50 +01:00
function printOriginLine ( $line , $var )
2011-11-07 15:04:02 +01:00
{
2012-05-01 14:09:58 +02:00
global $conf , $langs , $bc ;
2011-11-07 15:04:02 +01:00
//var_dump($line);
2013-04-19 13:30:08 +02:00
if ( ! empty ( $line -> date_start ))
{
2013-04-15 15:55:10 +02:00
$date_start = $line -> date_start ;
}
2013-04-19 13:30:08 +02:00
else
2013-04-15 15:55:10 +02:00
{
$date_start = $line -> date_debut_prevue ;
if ( $line -> date_debut_reel ) $date_start = $line -> date_debut_reel ;
}
if ( ! empty ( $line -> date_end ))
{
$date_end = $line -> date_end ;
}
2013-04-19 13:30:08 +02:00
else
2013-04-15 15:55:10 +02:00
{
$date_end = $line -> date_fin_prevue ;
if ( $line -> date_fin_reel ) $date_end = $line -> date_fin_reel ;
}
2011-11-07 15:04:02 +01:00
$this -> tpl [ 'label' ] = '' ;
if ( ! empty ( $line -> fk_parent_line )) $this -> tpl [ 'label' ] .= img_picto ( '' , 'rightarrow' );
if (( $line -> info_bits & 2 ) == 2 ) // TODO Not sure this is used for source object
{
2011-12-05 18:36:54 +01:00
$discount = new DiscountAbsolute ( $this -> db );
2011-11-07 15:04:02 +01:00
$discount -> fk_soc = $this -> socid ;
$this -> tpl [ 'label' ] .= $discount -> getNomUrl ( 0 , 'discount' );
}
2012-08-25 03:28:30 +02:00
else if ( ! empty ( $line -> fk_product ))
2011-11-07 15:04:02 +01:00
{
$productstatic = new Product ( $this -> db );
$productstatic -> id = $line -> fk_product ;
$productstatic -> ref = $line -> ref ;
$productstatic -> type = $line -> fk_product_type ;
$this -> tpl [ 'label' ] .= $productstatic -> getNomUrl ( 1 );
2012-08-25 03:28:30 +02:00
$this -> tpl [ 'label' ] .= ' - ' . ( ! empty ( $line -> label ) ? $line -> label : $line -> product_label );
2011-11-07 15:04:02 +01:00
// Dates
if ( $line -> product_type == 1 && ( $date_start || $date_end ))
{
$this -> tpl [ 'label' ] .= get_date_range ( $date_start , $date_end );
}
}
else
{
$this -> tpl [ 'label' ] .= ( $line -> product_type == - 1 ? ' ' : ( $line -> product_type == 1 ? img_object ( $langs -> trans ( '' ), 'service' ) : img_object ( $langs -> trans ( '' ), 'product' )));
2013-04-15 15:55:10 +02:00
if ( ! empty ( $line -> desc )) {
$this -> tpl [ 'label' ] .= $line -> desc ;
} else {
$this -> tpl [ 'label' ] .= ( $line -> label ? ' ' . $line -> label : '' );
2013-04-19 13:30:08 +02:00
}
2011-11-07 15:04:02 +01:00
// Dates
if ( $line -> product_type == 1 && ( $date_start || $date_end ))
{
$this -> tpl [ 'label' ] .= get_date_range ( $date_start , $date_end );
}
}
2012-08-25 03:28:30 +02:00
if ( ! empty ( $line -> desc ))
2011-11-07 15:04:02 +01:00
{
if ( $line -> desc == '(CREDIT_NOTE)' ) // TODO Not sure this is used for source object
{
$discount = new DiscountAbsolute ( $this -> db );
$discount -> fetch ( $line -> fk_remise_except );
$this -> tpl [ 'description' ] = $langs -> transnoentities ( " DiscountFromCreditNote " , $discount -> getNomUrl ( 0 ));
}
elseif ( $line -> desc == '(DEPOSIT)' ) // TODO Not sure this is used for source object
{
$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' ] = ' ' ;
}
$this -> tpl [ 'vat_rate' ] = vatrate ( $line -> tva_tx , true );
$this -> tpl [ 'price' ] = price ( $line -> subprice );
$this -> tpl [ 'qty' ] = (( $line -> info_bits & 2 ) != 2 ) ? $line -> qty : ' ' ;
$this -> tpl [ 'remise_percent' ] = (( $line -> info_bits & 2 ) != 2 ) ? vatrate ( $line -> remise_percent , true ) : ' ' ;
2012-04-29 11:00:28 +02:00
// Output template part (modules that overwrite templates must declare this into descriptor)
// Use global variables + $dateSelector + $seller and $buyer
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( '/core/tpl' ));
foreach ( $dirtpls as $reldir )
{
2012-08-25 15:21:34 +02:00
$tpl = dol_buildpath ( $reldir . '/originproductline.tpl.php' );
if ( empty ( $conf -> file -> strict_mode )) {
$res =@ include $tpl ;
} else {
$res = include $tpl ; // for debug
2012-08-25 03:28:30 +02:00
}
2012-04-29 11:00:28 +02:00
if ( $res ) break ;
}
2011-11-07 15:04:02 +01:00
}
2012-07-18 15:26:33 +02:00
2013-04-19 13:30:08 +02:00
/**
2013-07-07 13:14:32 +02:00
* get Margin info
2013-04-19 13:30:08 +02:00
*
2013-07-07 13:14:32 +02:00
* @ param string $force_price True of not
* @ return mixed Array with info
2013-04-19 13:30:08 +02:00
*/
2014-05-13 10:12:45 +02:00
function getMarginInfos ( $force_price = false )
{
2013-04-19 13:30:08 +02:00
global $conf ;
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php' ;
$marginInfos = array (
'pa_products' => 0 ,
'pv_products' => 0 ,
'margin_on_products' => 0 ,
'margin_rate_products' => '' ,
'mark_rate_products' => '' ,
'pa_services' => 0 ,
'pv_services' => 0 ,
'margin_on_services' => 0 ,
'margin_rate_services' => '' ,
'mark_rate_services' => '' ,
'pa_total' => 0 ,
'pv_total' => 0 ,
'total_margin' => 0 ,
'total_margin_rate' => '' ,
'total_mark_rate' => ''
);
foreach ( $this -> lines as $line ) {
if ( empty ( $line -> pa_ht ) && isset ( $line -> fk_fournprice ) && ! $force_price ) {
$product = new ProductFournisseur ( $this -> db );
if ( $product -> fetch_product_fournisseur_price ( $line -> fk_fournprice ))
2013-09-15 14:52:41 +02:00
$line -> pa_ht = $product -> fourn_unitprice * ( 1 - $product -> fourn_remise_percent / 100 );
2013-04-19 13:30:08 +02:00
if ( isset ( $conf -> global -> MARGIN_TYPE ) && $conf -> global -> MARGIN_TYPE == " 2 " && $product -> fourn_unitcharges > 0 )
$line -> pa_ht += $product -> fourn_unitcharges ;
}
// si prix d'achat non renseigné et devrait l'être, alors prix achat = prix vente
if (( ! isset ( $line -> pa_ht ) || $line -> pa_ht == 0 ) && $line -> subprice > 0 && ( isset ( $conf -> global -> ForceBuyingPriceIfNull ) && $conf -> global -> ForceBuyingPriceIfNull == 1 )) {
$line -> pa_ht = $line -> subprice * ( 1 - ( $line -> remise_percent / 100 ));
}
2012-07-18 15:26:33 +02:00
2013-04-19 13:30:08 +02:00
// calcul des marges
if ( isset ( $line -> fk_remise_except ) && isset ( $conf -> global -> MARGIN_METHODE_FOR_DISCOUNT )) { // remise
2013-09-24 17:49:56 +02:00
$pa = $line -> qty * $line -> pa_ht ;
$pv = $line -> qty * $line -> subprice * ( 1 - $line -> remise_percent / 100 );
2013-04-19 13:30:08 +02:00
if ( $conf -> global -> MARGIN_METHODE_FOR_DISCOUNT == '1' ) { // remise globale considérée comme produit
2013-09-24 17:49:56 +02:00
$marginInfos [ 'pa_products' ] += $pa ;
$marginInfos [ 'pv_products' ] += $pv ;
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ( $pv < 0 )
$marginInfos [ 'margin_on_products' ] += - 1 * ( abs ( $pv ) - $pa );
else
$marginInfos [ 'margin_on_products' ] += $pv - $pa ;
2012-07-18 15:26:33 +02:00
}
2013-04-19 13:30:08 +02:00
elseif ( $conf -> global -> MARGIN_METHODE_FOR_DISCOUNT == '2' ) { // remise globale considérée comme service
2013-09-24 17:49:56 +02:00
$marginInfos [ 'pa_services' ] += $pa ;
$marginInfos [ 'pv_services' ] += $pv ;
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ( $pv < 0 )
$marginInfos [ 'margin_on_services' ] += - 1 * ( abs ( $pv ) - $pa );
else
$marginInfos [ 'margin_on_services' ] += $pv - $pa ;
2012-07-18 15:26:33 +02:00
}
2013-04-19 13:30:08 +02:00
elseif ( $conf -> global -> MARGIN_METHODE_FOR_DISCOUNT == '3' ) { // remise globale prise en compte uniqt sur total
2013-09-24 17:49:56 +02:00
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
2012-07-18 15:26:33 +02:00
}
}
2013-04-19 13:30:08 +02:00
else {
$type = $line -> product_type ? $line -> product_type : $line -> fk_product_type ;
if ( $type == 0 ) { // product
2013-09-24 17:49:56 +02:00
$pa = $line -> qty * $line -> pa_ht ;
$pv = $line -> qty * $line -> subprice * ( 1 - $line -> remise_percent / 100 );
$marginInfos [ 'pa_products' ] += $pa ;
$marginInfos [ 'pv_products' ] += $pv ;
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ( $pv < 0 )
$marginInfos [ 'margin_on_products' ] += - 1 * ( abs ( $pv ) - $pa );
else
$marginInfos [ 'margin_on_products' ] += $pv - $pa ;
2013-04-19 13:30:08 +02:00
}
elseif ( $type == 1 ) { // service
2013-09-24 17:49:56 +02:00
$pa = $line -> qty * $line -> pa_ht ;
$pv = $line -> qty * $line -> subprice * ( 1 - $line -> remise_percent / 100 );
$marginInfos [ 'pa_services' ] += $pa ;
$marginInfos [ 'pv_services' ] += $pv ;
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ( $pv < 0 )
$marginInfos [ 'margin_on_services' ] += - 1 * ( abs ( $pv ) - $pa );
else
$marginInfos [ 'margin_on_services' ] += $pv - $pa ;
2013-04-19 13:30:08 +02:00
}
}
}
if ( $marginInfos [ 'pa_products' ] > 0 )
2013-10-30 18:17:37 +01:00
$marginInfos [ 'margin_rate_products' ] = 100 * $marginInfos [ 'margin_on_products' ] / $marginInfos [ 'pa_products' ];
2013-04-19 13:30:08 +02:00
if ( $marginInfos [ 'pv_products' ] > 0 )
2013-10-30 18:17:37 +01:00
$marginInfos [ 'mark_rate_products' ] = 100 * $marginInfos [ 'margin_on_products' ] / $marginInfos [ 'pv_products' ];
2013-04-19 13:30:08 +02:00
if ( $marginInfos [ 'pa_services' ] > 0 )
2013-10-30 18:17:37 +01:00
$marginInfos [ 'margin_rate_services' ] = 100 * $marginInfos [ 'margin_on_services' ] / $marginInfos [ 'pa_services' ];
2013-04-19 13:30:08 +02:00
if ( $marginInfos [ 'pv_services' ] > 0 )
2013-10-30 18:17:37 +01:00
$marginInfos [ 'mark_rate_services' ] = 100 * $marginInfos [ 'margin_on_services' ] / $marginInfos [ 'pv_services' ];
2013-04-19 13:30:08 +02:00
2013-09-24 17:49:56 +02:00
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ( $marginInfos [ 'pv_total' ] < 0 )
$marginInfos [ 'total_margin' ] = - 1 * ( abs ( $marginInfos [ 'pv_total' ]) - $marginInfos [ 'pa_total' ]);
else
$marginInfos [ 'total_margin' ] = $marginInfos [ 'pv_total' ] - $marginInfos [ 'pa_total' ];
2013-04-19 13:30:08 +02:00
if ( $marginInfos [ 'pa_total' ] > 0 )
2013-10-30 18:17:37 +01:00
$marginInfos [ 'total_margin_rate' ] = 100 * $marginInfos [ 'total_margin' ] / $marginInfos [ 'pa_total' ];
2013-04-19 13:30:08 +02:00
if ( $marginInfos [ 'pv_total' ] > 0 )
2013-10-30 18:17:37 +01:00
$marginInfos [ 'total_mark_rate' ] = 100 * $marginInfos [ 'total_margin' ] / $marginInfos [ 'pv_total' ];
2013-04-19 13:30:08 +02:00
return $marginInfos ;
}
/**
2014-05-12 13:25:36 +02:00
* Show the array with all margin infos
2013-04-19 13:30:08 +02:00
*
2013-05-20 17:37:08 +02:00
* @ param string $force_price Force price
* @ return void
2013-04-19 13:30:08 +02:00
*/
2013-05-20 17:37:08 +02:00
function displayMarginInfos ( $force_price = false )
{
global $langs , $conf , $user ;
if ( ! empty ( $user -> societe_id )) return ;
2013-08-24 22:09:32 +02:00
2013-08-20 12:11:19 +02:00
if ( ! $user -> rights -> margins -> liretous ) return ;
2013-04-19 13:30:08 +02:00
2014-06-26 22:34:40 +02:00
$rounding = min ( $conf -> global -> MAIN_MAX_DECIMALS_UNIT , $conf -> global -> MAIN_MAX_DECIMALS_TOT );
2013-11-01 17:58:38 +01:00
2013-04-19 13:30:08 +02:00
$marginInfo = $this -> getMarginInfos ( $force_price );
2014-06-26 23:04:26 +02:00
if ( ! empty ( $conf -> global -> MARGININFO_HIDE_SHOW ))
{
2014-06-26 22:34:40 +02:00
print " <img onclick= \" $ ('.margininfos').toggle(); \" src=' " . img_picto ( $langs -> trans ( " Hide " ) . " / " . $langs -> trans ( " Show " ), 'object_margin.png' , '' , '' , 1 ) . " '> " ;
2014-06-26 23:04:26 +02:00
if ( $conf -> global -> MARGININFO_HIDE_SHOW == 2 ) print '<script>$(document).ready(function() {$(".margininfos").hide();});</script>' ; // hide by default
}
2014-05-29 21:01:00 +02:00
2014-05-19 09:32:42 +02:00
print '<table class="nobordernopadding margintable" width="100%">' ;
2013-04-19 13:30:08 +02:00
print '<tr class="liste_titre">' ;
print '<td width="30%">' . $langs -> trans ( 'Margins' ) . '</td>' ;
print '<td width="20%" align="right">' . $langs -> trans ( 'SellingPrice' ) . '</td>' ;
if ( $conf -> global -> MARGIN_TYPE == " 1 " )
print '<td width="20%" align="right">' . $langs -> trans ( 'BuyingPrice' ) . '</td>' ;
else
print '<td width="20%" align="right">' . $langs -> trans ( 'CostPrice' ) . '</td>' ;
print '<td width="20%" align="right">' . $langs -> trans ( 'Margin' ) . '</td>' ;
if ( ! empty ( $conf -> global -> DISPLAY_MARGIN_RATES ))
print '<td align="right">' . $langs -> trans ( 'MarginRate' ) . '</td>' ;
if ( ! empty ( $conf -> global -> DISPLAY_MARK_RATES ))
print '<td align="right">' . $langs -> trans ( 'MarkRate' ) . '</td>' ;
print '</tr>' ;
2014-05-29 21:01:00 +02:00
if ( ! empty ( $conf -> product -> enabled ))
{
//if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
print '<tr class="impair">' ;
print '<td>' . $langs -> trans ( 'MarginOnProducts' ) . '</td>' ;
print '<td align="right">' . price ( $marginInfo [ 'pv_products' ], null , null , null , null , $rounding ) . '</td>' ;
print '<td align="right">' . price ( $marginInfo [ 'pa_products' ], null , null , null , null , $rounding ) . '</td>' ;
print '<td align="right">' . price ( $marginInfo [ 'margin_on_products' ], null , null , null , null , $rounding ) . '</td>' ;
if ( ! empty ( $conf -> global -> DISPLAY_MARGIN_RATES ))
print '<td align="right">' . (( $marginInfo [ 'margin_rate_products' ] == '' ) ? '' : price ( $marginInfo [ 'margin_rate_products' ], null , null , null , null , $rounding ) . '%' ) . '</td>' ;
if ( ! empty ( $conf -> global -> DISPLAY_MARK_RATES ))
print '<td align="right">' . (( $marginInfo [ 'mark_rate_products' ] == '' ) ? '' : price ( $marginInfo [ 'mark_rate_products' ], null , null , null , null , $rounding ) . '%' ) . '</td>' ;
print '</tr>' ;
}
if ( ! empty ( $conf -> service -> enabled ))
{
print '<tr class="pair">' ;
print '<td>' . $langs -> trans ( 'MarginOnServices' ) . '</td>' ;
print '<td align="right">' . price ( $marginInfo [ 'pv_services' ], null , null , null , null , $rounding ) . '</td>' ;
print '<td align="right">' . price ( $marginInfo [ 'pa_services' ], null , null , null , null , $rounding ) . '</td>' ;
print '<td align="right">' . price ( $marginInfo [ 'margin_on_services' ], null , null , null , null , $rounding ) . '</td>' ;
if ( ! empty ( $conf -> global -> DISPLAY_MARGIN_RATES ))
print '<td align="right">' . (( $marginInfo [ 'margin_rate_services' ] == '' ) ? '' : price ( $marginInfo [ 'margin_rate_services' ], null , null , null , null , $rounding ) . '%' ) . '</td>' ;
if ( ! empty ( $conf -> global -> DISPLAY_MARK_RATES ))
print '<td align="right">' . (( $marginInfo [ 'mark_rate_services' ] == '' ) ? '' : price ( $marginInfo [ 'mark_rate_services' ], null , null , null , null , $rounding ) . '%' ) . '</td>' ;
print '</tr>' ;
}
if ( ! empty ( $conf -> product -> enabled ) && ! empty ( $conf -> service -> enabled ))
{
print '<tr class="impair">' ;
print '<td>' . $langs -> trans ( 'TotalMargin' ) . '</td>' ;
print '<td align="right">' . price ( $marginInfo [ 'pv_total' ], null , null , null , null , $rounding ) . '</td>' ;
print '<td align="right">' . price ( $marginInfo [ 'pa_total' ], null , null , null , null , $rounding ) . '</td>' ;
print '<td align="right">' . price ( $marginInfo [ 'total_margin' ], null , null , null , null , $rounding ) . '</td>' ;
if ( ! empty ( $conf -> global -> DISPLAY_MARGIN_RATES ))
print '<td align="right">' . (( $marginInfo [ 'total_margin_rate' ] == '' ) ? '' : price ( $marginInfo [ 'total_margin_rate' ], null , null , null , null , $rounding ) . '%' ) . '</td>' ;
if ( ! empty ( $conf -> global -> DISPLAY_MARK_RATES ))
print '<td align="right">' . (( $marginInfo [ 'total_mark_rate' ] == '' ) ? '' : price ( $marginInfo [ 'total_mark_rate' ], null , null , null , null , $rounding ) . '%' ) . '</td>' ;
print '</tr>' ;
}
2013-04-19 13:30:08 +02:00
print '</table>' ;
}
2013-09-04 10:45:15 +02:00
2014-04-23 19:04:10 +02:00
2014-03-15 14:54:23 +01:00
/**
2014-04-23 19:04:10 +02:00
* Add resources to the current object : add entry into llx_element_resources
* Need $this -> element & $this -> id
2014-03-15 14:54:23 +01:00
*
2014-05-14 12:19:45 +02:00
* @ param int $resource_id Resource id
2014-03-15 14:54:23 +01:00
* @ param string $resource_element Resource element
2014-05-14 12:19:45 +02:00
* @ param int $busy Busy or not
* @ param int $mandatory Mandatory or not
* @ return int <= 0 if KO , > 0 if OK
2014-03-15 14:54:23 +01:00
*/
function add_element_resource ( $resource_id , $resource_element , $busy = 0 , $mandatory = 0 )
{
$this -> db -> begin ();
2014-04-23 19:04:10 +02:00
2014-03-15 14:54:23 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " element_resources ( " ;
$sql .= " resource_id " ;
$sql .= " , resource_type " ;
$sql .= " , element_id " ;
$sql .= " , element_type " ;
$sql .= " , busy " ;
$sql .= " , mandatory " ;
$sql .= " ) VALUES ( " ;
$sql .= $resource_id ;
$sql .= " , ' " . $resource_element . " ' " ;
$sql .= " , ' " . $this -> id . " ' " ;
$sql .= " , ' " . $this -> element . " ' " ;
$sql .= " , ' " . $busy . " ' " ;
$sql .= " , ' " . $mandatory . " ' " ;
$sql .= " ) " ;
2014-04-23 19:04:10 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::add_element_resource " , LOG_DEBUG );
2014-03-15 14:54:23 +01:00
if ( $this -> db -> query ( $sql ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return 0 ;
}
}
2014-05-14 12:19:45 +02:00
2014-05-11 12:26:57 +02:00
/**
* Delete a link to resource line
*
* @ param int $rowid Id of resource line to delete
* @ param int $element element name ( for trigger ) TODO : use $this -> element into commonobject class
* @ param int $notrigger Disable all triggers
* @ return int > 0 if OK , < 0 if KO
*/
function delete_resource ( $rowid , $element , $notrigger = 0 )
{
global $user , $langs , $conf ;
2014-05-14 12:19:45 +02:00
2014-06-25 16:51:26 +02:00
$this -> db -> begin ();
2014-05-14 12:19:45 +02:00
2014-05-11 12:26:57 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_resources " ;
$sql .= " WHERE rowid = " . $rowid ;
2014-05-14 12:19:45 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_resource " , LOG_DEBUG );
2014-05-11 12:26:57 +02:00
if ( $this -> db -> query ( $sql ))
{
if ( ! $notrigger )
{
2014-06-25 16:51:26 +02:00
$result = $this -> call_trigger ( strtoupper ( $element ) . '_DELETE_RESOURCE' , $user );
if ( $result < 0 ) { $this -> db -> rollback (); return - 1 ; }
2014-05-11 12:26:57 +02:00
}
2014-05-14 12:19:45 +02:00
2014-05-11 12:26:57 +02:00
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
2014-06-25 16:51:26 +02:00
$this -> db -> rollback ();
2014-05-11 12:26:57 +02:00
return - 1 ;
}
}
2014-04-23 19:04:10 +02:00
2013-09-10 13:45:39 +02:00
/**
* Overwrite magic function to solve problem of cloning object that are kept as references
2013-09-18 16:46:54 +02:00
*
2013-09-10 13:45:39 +02:00
* @ return void
*/
2013-09-04 10:45:15 +02:00
function __clone ()
{
// Force a copy of this->lines, otherwise it will point to same object.
if ( isset ( $this -> lines ) && is_array ( $this -> lines ))
{
2013-09-10 13:45:39 +02:00
$nboflines = count ( $this -> lines );
for ( $i = 0 ; $i < $nboflines ; $i ++ )
2013-09-04 10:45:15 +02:00
{
$this -> lines [ $i ] = dol_clone ( $this -> lines [ $i ]);
}
}
}
2013-09-18 16:46:54 +02:00
2014-06-25 16:51:26 +02:00
/**
* Call trigger based on this instance
2014-07-05 08:59:41 +02:00
* NB : Error from trigger are stacked in interface -> errors
* NB2 : If return code of triggers are < 0 , action calling trigger should cancel all transaction .
2014-06-26 23:04:26 +02:00
*
2014-06-25 16:51:26 +02:00
* @ param string $trigger_name trigger ' s name to execute
* @ param User $user Object user
* @ return int Result of run_triggers
*/
function call_trigger ( $trigger_name , $user )
{
2014-07-05 08:59:41 +02:00
global $langs , $conf ;
2014-06-26 23:04:26 +02:00
2014-07-05 08:59:41 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( $trigger_name , $this , $user , $langs , $conf );
if ( $result < 0 )
{
if ( ! empty ( $this -> errors ))
{
$this -> errors = array_merge ( $this -> errors , $interface -> errors );
}
else
{
$this -> errors = $interface -> errors ;
}
}
return $result ;
2014-06-25 16:51:26 +02:00
}
2013-09-18 16:46:54 +02:00
2013-04-19 13:30:08 +02:00
}