2004-10-20 23:06:45 +02:00
< ? php
2007-01-02 16:55:07 +01:00
/* Copyright ( C ) 2001 - 2007 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2013-10-27 02:23:25 +02:00
* Copyright ( C ) 2004 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2013-06-24 17:10:11 +02:00
* Copyright ( C ) 2005 - 2013 Regis Houssin < regis . houssin @ capnetworks . com >
2006-03-17 01:17:27 +01:00
* Copyright ( C ) 2006 Andre Cianfarani < acianfa @ free . fr >
2011-06-10 13:39:01 +02:00
* Copyright ( C ) 2007 - 2011 Jean Heimburger < jean @ tiaris . info >
2011-04-18 17:54:13 +02:00
* Copyright ( C ) 2010 - 2011 Juanjo Menent < jmenent @ 2 byte . es >
2013-05-07 12:07:06 +02:00
* Copyright ( C ) 2013 Cedric GROSS < c . gross @ kreiz - it . fr >
2013-06-10 00:11:25 +02:00
* Copyright ( C ) 2013 Marcos GarcÃa < marcosgdf @ gmail . com >
2006-11-30 15:23:38 +01:00
*
2002-04-30 17:52:56 +02:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2002-04-30 17:52:56 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2011-08-01 01:19:04 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2002-04-30 17:52:56 +02:00
*/
2005-03-01 22:44:32 +01:00
/**
2010-07-21 14:35:56 +02:00
* \file htdocs / product / class / product . class . php
2008-12-08 22:55:18 +01:00
* \ingroup produit
2012-02-22 12:37:39 +01:00
* \brief File of class to manage predefined products or services
2008-07-27 22:47:13 +02:00
*/
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2007-11-05 23:37:41 +01:00
2005-03-01 22:44:32 +01:00
/**
2012-02-22 12:37:39 +01:00
* Class to manage products or services
2008-07-27 22:47:13 +02:00
*/
2007-11-05 23:37:41 +01:00
class Product extends CommonObject
2003-06-22 14:16:11 +02:00
{
2011-09-20 19:19:46 +02:00
public $element = 'product' ;
public $table_element = 'product' ;
public $fk_element = 'fk_product' ;
2014-01-21 12:53:27 +01:00
protected $childtables = array ( 'propaldet' , 'commandedet' , 'facturedet' , 'contratdet' , 'facture_fourn_det' , 'commande_fournisseurdet' ); // To test if we can delete object
2011-09-20 19:19:46 +02:00
protected $isnolinkedbythird = 1 ; // No field fk_soc
protected $ismultientitymanaged = 1 ; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
2007-11-05 23:37:41 +01:00
//! Identifiant unique
2008-07-27 22:47:13 +02:00
var $id ;
//! Ref
var $ref ;
2011-10-02 01:23:57 +02:00
var $libelle ; // TODO deprecated
var $label ;
2008-07-27 22:47:13 +02:00
var $description ;
2011-10-09 17:37:27 +02:00
//! Type 0 for regular product, 1 for service (Advanced feature: 2 for assembly kit, 3 for stock kit)
var $type ;
//! Selling price
var $price ; // Price net
2008-08-04 02:30:52 +02:00
var $price_ttc ; // Price with tax
2011-10-09 17:37:27 +02:00
var $price_min ; // Minimum price net
var $price_min_ttc ; // Minimum price with tax
//! Base price ('TTC' for price including tax or 'HT' for net price)
2008-07-27 22:47:13 +02:00
var $price_base_type ;
2011-10-09 17:37:27 +02:00
//! Arrays for multiprices
2008-07-27 22:47:13 +02:00
var $multiprices = array ();
var $multiprices_ttc = array ();
var $multiprices_base_type = array ();
2009-11-30 01:23:58 +01:00
var $multiprices_tva_tx = array ();
2013-06-14 00:11:15 +02:00
var $multiprices_recuperableonly = array ();
2012-10-22 17:47:15 +02:00
//! Price by quantity arrays
var $price_by_qty ;
var $prices_by_qty = array ();
var $prices_by_qty_id = array ();
var $prices_by_qty_list = array ();
2011-10-09 17:37:27 +02:00
//! Default VAT rate of product
2008-07-27 22:47:13 +02:00
var $tva_tx ;
2011-10-09 17:37:27 +02:00
//! French VAT NPR (0 or 1)
2010-08-09 21:08:22 +02:00
var $tva_npr = 0 ;
//! Spanish local taxes
2010-03-27 18:08:41 +01:00
var $localtax1_tx ;
var $localtax2_tx ;
2009-07-09 20:44:13 +02:00
//! Stock
2008-07-27 22:47:13 +02:00
var $stock_reel ;
2011-05-18 18:20:43 +02:00
//! Average price value for product entry into stock (PMP)
2009-07-09 20:44:13 +02:00
var $pmp ;
2011-10-02 01:23:57 +02:00
//! Stock alert
2008-07-27 22:47:13 +02:00
var $seuil_stock_alerte ;
2013-05-30 16:32:48 +02:00
//! Ask for replenishment when $desiredstock < $stock_reel
public $desiredstock ;
2008-07-27 22:47:13 +02:00
//! Duree de validite du service
var $duration_value ;
//! Unite de duree
var $duration_unit ;
// Statut indique si le produit est en vente '1' ou non '0'
var $status ;
2010-08-12 18:19:52 +02:00
// Status indicate whether the product is available for purchase '1' or not '0'
2010-08-19 22:58:08 +02:00
var $status_buy ;
2010-03-13 00:40:39 +01:00
// Statut indique si le produit est un produit fini '1' ou une matiere premiere '0'
2009-01-09 21:53:03 +01:00
var $finished ;
2008-07-27 22:47:13 +02:00
2012-01-30 20:08:11 +01:00
var $customcode ; // Customs code
2010-11-02 12:22:41 +01:00
var $country_id ; // Country origin id
var $country_code ; // Country origin code (US, FR, ...)
2008-07-27 22:47:13 +02:00
//! Unites de mesure
var $weight ;
var $weight_units ;
2010-03-13 00:40:39 +01:00
var $length ;
var $length_units ;
var $surface ;
var $surface_units ;
2008-07-27 22:47:13 +02:00
var $volume ;
var $volume_units ;
2010-03-20 19:39:32 +01:00
var $accountancy_code_buy ;
var $accountancy_code_sell ;
2011-12-07 15:06:29 +01:00
//! barcode
var $barcode ; // value
var $barcode_type ; // id
var $barcode_type_code ; // code (loaded by fetch_barcode)
var $barcode_type_label ; // label (loaded by fetch_barcode)
var $barcode_type_coder ; // coder (loaded by fetch_barcode)
2008-07-27 22:47:13 +02:00
var $stats_propale = array ();
var $stats_commande = array ();
var $stats_contrat = array ();
var $stats_facture = array ();
var $multilangs = array ();
//! Taille de l'image
var $imgWidth ;
var $imgHeight ;
//! Canevas a utiliser si le produit n'est pas un produit generique
var $canvas ;
2011-10-01 01:30:32 +02:00
var $import_key ;
var $date_creation ;
var $date_modification ;
2008-07-27 22:47:13 +02:00
//! Id du fournisseur
var $product_fourn_id ;
2009-12-13 23:29:13 +01:00
2009-12-10 12:25:12 +01:00
//! Product ID already linked to a reference supplier
var $product_id_already_linked ;
2010-05-26 16:52:32 +02:00
2010-05-01 11:03:15 +02:00
var $nbphoto ;
2008-07-27 22:47:13 +02:00
2010-07-14 17:41:39 +02:00
//! Contains detail of stock of product into each warehouse
var $stock_warehouse = array ();
2012-07-02 12:07:42 +02:00
2012-06-08 20:27:02 +02:00
var $oldcopy ;
2010-07-14 17:41:39 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-09-11 20:35:38 +02:00
* Constructor
2011-08-27 19:25:09 +02:00
*
2011-12-05 19:41:38 +01:00
* @ param DoliDB $db Database handler
2008-07-27 22:47:13 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db )
2008-07-27 22:47:13 +02:00
{
global $langs ;
2011-12-05 19:41:38 +01:00
$this -> db = $db ;
2008-07-27 22:47:13 +02:00
$this -> status = 0 ;
2010-08-12 18:19:52 +02:00
$this -> status_buy = 0 ;
2010-05-01 11:03:15 +02:00
$this -> nbphoto = 0 ;
2008-07-27 22:47:13 +02:00
$this -> stock_reel = 0 ;
$this -> seuil_stock_alerte = 0 ;
2013-05-30 16:32:48 +02:00
$this -> desiredstock = 0 ;
2008-07-27 22:47:13 +02:00
$this -> canvas = '' ;
}
2008-08-04 02:30:52 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Check that ref and label are ok
2011-08-27 19:25:09 +02:00
*
2011-06-10 13:39:01 +02:00
* @ return int > 1 if OK , <= 0 if KO
2008-07-27 22:47:13 +02:00
*/
function check ()
{
2009-04-29 20:02:50 +02:00
$this -> ref = dol_sanitizeFileName ( stripslashes ( $this -> ref ));
2008-07-27 22:47:13 +02:00
$err = 0 ;
2010-08-24 21:51:56 +02:00
if ( dol_strlen ( trim ( $this -> ref )) == 0 )
2008-07-27 22:47:13 +02:00
$err ++ ;
2010-08-24 21:51:56 +02:00
if ( dol_strlen ( trim ( $this -> libelle )) == 0 )
2008-07-27 22:47:13 +02:00
$err ++ ;
if ( $err > 0 )
{
return 0 ;
}
else
{
return 1 ;
}
}
/**
2010-11-02 12:22:41 +01:00
* Insert product into database
2011-08-27 19:25:09 +02:00
*
2011-10-02 01:23:57 +02:00
* @ param User $user User making insert
* @ param int $notrigger Disable triggers
* @ return int Id of product / service if OK or number of error < 0
2008-07-27 22:47:13 +02:00
*/
2011-07-29 22:47:34 +02:00
function create ( $user , $notrigger = 0 )
2007-01-29 11:32:52 +01:00
{
2011-07-29 22:47:34 +02:00
global $conf , $langs ;
2010-02-21 02:45:58 +01:00
2011-07-29 22:47:34 +02:00
$error = 0 ;
2007-06-30 15:06:03 +02:00
2007-07-09 01:03:06 +02:00
// Clean parameters
2008-10-25 23:35:27 +02:00
$this -> ref = dol_string_nospecial ( trim ( $this -> ref ));
2007-12-06 22:11:11 +01:00
$this -> libelle = trim ( $this -> libelle );
2009-01-25 15:30:42 +01:00
$this -> price_ttc = price2num ( $this -> price_ttc );
$this -> price = price2num ( $this -> price );
$this -> price_min_ttc = price2num ( $this -> price_min_ttc );
$this -> price_min = price2num ( $this -> price_min );
2010-03-27 18:08:41 +01:00
if ( empty ( $this -> tva_tx )) $this -> tva_tx = 0 ;
2011-10-09 17:37:27 +02:00
if ( empty ( $this -> tva_npr )) $this -> tva_npr = 0 ;
2010-03-27 18:08:41 +01:00
//Local taxes
if ( empty ( $this -> localtax1_tx )) $this -> localtax1_tx = 0 ;
if ( empty ( $this -> localtax2_tx )) $this -> localtax2_tx = 0 ;
2010-03-29 01:26:14 +02:00
2010-03-27 18:08:41 +01:00
if ( empty ( $this -> price )) $this -> price = 0 ;
if ( empty ( $this -> price_min )) $this -> price_min = 0 ;
2012-10-22 17:47:15 +02:00
// Price by quantity
if ( empty ( $this -> price_by_qty )) $this -> price_by_qty = 0 ;
2010-03-27 18:08:41 +01:00
if ( empty ( $this -> status )) $this -> status = 0 ;
2011-04-06 15:29:46 +02:00
if ( empty ( $this -> status_buy )) $this -> status_buy = 0 ;
2009-01-09 21:53:03 +01:00
2007-07-09 01:03:06 +02:00
$price_ht = 0 ;
$price_ttc = 0 ;
2008-08-28 01:00:37 +02:00
$price_min_ht = 0 ;
$price_min_ttc = 0 ;
2008-08-04 02:30:52 +02:00
if ( $this -> price_base_type == 'TTC' && $this -> price_ttc > 0 )
2007-06-30 15:06:03 +02:00
{
2008-08-04 02:30:52 +02:00
$price_ttc = price2num ( $this -> price_ttc , 'MU' );
$price_ht = price2num ( $this -> price_ttc / ( 1 + ( $this -> tva_tx / 100 )), 'MU' );
}
if ( $this -> price_base_type != 'TTC' && $this -> price > 0 )
{
$price_ht = price2num ( $this -> price , 'MU' );
$price_ttc = price2num ( $this -> price * ( 1 + ( $this -> tva_tx / 100 )), 'MU' );
2007-06-30 15:06:03 +02:00
}
2009-01-25 15:30:42 +01:00
if (( $this -> price_min_ttc > 0 ) && ( $this -> price_base_type == 'TTC' ))
2008-08-28 01:00:37 +02:00
{
$price_min_ttc = price2num ( $this -> price_min_ttc , 'MU' );
$price_min_ht = price2num ( $this -> price_min_ttc / ( 1 + ( $this -> tva_tx / 100 )), 'MU' );
}
2009-01-25 15:30:42 +01:00
if (( $this -> price_min > 0 ) && ( $this -> price_base_type != 'TTC' ))
2008-08-28 01:00:37 +02:00
{
$price_min_ht = price2num ( $this -> price_min , 'MU' );
$price_min_ttc = price2num ( $this -> price_min * ( 1 + ( $this -> tva_tx / 100 )), 'MU' );
}
2008-07-27 22:47:13 +02:00
2008-06-19 19:25:15 +02:00
// Check parameters
2008-07-27 22:47:13 +02:00
if ( empty ( $this -> libelle ))
2008-06-19 19:25:15 +02:00
{
$this -> error = 'ErrorWrongParameters' ;
return - 1 ;
}
2011-07-29 22:47:34 +02:00
if ( empty ( $this -> ref ))
{
2013-07-27 12:22:00 +02:00
// Load object modCodeProduct
$module = ( ! empty ( $conf -> global -> PRODUCT_CODEPRODUCT_ADDON ) ? $conf -> global -> PRODUCT_CODEPRODUCT_ADDON : 'mod_codeproduct_leopard' );
if ( $module != 'mod_codeproduct_leopard' ) // Do not load module file
{
if ( substr ( $module , 0 , 16 ) == 'mod_codeproduct_' && substr ( $module , - 3 ) == 'php' )
{
$module = substr ( $module , 0 , dol_strlen ( $module ) - 4 );
}
dol_include_once ( '/core/modules/product/' . $module . '.php' );
$modCodeProduct = new $module ;
if ( ! empty ( $modCodeProduct -> code_auto ))
{
$this -> ref = $modCodeProduct -> getNextValue ( $this , $this -> type );
}
unset ( $modCodeProduct );
}
if ( empty ( $this -> ref ))
{
$this -> error = 'ProductModuleNotSetupForAutoRef' ;
return - 2 ;
}
2011-07-29 22:47:34 +02:00
}
2008-07-27 22:47:13 +02:00
2012-09-16 21:12:55 +02:00
dol_syslog ( get_class ( $this ) . " ::create ref= " . $this -> ref . " price= " . $this -> price . " price_ttc= " . $this -> price_ttc . " tva_tx= " . $this -> tva_tx . " price_base_type= " . $this -> price_base_type , LOG_DEBUG );
2008-07-27 22:47:13 +02:00
2011-10-02 01:23:57 +02:00
$now = dol_now ();
2008-08-04 02:30:52 +02:00
2011-07-29 22:47:34 +02:00
$this -> db -> begin ();
2008-08-04 02:30:52 +02:00
2011-07-29 22:47:34 +02:00
$sql = " SELECT count(*) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product " ;
2012-02-11 10:18:09 +01:00
$sql .= " WHERE entity IN ( " . getEntity ( 'product' , 1 ) . " ) " ;
$sql .= " AND ref = ' " . $this -> ref . " ' " ;
2011-07-29 22:47:34 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
if ( $obj -> nb == 0 )
2007-06-30 15:06:03 +02:00
{
2011-07-29 22:47:34 +02:00
// Produit non deja existant
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " product ( " ;
$sql .= " datec " ;
$sql .= " , entity " ;
$sql .= " , ref " ;
2012-07-19 14:26:02 +02:00
$sql .= " , ref_ext " ;
2011-07-29 22:47:34 +02:00
$sql .= " , price_min " ;
$sql .= " , price_min_ttc " ;
$sql .= " , label " ;
$sql .= " , fk_user_author " ;
$sql .= " , fk_product_type " ;
$sql .= " , price " ;
$sql .= " , price_ttc " ;
$sql .= " , price_base_type " ;
$sql .= " , tobuy " ;
$sql .= " , tosell " ;
$sql .= " , canvas " ;
$sql .= " , finished " ;
$sql .= " ) VALUES ( " ;
2011-10-02 01:23:57 +02:00
$sql .= $this -> db -> idate ( $now );
2011-07-29 22:47:34 +02:00
$sql .= " , " . $conf -> entity ;
2012-07-19 14:26:02 +02:00
$sql .= " , ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
2012-09-16 21:12:55 +02:00
$sql .= " , " . ( ! empty ( $this -> ref_ext ) ? " ' " . $this -> db -> escape ( $this -> ref_ext ) . " ' " : " null " );
2011-07-29 22:47:34 +02:00
$sql .= " , " . price2num ( $price_min_ht );
$sql .= " , " . price2num ( $price_min_ttc );
2012-09-16 21:12:55 +02:00
$sql .= " , " . ( ! empty ( $this -> libelle ) ? " ' " . $this -> db -> escape ( $this -> libelle ) . " ' " : " null " );
2011-07-29 22:47:34 +02:00
$sql .= " , " . $user -> id ;
$sql .= " , " . $this -> type ;
$sql .= " , " . price2num ( $price_ht );
$sql .= " , " . price2num ( $price_ttc );
$sql .= " , ' " . $this -> price_base_type . " ' " ;
$sql .= " , " . $this -> status ;
$sql .= " , " . $this -> status_buy ;
$sql .= " , ' " . $this -> canvas . " ' " ;
2014-03-06 18:31:02 +01:00
$sql .= " , " . (( ! isset ( $this -> finished ) || $this -> finished < 0 || $this -> finished == '' ) ? 'null' : ( int ) $this -> finished );
2011-07-29 22:47:34 +02:00
$sql .= " ) " ;
2012-01-14 20:23:47 +01:00
dol_syslog ( get_class ( $this ) . " ::Create sql= " . $sql );
2011-07-29 22:47:34 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
2007-06-30 15:06:03 +02:00
{
2011-07-29 22:47:34 +02:00
$id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " product " );
2008-07-27 22:47:13 +02:00
2011-07-29 22:47:34 +02:00
if ( $id > 0 )
{
$this -> id = $id ;
$this -> price = $price_ht ;
$this -> price_ttc = $price_ttc ;
$this -> price_min = $price_min_ht ;
$this -> price_min_ttc = $price_min_ttc ;
$result = $this -> _log_price ( $user );
if ( $result > 0 )
2007-06-30 15:06:03 +02:00
{
2013-06-05 15:44:42 +02:00
if ( $this -> update ( $id , $user , true , 'add' ) <= 0 )
2007-06-30 15:06:03 +02:00
{
2011-07-29 22:47:34 +02:00
$error ++ ;
$this -> error = 'ErrorFailedToUpdateRecord' ;
2007-06-30 15:06:03 +02:00
}
}
else
{
2011-07-29 22:47:34 +02:00
$error ++ ;
$this -> error = $this -> db -> lasterror ();
2007-06-30 15:06:03 +02:00
}
}
else
{
2011-07-29 22:47:34 +02:00
$error ++ ;
$this -> error = 'ErrorFailedToGetInsertedId' ;
2007-06-30 15:06:03 +02:00
}
}
else
{
2011-07-29 22:47:34 +02:00
$error ++ ;
$this -> error = $this -> db -> lasterror ();
2007-06-30 15:06:03 +02:00
}
}
else
{
2011-07-29 22:47:34 +02:00
// Product already exists with this ref
2011-10-02 01:23:57 +02:00
$langs -> load ( " products " );
2012-09-02 15:09:48 +02:00
$error ++ ;
2012-06-08 21:33:12 +02:00
$this -> error = " ErrorProductAlreadyExists " ;
2008-07-27 22:47:13 +02:00
}
2007-06-30 15:06:03 +02:00
}
else
{
2011-07-29 22:47:34 +02:00
$error ++ ;
$this -> error = $this -> db -> lasterror ();
2007-06-30 15:06:03 +02:00
}
2008-07-27 22:47:13 +02:00
2011-07-29 22:47:34 +02:00
if ( ! $error && ! $notrigger )
{
// Appel des triggers
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
2011-07-29 22:47:34 +02:00
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'PRODUCT_CREATE' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// Fin appel triggers
}
2008-07-27 22:47:13 +02:00
2011-07-29 22:47:34 +02:00
if ( ! $error )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
$this -> db -> rollback ();
return - $error ;
}
2008-07-27 22:47:13 +02:00
}
2007-06-30 15:06:03 +02:00
/**
2010-11-02 12:22:41 +01:00
* Update a record into database
2011-08-27 19:25:09 +02:00
*
2011-10-02 01:23:57 +02:00
* @ param int $id Id of product
* @ param User $user Object user making update
2012-01-14 20:23:47 +01:00
* @ param int $notrigger Disable triggers
2012-11-10 16:08:26 +01:00
* @ param string $action Current action for hookmanager
2011-10-02 01:23:57 +02:00
* @ return int 1 if OK , - 1 if ref already exists , - 2 if other error
2009-11-30 01:23:58 +01:00
*/
2012-11-10 16:08:26 +01:00
function update ( $id , $user , $notrigger = false , $action = 'update' )
2007-06-30 15:06:03 +02:00
{
2013-01-25 19:12:54 +01:00
global $langs , $conf , $hookmanager ;
2006-11-30 15:23:38 +01:00
2011-12-05 19:41:38 +01:00
$error = 0 ;
2012-07-02 12:07:42 +02:00
2012-06-08 21:38:11 +02:00
$this -> db -> begin ();
2011-12-07 12:58:39 +01:00
2007-06-30 15:06:03 +02:00
// Verification parametres
2011-10-09 17:37:27 +02:00
if ( ! $this -> libelle ) $this -> libelle = 'MISSING LABEL' ;
2006-11-30 15:23:38 +01:00
2010-11-02 12:22:41 +01:00
// Clean parameters
2008-10-25 23:35:27 +02:00
$this -> ref = dol_string_nospecial ( trim ( $this -> ref ));
2007-06-30 15:06:03 +02:00
$this -> libelle = trim ( $this -> libelle );
$this -> description = trim ( $this -> description );
2013-04-30 15:22:39 +02:00
$this -> note = ( isset ( $this -> note ) ? trim ( $this -> note ) : " null " );
2008-04-21 14:23:08 +02:00
$this -> weight = price2num ( $this -> weight );
$this -> weight_units = trim ( $this -> weight_units );
2010-03-13 00:40:39 +01:00
$this -> length = price2num ( $this -> length );
$this -> length_units = trim ( $this -> length_units );
$this -> surface = price2num ( $this -> surface );
$this -> surface_units = trim ( $this -> surface_units );
2008-04-21 14:23:08 +02:00
$this -> volume = price2num ( $this -> volume );
$this -> volume_units = trim ( $this -> volume_units );
2008-12-09 22:02:58 +01:00
if ( empty ( $this -> tva_tx )) $this -> tva_tx = 0 ;
2011-10-09 17:37:27 +02:00
if ( empty ( $this -> tva_npr )) $this -> tva_npr = 0 ;
2010-03-27 18:08:41 +01:00
if ( empty ( $this -> localtax1_tx )) $this -> localtax1_tx = 0 ;
if ( empty ( $this -> localtax2_tx )) $this -> localtax2_tx = 0 ;
2013-10-01 11:58:38 +02:00
if ( empty ( $this -> status )) $this -> status = 0 ;
if ( empty ( $this -> status_buy )) $this -> status_buy = 0 ;
2013-10-27 02:23:25 +02:00
2010-11-02 12:22:41 +01:00
if ( empty ( $this -> country_id )) $this -> country_id = 0 ;
2010-03-20 19:39:32 +01:00
$this -> accountancy_code_buy = trim ( $this -> accountancy_code_buy );
$this -> accountancy_code_sell = trim ( $this -> accountancy_code_sell );
$sql = " UPDATE " . MAIN_DB_PREFIX . " product " ;
2011-02-24 19:11:12 +01:00
$sql .= " SET label = ' " . $this -> db -> escape ( $this -> libelle ) . " ' " ;
2013-10-01 11:58:38 +02:00
$sql .= " , ref = ' " . $this -> ref . " ' " ;
$sql .= " , ref_ext = " . ( ! empty ( $this -> ref_ext ) ? " ' " . $this -> db -> escape ( $this -> ref_ext ) . " ' " : " null " );
$sql .= " , tva_tx = " . $this -> tva_tx ;
$sql .= " , recuperableonly = " . $this -> tva_npr ;
$sql .= " , localtax1_tx = " . $this -> localtax1_tx ;
$sql .= " , localtax2_tx = " . $this -> localtax2_tx ;
$sql .= " , tosell = " . $this -> status ;
$sql .= " , tobuy = " . $this -> status_buy ;
2014-03-06 18:31:02 +01:00
$sql .= " , finished = " . (( ! isset ( $this -> finished ) || $this -> finished < 0 ) ? " null " : ( int ) $this -> finished );
2013-10-01 11:58:38 +02:00
$sql .= " , weight = " . ( $this -> weight != '' ? " ' " . $this -> weight . " ' " : 'null' );
$sql .= " , weight_units = " . ( $this -> weight_units != '' ? " ' " . $this -> weight_units . " ' " : 'null' );
$sql .= " , length = " . ( $this -> length != '' ? " ' " . $this -> length . " ' " : 'null' );
$sql .= " , length_units = " . ( $this -> length_units != '' ? " ' " . $this -> length_units . " ' " : 'null' );
$sql .= " , surface = " . ( $this -> surface != '' ? " ' " . $this -> surface . " ' " : 'null' );
$sql .= " , surface_units = " . ( $this -> surface_units != '' ? " ' " . $this -> surface_units . " ' " : 'null' );
$sql .= " , volume = " . ( $this -> volume != '' ? " ' " . $this -> volume . " ' " : 'null' );
$sql .= " , volume_units = " . ( $this -> volume_units != '' ? " ' " . $this -> volume_units . " ' " : 'null' );
$sql .= " , seuil_stock_alerte = " . (( isset ( $this -> seuil_stock_alerte ) && $this -> seuil_stock_alerte != '' ) ? " ' " . $this -> seuil_stock_alerte . " ' " : " null " );
$sql .= " , description = ' " . $this -> db -> escape ( $this -> description ) . " ' " ;
$sql .= " , customcode = ' " . $this -> db -> escape ( $this -> customcode ) . " ' " ;
$sql .= " , fk_country = " . ( $this -> country_id > 0 ? $this -> country_id : 'null' );
$sql .= " , note = ' " . $this -> db -> escape ( $this -> note ) . " ' " ;
$sql .= " , duration = ' " . $this -> duration_value . $this -> duration_unit . " ' " ;
$sql .= " , accountancy_code_buy = ' " . $this -> accountancy_code_buy . " ' " ;
$sql .= " , accountancy_code_sell= ' " . $this -> accountancy_code_sell . " ' " ;
2013-05-30 16:32:48 +02:00
$sql .= " , desiredstock = " . (( isset ( $this -> desiredstock ) && $this -> desiredstock != '' ) ? $this -> desiredstock : " null " );
2010-03-20 19:39:32 +01:00
$sql .= " WHERE rowid = " . $id ;
2006-11-30 15:23:38 +01:00
2012-03-03 19:02:44 +01:00
dol_syslog ( get_class ( $this ) . " update sql= " . $sql );
2008-06-17 20:03:36 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2007-06-30 15:06:03 +02:00
{
2011-04-06 15:29:46 +02:00
$this -> id = $id ;
2011-04-20 00:25:06 +02:00
2007-06-30 15:06:03 +02:00
// Multilangs
2012-09-15 10:01:35 +02:00
if ( ! empty ( $conf -> global -> MAIN_MULTILANGS ))
2007-06-30 15:06:03 +02:00
{
2009-01-10 01:32:42 +01:00
if ( $this -> setMultiLangs () < 0 )
{
$this -> error = $langs -> trans ( " Error " ) . " : " . $this -> db -> error () . " - " . $sql ;
return - 2 ;
}
2007-06-30 15:06:03 +02:00
}
2012-01-30 20:08:11 +01:00
2012-03-03 19:02:44 +01:00
// Actions on extra fields (by external module or standard code)
$hookmanager -> initHooks ( array ( 'productdao' ));
$parameters = array ( 'id' => $this -> id );
$reshook = $hookmanager -> executeHooks ( 'insertExtraFields' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( empty ( $reshook ))
{
2012-11-09 19:34:05 +01:00
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
2012-11-09 19:23:46 +01:00
{
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
}
2012-03-03 19:02:44 +01:00
}
else if ( $reshook < 0 ) $error ++ ;
if ( ! $error && ! $notrigger )
2012-01-14 20:23:47 +01:00
{
// Appel des triggers
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
2012-01-14 20:23:47 +01:00
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'PRODUCT_MODIFY' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// Fin appel triggers
}
2012-07-02 12:07:42 +02:00
2012-06-08 21:38:11 +02:00
if ( ! $error && ( is_object ( $this -> oldcopy ) && $this -> oldcopy -> ref != $this -> ref ))
{
2012-07-02 19:30:37 +02:00
// We remove directory
if ( $conf -> product -> dir_output )
{
2012-06-08 21:38:11 +02:00
$olddir = $conf -> product -> dir_output . " / " . dol_sanitizeFileName ( $this -> oldcopy -> ref );
2012-07-02 19:30:37 +02:00
$newdir = $conf -> product -> dir_output . " / " . dol_sanitizeFileName ( $this -> ref );
if ( file_exists ( $olddir ))
{
2012-09-19 14:11:01 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2012-09-19 14:16:29 +02:00
$res =@ dol_move ( $olddir , $newdir );
2012-07-02 19:30:37 +02:00
if ( ! $res )
{
$this -> error = 'ErrorFailToMoveDir' ;
$error ++ ;
}
}
2012-06-08 21:38:11 +02:00
}
}
2012-07-02 12:07:42 +02:00
2012-07-02 19:30:37 +02:00
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return - $error ;
2012-06-08 21:38:11 +02:00
}
2007-06-30 15:06:03 +02:00
}
else
{
if ( $this -> db -> errno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' )
{
$this -> error = $langs -> trans ( " Error " ) . " : " . $langs -> trans ( " ErrorProductAlreadyExists " , $this -> ref );
2012-09-19 14:11:01 +02:00
$this -> db -> rollback ();
2007-06-30 15:06:03 +02:00
return - 1 ;
}
else
{
$this -> error = $langs -> trans ( " Error " ) . " : " . $this -> db -> error () . " - " . $sql ;
2012-09-19 14:11:01 +02:00
$this -> db -> rollback ();
2007-06-30 15:06:03 +02:00
return - 2 ;
}
}
}
2004-08-29 15:06:30 +02:00
2008-02-29 01:00:54 +01:00
/**
2010-11-02 12:22:41 +01:00
* Delete a product from database ( if not used )
2011-08-27 19:25:09 +02:00
*
2013-10-27 02:23:25 +02:00
* @ param int $id Product id ( usage of this is deprecated , delete should be called without parameters on a fetched object )
2011-12-19 18:24:49 +01:00
* @ return int < 0 if KO , 0 = Not possible , > 0 if OK
2008-02-29 01:00:54 +01:00
*/
2013-10-27 02:23:25 +02:00
function delete ( $id = 0 )
2008-02-29 01:00:54 +01:00
{
2009-09-18 16:57:39 +02:00
global $conf , $user , $langs ;
2011-05-18 18:20:43 +02:00
2009-09-27 19:50:59 +02:00
$error = 0 ;
2008-07-27 22:47:13 +02:00
2013-10-27 02:23:25 +02:00
// Clean parameters
if ( empty ( $id )) $id = $this -> id ;
else $this -> fetch ( $id );
// Check parameters
if ( empty ( $id ))
{
$this -> error = " Object must be fetched before calling delete " ;
return - 1 ;
}
2013-08-27 09:36:54 +02:00
if (( $this -> type == 0 && empty ( $user -> rights -> produit -> supprimer )) || ( $this -> type == 1 && empty ( $user -> rights -> service -> supprimer )))
2008-02-29 01:00:54 +01:00
{
2013-05-07 12:07:06 +02:00
$this -> error = " ErrorForbidden " ;
return 0 ;
}
2013-08-27 09:36:54 +02:00
$objectisused = $this -> isObjectUsed ( $id );
if ( empty ( $objectisused ))
{
$this -> db -> begin ();
2012-07-02 12:07:42 +02:00
2013-08-27 09:36:54 +02:00
if ( ! $error )
2008-02-29 01:00:54 +01:00
{
2013-08-27 09:36:54 +02:00
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'PRODUCT_DELETE' , $this , $user , $langs , $conf );
if ( $result < 0 ) {
$error ++ ; $this -> errors = $interface -> errors ;
2012-02-22 12:37:39 +01:00
}
2013-08-27 09:36:54 +02:00
// Fin appel triggers
}
2011-10-05 14:29:16 +02:00
2013-08-27 09:36:54 +02:00
// Delete all child tables
2013-10-15 11:11:42 +02:00
$elements = array ( 'product_fournisseur_price' , 'product_price' , 'product_lang' , 'categorie_product' , 'product_stock' );
2013-08-27 09:36:54 +02:00
foreach ( $elements as $table )
{
2012-06-08 21:38:11 +02:00
if ( ! $error )
2012-12-08 14:23:52 +01:00
{
2013-08-27 09:36:54 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE fk_product = " . $id ;
dol_syslog ( get_class ( $this ) . '::delete sql=' . $sql , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
if ( ! $result )
2012-12-10 19:06:59 +01:00
{
2012-12-08 14:23:52 +01:00
$error ++ ;
2013-08-27 09:36:54 +02:00
$this -> errors [] = $this -> db -> lasterror ();
2012-12-08 14:23:52 +01:00
dol_syslog ( get_class ( $this ) . '::delete error ' . $this -> error , LOG_ERR );
}
}
2013-08-27 09:36:54 +02:00
}
2012-12-08 14:23:52 +01:00
2013-08-27 09:36:54 +02:00
// Delete product
if ( ! $error )
{
$sqlz = " DELETE FROM " . MAIN_DB_PREFIX . " product " ;
$sqlz .= " WHERE rowid = " . $id ;
2013-10-15 11:11:42 +02:00
dol_syslog ( get_class ( $this ) . '::delete sql=' . $sqlz , LOG_DEBUG );
2013-08-27 09:36:54 +02:00
$resultz = $this -> db -> query ( $sqlz );
if ( ! $resultz )
2009-10-04 19:18:09 +02:00
{
2013-08-27 09:36:54 +02:00
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . '::delete error ' . $this -> error , LOG_ERR );
}
}
2011-08-28 14:57:23 +02:00
2013-08-27 09:36:54 +02:00
if ( ! $error )
{
// We remove directory
$ref = dol_sanitizeFileName ( $this -> ref );
if ( $conf -> product -> dir_output )
2009-10-04 19:18:09 +02:00
{
2013-08-27 09:36:54 +02:00
$dir = $conf -> product -> dir_output . " / " . $ref ;
if ( file_exists ( $dir ))
2013-08-26 14:39:35 +02:00
{
2013-08-27 09:36:54 +02:00
$res =@ dol_delete_dir_recursive ( $dir );
if ( ! $res )
{
$this -> errors [] = 'ErrorFailToDeleteDir' ;
$error ++ ;
}
2013-08-26 14:39:35 +02:00
}
2009-10-04 19:18:09 +02:00
}
2008-02-29 01:00:54 +01:00
}
2013-08-27 09:36:54 +02:00
// Remove extrafields
if (( ! $error ) && ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))) // For avoid conflicts if trigger used
{
$result = $this -> deleteExtraFields ();
if ( $result < 0 )
2009-10-04 19:18:09 +02:00
{
2013-08-27 09:36:54 +02:00
$error ++ ;
dol_syslog ( get_class ( $this ) . " ::delete error -4 " . $this -> error , LOG_ERR );
2009-10-04 19:18:09 +02:00
}
2008-02-29 01:00:54 +01:00
}
2013-08-27 09:36:54 +02:00
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
}
2008-02-29 01:00:54 +01:00
else
{
2013-08-27 09:36:54 +02:00
foreach ( $this -> errors as $errmsg )
{
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
}
$this -> db -> rollback ();
return - $error ;
2008-02-29 01:00:54 +01:00
}
}
2013-08-27 09:36:54 +02:00
else
{
$this -> error = " ErrorRecordHasChildren " ;
return 0 ;
}
2008-02-29 01:00:54 +01:00
}
2006-04-07 14:39:22 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-08-27 19:25:09 +02:00
* Update ou cree les traductions des infos produits
2011-08-28 13:53:06 +02:00
*
2011-12-19 18:24:49 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
function setMultiLangs ()
{
global $langs ;
2010-02-21 02:45:58 +01:00
2008-07-27 22:47:13 +02:00
$langs_available = $langs -> get_available_languages ();
$current_lang = $langs -> getDefaultLang ();
2006-11-30 15:23:38 +01:00
2010-02-18 13:58:42 +01:00
foreach ( $langs_available as $key => $value )
2008-07-27 22:47:13 +02:00
{
2010-02-18 13:58:42 +01:00
$sql = " SELECT rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_lang " ;
$sql .= " WHERE fk_product= " . $this -> id ;
$sql .= " AND lang=' " . $key . " ' " ;
2006-11-30 15:23:38 +01:00
2008-07-27 22:47:13 +02:00
$result = $this -> db -> query ( $sql );
2009-06-16 20:41:01 +02:00
2010-02-18 13:58:42 +01:00
if ( $key == $current_lang )
2009-06-08 12:56:54 +02:00
{
if ( $this -> db -> num_rows ( $result )) // si aucune ligne dans la base
{
2010-02-18 10:04:55 +01:00
$sql2 = " UPDATE " . MAIN_DB_PREFIX . " product_lang " ;
2011-02-24 19:11:12 +01:00
$sql2 .= " SET label=' " . $this -> db -> escape ( $this -> libelle ) . " ', " ;
$sql2 .= " description=' " . $this -> db -> escape ( $this -> description ) . " ', " ;
$sql2 .= " note=' " . $this -> db -> escape ( $this -> note ) . " ' " ;
2010-02-18 13:58:42 +01:00
$sql2 .= " WHERE fk_product= " . $this -> id . " AND lang=' " . $key . " ' " ;
2009-06-08 12:56:54 +02:00
}
else
{
2010-02-18 10:04:55 +01:00
$sql2 = " INSERT INTO " . MAIN_DB_PREFIX . " product_lang (fk_product, lang, label, description, note) " ;
2011-02-24 19:11:12 +01:00
$sql2 .= " VALUES( " . $this -> id . " ,' " . $key . " ',' " . $this -> db -> escape ( $this -> libelle );
$sql2 .= " ',' " . $this -> db -> escape ( $this -> description );
$sql2 .= " ',' " . $this -> db -> escape ( $this -> note ) . " ') " ;
2009-06-08 12:56:54 +02:00
}
2012-08-28 23:01:04 +02:00
dol_syslog ( get_class ( $this ) . '::setMultiLangs sql=' . $sql2 );
if ( ! $this -> db -> query ( $sql2 ))
{
$this -> error = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . '::setMultiLangs error=' . $this -> error , LOG_ERR );
return - 1 ;
}
2009-06-08 12:56:54 +02:00
}
2012-07-24 17:52:46 +02:00
else if ( isset ( $this -> multilangs [ " $key " ]))
2009-06-08 12:56:54 +02:00
{
if ( $this -> db -> num_rows ( $result )) // si aucune ligne dans la base
{
2010-02-18 10:04:55 +01:00
$sql2 = " UPDATE " . MAIN_DB_PREFIX . " product_lang " ;
2012-08-25 12:07:18 +02:00
$sql2 .= " SET label=' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " label " ]) . " ', " ;
2011-02-24 19:11:12 +01:00
$sql2 .= " description=' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " description " ]) . " ', " ;
$sql2 .= " note=' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " note " ]) . " ' " ;
2010-02-18 13:58:42 +01:00
$sql2 .= " WHERE fk_product= " . $this -> id . " AND lang=' " . $key . " ' " ;
2009-06-08 12:56:54 +02:00
}
else
{
2010-02-18 10:04:55 +01:00
$sql2 = " INSERT INTO " . MAIN_DB_PREFIX . " product_lang (fk_product, lang, label, description, note) " ;
2012-08-25 12:07:18 +02:00
$sql2 .= " VALUES( " . $this -> id . " ,' " . $key . " ',' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " label " ]);
2011-02-24 19:11:12 +01:00
$sql2 .= " ',' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " description " ]);
$sql2 .= " ',' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " note " ]) . " ') " ;
2009-06-08 12:56:54 +02:00
}
2009-06-16 20:41:01 +02:00
2009-06-08 12:56:54 +02:00
// on ne sauvegarde pas des champs vides
2012-08-25 12:07:18 +02:00
if ( $this -> multilangs [ " $key " ][ " label " ] || $this -> multilangs [ " $key " ][ " description " ] || $this -> multilangs [ " $key " ][ " note " ] )
2012-08-28 23:01:04 +02:00
dol_syslog ( get_class ( $this ) . '::setMultiLangs sql=' . $sql2 );
if ( ! $this -> db -> query ( $sql2 ))
{
$this -> error = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . '::setMultiLangs error=' . $this -> error , LOG_ERR );
return - 1 ;
}
2009-06-08 12:56:54 +02:00
}
2008-07-27 22:47:13 +02:00
}
return 1 ;
}
/**
2011-06-10 13:39:01 +02:00
* Load array this -> multilangs
2011-08-28 13:53:06 +02:00
*
2011-12-19 18:24:49 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2010-02-28 01:27:37 +01:00
function getMultiLangs ()
2008-07-27 22:47:13 +02:00
{
global $langs ;
2010-02-21 02:45:58 +01:00
2010-02-28 01:27:37 +01:00
$current_lang = $langs -> getDefaultLang ();
2008-07-27 22:47:13 +02:00
$sql = " SELECT lang, label, description, note " ;
2010-02-18 10:04:55 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_lang " ;
2008-07-27 22:47:13 +02:00
$sql .= " WHERE fk_product= " . $this -> id ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
while ( $obj = $this -> db -> fetch_object ( $result ) )
2009-06-08 12:56:54 +02:00
{
2010-02-21 09:53:42 +01:00
//print 'lang='.$obj->lang.' current='.$current_lang.'<br>';
2011-06-10 13:39:01 +02:00
if ( $obj -> lang == $current_lang ) // si on a les traduct. dans la langue courante on les charge en infos principales.
2009-06-08 12:56:54 +02:00
{
2012-08-25 12:07:18 +02:00
$this -> label = $obj -> label ;
2009-06-08 12:56:54 +02:00
$this -> description = $obj -> description ;
$this -> note = $obj -> note ;
2009-06-16 20:41:01 +02:00
2009-06-08 12:56:54 +02:00
}
2012-08-25 12:07:18 +02:00
$this -> multilangs [ " $obj->lang " ][ " label " ] = $obj -> label ;
2009-06-08 12:56:54 +02:00
$this -> multilangs [ " $obj->lang " ][ " description " ] = $obj -> description ;
$this -> multilangs [ " $obj->lang " ][ " note " ] = $obj -> note ;
}
2011-08-28 13:53:06 +02:00
return 1 ;
2008-07-27 22:47:13 +02:00
}
else
{
$this -> error = $langs -> trans ( " Error " ) . " : " . $this -> db -> error () . " - " . $sql ;
return - 1 ;
}
}
2006-02-27 17:15:07 +01:00
2005-06-11 14:10:25 +02:00
2006-09-12 00:26:58 +02:00
2007-06-16 15:06:05 +02:00
/**
2011-06-10 13:39:01 +02:00
* Ajoute un changement de prix en base dans l ' historique des prix
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param User $user Objet utilisateur qui modifie le prix
* @ param int $level price level to change
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2009-01-10 01:32:42 +01:00
function _log_price ( $user , $level = 0 )
2007-06-16 15:06:05 +02:00
{
2013-06-24 17:10:11 +02:00
global $conf ;
2010-08-09 21:08:22 +02:00
$now = dol_now ();
2009-01-10 01:32:42 +01:00
// Add new price
2010-08-11 20:35:31 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " product_price(price_level,date_price,fk_product,fk_user_author,price,price_ttc,price_base_type,tosell,tva_tx,recuperableonly, " ;
2013-06-24 17:10:11 +02:00
$sql .= " localtax1_tx, localtax2_tx, price_min,price_min_ttc,price_by_qty,entity) " ;
2012-03-11 21:45:17 +01:00
$sql .= " VALUES( " . ( $level ? $level : 1 ) . " , ' " . $this -> db -> idate ( $now ) . " ', " . $this -> id . " , " . $user -> id . " , " . $this -> price . " , " . $this -> price_ttc . " ,' " . $this -> price_base_type . " ', " . $this -> status . " , " . $this -> tva_tx . " , " . $this -> tva_npr . " , " ;
2013-06-24 17:10:11 +02:00
$sql .= " " . $this -> localtax1_tx . " , " . $this -> localtax2_tx . " , " . $this -> price_min . " , " . $this -> price_min_ttc . " , " . $this -> price_by_qty . " , " . $conf -> entity ;
2009-01-10 01:32:42 +01:00
$sql .= " ) " ;
2012-03-03 19:02:44 +01:00
dol_syslog ( get_class ( $this ) . " _log_price sql= " . $sql );
2009-01-10 01:32:42 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
2007-06-16 15:06:05 +02:00
{
2009-01-10 01:32:42 +01:00
$this -> error = $this -> db -> error ();
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2009-01-10 01:32:42 +01:00
return - 1 ;
2007-06-16 15:06:05 +02:00
}
else
{
2009-01-10 01:32:42 +01:00
return 1 ;
2007-06-16 15:06:05 +02:00
}
}
2005-06-11 14:10:25 +02:00
2006-11-30 15:23:38 +01:00
2006-12-05 01:47:40 +01:00
/**
2011-06-10 13:39:01 +02:00
* Delete a price line
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param User $user Object user
* @ param int $rowid Line id to delete
* @ return int < 0 if KO , > 0 if OK
2008-10-25 14:14:16 +02:00
*/
function log_price_delete ( $user , $rowid )
{
2009-01-09 21:53:03 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " product_price " ;
$sql .= " WHERE rowid= " . $rowid ;
2012-03-03 19:02:44 +01:00
dol_syslog ( get_class ( $this ) . " log_price_delete sql= " . $sql , LOG_DEBUG );
2009-01-09 21:53:03 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
2008-10-25 14:14:16 +02:00
}
2009-01-09 21:53:03 +01:00
2008-10-25 14:14:16 +02:00
/**
2011-06-10 13:39:01 +02:00
* Lit le prix pratique par un fournisseur
2013-09-28 13:24:58 +02:00
* On renseigne le couple prodfournprice / qty ou le triplet qty / product_id / fourn_ref
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $prodfournprice Id du tarif = rowid table product_fournisseur_price
* @ param double $qty Quantity asked
* @ param int $product_id Filter on a particular product id
* @ param string $fourn_ref Filter on a supplier ref
* @ return int <- 1 if KO , - 1 if qty not enough , 0 si ok mais rien trouve , id_product si ok et trouve
2008-07-27 22:47:13 +02:00
*/
2007-09-27 01:19:39 +02:00
function get_buyprice ( $prodfournprice , $qty , $product_id = 0 , $fourn_ref = 0 )
2006-12-05 01:47:40 +01:00
{
$result = 0 ;
2013-10-27 02:23:25 +02:00
2013-09-28 13:24:58 +02:00
// We do select by searching with qty and prodfournprice
2007-09-27 01:19:39 +02:00
$sql = " SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, " ;
2012-05-08 21:55:52 +02:00
$sql .= " pfp.fk_product, pfp.ref_fourn, pfp.fk_soc, pfp.tva_tx " ;
2011-10-04 01:26:07 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_fournisseur_price as pfp " ;
$sql .= " WHERE pfp.rowid = " . $prodfournprice ;
2012-05-08 21:55:52 +02:00
if ( $qty ) $sql .= " AND pfp.quantity <= " . $qty ;
2008-07-27 22:47:13 +02:00
2012-05-08 21:55:52 +02:00
dol_syslog ( get_class ( $this ) . " ::get_buyprice sql= " . $sql );
2006-12-05 01:47:40 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
2013-09-28 13:24:58 +02:00
if ( $obj && $obj -> quantity > 0 ) // If found
2006-12-05 01:47:40 +01:00
{
$this -> buyprice = $obj -> price ; // \deprecated
$this -> fourn_pu = $obj -> price / $obj -> quantity ; // Prix unitaire du produit pour le fournisseur $fourn_id
2012-05-08 21:55:52 +02:00
$this -> ref_fourn = $obj -> ref_fourn ; // Ref supplier
$this -> vatrate_supplier = $obj -> tva_tx ; // Vat ref supplier
2007-09-27 01:19:39 +02:00
$result = $obj -> fk_product ;
return $result ;
2006-12-05 01:47:40 +01:00
}
else
{
2013-09-28 13:24:58 +02:00
// We do same select again but searching with qty, ref and id product
2012-05-08 21:55:52 +02:00
$sql = " SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.fk_soc, " ;
$sql .= " pfp.fk_product, pfp.ref_fourn, pfp.tva_tx " ;
2011-10-04 01:26:07 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_fournisseur_price as pfp " ;
$sql .= " WHERE pfp.ref_fourn = ' " . $fourn_ref . " ' " ;
$sql .= " AND pfp.fk_product = " . $product_id ;
$sql .= " AND pfp.quantity <= " . $qty ;
2008-07-27 22:47:13 +02:00
$sql .= " ORDER BY pfp.quantity DESC " ;
2007-09-27 01:19:39 +02:00
$sql .= " LIMIT 1 " ;
2007-09-04 22:38:39 +02:00
2012-05-08 21:55:52 +02:00
dol_syslog ( get_class ( $this ) . " ::get_buyprice sql= " . $sql );
2006-12-05 01:47:40 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2007-09-04 22:38:39 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2013-09-28 13:24:58 +02:00
if ( $obj && $obj -> quantity > 0 ) // If found
2006-12-05 01:47:40 +01:00
{
2007-09-04 22:38:39 +02:00
$this -> buyprice = $obj -> price ; // \deprecated
$this -> fourn_pu = $obj -> price / $obj -> quantity ; // Prix unitaire du produit pour le fournisseur $fourn_id
2012-05-08 21:55:52 +02:00
$this -> ref_fourn = $obj -> ref_fourn ; // Ref supplier
$this -> vatrate_supplier = $obj -> tva_tx ; // Vat ref supplier
2007-09-27 01:19:39 +02:00
$result = $obj -> fk_product ;
return $result ;
2006-12-05 01:47:40 +01:00
}
else
{
2013-09-28 13:24:58 +02:00
return - 1 ; // Ce produit n'existe pas avec cette ref fournisseur ou existe mais qte insuffisante
2006-12-05 01:47:40 +01:00
}
}
else
{
$this -> error = $this -> db -> error ();
2012-05-08 21:55:52 +02:00
dol_syslog ( get_class ( $this ) . " ::get_buyprice " . $this -> error , LOG_ERR );
2006-12-05 01:47:40 +01:00
return - 3 ;
}
}
}
2006-11-30 15:23:38 +01:00
else
2006-12-05 01:47:40 +01:00
{
$this -> error = $this -> db -> error ();
2012-05-08 21:55:52 +02:00
dol_syslog ( get_class ( $this ) . " ::get_buyprice " . $this -> error , LOG_ERR );
2006-12-05 01:47:40 +01:00
return - 2 ;
}
}
2005-06-11 14:10:25 +02:00
2007-06-16 15:06:05 +02:00
/**
2011-06-10 13:39:01 +02:00
* Modify price of a product / Service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param double $newprice New price
* @ param string $newpricebase HT or TTC
* @ param User $user Object user that make change
* @ param double $newvat New VAT Rate
* @ param double $newminprice New price min
* @ param int $level 0 = standard , > 0 = level if multilevel prices
* @ param int $newnpr 0 = Standard vat rate , 1 = Special vat rate for French NPR VAT
2012-10-22 17:47:15 +02:00
* @ param int $newpsq 1 if it has price by quantity
2011-12-19 18:24:49 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2013-06-14 00:23:38 +02:00
function updatePrice ( $newprice , $newpricebase , $user , $newvat = '' , $newminprice = '' , $level = 0 , $newnpr = 0 , $newpsq = 0 )
2007-06-16 15:06:05 +02:00
{
global $conf , $langs ;
2009-01-25 15:30:42 +01:00
2013-06-14 00:23:38 +02:00
$id = $this -> id ;
2012-03-03 19:02:44 +01:00
dol_syslog ( get_class ( $this ) . " update_price id= " . $id . " newprice= " . $newprice . " newpricebase= " . $newpricebase . " newminprice= " . $newminprice . " level= " . $level . " npr= " . $newnpr );
2003-06-27 18:11:26 +02:00
2009-10-17 15:48:17 +02:00
// Clean parameters
2010-08-09 21:08:22 +02:00
if ( empty ( $this -> tva_tx )) $this -> tva_tx = 0 ;
if ( empty ( $newnpr )) $newnpr = 0 ;
2009-10-17 15:48:17 +02:00
// Check parameters
2008-05-14 00:53:31 +02:00
if ( $newvat == '' ) $newvat = $this -> tva_tx ;
2013-11-06 19:31:13 +01:00
if ( ! empty ( $newminprice ) && ( $newminprice > $newprice ))
{
$this -> error = 'ErrorPricCanBeLowerThanMinPrice' ;
return - 1 ;
}
2008-07-27 22:47:13 +02:00
2009-05-19 15:58:11 +02:00
if ( $newprice != '' || $newprice == 0 )
2007-06-16 15:06:05 +02:00
{
if ( $newpricebase == 'TTC' )
{
$price_ttc = price2num ( $newprice , 'MU' );
2008-05-14 00:53:31 +02:00
$price = price2num ( $newprice ) / ( 1 + ( $newvat / 100 ));
2007-06-16 15:06:05 +02:00
$price = price2num ( $price , 'MU' );
2008-10-23 23:54:30 +02:00
2009-05-19 15:58:11 +02:00
if ( $newminprice != '' || $newminprice == 0 )
2008-10-25 13:45:31 +02:00
{
2008-08-28 01:00:37 +02:00
$price_min_ttc = price2num ( $newminprice , 'MU' );
$price_min = price2num ( $newminprice ) / ( 1 + ( $newvat / 100 ));
$price_min = price2num ( $price_min , 'MU' );
}
2009-01-09 21:53:03 +01:00
else
2008-10-25 13:45:31 +02:00
{
$price_min = 0 ;
$price_min_ttc = 0 ;
}
2007-06-16 15:06:05 +02:00
}
else
{
$price = price2num ( $newprice , 'MU' );
2013-03-25 14:42:48 +01:00
$price_ttc = ( $newnpr != 1 ) ? price2num ( $newprice ) * ( 1 + ( $newvat / 100 )) : $price ;
2007-06-16 15:06:05 +02:00
$price_ttc = price2num ( $price_ttc , 'MU' );
2008-10-23 23:54:30 +02:00
2009-05-19 15:58:11 +02:00
if ( $newminprice != '' || $newminprice == 0 )
2008-10-25 13:45:31 +02:00
{
2008-08-28 01:00:37 +02:00
$price_min = price2num ( $newminprice , 'MU' );
$price_min_ttc = price2num ( $newminprice ) * ( 1 + ( $newvat / 100 ));
$price_min_ttc = price2num ( $price_min_ttc , 'MU' );
2009-11-16 01:19:30 +01:00
//print 'X'.$newminprice.'-'.$price_min;
2008-08-28 01:00:37 +02:00
}
2009-01-09 21:53:03 +01:00
else
2008-10-25 13:45:31 +02:00
{
$price_min = 0 ;
$price_min_ttc = 0 ;
}
2007-06-16 15:06:05 +02:00
}
2008-10-25 13:45:31 +02:00
//print 'x'.$id.'-'.$newprice.'-'.$newpricebase.'-'.$price.'-'.$price_ttc.'-'.$price_min.'-'.$price_min_ttc;
2010-03-29 01:26:14 +02:00
2010-03-28 17:13:17 +02:00
//Local taxes
$localtax1 = get_localtax ( $newvat , 1 );
$localtax2 = get_localtax ( $newvat , 2 );
2010-03-29 01:26:14 +02:00
if ( empty ( $localtax1 )) $localtax1 = 0 ; // If = '' then = 0
if ( empty ( $localtax2 )) $localtax2 = 0 ; // If = '' then = 0
2011-06-10 13:39:01 +02:00
// Ne pas mettre de quote sur les numeriques decimaux.
// Ceci provoque des stockages avec arrondis en base au lieu des valeurs exactes.
2008-05-14 00:53:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " product SET " ;
$sql .= " price_base_type=' " . $newpricebase . " ', " ;
$sql .= " price= " . $price . " , " ;
$sql .= " price_ttc= " . $price_ttc . " , " ;
2008-08-28 01:00:37 +02:00
$sql .= " price_min= " . $price_min . " , " ;
2008-10-23 23:54:30 +02:00
$sql .= " price_min_ttc= " . $price_min_ttc . " , " ;
2010-03-29 01:26:14 +02:00
$sql .= " localtax1_tx= " . ( $localtax1 >= 0 ? $localtax1 : 'NULL' ) . " , " ;
$sql .= " localtax2_tx= " . ( $localtax2 >= 0 ? $localtax2 : 'NULL' ) . " , " ;
2010-08-09 21:08:22 +02:00
$sql .= " tva_tx=' " . price2num ( $newvat ) . " ', " ;
$sql .= " recuperableonly=' " . $newnpr . " ' " ;
$sql .= " WHERE rowid = " . $id ;
2007-06-16 15:06:05 +02:00
2012-03-03 19:02:44 +01:00
dol_syslog ( get_class ( $this ) . " update_price sql= " . $sql , LOG_DEBUG );
2007-06-16 15:06:05 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2008-07-27 22:47:13 +02:00
$this -> price = $price ;
$this -> price_ttc = $price_ttc ;
2008-08-28 01:00:37 +02:00
$this -> price_min = $price_min ;
$this -> price_min_ttc = $price_min_ttc ;
2007-06-16 15:06:05 +02:00
$this -> price_base_type = $newpricebase ;
2008-05-14 00:53:31 +02:00
$this -> tva_tx = $newvat ;
2010-08-09 21:08:22 +02:00
$this -> tva_npr = $newnpr ;
2010-03-28 17:13:17 +02:00
//Local taxes
$this -> localtax1_tx = $localtax1 ;
2010-03-29 01:26:14 +02:00
$this -> localtax2_tx = $localtax2 ;
2012-10-22 17:47:15 +02:00
// Price by quantity
$this -> price_by_qty = $newpsq ;
2013-06-14 12:48:27 +02:00
$this -> _log_price ( $user , $level ); // Save price for level into table product_price
$this -> level = $level ; // Store level of price edited for trigger
2012-09-16 21:12:55 +02:00
2012-09-07 17:23:16 +02:00
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /core/class/interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'PRODUCT_PRICE_MODIFY' , $this , $user , $langs , $conf );
2012-09-16 21:12:55 +02:00
if ( $result < 0 )
2012-09-07 17:23:16 +02:00
{
$error ++ ; $this -> errors = $interface -> errors ;
}
2012-09-05 14:38:16 +02:00
// Fin appel triggers
2007-06-16 15:06:05 +02:00
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2007-06-16 15:06:05 +02:00
}
}
2009-01-10 01:32:42 +01:00
return 1 ;
2007-06-16 15:06:05 +02:00
}
2003-06-30 01:01:47 +02:00
2006-09-12 00:26:58 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Load a product in memory from database
2011-08-27 19:25:09 +02:00
*
2011-10-01 01:30:32 +02:00
* @ param int $id Id of product / service to load
* @ param string $ref Ref of product / service to load
* @ param string $ref_ext Ref ext of product / service to load
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2011-10-01 01:30:32 +02:00
function fetch ( $id = '' , $ref = '' , $ref_ext = '' )
2007-09-29 12:57:20 +02:00
{
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php' ;
2010-11-02 12:22:41 +01:00
2011-10-01 01:30:32 +02:00
global $langs , $conf ;
2008-07-27 22:47:13 +02:00
2012-01-31 00:51:52 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch id= " . $id . " ref= " . $ref . " ref_ext= " . $ref_ext );
2008-07-27 22:47:13 +02:00
2010-03-13 00:40:39 +01:00
// Check parameters
2011-10-01 01:30:32 +02:00
if ( ! $id && ! $ref && ! $ref_ext )
2007-09-29 12:57:20 +02:00
{
2012-07-17 01:22:19 +02:00
$this -> error = 'ErrorWrongParameters' ;
2012-01-31 00:51:52 +01:00
dol_print_error ( get_class ( $this ) . " ::fetch " . $this -> error , LOG_ERR );
2008-07-27 22:47:13 +02:00
return - 1 ;
2007-09-29 12:57:20 +02:00
}
2008-07-27 22:47:13 +02:00
2010-11-02 12:22:41 +01:00
$sql = " SELECT rowid, ref, label, description, note, customcode, fk_country, price, price_ttc, " ;
2010-08-11 20:35:31 +02:00
$sql .= " price_min, price_min_ttc, price_base_type, tva_tx, recuperableonly as tva_npr, localtax1_tx, localtax2_tx, tosell, " ;
2010-08-12 18:19:52 +02:00
$sql .= " tobuy, fk_product_type, duration, seuil_stock_alerte, canvas, " ;
2011-08-28 14:37:39 +02:00
$sql .= " weight, weight_units, length, length_units, surface, surface_units, volume, volume_units, barcode, fk_barcode_type, finished, " ;
2010-03-20 19:39:32 +01:00
$sql .= " accountancy_code_buy, accountancy_code_sell, stock, pmp, " ;
2013-05-30 16:32:48 +02:00
$sql .= " datec, tms, import_key, entity, desiredstock " ;
2008-07-27 22:47:13 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product " ;
2013-12-10 17:21:48 +01:00
if ( $id ) $sql .= " WHERE rowid = " . $this -> db -> escape ( $id );
2012-02-11 10:18:09 +01:00
else
{
2012-02-16 16:26:55 +01:00
$sql .= " WHERE entity IN ( " . getEntity ( $this -> element , 1 ) . " ) " ;
2012-02-11 10:18:09 +01:00
if ( $ref ) $sql .= " AND ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
else if ( $ref_ext ) $sql .= " AND ref_ext = ' " . $this -> db -> escape ( $ref_ext ) . " ' " ;
}
2008-07-27 22:47:13 +02:00
2012-01-31 00:51:52 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch sql= " . $sql );
2011-05-21 10:01:23 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2007-09-29 12:57:20 +02:00
{
2011-05-21 10:01:23 +02:00
if ( $this -> db -> num_rows ( $resql ) > 0 )
2007-09-29 22:31:41 +02:00
{
2012-04-06 22:39:40 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
$this -> ref = $obj -> ref ;
$this -> libelle = $obj -> label ; // TODO deprecated
$this -> label = $obj -> label ;
$this -> description = $obj -> description ;
$this -> note = $obj -> note ;
$this -> type = $obj -> fk_product_type ;
$this -> status = $obj -> tosell ;
$this -> status_buy = $obj -> tobuy ;
$this -> customcode = $obj -> customcode ;
$this -> country_id = $obj -> fk_country ;
$this -> country_code = getCountry ( $this -> country_id , 2 , $this -> db );
$this -> price = $obj -> price ;
$this -> price_ttc = $obj -> price_ttc ;
$this -> price_min = $obj -> price_min ;
$this -> price_min_ttc = $obj -> price_min_ttc ;
$this -> price_base_type = $obj -> price_base_type ;
$this -> tva_tx = $obj -> tva_tx ;
2011-05-21 10:01:23 +02:00
//! French VAT NPR
2012-04-06 22:39:40 +02:00
$this -> tva_npr = $obj -> tva_npr ;
2011-05-21 10:01:23 +02:00
//! Spanish local taxes
2012-04-06 22:39:40 +02:00
$this -> localtax1_tx = $obj -> localtax1_tx ;
$this -> localtax2_tx = $obj -> localtax2_tx ;
$this -> finished = $obj -> finished ;
$this -> duration = $obj -> duration ;
$this -> duration_value = substr ( $obj -> duration , 0 , dol_strlen ( $obj -> duration ) - 1 );
$this -> duration_unit = substr ( $obj -> duration , - 1 );
$this -> canvas = $obj -> canvas ;
$this -> weight = $obj -> weight ;
$this -> weight_units = $obj -> weight_units ;
$this -> length = $obj -> length ;
$this -> length_units = $obj -> length_units ;
$this -> surface = $obj -> surface ;
$this -> surface_units = $obj -> surface_units ;
$this -> volume = $obj -> volume ;
$this -> volume_units = $obj -> volume_units ;
$this -> barcode = $obj -> barcode ;
$this -> barcode_type = $obj -> fk_barcode_type ;
$this -> accountancy_code_buy = $obj -> accountancy_code_buy ;
$this -> accountancy_code_sell = $obj -> accountancy_code_sell ;
$this -> seuil_stock_alerte = $obj -> seuil_stock_alerte ;
2013-05-30 16:32:48 +02:00
$this -> desiredstock = $obj -> desiredstock ;
2012-04-06 22:39:40 +02:00
$this -> stock_reel = $obj -> stock ;
$this -> pmp = $obj -> pmp ;
$this -> date_creation = $obj -> datec ;
$this -> date_modification = $obj -> tms ;
$this -> import_key = $obj -> import_key ;
$this -> entity = $obj -> entity ;
2011-05-22 04:14:01 +02:00
2011-05-21 10:01:23 +02:00
$this -> db -> free ( $resql );
2011-05-22 04:14:01 +02:00
2013-12-10 19:39:28 +01:00
2011-05-21 10:01:23 +02:00
// multilangs
2012-06-08 18:17:03 +02:00
if ( ! empty ( $conf -> global -> MAIN_MULTILANGS )) $this -> getMultiLangs ();
2011-05-22 04:14:01 +02:00
2011-05-21 10:01:23 +02:00
// Load multiprices array
2012-06-08 18:17:03 +02:00
if ( ! empty ( $conf -> global -> PRODUIT_MULTIPRICES ))
2008-07-27 22:47:13 +02:00
{
2011-05-21 10:01:23 +02:00
for ( $i = 1 ; $i <= $conf -> global -> PRODUIT_MULTIPRICES_LIMIT ; $i ++ )
2008-07-27 22:47:13 +02:00
{
2011-05-21 10:01:23 +02:00
$sql = " SELECT price, price_ttc, price_min, price_min_ttc, " ;
2013-06-14 00:11:15 +02:00
$sql .= " price_base_type, tva_tx, tosell, price_by_qty, rowid, recuperableonly " ;
2011-05-21 10:01:23 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price " ;
2013-06-24 17:10:11 +02:00
$sql .= " WHERE entity IN ( " . getEntity ( 'productprice' , 1 ) . " ) " ;
$sql .= " AND price_level= " . $i ;
2012-02-11 10:18:09 +01:00
$sql .= " AND fk_product = ' " . $this -> id . " ' " ;
2011-05-21 10:01:23 +02:00
$sql .= " ORDER BY date_price DESC " ;
$sql .= " LIMIT 1 " ;
2011-09-20 15:32:16 +02:00
$resql = $this -> db -> query ( $sql );
2011-05-21 10:01:23 +02:00
if ( $resql )
{
$result = $this -> db -> fetch_array ( $resql );
2011-05-22 04:14:01 +02:00
2011-05-21 10:01:23 +02:00
$this -> multiprices [ $i ] = $result [ " price " ];
$this -> multiprices_ttc [ $i ] = $result [ " price_ttc " ];
$this -> multiprices_min [ $i ] = $result [ " price_min " ];
$this -> multiprices_min_ttc [ $i ] = $result [ " price_min_ttc " ];
$this -> multiprices_base_type [ $i ] = $result [ " price_base_type " ];
2013-06-14 12:48:27 +02:00
$this -> multiprices_tva_tx [ $i ] = $result [ " tva_tx " ];
$this -> multiprices_recuperableonly [ $i ] = $result [ " recuperableonly " ];
2012-10-22 17:47:15 +02:00
// Price by quantity
$this -> prices_by_qty [ $i ] = $result [ " price_by_qty " ];
$this -> prices_by_qty_id [ $i ] = $result [ " rowid " ];
// Récuperation de la liste des prix selon qty si flag positionné
if ( $this -> prices_by_qty [ $i ] == 1 )
{
2012-11-07 00:38:53 +01:00
$sql = " SELECT rowid,price, unitprice, quantity, remise_percent, remise " ;
2012-10-22 17:47:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price_by_qty " ;
$sql .= " WHERE fk_product_price = ' " . $this -> prices_by_qty_id [ $i ] . " ' " ;
2012-11-07 00:38:53 +01:00
$sql .= " ORDER BY quantity ASC " ;
2012-10-22 17:47:15 +02:00
$resultat = array ();
2012-12-15 11:39:47 +01:00
$resql = $this -> db -> query ( $sql );
2012-10-22 17:47:15 +02:00
if ( $resql )
{
$ii = 0 ;
while ( $result = $this -> db -> fetch_array ( $resql )) {
$resultat [ $ii ] = array ();
$resultat [ $ii ][ " rowid " ] = $result [ " rowid " ];
$resultat [ $ii ][ " price " ] = $result [ " price " ];
2012-11-07 00:38:53 +01:00
$resultat [ $ii ][ " unitprice " ] = $result [ " unitprice " ];
$resultat [ $ii ][ " quantity " ] = $result [ " quantity " ];
2012-10-25 08:18:59 +02:00
$resultat [ $ii ][ " remise_percent " ] = $result [ " remise_percent " ];
$resultat [ $ii ][ " remise " ] = $result [ " remise " ];
2012-10-22 17:47:15 +02:00
$ii ++ ;
}
$this -> prices_by_qty_list [ $i ] = $resultat ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
2011-05-21 10:01:23 +02:00
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
2008-07-27 22:47:13 +02:00
}
2012-12-01 14:38:59 +01:00
} else if ( ! empty ( $conf -> global -> PRODUIT_CUSTOMER_PRICES_BY_QTY ))
2012-10-22 17:47:15 +02:00
{
$sql = " SELECT price, price_ttc, price_min, price_min_ttc, " ;
$sql .= " price_base_type, tva_tx, tosell, price_by_qty, rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price " ;
$sql .= " WHERE fk_product = ' " . $this -> id . " ' " ;
$sql .= " ORDER BY date_price DESC " ;
$sql .= " LIMIT 1 " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$result = $this -> db -> fetch_array ( $resql );
// Price by quantity
$this -> prices_by_qty [ 0 ] = $result [ " price_by_qty " ];
$this -> prices_by_qty_id [ 0 ] = $result [ " rowid " ];
// Récuperation de la liste des prix selon qty si flag positionné
if ( $this -> prices_by_qty [ 0 ] == 1 )
{
2012-11-07 00:38:53 +01:00
$sql = " SELECT rowid,price, unitprice, quantity, remise_percent, remise " ;
2012-10-22 17:47:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price_by_qty " ;
$sql .= " WHERE fk_product_price = ' " . $this -> prices_by_qty_id [ 0 ] . " ' " ;
2012-11-07 00:38:53 +01:00
$sql .= " ORDER BY quantity ASC " ;
2012-10-22 17:47:15 +02:00
$resultat = array ();
2012-12-15 11:39:47 +01:00
$resql = $this -> db -> query ( $sql );
2012-10-22 17:47:15 +02:00
if ( $resql )
{
$ii = 0 ;
while ( $result = $this -> db -> fetch_array ( $resql )) {
$resultat [ $ii ] = array ();
$resultat [ $ii ][ " rowid " ] = $result [ " rowid " ];
$resultat [ $ii ][ " price " ] = $result [ " price " ];
2012-11-07 00:38:53 +01:00
$resultat [ $ii ][ " unitprice " ] = $result [ " unitprice " ];
$resultat [ $ii ][ " quantity " ] = $result [ " quantity " ];
2012-10-25 08:18:59 +02:00
$resultat [ $ii ][ " remise_percent " ] = $result [ " remise_percent " ];
$resultat [ $ii ][ " remise " ] = $result [ " remise " ];
2012-10-22 17:47:15 +02:00
$ii ++ ;
}
$this -> prices_by_qty_list [ 0 ] = $resultat ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
2008-07-27 22:47:13 +02:00
}
2011-05-22 04:14:01 +02:00
2013-12-10 19:39:28 +01:00
// We should not load stock at each fetch. If someone need stock, he must call load_stock after fetch.
2013-12-10 18:04:25 +01:00
//$res=$this->load_stock();
2013-12-10 19:39:28 +01:00
//return $res;
2014-02-08 01:02:09 +01:00
2013-12-10 19:39:28 +01:00
return 1 ;
2011-05-21 10:01:23 +02:00
}
else
{
return 0 ;
2008-10-23 23:54:30 +02:00
}
2008-07-27 22:47:13 +02:00
}
2006-11-30 15:23:38 +01:00
else
2008-07-27 22:47:13 +02:00
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2006-08-13 21:07:43 +02:00
return - 1 ;
2008-07-27 22:47:13 +02:00
}
2006-12-04 22:03:17 +01:00
}
2006-11-30 15:23:38 +01:00
2006-09-12 00:26:58 +02:00
2008-03-31 07:56:45 +02:00
/**
2011-06-10 13:39:01 +02:00
* Charge tableau des stats propale pour le produit / service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Id societe
* @ return array Tableau des stats
2008-07-27 22:47:13 +02:00
*/
function load_stats_propale ( $socid = 0 )
2008-03-31 07:56:45 +02:00
{
2008-07-27 22:47:13 +02:00
global $conf ;
2008-03-31 07:56:45 +02:00
global $user ;
2006-09-12 00:26:58 +02:00
2009-04-27 22:37:50 +02:00
$sql = " SELECT COUNT(DISTINCT p.fk_soc) as nb_customers, COUNT(DISTINCT p.rowid) as nb, " ;
2008-07-27 22:47:13 +02:00
$sql .= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty " ;
2009-04-27 22:37:50 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " propaldet as pd " ;
$sql .= " , " . MAIN_DB_PREFIX . " propal as p " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
2008-07-27 22:47:13 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2009-04-27 22:37:50 +02:00
$sql .= " WHERE p.rowid = pd.fk_propal " ;
$sql .= " AND p.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND p.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
$sql .= " AND pd.fk_product = " . $this -> id ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2008-07-27 22:47:13 +02:00
//$sql.= " AND pr.fk_statut != 0";
2009-04-27 22:37:50 +02:00
if ( $socid > 0 ) $sql .= " AND p.fk_soc = " . $socid ;
2008-07-27 22:47:13 +02:00
2011-09-20 15:32:16 +02:00
$result = $this -> db -> query ( $sql );
2008-07-27 22:47:13 +02:00
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> stats_propale [ 'customers' ] = $obj -> nb_customers ;
$this -> stats_propale [ 'nb' ] = $obj -> nb ;
$this -> stats_propale [ 'rows' ] = $obj -> nb_rows ;
$this -> stats_propale [ 'qty' ] = $obj -> qty ? $obj -> qty : 0 ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
/**
2011-06-10 13:39:01 +02:00
* Charge tableau des stats commande client pour le produit / service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Id societe pour filtrer sur une societe
* @ param int $filtrestatut Id statut pour filtrer sur un statut
* @ return array Tableau des stats
2008-07-27 22:47:13 +02:00
*/
function load_stats_commande ( $socid = 0 , $filtrestatut = '' )
{
global $conf , $user ;
$sql = " SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb, " ;
$sql .= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty " ;
2009-04-27 22:37:50 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " commandedet as cd " ;
$sql .= " , " . MAIN_DB_PREFIX . " commande as c " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE c.rowid = cd.fk_commande " ;
$sql .= " AND c.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND c.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
$sql .= " AND cd.fk_product = " . $this -> id ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
if ( $socid > 0 ) $sql .= " AND c.fk_soc = " . $socid ;
if ( $filtrestatut <> '' ) $sql .= " AND c.fk_statut in ( " . $filtrestatut . " ) " ;
2008-07-27 22:47:13 +02:00
2011-09-20 15:32:16 +02:00
$result = $this -> db -> query ( $sql );
2008-07-27 22:47:13 +02:00
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> stats_commande [ 'customers' ] = $obj -> nb_customers ;
$this -> stats_commande [ 'nb' ] = $obj -> nb ;
$this -> stats_commande [ 'rows' ] = $obj -> nb_rows ;
$this -> stats_commande [ 'qty' ] = $obj -> qty ? $obj -> qty : 0 ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
/**
2011-06-10 13:39:01 +02:00
* Charge tableau des stats commande fournisseur pour le produit / service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Id societe pour filtrer sur une societe
* @ param string $filtrestatut Id des statuts pour filtrer sur des statuts
* @ return array Tableau des stats
2008-07-27 22:47:13 +02:00
*/
function load_stats_commande_fournisseur ( $socid = 0 , $filtrestatut = '' )
{
global $conf , $user ;
$sql = " SELECT COUNT(DISTINCT c.fk_soc) as nb_suppliers, COUNT(DISTINCT c.rowid) as nb, " ;
$sql .= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty " ;
2009-04-27 22:37:50 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " commande_fournisseurdet as cd " ;
$sql .= " , " . MAIN_DB_PREFIX . " commande_fournisseur as c " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE c.rowid = cd.fk_commande " ;
$sql .= " AND c.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND c.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
$sql .= " AND cd.fk_product = " . $this -> id ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
if ( $socid > 0 ) $sql .= " AND c.fk_soc = " . $socid ;
if ( $filtrestatut != '' ) $sql .= " AND c.fk_statut in ( " . $filtrestatut . " ) " ; // Peut valoir 0
2008-07-27 22:47:13 +02:00
2011-09-20 15:32:16 +02:00
$result = $this -> db -> query ( $sql );
2008-07-27 22:47:13 +02:00
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> stats_commande_fournisseur [ 'suppliers' ] = $obj -> nb_suppliers ;
$this -> stats_commande_fournisseur [ 'nb' ] = $obj -> nb ;
$this -> stats_commande_fournisseur [ 'rows' ] = $obj -> nb_rows ;
$this -> stats_commande_fournisseur [ 'qty' ] = $obj -> qty ? $obj -> qty : 0 ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error () . ' sql=' . $sql ;
return - 1 ;
}
}
2008-10-23 23:54:30 +02:00
/**
2011-06-10 13:39:01 +02:00
* Charge tableau des stats expedition client pour le produit / service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Id societe pour filtrer sur une societe
* @ param int $filtrestatut Id statut pour filtrer sur un statut
* @ return array Tableau des stats
2008-10-23 23:54:30 +02:00
*/
function load_stats_sending ( $socid = 0 , $filtrestatut = '' )
{
global $conf , $user ;
2009-04-27 22:37:50 +02:00
$sql = " SELECT COUNT(DISTINCT e.fk_soc) as nb_customers, COUNT(DISTINCT e.rowid) as nb, " ;
2008-10-23 23:54:30 +02:00
$sql .= " COUNT(ed.rowid) as nb_rows, SUM(ed.qty) as qty " ;
2009-04-27 22:37:50 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " expeditiondet as ed " ;
$sql .= " , " . MAIN_DB_PREFIX . " commandedet as cd " ;
$sql .= " , " . MAIN_DB_PREFIX . " expedition as e " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE e.rowid = ed.fk_expedition " ;
$sql .= " AND e.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND e.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
$sql .= " AND ed.fk_origin_line = cd.rowid " ;
$sql .= " AND cd.fk_product = " . $this -> id ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND e.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
if ( $socid > 0 ) $sql .= " AND e.fk_soc = " . $socid ;
if ( $filtrestatut <> '' ) $sql .= " AND e.fk_statut in ( " . $filtrestatut . " ) " ;
2008-10-23 23:54:30 +02:00
2011-09-20 15:32:16 +02:00
$result = $this -> db -> query ( $sql );
2008-10-23 23:54:30 +02:00
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> stats_expedition [ 'customers' ] = $obj -> nb_customers ;
$this -> stats_expedition [ 'nb' ] = $obj -> nb ;
$this -> stats_expedition [ 'rows' ] = $obj -> nb_rows ;
$this -> stats_expedition [ 'qty' ] = $obj -> qty ? $obj -> qty : 0 ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Charge tableau des stats contrat pour le produit / service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Id societe
* @ return array Tableau des stats
2008-07-27 22:47:13 +02:00
*/
function load_stats_contrat ( $socid = 0 )
{
global $conf ;
global $user ;
$sql = " SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb, " ;
$sql .= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty " ;
2009-04-27 22:37:50 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " contratdet as cd " ;
$sql .= " , " . MAIN_DB_PREFIX . " contrat as c " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE c.rowid = cd.fk_contrat " ;
$sql .= " AND c.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND c.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
$sql .= " AND cd.fk_product = " . $this -> id ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2008-07-27 22:47:13 +02:00
//$sql.= " AND c.statut != 0";
2009-04-27 22:37:50 +02:00
if ( $socid > 0 ) $sql .= " AND c.fk_soc = " . $socid ;
2009-04-29 20:02:50 +02:00
2011-09-20 15:32:16 +02:00
$result = $this -> db -> query ( $sql );
2008-07-27 22:47:13 +02:00
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> stats_contrat [ 'customers' ] = $obj -> nb_customers ;
$this -> stats_contrat [ 'nb' ] = $obj -> nb ;
$this -> stats_contrat [ 'rows' ] = $obj -> nb_rows ;
$this -> stats_contrat [ 'qty' ] = $obj -> qty ? $obj -> qty : 0 ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error () . ' sql=' . $sql ;
return - 1 ;
}
}
/**
2011-06-10 13:39:01 +02:00
* Charge tableau des stats facture pour le produit / service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Id societe
* @ return array Tableau des stats
2008-07-27 22:47:13 +02:00
*/
function load_stats_facture ( $socid = 0 )
{
global $conf ;
global $user ;
$sql = " SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb, " ;
2009-04-27 22:37:50 +02:00
$sql .= " COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facturedet as fd " ;
$sql .= " , " . MAIN_DB_PREFIX . " facture as f " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE f.rowid = fd.fk_facture " ;
$sql .= " AND f.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND f.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
$sql .= " AND fd.fk_product = " . $this -> id ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2008-07-27 22:47:13 +02:00
//$sql.= " AND f.fk_statut != 0";
2009-04-27 22:37:50 +02:00
if ( $socid > 0 ) $sql .= " AND f.fk_soc = " . $socid ;
2008-07-27 22:47:13 +02:00
2011-09-20 15:32:16 +02:00
$result = $this -> db -> query ( $sql );
2008-07-27 22:47:13 +02:00
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> stats_facture [ 'customers' ] = $obj -> nb_customers ;
$this -> stats_facture [ 'nb' ] = $obj -> nb ;
$this -> stats_facture [ 'rows' ] = $obj -> nb_rows ;
$this -> stats_facture [ 'qty' ] = $obj -> qty ? $obj -> qty : 0 ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
/**
2011-06-10 13:39:01 +02:00
* Charge tableau des stats facture pour le produit / service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Id societe
* @ return array Tableau des stats
2008-07-27 22:47:13 +02:00
*/
function load_stats_facture_fournisseur ( $socid = 0 )
{
global $conf ;
global $user ;
$sql = " SELECT COUNT(DISTINCT f.fk_soc) as nb_suppliers, COUNT(DISTINCT f.rowid) as nb, " ;
2009-04-27 22:37:50 +02:00
$sql .= " COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facture_fourn_det as fd " ;
$sql .= " , " . MAIN_DB_PREFIX . " facture_fourn as f " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE f.rowid = fd.fk_facture_fourn " ;
$sql .= " AND f.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND f.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
$sql .= " AND fd.fk_product = " . $this -> id ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2008-07-27 22:47:13 +02:00
//$sql.= " AND f.fk_statut != 0";
2009-04-27 22:37:50 +02:00
if ( $socid > 0 ) $sql .= " AND f.fk_soc = " . $socid ;
2008-07-27 22:47:13 +02:00
2011-09-20 15:32:16 +02:00
$result = $this -> db -> query ( $sql );
2008-07-27 22:47:13 +02:00
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> stats_facture_fournisseur [ 'suppliers' ] = $obj -> nb_suppliers ;
$this -> stats_facture_fournisseur [ 'nb' ] = $obj -> nb ;
$this -> stats_facture_fournisseur [ 'rows' ] = $obj -> nb_rows ;
$this -> stats_facture_fournisseur [ 'qty' ] = $obj -> qty ? $obj -> qty : 0 ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
/**
2011-06-10 13:39:01 +02:00
* Return an array formated for showing graphs
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param string $sql Request to execute
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2009-01-09 21:53:03 +01:00
function _get_stats ( $sql , $mode )
2008-07-27 22:47:13 +02:00
{
2009-01-09 21:53:03 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2008-07-27 22:47:13 +02:00
{
2009-01-09 21:53:03 +01:00
$num = $this -> db -> num_rows ( $resql );
2008-07-27 22:47:13 +02:00
$i = 0 ;
while ( $i < $num )
2009-01-09 21:53:03 +01:00
{
$arr = $this -> db -> fetch_array ( $resql );
if ( $mode == 'byunit' ) $tab [ $arr [ 1 ]] = $arr [ 0 ]; // 1st field
if ( $mode == 'bynumber' ) $tab [ $arr [ 1 ]] = $arr [ 2 ]; // 3rd field
$i ++ ;
}
2008-07-27 22:47:13 +02:00
}
else
{
$this -> error = $this -> db -> error () . ' sql=' . $sql ;
return - 1 ;
}
$year = strftime ( '%Y' , time ());
$month = strftime ( '%m' , time ());
$result = array ();
for ( $j = 0 ; $j < 12 ; $j ++ )
{
2011-12-17 16:44:48 +01:00
$idx = ucfirst ( dol_trunc ( dol_print_date ( dol_mktime ( 12 , 0 , 0 , $month , 1 , $year ), " %b " ), 3 , 'right' , 'UTF-8' , 1 ));
2008-07-27 22:47:13 +02:00
$monthnum = sprintf ( " %02s " , $month );
$result [ $j ] = array ( $idx , isset ( $tab [ $year . $month ]) ? $tab [ $year . $month ] : 0 );
// $result[$j] = array($monthnum,isset($tab[$year.$month])?$tab[$year.$month]:0);
$month = " 0 " . ( $month - 1 );
2010-08-24 21:51:56 +02:00
if ( dol_strlen ( $month ) == 3 )
2009-01-09 21:53:03 +01:00
{
$month = substr ( $month , 1 );
}
if ( $month == 0 )
{
$month = 12 ;
$year = $year - 1 ;
}
2008-07-27 22:47:13 +02:00
}
return array_reverse ( $result );
}
/**
2011-06-10 13:39:01 +02:00
* Return nb of units or customers invoices in which product is included
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Limit count on a particular third party id
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2012-01-04 13:54:07 +01:00
function get_nb_vente ( $socid , $mode )
2008-07-27 22:47:13 +02:00
{
global $conf ;
global $user ;
2009-01-09 21:53:03 +01:00
$sql = " SELECT sum(d.qty), date_format(f.datef, '%Y%m') " ;
if ( $mode == 'bynumber' ) $sql .= " , count(DISTINCT f.rowid) " ;
2009-04-27 22:37:50 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facturedet as d, " . MAIN_DB_PREFIX . " facture as f, " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE f.rowid = d.fk_facture " ;
$sql .= " AND d.fk_product = " . $this -> id ;
$sql .= " AND f.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND f.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
if ( $socid > 0 ) $sql .= " AND f.fk_soc = $socid " ;
2010-04-16 20:02:59 +02:00
$sql .= " GROUP BY date_format(f.datef,'%Y%m') " ;
$sql .= " ORDER BY date_format(f.datef,'%Y%m') DESC " ;
2008-07-27 22:47:13 +02:00
2009-01-09 21:53:03 +01:00
return $this -> _get_stats ( $sql , $mode );
2008-07-27 22:47:13 +02:00
}
/**
2011-06-10 13:39:01 +02:00
* Return nb of units or supplier invoices in which product is included
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Limit count on a particular third party id
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2012-01-04 13:54:07 +01:00
function get_nb_achat ( $socid , $mode )
2008-07-27 22:47:13 +02:00
{
global $conf ;
global $user ;
2009-01-09 21:53:03 +01:00
$sql = " SELECT sum(d.qty), date_format(f.datef, '%Y%m') " ;
if ( $mode == 'bynumber' ) $sql .= " , count(DISTINCT f.rowid) " ;
2009-04-27 22:37:50 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facture_fourn_det as d, " . MAIN_DB_PREFIX . " facture_fourn as f, " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE f.rowid = d.fk_facture_fourn " ;
$sql .= " AND d.fk_product = " . $this -> id ;
$sql .= " AND f.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND f.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
if ( $socid > 0 ) $sql .= " AND f.fk_soc = $socid " ;
2010-04-16 20:02:59 +02:00
$sql .= " GROUP BY date_format(f.datef,'%Y%m') " ;
$sql .= " ORDER BY date_format(f.datef,'%Y%m') DESC " ;
2008-07-27 22:47:13 +02:00
2009-01-09 21:53:03 +01:00
$resarray = $this -> _get_stats ( $sql , $mode );
2008-07-27 22:47:13 +02:00
return $resarray ;
}
/**
2011-06-10 13:39:01 +02:00
* Return nb of units or proposals in which product is included
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Limit count on a particular third party id
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2012-01-04 13:54:07 +01:00
function get_nb_propal ( $socid , $mode )
2008-07-27 22:47:13 +02:00
{
global $conf ;
global $user ;
2009-01-09 21:53:03 +01:00
$sql = " SELECT sum(d.qty), date_format(p.datep, '%Y%m') " ;
if ( $mode == 'bynumber' ) $sql .= " , count(DISTINCT p.rowid) " ;
2009-04-27 22:37:50 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " propaldet as d, " . MAIN_DB_PREFIX . " propal as p, " . MAIN_DB_PREFIX . " societe as s " ;
2008-07-27 22:47:13 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2009-04-27 22:37:50 +02:00
$sql .= " WHERE p.rowid = d.fk_propal " ;
$sql .= " AND d.fk_product = " . $this -> id ;
$sql .= " AND p.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND p.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
if ( $socid > 0 ) $sql .= " AND p.fk_soc = " . $socid ;
2010-04-16 20:02:59 +02:00
$sql .= " GROUP BY date_format(p.datep,'%Y%m') " ;
$sql .= " ORDER BY date_format(p.datep,'%Y%m') DESC " ;
2008-07-27 22:47:13 +02:00
2009-01-09 21:53:03 +01:00
return $this -> _get_stats ( $sql , $mode );
2008-07-27 22:47:13 +02:00
}
/**
2011-06-10 13:39:01 +02:00
* Return nb of units or orders in which product is included
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $socid Limit count on a particular third party id
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2012-01-04 13:54:07 +01:00
function get_nb_order ( $socid , $mode )
2008-07-27 22:47:13 +02:00
{
global $conf , $user ;
2009-04-27 22:37:50 +02:00
$sql = " SELECT sum(d.qty), date_format(c.date_commande, '%Y%m') " ;
if ( $mode == 'bynumber' ) $sql .= " , count(DISTINCT c.rowid) " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " commandedet as d, " . MAIN_DB_PREFIX . " commande as c, " . MAIN_DB_PREFIX . " societe as s " ;
2008-07-27 22:47:13 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2009-04-27 22:37:50 +02:00
$sql .= " WHERE c.rowid = d.fk_commande " ;
$sql .= " AND d.fk_product = " . $this -> id ;
$sql .= " AND c.fk_soc = s.rowid " ;
2012-02-01 11:32:55 +01:00
$sql .= " AND c.entity = " . $conf -> entity ;
2009-04-27 22:37:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
if ( $socid > 0 ) $sql .= " AND c.fk_soc = " . $socid ;
2010-04-16 20:02:59 +02:00
$sql .= " GROUP BY date_format(c.date_commande,'%Y%m') " ;
$sql .= " ORDER BY date_format(c.date_commande,'%Y%m') DESC " ;
2008-07-27 22:47:13 +02:00
2009-01-09 21:53:03 +01:00
return $this -> _get_stats ( $sql , $mode );
2008-07-27 22:47:13 +02:00
}
/**
2011-06-10 13:39:01 +02:00
* Lie un produit associe au produit / service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $id_pere Id du produit auquel sera lie le produit a lier
* @ param int $id_fils Id du produit a lier
2012-01-04 13:54:07 +01:00
* @ param int $qty Quantity
2011-12-19 18:24:49 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
function add_sousproduit ( $id_pere , $id_fils , $qty )
{
2013-04-22 01:59:52 +02:00
// Clean parameters
if ( ! is_numeric ( $id_pere )) $id_pere = 0 ;
if ( ! is_numeric ( $id_fils )) $id_fils = 0 ;
$result = $this -> del_sousproduit ( $id_pere , $id_fils );
if ( $result < 0 ) return $result ;
2013-04-22 13:01:22 +02:00
2013-04-22 01:59:52 +02:00
// Check not already father of id_pere (to avoid father -> child -> father links)
$sql = 'SELECT fk_product_pere from ' . MAIN_DB_PREFIX . 'product_association' ;
$sql .= ' WHERE fk_product_pere = ' . $id_fils . ' AND fk_product_fils = ' . $id_pere ;
2008-07-27 22:47:13 +02:00
if ( ! $this -> db -> query ( $sql ))
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-27 22:47:13 +02:00
return - 1 ;
}
else
{
2013-04-22 01:59:52 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
2009-01-09 21:53:03 +01:00
{
2013-04-22 01:59:52 +02:00
$num = $this -> db -> num_rows ( $result );
if ( $num > 0 )
{
$this -> error = " isFatherOfThis " ;
return - 1 ;
}
else
2009-01-09 21:53:03 +01:00
{
2013-04-22 01:59:52 +02:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'product_association(fk_product_pere,fk_product_fils,qty)' ;
$sql .= ' VALUES (' . $id_pere . ', ' . $id_fils . ', ' . $qty . ')' ;
if ( ! $this -> db -> query ( $sql ))
2009-01-09 21:53:03 +01:00
{
2013-04-22 01:59:52 +02:00
dol_print_error ( $this -> db );
2009-01-09 21:53:03 +01:00
return - 1 ;
}
else
{
2013-04-22 01:59:52 +02:00
return 1 ;
2009-01-09 21:53:03 +01:00
}
}
}
2008-07-27 22:47:13 +02:00
}
}
/**
2011-06-10 13:39:01 +02:00
* Retire le lien entre un sousproduit et un produit / service
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $fk_parent Id du produit auquel ne sera plus lie le produit lie
* @ param int $fk_child Id du produit a ne plus lie
2013-04-22 01:59:52 +02:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2010-02-20 13:02:48 +01:00
function del_sousproduit ( $fk_parent , $fk_child )
2008-07-27 22:47:13 +02:00
{
2013-04-22 01:59:52 +02:00
if ( ! is_numeric ( $fk_parent )) $fk_parent = 0 ;
2013-06-05 16:24:32 +02:00
if ( ! is_numeric ( $fk_child )) $fk_child = 0 ;
2013-04-22 13:01:22 +02:00
2010-02-20 13:02:48 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " product_association " ;
2013-04-22 01:59:52 +02:00
$sql .= " WHERE fk_product_pere = " . $fk_parent ;
$sql .= " AND fk_product_fils = " . $fk_child ;
2013-04-22 13:01:22 +02:00
2013-04-22 01:59:52 +02:00
dol_syslog ( get_class ( $this ) . '::del_sousproduit sql=' . $sql );
2008-07-27 22:47:13 +02:00
if ( ! $this -> db -> query ( $sql ))
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-27 22:47:13 +02:00
return - 1 ;
}
2010-02-20 13:02:48 +01:00
2008-07-27 22:47:13 +02:00
return 1 ;
}
/**
2011-06-10 13:39:01 +02:00
* Verifie si c ' est un sous - produit
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $fk_parent Id du produit auquel le produit est lie
* @ param int $fk_child Id du produit lie
* @ return int < 0 si erreur , > 0 si ok
2008-07-27 22:47:13 +02:00
*/
2010-02-20 13:02:48 +01:00
function is_sousproduit ( $fk_parent , $fk_child )
2008-07-27 22:47:13 +02:00
{
2010-02-20 13:02:48 +01:00
$sql = " SELECT fk_product_pere, qty " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_association " ;
$sql .= " WHERE fk_product_pere = ' " . $fk_parent . " ' " ;
$sql .= " AND fk_product_fils = ' " . $fk_child . " ' " ;
2010-02-21 02:45:58 +01:00
2010-02-20 13:02:48 +01:00
$result = $this -> db -> query ( $sql );
if ( $result )
2008-07-27 22:47:13 +02:00
{
2010-02-20 13:02:48 +01:00
$num = $this -> db -> num_rows ( $result );
2010-02-21 02:45:58 +01:00
2010-02-20 13:02:48 +01:00
if ( $num > 0 )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> is_sousproduit_qty = $obj -> qty ;
2010-02-21 02:45:58 +01:00
2010-02-20 13:02:48 +01:00
return true ;
}
else
{
return false ;
}
2008-07-27 22:47:13 +02:00
}
else
{
2010-02-20 13:02:48 +01:00
dol_print_error ( $this -> db );
return - 1 ;
2008-07-27 22:47:13 +02:00
}
}
2010-07-11 01:13:28 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-10-05 12:51:55 +02:00
* Add a supplier price for the product .
2012-08-26 18:28:59 +02:00
* Note : Duplicate ref is accepted for different quantity only , or for different companies .
2011-08-27 19:25:09 +02:00
*
2011-10-05 12:51:55 +02:00
* @ param User $user User that make link
* @ param int $id_fourn Supplier id
* @ param string $ref_fourn Supplier ref
* @ param float $quantity Quantity minimum for price
* @ return int < 0 if KO , 0 if link already exists for this product , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2011-10-05 12:51:55 +02:00
function add_fournisseur ( $user , $id_fourn , $ref_fourn , $quantity )
2008-07-27 22:47:13 +02:00
{
2009-12-09 13:52:32 +01:00
global $conf ;
2009-12-13 23:29:13 +01:00
2011-10-04 01:26:07 +02:00
$now = dol_now ();
2011-10-05 12:51:55 +02:00
if ( $ref_fourn )
{
$sql = " SELECT rowid, fk_product " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_fournisseur_price " ;
$sql .= " WHERE fk_soc = " . $id_fourn ;
$sql .= " AND ref_fourn = ' " . $ref_fourn . " ' " ;
$sql .= " AND fk_product != " . $this -> id ;
$sql .= " AND entity = " . $conf -> entity ;
dol_syslog ( get_class ( $this ) . " ::add_fournisseur sql= " . $sql );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj )
{
// If the supplier ref already exists but for another product (duplicate ref is accepted for different quantity only or different companies)
$this -> product_id_already_linked = $obj -> fk_product ;
return - 3 ;
}
$this -> db -> free ( $resql );
}
}
$sql = " SELECT rowid " ;
2011-10-04 01:26:07 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_fournisseur_price " ;
2009-12-10 12:25:12 +01:00
$sql .= " WHERE fk_soc = " . $id_fourn ;
2011-10-05 12:51:55 +02:00
if ( $ref_fourn ) $sql .= " AND ref_fourn = ' " . $ref_fourn . " ' " ;
else $sql .= " AND (ref_fourn = '' OR ref_fourn IS NULL) " ;
$sql .= " AND quantity = ' " . $quantity . " ' " ;
$sql .= " AND fk_product = " . $this -> id ;
2009-12-09 13:52:32 +01:00
$sql .= " AND entity = " . $conf -> entity ;
2008-07-27 22:47:13 +02:00
2011-10-05 12:51:55 +02:00
dol_syslog ( get_class ( $this ) . " ::add_fournisseur sql= " . $sql );
2008-07-27 22:47:13 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2011-10-05 12:51:55 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2009-12-13 23:29:13 +01:00
2011-10-05 12:51:55 +02:00
// The reference supplier does not exist, we create it for this product.
2009-12-13 23:29:13 +01:00
if ( ! $obj )
2008-07-27 22:47:13 +02:00
{
2011-10-05 12:51:55 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " product_fournisseur_price( " ;
2009-12-09 13:52:32 +01:00
$sql .= " datec " ;
$sql .= " , entity " ;
$sql .= " , fk_product " ;
$sql .= " , fk_soc " ;
$sql .= " , ref_fourn " ;
2011-10-05 12:51:55 +02:00
$sql .= " , quantity " ;
2011-10-04 01:26:07 +02:00
$sql .= " , fk_user " ;
2012-05-08 19:34:12 +02:00
$sql .= " , tva_tx " ;
2009-12-09 13:52:32 +01:00
$sql .= " ) VALUES ( " ;
2011-10-05 12:51:55 +02:00
$sql .= " ' " . $this -> db -> idate ( $now ) . " ' " ;
2009-12-09 13:52:32 +01:00
$sql .= " , " . $conf -> entity ;
$sql .= " , " . $this -> id ;
$sql .= " , " . $id_fourn ;
$sql .= " , ' " . $ref_fourn . " ' " ;
2011-10-05 12:51:55 +02:00
$sql .= " , " . $quantity ;
2009-12-09 13:52:32 +01:00
$sql .= " , " . $user -> id ;
2012-05-08 19:34:12 +02:00
$sql .= " , 0 " ;
2009-12-09 13:52:32 +01:00
$sql .= " ) " ;
2009-01-25 15:30:42 +01:00
2012-08-26 18:28:59 +02:00
dol_syslog ( get_class ( $this ) . " ::add_fournisseur sql= " . $sql );
2008-07-27 22:47:13 +02:00
if ( $this -> db -> query ( $sql ))
{
2011-10-04 01:26:07 +02:00
$this -> product_fourn_price_id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " product_fournisseur_price " );
2008-07-27 22:47:13 +02:00
return 1 ;
}
else
{
2009-07-10 04:26:19 +02:00
$this -> error = $this -> db -> lasterror ();
2011-10-05 12:51:55 +02:00
dol_syslog ( get_class ( $this ) . " ::add_fournisseur " . $this -> error , LOG_ERR );
2008-07-27 22:47:13 +02:00
return - 1 ;
}
}
2011-10-05 12:51:55 +02:00
// If the supplier price already exists for this product and quantity
else
2008-07-27 22:47:13 +02:00
{
2011-10-04 01:26:07 +02:00
$this -> product_fourn_price_id = $obj -> rowid ;
2009-12-10 00:40:30 +01:00
return 0 ;
2008-07-27 22:47:13 +02:00
}
}
else
{
2009-07-10 04:26:19 +02:00
$this -> error = $this -> db -> lasterror ();
2008-07-27 22:47:13 +02:00
return - 2 ;
}
}
/**
2011-06-10 13:39:01 +02:00
* Renvoie la liste des fournisseurs du produit / service
2011-08-27 19:25:09 +02:00
*
2011-06-10 13:39:01 +02:00
* @ return array Tableau des id de fournisseur
2008-07-27 22:47:13 +02:00
*/
function list_suppliers ()
{
2009-12-09 13:52:32 +01:00
global $conf ;
2009-12-13 23:29:13 +01:00
2008-07-27 22:47:13 +02:00
$list = array ();
2011-10-04 01:26:07 +02:00
$sql = " SELECT p.fk_soc " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_fournisseur_price as p " ;
2008-07-27 22:47:13 +02:00
$sql .= " WHERE p.fk_product = " . $this -> id ;
2009-12-09 13:52:32 +01:00
$sql .= " AND p.entity = " . $conf -> entity ;
2008-07-27 22:47:13 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
2009-12-09 13:52:32 +01:00
{
$obj = $this -> db -> fetch_object ( $result );
$list [ $i ] = $obj -> fk_soc ;
$i ++ ;
}
2008-07-27 22:47:13 +02:00
}
return $list ;
}
/**
2011-06-10 13:39:01 +02:00
* Recopie les prix d ' un produit / service sur un autre
2011-08-27 19:25:09 +02:00
*
2012-01-04 13:54:07 +01:00
* @ param int $fromId Id product source
* @ param int $toId Id product target
* @ return nt < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
function clone_price ( $fromId , $toId )
{
2010-02-20 15:24:47 +01:00
$this -> db -> begin ();
2008-07-27 22:47:13 +02:00
// les prix
2012-01-04 13:54:07 +01:00
$sql = " INSERT " . MAIN_DB_PREFIX . " product_price ( " ;
$sql .= " fk_product, date_price, price, tva_tx, localtax1_tx, localtax2_tx, fk_user_author, tosell) " ;
$sql .= " SELECT " . $toId . " , date_price, price, tva_tx, localtax1_tx, localtax2_tx, fk_user_author, tosell " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price " ;
$sql .= " WHERE fk_product = " . $fromId ;
2010-02-21 02:45:58 +01:00
2012-01-04 13:54:07 +01:00
if ( ! $this -> db -> query ( $sql ))
2010-02-20 15:24:47 +01:00
{
$this -> db -> rollback ();
2008-07-27 22:47:13 +02:00
return - 1 ;
}
2010-02-20 15:24:47 +01:00
$this -> db -> commit ();
2008-07-27 22:47:13 +02:00
return 1 ;
}
2013-06-10 00:11:25 +02:00
function clone_associations ( $fromId , $toId )
{
$this -> db -> begin ();
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'product_association (rowid, fk_product_pere, fk_product_fils, qty)' ;
$sql .= " SELECT null, $toId , fk_product_fils, qty FROM " . MAIN_DB_PREFIX . " product_association " ;
$sql .= " WHERE fk_product_pere = ' " . $fromId . " ' " ;
if ( ! $this -> db -> query ( $sql ))
{
$this -> db -> rollback ();
return - 1 ;
}
$this -> db -> commit ();
return 1 ;
}
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Recopie les fournisseurs et prix fournisseurs d ' un produit / service sur un autre
2011-08-27 19:25:09 +02:00
*
2011-10-04 01:26:07 +02:00
* @ param int $fromId Id produit source
* @ param int $toId Id produit cible
* @ return int < 0 si erreur , > 0 si ok
2008-07-27 22:47:13 +02:00
*/
function clone_fournisseurs ( $fromId , $toId )
{
2010-02-20 15:24:47 +01:00
$this -> db -> begin ();
2012-06-08 21:33:12 +02:00
2012-04-30 15:01:25 +02:00
$now = dol_now ();
2008-07-27 22:47:13 +02:00
// les fournisseurs
2011-10-04 01:26:07 +02:00
/* $sql = " INSERT " . MAIN_DB_PREFIX . " product_fournisseur ( "
2008-07-27 22:47:13 +02:00
. " datec, fk_product, fk_soc, ref_fourn, fk_user_author ) "
2012-04-30 15:01:25 +02:00
. " SELECT ' " . $this -> db -> idate ( $now ) . " ', " . $toId . " , fk_soc, ref_fourn, fk_user_author "
2010-02-20 15:24:47 +01:00
. " FROM " . MAIN_DB_PREFIX . " product_fournisseur "
. " WHERE fk_product = " . $fromId ;
2010-02-21 02:45:58 +01:00
2010-02-20 15:24:47 +01:00
if ( ! $this -> db -> query ( $sql ) )
{
$this -> db -> rollback ();
2008-07-27 22:47:13 +02:00
return - 1 ;
2011-10-04 01:26:07 +02:00
} */
2010-02-21 02:45:58 +01:00
2008-07-27 22:47:13 +02:00
// les prix de fournisseurs.
2012-01-04 13:54:07 +01:00
$sql = " INSERT " . MAIN_DB_PREFIX . " product_fournisseur_price ( " ;
$sql .= " datec, fk_product, fk_soc, price, quantity, fk_user) " ;
2012-04-30 15:01:25 +02:00
$sql .= " SELECT ' " . $this -> db -> idate ( $now ) . " ', " . $toId . " , fk_soc, price, quantity, fk_user " ;
2012-01-04 13:54:07 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_fournisseur_price " ;
$sql .= " WHERE fk_product = " . $fromId ;
2010-02-21 02:45:58 +01:00
2011-10-04 01:26:07 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
2010-02-20 15:24:47 +01:00
{
$this -> db -> rollback ();
2008-07-27 22:47:13 +02:00
return - 1 ;
}
2011-10-04 01:26:07 +02:00
else
{
$this -> db -> commit ();
return 1 ;
}
2008-07-27 22:47:13 +02:00
}
2008-10-23 23:54:30 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Fonction recursive uniquement utilisee par get_arbo_each_prod , recompose l ' arborescence des sousproduits
2011-08-27 19:25:09 +02:00
* Define value of this -> res
*
2011-12-19 18:24:49 +01:00
* @ param array $prod Products array
2013-04-22 13:01:22 +02:00
* @ param string $compl_path Directory path of parents to add before
2011-12-19 18:24:49 +01:00
* @ param int $multiply Because each sublevel must be multiplicated by parent nb
* @ param int $level Init level
2011-06-10 13:39:01 +02:00
* @ return void
2008-07-27 22:47:13 +02:00
*/
2009-04-05 21:01:43 +02:00
function fetch_prod_arbo ( $prod , $compl_path = " " , $multiply = 1 , $level = 1 )
2008-07-27 22:47:13 +02:00
{
2010-03-04 20:42:01 +01:00
global $conf , $langs ;
2008-10-23 23:54:30 +02:00
2010-03-04 20:42:01 +01:00
$product = new Product ( $this -> db );
2013-04-22 13:01:22 +02:00
//var_dump($prod);
2013-11-14 17:16:17 +01:00
foreach ( $prod as $id_product => $desc_pere ) // $id_product is 0 (there is no mode sub_product) or an id of a sub_product
2008-07-27 22:47:13 +02:00
{
2013-11-14 17:16:17 +01:00
if ( is_array ( $desc_pere )) // If desc_pere is an array, this means it's a child
2008-10-23 23:54:30 +02:00
{
2012-07-09 12:31:21 +02:00
$id = ( ! empty ( $desc_pere [ 0 ]) ? $desc_pere [ 0 ] : '' );
$nb = ( ! empty ( $desc_pere [ 1 ]) ? $desc_pere [ 1 ] : '' );
$type = ( ! empty ( $desc_pere [ 2 ]) ? $desc_pere [ 2 ] : '' );
2013-06-05 16:24:32 +02:00
$label = ( ! empty ( $desc_pere [ 3 ]) ? $desc_pere [ 3 ] : '' );
2013-04-22 13:01:22 +02:00
if ( $multiply < 1 ) $multiply = 1 ;
2012-07-09 12:31:21 +02:00
2013-04-22 13:01:22 +02:00
//print "XXX We add id=".$id." - label=".$label." - nb=".$nb." - multiply=".$multiply." fullpath=".$compl_path.$label."\n";
2013-11-14 17:16:17 +01:00
$this -> fetch ( $id ); // Load product
$this -> load_stock (); // Load stock
2013-04-22 13:01:22 +02:00
$this -> res [] = array (
'id' => $id , // Id product
2013-11-14 17:16:17 +01:00
'ref' => $this -> ref , // Ref product
2012-07-09 12:31:21 +02:00
'nb' => $nb , // Nb of units that compose parent product
2013-04-22 13:01:22 +02:00
'nb_total' => $nb * $multiply , // Nb of units for all nb of product
2013-11-14 17:16:17 +01:00
'stock' => $this -> stock_reel , // Stock
2010-03-04 20:42:01 +01:00
'stock_alert' => $this -> seuil_stock_alerte , // Stock alert
2013-11-14 17:16:17 +01:00
'label' => $label ,
'fullpath' => $compl_path . $label , // Label
2013-05-30 16:32:48 +02:00
'type' => $type , // Nb of units that compose parent product
2013-11-14 17:16:17 +01:00
'desiredstock' => $this -> desiredstock ,
'level' => $level
2013-04-22 13:01:22 +02:00
);
2009-04-05 21:01:43 +02:00
2013-11-14 17:16:17 +01:00
// Recursive call if there is childs to child
2013-06-01 16:11:45 +02:00
if ( is_array ( $desc_pere [ 'childs' ]))
{
//print 'YYY We go down for '.$desc_pere[3]." -> \n";
$this -> fetch_prod_arbo ( $desc_pere [ 'childs' ], $compl_path . $desc_pere [ 3 ] . " -> " , $desc_pere [ 1 ] * $multiply , $level + 1 );
}
2008-10-23 23:54:30 +02:00
}
2008-07-27 22:47:13 +02:00
}
}
2008-10-23 23:54:30 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* fonction recursive uniquement utilisee par get_each_prod , ajoute chaque sousproduits dans le tableau res
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param array $prod Products array
2011-06-10 13:39:01 +02:00
* @ return void
2008-07-27 22:47:13 +02:00
*/
function fetch_prods ( $prod )
{
$this -> res ;
foreach ( $prod as $nom_pere => $desc_pere )
{
2008-10-23 23:54:30 +02:00
// on est dans une sous-categorie
2008-07-27 22:47:13 +02:00
if ( is_array ( $desc_pere ))
2008-10-23 23:54:30 +02:00
$this -> res [] = array ( $desc_pere [ 1 ], $desc_pere [ 0 ]);
2011-09-17 21:49:50 +02:00
if ( count ( $desc_pere ) > 1 )
2008-10-23 23:54:30 +02:00
{
$this -> fetch_prods ( $desc_pere );
}
2008-07-27 22:47:13 +02:00
}
}
2008-10-23 23:54:30 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* reconstruit l 'arborescence des categories sous la forme d' un tableau
2011-08-27 19:25:09 +02:00
*
2012-01-04 13:54:07 +01:00
* @ param int $multiply Because each sublevel must be multiplicated by parent nb
2011-12-19 18:24:49 +01:00
* @ return array $this -> res
2008-07-27 22:47:13 +02:00
*/
2009-04-05 21:01:43 +02:00
function get_arbo_each_prod ( $multiply = 1 )
2008-07-27 22:47:13 +02:00
{
$this -> res = array ();
2012-07-23 20:52:55 +02:00
if ( isset ( $this -> sousprods ) && is_array ( $this -> sousprods ))
2008-07-27 22:47:13 +02:00
{
2013-04-22 13:01:22 +02:00
foreach ( $this -> sousprods as $prod_name => $desc_product )
2008-10-23 23:54:30 +02:00
{
2013-04-22 13:01:22 +02:00
if ( is_array ( $desc_product )) $this -> fetch_prod_arbo ( $desc_product , " " , $multiply );
2008-10-23 23:54:30 +02:00
}
2008-07-27 22:47:13 +02:00
}
2013-04-22 13:01:22 +02:00
//var_dump($this->res);
2008-07-27 22:47:13 +02:00
return $this -> res ;
}
2008-10-23 23:54:30 +02:00
2008-07-27 22:47:13 +02:00
/**
2012-01-04 13:54:07 +01:00
* Renvoie tous les sousproduits dans le tableau res , chaque ligne de res contient : id -> qty
2011-08-27 19:25:09 +02:00
*
2011-06-10 13:39:01 +02:00
* @ return array $this -> res
2008-07-27 22:47:13 +02:00
*/
function get_each_prod ()
{
$this -> res = array ();
if ( is_array ( $this -> sousprods ))
{
foreach ( $this -> sousprods as $nom_pere => $desc_pere )
2008-10-23 23:54:30 +02:00
{
2011-09-17 21:49:50 +02:00
if ( count ( $desc_pere ) > 1 )
2008-10-23 23:54:30 +02:00
$this -> fetch_prods ( $desc_pere );
2008-07-27 22:47:13 +02:00
2008-10-23 23:54:30 +02:00
}
sort ( $this -> res );
2008-07-27 22:47:13 +02:00
}
return $this -> res ;
}
2011-08-27 19:25:09 +02:00
2011-08-18 18:16:04 +02:00
/**
* Return all Father products fo current product
2011-08-27 19:25:09 +02:00
*
2011-08-18 18:16:04 +02:00
* @ return array prod
*/
function getFather ()
{
$sql = " SELECT p.label as label,p.rowid,pa.fk_product_pere as id,p.fk_product_type " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_association as pa, " ;
$sql .= " " . MAIN_DB_PREFIX . " product as p " ;
$sql .= " WHERE p.rowid = pa.fk_product_pere " ;
$sql .= " AND pa.fk_product_fils= " . $this -> id ;
$res = $this -> db -> query ( $sql );
if ( $res )
{
$prods = array ();
2011-09-20 14:56:06 +02:00
while ( $record = $this -> db -> fetch_array ( $res ))
2011-08-18 18:16:04 +02:00
{
$prods [ $record [ 'id' ]][ 'id' ] = $record [ 'rowid' ];
$prods [ $record [ 'id' ]][ 'label' ] = $this -> db -> escape ( $record [ 'label' ]);
$prods [ $record [ 'id' ]][ 'fk_product_type' ] = $record [ 'fk_product_type' ];
}
return $prods ;
}
else
{
2012-01-04 13:54:07 +01:00
dol_print_error ( $this -> db );
2011-08-18 18:16:04 +02:00
return - 1 ;
}
}
2011-08-27 19:25:09 +02:00
2008-07-27 22:47:13 +02:00
/**
2013-04-22 13:01:22 +02:00
* Return all direct parent products fo current product
2011-08-27 19:25:09 +02:00
*
2011-06-10 13:39:01 +02:00
* @ return array prod
2008-07-27 22:47:13 +02:00
*/
2010-02-21 08:47:39 +01:00
function getParent ()
2008-07-27 22:47:13 +02:00
{
2010-03-04 20:42:01 +01:00
$sql = " SELECT p.label as label,p.rowid,pa.fk_product_pere as id,p.fk_product_type " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_association as pa, " ;
$sql .= " " . MAIN_DB_PREFIX . " product as p " ;
2010-02-20 15:24:47 +01:00
$sql .= " WHERE p.rowid = pa.fk_product_pere " ;
$sql .= " AND p.rowid = " . $this -> id ;
2009-04-05 21:01:43 +02:00
2010-02-20 15:24:47 +01:00
$res = $this -> db -> query ( $sql );
2008-07-27 22:47:13 +02:00
if ( $res )
{
$prods = array ();
2011-09-20 14:56:06 +02:00
while ( $record = $this -> db -> fetch_array ( $res ))
2009-04-05 21:01:43 +02:00
{
2011-02-24 19:11:12 +01:00
$prods [ $this -> db -> escape ( $record [ 'label' ])] = array ( 0 => $record [ 'id' ]);
2009-04-05 21:01:43 +02:00
}
return $prods ;
2008-07-27 22:47:13 +02:00
}
else
{
2012-01-04 13:54:07 +01:00
dol_print_error ( $this -> db );
2008-07-27 22:47:13 +02:00
return - 1 ;
}
}
/**
2013-04-22 13:01:22 +02:00
* Return childs of product $id
2011-08-27 19:25:09 +02:00
*
2013-04-22 13:01:22 +02:00
* @ param int $id Id of product to search childs of
2011-12-19 18:24:49 +01:00
* @ return array Prod
2008-07-27 22:47:13 +02:00
*/
2013-04-22 13:01:22 +02:00
function getChildsArbo ( $id )
2008-07-27 22:47:13 +02:00
{
2010-03-04 20:42:01 +01:00
$sql = " SELECT p.rowid, p.label as label, pa.qty as qty, pa.fk_product_fils as id, p.fk_product_type " ;
2010-02-21 08:38:01 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product as p " ;
2010-02-20 15:24:47 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " product_association as pa " ;
$sql .= " WHERE p.rowid = pa.fk_product_fils " ;
2013-04-22 13:01:22 +02:00
$sql .= " AND pa.fk_product_pere = " . $id ;
$sql .= " AND pa.fk_product_fils != " . $id ; // This should not happens, it is to avoid infinite loop if it happens
2008-07-27 22:47:13 +02:00
2013-02-19 21:10:31 +01:00
dol_syslog ( get_class ( $this ) . '::getChildsArbo sql=' . $sql );
2010-02-20 15:24:47 +01:00
$res = $this -> db -> query ( $sql );
2008-07-27 22:47:13 +02:00
if ( $res )
{
$prods = array ();
2010-02-20 15:24:47 +01:00
while ( $rec = $this -> db -> fetch_array ( $res ))
2009-04-05 21:01:43 +02:00
{
2013-02-08 21:21:25 +01:00
$prods [ $rec [ 'rowid' ]] = array ( 0 => $rec [ 'id' ], 1 => $rec [ 'qty' ], 2 => $rec [ 'fk_product_type' ], 3 => $this -> db -> escape ( $rec [ 'label' ]));
2011-02-24 19:11:12 +01:00
//$prods[$this->db->escape($rec['label'])]= array(0=>$rec['id'],1=>$rec['qty'],2=>$rec['fk_product_type']);
2013-02-08 21:21:25 +01:00
//$prods[$this->db->escape($rec['label'])]= array(0=>$rec['id'],1=>$rec['qty']);
2010-03-04 20:42:01 +01:00
$listofchilds = $this -> getChildsArbo ( $rec [ 'id' ]);
foreach ( $listofchilds as $keyChild => $valueChild )
{
2013-04-22 13:01:22 +02:00
$prods [ $rec [ 'rowid' ]][ 'childs' ][ $keyChild ] = $valueChild ;
2010-03-04 20:42:01 +01:00
}
2009-04-05 21:01:43 +02:00
}
2011-07-29 22:47:34 +02:00
2009-04-05 21:01:43 +02:00
return $prods ;
2008-07-27 22:47:13 +02:00
}
else
{
2012-01-04 13:54:07 +01:00
dol_print_error ( $this -> db );
2008-07-27 22:47:13 +02:00
return - 1 ;
}
}
2009-04-05 21:01:43 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Return tree of all subproducts for product . Tree contains id , name and quantity .
2011-08-27 19:25:09 +02:00
* Set this -> sousprods
*
2011-06-10 13:39:01 +02:00
* @ return void
2008-07-27 22:47:13 +02:00
*/
2009-04-05 21:01:43 +02:00
function get_sousproduits_arbo ()
2008-07-27 22:47:13 +02:00
{
2010-02-21 08:47:39 +01:00
$parent = $this -> getParent ();
2013-04-22 13:01:22 +02:00
foreach ( $parent as $key => $value ) // key=label, value[0]=id
2008-07-27 22:47:13 +02:00
{
2010-02-21 08:47:39 +01:00
foreach ( $this -> getChildsArbo ( $value [ 0 ]) as $keyChild => $valueChild )
2009-01-29 21:56:14 +01:00
{
2010-02-21 08:47:39 +01:00
$parent [ $key ][ $keyChild ] = $valueChild ;
2009-01-29 21:56:14 +01:00
}
2008-07-27 22:47:13 +02:00
}
2013-04-22 13:01:22 +02:00
foreach ( $parent as $key => $value ) // key=label, value is array of childs
2008-07-27 22:47:13 +02:00
{
2010-02-21 08:47:39 +01:00
$this -> sousprods [ $key ] = $value ;
2008-07-27 22:47:13 +02:00
}
}
/**
2011-12-19 18:24:49 +01:00
* Return clicable link of object ( with eventually picto )
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $withpicto Add picto into link
* @ param string $option Where point the link
* @ param int $maxlength Maxlength of ref
* @ return string String with URL
2008-07-27 22:47:13 +02:00
*/
function getNomUrl ( $withpicto = 0 , $option = '' , $maxlength = 0 )
{
global $langs ;
$result = '' ;
2008-12-28 21:43:24 +01:00
if ( $option == 'supplier' )
{
$lien = '<a href="' . DOL_URL_ROOT . '/product/fournisseurs.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
2011-05-22 04:14:01 +02:00
else if ( $option == 'stock' )
{
$lien = '<a href="' . DOL_URL_ROOT . '/product/stock/product.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
2011-09-07 21:18:28 +02:00
else if ( $option == 'composition' )
{
$lien = '<a href="' . DOL_URL_ROOT . '/product/composition/fiche.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
2013-06-05 16:24:32 +02:00
else if ( $option == 'category' )
{
$lien = '<a href="' . DOL_URL_ROOT . '/categories/categorie.php?id=' . $this -> id . '&type=0">' ;
2013-04-22 13:01:22 +02:00
}
2011-09-07 21:18:28 +02:00
else
2008-12-28 21:43:24 +01:00
{
$lien = '<a href="' . DOL_URL_ROOT . '/product/fiche.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
2008-07-27 22:47:13 +02:00
$newref = $this -> ref ;
2009-02-20 23:53:15 +01:00
if ( $maxlength ) $newref = dol_trunc ( $newref , $maxlength , 'middle' );
2008-07-27 22:47:13 +02:00
if ( $withpicto ) {
if ( $this -> type == 0 ) $result .= ( $lien . img_object ( $langs -> trans ( " ShowProduct " ) . ' ' . $this -> ref , 'product' ) . $lienfin . ' ' );
if ( $this -> type == 1 ) $result .= ( $lien . img_object ( $langs -> trans ( " ShowService " ) . ' ' . $this -> ref , 'service' ) . $lienfin . ' ' );
}
$result .= $lien . $newref . $lienfin ;
return $result ;
}
/**
2011-12-19 18:24:49 +01:00
* Return label of status of object
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
* @ param int $type 0 = Shell , 1 = Buy
* @ return string Label of status
2008-07-27 22:47:13 +02:00
*/
2010-08-12 18:19:52 +02:00
function getLibStatut ( $mode = 0 , $type = 0 )
2008-07-27 22:47:13 +02:00
{
2010-08-12 18:19:52 +02:00
if ( $type == 0 )
return $this -> LibStatut ( $this -> status , $mode , $type );
else
return $this -> LibStatut ( $this -> status_buy , $mode , $type );
2008-07-27 22:47:13 +02:00
}
/**
2011-12-19 18:24:49 +01:00
* Return label of a given status
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param int $status Statut
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
* @ param int $type 0 = Status " to sell " , 1 = Status " to buy "
* @ return string Label of status
2008-07-27 22:47:13 +02:00
*/
2010-08-12 18:19:52 +02:00
function LibStatut ( $status , $mode = 0 , $type = 0 )
2008-07-27 22:47:13 +02:00
{
global $langs ;
$langs -> load ( 'products' );
2010-08-19 22:58:08 +02:00
2008-07-27 22:47:13 +02:00
if ( $mode == 0 )
{
2010-08-12 18:19:52 +02:00
if ( $status == 0 ) return ( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSellShort' ) : $langs -> trans ( 'ProductStatusNotOnBuyShort' ));
if ( $status == 1 ) return ( $type == 0 ? $langs -> trans ( 'ProductStatusOnSellShort' ) : $langs -> trans ( 'ProductStatusOnBuyShort' ));
2008-07-27 22:47:13 +02:00
}
if ( $mode == 1 )
{
2010-08-12 18:19:52 +02:00
if ( $status == 0 ) return ( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSell' ) : $langs -> trans ( 'ProductStatusNotOnBuy' ));
if ( $status == 1 ) return ( $type == 0 ? $langs -> trans ( 'ProductStatusOnSell' ) : $langs -> trans ( 'ProductStatusOnBuy' ));
2008-07-27 22:47:13 +02:00
}
if ( $mode == 2 )
{
2010-08-19 22:58:08 +02:00
if ( $status == 0 ) return img_picto ( $langs -> trans ( 'ProductStatusNotOnSell' ), 'statut5' ) . ' ' . ( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSellShort' ) : $langs -> trans ( 'ProductStatusNotOnBuyShort' ));
if ( $status == 1 ) return img_picto ( $langs -> trans ( 'ProductStatusOnSell' ), 'statut4' ) . ' ' . ( $type == 0 ? $langs -> trans ( 'ProductStatusOnSellShort' ) : $langs -> trans ( 'ProductStatusOnBuyShort' ));
2008-07-27 22:47:13 +02:00
}
if ( $mode == 3 )
{
2010-08-19 23:19:51 +02:00
if ( $status == 0 ) return img_picto (( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSell' ) : $langs -> trans ( 'ProductStatusNotOnBuy' )), 'statut5' );
if ( $status == 1 ) return img_picto (( $type == 0 ? $langs -> trans ( 'ProductStatusOnSell' ) : $langs -> trans ( 'ProductStatusOnBuy' )), 'statut4' );
2008-07-27 22:47:13 +02:00
}
if ( $mode == 4 )
{
2010-08-12 18:19:52 +02:00
if ( $status == 0 ) return img_picto ( $langs -> trans ( 'ProductStatusNotOnSell' ), 'statut5' ) . ' ' . ( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSell' ) : $langs -> trans ( 'ProductStatusNotOnBuy' ));
if ( $status == 1 ) return img_picto ( $langs -> trans ( 'ProductStatusOnSell' ), 'statut4' ) . ' ' . ( $type == 0 ? $langs -> trans ( 'ProductStatusOnSell' ) : $langs -> trans ( 'ProductStatusOnBuy' ));
2008-07-27 22:47:13 +02:00
}
if ( $mode == 5 )
{
2010-08-19 22:58:08 +02:00
if ( $status == 0 ) return ( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSellShort' ) : $langs -> trans ( 'ProductStatusNotOnBuyShort' )) . ' ' . img_picto (( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSell' ) : $langs -> trans ( 'ProductStatusNotOnBuy' )), 'statut5' );
if ( $status == 1 ) return ( $type == 0 ? $langs -> trans ( 'ProductStatusOnSellShort' ) : $langs -> trans ( 'ProductStatusOnBuyShort' )) . ' ' . img_picto (( $type == 0 ? $langs -> trans ( 'ProductStatusOnSell' ) : $langs -> trans ( 'ProductStatusOnBuy' )), 'statut4' );
2008-07-27 22:47:13 +02:00
}
return $langs -> trans ( 'Unknown' );
}
2009-01-09 21:53:03 +01:00
2008-12-07 22:38:33 +01:00
/**
2011-06-10 13:39:01 +02:00
* Retourne le libelle du finished du produit
2011-08-27 19:25:09 +02:00
*
2011-06-10 13:39:01 +02:00
* @ return string Libelle
2009-01-09 21:53:03 +01:00
*/
2008-12-07 22:38:33 +01:00
function getLibFinished ()
{
global $langs ;
$langs -> load ( 'products' );
2009-01-09 21:53:03 +01:00
2008-12-07 22:38:33 +01:00
if ( $this -> finished == '0' ) return $langs -> trans ( " RowMaterial " );
if ( $this -> finished == '1' ) return $langs -> trans ( " Finished " );
return '' ;
}
2008-07-27 22:47:13 +02:00
2005-06-11 14:10:25 +02:00
2008-03-31 07:56:45 +02:00
/**
2011-06-10 13:39:01 +02:00
* Adjust stock in a warehouse for product
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param User $user user asking change
* @ param int $id_entrepot id of warehouse
* @ param double $nbpiece nb of units
* @ param int $movement 0 = add , 1 = remove
* @ param string $label Label of stock movement
* @ param double $price Price to use for stock eval
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2010-07-14 17:41:39 +02:00
function correct_stock ( $user , $id_entrepot , $nbpiece , $movement , $label = '' , $price = 0 )
2008-03-31 07:56:45 +02:00
{
if ( $id_entrepot )
{
2009-02-06 07:26:45 +01:00
$this -> db -> begin ();
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php' ;
2006-11-30 15:23:38 +01:00
2010-07-14 17:41:39 +02:00
$op [ 0 ] = " + " . trim ( $nbpiece );
$op [ 1 ] = " - " . trim ( $nbpiece );
$movementstock = new MouvementStock ( $this -> db );
$result = $movementstock -> _create ( $user , $this -> id , $id_entrepot , $op [ $movement ], $movement , $price , $label );
2009-02-06 07:26:45 +01:00
if ( $result >= 0 )
{
$this -> db -> commit ();
return 1 ;
}
2008-03-31 07:56:45 +02:00
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-03-31 07:56:45 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
}
2004-08-29 15:06:30 +02:00
2008-03-31 07:56:45 +02:00
/**
2011-05-18 18:20:43 +02:00
* Load information about stock of a product into stock_warehouse [] and stock_reel
2011-08-27 19:25:09 +02:00
*
2012-01-31 00:51:52 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
function load_stock ()
{
$this -> stock_reel = 0 ;
2014-04-12 01:45:18 +02:00
$this -> stock_warehouse = array ();
2008-07-27 22:47:13 +02:00
2012-07-02 12:07:42 +02:00
$sql = " SELECT ps.reel, ps.fk_entrepot, ps.pmp " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_stock as ps " ;
$sql .= " , " . MAIN_DB_PREFIX . " entrepot as w " ;
2012-07-26 20:52:05 +02:00
$sql .= " WHERE w.entity IN ( " . getEntity ( 'warehouse' , 1 ) . " ) " ;
2012-07-02 12:07:42 +02:00
$sql .= " AND w.rowid = ps.fk_entrepot " ;
$sql .= " AND ps.fk_product = " . $this -> id ;
2009-02-06 07:26:45 +01:00
2012-01-31 00:51:52 +01:00
dol_syslog ( get_class ( $this ) . " ::load_stock sql= " . $sql );
2011-09-20 15:32:16 +02:00
$result = $this -> db -> query ( $sql );
2008-07-27 22:47:13 +02:00
if ( $result )
{
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
if ( $num > 0 )
{
2010-06-17 22:24:11 +02:00
while ( $i < $num )
2008-07-27 22:47:13 +02:00
{
2010-07-14 17:41:39 +02:00
$row = $this -> db -> fetch_object ( $result );
2013-01-19 14:32:37 +01:00
$this -> stock_warehouse [ $row -> fk_entrepot ] = new stdClass ();
2010-07-14 17:41:39 +02:00
$this -> stock_warehouse [ $row -> fk_entrepot ] -> real = $row -> reel ;
$this -> stock_warehouse [ $row -> fk_entrepot ] -> pmp = $row -> pmp ;
$this -> stock_reel += $row -> reel ;
2008-07-27 22:47:13 +02:00
$i ++ ;
}
}
$this -> db -> free ( $result );
return 1 ;
}
else
{
2012-01-31 00:51:52 +01:00
$this -> error = $this -> db -> lasterror ();
2008-07-27 22:47:13 +02:00
return - 1 ;
}
}
/**
2011-06-10 13:39:01 +02:00
* Deplace fichier uploade sous le nom $files dans le repertoire sdir
2011-08-27 19:25:09 +02:00
*
2012-01-04 13:54:07 +01:00
* @ param string $sdir Repertoire destination finale
* @ param string $file Nom du fichier uploade
* @ param int $maxWidth Largeur maximum que dois faire la miniature ( 160 par defaut )
* @ param int $maxHeight Hauteur maximum que dois faire la miniature ( 120 par defaut )
* @ return void
2008-07-27 22:47:13 +02:00
*/
function add_photo ( $sdir , $file , $maxWidth = 160 , $maxHeight = 120 )
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2010-05-01 16:28:48 +02:00
2010-02-13 23:20:32 +01:00
$dir = $sdir . '/' . get_exdir ( $this -> id , 2 ) . $this -> id . " /photos " ;
2008-07-27 22:47:13 +02:00
2012-02-19 18:34:22 +01:00
dol_mkdir ( $dir );
2008-07-27 22:47:13 +02:00
2009-12-15 12:30:49 +01:00
$dir_osencoded = $dir ;
2010-02-13 23:20:32 +01:00
if ( is_dir ( $dir_osencoded ))
2008-07-27 22:47:13 +02:00
{
2010-02-13 23:20:32 +01:00
$originImage = $dir . '/' . $file [ 'name' ];
2008-08-04 02:30:52 +02:00
2008-07-27 22:47:13 +02:00
// Cree fichier en taille origine
$result = dol_move_uploaded_file ( $file [ 'tmp_name' ], $originImage , 1 );
2009-12-15 12:30:49 +01:00
if ( file_exists ( dol_osencode ( $originImage )))
2008-07-27 22:47:13 +02:00
{
// Cree fichier en taille vignette
$this -> add_thumb ( $originImage , $maxWidth , $maxHeight );
}
}
}
2008-04-05 16:18:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Build thumb
2011-08-27 19:25:09 +02:00
*
2012-01-04 13:54:07 +01:00
* @ param string $file Chemin du fichier d ' origine
* @ param int $maxWidth Largeur maximum que dois faire la miniature ( 160 par defaut )
* @ param int $maxHeight Hauteur maximum que dois faire la miniature ( 120 par defaut )
* @ return void
2008-07-27 22:47:13 +02:00
*/
2008-04-05 16:18:13 +02:00
function add_thumb ( $file , $maxWidth = 160 , $maxHeight = 120 )
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php' ;
2008-07-27 22:47:13 +02:00
2009-12-15 12:30:49 +01:00
$file_osencoded = dol_osencode ( $file );
if ( file_exists ( $file_osencoded ))
2008-04-05 16:18:13 +02:00
{
vignette ( $file , $maxWidth , $maxHeight );
}
}
2005-04-02 15:56:33 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Deplace fichier recupere sur internet ( utilise pour interface avec OSC )
2011-08-27 19:25:09 +02:00
*
2012-01-04 13:54:07 +01:00
* @ param string $sdir Repertoire destination finale
* @ param string $file url de l ' image
* @ return void
2008-07-27 22:47:13 +02:00
*/
2009-12-15 12:30:49 +01:00
function add_photo_web ( $sdir , $file )
2008-07-27 22:47:13 +02:00
{
$dir = $sdir . '/' . get_exdir ( $this -> id , 2 ) . $this -> id . " / " ;
$dir .= " photos/ " ;
2009-12-15 12:30:49 +01:00
$dir_osencoded = dol_osencode ( $dir );
if ( ! file_exists ( $dir_osencoded ))
2008-07-27 22:47:13 +02:00
{
2009-12-15 12:30:49 +01:00
dol_syslog ( " Product Create " . $dir );
2012-02-19 18:34:22 +01:00
dol_mkdir ( $dir );
2008-07-27 22:47:13 +02:00
}
2009-12-15 12:30:49 +01:00
if ( file_exists ( $dir_osencoded ))
2008-07-27 22:47:13 +02:00
{
// Cree fichier en taille vignette
2011-09-03 02:14:27 +02:00
// TODO A faire
2008-07-27 22:47:13 +02:00
// Cree fichier en taille origine
2011-05-04 15:37:19 +02:00
$content = @ file_get_contents ( $file );
if ( $content )
2011-05-12 00:53:40 +02:00
{
2011-05-04 15:37:19 +02:00
$nom = basename ( $file );
$im = fopen ( dol_osencode ( $dir . $nom ), 'wb' );
fwrite ( $im , $content );
fclose ( $im );
}
2008-07-27 22:47:13 +02:00
}
}
2006-09-12 00:26:58 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Affiche la premiere photo du produit
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param string $sdir Repertoire a scanner
* @ return boolean true si photo dispo , false sinon
2008-07-27 22:47:13 +02:00
*/
function is_photo_available ( $sdir )
{
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2009-10-29 03:15:37 +01:00
2008-07-27 22:47:13 +02:00
$pdir = get_exdir ( $this -> id , 2 ) . $this -> id . " /photos/ " ;
$dir = $sdir . '/' . $pdir ;
2006-09-12 00:26:58 +02:00
2008-07-27 22:47:13 +02:00
$nbphoto = 0 ;
2009-12-15 12:30:49 +01:00
$dir_osencoded = dol_osencode ( $dir );
if ( file_exists ( $dir_osencoded ))
2008-07-27 22:47:13 +02:00
{
2009-12-15 12:30:49 +01:00
$handle = opendir ( $dir_osencoded );
2010-12-15 19:15:08 +01:00
if ( is_resource ( $handle ))
2008-07-27 22:47:13 +02:00
{
2010-12-15 19:15:08 +01:00
while (( $file = readdir ( $handle )) != false )
{
if ( ! utf8_check ( $file )) $file = utf8_encode ( $file ); // To be sure data is stored in UTF8 in memory
if ( dol_is_file ( $dir . $file )) return true ;
}
2008-07-27 22:47:13 +02:00
}
}
return false ;
}
/**
2011-12-07 15:06:29 +01:00
* Show photos of a product ( nbmax maximum ), into several columns
2011-12-07 12:58:39 +01:00
* TODO Move this into html . formproduct . class . php
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param string $sdir Directory to scan
* @ param int $size 0 = original size , 1 use thumbnail if possible
* @ param int $nbmax Nombre maximum de photos ( 0 = pas de max )
* @ param int $nbbyrow Nombre vignettes par ligne ( si mode vignette )
* @ param int $showfilename 1 = Show filename
* @ param int $showaction 1 = Show icon with action links ( resize , delete )
* @ param int $maxHeight Max height of image when size = 1
* @ param int $maxWidth Max width of image when size = 1
* @ return string Html code to show photo . Number of photos shown is saved in this -> nbphoto
2008-07-27 22:47:13 +02:00
*/
2011-12-05 19:41:38 +01:00
function show_photos ( $sdir , $size = 0 , $nbmax = 0 , $nbbyrow = 5 , $showfilename = 0 , $showaction = 0 , $maxHeight = 120 , $maxWidth = 160 )
2008-07-27 22:47:13 +02:00
{
2010-02-21 04:09:45 +01:00
global $conf , $user , $langs ;
2010-02-20 16:07:46 +01:00
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php' ;
2009-10-29 03:15:37 +01:00
2008-07-27 22:47:13 +02:00
$pdir = get_exdir ( $this -> id , 2 ) . $this -> id . " /photos/ " ;
$dir = $sdir . '/' . $pdir ;
$dirthumb = $dir . 'thumbs/' ;
$pdirthumb = $pdir . 'thumbs/' ;
2011-05-12 00:53:40 +02:00
$return = '<!-- Photo -->' . " \n " ;
2008-07-27 22:47:13 +02:00
$nbphoto = 0 ;
2009-12-15 12:30:49 +01:00
$dir_osencoded = dol_osencode ( $dir );
if ( file_exists ( $dir_osencoded ))
2008-07-27 22:47:13 +02:00
{
2009-12-15 12:30:49 +01:00
$handle = opendir ( $dir_osencoded );
2010-12-15 19:15:08 +01:00
if ( is_resource ( $handle ))
{
while (( $file = readdir ( $handle )) != false )
{
$photo = '' ;
if ( ! utf8_check ( $file )) $file = utf8_encode ( $file ); // To be sure file is stored in UTF8 in memory
2012-07-03 21:07:54 +02:00
if ( dol_is_file ( $dir . $file ) && preg_match ( '/(\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i' , $dir . $file ))
2010-12-15 19:15:08 +01:00
{
$nbphoto ++ ;
$photo = $file ;
$viewfilename = $file ;
if ( $size == 1 ) { // Format vignette
// On determine nom du fichier vignette
$photo_vignette = '' ;
2012-07-03 21:07:54 +02:00
if ( preg_match ( '/(\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i' , $photo , $regs )) {
$photo_vignette = preg_replace ( '/' . $regs [ 0 ] . '/i' , '' , $photo ) . " _small " . $regs [ 0 ];
2010-12-15 19:15:08 +01:00
if ( ! dol_is_file ( $dirthumb . $photo_vignette )) $photo_vignette = '' ;
}
// Get filesize of original file
$imgarray = dol_getImageSize ( $dir . $photo );
if ( $nbbyrow && $nbphoto == 1 ) $return .= '<table width="100%" valign="top" align="center" border="0" cellpadding="2" cellspacing="2">' ;
if ( $nbbyrow && ( $nbphoto % $nbbyrow == 1 )) $return .= '<tr align=center valign=middle border=1>' ;
if ( $nbbyrow ) $return .= '<td width="' . ceil ( 100 / $nbbyrow ) . '%" class="photo">' ;
$return .= " \n " ;
2012-08-08 20:46:50 +02:00
$return .= '<a href="' . DOL_URL_ROOT . '/viewimage.php?modulepart=product&entity=' . $this -> entity . '&file=' . urlencode ( $pdir . $photo ) . '" class="aphoto" target="_blank">' ;
2010-12-15 19:15:08 +01:00
2011-12-05 19:41:38 +01:00
// Show image (width height=$maxHeight)
2010-12-15 19:15:08 +01:00
// Si fichier vignette disponible et image source trop grande, on utilise la vignette, sinon on utilise photo origine
$alt = $langs -> transnoentitiesnoconv ( 'File' ) . ': ' . $pdir . $photo ;
$alt .= ' - ' . $langs -> transnoentitiesnoconv ( 'Size' ) . ': ' . $imgarray [ 'width' ] . 'x' . $imgarray [ 'height' ];
2011-12-05 19:41:38 +01:00
if ( $photo_vignette && $imgarray [ 'height' ] > $maxHeight ) {
2010-12-15 19:15:08 +01:00
$return .= '<!-- Show thumb -->' ;
2012-04-06 22:39:40 +02:00
$return .= '<img class="photo" border="0" height="' . $maxHeight . '" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=product&entity=' . $this -> entity . '&file=' . urlencode ( $pdirthumb . $photo_vignette ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2010-12-15 19:15:08 +01:00
}
else {
$return .= '<!-- Show original file -->' ;
2012-04-06 22:39:40 +02:00
$return .= '<img class="photo" border="0" height="' . $maxHeight . '" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=product&entity=' . $this -> entity . '&file=' . urlencode ( $pdir . $photo ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2010-12-15 19:15:08 +01:00
}
2011-05-12 00:53:40 +02:00
$return .= '</a>' . " \n " ;
2010-12-15 19:15:08 +01:00
if ( $showfilename ) $return .= '<br>' . $viewfilename ;
if ( $showaction )
{
$return .= '<br>' ;
// On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites
2012-07-03 21:07:54 +02:00
if ( $photo_vignette && preg_match ( '/(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/i' , $photo ) && ( $product -> imgWidth > $maxWidth || $product -> imgHeight > $maxHeight ))
2010-12-15 19:15:08 +01:00
{
2011-08-17 17:56:22 +02:00
$return .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $_GET [ " id " ] . '&action=addthumb&file=' . urlencode ( $pdir . $viewfilename ) . '">' . img_picto ( $langs -> trans ( 'GenerateThumb' ), 'refresh' ) . ' </a>' ;
2010-12-15 19:15:08 +01:00
}
if ( $user -> rights -> produit -> creer || $user -> rights -> service -> creer )
{
// Link to resize
$return .= '<a href="' . DOL_URL_ROOT . '/core/photos_resize.php?modulepart=' . urlencode ( 'produit|service' ) . '&id=' . $_GET [ " id " ] . '&file=' . urlencode ( $pdir . $viewfilename ) . '" title="' . dol_escape_htmltag ( $langs -> trans ( " Resize " )) . '">' . img_picto ( $langs -> trans ( " Resize " ), DOL_URL_ROOT . '/theme/common/transform-crop-and-resize' , '' , 1 ) . '</a> ' ;
// Link to delete
$return .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $_GET [ " id " ] . '&action=delete&file=' . urlencode ( $pdir . $viewfilename ) . '">' ;
$return .= img_delete () . '</a>' ;
}
}
$return .= " \n " ;
if ( $nbbyrow ) $return .= '</td>' ;
if ( $nbbyrow && ( $nbphoto % $nbbyrow == 0 )) $return .= '</tr>' ;
}
if ( $size == 0 ) { // Format origine
2012-04-06 22:39:40 +02:00
$return .= '<img class="photo" border="0" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=product&entity=' . $this -> entity . '&file=' . urlencode ( $pdir . $photo ) . '">' ;
2010-12-15 19:15:08 +01:00
if ( $showfilename ) $return .= '<br>' . $viewfilename ;
if ( $showaction )
{
if ( $user -> rights -> produit -> creer || $user -> rights -> service -> creer )
{
// Link to resize
$return .= '<a href="' . DOL_URL_ROOT . '/core/photos_resize.php?modulepart=' . urlencode ( 'produit|service' ) . '&id=' . $_GET [ " id " ] . '&file=' . urlencode ( $pdir . $viewfilename ) . '" title="' . dol_escape_htmltag ( $langs -> trans ( " Resize " )) . '">' . img_picto ( $langs -> trans ( " Resize " ), DOL_URL_ROOT . '/theme/common/transform-crop-and-resize' , '' , 1 ) . '</a> ' ;
// Link to delete
$return .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $_GET [ " id " ] . '&action=delete&file=' . urlencode ( $pdir . $viewfilename ) . '">' ;
$return .= img_delete () . '</a>' ;
}
}
}
// On continue ou on arrete de boucler ?
if ( $nbmax && $nbphoto >= $nbmax ) break ;
}
}
}
2006-11-30 15:23:38 +01:00
2009-07-22 19:02:49 +02:00
if ( $nbbyrow && $size == 1 )
{
// Ferme tableau
2010-02-20 16:07:46 +01:00
while ( $nbphoto % $nbbyrow )
{
2010-05-01 11:03:15 +02:00
$return .= '<td width="' . ceil ( 100 / $nbbyrow ) . '%"> </td>' ;
2009-07-22 19:02:49 +02:00
$nbphoto ++ ;
}
2008-07-27 22:47:13 +02:00
2010-05-01 11:03:15 +02:00
if ( $nbphoto ) $return .= '</table>' ;
2009-07-22 19:02:49 +02:00
}
2008-07-27 22:47:13 +02:00
2009-07-22 19:02:49 +02:00
closedir ( $handle );
2008-07-27 22:47:13 +02:00
}
2010-05-26 16:52:32 +02:00
2010-05-01 11:03:15 +02:00
$this -> nbphoto = $nbphoto ;
2010-05-26 16:52:32 +02:00
2010-05-01 11:03:15 +02:00
return $return ;
2008-07-27 22:47:13 +02:00
}
2011-12-07 12:58:39 +01:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Retourne tableau de toutes les photos du produit
2011-08-27 19:25:09 +02:00
*
2011-12-19 18:24:49 +01:00
* @ param string $dir Repertoire a scanner
* @ param int $nbmax Nombre maximum de photos ( 0 = pas de max )
* @ return array Tableau de photos
2008-07-27 22:47:13 +02:00
*/
function liste_photos ( $dir , $nbmax = 0 )
{
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2009-10-04 19:18:09 +02:00
2008-07-27 22:47:13 +02:00
$nbphoto = 0 ;
$tabobj = array ();
2009-12-15 12:30:49 +01:00
$dir_osencoded = dol_osencode ( $dir );
$handle =@ opendir ( $dir_osencoded );
2010-12-15 19:15:08 +01:00
if ( is_resource ( $handle ))
2008-07-27 22:47:13 +02:00
{
while (( $file = readdir ( $handle )) != false )
{
2009-10-04 19:18:09 +02:00
if ( ! utf8_check ( $file )) $file = utf8_encode ( $file ); // readdir returns ISO
2012-07-03 21:07:54 +02:00
if ( dol_is_file ( $dir . $file ) && preg_match ( '/(\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i' , $dir . $file ))
2008-07-27 22:47:13 +02:00
{
$nbphoto ++ ;
2008-08-04 02:30:52 +02:00
2008-07-27 22:47:13 +02:00
// On determine nom du fichier vignette
2009-10-04 19:18:09 +02:00
$photo = $file ;
2008-07-27 22:47:13 +02:00
$photo_vignette = '' ;
2012-07-03 21:07:54 +02:00
if ( preg_match ( '/(\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i' , $photo , $regs ))
2008-07-27 22:47:13 +02:00
{
2012-07-03 21:07:54 +02:00
$photo_vignette = preg_replace ( '/' . $regs [ 0 ] . '/i' , '' , $photo ) . '_small' . $regs [ 0 ];
2008-07-27 22:47:13 +02:00
}
2009-10-04 19:18:09 +02:00
$dirthumb = $dir . 'thumbs/' ;
2008-07-27 22:47:13 +02:00
// Objet
$obj = array ();
$obj [ 'photo' ] = $photo ;
2014-01-06 11:28:41 +01:00
if ( $photo_vignette && dol_is_file ( $dirthumb . $photo_vignette )) $obj [ 'photo_vignette' ] = 'thumbs/' . $photo_vignette ;
2008-07-27 22:47:13 +02:00
else $obj [ 'photo_vignette' ] = " " ;
$tabobj [ $nbphoto - 1 ] = $obj ;
// On continue ou on arrete de boucler ?
if ( $nbmax && $nbphoto >= $nbmax ) break ;
}
}
2008-08-04 02:30:52 +02:00
2008-07-27 22:47:13 +02:00
closedir ( $handle );
}
return $tabobj ;
}
/**
2011-06-10 13:39:01 +02:00
* Efface la photo du produit et sa vignette
2011-08-27 19:25:09 +02:00
*
2012-01-04 13:54:07 +01:00
* @ param string $file Chemin de l ' image
* @ return void
2008-07-27 22:47:13 +02:00
*/
function delete_photo ( $file )
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2011-03-09 16:34:57 +01:00
$dir = dirname ( $file ) . '/' ; // Chemin du dossier contenant l'image d'origine
2008-07-27 22:47:13 +02:00
$dirthumb = $dir . '/thumbs/' ; // Chemin du dossier contenant la vignette
2009-10-23 17:26:45 +02:00
$filename = preg_replace ( '/' . preg_quote ( $dir , '/' ) . '/i' , '' , $file ); // Nom du fichier
2009-01-25 15:30:42 +01:00
2008-07-27 22:47:13 +02:00
// On efface l'image d'origine
2009-10-04 19:18:09 +02:00
dol_delete_file ( $file );
2009-01-25 15:30:42 +01:00
2008-07-27 22:47:13 +02:00
// Si elle existe, on efface la vignette
2009-10-21 16:02:14 +02:00
if ( preg_match ( '/(\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i' , $filename , $regs ))
2008-07-27 22:47:13 +02:00
{
2009-10-21 16:02:14 +02:00
$photo_vignette = preg_replace ( '/' . $regs [ 0 ] . '/i' , '' , $filename ) . '_small' . $regs [ 0 ];
2009-12-15 12:30:49 +01:00
if ( file_exists ( dol_osencode ( $dirthumb . $photo_vignette )))
2008-07-27 22:47:13 +02:00
{
2009-10-04 19:18:09 +02:00
dol_delete_file ( $dirthumb . $photo_vignette );
2008-07-27 22:47:13 +02:00
}
}
}
/**
2011-06-10 13:39:01 +02:00
* Load size of image file
2011-08-27 19:25:09 +02:00
*
2012-01-04 13:54:07 +01:00
* @ param string $file Path to file
* @ return void
2008-07-27 22:47:13 +02:00
*/
function get_image_size ( $file )
{
2009-12-15 12:30:49 +01:00
$file_osencoded = dol_osencode ( $file );
$infoImg = getimagesize ( $file_osencoded ); // Get information on image
2008-07-27 22:47:13 +02:00
$this -> imgWidth = $infoImg [ 0 ]; // Largeur de l'image
$this -> imgHeight = $infoImg [ 1 ]; // Hauteur de l'image
}
/**
2011-06-10 13:39:01 +02:00
* Charge indicateurs this -> nb de tableau de bord
2011-08-27 19:25:09 +02:00
*
2011-06-10 13:39:01 +02:00
* @ return int < 0 si ko , > 0 si ok
2008-07-27 22:47:13 +02:00
*/
function load_state_board ()
{
global $conf , $user ;
$this -> nb = array ();
$sql = " SELECT count(p.rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product as p " ;
2012-02-11 10:18:09 +01:00
$sql .= ' WHERE p.entity IN (' . getEntity ( $this -> element , 1 ) . ')' ;
$sql .= " AND p.fk_product_type <> 1 " ;
2010-03-13 00:40:39 +01:00
2008-07-27 22:47:13 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$this -> nb [ " products " ] = $obj -> nb ;
}
2013-06-16 21:31:21 +02:00
$this -> db -> free ( $resql );
2008-07-27 22:47:13 +02:00
return 1 ;
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-07-27 22:47:13 +02:00
$this -> error = $this -> db -> error ();
return - 1 ;
}
2007-07-30 11:38:55 +02:00
}
2008-07-27 22:47:13 +02:00
2010-11-11 12:15:06 +01:00
/**
* Return if object is a product
2012-01-04 13:54:07 +01:00
*
2010-11-11 12:15:06 +01:00
* @ return boolean True if it ' s a product
*/
2010-02-20 15:24:47 +01:00
function isproduct ()
{
2012-03-04 01:41:36 +01:00
return ( $this -> type != 1 ? true : false );
2008-07-27 22:47:13 +02:00
}
2010-11-11 12:15:06 +01:00
/**
* Return if object is a product
2012-01-04 13:54:07 +01:00
*
2010-11-11 12:15:06 +01:00
* @ return boolean True if it ' s a service
*/
2010-02-21 02:45:58 +01:00
function isservice ()
2010-02-20 15:24:47 +01:00
{
2012-03-04 01:41:36 +01:00
return ( $this -> type == 1 ? true : false );
2008-07-27 22:47:13 +02:00
}
2007-01-31 18:52:54 +01:00
2011-10-01 01:40:15 +02:00
/**
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
*/
function initAsSpecimen ()
{
global $user , $langs , $conf , $mysoc ;
$now = dol_now ();
// Initialize parameters
$this -> id = 0 ;
$this -> ref = 'PRODUCT_SPEC' ;
$this -> libelle = 'PRODUCT SPECIMEN' ;
$this -> description = 'PRODUCT SPECIMEN ' . dol_print_date ( $now , 'dayhourlog' );
$this -> specimen = 1 ;
$this -> country_id = 1 ;
$this -> tosell = 1 ;
$this -> tobuy = 1 ;
$this -> type = 0 ;
$this -> note = 'This is a comment (private)' ;
}
2002-04-30 17:52:56 +02:00
}
2012-02-01 11:33:11 +01:00
?>