2006-06-18 17:06:29 +02:00
< ? php
2015-04-26 16:30:33 +02:00
/* Copyright ( C ) 2006 - 2015 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 >
2015-04-22 23:31:24 +02:00
* Copyright ( C ) 2010 - 2015 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 >
2015-04-18 20:50:03 +02:00
* Copyright ( C ) 2012 - 2015 Marcos GarcÃa < marcosgdf @ gmail . com >
2015-04-23 23:21:06 +02:00
* Copyright ( C ) 2012 - 2015 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
2015-02-26 14:15:33 +01:00
* Copyright ( C ) 2012 Cedric Salvador < csalvador @ gpcsolutions . fr >
2015-11-25 20:19:18 +01:00
* Copyright ( C ) 2015 Alexandre Spangaro < aspangaro . dolibarr @ gmail . com >
2016-04-16 12:19:46 +02:00
* Copyright ( C ) 2016 Bahfir abbes < dolipar @ dolipar . org >
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
{
2015-03-17 10:47:20 +01:00
/**
2015-03-17 10:55:45 +01:00
* @ var DoliDb Database handler ( result of a new DoliDB )
2015-03-17 10:47:20 +01:00
*/
public $db ;
2014-12-26 22:04:16 +01:00
2015-05-23 23:17:39 +02:00
/**
* @ var int The object identifier
*/
public $id ;
2014-12-26 22:04:16 +01:00
/**
2015-03-17 10:55:45 +01:00
* @ var string Error string
2015-03-17 10:47:20 +01:00
* @ deprecated Use instead the array of error strings
2015-04-23 23:21:06 +02:00
* @ see errors
2014-12-26 22:04:16 +01:00
*/
2011-11-07 15:04:02 +01:00
public $error ;
2015-09-07 14:22:03 +02:00
/**
* @ var string [] Array of error strings
*/
public $errors = array ();
2014-12-28 19:16:14 +01:00
/**
2015-03-17 10:55:45 +01:00
* @ var string Key value used to track if data is coming from import wizard
2014-12-28 19:16:14 +01:00
*/
2013-06-27 16:02:52 +02:00
public $import_key ;
2013-07-04 00:11:40 +02:00
2015-03-17 10:47:20 +01:00
/**
2015-03-17 10:55:45 +01:00
* @ var mixed Contains data to manage extrafields
2015-03-17 10:47:20 +01:00
*/
2012-07-25 11:46:33 +02:00
public $array_options = array ();
2014-12-28 19:16:14 +01:00
/**
2015-03-17 10:55:45 +01:00
* @ var int [] Array of linked objects ids . Loaded by -> fetchObjectLinked
2014-12-28 19:16:14 +01:00
*/
2015-03-17 10:47:20 +01:00
public $linkedObjectsIds ;
2012-07-25 11:46:33 +02:00
2014-12-25 21:47:39 +01:00
/**
2015-03-17 10:55:45 +01:00
* @ var mixed Array of linked objects . Loaded by -> fetchObjectLinked
2014-12-25 21:47:39 +01:00
*/
2015-03-17 10:47:20 +01:00
public $linkedObjects ;
2011-11-07 15:04:02 +01:00
2014-12-25 21:47:39 +01:00
/**
2015-03-17 10:52:27 +01:00
* @ var string Column name of the ref field .
2014-12-25 21:47:39 +01:00
*/
protected $table_ref_field = '' ;
2011-11-07 15:04:02 +01:00
2015-03-17 10:47:20 +01:00
2015-04-26 16:30:33 +02:00
// Following vars are used by some objects only. We keep this property here in CommonObject to be able to provide common method using them.
/**
2015-04-23 23:21:06 +02:00
* @ var string [] Can be used to pass information when only object is provided to method
2015-04-26 16:30:33 +02:00
*/
public $context = array ();
/**
* @ var string Contains canvas name if record is an alternative canvas record
*/
public $canvas ;
2015-05-23 23:17:39 +02:00
/**
* @ var Project The related project
* @ see fetch_projet ()
*/
public $project ;
/**
* @ var int The related project ID
* @ see setProject (), project
*/
public $fk_project ;
/**
* @ deprecated
* @ see project
*/
public $projet ;
2015-05-05 22:09:58 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var Contact a related contact
* @ see fetch_contact ()
*/
public $contact ;
/**
* @ var int The related contact ID
* @ see fetch_contact ()
*/
public $contact_id ;
/**
* @ var Societe A related thirdparty
* @ see fetch_thirdparty ()
*/
public $thirdparty ;
/**
* @ var User A related user
* @ see fetch_user ()
*/
public $user ;
/**
* @ var CommonObject An originating object ?
* @ see fetch_origin ()
*/
public $origin ;
/**
* @ var int The originating object ?
* @ see fetch_origin (), origin
*/
public $origin_id ;
/**
* @ var string The object ' s reference
*/
public $ref ;
/**
* @ var string The object ' s previous reference
*/
public $ref_previous ;
/**
* @ var string The object ' s next reference
*/
public $ref_next ;
/**
* @ var string An external reference for the object
*/
public $ref_ext ;
/**
* @ var string
*/
public $element ;
/**
* @ var string
*/
public $table_element ;
/**
* @ var
*/
public $table_element_line ;
/**
* @ var int The object ' s status
* @ see setStatut ()
*/
public $statut ;
/**
* @ var string
* @ see getFullAddress ()
*/
public $country ;
/**
* @ var int
* @ see getFullAddress (), country
*/
public $country_id ;
/**
* @ var string
* @ see getFullAddress (), isInEEC (), country
*/
public $country_code ;
/**
* @ var int
* @ see fetch_barcode ()
*/
public $barcode_type ;
/**
* @ var string
* @ see fetch_barcode (), barcode_type
*/
public $barcode_type_code ;
/**
* @ var string
* @ see fetch_barcode (), barcode_type
*/
public $barcode_type_label ;
/**
* @ var string
* @ see fetch_barcode (), barcode_type
*/
public $barcode_type_coder ;
/**
2016-02-24 12:01:50 +01:00
* @ var int Payment method ID ( cheque , cash , ... )
2015-05-23 23:17:39 +02:00
* @ see setPaymentMethods ()
*/
public $mode_reglement_id ;
2015-04-23 23:21:06 +02:00
/**
2016-05-08 12:08:23 +02:00
* @ var int Payment terms ID
2015-05-23 23:17:39 +02:00
* @ see setPaymentTerms ()
2015-04-23 23:21:06 +02:00
*/
2015-05-23 23:17:39 +02:00
public $cond_reglement_id ;
2015-04-23 23:21:06 +02:00
/**
2016-05-08 12:08:23 +02:00
* @ var int Payment terms ID
* @ deprecated Kept for compatibility
2015-05-23 23:17:39 +02:00
* @ see cond_reglement_id ;
2015-04-23 23:21:06 +02:00
*/
2015-05-23 23:17:39 +02:00
public $cond_reglement ;
2015-04-23 23:21:06 +02:00
/**
2015-05-23 23:17:39 +02:00
* @ var int Delivery address ID
* @ see setDeliveryAddress ()
2015-04-23 23:21:06 +02:00
*/
2015-05-23 23:17:39 +02:00
public $fk_delivery_address ;
2015-03-17 10:47:20 +01:00
2015-05-23 23:17:39 +02:00
/**
* @ var int Shipping method ID
* @ see setShippingMethod ()
*/
public $shipping_method_id ;
/**
* @ var string
* @ see SetDocModel ()
*/
public $modelpdf ;
/**
* @ var int Bank account ID
* @ see SetBankAccount ()
*/
public $fk_account ;
/**
* @ var string Public note
* @ see update_note ()
*/
public $note_public ;
/**
* @ var string Private note
* @ see update_note ()
*/
public $note_private ;
/**
* @ deprecated
* @ see note_public
*/
public $note ;
/**
2015-09-24 20:12:55 +02:00
* @ var float Total amount before taxes
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_ht ;
/**
2015-09-24 20:12:55 +02:00
* @ var float Total VAT amount
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_tva ;
/**
2015-09-24 20:12:55 +02:00
* @ var float Total local tax 1 amount
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_localtax1 ;
/**
2015-09-24 20:12:55 +02:00
* @ var float Total local tax 2 amount
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_localtax2 ;
/**
2015-09-24 20:12:55 +02:00
* @ var float Total amount with taxes
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_ttc ;
2014-12-25 21:47:39 +01:00
2015-05-23 23:17:39 +02:00
/**
* @ var CommonObjectLine []
*/
public $lines ;
/**
* @ var int
* @ see setIncoterms ()
*/
public $fk_incoterms ;
/**
* @ var string
* @ see SetIncoterms ()
*/
public $libelle_incoterms ;
/**
* @ var string
* @ see display_incoterms ()
*/
public $location_incoterms ;
public $name ;
public $lastname ;
public $firstname ;
public $civility_id ;
// No constructor as it is an abstract class
2011-11-07 15:04:02 +01:00
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 = '' )
{
2015-05-22 17:22:57 +02:00
global $db , $conf ;
2014-05-13 10:12:45 +02:00
$sql = " SELECT rowid, ref, ref_ext " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $element ;
2015-07-08 16:03:15 +02:00
$sql .= " WHERE entity IN ( " . getEntity ( $element , true ) . " ) " ;
2015-06-06 12:26:47 +02:00
2015-05-22 17:22:57 +02:00
if ( $id > 0 ) $sql .= " AND rowid = " . $db -> escape ( $id );
else if ( $ref ) $sql .= " AND ref = ' " . $db -> escape ( $ref ) . " ' " ;
else if ( $ref_ext ) $sql .= " AND ref_ext = ' " . $db -> escape ( $ref_ext ) . " ' " ;
2014-05-13 10:12:45 +02:00
else {
$error = 'ErrorWrongParameters' ;
dol_print_error ( get_class () . " ::isExistingObject " . $error , LOG_ERR );
return - 1 ;
}
2015-12-07 02:02:03 +01:00
if ( $ref || $ref_ext ) $sql .= " AND entity = " . $conf -> entity ;
2014-05-13 10:12:45 +02:00
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 ()
{
2016-02-05 18:37:01 +01:00
return $this -> error . ( is_array ( $this -> errors ) ? (( $this -> error != '' ? ', ' : '' ) . join ( ', ' , $this -> errors )) : '' );
2013-06-28 12:44:17 +02:00
}
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
2015-09-21 17:28:11 +02:00
* @ param int $nameorder - 1 = Auto , 0 = Lastname + Firstname , 1 = Firstname + Lastname , 2 = Firstname
2012-02-01 13:21:39 +01:00
* @ param int $maxlen Maximum length
* @ return string String with full name
*/
function getFullName ( $langs , $option = 0 , $nameorder =- 1 , $maxlen = 0 )
{
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
2015-10-10 01:51:12 +02:00
/**
2016-09-18 21:13:23 +02:00
* Return full address for banner
2015-10-10 01:51:12 +02:00
*
2015-10-10 18:38:22 +02:00
* @ param string $htmlkey HTML id to make banner content unique
* @ param Object $object Object ( thirdparty , thirdparty of contact for contact , null for a member )
2015-10-10 01:51:12 +02:00
* @ return string Full address string
*/
2015-10-10 18:38:22 +02:00
function getBannerAddress ( $htmlkey , $object )
2015-10-10 01:51:12 +02:00
{
global $conf , $langs ;
2016-02-22 13:28:08 +01:00
$countriesusingstate = array ( 'AU' , 'US' , 'IN' , 'GB' , 'ES' , 'UK' , 'TR' ); // See also option MAIN_FORCE_STATE_INTO_ADDRESS
2016-06-30 17:28:13 +02:00
2015-10-16 16:14:33 +02:00
$contactid = 0 ;
2015-10-11 12:27:04 +02:00
$thirdpartyid = 0 ;
2015-10-16 16:14:33 +02:00
if ( $this -> element == 'societe' )
2015-10-11 12:27:04 +02:00
{
$thirdpartyid = $this -> id ;
}
2015-10-16 16:14:33 +02:00
if ( $this -> element == 'contact' )
2015-10-11 12:27:04 +02:00
{
$contactid = $this -> id ;
2015-10-11 15:21:21 +02:00
$thirdpartyid = $object -> fk_soc ;
}
2015-10-16 16:14:33 +02:00
if ( $this -> element == 'user' )
2015-10-11 15:21:21 +02:00
{
$contactid = $this -> contact_id ;
$thirdpartyid = $object -> fk_soc ;
2015-10-11 12:27:04 +02:00
}
2016-06-30 17:28:13 +02:00
2015-10-11 12:27:04 +02:00
$out = '<!-- BEGIN part to show address block -->' ;
2016-06-30 17:28:13 +02:00
2015-10-10 01:51:12 +02:00
$outdone = 0 ;
$coords = $this -> getFullAddress ( 1 , ', ' );
2016-06-30 17:28:13 +02:00
if ( $coords )
2015-10-10 01:51:12 +02:00
{
2015-10-11 15:21:21 +02:00
if ( ! empty ( $conf -> use_javascript_ajax ))
{
$namecoords = $this -> getFullName ( $langs , 1 ) . '<br>' . $coords ;
// hideonsmatphone because copyToClipboard call jquery dialog that does not work with jmobile
$out .= '<a href="#" class="hideonsmartphone" onclick="return copyToClipboard(\'' . dol_escape_js ( $namecoords ) . '\',\'' . dol_escape_js ( $langs -> trans ( " HelpCopyToClipboard " )) . '\');">' ;
$out .= img_picto ( $langs -> trans ( " Address " ), 'object_address.png' );
$out .= '</a> ' ;
}
2016-09-18 21:13:23 +02:00
$out .= dol_print_address ( $coords , 'address_' . $htmlkey . '_' . $this -> id , $this -> element , $this -> id , 1 , ', ' ); $outdone ++ ;
2015-10-10 01:51:12 +02:00
$outdone ++ ;
}
2016-02-22 13:28:08 +01:00
if ( ! in_array ( $this -> country_code , $countriesusingstate ) && empty ( $conf -> global -> MAIN_FORCE_STATE_INTO_ADDRESS ) // If MAIN_FORCE_STATE_INTO_ADDRESS is on, state is already returned previously with getFullAddress
2016-06-30 17:28:13 +02:00
&& empty ( $conf -> global -> SOCIETE_DISABLE_STATE ) && $this -> state )
2015-10-10 01:51:12 +02:00
{
2016-02-22 13:28:08 +01:00
$out .= ( $outdone ? ' - ' : '' ) . $this -> state ;
2015-10-10 01:51:12 +02:00
$outdone ++ ;
}
2015-10-11 12:27:04 +02:00
2016-02-20 02:19:18 +01:00
if ( ! empty ( $this -> phone ) || ! empty ( $this -> phone_pro ) || ! empty ( $this -> phone_mobile ) || ! empty ( $this -> phone_perso ) || ! empty ( $this -> fax ) || ! empty ( $this -> office_phone ) || ! empty ( $this -> user_mobile ) || ! empty ( $this -> office_fax )) $out .= ( $outdone ? '<br>' : '' );
2015-10-16 16:14:33 +02:00
if ( ! empty ( $this -> phone ) && empty ( $this -> phone_pro )) { // For objects that store pro phone into ->phone
$out .= dol_print_phone ( $this -> phone , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhonePro " )); $outdone ++ ;
2015-10-11 12:27:04 +02:00
}
if ( ! empty ( $this -> phone_pro )) {
2015-10-16 16:14:33 +02:00
$out .= dol_print_phone ( $this -> phone_pro , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhonePro " )); $outdone ++ ;
2015-10-10 01:51:12 +02:00
}
2015-10-11 12:27:04 +02:00
if ( ! empty ( $this -> phone_mobile )) {
2015-10-16 16:14:33 +02:00
$out .= dol_print_phone ( $this -> phone_mobile , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhoneMobile " )); $outdone ++ ;
2015-10-10 01:51:12 +02:00
}
2015-10-11 12:27:04 +02:00
if ( ! empty ( $this -> phone_perso )) {
2015-10-16 16:14:33 +02:00
$out .= dol_print_phone ( $this -> phone_perso , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhonePerso " )); $outdone ++ ;
2015-10-10 01:51:12 +02:00
}
2015-10-11 12:27:04 +02:00
if ( ! empty ( $this -> fax )) {
2015-10-16 16:14:33 +02:00
$out .= dol_print_phone ( $this -> fax , $this -> country_code , $contactid , $thirdpartyid , 'AC_FAX' , ' ' , 'fax' , $langs -> trans ( " Fax " )); $outdone ++ ;
2015-10-10 01:51:12 +02:00
}
2015-10-11 15:21:21 +02:00
if ( ! empty ( $this -> office_phone )) {
2015-10-16 16:14:33 +02:00
$out .= dol_print_phone ( $this -> office_phone , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhonePro " )); $outdone ++ ;
2015-10-11 15:21:21 +02:00
}
if ( ! empty ( $this -> user_mobile )) {
2015-10-16 16:14:33 +02:00
$out .= dol_print_phone ( $this -> user_mobile , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhoneMobile " )); $outdone ++ ;
2015-10-11 15:21:21 +02:00
}
if ( ! empty ( $this -> office_fax )) {
2015-10-16 16:14:33 +02:00
$out .= dol_print_phone ( $this -> fax , $this -> country_code , $contactid , $thirdpartyid , 'AC_FAX' , ' ' , 'fax' , $langs -> trans ( " Fax " )); $outdone ++ ;
2015-10-11 15:21:21 +02:00
}
2016-06-30 17:28:13 +02:00
2015-10-10 01:51:12 +02:00
$out .= '<div style="clear: both;"></div>' ;
$outdone = 0 ;
2016-06-30 17:28:13 +02:00
if ( ! empty ( $this -> email ))
2015-10-10 01:51:12 +02:00
{
$out .= dol_print_email ( $this -> email , $this -> id , $object -> id , 'AC_EMAIL' , 0 , 0 , 1 );
$outdone ++ ;
}
2016-06-30 17:28:13 +02:00
if ( ! empty ( $this -> url ))
2015-10-10 01:51:12 +02:00
{
2016-10-22 17:05:47 +02:00
$out .= dol_print_url ( $this -> url , '_goout' , 0 , 1 );
2015-10-10 01:51:12 +02:00
$outdone ++ ;
}
if ( ! empty ( $conf -> skype -> enabled ))
{
2015-11-28 14:43:16 +01:00
$out .= '<div style="clear: both;"></div>' ;
if ( $this -> skype ) $out .= dol_print_skype ( $this -> skype , $this -> id , $object -> id , 'AC_SKYPE' );
$outdone ++ ;
2015-10-10 01:51:12 +02:00
}
2016-06-30 17:28:13 +02:00
2015-10-11 12:27:04 +02:00
$out .= '<!-- END Part to show address block -->' ;
2016-06-30 17:28:13 +02:00
2015-10-10 01:51:12 +02:00
return $out ;
}
2016-06-30 17:28:13 +02:00
2011-11-07 15:04:02 +01:00
/**
* Add a link between element $this -> element and a contact
*
2015-02-17 19:49:17 +01:00
* @ param int $fk_socpeople Id of thirdparty contact ( if source = 'external' ) or id of user ( if souce = 'internal' ) to link
2016-07-28 19:02:23 +02:00
* @ param int $type_contact Type of contact ( code or id ) . Must be id or code found into table llx_c_type_contact . For example : SALESREPFOLL
2015-05-23 23:17:39 +02:00
* @ param string $source external = Contact extern ( llx_socpeople ), internal = Contact intern ( llx_user )
2012-02-01 13:21:39 +01:00
* @ 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 )
{
2015-05-23 23:17:39 +02:00
global $user , $langs ;
2011-11-07 15:04:02 +01:00
2011-12-07 15:06:29 +01:00
2016-07-28 19:02:23 +02:00
dol_syslog ( get_class ( $this ) . " ::add_contact $fk_socpeople , $type_contact , $source , $notrigger " );
2011-11-07 15:04:02 +01:00
// Check parameters
if ( $fk_socpeople <= 0 )
{
2015-09-16 22:04:03 +02:00
$langs -> load ( " errors " );
$this -> error = $langs -> trans ( " ErrorWrongValueForParameterX " , " 1 " );
2011-11-07 15:04:02 +01:00
dol_syslog ( get_class ( $this ) . " ::add_contact " . $this -> error , LOG_ERR );
return - 1 ;
}
if ( ! $type_contact )
{
2015-09-16 22:04:03 +02:00
$langs -> load ( " errors " );
$this -> error = $langs -> trans ( " ErrorWrongValueForParameterX " , " 2 " );
2011-11-07 15:04:02 +01:00
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 " ;
2014-10-14 00:27:23 +02:00
$sql .= " WHERE tc.element=' " . $this -> element . " ' " ;
$sql .= " AND tc.source=' " . $source . " ' " ;
$sql .= " AND tc.code=' " . $type_contact . " ' AND tc.active=1 " ;
//print $sql;
2011-11-07 15:04:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
2016-07-28 19:02:23 +02:00
if ( $obj ) $id_type_contact = $obj -> rowid ;
2011-11-07 15:04:02 +01:00
}
}
2016-07-28 19:02:23 +02:00
if ( $id_type_contact == 0 )
{
$this -> error = 'CODE_NOT_VALID_FOR_THIS_ELEMENT' ;
dol_syslog ( " CODE_NOT_VALID_FOR_THIS_ELEMENT " );
return - 3 ;
}
2011-11-07 15:04:02 +01:00
$datecreate = dol_now ();
2016-06-30 17:28:13 +02:00
2014-06-25 16:51:26 +02:00
$this -> db -> begin ();
2016-08-28 02:40:20 +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 ) . " ' " ;
2016-08-28 02:40:20 +02:00
$sql .= " , 4, " . $id_type_contact ;
2011-11-07 15:04:02 +01:00
$sql .= " ) " ;
$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 );
2016-06-30 17:28:13 +02:00
if ( $result < 0 )
{
$this -> db -> rollback ();
2015-09-16 22:04:03 +02:00
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' )
{
$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 )
{
2015-05-23 23:17:39 +02:00
global $user ;
2011-11-07 15:04:02 +01:00
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
*
2014-10-14 00:27:23 +02:00
* @ param string $source '' or 'internal' or 'external'
* @ param string $code Type of contact ( code or id )
* @ return int > 0 if OK , < 0 if KO
2011-11-07 15:04:02 +01:00
*/
2014-10-14 00:27:23 +02:00
function delete_linked_contact ( $source = '' , $code = '' )
2011-11-07 15:04:02 +01:00
{
$temp = array ();
2014-10-14 00:27:23 +02:00
$typeContact = $this -> liste_type_contact ( $source , '' , 0 , 0 , $code );
2011-11-07 15:04:02 +01:00
foreach ( $typeContact as $key => $value )
{
array_push ( $temp , $key );
}
$listId = implode ( " , " , $temp );
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_contact " ;
2014-10-14 00:27:23 +02:00
$sql .= " WHERE element_id = " . $this -> id ;
2016-06-04 17:55:28 +02:00
if ( $listId )
$sql .= " AND fk_c_type_contact IN ( " . $listId . " ) " ;
2011-11-07 15:04:02 +01:00
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
2014-10-14 00:27:23 +02:00
{
2011-11-07 15:04:02 +01:00
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
/**
* Get array of all contacts for an object
*
2015-05-22 12:21:36 +02:00
* @ param int $statut Status of links to get ( - 1 = all )
2012-02-01 13:21:39 +01:00
* @ 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
2016-02-26 10:38:42 +01:00
* @ param string $code Filter on this code of contact type ( 'SHIPPING' , 'BILLING' , ... )
2012-02-01 13:21:39 +01:00
* @ return array Array of contacts
2011-11-07 15:04:02 +01:00
*/
2016-02-26 10:38:42 +01:00
function liste_contact ( $statut =- 1 , $source = 'external' , $list = 0 , $code = '' )
2011-11-07 15:04:02 +01:00
{
global $langs ;
$tab = array ();
2015-05-22 12:21:36 +02:00
$sql = " SELECT ec.rowid, ec.statut as statuslink, ec.fk_socpeople as id, ec.fk_c_type_contact " ; // This field contains id of llx_socpeople or id of llx_user
if ( $source == 'internal' ) $sql .= " , '-1' as socid, t.statut as statuscontact " ;
if ( $source == 'external' || $source == 'thirdparty' ) $sql .= " , t.fk_soc as socid, t.statut as statuscontact " ;
2014-08-12 06:03:27 +02:00
$sql .= " , t.civility 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 . " ' " ;
2016-02-26 10:38:42 +01:00
if ( $code ) $sql .= " AND tc.code = ' " . $this -> db -> escape ( $code ) . " ' " ;
2011-11-07 15:04:02 +01:00
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
2015-05-22 12:21:36 +02:00
'civility' => $obj -> civility , 'lastname' => $obj -> lastname , 'firstname' => $obj -> firstname , 'email' => $obj -> email , 'statuscontact' => $obj -> statuscontact ,
'rowid' => $obj -> rowid , 'code' => $obj -> code , 'libelle' => $libelle_type , 'status' => $obj -> statuslink , '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
{
2016-02-26 10:38:42 +01:00
$this -> error = $this -> db -> lasterror ();
2011-11-07 15:04:02 +01:00
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'
2016-02-26 15:03:07 +01:00
* @ param string $order Sort order by : 'position' , 'code' , 'rowid' ...
2015-05-25 18:43:46 +02:00
* @ param int $option 0 = Return array id -> label , 1 = Return array code -> label
* @ param int $activeonly 0 = all status of contact , 1 = only the active
2014-10-14 00:27:23 +02:00
* @ param string $code Type of contact ( Example : 'CUSTOMER' , 'SERVICE' )
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
*/
2016-02-26 15:03:07 +01:00
function liste_type_contact ( $source = 'internal' , $order = 'position' , $option = 0 , $activeonly = 0 , $code = '' )
2011-11-07 15:04:02 +01:00
{
global $langs ;
2016-02-26 15:03:07 +01:00
if ( empty ( $order )) $order = 'position' ;
if ( $order == 'position' ) $order .= ',code' ;
2016-06-30 17:28:13 +02:00
2011-11-07 15:04:02 +01:00
$tab = array ();
2016-06-30 17:28:13 +02:00
$sql = " SELECT DISTINCT tc.rowid, tc.code, tc.libelle, tc.position " ;
2011-11-07 15:04:02 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
$sql .= " WHERE tc.element=' " . $this -> element . " ' " ;
2015-05-25 18:43:46 +02:00
if ( $activeonly == 1 ) $sql .= " AND tc.active=1 " ; // only the active types
2014-10-14 00:27:23 +02:00
if ( ! empty ( $source ) && $source != 'all' ) $sql .= " AND tc.source=' " . $source . " ' " ;
if ( ! empty ( $code )) $sql .= " AND tc.code=' " . $code . " ' " ;
2016-02-26 15:03:07 +01:00
$sql .= $this -> db -> order ( $order , 'ASC' );
2011-11-07 15:04:02 +01:00
//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
/**
2015-05-25 18:43:46 +02:00
* Load object contact with id = $this -> contactid into $this -> contact
2011-11-07 15:04:02 +01:00
*
2014-09-29 13:21:03 +02:00
* @ param int $contactid Id du contact . Use this -> contactid if empty .
2012-02-01 13:21:39 +01:00
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
2015-05-25 18:43:46 +02:00
function fetch_contact ( $contactid = null )
2011-11-07 15:04:02 +01:00
{
2014-09-29 13:21:03 +02:00
if ( empty ( $contactid )) $contactid = $this -> contactid ;
if ( empty ( $contactid )) return 0 ;
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
*
2014-11-20 14:44:49 +01:00
* @ param int $force_thirdparty_id Force thirdparty id
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
2014-11-20 14:44:49 +01:00
function fetch_thirdparty ( $force_thirdparty_id = 0 )
2011-11-07 15:04:02 +01:00
{
global $conf ;
2016-01-31 10:10:30 +01:00
if ( empty ( $this -> socid ) && empty ( $this -> fk_soc ) && empty ( $this -> fk_thirdparty ) && empty ( $force_thirdparty_id ))
return 0 ;
2011-11-07 15:04:02 +01:00
2016-01-31 10:10:30 +01:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2011-11-07 15:04:02 +01:00
2016-01-31 10:10:30 +01:00
$idtofetch = isset ( $this -> socid ) ? $this -> socid : ( isset ( $this -> fk_soc ) ? $this -> fk_soc : $this -> fk_thirdparty );
if ( $force_thirdparty_id )
$idtofetch = $force_thirdparty_id ;
2014-11-20 14:44:49 +01:00
2016-01-31 10:10:30 +01:00
if ( $idtofetch ) {
$thirdparty = new Societe ( $this -> db );
$result = $thirdparty -> fetch ( $idtofetch );
$this -> thirdparty = $thirdparty ;
2011-11-07 15:04:02 +01:00
2016-01-31 10:10:30 +01:00
// Use first price level if level not defined for third party
if ( ! empty ( $conf -> global -> PRODUIT_MULTIPRICES ) && empty ( $this -> thirdparty -> price_level )) {
$this -> thirdparty -> price_level = 1 ;
}
2011-11-07 15:04:02 +01:00
2016-01-31 10:10:30 +01:00
return $result ;
} else
return - 1 ;
2011-11-07 15:04:02 +01:00
}
2011-12-07 15:06:29 +01:00
2014-12-25 21:47:39 +01:00
/**
* Looks for an object with ref matching the wildcard provided
* It does only work when $this -> table_ref_field is set
*
* @ param string $ref Wildcard
* @ return int > 1 = OK , 0 = Not found or table_ref_field not defined , < 0 = KO
*/
public function fetchOneLike ( $ref )
{
if ( ! $this -> table_ref_field ) {
return 0 ;
}
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' WHERE ' . $this -> table_ref_field . ' LIKE "' . $this -> db -> escape ( $ref ) . '" LIMIT 1' ;
$query = $this -> db -> query ( $sql );
if ( ! $this -> db -> num_rows ( $query )) {
return 0 ;
}
$result = $this -> db -> fetch_object ( $query );
return $this -> fetch ( $result -> rowid );
}
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-12-04 01:36:53 +01:00
* Properties -> barcode_type that is id of barcode . Type is used to find other properties , but
2014-05-25 17:14:33 +02:00
* 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 ;
}
}
}
2015-05-23 23:17:39 +02:00
return 0 ;
2011-12-07 15:06:29 +01:00
}
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 ()
{
2015-06-03 18:24:09 +02:00
include_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
if ( empty ( $this -> fk_project ) && ! empty ( $this -> fk_projet )) $this -> fk_project = $this -> fk_projet ; // For backward compatibility
2011-11-07 15:04:02 +01:00
if ( empty ( $this -> fk_project )) return 0 ;
$project = new Project ( $this -> db );
$result = $project -> fetch ( $this -> fk_project );
2015-06-03 18:24:09 +02:00
2015-02-02 18:32:38 +01:00
$this -> projet = $project ; // deprecated
$this -> project = $project ;
2011-11-07 15:04:02 +01:00
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 ()
{
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 ;
}
/**
2016-02-16 00:31:05 +01:00
* Getter generic . Load value from a specific field
2011-11-07 15:04:02 +01:00
*
* @ 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 ;
2015-09-23 13:17:58 +02:00
if ( ! empty ( $id ) && ! empty ( $field ) && ! empty ( $table )) {
$sql = " SELECT " . $field . " FROM " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE rowid = " . $id ;
2016-06-30 17:28:13 +02:00
2015-09-23 13:17:58 +02:00
dol_syslog ( get_class ( $this ) . '::getValueFrom' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$result = $row [ 0 ];
}
}
2011-11-07 15:04:02 +01:00
return $result ;
}
/**
2016-09-12 20:00:47 +02:00
* Setter generic . Update a specific field into database .
2016-09-12 20:16:39 +02:00
* Warning : Trigger is run only if param trigkey is provided .
2011-11-07 15:04:02 +01:00
*
2015-05-25 18:43:46 +02:00
* @ param string $field Field to update
* @ param mixed $value New value
* @ 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
2016-09-12 20:00:47 +02:00
* @ param string $id_field To force rowid field name . 'rowid' is used if not defined
2015-05-25 18:43:46 +02:00
* @ param User | string $user Update last update fields also if user object provided
2016-09-12 20:00:47 +02:00
* @ param string $trigkey Trigger key to run ( in most cases something like 'XXX_MODIFY' )
2015-05-25 18:43:46 +02:00
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
2016-09-12 20:00:47 +02:00
function setValueFrom ( $field , $value , $table = '' , $id = null , $format = '' , $id_field = '' , $user = '' , $trigkey = '' )
2011-11-07 15:04:02 +01:00
{
2016-09-12 20:00:47 +02:00
global $user , $langs , $conf ;
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
2016-09-12 20:00:47 +02:00
$error = 0 ;
2011-11-07 15:04:02 +01:00
$this -> db -> begin ();
2016-07-28 13:53:17 +02:00
// Special case
if ( $table == 'product' && $field == 'note_private' ) $field = 'note' ;
2011-11-07 15:04:02 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $table . " SET " ;
if ( $format == 'text' ) $sql .= $field . " = ' " . $this -> db -> escape ( $value ) . " ' " ;
2016-09-12 20:16:39 +02:00
else if ( $format == 'int' ) $sql .= $field . " = " . $this -> db -> escape ( $value );
2016-09-12 20:00:47 +02:00
else if ( $format == 'date' ) $sql .= $field . " = " . ( $value ? " ' " . $this -> db -> idate ( $value ) . " ' " : " null " );
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 )
{
2016-09-12 20:00:47 +02:00
if ( $trigkey )
{
$result = $this -> call_trigger ( $trigkey , $user ); // This may set this->errors
if ( $result < 0 ) $error ++ ;
}
if ( ! $error )
{
if ( property_exists ( $this , $field )) $this -> $field = $value ;
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return - 2 ;
}
2011-11-07 15:04:02 +01:00
}
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
*/
2016-09-12 17:27:44 +02:00
function load_previous_next_ref ( $filter , $fieldid , $nodbprefix = 0 )
2011-11-07 15:04:02 +01:00
{
2015-05-23 23:17:39 +02:00
global $user ;
2011-11-07 15:04:02 +01:00
if ( ! $this -> table_element )
{
2015-03-06 01:53:02 +01:00
dol_print_error ( '' , get_class ( $this ) . " ::load_previous_next_ref was called on objet with property table_element not defined " );
2011-11-07 15:04:02 +01:00
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 " ;
2016-12-18 13:01:26 +01:00
$sql .= " WHERE te. " . $fieldid . " < ' " . $this -> db -> escape ( $this -> ref ) . " ' " ; // ->ref must always be defined (set to id if field does not exists)
2011-11-07 15:04:02 +01:00
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 " ;
2016-12-18 13:01:26 +01:00
$sql .= " WHERE te. " . $fieldid . " > ' " . $this -> db -> escape ( $this -> ref ) . " ' " ; // ->ref must always be defined (set to id if field does not exists)
2011-11-07 15:04:02 +01:00
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 ;
2015-11-02 13:20:51 +01:00
// for supplier
if ( get_class ( $this ) == 'Fournisseur' ) $this -> mode_reglement_supplier_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
2016-01-18 19:45:27 +01:00
/**
* Change the multicurrency code
*
* @ param string $code multicurrency code
* @ return int > 0 if OK , < 0 if KO
*/
function setMulticurrencyCode ( $code )
{
dol_syslog ( get_class ( $this ) . '::setMulticurrencyCode(' . $id . ')' );
if ( $this -> statut >= 0 || $this -> element == 'societe' )
{
$fieldname = 'multicurrency_code' ;
2016-06-30 17:28:13 +02:00
2016-01-18 19:45:27 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' SET ' . $fieldname . ' = "' . $this -> db -> escape ( $code ) . '"' ;
$sql .= ' WHERE rowid=' . $this -> id ;
if ( $this -> db -> query ( $sql ))
{
$this -> multicurrency_code = $code ;
2016-06-30 17:28:13 +02:00
2016-01-23 21:36:07 +01:00
list ( $fk_multicurrency , $rate ) = MultiCurrency :: getIdAndTxFromCode ( $this -> db , $code );
if ( $rate ) $this -> setMulticurrencyRate ( $rate );
2016-06-30 17:28:13 +02:00
2016-01-18 19:45:27 +01:00
return 1 ;
}
else
{
dol_syslog ( get_class ( $this ) . '::setMulticurrencyCode Erreur ' . $sql . ' - ' . $this -> db -> error ());
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
else
{
dol_syslog ( get_class ( $this ) . '::setMulticurrencyCode, status of the object is incompatible' );
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
return - 2 ;
}
}
2016-06-30 17:28:13 +02:00
2016-01-18 19:45:27 +01:00
/**
* Change the multicurrency rate
*
* @ param double $rate multicurrency rate
2016-12-10 22:07:25 +01:00
* @ param int $mode mode 1 : amounts in company currency will be recalculated , mode 2 : amounts in foreign currency
2016-01-18 19:45:27 +01:00
* @ return int > 0 if OK , < 0 if KO
*/
2016-12-10 22:07:25 +01:00
function setMulticurrencyRate ( $rate , $mode = 1 )
2016-01-18 19:45:27 +01:00
{
dol_syslog ( get_class ( $this ) . '::setMulticurrencyRate(' . $id . ')' );
if ( $this -> statut >= 0 || $this -> element == 'societe' )
{
$fieldname = 'multicurrency_tx' ;
2016-06-30 17:28:13 +02:00
2016-01-18 19:45:27 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' SET ' . $fieldname . ' = ' . $rate ;
$sql .= ' WHERE rowid=' . $this -> id ;
if ( $this -> db -> query ( $sql ))
{
$this -> multicurrency_tx = $rate ;
2016-06-30 17:28:13 +02:00
2016-01-22 22:35:32 +01:00
// Update line price
if ( ! empty ( $this -> lines ))
{
2016-06-30 17:28:13 +02:00
foreach ( $this -> lines as & $line )
2016-01-23 21:36:07 +01:00
{
2016-12-10 22:07:25 +01:00
if ( $mode == 1 ) {
$line -> subprice = 0 ;
}
2016-01-23 21:36:07 +01:00
switch ( $this -> element ) {
case 'propal' :
2016-12-11 01:04:58 +01:00
$this -> updateline ( $line -> id , $line -> subprice , $line -> qty , $line -> remise_percent , $line -> tva_tx , $line -> localtax1_tx , $line -> localtax2_tx , $line -> desc , 'HT' , $line -> info_bits , $line -> special_code , $line -> fk_parent_line , $line -> skip_update_total , $line -> fk_fournprice , $line -> pa_ht , $line -> label , $line -> product_type , $line -> date_start , $line -> date_end , $line -> array_options , $line -> fk_unit , $line -> multicurrency_subprice );
2016-01-23 21:36:07 +01:00
break ;
case 'commande' :
2016-12-11 01:04:58 +01:00
$this -> updateline ( $line -> id , $line -> desc , $line -> subprice , $line -> qty , $line -> remise_percent , $line -> tva_tx , $line -> localtax1_tx , $line -> localtax2_tx , 'HT' , $line -> info_bits , $line -> date_start , $line -> date_end , $line -> product_type , $line -> fk_parent_line , $line -> skip_update_total , $line -> fk_fournprice , $line -> pa_ht , $line -> label , $line -> special_code , $line -> array_options , $line -> fk_unit , $line -> multicurrency_subprice );
2016-01-23 21:36:07 +01:00
break ;
case 'facture' :
2016-12-10 22:07:25 +01:00
$this -> updateline ( $line -> id , $line -> desc , $line -> subprice , $line -> qty , $line -> remise_percent , $line -> date_start , $line -> date_end , $line -> tva_tx , $line -> localtax1_tx , $line -> localtax2_tx , 'HT' , $line -> info_bits , $line -> product_type , $line -> fk_parent_line , $line -> skip_update_total , $line -> fk_fournprice , $line -> pa_ht , $line -> label , $line -> special_code , $line -> array_options , $line -> situation_percent , $line -> fk_unit , $line -> multicurrency_subprice );
2016-01-23 21:36:07 +01:00
break ;
2016-02-16 23:58:21 +01:00
case 'supplier_proposal' :
2016-12-11 02:50:21 +01:00
$this -> updateline ( $line -> id , $line -> subprice , $line -> qty , $line -> remise_percent , $line -> tva_tx , $line -> localtax1_tx , $line -> localtax2_tx , $line -> desc , 'HT' , $line -> info_bits , $line -> special_code , $line -> fk_parent_line , $line -> skip_update_total , $line -> fk_fournprice , $line -> pa_ht , $line -> label , $line -> product_type , $line -> array_options , $line -> ref_fourn , $line -> multicurrency_subprice );
2016-02-16 23:58:21 +01:00
break ;
case 'order_supplier' :
2016-12-11 02:50:21 +01:00
$this -> updateline ( $line -> id , $line -> desc , $line -> subprice , $line -> qty , $line -> remise_percent , $line -> tva_tx , $line -> localtax1_tx , $line -> localtax2_tx , 'HT' , $line -> info_bits , $line -> product_type , false , $line -> date_start , $line -> date_end , $line -> array_options , $line -> fk_unit , $line -> multicurrency_subprice );
break ;
case 'invoice_supplier' :
$this -> updateline ( $line -> id , $line -> desc , $line -> subprice , $line -> tva_tx , $line -> localtax1_tx , $line -> localtax2_tx , $line -> qty , 0 , 'HT' , $line -> info_bits , $line -> product_type , $line -> remise_percent , false , $line -> date_start , $line -> date_end , $line -> array_options , $line -> fk_unit , $line -> multicurrency_subprice );
2016-02-16 23:58:21 +01:00
break ;
2016-01-23 21:36:07 +01:00
default :
dol_syslog ( get_class ( $this ) . '::setMulticurrencyRate no updateline defined' , LOG_DEBUG );
break ;
}
2016-06-30 17:28:13 +02:00
2016-01-23 21:36:07 +01:00
}
2016-01-22 22:35:32 +01:00
}
2016-06-30 17:28:13 +02:00
2016-01-18 19:45:27 +01:00
return 1 ;
}
else
{
dol_syslog ( get_class ( $this ) . '::setMulticurrencyRate Erreur ' . $sql . ' - ' . $this -> db -> error ());
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
else
{
dol_syslog ( get_class ( $this ) . '::setMulticurrencyRate, status of the object is incompatible' );
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
return - 2 ;
}
}
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 ;
2015-11-02 13:20:51 +01:00
// for supplier
if ( get_class ( $this ) == 'Fournisseur' ) $this -> cond_reglement_supplier_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 ;
}
}
2015-10-19 21:09:02 +02:00
/**
* Change the warehouse
*
* @ param int $warehouse_id Id of warehouse
* @ return int 1 if OK , 0 if KO
*/
function setWarehouse ( $warehouse_id )
{
if ( ! $this -> table_element ) {
dol_syslog ( get_class ( $this ) . " ::setWarehouse was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
if ( $warehouse_id < 0 ) $warehouse_id = 'NULL' ;
dol_syslog ( get_class ( $this ) . '::setWarehouse(' . $warehouse_id . ')' );
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET fk_warehouse = " . $warehouse_id ;
$sql .= " WHERE rowid= " . $this -> id ;
if ( $this -> db -> query ( $sql )) {
$this -> warehouse_id = ( $warehouse_id == 'NULL' ) ? null : $warehouse_id ;
return 1 ;
} else {
dol_syslog ( get_class ( $this ) . '::setWarehouse 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 ;
}
}
2015-05-23 23:17:39 +02:00
// TODO: Move line related operations to CommonObjectLine?
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
*
2016-04-09 15:07:55 +02:00
* @ param boolean $renum True to renum all already ordered lines , false to renum only not already ordered lines .
* @ param string $rowidorder ASC or DESC
* @ param boolean $fk_parent_line Table with fk_parent_line field or not
* @ return int < 0 if KO , > 0 if OK
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
}
2016-04-09 15:07:55 +02:00
return 1 ;
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' )))
{
2015-04-06 08:20:23 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note Parameter suffix must be empty, '_private' or '_public' " , LOG_ERR );
2013-04-14 23:42:24 +02:00
return - 2 ;
}
2016-07-28 13:53:17 +02:00
// Special cas
//var_dump($this->table_element);exit;
if ( $this -> table_element == 'product' ) $suffix = '' ;
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 ;
2016-07-28 13:53:17 +02:00
else
{
$this -> note = $note ; // deprecated
$this -> note_private = $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
*
2015-04-23 23:21:06 +02:00
* @ param string $note New value for note
* @ return int < 0 if KO , > 0 if OK
* @ deprecated
* @ see update_note ()
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 )
2016-04-08 19:46:56 +02:00
* @ param string $roundingadjust 'none' = Do nothing , 'auto' = Use default method ( MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND if defined , or '0' ), '0' = Force mode total of rounding , '1' = Force mode rounding of total
2012-04-07 02:24:32 +02:00
* @ param int $nodatabaseupdate 1 = Do not update database . Update only properties of object .
2016-04-08 19:46:56 +02:00
* @ param Societe $seller If roundingadjust is '0' or '1' or maybe 'auto' , it means we recalculate total for lines before calculating total for object and 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
*/
2015-05-25 18:43:46 +02:00
function update_price ( $exclspec = 0 , $roundingadjust = 'none' , $nodatabaseupdate = 0 , $seller = null )
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 ;
2015-12-29 02:43:49 +01:00
elseif ( $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' ;
}
2015-02-14 18:39:45 +01:00
if ( $this -> element == 'expensereport' )
{
$fieldup = 'value_unit' ;
}
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' ;
2016-01-13 13:42:58 +01:00
if ( $this -> table_element_line == 'facturedet' ) $sql .= ', situation_percent' ;
2016-01-18 19:45:27 +01:00
$sql .= ', multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc' ;
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 ();
2016-01-18 19:45:27 +01:00
$this -> multicurrency_total_ht = 0 ;
$this -> multicurrency_total_tva = 0 ;
$this -> multicurrency_total_ttc = 0 ;
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'
2016-01-18 19:45:27 +01:00
$multicurrency_tx = ! empty ( $this -> multicurrency_tx ) ? $this -> multicurrency_tx : 1 ;
2014-05-13 13:22:29 +02:00
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 );
2016-01-18 19:45:27 +01:00
$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 , ( isset ( $obj -> situation_percent ) ? $obj -> situation_percent : 100 ), $multicurrency_tx );
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 ;
2016-06-30 17:28:13 +02:00
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
2014-11-14 16:43:49 +01:00
// Situations totals
if ( $this -> situation_cycle_ref && $this -> situation_counter > 1 ) {
$prev_sits = $this -> get_prev_sits ();
foreach ( $prev_sits as $sit ) {
$this -> total_ht -= $sit -> total_ht ;
$this -> total_tva -= $sit -> total_tva ;
$this -> total_localtax1 -= $sit -> total_localtax1 ;
$this -> total_localtax2 -= $sit -> total_localtax2 ;
$this -> total_ttc -= $sit -> total_ttc ;
}
}
2016-06-30 17:28:13 +02:00
2016-01-18 19:45:27 +01:00
// Multicurrency
$this -> multicurrency_total_ht += $this -> total_ht * $multicurrency_tx ;
$this -> multicurrency_total_tva += $this -> total_tva * $multicurrency_tx ;
$this -> multicurrency_total_ttc += $this -> total_ttc * $multicurrency_tx ;
2016-06-30 17:28:13 +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' ;
2015-02-14 18:39:45 +01:00
if ( $this -> element == 'expensereport' ) $fieldtva = 'total_tva' ;
2015-11-15 18:23:42 +01:00
if ( $this -> element == 'supplier_proposal' ) $fieldttc = 'total' ;
2011-11-07 15:04:02 +01:00
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 ) . " ' " ;
2016-01-18 19:45:27 +01:00
$sql .= " , multicurrency_total_ht=' " . price2num ( $this -> multicurrency_total_ht , 'MT' , 1 ) . " ' " ;
$sql .= " , multicurrency_total_tva=' " . price2num ( $this -> multicurrency_total_tva , 'MT' , 1 ) . " ' " ;
$sql .= " , multicurrency_total_ttc=' " . price2num ( $this -> multicurrency_total_ttc , 'MT' , 1 ) . " ' " ;
2012-04-07 02:24:32 +02:00
$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 ++ ;
2015-02-15 22:49:23 +01:00
$this -> error = $this -> db -> lasterror ();
2016-02-11 09:50:35 +01:00
$this -> errors [] = $this -> db -> lasterror ();
2012-04-07 02:24:32 +02:00
}
}
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
2014-10-17 14:45:12 +02:00
* @ see fetchObjectLinked , updateObjectLinked , deleteObjectLinked
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
2016-08-04 11:57:37 +02:00
// Special case
if ( $origin == 'order' ) $origin = 'commande' ;
if ( $origin == 'invoice' ) $origin = 'facture' ;
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
2015-10-20 02:34:53 +02:00
* Possible usage for parameters :
* - all parameters empty -> we look all link to current object ( current object can be source or target )
2017-01-11 16:08:04 +01:00
* - source id + type -> will get target list linked to source
* - target id + type -> will get source list linked to target
* - source id + type + target type -> will get target list of the type
* - target id + type + target source -> will get source list of the type
2016-06-30 17:28:13 +02:00
*
2015-10-20 02:34:53 +02:00
* @ param int $sourceid Object source id ( if not defined , id of object )
* @ param string $sourcetype Object source type ( if not defined , element name of object )
* @ param int $targetid Object target id ( if not defined , id of object )
* @ param string $targettype Object target type ( if not defined , elemennt name of object )
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
2015-10-20 02:34:53 +02:00
* @ param int $alsosametype 0 = Return only links to object that differs from source . 1 = Include also link to objects of same type .
2011-11-10 10:13:44 +01:00
* @ return void
2014-10-17 14:45:12 +02:00
* @ see add_object_linked , updateObjectLinked , deleteObjectLinked
2011-11-07 15:04:02 +01:00
*/
2015-05-23 23:17:39 +02:00
function fetchObjectLinked ( $sourceid = null , $sourcetype = '' , $targetid = null , $targettype = '' , $clause = 'OR' , $alsosametype = 1 )
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 ))
{
2015-10-20 02:34:53 +02:00
$justsource = true ; // the source (id and type) is a search criteria
2012-07-08 12:18:29 +02:00
if ( ! empty ( $targettype )) $withtargettype = true ;
}
if ( ! empty ( $targetid ) && ! empty ( $targettype ) && empty ( $sourceid ))
{
2015-10-20 02:34:53 +02:00
$justtarget = true ; // the target (id and type) is a search criteria
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
2015-08-27 17:23:23 +02:00
/* if ( empty ( $sourceid ) && empty ( $targetid ))
2014-10-17 14:45:12 +02:00
{
2015-05-23 23:17:39 +02:00
dol_syslog ( 'Bad usage of function. No source nor target id defined (nor as parameter nor as object id)' , LOG_ERR );
2014-10-17 14:45:12 +02:00
return - 1 ;
2015-08-27 17:23:23 +02:00
} */
2014-10-17 14:45:12 +02:00
2015-04-08 21:34:04 +02:00
// Links between objects are stored in table element_element
2015-07-28 14:13:16 +02:00
$sql = 'SELECT rowid, fk_source, sourcetype, fk_target, targettype' ;
2011-11-07 15:04:02 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'element_element' ;
$sql .= " WHERE " ;
if ( $justsource || $justtarget )
{
2012-07-08 12:18:29 +02:00
if ( $justsource )
{
2016-06-07 18:08:33 +02:00
$sql .= " fk_source = " . $sourceid . " AND sourcetype = ' " . $sourcetype . " ' " ;
2012-07-08 12:18:29 +02:00
if ( $withtargettype ) $sql .= " AND targettype = ' " . $targettype . " ' " ;
}
else if ( $justtarget )
{
2016-06-07 18:08:33 +02:00
$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
2014-12-23 12:59:34 +01:00
{
2016-06-07 18:08:33 +02:00
$sql .= " (fk_source = " . $sourceid . " AND sourcetype = ' " . $sourcetype . " ') " ;
$sql .= " " . $clause . " (fk_target = " . $targetid . " AND targettype = ' " . $targettype . " ') " ;
2011-11-07 15:04:02 +01:00
}
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 );
2015-10-20 02:34:53 +02:00
if ( $justsource || $justtarget )
2011-11-07 15:04:02 +01:00
{
2015-10-20 02:34:53 +02:00
if ( $justsource )
{
$this -> linkedObjectsIds [ $obj -> targettype ][ $obj -> rowid ] = $obj -> fk_target ;
}
else if ( $justtarget )
{
$this -> linkedObjectsIds [ $obj -> sourcetype ][ $obj -> rowid ] = $obj -> fk_source ;
}
2011-11-07 15:04:02 +01:00
}
2015-10-20 02:34:53 +02:00
else
2011-11-07 15:04:02 +01:00
{
2015-10-20 02:34:53 +02:00
if ( $obj -> fk_source == $sourceid && $obj -> sourcetype == $sourcetype )
{
$this -> linkedObjectsIds [ $obj -> targettype ][ $obj -> rowid ] = $obj -> fk_target ;
}
if ( $obj -> fk_target == $targetid && $obj -> targettype == $targettype )
{
$this -> linkedObjectsIds [ $obj -> sourcetype ][ $obj -> rowid ] = $obj -> fk_source ;
}
2011-11-07 15:04:02 +01:00
}
$i ++ ;
}
2012-02-21 10:47:08 +01:00
2011-11-07 15:04:02 +01:00
if ( ! empty ( $this -> linkedObjectsIds ))
{
2017-01-21 17:30:35 +01:00
foreach ( $this -> linkedObjectsIds as $objecttype => $objectids ) // $objecttype is a module name ('facture', 'mymodule', ...) or a module name with a suffix ('project_task', 'mymodule_myobj', ...)
2011-11-07 15:04:02 +01:00
{
2017-01-21 17:30:35 +01:00
// Parse element/subelement (ex: project_task, cabinetmed_consultation, ...)
2011-11-07 15:04:02 +01:00
$module = $element = $subelement = $objecttype ;
2016-06-30 17:28:13 +02:00
if ( $objecttype != 'supplier_proposal' && $objecttype != 'order_supplier' && $objecttype != 'invoice_supplier'
2016-01-25 12:33:38 +01:00
&& 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' ;
2015-04-08 21:34:04 +02:00
// To work with non standard classpath or module name
2011-11-07 15:04:02 +01:00
if ( $objecttype == 'facture' ) {
$classpath = 'compta/facture/class' ;
}
2016-03-03 13:38:16 +01:00
else if ( $objecttype == 'facturerec' ) {
$classpath = 'compta/facture/class' ; $module = 'facture' ;
}
2011-11-10 10:13:44 +01:00
else if ( $objecttype == 'propal' ) {
2011-11-07 15:04:02 +01:00
$classpath = 'comm/propal/class' ;
}
2015-11-15 18:23:42 +01:00
else if ( $objecttype == 'supplier_proposal' ) {
$classpath = 'supplier_proposal/class' ;
2015-01-28 14:34:27 +01:00
}
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' ;
}
2016-06-24 00:45:35 +02:00
else if ( $objecttype == 'subscription' ) {
$classpath = 'adherents/class' ; $module = 'adherent' ;
2011-11-07 15:04:02 +01:00
}
2016-06-30 17:28:13 +02:00
2016-03-03 13:38:16 +01:00
// Set classfile
2011-11-07 15:04:02 +01:00
$classfile = strtolower ( $subelement ); $classname = ucfirst ( $subelement );
2016-06-30 17:28:13 +02:00
2016-08-04 11:57:37 +02:00
if ( $objecttype == 'order' ) {
$classfile = 'commande' ; $classname = 'Commande' ;
}
else if ( $objecttype == 'invoice_supplier' ) {
2011-11-07 15:04:02 +01:00
$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' ;
}
2016-01-25 12:33:38 +01:00
else if ( $objecttype == 'supplier_proposal' ) {
$classfile = 'supplier_proposal' ; $classname = 'SupplierProposal' ;
}
2016-03-03 13:38:16 +01:00
else if ( $objecttype == 'facturerec' ) {
$classfile = 'facture-rec' ; $classname = 'FactureRec' ;
}
2016-06-24 00:45:35 +02:00
else if ( $objecttype == 'subscription' ) {
2016-09-24 06:48:40 +02:00
$classfile = 'subscription' ; $classname = 'Subscription' ;
2016-06-24 00:45:35 +02:00
}
2016-06-30 17:28:13 +02:00
2015-07-28 14:13:16 +02:00
// Here $module, $classfile and $classname are set
2015-04-08 21:34:04 +02:00
if ( $conf -> $module -> enabled && (( $element != $this -> element ) || $alsosametype ))
2011-11-07 15:04:02 +01:00
{
dol_include_once ( '/' . $classpath . '/' . $classfile . '.class.php' );
2017-01-21 17:30:35 +01:00
//print '/'.$classpath.'/'.$classfile.'.class.php '.class_exists($classname);
2016-08-04 11:57:37 +02:00
if ( class_exists ( $classname ))
2011-11-07 15:04:02 +01:00
{
2016-08-04 11:57:37 +02:00
foreach ( $objectids as $i => $objectid ) // $i is rowid into llx_element_element
{
$object = new $classname ( $this -> db );
$ret = $object -> fetch ( $objectid );
if ( $ret >= 0 )
{
$this -> linkedObjects [ $objecttype ][ $i ] = $object ;
}
}
2011-11-07 15:04:02 +01:00
}
}
}
}
}
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
2014-10-17 14:45:12 +02:00
* @ see add_object_linked , fetObjectLinked , deleteObjectLinked
2012-02-07 18:33:27 +01:00
*/
2015-05-23 23:17:39 +02:00
function updateObjectLinked ( $sourceid = null , $sourcetype = '' , $targetid = null , $targettype = '' )
2012-02-07 18:33:27 +01:00
{
$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
2015-07-28 14:13:16 +02:00
* @ param int $rowid Row id of line to delete . If defined , other parameters are not used .
* @ return int > 0 if OK , < 0 if KO
2014-10-17 14:45:12 +02:00
* @ see add_object_linked , updateObjectLinked , fetchObjectLinked
2012-02-07 16:47:46 +01:00
*/
2015-07-28 14:13:16 +02:00
function deleteObjectLinked ( $sourceid = null , $sourcetype = '' , $targetid = null , $targettype = '' , $rowid = '' )
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 " ;
2015-07-28 14:13:16 +02:00
if ( $rowid > 0 )
2012-07-05 14:31:13 +02:00
{
2015-07-28 14:13:16 +02:00
$sql .= " rowid = " . $rowid ;
2012-07-05 14:31:13 +02:00
}
else
{
2015-07-28 14:13:16 +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-07-05 14:31:13 +02:00
}
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 ();
2015-07-28 14:13:16 +02:00
$this -> errors [] = $this -> error ;
2012-02-07 16:47:46 +01:00
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 )
2015-02-21 12:57:17 +01:00
* @ param string $elementType Type of element to force ( use this -> table_element by default )
2012-02-01 13:21:39 +01:00
* @ return int < 0 if KO , > 0 if OK
2011-11-07 15:04:02 +01:00
*/
2015-05-23 23:17:39 +02:00
function setStatut ( $status , $elementId = null , $elementType = '' )
2011-11-07 15:04:02 +01:00
{
2014-09-22 20:16:58 +02:00
global $user , $langs , $conf ;
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 " ;
2017-01-29 15:31:22 +01:00
if ( $elementTable == 'mailing' ) $fieldstatus = " statut " ;
2012-05-05 20:22:46 +02:00
if ( $elementTable == 'user' ) $fieldstatus = " statut " ;
2015-03-08 08:21:49 +01:00
if ( $elementTable == 'expensereport' ) $fieldstatus = " fk_statut " ;
2015-03-08 02:08:26 +01:00
if ( $elementTable == 'commande_fournisseur_dispatch' ) $fieldstatus = " status " ;
2012-05-05 20:22:46 +02:00
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 ;
2015-02-21 12:57:17 +01:00
// If status = 1 = validated, update also fk_user_valid
if ( $status == 1 && $elementTable == 'expensereport' ) $sql .= " , fk_user_valid = " . $user -> id ;
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 ))
{
2014-12-28 20:12:32 +01:00
$error = 0 ;
2014-09-22 20:16:58 +02:00
2014-12-28 20:12:32 +01:00
$trigkey = '' ;
2016-01-25 12:33:38 +01:00
if ( $this -> element == 'supplier_proposal' && $status == 2 ) $trigkey = 'SUPPLIER_PROPOSAL_CLOSE' ;
2016-09-19 13:57:50 +02:00
if ( $this -> element == 'fichinter' && $status == 3 ) $trigkey = 'FICHINTER_CLASSIFY_DONE' ;
2014-12-28 20:12:32 +01:00
if ( $this -> element == 'fichinter' && $status == 2 ) $trigkey = 'FICHINTER_CLASSIFY_BILLED' ;
if ( $this -> element == 'fichinter' && $status == 1 ) $trigkey = 'FICHINTER_CLASSIFY_UNBILLED' ;
if ( $trigkey )
{
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( $trigkey , $this , $user , $langs , $conf );
if ( $result < 0 ) {
$error ++ ; $this -> errors = $interface -> errors ;
}
// Fin appel triggers
}
2014-09-22 20:16:58 +02:00
if ( ! $error )
{
$this -> db -> commit ();
2016-10-30 16:34:46 +01:00
if ( empty ( $elementId )) // If the element we update was $this (so $elementId is null)
{
$this -> statut = $status ;
$this -> status = $status ;
}
2014-09-22 20:16:58 +02:00
return 1 ;
}
else
{
$this -> db -> rollback ();
dol_syslog ( get_class ( $this ) . " ::setStatus " . $this -> error , LOG_ERR );
return - 1 ;
}
2012-04-28 18:24:52 +02:00
}
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 ;
2014-09-13 19:04:25 +02:00
if ( ! empty ( $ref )) $sql .= " AND ref = ' " . $this -> db -> escape ( $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 -> 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
2011-06-22 12:58:22 +02:00
/**
2014-09-23 19:37: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
*
2016-11-19 02:16:12 +01:00
* @ param int $id Force id of object
2014-09-23 19:37:59 +02:00
* @ return int < 0 if KO , 0 if not used , > 0 if already used
2011-06-22 12:58:22 +02:00
*/
2016-11-19 02:16:12 +01:00
function isObjectUsed ( $id = 0 )
2011-06-22 12:58:22 +02:00
{
2016-11-19 02:16:12 +01:00
if ( empty ( $id )) $id = $this -> id ;
2014-09-23 19:37:59 +02:00
// Check parameters
if ( ! isset ( $this -> childtables ) || ! is_array ( $this -> childtables ) || count ( $this -> childtables ) == 0 )
2011-06-22 12:58:22 +02:00
{
2014-09-23 19:37:59 +02:00
dol_print_error ( 'Called isObjectUsed on a class with property this->childtables not defined' );
return - 1 ;
2011-06-22 12:58:22 +02:00
}
2010-09-02 23:08:29 +02:00
2014-09-23 19:37:59 +02:00
// Test if child exists
$haschild = 0 ;
foreach ( $this -> childtables as $table )
2011-06-22 12:58:22 +02:00
{
2014-09-23 19:37:59 +02:00
// Check if third party can be deleted
$sql = " SELECT COUNT(*) as nb from " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE " . $this -> fk_element . " = " . $id ;
2011-09-20 11:40:27 +02:00
$resql = $this -> db -> query ( $sql );
2011-06-22 12:58:22 +02:00
if ( $resql )
{
2014-09-23 19:37:59 +02:00
$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
2011-06-22 12:58:22 +02:00
}
else
{
2014-09-23 19:37:59 +02:00
$this -> error = $this -> db -> lasterror ();
2013-12-13 18:33:05 +01:00
return - 1 ;
2011-06-22 12:58:22 +02:00
}
}
2014-09-23 19:37:59 +02:00
if ( $haschild > 0 )
{
$this -> error = " ErrorRecordHasChildren " ;
return $haschild ;
}
else 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
/**
2014-09-23 19:37:59 +02:00
* Function to say how many lines object contains
2012-12-10 19:06:59 +01:00
*
2014-09-23 19:37:59 +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
* @ return int < 0 if KO , 0 if no predefined products , nb of lines with predefined products if found
2012-12-08 12:49:20 +01:00
*/
2014-09-23 19:37:59 +02:00
function hasProductsOrServices ( $predefined =- 1 )
{
$nb = 0 ;
2012-12-10 19:06:59 +01:00
2014-09-23 19:37:59 +02:00
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 ;
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 ;
if ( $qualified ) $nb ++ ;
}
dol_syslog ( get_class ( $this ) . '::hasProductsOrServices we found ' . $nb . ' qualified lines of products/servcies' );
return $nb ;
}
2011-06-22 12:58:22 +02:00
2011-09-13 14:12:48 +02:00
/**
2014-09-23 19:37:59 +02:00
* Function that returns the total amount HT of discounts applied for all lines .
2012-02-01 13:21:39 +01:00
*
2014-09-23 19:37:59 +02:00
* @ return float
2011-11-07 15:04:02 +01:00
*/
2014-09-23 19:37:59 +02:00
function getTotalDiscount ()
2011-11-07 15:04:02 +01:00
{
2014-09-23 19:37:59 +02:00
$total_discount = 0.00 ;
2012-02-13 01:43:35 +01:00
2014-09-23 19:37:59 +02: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 ;
2014-01-04 12:29:20 +01:00
2014-09-23 19:37:59 +02:00
dol_syslog ( get_class ( $this ) . '::getTotalDiscount' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
2011-09-13 14:12:48 +02:00
{
2014-09-23 19:37:59 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
2012-02-01 20:44:06 +01:00
2014-09-23 19:37:59 +02:00
$pu_ht = $obj -> pu_ht ;
$qty = $obj -> qty ;
$total_ht = $obj -> total_ht ;
2011-09-13 14:12:48 +02:00
2015-05-23 23:17:39 +02:00
$total_discount_line = floatval ( price2num (( $pu_ht * $qty ) - $total_ht , 'MT' ));
2014-09-23 19:37:59 +02:00
$total_discount += $total_discount_line ;
2011-09-13 14:12:48 +02:00
2014-09-23 19:37:59 +02:00
$i ++ ;
}
2011-09-13 14:12:48 +02:00
}
2014-09-23 19:37:59 +02:00
//print $total_discount; exit;
return price2num ( $total_discount );
2011-11-07 15:04:02 +01:00
}
2013-04-07 17:39:08 +02:00
2016-06-30 17:28:13 +02:00
2016-04-01 18:44:23 +02:00
/**
* Return into unit = 0 , the calculated total of weight and volume of all lines * qty
* Calculate by adding weight and volume of each product line , so properties -> volume / volume_units / weight / weight_units must be loaded on line .
*
* @ return array array ( 'weight' =>... , 'volume' =>... )
*/
function getTotalWeightVolume ()
{
$weightUnit = 0 ;
$volumeUnit = 0 ;
$totalWeight = '' ;
$totalVolume = '' ;
$totalOrdered = '' ; // defined for shipment only
$totalToShip = '' ; // defined for shipment only
2016-06-30 17:28:13 +02:00
2016-04-01 18:44:23 +02:00
foreach ( $this -> lines as $line )
{
2017-01-16 01:21:33 +01:00
if ( isset ( $line -> qty_asked ))
{
if ( empty ( $totalOrdered )) $totalOrdered = 0 ; // Avoid warning because $totalOrdered is ''
$totalOrdered += $line -> qty_asked ; // defined for shipment only
}
if ( isset ( $line -> qty_shipped ))
{
if ( empty ( $totalToShip )) $totalToShip = 0 ; // Avoid warning because $totalToShip is ''
$totalToShip += $line -> qty_shipped ; // defined for shipment only
}
2016-06-30 17:28:13 +02:00
2016-04-01 18:44:23 +02:00
// Define qty, weight, volume, weight_units, volume_units
if ( $this -> element == 'shipping' ) $qty = $line -> qty_shipped ; // for shipments
else $qty = $line -> qty ;
$weight = $line -> weight ;
$volume = $line -> volume ;
$weight_units = $line -> weight_units ;
$volume_units = $line -> volume_units ;
2016-06-30 17:28:13 +02:00
2016-04-01 18:44:23 +02:00
$weightUnit = 0 ;
$volumeUnit = 0 ;
if ( ! empty ( $weight_units )) $weightUnit = $weight_units ;
if ( ! empty ( $volume_units )) $volumeUnit = $volume_units ;
2016-06-30 17:28:13 +02:00
2017-01-16 01:21:33 +01:00
if ( empty ( $totalWeight )) $totalWeight = 0 ; // Avoid warning because $totalWeight is ''
if ( empty ( $totalVolume )) $totalVolume = 0 ; // Avoid warning because $totalVolume is ''
2016-04-01 18:44:23 +02:00
//var_dump($line->volume_units);
2017-01-16 01:21:33 +01:00
if ( $weight_units < 50 ) // >50 means a standard unit (power of 10 of official unit), > 50 means an exotic unit (like inch)
2016-04-01 18:44:23 +02:00
{
$trueWeightUnit = pow ( 10 , $weightUnit );
$totalWeight += $weight * $qty * $trueWeightUnit ;
}
else
{
$totalWeight += $weight * $qty ; // This may be wrong if we mix different units
}
2017-01-16 01:21:33 +01:00
if ( $volume_units < 50 ) // >50 means a standard unit (power of 10 of official unit), > 50 means an exotic unit (like inch)
2016-04-01 18:44:23 +02:00
{
//print $line->volume."x".$line->volume_units."x".($line->volume_units < 50)."x".$volumeUnit;
$trueVolumeUnit = pow ( 10 , $volumeUnit );
//print $line->volume;
$totalVolume += $volume * $qty * $trueVolumeUnit ;
}
else
{
$totalVolume += $volume * $qty ; // This may be wrong if we mix different units
}
}
2016-06-30 17:28:13 +02:00
2016-04-01 18:44:23 +02:00
return array ( 'weight' => $totalWeight , 'volume' => $totalVolume , 'ordered' => $totalOrdered , 'toship' => $totalToShip );
2016-06-30 17:28:13 +02:00
}
2014-09-23 19:37:59 +02:00
/**
* Set extra parameters
2013-04-07 17:39:08 +02:00
*
2016-04-09 15:07:55 +02:00
* @ return int < 0 if KO , > 0 if OK
2013-03-30 17:43:21 +01:00
*/
2014-09-23 19:37:59 +02:00
function setExtraParameters ()
2013-03-30 17:43:21 +01:00
{
2014-09-23 19:37:59 +02:00
$this -> db -> begin ();
2013-04-07 17:39:08 +02:00
2014-09-23 19:37:59 +02:00
$extraparams = ( ! empty ( $this -> extraparams ) ? json_encode ( $this -> extraparams ) : null );
2013-04-07 17:39:08 +02:00
2014-09-23 19:37:59 +02: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
2015-02-10 11:46:09 +01:00
/**
* Return incoterms informations
2016-10-02 16:50:28 +02:00
* TODO Use a cache for label get
2015-02-10 11:46:09 +01:00
*
2015-02-23 10:36:44 +01:00
* @ return string incoterms info
2015-02-10 11:46:09 +01:00
*/
function display_incoterms ()
{
$out = '' ;
2015-05-23 23:17:39 +02:00
$this -> libelle_incoterms = '' ;
2015-02-10 11:46:09 +01:00
if ( ! empty ( $this -> fk_incoterms ))
{
$sql = 'SELECT code FROM ' . MAIN_DB_PREFIX . 'c_incoterms WHERE rowid = ' . ( int ) $this -> fk_incoterms ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
$res = $this -> db -> fetch_object ( $result );
$out .= $res -> code ;
}
2015-03-06 01:53:02 +01:00
}
2016-10-02 16:50:28 +02:00
$out .= (( $res -> code && $this -> location_incoterms ) ? ' - ' : '' ) . $this -> location_incoterms ;
2015-03-06 01:53:02 +01:00
2015-02-10 11:46:09 +01:00
return $out ;
}
2015-03-06 01:53:02 +01:00
2015-02-23 10:36:44 +01:00
/**
* Return incoterms informations for pdf display
*
* @ return string incoterms info
*/
function getIncotermsForPDF ()
{
2015-02-23 11:19:00 +01:00
$sql = 'SELECT code FROM ' . MAIN_DB_PREFIX . 'c_incoterms WHERE rowid = ' . ( int ) $this -> fk_incoterms ;
2015-02-23 10:36:44 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$res = $this -> db -> fetch_object ( $resql );
return 'Incoterm : ' . $res -> code . ' - ' . $this -> location_incoterms ;
}
2015-03-06 01:53:02 +01:00
else
2015-02-23 10:36:44 +01:00
{
2015-11-09 18:13:17 +01:00
$this -> errors [] = $this -> db -> lasterror ();
2015-03-06 01:53:02 +01:00
return false ;
2015-02-23 10:36:44 +01:00
}
}
2015-03-06 01:53:02 +01:00
2015-02-10 10:23:38 +01:00
/**
* Define incoterms values of current object
*
* @ param int $id_incoterm Id of incoterm to set or '' to remove
* @ param string $location location of incoterm
2015-11-11 17:19:53 +01:00
* @ return int < 0 if KO , > 0 if OK
2015-02-10 10:23:38 +01:00
*/
function setIncoterms ( $id_incoterm , $location )
{
if ( $this -> id && $this -> table_element )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET fk_incoterms = " . ( $id_incoterm > 0 ? $id_incoterm : " null " );
2016-03-25 17:55:58 +01:00
$sql .= " , location_incoterms = " . ( $id_incoterm > 0 ? " ' " . $this -> db -> escape ( $location ) . " ' " : " null " );
2015-02-10 10:23:38 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
dol_syslog ( get_class ( $this ) . '::setIncoterms' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> fk_incoterms = $id_incoterm ;
$this -> location_incoterms = $location ;
2015-03-06 01:53:02 +01:00
2015-02-10 11:46:09 +01:00
$sql = 'SELECT libelle FROM ' . MAIN_DB_PREFIX . 'c_incoterms WHERE rowid = ' . ( int ) $this -> fk_incoterms ;
$res = $this -> db -> query ( $sql );
if ( $res )
{
$obj = $this -> db -> fetch_object ( $res );
$this -> libelle_incoterms = $obj -> libelle ;
2015-03-06 01:53:02 +01:00
}
2015-02-10 10:23:38 +01:00
return 1 ;
}
else
{
2015-11-09 18:13:17 +01:00
$this -> errors [] = $this -> db -> lasterror ();
2015-02-10 10:23:38 +01:00
return - 1 ;
}
}
else return - 1 ;
}
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 )
2016-01-10 13:05:22 +01:00
* TODO Add a field into dictionary
2012-04-08 13:05:58 +02:00
*
* @ 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
2016-01-10 13:05:22 +01:00
'HR' , // Croatia
'NL' , // Holland
2012-04-08 13:05:58 +02:00
'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
2016-01-10 13:05:22 +01:00
//'NO', // Norway
2012-04-08 13:05:58 +02:00
'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 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
2015-05-23 23:17:39 +02:00
* @ param string $seller Object of seller third party
* @ param string $buyer Object of buyer third party
2015-05-25 18:43:46 +02:00
* @ param int $selected Object line selected
2012-04-06 11:38:13 +02:00
* @ 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
{
2016-04-26 22:00:41 +02:00
global $conf , $hookmanager , $langs , $user ;
2016-04-27 09:41:42 +02:00
// TODO We should not use global var for this !
2016-10-24 10:18:24 +02:00
global $inputalsopricewithtax , $usemargins , $disableedit , $disablemove , $disableremove , $outputalsopricetotalwithtax ;
2015-02-27 18:05:12 +01:00
2015-03-04 02:28:16 +01:00
// Define usemargins
2015-02-27 18:05:12 +01:00
$usemargins = 0 ;
if ( ! empty ( $conf -> margin -> enabled ) && ! empty ( $this -> element ) && in_array ( $this -> element , array ( 'facture' , 'propal' , 'commande' ))) $usemargins = 1 ;
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
2015-10-20 16:08:25 +02:00
if ( ! empty ( $conf -> global -> MAIN_VIEW_LINE_NUMBER )) print '<td class="linecolnum" align="center" width="5"> </td>' ;
2013-04-16 01:13:20 +02:00
// Description
2015-10-20 16:08:25 +02:00
print '<td class="linecoldescription">' . $langs -> trans ( 'Description' ) . '</td>' ;
2013-04-16 01:13:20 +02:00
2015-11-15 18:23:42 +01:00
if ( $this -> element == 'supplier_proposal' )
2015-03-04 17:54:43 +01:00
{
2015-11-15 18:23:42 +01:00
print '<td class="linerefsupplier" align="right"><span id="title_fourn_ref">' . $langs -> trans ( " SupplierProposalRefFourn " ) . '</span></td>' ;
2015-03-04 17:54:43 +01:00
}
2013-04-16 01:13:20 +02:00
// VAT
2016-12-04 13:30:17 +01:00
print '<td class="linecolvat" align="right" width="80">' . $langs -> trans ( 'VAT' ) . '</td>' ;
2013-04-16 01:13:20 +02:00
// Price HT
2015-10-20 16:08:25 +02:00
print '<td class="linecoluht" align="right" width="80">' . $langs -> trans ( 'PriceUHT' ) . '</td>' ;
2013-04-16 01:13:20 +02:00
2016-01-18 19:45:27 +01:00
// Multicurrency
2016-12-10 22:07:25 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled )) print '<td class="linecoluht_currency" align="right" width="80">' . $langs -> trans ( 'PriceUHTCurrency' , $this -> multicurrency_code ) . '</td>' ;
2016-06-30 17:28:13 +02:00
2015-02-28 18:10:31 +01:00
if ( $inputalsopricewithtax ) print '<td align="right" width="80">' . $langs -> trans ( 'PriceUTTC' ) . '</td>' ;
2013-04-16 01:13:20 +02:00
// Qty
2016-04-07 13:06:22 +02:00
print '<td class="linecolqty" align="right">' . $langs -> trans ( 'Qty' ) . '</td>' ;
2013-04-16 01:13:20 +02:00
2015-02-26 14:15:33 +01:00
if ( $conf -> global -> PRODUCT_USE_UNITS )
{
2016-04-07 13:06:22 +02:00
print '<td class="linecoluseunit" align="left">' . $langs -> trans ( 'Unit' ) . '</td>' ;
2015-02-26 14:15:33 +01:00
}
2013-04-16 01:13:20 +02:00
// Reduction short
2016-04-07 13:06:22 +02:00
print '<td class="linecoldiscount" align="right">' . $langs -> trans ( 'ReductionShort' ) . '</td>' ;
2013-04-16 01:13:20 +02:00
2014-11-14 16:43:49 +01:00
if ( $this -> situation_cycle_ref ) {
2016-04-07 13:06:22 +02:00
print '<td class="linecolcycleref" align="right">' . $langs -> trans ( 'Progress' ) . '</td>' ;
2014-11-14 16:43:49 +01:00
}
2013-04-16 01:13:20 +02:00
2015-02-28 18:10:31 +01:00
if ( $usemargins && ! empty ( $conf -> margin -> enabled ) && empty ( $user -> societe_id ))
2013-05-20 17:37:08 +02:00
{
2016-12-15 12:01:46 +01:00
if ( ! empty ( $user -> rights -> margins -> creer ))
{
if ( $conf -> global -> MARGIN_TYPE == " 1 " )
print '<td class="linecolmargin1 margininfos" align="right" width="80">' . $langs -> trans ( 'BuyingPrice' ) . '</td>' ;
else
print '<td class="linecolmargin1 margininfos" align="right" width="80">' . $langs -> trans ( 'CostPrice' ) . '</td>' ;
}
2013-08-20 12:11:19 +02:00
if ( ! empty ( $conf -> global -> DISPLAY_MARGIN_RATES ) && $user -> rights -> margins -> liretous )
2015-10-20 16:08:25 +02:00
print '<td class="linecolmargin2 margininfos" 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 )
2015-10-20 16:08:25 +02:00
print '<td class="linecolmargin2 margininfos" align="right" width="50">' . $langs -> trans ( 'MarkRate' ) . '</td>' ;
2012-08-01 17:36:15 +02:00
}
2013-04-16 01:13:20 +02:00
// Total HT
2016-04-07 13:06:22 +02:00
print '<td class="linecolht" align="right">' . $langs -> trans ( 'TotalHTShort' ) . '</td>' ;
2013-04-16 01:13:20 +02:00
2016-01-18 19:45:27 +01:00
// Multicurrency
2016-12-10 22:07:25 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled )) print '<td class="linecoltotalht_currency" align="right">' . $langs -> trans ( 'TotalHTShortCurrency' , $this -> multicurrency_code ) . '</td>' ;
2016-01-18 19:45:27 +01:00
2016-10-24 10:18:24 +02:00
if ( $outputalsopricetotalwithtax ) print '<td align="right" width="80">' . $langs -> trans ( 'TotalTTCShort' ) . '</td>' ;
2016-10-12 12:30:22 +02:00
2015-10-20 16:08:25 +02:00
print '<td class="linecoledit"></td>' ; // No width to allow autodim
2013-04-16 01:13:20 +02:00
2015-10-20 16:08:25 +02:00
print '<td class="linecoldelete" width="10"></td>' ;
2013-04-16 01:13:20 +02:00
2015-10-20 16:08:25 +02:00
print '<td class="linecolmove" 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
2015-10-26 14:00:43 +01:00
//if (is_object($hookmanager) && (($line->product_type == 9 && ! empty($line->special_code)) || ! empty($line->fk_parent_line)))
if ( is_object ( $hookmanager )) // Old code is commented on preceding line.
2012-04-06 11:38:13 +02:00
{
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 );
2015-10-26 14:00:43 +01:00
$reshook = $hookmanager -> executeHooks ( 'printObjectLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2014-10-20 13:15:48 +02:00
}
2014-11-03 17:23:02 +01:00
else
2014-10-20 13:15:48 +02:00
{
$parameters = array ( 'line' => $line , 'var' => $var , 'num' => $num , 'i' => $i , 'dateSelector' => $dateSelector , 'seller' => $seller , 'buyer' => $buyer , 'selected' => $selected , 'extrafieldsline' => $extrafieldsline );
2015-10-26 14:00:43 +01:00
$reshook = $hookmanager -> executeHooks ( 'printObjectSubLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2012-04-06 11:38:13 +02:00
}
}
2015-10-26 14:00:43 +01:00
if ( empty ( $reshook ))
2012-04-06 11:38:13 +02:00
{
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 )
*
2015-05-23 23:17:39 +02:00
* @ param string $action GET / POST action
* @ param CommonObjectLine $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 int $selected Object line selected
* @ param int $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 ;
2016-04-24 18:55:19 +02:00
global $form , $bc , $bcdd ;
global $object_rights , $disableedit , $disablemove ; // TODO We should not use global var for this !
2015-03-23 01:39:12 +01:00
$object_rights = $this -> getRights ();
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 );
2015-03-11 14:55:45 +01:00
$product_static -> fetch ( $line -> fk_product );
2015-10-26 14:00:43 +01:00
$product_static -> ref = $line -> ref ; //can change ref in hook
$product_static -> label = $line -> label ; //can change label in hook
2013-03-30 14:27:13 +01:00
$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 );
2015-03-11 14:55:45 +01:00
$description .= ( ! empty ( $conf -> global -> PRODUIT_DESC_IN_FORM ) ? '' : dol_htmlentitiesbr ( $line -> description )); // Description is what to show on popup. We shown nothing if already into desc.
2012-04-06 11:38:13 +02:00
}
2016-06-30 17:28:13 +02:00
2016-02-05 13:49:19 +01:00
$line -> pu_ttc = price2num ( $line -> subprice * ( 1 + ( $line -> tva_tx / 100 )), 'MU' );
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 " ) . '"' ;
2016-02-05 13:49:19 +01:00
$line -> pu_ttc = price2num ( $line -> subprice * ( 1 + ( $line -> tva_tx / 100 )), 'MU' );
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_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
{
2015-02-26 14:15:33 +01:00
global $langs , $hookmanager , $conf ;
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>' ;
2016-01-23 00:38:17 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled )) print '<td align="right">' . $langs -> trans ( 'PriceUHTCurrency' ) . '</td>' ;
2011-11-07 15:04:02 +01:00
print '<td align="right">' . $langs -> trans ( 'Qty' ) . '</td>' ;
2015-02-26 14:15:33 +01:00
if ( $conf -> global -> PRODUCT_USE_UNITS )
{
print '<td align="left">' . $langs -> trans ( 'Unit' ) . '</td>' ;
}
2011-11-07 15:04:02 +01:00
print '<td align="right">' . $langs -> trans ( 'ReductionShort' ) . '</td></tr>' ;
$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 = '' ;
2015-05-23 23:17:39 +02:00
$hookmanager -> executeHooks ( 'printOriginObjectLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2011-11-07 15:04:02 +01:00
}
}
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
*
2015-05-23 23:17:39 +02:00
* @ param CommonObjectLine $line Line
* @ param string $var Var
2012-02-01 13:21:39 +01:00
* @ 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
{
2015-05-23 23:17:39 +02:00
global $langs , $conf ;
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 );
2016-01-23 00:38:17 +01:00
$this -> tpl [ 'multicurrency_price' ] = price ( $line -> multicurrency_subprice );
2011-11-07 15:04:02 +01:00
$this -> tpl [ 'qty' ] = (( $line -> info_bits & 2 ) != 2 ) ? $line -> qty : ' ' ;
2015-04-21 15:49:58 +02:00
if ( $conf -> global -> PRODUCT_USE_UNITS ) $this -> tpl [ 'unit' ] = $line -> getLabelOfUnit ( 'long' );
2011-11-07 15:04:02 +01:00
$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
/**
2014-05-12 13:25:36 +02:00
* Show the array with all margin infos
2013-04-19 13:30:08 +02:00
*
2015-05-23 23:17:39 +02:00
* @ param bool $force_price Force price
2015-06-06 15:19:25 +02:00
* @ return void
* @ deprecated 3.8 Load FormMargin class and make a direct call to displayMarginInfos
2013-04-19 13:30:08 +02:00
*/
2013-05-20 17:37:08 +02:00
function displayMarginInfos ( $force_price = false )
{
2015-06-06 15:19:25 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmargin.class.php' ;
$formmargin = new FormMargin ( $this -> db );
$formmargin -> displayMarginInfos ( $this , $force_price );
2013-04-19 13:30:08 +02:00
}
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
2015-05-25 18:43:46 +02:00
* 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
2015-08-14 17:35:05 +02:00
* @ param string $resource_type 'resource'
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
*/
2015-08-14 17:35:05 +02:00
function add_element_resource ( $resource_id , $resource_type , $busy = 0 , $mandatory = 0 )
2014-03-15 14:54:23 +01:00
{
$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 ;
2015-08-14 17:35:05 +02:00
$sql .= " , ' " . $resource_type . " ' " ;
2014-03-15 14:54:23 +01:00
$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 )
{
2015-05-23 23:17:39 +02:00
global $user ;
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 " ;
2015-04-18 18:07:59 +02:00
$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 );
2016-10-02 20:15:11 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
else
{
if ( ! $notrigger )
2015-04-18 18:07:59 +02:00
{
2016-10-02 20:15:11 +02:00
$result = $this -> call_trigger ( strtoupper ( $element ) . '_DELETE_RESOURCE' , $user );
if ( $result < 0 ) { $this -> db -> rollback (); return - 1 ; }
2015-04-18 18:07:59 +02:00
}
2016-10-02 20:15:11 +02:00
$this -> db -> commit ();
return 1 ;
}
2014-05-11 12:26:57 +02:00
}
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
{
2015-05-23 23:17:39 +02:00
$this -> lines [ $i ] = clone $this -> lines [ $i ];
2013-09-04 10:45:15 +02:00
}
}
}
2013-09-18 16:46:54 +02:00
2014-09-23 19:37:59 +02:00
/**
* Common function for all objects extending CommonObject for generating documents
*
2015-05-03 20:07:16 +02:00
* @ param string $modelspath Relative folder where generators are placed
* @ param string $modele Generator to use . Caller must set it to obj -> modelpdf or GETPOST ( 'modelpdf' ) for example .
* @ param Translate $outputlangs Language to use
* @ param int $hidedetails 1 to hide details . 0 by default
* @ param int $hidedesc 1 to hide product description . 0 by default
* @ param int $hideref 1 to hide product reference . 0 by default
2016-10-05 22:24:14 +02:00
* @ param null | array $moreparams Array to provide more information
2015-05-03 20:07:16 +02:00
* @ return int > 0 if OK , < 0 if KO
2014-09-23 19:37:59 +02:00
*/
2016-10-05 22:24:14 +02:00
protected function commonGenerateDocument ( $modelspath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref , $moreparams = null )
2014-09-23 19:37:59 +02:00
{
global $conf , $langs ;
2014-09-21 18:16:14 +02:00
$srctemplatepath = '' ;
// Increase limit for PDF build
$err = error_reporting ();
error_reporting ( 0 );
@ set_time_limit ( 120 );
error_reporting ( $err );
2015-05-03 20:07:16 +02:00
// If selected model is a filename template (then $modele="modelname" or "modelname:filename")
2014-09-21 18:16:14 +02:00
$tmp = explode ( ':' , $modele , 2 );
if ( ! empty ( $tmp [ 1 ]))
{
$modele = $tmp [ 0 ];
$srctemplatepath = $tmp [ 1 ];
}
// Search template files
$file = '' ; $classname = '' ; $filefound = 0 ;
$dirmodels = array ( '/' );
if ( is_array ( $conf -> modules_parts [ 'models' ])) $dirmodels = array_merge ( $dirmodels , $conf -> modules_parts [ 'models' ]);
foreach ( $dirmodels as $reldir )
{
foreach ( array ( 'doc' , 'pdf' ) as $prefix )
{
2016-10-02 20:15:11 +02:00
if ( in_array ( get_class ( $this ), array ( 'Adherent' ))) $file = $prefix . " _ " . $modele . " .class.php " ; // Member module use prefix_module.class.php
else $file = $prefix . " _ " . $modele . " .modules.php " ;
2014-09-21 18:16:14 +02:00
// On verifie l'emplacement du modele
$file = dol_buildpath ( $reldir . $modelspath . $file , 0 );
if ( file_exists ( $file ))
{
$filefound = 1 ;
$classname = $prefix . '_' . $modele ;
break ;
}
}
if ( $filefound ) break ;
}
2015-05-03 20:07:16 +02:00
// If generator was found
2014-09-21 18:16:14 +02:00
if ( $filefound )
{
2016-10-02 20:15:11 +02:00
global $db ; // Required to solve a conception default in commonstickergenerator.class.php making an include of code using $db
2014-09-21 18:16:14 +02:00
require_once $file ;
$obj = new $classname ( $this -> db );
2015-05-03 20:07:16 +02:00
// If generator is ODT, we must have srctemplatepath defined, if not we set it.
if ( $obj -> type == 'odt' && empty ( $srctemplatepath ))
{
$varfortemplatedir = $obj -> scandir ;
if ( $varfortemplatedir && ! empty ( $conf -> global -> $varfortemplatedir ))
{
$dirtoscan = $conf -> global -> $varfortemplatedir ;
$listoffiles = array ();
// Now we add first model found in directories scanned
$listofdir = explode ( ',' , $dirtoscan );
foreach ( $listofdir as $key => $tmpdir )
{
$tmpdir = trim ( $tmpdir );
$tmpdir = preg_replace ( '/DOL_DATA_ROOT/' , DOL_DATA_ROOT , $tmpdir );
if ( ! $tmpdir ) { unset ( $listofdir [ $key ]); continue ; }
if ( is_dir ( $tmpdir ))
{
$tmpfiles = dol_dir_list ( $tmpdir , 'files' , 0 , '\.od(s|t)$' , '' , 'name' , SORT_ASC , 0 );
if ( count ( $tmpfiles )) $listoffiles = array_merge ( $listoffiles , $tmpfiles );
}
}
if ( count ( $listoffiles ))
{
foreach ( $listoffiles as $record )
{
$srctemplatepath = $record [ 'fullname' ];
break ;
}
}
}
if ( empty ( $srctemplatepath ))
{
2015-08-31 13:00:24 +02:00
$this -> error = 'ErrorGenerationAskedForOdtTemplateWithSrcFileNotDefined' ;
2015-05-03 20:07:16 +02:00
return - 1 ;
}
}
2015-08-31 13:00:24 +02:00
if ( $obj -> type == 'odt' && ! empty ( $srctemplatepath ))
{
if ( ! dol_is_file ( $srctemplatepath ))
{
$this -> error = 'ErrorGenerationAskedForOdtTemplateWithSrcFileNotFound' ;
return - 1 ;
}
}
2016-06-30 17:28:13 +02:00
2014-09-21 18:16:14 +02:00
// We save charset_output to restore it because write_file can change it if needed for
// output format that does not support UTF8.
$sav_charset_output = $outputlangs -> charset_output ;
2016-10-05 22:24:14 +02:00
2016-10-02 20:15:11 +02:00
if ( in_array ( get_class ( $this ), array ( 'Adherent' )))
{
2016-10-05 22:24:14 +02:00
$arrayofrecords = array (); // The write_file of templates of adherent class need this
$resultwritefile = $obj -> write_file ( $this , $outputlangs , $srctemplatepath , 'member' , 1 , $moreparams );
2016-10-02 20:15:11 +02:00
}
2016-10-05 22:24:14 +02:00
else $resultwritefile = $obj -> write_file ( $this , $outputlangs , $srctemplatepath , $hidedetails , $hidedesc , $hideref , $moreparams );
2016-10-02 20:15:11 +02:00
if ( $resultwritefile > 0 )
2014-09-21 18:16:14 +02:00
{
$outputlangs -> charset_output = $sav_charset_output ;
// We delete old preview
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
dol_delete_preview ( $this );
// Success in building document. We build meta file.
dol_meta_create ( $this );
return 1 ;
}
else
{
$outputlangs -> charset_output = $sav_charset_output ;
2015-05-03 20:07:16 +02:00
dol_print_error ( $this -> db , " Error generating document for " . __CLASS__ . " . Error: " . $obj -> error , $obj -> errors );
2014-09-21 18:16:14 +02:00
return - 1 ;
}
}
else
{
2015-05-03 20:07:16 +02:00
$this -> error = $langs -> trans ( " Error " ) . " " . $langs -> trans ( " ErrorFileDoesNotExists " , $file );
dol_print_error ( '' , $this -> error );
2014-09-21 18:16:14 +02:00
return - 1 ;
}
}
2015-03-14 14:55:41 +01:00
/**
* Build thumb
*
* @ param string $file Path file in UTF8 to original file to create thumbs from .
* @ return void
*/
2016-04-09 14:12:21 +02:00
function addThumbs ( $file )
2015-03-14 14:55:41 +01:00
{
global $maxwidthsmall , $maxheightsmall , $maxwidthmini , $maxheightmini , $quality ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php' ; // This define also $maxwidthsmall, $quality, ...
$file_osencoded = dol_osencode ( $file );
if ( file_exists ( $file_osencoded ))
{
// Create small thumbs for company (Ratio is near 16/9)
// Used on logon for example
2015-05-23 23:17:39 +02:00
vignette ( $file_osencoded , $maxwidthsmall , $maxheightsmall , '_small' , $quality );
2015-03-14 14:55:41 +01:00
// Create mini thumbs for company (Ratio is near 16/9)
// Used on menu or for setup page for example
2015-05-23 23:17:39 +02:00
vignette ( $file_osencoded , $maxwidthmini , $maxheightmini , '_mini' , $quality );
2015-03-14 14:55:41 +01:00
}
}
2014-09-23 19:37:59 +02:00
/* Functions common to commonobject and commonobjectline */
2015-09-22 13:26:46 +02:00
/* For default values */
/**
2016-06-30 17:28:13 +02:00
* Return the default value to use for a field when showing the create form of object .
2015-09-22 13:26:46 +02:00
* Return values in this order :
* 1 ) If parameter is available into POST , we return it first .
* 2 ) If not but an alternate value was provided as parameter of function , we return it .
2016-06-30 17:28:13 +02:00
* 3 ) If not but a constant $conf -> global -> OBJECTELEMENT_FIELDNAME is set , we return it ( It is better to use the dedicated table ) .
2015-09-22 13:26:46 +02:00
* 4 ) Return value found into database ( TODO No yet implemented )
2016-06-30 17:28:13 +02:00
*
2016-04-09 15:38:15 +02:00
* @ param string $fieldname Name of field
* @ param string $alternatevalue Alternate value to use
* @ return string | string [] Default value ( can be an array if the GETPOST return an array )
2015-09-22 13:26:46 +02:00
**/
function getDefaultCreateValueFor ( $fieldname , $alternatevalue = null )
{
global $conf , $_POST ;
2014-09-23 19:37:59 +02:00
2016-04-09 15:38:15 +02:00
// If param here has been posted, we use this value first.
2015-09-22 13:26:46 +02:00
if ( isset ( $_POST [ $fieldname ])) return GETPOST ( $fieldname , 2 );
2016-06-30 17:28:13 +02:00
2015-09-22 13:26:46 +02:00
if ( isset ( $alternatevalue )) return $alternatevalue ;
2016-06-30 17:28:13 +02:00
2015-09-22 13:26:46 +02:00
$newelement = $this -> element ;
if ( $newelement == 'facture' ) $newelement = 'invoice' ;
if ( $newelement == 'commande' ) $newelement = 'order' ;
2016-06-30 17:28:13 +02:00
if ( empty ( $newelement ))
2015-09-22 13:26:46 +02:00
{
dol_syslog ( " Ask a default value using common method getDefaultCreateValueForField on an object with no property ->element defined. Return empty string. " , LOG_WARNING );
return '' ;
}
2016-06-30 17:28:13 +02:00
2015-09-22 13:26:46 +02:00
$keyforfieldname = strtoupper ( $newelement . '_DEFAULT_' . $fieldname );
//var_dump($keyforfieldname);
if ( isset ( $conf -> global -> $keyforfieldname )) return $conf -> global -> $keyforfieldname ;
2016-06-30 17:28:13 +02:00
// TODO Ad here a scan into table llx_overwrite_default with a filter on $this->element and $fieldname
2015-09-22 13:26:46 +02:00
}
2016-06-30 17:28:13 +02:00
2014-09-23 19:37:59 +02:00
/* For triggers */
/**
2015-07-19 15:24:54 +02:00
* Call trigger based on this instance .
* Some context information may also be provided into array property this -> context .
2014-09-23 19:37:59 +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 .
*
* @ 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 )
{
global $langs , $conf ;
2016-06-30 17:28:13 +02:00
2014-09-23 19:37:59 +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 ))
{
2016-06-25 18:48:50 +02:00
$this -> errors = array_unique ( array_merge ( $this -> errors , $interface -> errors )); // We use array_unique because when a trigger call another trigger on same object, this->errors is added twice.
2014-09-23 19:37:59 +02:00
}
else
{
$this -> errors = $interface -> errors ;
}
}
return $result ;
}
/* Functions for extrafields */
/**
* Function to get extra fields of a member into $this -> array_options
* This method is in most cases called by method fetch of objects but you can call it separately .
*
* @ param int $rowid Id of line
* @ param array $optionsArray Array resulting of call of extrafields -> fetch_name_optionals_label ()
* @ return int < 0 if error , 0 if no optionals to find nor found , 1 if a line is found and optional loaded
*/
2015-05-23 23:17:39 +02:00
function fetch_optionals ( $rowid = null , $optionsArray = null )
2014-09-23 19:37:59 +02:00
{
if ( empty ( $rowid )) $rowid = $this -> id ;
2015-08-29 19:42:51 +02:00
//To avoid SQL errors. Probably not the better solution though
if ( ! $this -> table_element ) {
return 0 ;
}
2014-09-23 19:37:59 +02:00
if ( ! is_array ( $optionsArray ))
{
// optionsArray not already loaded, so we load it
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafields = new ExtraFields ( $this -> db );
$optionsArray = $extrafields -> fetch_name_optionals_label ( $this -> table_element );
}
// Request to get complementary values
if ( count ( $optionsArray ) > 0 )
{
$sql = " SELECT rowid " ;
foreach ( $optionsArray as $name => $label )
{
$sql .= " , " . $name ;
}
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields " ;
$sql .= " WHERE fk_object = " . $rowid ;
dol_syslog ( get_class ( $this ) . " ::fetch_optionals " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$numrows = $this -> db -> num_rows ( $resql );
if ( $numrows )
{
$tab = $this -> db -> fetch_array ( $resql );
foreach ( $tab as $key => $value )
{
2015-10-20 12:40:37 +02:00
// Test fetch_array ! is_int($key) because fetch_array result is a mix table with Key as alpha and Key as int (depend db engine)
2014-09-23 19:37:59 +02:00
if ( $key != 'rowid' && $key != 'tms' && $key != 'fk_member' && ! is_int ( $key ))
{
2015-10-20 12:40:37 +02:00
// we can add this attribute to object
2014-09-23 19:37:59 +02:00
$this -> array_options [ " options_ " . $key ] = $value ;
}
}
}
$this -> db -> free ( $resql );
if ( $numrows ) return $numrows ;
else return 0 ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
return 0 ;
}
/**
* Delete all extra fields values for the current object .
*
* @ return int < 0 if KO , > 0 if OK
*/
function deleteExtraFields ()
{
$this -> db -> begin ();
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields WHERE fk_object = " . $this -> id ;
dol_syslog ( get_class ( $this ) . " ::deleteExtraFields delete " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql_del );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
/**
* Add / Update all extra fields values for the current object .
2015-02-26 16:19:45 +01:00
* Data to describe values to insert / update are stored into $this -> array_options = array ( 'options_codeforfield1' => 'valueforfield1' , 'options_codeforfield2' => 'valueforfield2' , ... )
2015-10-20 12:40:37 +02:00
* This function delete record with all extrafields and insert them again from the array $this -> array_options .
2014-09-23 19:37:59 +02:00
*
* @ return int - 1 = error , O = did nothing , 1 = OK
*/
function insertExtraFields ()
{
global $conf , $langs ;
$error = 0 ;
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) return 0 ; // For avoid conflicts if trigger used
if ( ! empty ( $this -> array_options ))
{
// Check parameters
$langs -> load ( 'admin' );
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafields = new ExtraFields ( $this -> db );
2016-02-12 21:51:26 +01:00
$target_extrafields = $extrafields -> fetch_name_optionals_label ( $this -> table_element );
2016-07-15 11:43:01 +02:00
//Eliminate copied source object extra_fields that do not exist in target object
2016-02-12 21:51:26 +01:00
$new_array_options = array ();
foreach ( $this -> array_options as $key => $value ) {
if ( in_array ( substr ( $key , 8 ), array_keys ( $target_extrafields )))
$new_array_options [ $key ] = $value ;
}
2014-09-23 19:37:59 +02:00
2016-07-15 11:43:01 +02:00
foreach ( $new_array_options as $key => $value )
2014-09-23 19:37:59 +02:00
{
$attributeKey = substr ( $key , 8 ); // Remove 'options_' prefix
$attributeType = $extrafields -> attribute_type [ $attributeKey ];
$attributeLabel = $extrafields -> attribute_label [ $attributeKey ];
2015-03-14 14:55:41 +01:00
$attributeParam = $extrafields -> attribute_param [ $attributeKey ];
2014-09-23 19:37:59 +02:00
switch ( $attributeType )
{
case 'int' :
if ( ! is_numeric ( $value ) && $value != '' )
{
2015-05-23 23:17:39 +02:00
$this -> errors [] = $langs -> trans ( " ExtraFieldHasWrongValue " , $attributeLabel );
2014-09-23 19:37:59 +02:00
return - 1 ;
}
elseif ( $value == '' )
{
$this -> array_options [ $key ] = null ;
}
break ;
case 'price' :
$this -> array_options [ $key ] = price2num ( $this -> array_options [ $key ]);
break ;
case 'date' :
$this -> array_options [ $key ] = $this -> db -> idate ( $this -> array_options [ $key ]);
break ;
case 'datetime' :
$this -> array_options [ $key ] = $this -> db -> idate ( $this -> array_options [ $key ]);
break ;
2015-05-25 18:43:46 +02:00
case 'link' :
$param_list = array_keys ( $attributeParam [ 'options' ]);
// 0 : ObjectName
// 1 : classPath
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
dol_include_once ( $InfoFieldList [ 1 ]);
2015-10-20 12:40:37 +02:00
if ( $InfoFieldList [ 0 ] && class_exists ( $InfoFieldList [ 0 ]))
{
$object = new $InfoFieldList [ 0 ]( $this -> db );
if ( $value )
{
2016-09-13 10:12:55 +02:00
if ( is_numeric ( $value )) $res = $object -> fetch ( $value );
else $res = $object -> fetch ( '' , $value );
2015-10-20 12:40:37 +02:00
if ( $res > 0 ) $this -> array_options [ $key ] = $object -> id ;
2016-06-30 17:28:13 +02:00
else
2015-10-20 12:40:37 +02:00
{
$this -> error = " Ref ' " . $value . " ' for object ' " . $object -> element . " ' not found " ;
$this -> db -> rollback ();
return - 1 ;
}
}
}
else
{
dol_syslog ( 'Error bad setup of extrafield' , LOG_WARNING );
}
2015-05-25 18:43:46 +02:00
break ;
2014-09-23 19:37:59 +02:00
}
}
$this -> db -> begin ();
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields WHERE fk_object = " . $this -> id ;
dol_syslog ( get_class ( $this ) . " ::insertExtraFields delete " , LOG_DEBUG );
$this -> db -> query ( $sql_del );
2015-05-25 18:43:46 +02:00
2014-09-23 19:37:59 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields (fk_object " ;
2016-07-15 11:43:01 +02:00
foreach ( $new_array_options as $key => $value )
2014-09-23 19:37:59 +02:00
{
$attributeKey = substr ( $key , 8 ); // Remove 'options_' prefix
// Add field of attribut
if ( $extrafields -> attribute_type [ $attributeKey ] != 'separate' ) // Only for other type of separate
$sql .= " , " . $attributeKey ;
}
$sql .= " ) VALUES ( " . $this -> id ;
2016-07-15 11:43:01 +02:00
foreach ( $new_array_options as $key => $value )
2014-09-23 19:37:59 +02:00
{
$attributeKey = substr ( $key , 8 ); // Remove 'options_' prefix
// Add field o fattribut
if ( $extrafields -> attribute_type [ $attributeKey ] != 'separate' ) // Only for other type of separate)
{
if ( $this -> array_options [ $key ] != '' )
{
$sql .= " ,' " . $this -> db -> escape ( $this -> array_options [ $key ]) . " ' " ;
}
else
{
$sql .= " ,null " ;
}
}
}
$sql .= " ) " ;
dol_syslog ( get_class ( $this ) . " ::insertExtraFields insert " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
2016-04-16 12:19:46 +02:00
else return 0 ;
}
2016-06-30 17:28:13 +02:00
2016-04-16 12:19:46 +02:00
/**
* Update an exta field value for the current object .
* Data to describe values to insert / update are stored into $this -> array_options = array ( 'options_codeforfield1' => 'valueforfield1' , 'options_codeforfield2' => 'valueforfield2' , ... )
* This function delte record with all extrafields and insert them again from the array $this -> array_options .
2016-04-22 18:12:31 +02:00
*
* @ param string $key Key of the extrafield
* @ return int - 1 = error , O = did nothing , 1 = OK
2016-04-16 12:19:46 +02:00
*/
function updateExtraField ( $key )
{
global $conf , $langs ;
$error = 0 ;
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) return 0 ; // For avoid conflicts if trigger used
2016-05-08 12:38:30 +02:00
if ( ! empty ( $this -> array_options ) && isset ( $this -> array_options [ " options_ " . $key ]))
2016-04-16 12:19:46 +02:00
{
// Check parameters
$langs -> load ( 'admin' );
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafields = new ExtraFields ( $this -> db );
$target_extrafields = $extrafields -> fetch_name_optionals_label ( $this -> table_element );
2016-05-08 12:38:30 +02:00
$value = $this -> array_options [ " options_ " . $key ];
2016-04-16 12:19:46 +02:00
$attributeType = $extrafields -> attribute_type [ $key ];
$attributeLabel = $extrafields -> attribute_label [ $key ];
$attributeParam = $extrafields -> attribute_param [ $key ];
switch ( $attributeType )
{
case 'int' :
if ( ! is_numeric ( $value ) && $value != '' )
{
$this -> errors [] = $langs -> trans ( " ExtraFieldHasWrongValue " , $attributeLabel );
return - 1 ;
}
elseif ( $value == '' )
{
2016-05-08 12:38:30 +02:00
$this -> array_options [ " options_ " . $key ] = null ;
2016-04-16 12:19:46 +02:00
}
break ;
case 'price' :
2016-05-08 12:38:30 +02:00
$this -> array_options [ " options_ " . $key ] = price2num ( $this -> array_options [ " options_ " . $key ]);
2016-04-16 12:19:46 +02:00
break ;
case 'date' :
2016-05-08 12:38:30 +02:00
$this -> array_options [ " options_ " . $key ] = $this -> db -> idate ( $this -> array_options [ " options_ " . $key ]);
2016-04-16 12:19:46 +02:00
break ;
case 'datetime' :
2016-05-08 12:38:30 +02:00
$this -> array_options [ " options_ " . $key ] = $this -> db -> idate ( $this -> array_options [ " options_ " . $key ]);
2016-04-16 12:19:46 +02:00
break ;
case 'link' :
$param_list = array_keys ( $attributeParam [ 'options' ]);
// 0 : ObjectName
// 1 : classPath
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
dol_include_once ( $InfoFieldList [ 1 ]);
$object = new $InfoFieldList [ 0 ]( $this -> db );
if ( $value )
{
$object -> fetch ( 0 , $value );
2016-05-08 12:38:30 +02:00
$this -> array_options [ " options_ " . $key ] = $object -> id ;
2016-04-16 12:19:46 +02:00
}
break ;
}
2016-06-30 17:28:13 +02:00
2016-04-16 12:19:46 +02:00
$this -> db -> begin ();
2016-05-08 12:38:30 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields SET " . $key . " =' " . $this -> db -> escape ( $this -> array_options [ " options_ " . $key ]) . " ' " ;
2016-04-16 12:19:46 +02:00
$sql .= " WHERE fk_object = " . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
2014-09-23 19:37:59 +02:00
else return 0 ;
}
/**
* Function to show lines of extrafields with output datas
*
2015-10-05 20:30:55 +02:00
* @ param Extrafields $extrafields Extrafield Object
* @ param string $mode Show output ( view ) or input ( edit ) for extrafield
* @ param array $params Optional parameters
* @ param string $keyprefix Prefix string to add into name and id of field ( can be used to avoid duplicate names )
2014-09-23 19:37:59 +02:00
*
* @ return string
*/
2015-05-25 18:43:46 +02:00
function showOptionals ( $extrafields , $mode = 'view' , $params = null , $keyprefix = '' )
2014-09-23 19:37:59 +02:00
{
2015-08-18 16:40:03 +02:00
global $_POST , $conf , $langs ;
2014-09-23 19:37:59 +02:00
$out = '' ;
if ( count ( $extrafields -> attribute_label ) > 0 )
{
$out .= " \n " ;
$out .= '<!-- showOptionalsInput --> ' ;
$out .= " \n " ;
$e = 0 ;
foreach ( $extrafields -> attribute_label as $key => $label )
{
if ( is_array ( $params ) && count ( $params ) > 0 ) {
if ( array_key_exists ( 'colspan' , $params )) {
$colspan = $params [ 'colspan' ];
}
} else {
$colspan = '3' ;
}
switch ( $mode ) {
case " view " :
$value = $this -> array_options [ " options_ " . $key ];
break ;
case " edit " :
2015-10-05 20:39:14 +02:00
if ( isset ( $_POST [ " options_ " . $key ])) {
if ( is_array ( $_POST [ " options_ " . $key ])) {
// $_POST["options"] is an array but following code expects a comma separated string
$value = implode ( " , " , $_POST [ " options_ " . $key ]);
} else {
$value = $_POST [ " options_ " . $key ];
}
} else {
$value = $this -> array_options [ " options_ " . $key ];
}
2014-09-23 19:37:59 +02:00
break ;
}
if ( $extrafields -> attribute_type [ $key ] == 'separate' )
{
$out .= $extrafields -> showSeparator ( $key );
}
else
{
$csstyle = '' ;
2016-02-03 22:49:43 +01:00
$class = ( ! empty ( $extrafields -> attribute_hidden [ $key ]) ? 'class="hideobject" ' : '' );
2014-09-23 19:37:59 +02:00
if ( is_array ( $params ) && count ( $params ) > 0 ) {
if ( array_key_exists ( 'style' , $params )) {
$csstyle = $params [ 'style' ];
}
}
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_USE_TWO_COLUMS ) && ( $e % 2 ) == 0 )
{
2016-11-06 16:26:18 +01:00
$out .= '<tr ' . $class . $csstyle . ' class="' . $this -> element . '_extras_' . $key . '">' ;
2014-09-23 19:37:59 +02:00
$colspan = '0' ;
}
else
{
2016-11-06 16:26:18 +01:00
$out .= '<tr ' . $class . $csstyle . ' class="' . $this -> element . '_extras_' . $key . '">' ;
2014-09-23 19:37:59 +02:00
}
// Convert date into timestamp format
if ( in_array ( $extrafields -> attribute_type [ $key ], array ( 'date' , 'datetime' )))
{
$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 ]);
}
if ( $extrafields -> attribute_required [ $key ])
2016-01-15 00:44:38 +01:00
$label = '<span' . ( $mode != 'view' ? ' class="fieldrequired"' : '' ) . '>' . $label . '</span>' ;
2014-09-23 19:37:59 +02:00
2015-08-18 16:40:03 +02:00
$out .= '<td>' . $langs -> trans ( $label ) . '</td>' ;
2016-02-04 21:05:19 +01:00
$html_id = ! empty ( $this -> id ) ? $this -> element . '_extras_' . $key . '_' . $this -> id : '' ;
$out .= '<td id="' . $html_id . '" class="' . $this -> element . '_extras_' . $key . '" ' . ( $colspan ? ' colspan="' . $colspan . '"' : '' ) . '>' ;
2014-09-23 19:37:59 +02:00
switch ( $mode ) {
2016-11-25 17:47:47 +01:00
case " view " :
2016-12-06 13:10:44 +01:00
$out .= $extrafields -> showOutputField ( $key , $value );
2016-11-25 17:47:47 +01:00
break ;
case " edit " :
2016-12-06 13:10:44 +01:00
$out .= $extrafields -> showInputField ( $key , $value , '' , $keyprefix , '' , 0 , $this -> id );
2016-11-25 17:47:47 +01:00
break ;
2014-09-23 19:37:59 +02:00
}
2014-11-30 22:06:58 +01:00
$out .= '</td>' ;
2014-09-23 19:37:59 +02:00
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_USE_TWO_COLUMS ) && (( $e % 2 ) == 1 )) $out .= '</tr>' ;
else $out .= '</tr>' ;
$e ++ ;
}
}
$out .= " \n " ;
2016-11-25 17:47:47 +01:00
// Add code to manage list depending on others
if ( ! empty ( $conf -> use_javascript_ajax ))
2014-09-23 19:37:59 +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 );
});
});
}
setListDependencies ();
});
2014-11-30 22:06:58 +01:00
</ script > ' . " \n " ;
$out .= '<!-- /showOptionalsInput --> ' . " \n " ;
2014-09-23 19:37:59 +02:00
}
return $out ;
}
2015-03-23 01:39:12 +01:00
/**
* Returns the rights used for this class
* @ return stdClass
*/
public function getRights ()
{
global $user ;
2016-04-24 18:55:19 +02:00
$element = $this -> element ;
if ( $element == 'facturerec' ) $element = 'facture' ;
return $user -> rights -> { $element };
2015-03-23 01:39:12 +01:00
}
2015-04-18 18:11:01 +02:00
/**
* Function used to replace a thirdparty id with another one .
* This function is meant to be called from replaceThirdparty with the appropiate tables
* Column name fk_soc MUST be used to identify thirdparties
*
2016-06-20 15:42:16 +02:00
* @ param DoliDB $db Database handler
* @ param int $origin_id Old thirdparty id ( the thirdparty to delete )
* @ param int $dest_id New thirdparty id ( the thirdparty that will received element of the other )
* @ param string [] $tables Tables that need to be changed
2016-06-30 17:28:13 +02:00
* @ param int $ignoreerrors Ignore errors . Return true even if errors . We need this when replacement can fails like for categories ( categorie of old thirdparty may already exists on new one )
2015-04-18 18:11:01 +02:00
* @ return bool
*/
2016-06-20 15:42:16 +02:00
public static function commonReplaceThirdparty ( DoliDB $db , $origin_id , $dest_id , array $tables , $ignoreerrors = 0 )
2015-04-18 18:11:01 +02:00
{
2015-06-05 14:46:30 +02:00
foreach ( $tables as $table )
{
2015-04-18 18:11:01 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $table . ' SET fk_soc = ' . $dest_id . ' WHERE fk_soc = ' . $origin_id ;
2015-03-23 01:39:12 +01:00
2016-06-30 17:28:13 +02:00
if ( ! $db -> query ( $sql ))
2016-06-20 15:42:16 +02:00
{
2016-08-16 09:07:08 +02:00
if ( $ignoreerrors ) return true ; // TODO Not enough. If there is A-B on kept thirdarty and B-C on old one, we must get A-B-C after merge. Not A-B.
2016-06-20 15:42:16 +02:00
//$this->errors = $db->lasterror();
return false ;
2015-04-18 18:11:01 +02:00
}
}
2015-03-23 01:39:12 +01:00
2015-04-18 18:11:01 +02:00
return true ;
}
2016-06-30 17:28:13 +02:00
2016-01-13 12:48:59 +01:00
/**
* Get buy price to use for margin calculation . This function is called when buy price is unknown .
2015-10-26 20:33:42 +01:00
* set buy price = sell price if ForceBuyingPriceIfNull configured ,
2016-01-13 12:48:59 +01:00
* else if calculation MARGIN_TYPE = 'costprice' and costprice is defined , use costprice as buyprice
* else if calculation MARGIN_TYPE = 'pmp' and pmp is calculated , use pmp as buyprice
2015-10-26 20:33:42 +01:00
* else set min buy price as buy price
2016-06-30 17:28:13 +02:00
*
2015-10-26 20:33:42 +01:00
* @ param float $unitPrice product unit price
* @ param float $discountPercent line discount percent
* @ param int $fk_product product id
*
* @ return float < 0 if ko , buyprice if ok
*/
2016-06-30 17:28:13 +02:00
public function defineBuyPrice ( $unitPrice = 0 , $discountPercent = 0 , $fk_product = 0 )
2015-10-26 20:33:42 +01:00
{
global $conf ;
2016-06-30 17:28:13 +02:00
2015-10-26 20:33:42 +01:00
$buyPrice = 0 ;
2016-06-30 17:28:13 +02:00
2016-01-13 12:48:59 +01:00
if (( $unitPrice > 0 ) && ( isset ( $conf -> global -> ForceBuyingPriceIfNull ) && $conf -> global -> ForceBuyingPriceIfNull == 1 )) // In most cases, test here is false
2015-10-26 20:33:42 +01:00
{
$buyPrice = $unitPrice * ( 1 - $discountPercent / 100 );
}
else
{
2016-01-13 12:48:59 +01:00
// Get cost price for margin calculation
2015-10-26 20:33:42 +01:00
if ( ! empty ( $fk_product ))
{
2016-01-13 12:48:59 +01:00
if ( isset ( $conf -> global -> MARGIN_TYPE ) && $conf -> global -> MARGIN_TYPE == 'costprice' )
{
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
$product = new Product ( $this -> db );
$result = $product -> fetch ( $fk_product );
if ( $result <= 0 )
{
$this -> errors [] = 'ErrorProductIdDoesNotExists' ;
return - 1 ;
}
if ( $product -> cost_price > 0 )
{
$buyPrice = $product -> cost_price ;
}
else if ( $product -> pmp > 0 )
{
$buyPrice = $product -> pmp ;
}
}
else if ( isset ( $conf -> global -> MARGIN_TYPE ) && $conf -> global -> MARGIN_TYPE == 'pmp' )
2015-10-26 20:33:42 +01:00
{
2015-10-27 15:50:55 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
2015-10-26 20:33:42 +01:00
$product = new Product ( $this -> db );
$result = $product -> fetch ( $fk_product );
if ( $result <= 0 )
{
2015-11-21 12:35:45 +01:00
$this -> errors [] = 'ErrorProductIdDoesNotExists' ;
2015-10-26 20:33:42 +01:00
return - 1 ;
}
2016-01-13 12:48:59 +01:00
if ( $product -> pmp > 0 )
2015-10-26 20:33:42 +01:00
{
$buyPrice = $product -> pmp ;
}
}
2016-06-30 17:28:13 +02:00
2016-01-13 12:48:59 +01:00
if ( empty ( $buyPrice ) && isset ( $conf -> global -> MARGIN_TYPE ) && in_array ( $conf -> global -> MARGIN_TYPE , array ( '1' , 'pmp' , 'costprice' )))
2015-10-26 20:33:42 +01:00
{
2015-10-27 15:50:55 +01:00
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php' ;
2015-10-26 20:33:42 +01:00
$productFournisseur = new ProductFournisseur ( $this -> db );
if (( $result = $productFournisseur -> find_min_price_product_fournisseur ( $fk_product )) > 0 )
{
2016-04-27 16:42:59 +02:00
$buyPrice = $productFournisseur -> fourn_unitprice ;
2015-10-26 20:33:42 +01:00
}
2015-11-21 12:35:45 +01:00
else if ( $result < 0 )
2015-10-26 20:33:42 +01:00
{
2015-11-21 12:35:45 +01:00
$this -> errors [] = $productFournisseur -> error ;
return - 2 ;
2015-10-26 20:33:42 +01:00
}
}
}
}
return $buyPrice ;
}
2013-04-19 13:30:08 +02:00
}