2004-10-20 23:06:45 +02:00
< ? php
2014-10-02 09:30:31 +02:00
/* Copyright ( C ) 2001 - 2007 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2004 - 2014 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2005 - 2014 Regis Houssin < regis . houssin @ capnetworks . com >
* Copyright ( C ) 2006 Andre Cianfarani < acianfa @ free . fr >
* Copyright ( C ) 2007 - 2011 Jean Heimburger < jean @ tiaris . info >
* Copyright ( C ) 2010 - 2013 Juanjo Menent < jmenent @ 2 byte . es >
2015-02-26 14:15:33 +01:00
* Copyright ( C ) 2012 Cedric Salvador < csalvador @ gpcsolutions . fr >
2014-10-02 09:30:31 +02:00
* Copyright ( C ) 2013 - 2014 Cedric GROSS < c . gross @ kreiz - it . fr >
2015-04-18 20:50:03 +02:00
* Copyright ( C ) 2013 - 2015 Marcos García < marcosgdf @ gmail . com >
2014-10-02 09:30:31 +02:00
* Copyright ( C ) 2011 - 2014 Alexandre Spangaro < alexandre . spangaro @ gmail . com >
* Copyright ( C ) 2014 Henry Florian < florian . henry @ open - concept . pro >
* Copyright ( C ) 2014 Philippe Grand < philippe . grand @ atoo - net . com >
2015-02-01 15:08:56 +01:00
* Copyright ( C ) 2014 Ion agorria < ion @ agorria . 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
*/
2015-01-31 14:53:14 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2014-03-05 10:30:06 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/class/productbatch.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' ;
2015-01-30 16:54:50 +01:00
protected $childtables = array ( 'askpricesupplierdet' , '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
2014-12-25 21:47:39 +01:00
/**
* { @ inheritdoc }
*/
protected $table_ref_field = 'ref' ;
2014-10-18 14:56:06 +02:00
var $regeximgext = '\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff' ;
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 ;
2015-03-30 03:14:07 +02:00
/**
* Check TYPE constants
* @ var int
*/
var $type = self :: TYPE_PRODUCT ;
2011-10-09 17:37:27 +02:00
//! 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 ;
2014-03-04 18:55:55 +01:00
// We must manage batch number, sell-by date and so on : '1':yes '0':no
var $status_batch ;
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
2014-12-04 01:36:53 +01:00
var $barcode_type_code ; // code (loaded by fetch_barcode). Example 'ean', 'isbn', ...
2011-12-07 15:06:29 +01:00
var $barcode_type_label ; // label (loaded by fetch_barcode)
2014-12-04 01:36:53 +01:00
var $barcode_type_coder ; // coder (loaded by fetch_barcode). Engine.
2008-07-27 22:47:13 +02:00
var $stats_propale = array ();
var $stats_commande = array ();
var $stats_contrat = array ();
var $stats_facture = array ();
2015-03-14 14:55:41 +01:00
var $stats_commande_fournisseur = array ();
2008-07-27 22:47:13 +02:00
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
2014-05-10 14:57:22 +02:00
//note not visible on orders and invoices
var $note ;
2010-07-14 17:41:39 +02:00
2014-12-28 01:26:41 +01:00
var $fk_price_expression ;
2010-07-14 17:41:39 +02:00
2015-02-26 14:15:33 +01:00
/**
2015-04-21 15:49:58 +02:00
* Unit code ( 'km' , 'm' , 'l' , 'p' , ... )
* @ var string
2015-02-26 14:15:33 +01:00
*/
public $fk_unit ;
2015-03-30 03:14:07 +02:00
/**
* Regular product
*/
const TYPE_PRODUCT = 0 ;
/**
* Service
*/
const TYPE_SERVICE = 1 ;
/**
* Advanced feature : assembly kit
*/
const TYPE_ASSEMBLYKIT = 2 ;
/**
* Advanced feature : stock kit
*/
const TYPE_STOCKKIT = 3 ;
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 = '' ;
2014-03-04 18:55:55 +01:00
$this -> status_batch = 0 ;
2008-07-27 22:47:13 +02:00
}
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
2014-02-19 01:51:19 +01:00
* @ return int Id of product / service if OK , < 0 if KO
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' );
}
2014-02-10 00:24:20 +01:00
$this -> accountancy_code_buy = trim ( $this -> accountancy_code_buy );
2014-02-02 18:26:17 +01:00
$this -> accountancy_code_sell = trim ( $this -> accountancy_code_sell );
2008-07-27 22:47:13 +02:00
2014-05-25 17:14:33 +02:00
// Barcode value
$this -> barcode = trim ( $this -> barcode );
2014-06-20 15:05:39 +02:00
2014-05-25 17:14:33 +02:00
// Check parameters
2008-07-27 22:47:13 +02:00
if ( empty ( $this -> libelle ))
2008-06-19 19:25:15 +02:00
{
2014-05-02 16:47:54 +02:00
$this -> error = 'ErrorMandatoryParametersNotProvided' ;
2008-06-19 19:25:15 +02:00
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' );
2014-05-25 17:14:33 +02:00
if ( $module != 'mod_codeproduct_leopard' ) // Do not load module file for leopard
2013-07-27 12:22:00 +02:00
{
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
2014-02-10 00:24:20 +01:00
// For automatic creation during create action (not used by Dolibarr GUI, can be used by scripts)
2014-03-16 17:37:54 +01:00
if ( $this -> barcode == - 1 ) $this -> barcode = $this -> get_barcode ( $this , $this -> barcode_type_code );
2014-06-20 15:05:39 +02:00
2014-03-16 17:37:54 +01:00
// Check more parameters
2014-02-19 01:51:19 +01:00
// If error, this->errors[] is filled
$result = $this -> verify ();
2014-06-20 15:05:39 +02:00
2014-02-19 01:51:19 +01:00
if ( $result >= 0 )
{
$sql = " SELECT count(*) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product " ;
$sql .= " WHERE entity IN ( " . getEntity ( 'product' , 1 ) . " ) " ;
$sql .= " AND ref = ' " . $this -> ref . " ' " ;
2011-07-29 22:47:34 +02:00
2014-02-19 01:51:19 +01: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
{
2014-02-19 01:51:19 +01:00
// Produit non deja existant
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " product ( " ;
$sql .= " datec " ;
$sql .= " , entity " ;
$sql .= " , ref " ;
$sql .= " , ref_ext " ;
$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 .= " , accountancy_code_buy " ;
$sql .= " , accountancy_code_sell " ;
$sql .= " , canvas " ;
$sql .= " , finished " ;
2014-03-04 18:55:55 +01:00
$sql .= " , tobatch " ;
2015-02-26 14:15:33 +01:00
$sql .= " , fk_unit " ;
2014-02-19 01:51:19 +01:00
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $this -> db -> idate ( $now ) . " ' " ;
$sql .= " , " . $conf -> entity ;
$sql .= " , ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
$sql .= " , " . ( ! empty ( $this -> ref_ext ) ? " ' " . $this -> db -> escape ( $this -> ref_ext ) . " ' " : " null " );
$sql .= " , " . price2num ( $price_min_ht );
$sql .= " , " . price2num ( $price_min_ttc );
$sql .= " , " . ( ! empty ( $this -> libelle ) ? " ' " . $this -> db -> escape ( $this -> libelle ) . " ' " : " null " );
$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 -> accountancy_code_buy . " ' " ;
$sql .= " , ' " . $this -> accountancy_code_sell . " ' " ;
$sql .= " , ' " . $this -> canvas . " ' " ;
2014-03-08 14:25:59 +01:00
$sql .= " , " . (( ! isset ( $this -> finished ) || $this -> finished < 0 || $this -> finished == '' ) ? 'null' : ( int ) $this -> finished );
2014-03-04 18:55:55 +01:00
$sql .= " , " . (( empty ( $this -> status_batch ) || $this -> status_batch < 0 ) ? '0' : $this -> status_batch );
2015-02-26 14:15:33 +01:00
$sql .= " , " . ( ! $this -> fk_unit ? 'NULL' : $this -> fk_unit );
2014-02-19 01:51:19 +01:00
$sql .= " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::Create " , LOG_DEBUG );
2014-02-19 01:51:19 +01:00
$result = $this -> db -> query ( $sql );
if ( $result )
2011-07-29 22:47:34 +02:00
{
2014-02-19 01:51:19 +01:00
$id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " product " );
if ( $id > 0 )
2007-06-30 15:06:03 +02:00
{
2014-02-19 01:51:19 +01:00
$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 )
{
if ( $this -> update ( $id , $user , true , 'add' ) <= 0 )
{
$error ++ ;
}
}
else
2007-06-30 15:06:03 +02:00
{
2014-02-19 01:51:19 +01: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 ++ ;
2014-02-19 01:51:19 +01:00
$this -> error = 'ErrorFailedToGetInsertedId' ;
2007-06-30 15:06:03 +02:00
}
}
else
{
2011-07-29 22:47:34 +02:00
$error ++ ;
2014-02-19 01:51:19 +01:00
$this -> error = $this -> db -> lasterror ();
2007-06-30 15:06:03 +02:00
}
}
else
{
2014-02-19 01:51:19 +01:00
// Product already exists with this ref
$langs -> load ( " products " );
2011-07-29 22:47:34 +02:00
$error ++ ;
2014-02-19 01:51:19 +01:00
$this -> error = " ErrorProductAlreadyExists " ;
2007-06-30 15:06:03 +02:00
}
}
else
{
2012-09-02 15:09:48 +02:00
$error ++ ;
2014-02-19 01:51:19 +01:00
$this -> error = $this -> db -> lasterror ();
2008-07-27 22:47:13 +02:00
}
2014-02-19 01:51:19 +01:00
if ( ! $error && ! $notrigger )
{
2014-07-04 15:54:20 +02:00
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_CREATE' , $user );
2014-09-07 13:18:56 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-04 15:54:20 +02:00
// End call triggers
2014-02-19 01:51:19 +01:00
}
if ( ! $error )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
$this -> db -> rollback ();
return - $error ;
}
}
else
{
$this -> db -> rollback ();
dol_syslog ( get_class ( $this ) . " ::Create fails verify " . join ( ',' , $this -> errors ), LOG_WARNING );
return - 3 ;
}
2008-07-27 22:47:13 +02:00
}
2014-02-19 01:51:19 +01:00
/**
2014-05-25 17:14:33 +02:00
* Check properties of product are ok ( like name , barcode , ... ) .
* All properties must be already loaded on object ( this -> barcode , this -> barcode_type_code , ... ) .
2014-02-19 01:51:19 +01:00
*
* @ return int 0 if OK , < 0 if KO
*/
function verify ()
{
$this -> errors = array ();
$result = 0 ;
$this -> ref = trim ( $this -> ref );
if ( ! $this -> ref )
{
$this -> errors [] = 'ErrorBadRef' ;
$result = - 2 ;
}
2014-05-25 17:14:33 +02:00
$rescode = $this -> check_barcode ( $this -> barcode , $this -> barcode_type_code );
2014-02-19 01:51:19 +01:00
if ( $rescode <> 0 )
{
if ( $rescode == - 1 )
{
$this -> errors [] = 'ErrorBadBarCodeSyntax' ;
}
if ( $rescode == - 2 )
{
$this -> errors [] = 'ErrorBarCodeRequired' ;
}
if ( $rescode == - 3 )
{
$this -> errors [] = 'ErrorBarCodeAlreadyUsed' ;
}
$result = - 3 ;
}
return $result ;
}
/**
2014-05-25 17:14:33 +02:00
* Check barcode
2014-02-19 01:51:19 +01:00
*
* @ param string $valuetotest Value to test
2014-05-25 17:14:33 +02:00
* @ param string $typefortest Type of barcode ( ISBN , EAN , ... )
2014-02-19 01:51:19 +01:00
* @ return int 0 if OK
* - 1 ErrorBadBarCodeSyntax
* - 2 ErrorBarCodeRequired
* - 3 ErrorBarCodeAlreadyUsed
*/
2014-05-25 17:14:33 +02:00
function check_barcode ( $valuetotest , $typefortest )
2014-02-19 01:51:19 +01:00
{
global $conf ;
if ( ! empty ( $conf -> barcode -> enabled ) && ! empty ( $conf -> global -> BARCODE_PRODUCT_ADDON_NUM ))
{
$module = strtolower ( $conf -> global -> BARCODE_PRODUCT_ADDON_NUM );
$dirsociete = array_merge ( array ( '/core/modules/barcode/' ), $conf -> modules_parts [ 'barcode' ]);
foreach ( $dirsociete as $dirroot )
{
$res = dol_include_once ( $dirroot . $module . '.php' );
if ( $res ) break ;
}
$mod = new $module ();
2014-06-20 15:05:39 +02:00
2014-05-25 17:14:33 +02:00
dol_syslog ( get_class ( $this ) . " ::check_barcode value= " . $valuetotest . " type= " . $typefortest . " module= " . $module );
$result = $mod -> verif ( $this -> db , $valuetotest , $this , 0 , $typefortest );
2014-02-19 01:51:19 +01:00
return $result ;
}
else
{
return 0 ;
}
}
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
2014-02-19 01:51:19 +01:00
* @ param string $action Current action for hookmanager ( 'add' or 'update' )
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
2014-02-19 01:51:19 +01:00
// Check parameters
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 );
2014-05-10 14:57:22 +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 ;
2014-04-13 02:08:57 +02:00
2010-11-02 12:22:41 +01:00
if ( empty ( $this -> country_id )) $this -> country_id = 0 ;
2014-05-25 17:14:33 +02:00
// Barcode value
2014-02-10 00:24:20 +01:00
$this -> barcode = trim ( $this -> barcode );
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 );
2011-04-20 00:25:06 +02:00
2014-02-19 01:51:19 +01:00
$this -> db -> begin ();
// Check name is required and codes are ok or unique.
// If error, this->errors[] is filled
if ( $action != 'add' )
{
$result = $this -> verify (); // We don't check when update called during a create because verify was already done
}
2014-06-20 15:05:39 +02:00
2014-02-19 01:51:19 +01:00
if ( $result >= 0 )
{
2014-07-15 11:22:16 +02:00
if ( empty ( $this -> oldcopy ))
{
2014-07-15 19:02:43 +02:00
$org = new self ( $this -> db );
2014-07-15 11:22:16 +02:00
$org -> fetch ( $this -> id );
$this -> oldcopy = $org ;
}
// test if batch management is activated on existing product
if ( $this -> hasbatch () && ! $this -> oldcopy -> hasbatch ())
{
$this -> load_stock ();
foreach ( $this -> stock_warehouse as $idW => $ObjW )
{
$qty_batch = 0 ;
2014-09-07 13:18:56 +02:00
foreach ( $ObjW -> detail_batch as $detail )
2014-07-15 11:22:16 +02:00
{
$qty_batch += $detail -> qty ;
}
// Quantities in batch details are not same same as stock quantity
// So we add a default batch record
if ( $ObjW -> real <> $qty_batch )
{
$ObjBatch = new Productbatch ( $this -> db );
$ObjBatch -> batch = $langs -> trans ( 'BatchDefaultNumber' );
$ObjBatch -> qty = $ObjW -> real - $qty_batch ;
$ObjBatch -> fk_product_stock = $ObjW -> id ;
if ( $ObjBatch -> create ( $user , 1 ) < 0 )
2014-09-07 13:18:56 +02:00
{
2014-07-15 11:22:16 +02:00
$error ++ ;
$this -> errors = $ObjBatch -> errors ;
}
}
2014-09-07 13:18:56 +02:00
}
2014-07-15 11:22:16 +02:00
}
2014-02-19 01:51:19 +01:00
// For automatic creation
2014-03-16 17:37:54 +01:00
if ( $this -> barcode == - 1 ) $this -> barcode = $this -> get_barcode ( $this , $this -> barcode_type_code );
2014-02-19 01:51:19 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " product " ;
$sql .= " SET label = ' " . $this -> db -> escape ( $this -> libelle ) . " ' " ;
$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 ;
2014-06-20 15:05:39 +02:00
2014-02-19 01:51:19 +01:00
$sql .= " , barcode = " . ( empty ( $this -> barcode ) ? " null " : " ' " . $this -> db -> escape ( $this -> barcode ) . " ' " );
$sql .= " , fk_barcode_type = " . ( empty ( $this -> barcode_type ) ? " null " : $this -> db -> escape ( $this -> barcode_type ));
$sql .= " , tosell = " . $this -> status ;
$sql .= " , tobuy = " . $this -> status_buy ;
2014-03-04 18:55:55 +01:00
$sql .= " , tobatch = " . (( empty ( $this -> status_batch ) || $this -> status_batch < 0 ) ? '0' : $this -> status_batch );
2014-03-08 14:25:59 +01:00
$sql .= " , finished = " . (( ! isset ( $this -> finished ) || $this -> finished < 0 ) ? " null " : ( int ) $this -> finished );
2014-02-19 01:51:19 +01: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 ) . " ' " ;
2014-03-14 23:04:27 +01:00
$sql .= " , url = " . ( $this -> url ? " ' " . $this -> db -> escape ( $this -> url ) . " ' " : 'null' );
2014-03-14 13:51:46 +01:00
$sql .= " , customcode = ' " . $this -> db -> escape ( $this -> customcode ) . " ' " ;
2014-02-19 01:51:19 +01:00
$sql .= " , fk_country = " . ( $this -> country_id > 0 ? $this -> country_id : 'null' );
2014-05-10 14:57:22 +02:00
$sql .= " , note = " . ( isset ( $this -> note ) ? " ' " . $this -> db -> escape ( $this -> note ) . " ' " : 'null' );
2014-02-19 01:51:19 +01:00
$sql .= " , duration = ' " . $this -> duration_value . $this -> duration_unit . " ' " ;
$sql .= " , accountancy_code_buy = ' " . $this -> accountancy_code_buy . " ' " ;
$sql .= " , accountancy_code_sell= ' " . $this -> accountancy_code_sell . " ' " ;
$sql .= " , desiredstock = " . (( isset ( $this -> desiredstock ) && $this -> desiredstock != '' ) ? $this -> desiredstock : " null " );
2015-02-26 14:15:33 +01:00
$sql .= " , fk_unit= " . ( ! $this -> fk_unit ? 'NULL' : $this -> fk_unit );
2014-02-19 01:51:19 +01:00
$sql .= " WHERE rowid = " . $id ;
2014-06-20 15:05:39 +02:00
2015-03-21 15:31:11 +01:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2014-07-02 21:20:02 +02:00
2014-02-19 01:51:19 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2007-06-30 15:06:03 +02:00
{
2014-02-19 01:51:19 +01:00
$this -> id = $id ;
// Multilangs
if ( ! empty ( $conf -> global -> MAIN_MULTILANGS ))
2009-01-10 01:32:42 +01:00
{
2014-02-19 01:51:19 +01:00
if ( $this -> setMultiLangs () < 0 )
{
$this -> error = $langs -> trans ( " Error " ) . " : " . $this -> db -> error () . " - " . $sql ;
return - 2 ;
}
2009-01-10 01:32:42 +01:00
}
2012-01-30 20:08:11 +01:00
2014-11-15 19:04:21 +01:00
$action = 'update' ;
2014-02-19 01:51:19 +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:23:46 +01:00
{
2014-02-19 01:51:19 +01:00
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
2012-11-09 19:23:46 +01:00
{
2014-02-19 01:51:19 +01:00
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
2012-11-09 19:23:46 +01:00
}
}
2014-02-19 01:51:19 +01:00
else if ( $reshook < 0 ) $error ++ ;
2012-03-03 19:02:44 +01:00
2014-02-19 01:51:19 +01:00
if ( ! $error && ! $notrigger )
{
2014-07-04 15:54:20 +02:00
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_MODIFY' , $user );
2014-09-07 13:18:56 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-04 15:54:20 +02:00
// End call triggers
2014-02-19 01:51:19 +01:00
}
2012-07-02 12:07:42 +02:00
2014-02-19 01:51:19 +01:00
if ( ! $error && ( is_object ( $this -> oldcopy ) && $this -> oldcopy -> ref != $this -> ref ))
2012-07-02 19:30:37 +02:00
{
2014-02-19 01:51:19 +01:00
// We remove directory
if ( $conf -> product -> dir_output )
2012-07-02 19:30:37 +02:00
{
2014-02-19 01:51:19 +01:00
$olddir = $conf -> product -> dir_output . " / " . dol_sanitizeFileName ( $this -> oldcopy -> ref );
$newdir = $conf -> product -> dir_output . " / " . dol_sanitizeFileName ( $this -> ref );
if ( file_exists ( $olddir ))
2012-07-02 19:30:37 +02:00
{
2014-02-19 01:51:19 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2014-06-25 20:22:25 +02:00
$res = dol_move ( $olddir , $newdir );
2014-02-19 01:51:19 +01:00
if ( ! $res )
{
$this -> error = 'ErrorFailToMoveDir' ;
$error ++ ;
}
2012-07-02 19:30:37 +02:00
}
}
2012-06-08 21:38:11 +02:00
}
2012-07-02 12:07:42 +02:00
2014-02-19 01:51:19 +01:00
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return - $error ;
}
2007-06-30 15:06:03 +02:00
}
else
{
2014-02-19 01:51:19 +01:00
if ( $this -> db -> errno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' )
{
$this -> error = $langs -> trans ( " Error " ) . " : " . $langs -> trans ( " ErrorProductAlreadyExists " , $this -> ref );
$this -> db -> rollback ();
return - 1 ;
}
else
{
$this -> error = $langs -> trans ( " Error " ) . " : " . $this -> db -> error () . " - " . $sql ;
$this -> db -> rollback ();
return - 2 ;
}
2007-06-30 15:06:03 +02:00
}
2014-02-19 01:51:19 +01:00
}
else
{
$this -> db -> rollback ();
dol_syslog ( get_class ( $this ) . " ::Update fails verify " . join ( ',' , $this -> errors ), LOG_WARNING );
return - 3 ;
}
2007-06-30 15:06:03 +02:00
}
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 ;
2014-08-13 12:42:55 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
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 ;
}
2015-03-30 03:14:07 +02:00
if (( $this -> type == Product :: TYPE_PRODUCT && empty ( $user -> rights -> produit -> supprimer )) || ( $this -> type == Product :: TYPE_SERVICE && 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
{
2014-07-04 15:54:20 +02:00
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_DELETE' , $user );
2014-09-07 13:18:56 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-04 15:54:20 +02:00
// End call triggers
2013-08-27 09:36:54 +02:00
}
2011-10-05 14:29:16 +02:00
2014-07-04 15:54:20 +02:00
// Delete all child tables
if ( ! $error )
2013-08-27 09:36:54 +02:00
{
2014-07-04 15:54:20 +02:00
$elements = array ( 'product_fournisseur_price' , 'product_price' , 'product_lang' , 'categorie_product' , 'product_stock' );
foreach ( $elements as $table )
{
if ( ! $error )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE fk_product = " . $id ;
dol_syslog ( get_class ( $this ) . '::delete' , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
if ( ! $result )
{
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ();
}
}
}
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 ;
2014-06-13 01:42:00 +02:00
dol_syslog ( get_class ( $this ) . '::delete' , 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 ();
}
}
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
{
2014-01-21 12:55:05 +01:00
$this -> error = " ErrorRecordIsUsedCantDelete " ;
2013-08-27 09:36:54 +02:00
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
/**
2014-06-20 15:05:39 +02:00
* Update or add a translation for a product
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
if ( $key == $current_lang )
2009-06-08 12:56:54 +02:00
{
2015-03-21 15:31:11 +01:00
$sql = " SELECT rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_lang " ;
$sql .= " WHERE fk_product= " . $this -> id ;
$sql .= " AND lang=' " . $key . " ' " ;
$result = $this -> db -> query ( $sql );
if ( $this -> db -> num_rows ( $result )) // if there is already a description line for this language
2009-06-08 12:56:54 +02:00
{
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
}
2014-06-13 01:42:00 +02:00
dol_syslog ( get_class ( $this ) . '::setMultiLangs' );
2012-08-28 23:01:04 +02:00
if ( ! $this -> db -> query ( $sql2 ))
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
2009-06-08 12:56:54 +02:00
}
2015-03-21 15:31:11 +01:00
else if ( isset ( $this -> multilangs [ $key ]))
2009-06-08 12:56:54 +02:00
{
2015-03-21 15:31:11 +01:00
$sql = " SELECT rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_lang " ;
$sql .= " WHERE fk_product= " . $this -> id ;
$sql .= " AND lang=' " . $key . " ' " ;
$result = $this -> db -> query ( $sql );
if ( $this -> db -> num_rows ( $result )) // if there is already a description line for this language
2009-06-08 12:56:54 +02:00
{
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 " ] )
2014-06-13 01:42:00 +02:00
dol_syslog ( get_class ( $this ) . '::setMultiLangs' );
2012-08-28 23:01:04 +02:00
if ( ! $this -> db -> query ( $sql2 ))
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
2009-06-08 12:56:54 +02:00
}
2015-03-21 15:31:11 +01:00
else
{
// language is not current language and we didn't provide a multilang description for this language
}
2008-07-27 22:47:13 +02:00
}
return 1 ;
}
2014-06-20 15:05:39 +02:00
/**
* Delete a language for this product
*
2014-06-26 21:38:03 +02:00
* @ param string $langtodelete Language code to delete
2014-06-20 15:05:39 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
function delMultiLangs ( $langtodelete )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " product_lang " ;
$sql .= " WHERE fk_product= " . $this -> id . " AND lang=' " . $this -> db -> escape ( $langtodelete ) . " ' " ;
2014-07-02 21:29:07 +02:00
dol_syslog ( get_class ( $this ) . '::delMultiLangs' , LOG_DEBUG );
2014-06-20 15:05:39 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
return 1 ;
}
else
{
2014-06-17 12:01:45 +02:00
$this -> error = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . '::delMultiLangs error=' . $this -> error , LOG_ERR );
2014-06-20 15:05:39 +02:00
return - 1 ;
}
}
2008-07-27 22:47:13 +02:00
2014-09-15 22:52:59 +02:00
/*
2014-09-14 13:59:06 +02:00
* Sets an accountancy code for a product .
* Also calls PRODUCT_MODIFY trigger when modified
*
* @ param string $type It can be 'buy' or 'sell'
* @ param string $value Accountancy code
* @ return int < 0 KO > 0 OK
*/
public function setAccountancyCode ( $type , $value )
{
2014-09-15 22:52:59 +02:00
global $user , $langs , $conf ;
2014-09-14 13:59:06 +02:00
$this -> db -> begin ();
if ( $type == 'buy' ) {
$field = 'accountancy_code_buy' ;
} elseif ( $type == 'sell' ) {
$field = 'accountancy_code_sell' ;
} else {
return - 1 ;
}
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " SET " ;
$sql .= " $field = ' " . $this -> db -> escape ( $value ) . " ' " ;
$sql .= " WHERE rowid = " . $this -> id ;
dol_syslog ( get_class ( $this ) . " :: " . __FUNCTION__ . " sql= " . $sql , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2014-09-15 22:52:59 +02:00
if ( $resql )
{
2014-09-14 13:59:06 +02:00
// Call triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'PRODUCT_MODIFY' , $this , $user , $langs , $conf );
2014-09-15 22:52:59 +02:00
if ( $result < 0 )
{
2014-09-14 13:59:06 +02:00
$this -> errors = $interface -> errors ;
$this -> db -> rollback ();
return - 1 ;
}
// End call triggers
$this -> $field = $value ;
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
}
2008-07-27 22:47:13 +02:00
/**
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 )
{
2015-02-02 13:37:19 +01:00
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>';
2015-02-02 13:37:19 +01: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
{
2015-02-02 13:37:19 +01:00
$this -> error = " Error: " . $this -> db -> lasterror () . " - " . $sql ;
2008-07-27 22:47:13 +02:00
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
/**
2015-02-02 13:37:19 +01:00
* Insert a track that we changed a customer price
2011-08-27 19:25:09 +02:00
*
2015-02-02 13:37:19 +01:00
* @ param User $user User making change
2011-12-19 18:24:49 +01:00
* @ 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 ();
2015-04-02 19:41:19 +02:00
// Clean parameters
if ( empty ( $this -> price_by_qty )) $this -> price_by_qty = 0 ;
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, " ;
2014-12-28 01:26:41 +01:00
$sql .= " localtax1_tx, localtax2_tx, price_min,price_min_ttc,price_by_qty,entity,fk_price_expression) " ;
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 . " , " ;
2015-02-02 13:37:19 +01:00
$sql .= " " . $this -> localtax1_tx . " , " . $this -> localtax2_tx . " , " . $this -> price_min . " , " . $this -> price_min_ttc . " , " . $this -> price_by_qty . " , " . $conf -> entity . " , " . ( $this -> fk_price_expression > 0 ? $this -> fk_price_expression : 'null' );
2009-01-10 01:32:42 +01:00
$sql .= " ) " ;
2015-04-02 19:41:19 +02:00
dol_syslog ( get_class ( $this ) . " ::_log_price " , LOG_DEBUG );
2009-01-10 01:32:42 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
2007-06-16 15:06:05 +02:00
{
2015-02-02 13:37:19 +01:00
$this -> error = $this -> db -> lasterror ();
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 ;
2015-04-02 19:41:19 +02:00
dol_syslog ( get_class ( $this ) . " ::log_price_delete " , 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
/**
2014-05-25 03:57:13 +02:00
* Read price used by a provider
* We enter as input couple prodfournprice / qty or 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
2014-05-25 03:57:13 +02:00
* @ return int <- 1 if KO , - 1 if qty not enough , 0 si ok mais rien trouve , id_product si ok et trouve . May also initialize some properties like ( -> ref_supplier , buyprice , fourn_pu , vatrate_supplier ... )
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, " ;
2014-12-28 01:26:41 +01:00
$sql .= " pfp.fk_product, pfp.ref_fourn, pfp.fk_soc, pfp.tva_tx, pfp.fk_supplier_price_expression " ;
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
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::get_buyprice " , LOG_DEBUG );
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
{
2015-02-01 15:08:56 +01:00
if ( ! empty ( $obj -> fk_supplier_price_expression ))
{
2015-03-14 16:50:02 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/dynamic_price/class/price_parser.class.php' ;
2015-02-01 15:08:56 +01:00
$priceparser = new PriceParser ( $this -> db );
2014-12-28 01:26:41 +01:00
$price_result = $priceparser -> parseProductSupplier ( $obj -> fk_product , $obj -> fk_supplier_price_expression , $obj -> quantity , $obj -> tva_tx );
2014-11-29 06:31:35 +01:00
if ( $price_result >= 0 ) {
$obj -> price = $price_result ;
}
}
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, " ;
2014-12-28 01:26:41 +01:00
$sql .= " pfp.fk_product, pfp.ref_fourn as ref_supplier, pfp.tva_tx, pfp.fk_supplier_price_expression " ;
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
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::get_buyprice " , LOG_DEBUG );
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
{
2015-02-01 15:08:56 +01:00
if ( ! empty ( $obj -> fk_supplier_price_expression ))
{
2015-03-14 16:50:02 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/dynamic_price/class/price_parser.class.php' ;
2015-02-01 15:08:56 +01:00
$priceparser = new PriceParser ( $this -> db );
2014-12-28 01:26:41 +01:00
$price_result = $priceparser -> parseProductSupplier ( $obj -> fk_product , $obj -> fk_supplier_price_expression , $obj -> quantity , $obj -> tva_tx );
2014-11-29 06:31:35 +01:00
if ( $result >= 0 ) {
$obj -> price = $price_result ;
}
}
2014-05-25 03:57:13 +02:00
$this -> buyprice = $obj -> price ; // deprecated
$this -> fourn_qty = $obj -> quantity ; // min quantity for price
2007-09-04 22:38:39 +02:00
$this -> fourn_pu = $obj -> price / $obj -> quantity ; // Prix unitaire du produit pour le fournisseur $fourn_id
2014-05-25 03:57:13 +02:00
$this -> ref_fourn = $obj -> ref_supplier ; // deprecated
$this -> ref_supplier = $obj -> ref_supplier ; // Ref supplier
2012-05-08 21:55:52 +02:00
$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
{
2015-03-06 14:36:22 +01:00
$this -> error = $this -> db -> lasterror ();
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
{
2015-03-06 14:36:22 +01:00
$this -> error = $this -> db -> lasterror ();
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 ;
2015-04-02 19:41:19 +02: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 ))
{
2014-08-27 11:34:24 +02:00
$this -> error = 'ErrorPriceCantBeLowerThanMinPrice' ;
2013-11-06 19:31:13 +01:00
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
2014-02-23 16:16:47 +01: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
2014-07-04 15:54:20 +02:00
$this -> db -> begin ();
2010-03-29 01:26:14 +02:00
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
2015-04-02 19:41:19 +02:00
dol_syslog ( get_class ( $this ) . " ::update_price " , 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
2014-07-04 15:54:20 +02:00
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_PRICE_MODIFY' , $user );
2014-09-07 13:18:56 +02:00
if ( $result < 0 )
{
2014-07-04 15:54:20 +02:00
$this -> db -> rollback ();
return - 1 ;
2014-09-07 13:18:56 +02:00
}
2014-07-04 15:54:20 +02:00
// End call triggers
2014-09-07 13:18:56 +02:00
2014-07-04 15:54:20 +02:00
$this -> db -> commit ();
2007-06-16 15:06:05 +02:00
}
else
{
2014-07-04 15:54:20 +02:00
$this -> db -> rollback ();
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
2014-12-28 01:26:41 +01:00
/**
* Sets the supplier price expression
*
* @ param int $expression_id Expression
* @ return int < 0 if KO , > 0 if OK
*/
function setPriceExpression ( $expression_id )
{
global $conf ;
// Clean parameters
$this -> db -> begin ();
$expression_id = $expression_id != 0 ? $expression_id : 'NULL' ;
$sql = " UPDATE " . MAIN_DB_PREFIX . " product " ;
$sql .= " SET fk_price_expression = " . $expression_id ;
$sql .= " WHERE rowid = " . $this -> id ;
dol_syslog ( get_class ( $this ) . " ::setPriceExpression " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> error () . " sql= " . $sql ;
$this -> db -> rollback ();
return - 1 ;
}
}
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
*
2015-01-31 14:53:14 +01: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
2014-12-28 01:26:41 +01:00
* @ param int $ignore_expression Ignores the math expression for calculating price and uses the db value instead
2015-01-31 14:53:14 +01:00
* @ return int < 0 if KO , 0 if not found , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2014-12-28 01:26:41 +01:00
function fetch ( $id = '' , $ref = '' , $ref_ext = '' , $ignore_expression = 0 )
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' ;
2014-11-15 15:19:37 +01:00
dol_print_error ( get_class ( $this ) . " ::fetch " . $this -> error );
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
2014-09-18 10:08:56 +02:00
$sql = " SELECT rowid, ref, ref_ext, label, description, url, 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, " ;
2015-02-26 14:15:33 +01:00
$sql .= " datec, tms, import_key, entity, desiredstock, tobatch, fk_unit " ;
2015-04-22 10:39:32 +02:00
$sql .= " , fk_price_expression " ;
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
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
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 ;
2014-09-18 10:08:56 +02:00
$this -> ref_ext = $obj -> ref_ext ;
2012-04-06 22:39:40 +02:00
$this -> libelle = $obj -> label ; // TODO deprecated
$this -> label = $obj -> label ;
$this -> description = $obj -> description ;
2014-03-14 13:51:46 +01:00
$this -> url = $obj -> url ;
2012-04-06 22:39:40 +02:00
$this -> note = $obj -> note ;
$this -> type = $obj -> fk_product_type ;
$this -> status = $obj -> tosell ;
$this -> status_buy = $obj -> tobuy ;
2014-03-04 18:55:55 +01:00
$this -> status_batch = $obj -> tobatch ;
2012-04-06 22:39:40 +02:00
$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 ;
2014-10-18 14:56:06 +02:00
2014-09-15 09:58:44 +02:00
$this -> ref_ext = $obj -> ref_ext ;
2014-12-28 01:26:41 +01:00
$this -> fk_price_expression = $obj -> fk_price_expression ;
2015-02-26 14:15:33 +01:00
$this -> fk_unit = $obj -> fk_unit ;
2011-05-22 04:14:01 +02:00
2011-05-21 10:01:23 +02:00
$this -> db -> free ( $resql );
2014-09-07 13:18:56 +02:00
2015-04-02 12:55:00 +02:00
// Retreive all extrafield for current object
2014-07-05 11:17:50 +02:00
// fetch optionals attributes and labels
require_once ( DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' );
$extrafields = new ExtraFields ( $this -> db );
$extralabels = $extrafields -> fetch_name_optionals_label ( $this -> table_element , true );
$this -> fetch_optionals ( $this -> id , $extralabels );
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
2015-02-01 15:08:56 +01:00
if ( ! empty ( $this -> fk_price_expression ) && empty ( $ignore_expression ))
2015-01-31 14:53:14 +01:00
{
2015-03-14 16:50:02 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/dynamic_price/class/price_parser.class.php' ;
2015-02-01 15:08:56 +01:00
$priceparser = new PriceParser ( $this -> db );
2014-12-28 01:26:41 +01:00
$price_result = $priceparser -> parseProduct ( $this );
2015-02-01 15:08:56 +01:00
if ( $price_result >= 0 )
2015-01-31 14:53:14 +01:00
{
2014-12-28 01:26:41 +01:00
$this -> price = $price_result ;
//Calculate the VAT
$this -> price_ttc = price2num ( $this -> price ) * ( 1 + ( $this -> tva_tx / 100 ));
$this -> price_ttc = price2num ( $this -> price_ttc , 'MU' );
}
}
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();
2014-04-13 02:08:57 +02:00
// instead we just init the stock_warehouse array
$this -> stock_warehouse = array ();
2014-02-10 00:24:20 +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
2015-02-12 15:23:31 +01:00
* @ return array Array of stats ( nb = nb of order , qty = qty ordered )
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 " ;
2014-07-03 12:31:55 +02:00
$sql .= " , " . MAIN_DB_PREFIX . " commande as c " ;
2009-04-27 22:37:50 +02:00
$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 " ;
2014-07-03 12:31:55 +02:00
$sql .= " AND c.rowid = cd.fk_commande " ;
2009-04-27 22:37:50 +02:00
$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 ;
2014-07-03 12:31:55 +02:00
if ( $filtrestatut <> '' ) $sql .= " AND c.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 ;
}
}
2014-07-03 10:24:29 +02:00
/**
* Charge tableau des stats réception fournisseur pour le produit / service
*
* @ 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
*/
function load_stats_reception ( $socid = 0 , $filtrestatut = '' )
{
global $conf , $user ;
$sql = " SELECT COUNT(DISTINCT cf.fk_soc) as nb_customers, COUNT(DISTINCT cf.rowid) as nb, " ;
$sql .= " COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " commande_fournisseur_dispatch as fd " ;
$sql .= " , " . MAIN_DB_PREFIX . " commande_fournisseur as cf " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE cf.rowid = fd.fk_commande " ;
$sql .= " AND cf.fk_soc = s.rowid " ;
$sql .= " AND cf.entity = " . $conf -> entity ;
$sql .= " AND fd.fk_product = " . $this -> id ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " AND cf.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
if ( $socid > 0 ) $sql .= " AND cf.fk_soc = " . $socid ;
if ( $filtrestatut <> '' ) $sql .= " AND cf.fk_statut in ( " . $filtrestatut . " ) " ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> stats_reception [ 'suppliers' ] = $obj -> nb_customers ;
$this -> stats_reception [ 'nb' ] = $obj -> nb ;
$this -> stats_reception [ 'rows' ] = $obj -> nb_rows ;
$this -> stats_reception [ 'qty' ] = $obj -> qty ? $obj -> qty : 0 ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2014-07-04 15:06:45 +02:00
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
}
2014-02-10 00:24:20 +01:00
2013-11-22 14:47:16 +01:00
/**
* Return nb of units or orders in which product is included
*
* @ 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
*/
function get_nb_ordersupplier ( $socid , $mode )
{
global $conf , $user ;
2014-02-10 00:24:20 +01:00
2013-11-22 14:47:16 +01: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 . " commande_fournisseurdet as d, " . MAIN_DB_PREFIX . " commande_fournisseur as c, " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> fournisseur -> lire && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE c.rowid = d.fk_commande " ;
$sql .= " AND d.fk_product = " . $this -> id ;
$sql .= " AND c.fk_soc = s.rowid " ;
$sql .= " AND c.entity = " . $conf -> entity ;
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 ;
$sql .= " GROUP BY date_format(c.date_commande,'%Y%m') " ;
$sql .= " ORDER BY date_format(c.date_commande,'%Y%m') DESC " ;
2014-02-10 00:24:20 +01:00
2013-11-22 14:47:16 +01:00
return $this -> _get_stats ( $sql , $mode );
}
2008-07-27 22:47:13 +02:00
/**
2015-02-24 19:55:59 +01:00
* Link a product / service to a parent product / service
2011-08-27 19:25:09 +02:00
*
2015-02-24 19:55:59 +01:00
* @ param int $id_pere Id of parent product / service
* @ param int $id_fils Id of child product / service
2012-01-04 13:54:07 +01:00
* @ param int $qty Quantity
2015-02-24 19:55:59 +01:00
* @ param int $incdec 1 = Increase / decrease stock of child when parent stock increase / decrease
2011-12-19 18:24:49 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2015-02-24 19:55:59 +01:00
function add_sousproduit ( $id_pere , $id_fils , $qty , $incdec = 1 )
2008-07-27 22:47:13 +02:00
{
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 ;
2015-02-24 19:55:59 +01:00
if ( ! is_numeric ( $incdec )) $incdec = 0 ;
2013-04-22 01:59:52 +02:00
$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
{
2015-02-24 19:55:59 +01:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'product_association(fk_product_pere,fk_product_fils,qty,incdec)' ;
$sql .= ' VALUES (' . $id_pere . ', ' . $id_fils . ', ' . $qty . ', ' . $incdec . ')' ;
2013-04-22 01:59:52 +02:00
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
}
}
2015-02-23 12:19:19 +01:00
/**
* Modify composed product
*
2015-02-24 19:55:59 +01:00
* @ param int $id_pere Id of parent product / service
* @ param int $id_fils Id of child product / service
2015-02-23 12:19:19 +01:00
* @ param int $qty Quantity
2015-02-24 19:55:59 +01:00
* @ param int $incdec 1 = Increase / decrease stock of child when parent stock increase / decrease
* @ return int < 0 if KO , > 0 if OK
2015-02-23 12:19:19 +01:00
*/
2015-02-24 19:55:59 +01:00
function update_sousproduit ( $id_pere , $id_fils , $qty , $incdec = 1 )
2015-02-23 12:19:19 +01:00
{
// Clean parameters
if ( ! is_numeric ( $id_pere )) $id_pere = 0 ;
if ( ! is_numeric ( $id_fils )) $id_fils = 0 ;
if ( ! is_numeric ( $incdec )) $incdec = 1 ;
if ( ! is_numeric ( $qty )) $qty = 1 ;
2015-02-24 19:55:59 +01:00
2015-02-23 12:19:19 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'product_association SET ' ;
$sql .= 'qty=' . $qty ;
$sql .= ',incdec=' . $incdec ;
$sql .= ' WHERE fk_product_pere=' . $id_pere . ' AND fk_product_fils=' . $id_fils ;
2015-02-24 19:55:59 +01:00
2015-02-23 12:19:19 +01:00
if ( ! $this -> db -> query ( $sql ))
{
dol_print_error ( $this -> db );
return - 1 ;
}
else
{
return 1 ;
}
2015-02-24 19:55:59 +01:00
2015-02-23 12:19:19 +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
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::del_sousproduit' , LOG_DEBUG );
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
{
2015-02-24 19:55:59 +01:00
$sql = " SELECT fk_product_pere, qty, incdec " ;
2010-02-20 13:02:48 +01:00
$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 ;
2015-02-24 19:55:59 +01:00
$this -> is_sousproduit_incdec = $obj -> incdec ;
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 ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::add_fournisseur " , LOG_DEBUG );
2011-10-05 12:51:55 +02:00
$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
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::add_fournisseur " , LOG_DEBUG );
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
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::add_fournisseur " , LOG_DEBUG );
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 ();
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
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::clone_price' , LOG_DEBUG );
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 ;
}
2014-05-13 10:12:45 +02:00
/**
* Clone links between products
*
* @ param int $fromId Product id
* @ param int $toId Product id
* @ return number
*/
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 . " ' " ;
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::clone_association' , LOG_DEBUG );
2013-06-10 00:11:25 +02:00
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
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::clone_fournisseurs' , LOG_DEBUG );
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
*
2015-03-13 21:39:39 +01:00
* @ param array $prod Products array
* @ param string $compl_path Directory path of parents to add before
* @ param int $multiply Because each sublevel must be multiplicated by parent nb
* @ param int $level Init level
* @ param int $id_parent Id parent
2011-06-10 13:39:01 +02:00
* @ return void
2008-07-27 22:47:13 +02:00
*/
2015-03-11 14:55:45 +01:00
function fetch_prod_arbo ( $prod , $compl_path = " " , $multiply = 1 , $level = 1 , $id_parent = 0 )
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);
2015-03-11 14:55:45 +01:00
foreach ( $prod as $id_product => $desc_pere ) // $id_product is 0 (first call starting with root top) 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 ] : '' );
2015-02-23 12:19:19 +01:00
$incdec =! empty ( $desc_pere [ 4 ]) ? $desc_pere [ 4 ] : 0 ;
2015-02-24 19:55:59 +01:00
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
2015-03-11 14:55:45 +01:00
'id_parent' => $id_parent ,
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 ,
2015-02-23 12:19:19 +01:00
'level' => $level ,
'incdec' => $incdec
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";
2015-03-11 14:55:45 +01:00
$this -> fetch_prod_arbo ( $desc_pere [ 'childs' ], $compl_path . $desc_pere [ 3 ] . " -> " , $desc_pere [ 1 ] * $multiply , $level + 1 , $id );
2013-06-01 16:11:45 +02:00
}
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
{
2015-03-11 14:55:45 +01:00
$this -> fetch_prods ( $desc_pere );
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
* 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
{
2015-03-11 14:55:45 +01:00
if ( is_array ( $desc_product )) $this -> fetch_prod_arbo ( $desc_product , " " , $multiply , 1 , $this -> id );
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 ();
2015-03-11 14:55:45 +01:00
if ( is_array ( $this -> sousprods ))
2008-07-27 22:47:13 +02:00
{
2015-03-11 14:55:45 +01:00
foreach ( $this -> sousprods as $nom_pere => $desc_pere )
2008-10-23 23:54:30 +02:00
{
2015-03-11 14:55:45 +01:00
if ( count ( $desc_pere ) > 1 ) $this -> fetch_prods ( $desc_pere );
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
*
2015-05-03 21:15:08 +02:00
* @ return array Array of product
2015-03-11 14:55:45 +01:00
* @ see getParent
2011-08-18 18:16:04 +02:00
*/
function getFather ()
{
2015-05-03 21:15:08 +02:00
$sql = " SELECT p.rowid, p.label as label, p.ref as ref, pa.fk_product_pere as id, p.fk_product_type, pa.qty, pa.incdec " ;
2011-08-18 18:16:04 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_association as pa, " ;
$sql .= " " . MAIN_DB_PREFIX . " product as p " ;
$sql .= " WHERE p.rowid = pa.fk_product_pere " ;
2015-05-03 21:15:08 +02:00
$sql .= " AND pa.fk_product_fils = " . $this -> id ;
2011-08-18 18:16:04 +02:00
$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
{
2015-05-03 21:15:08 +02:00
$prods [ $record [ 'id' ]][ 'id' ] = $record [ 'rowid' ];
$prods [ $record [ 'id' ]][ 'ref' ] = $record [ 'ref' ];
$prods [ $record [ 'id' ]][ 'label' ] = $record [ 'label' ];
$prods [ $record [ 'id' ]][ 'qty' ] = $record [ 'qty' ];
$prods [ $record [ 'id' ]][ 'incdec' ] = $record [ 'incdec' ];
2011-08-18 18:16:04 +02:00
$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
2015-03-11 14:55:45 +01:00
* @ see getFather
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
{
2015-05-03 21:15:08 +02:00
$sql = " SELECT p.rowid, p.label as label, p.ref as ref, pa.fk_product_pere as id, p.fk_product_type, pa.qty " ;
2010-03-04 20:42:01 +01:00
$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
*
2015-03-11 14:55:45 +01:00
* @ param int $id Id of product to search childs of
* @ param int $firstlevelonly Return only direct child
* @ return array Prod
2008-07-27 22:47:13 +02:00
*/
2015-03-11 14:55:45 +01:00
function getChildsArbo ( $id , $firstlevelonly = 0 )
2008-07-27 22:47:13 +02:00
{
2015-02-23 12:19:19 +01:00
$sql = " SELECT p.rowid, p.label as label, pa.qty as qty, pa.fk_product_fils as id, p.fk_product_type, pa.incdec " ;
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
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::getChildsArbo' , LOG_DEBUG );
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
{
2015-02-23 12:19:19 +01:00
$prods [ $rec [ 'rowid' ]] = array (
0 => $rec [ 'id' ],
1 => $rec [ 'qty' ],
2 => $rec [ 'fk_product_type' ],
3 => $this -> db -> escape ( $rec [ 'label' ]),
4 => $rec [ 'incdec' ]
);
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']);
2015-03-11 14:55:45 +01:00
if ( empty ( $firstlevelonly ))
2010-03-04 20:42:01 +01:00
{
2015-03-11 14:55:45 +01:00
$listofchilds = $this -> getChildsArbo ( $rec [ 'id' ]);
foreach ( $listofchilds as $keyChild => $valueChild )
{
$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 )
{
2015-01-25 01:20:58 +01:00
global $conf , $langs ;
2015-03-11 14:55:45 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/product.lib.php' ;
2008-07-27 22:47:13 +02:00
$result = '' ;
2015-01-20 20:45:38 +01:00
$newref = $this -> ref ;
if ( $maxlength ) $newref = dol_trunc ( $newref , $maxlength , 'middle' );
2015-03-30 03:14:07 +02:00
if ( $this -> type == Product :: TYPE_PRODUCT ) $label = '<u>' . $langs -> trans ( " ShowProduct " ) . '</u>' ;
if ( $this -> type == Product :: TYPE_SERVICE ) $label = '<u>' . $langs -> trans ( " ShowService " ) . '</u>' ;
2015-01-24 15:36:47 +01:00
if ( ! empty ( $this -> ref ))
2015-01-25 01:20:58 +01:00
$label .= '<br><b>' . $langs -> trans ( 'ProductRef' ) . ':</b> ' . $this -> ref ;
2015-01-24 15:36:47 +01:00
if ( ! empty ( $this -> label ))
2015-01-25 01:20:58 +01:00
$label .= '<br><b>' . $langs -> trans ( 'ProductLabel' ) . ':</b> ' . $this -> label ;
2015-03-11 14:55:45 +01:00
$tmptext = '' ;
if ( $this -> weight ) $tmptext .= " <br> " . $langs -> trans ( " Weight " ) . ': ' . $this -> weight . ' ' . measuring_units_string ( $this -> weight_units , " weight " );
if ( $this -> length ) $tmptext .= " <br> " . $langs -> trans ( " Length " ) . ': ' . $this -> length . ' ' . measuring_units_string ( $this -> length_units , 'length' );
if ( $this -> surface ) $tmptext .= " <br> " . $langs -> trans ( " Surface " ) . ': ' . $this -> surface . ' ' . measuring_units_string ( $this -> surface_units , 'surface' );
if ( $this -> volume ) $tmptext .= " <br> " . $langs -> trans ( " Volume " ) . ': ' . $this -> volume . ' ' . measuring_units_string ( $this -> volume_units , 'volume' );
if ( $tmptext ) $label .= $tmptext ;
2015-01-25 01:20:58 +01:00
if ( ! empty ( $this -> entity ))
$label .= '<br>' . $this -> show_photos ( $conf -> product -> multidir_output [ $this -> entity ], 1 , 1 , 0 , 0 , 0 , 80 );
$linkclose = '" title="' . str_replace ( '\n' , '' , dol_escape_htmltag ( $label , 1 )) . '" class="classfortooltip">' ;
2015-01-20 20:45:38 +01:00
if ( $option == 'supplier' ) {
2015-03-15 14:04:07 +01:00
$link = '<a href="' . DOL_URL_ROOT . '/product/fournisseurs.php?id=' . $this -> id . $linkclose ;
$linkend = '</a>' ;
2015-01-20 20:45:38 +01:00
} else if ( $option == 'stock' ) {
2015-03-15 14:04:07 +01:00
$link = '<a href="' . DOL_URL_ROOT . '/product/stock/product.php?id=' . $this -> id . $linkclose ;
$linkend = '</a>' ;
2015-01-20 20:45:38 +01:00
} else if ( $option == 'composition' ) {
2015-03-15 14:04:07 +01:00
$link = '<a href="' . DOL_URL_ROOT . '/product/composition/card.php?id=' . $this -> id . $linkclose ;
$linkend = '</a>' ;
2015-01-20 20:45:38 +01:00
} else if ( $option == 'category' ) {
2015-03-15 14:04:07 +01:00
$link = '<a href="' . DOL_URL_ROOT . '/categories/categorie.php?id=' . $this -> id . '&type=0' . $linkclose ;
2015-01-20 20:45:38 +01:00
} else {
2015-03-15 14:04:07 +01:00
$link = '<a href="' . DOL_URL_ROOT . '/product/card.php?id=' . $this -> id . $linkclose ;
$linkend = '</a>' ;
2011-05-22 04:14:01 +02:00
}
2008-07-27 22:47:13 +02:00
if ( $withpicto ) {
2015-03-30 03:14:07 +02:00
if ( $this -> type == Product :: TYPE_PRODUCT ) $result .= ( $link . img_object ( $langs -> trans ( " ShowProduct " ) . ' ' . $this -> label , 'product' , 'class="classfortooltip"' ) . $linkend . ' ' );
if ( $this -> type == Product :: TYPE_SERVICE ) $result .= ( $link . img_object ( $langs -> trans ( " ShowService " ) . ' ' . $this -> label , 'service' , 'class="classfortooltip"' ) . $linkend . ' ' );
2008-07-27 22:47:13 +02:00
}
2015-03-15 14:04:07 +01:00
$result .= $link . $newref . $linkend ;
2008-07-27 22:47:13 +02:00
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
2014-03-04 18:55:55 +01:00
* @ param int $type 0 = Sell , 1 = Buy , 2 = Batch Number management
2011-12-19 18:24:49 +01:00
* @ 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
{
2014-03-08 14:25:59 +01:00
switch ( $type )
2014-03-04 18:55:55 +01:00
{
case 0 :
2010-08-12 18:19:52 +02:00
return $this -> LibStatut ( $this -> status , $mode , $type );
2014-03-04 18:55:55 +01:00
case 1 :
return $this -> LibStatut ( $this -> status_buy , $mode , $type );
case 2 :
return $this -> LibStatut ( $this -> status_batch , $mode , $type );
default :
//Simulate previous behavior but should return an error string
2010-08-12 18:19:52 +02:00
return $this -> LibStatut ( $this -> status_buy , $mode , $type );
2014-03-04 18:55:55 +01:00
}
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
2014-03-04 18:55:55 +01:00
* @ param int $type 0 = Status " to sell " , 1 = Status " to buy " , 2 = Status " to Batch "
2011-12-19 18:24:49 +01:00
* @ 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' );
2014-10-02 09:30:31 +02:00
if ( ! empty ( $conf -> productbatch -> enabled )) $langs -> load ( " productbatch " );
2010-08-19 22:58:08 +02:00
2014-03-04 18:55:55 +01:00
if ( $type == 2 )
{
2014-03-08 14:25:59 +01:00
switch ( $mode )
2014-03-04 18:55:55 +01:00
{
case 0 :
return ( $status == 0 ? $langs -> trans ( 'ProductStatusNotOnBatch' ) : $langs -> trans ( 'ProductStatusOnBatch' ));
case 1 :
return ( $status == 0 ? $langs -> trans ( 'ProductStatusNotOnBatchShort' ) : $langs -> trans ( 'ProductStatusOnBatchShort' ));
case 2 :
return $this -> LibStatut ( $status , 3 , 2 ) . ' ' . $this -> LibStatut ( $status , 1 , 2 );
case 3 :
2014-03-08 14:25:59 +01:00
if ( $status == 0 )
2014-03-04 18:55:55 +01:00
{
return img_picto ( $langs -> trans ( 'ProductStatusNotOnBatch' ), 'statut5' );
}
else
{
return img_picto ( $langs -> trans ( 'ProductStatusOnBatch' ), 'statut4' );
}
case 4 :
return $this -> LibStatut ( $status , 3 , 2 ) . ' ' . $this -> LibStatut ( $status , 0 , 2 );
case 5 :
return $this -> LibStatut ( $status , 1 , 2 ) . ' ' . $this -> LibStatut ( $status , 3 , 2 );
default :
return $langs -> trans ( 'Unknown' );
}
}
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
2015-04-27 01:36:13 +02:00
* @ param double $price Unit price HT of product , used to calculate average weighted price ( PMP in french ) . If 0 , average weighted price is not changed .
2015-01-30 01:51:28 +01:00
* @ param string $inventorycode Inventory code
2011-12-19 18:24:49 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2015-01-30 01:51:28 +01:00
function correct_stock ( $user , $id_entrepot , $nbpiece , $movement , $label = '' , $price = 0 , $inventorycode = '' )
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 );
2015-01-30 01:51:28 +01:00
$result = $movementstock -> _create ( $user , $this -> id , $id_entrepot , $op [ $movement ], $movement , $price , $label , $inventorycode );
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
{
2014-07-04 16:18:54 +02:00
$this -> error = $movementstock -> error ;
2015-04-12 04:01:28 +02:00
$this -> errors = $movementstock -> errors ;
2008-03-31 07:56:45 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
}
2004-08-29 15:06:30 +02:00
2015-02-11 22:54:06 +01:00
/**
* Adjust stock in a warehouse for product with batch number
*
* @ 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
* @ param date $dlc eat - by date
* @ param date $dluo sell - by date
* @ param string $lot Lot number
* @ param string $inventorycode Inventory code
* @ return int < 0 if KO , > 0 if OK
*/
function correct_stock_batch ( $user , $id_entrepot , $nbpiece , $movement , $label = '' , $price = 0 , $dlc = '' , $dluo = '' , $lot = '' , $inventorycode = '' )
{
if ( $id_entrepot )
{
$this -> db -> begin ();
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php' ;
$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 , $inventorycode , '' , $dlc , $dluo , $lot );
if ( $result >= 0 )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $movementstock -> error ;
$this -> errors = $movementstock -> errors ;
$this -> db -> rollback ();
return - 1 ;
}
}
}
2008-03-31 07:56:45 +02:00
/**
2015-04-12 04:01:28 +02:00
* Load information about stock of a product into stock_reel , stock_warehouse [] ( including stock_warehouse [ idwarehouse ] -> detail_batch for batch products )
2011-08-27 19:25:09 +02:00
*
2015-04-12 17:08:14 +02:00
* @ return int < 0 if KO , > 0 if OK
* @ see load_virtual_stock , getBatchInfo
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
2014-03-05 10:30:06 +01:00
$sql = " SELECT ps.reel, ps.fk_entrepot, ps.pmp, ps.rowid " ;
2012-07-02 12:07:42 +02:00
$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
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::load_stock " , LOG_DEBUG );
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 ;
2014-07-15 11:22:16 +02:00
$this -> stock_warehouse [ $row -> fk_entrepot ] -> id = $row -> rowid ;
2014-03-05 10:30:06 +01:00
if ( $this -> hasbatch ()) $this -> stock_warehouse [ $row -> fk_entrepot ] -> detail_batch = Productbatch :: findAll ( $this -> db , $row -> rowid , 1 );
2010-07-14 17:41:39 +02:00
$this -> stock_reel += $row -> reel ;
2008-07-27 22:47:13 +02:00
$i ++ ;
}
}
$this -> db -> free ( $result );
2015-03-01 16:04:34 +01:00
$this -> load_virtual_stock (); // This also load stats_commande_fournisseur, ...
2008-07-27 22:47:13 +02:00
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 ;
}
}
2014-07-03 10:24:29 +02:00
/**
2015-04-12 17:08:14 +02:00
* Load information about objects that are delat between physical and virtual stock of a product
2014-07-03 10:24:29 +02:00
*
2015-04-12 17:08:14 +02:00
* @ return int < 0 if KO , > 0 if OK
* @ see load_stock , getBatchInfo
2014-07-03 10:24:29 +02:00
*/
2014-11-01 21:38:50 +01:00
function load_virtual_stock ()
{
global $conf ;
2014-07-03 10:24:29 +02:00
2014-11-01 21:38:50 +01:00
$stock_commande_client = 0 ;
$stock_commande_fournisseur = 0 ;
$stock_sending_client = 0 ;
$stock_reception_fournisseur = 0 ;
2014-07-04 15:06:45 +02:00
2015-03-01 16:04:34 +01:00
if ( ! empty ( $conf -> commande -> enabled ))
{
2014-11-01 21:38:50 +01:00
$result = $this -> load_stats_commande ( 0 , '1,2' );
if ( $result < 0 ) dol_print_error ( $db , $this -> error );
$stock_commande_client = $this -> stats_commande [ 'qty' ];
}
2015-03-01 16:04:34 +01:00
if ( ! empty ( $conf -> expedition -> enabled ))
{
2014-11-01 21:38:50 +01:00
$result = $this -> load_stats_sending ( 0 , '1,2' );
if ( $result < 0 ) dol_print_error ( $db , $this -> error );
$stock_sending_client = $this -> stats_expedition [ 'qty' ];
}
2015-03-01 16:04:34 +01:00
if ( ! empty ( $conf -> fournisseur -> enabled ))
{
$result = $this -> load_stats_commande_fournisseur ( 0 , '1,2,3,4' );
2014-11-01 21:38:50 +01:00
if ( $result < 0 ) dol_print_error ( $db , $this -> error );
$stock_commande_fournisseur = $this -> stats_commande_fournisseur [ 'qty' ];
$result = $this -> load_stats_reception ( 0 , '4' );
if ( $result < 0 ) dol_print_error ( $db , $this -> error );
$stock_reception_fournisseur = $this -> stats_reception [ 'qty' ];
}
2014-07-03 10:24:29 +02:00
2014-11-01 21:38:50 +01:00
// Stock decrease mode
if ( ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SHIPMENT )) {
$this -> stock_theorique = $this -> stock_reel - $stock_commande_client + $stock_sending_client ;
}
if ( ! empty ( $conf -> global -> STOCK_CALCULATE_ON_VALIDATE_ORDER )) {
$this -> stock_theorique = $this -> stock_reel ;
}
if ( ! empty ( $conf -> global -> STOCK_CALCULATE_ON_BILL )) {
$this -> stock_theorique = $this -> stock_reel - $stock_commande_client ;
}
// Stock Increase mode
if ( ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER )) {
$this -> stock_theorique += $stock_commande_fournisseur - $stock_reception_fournisseur ;
}
if ( ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER )) {
$this -> stock_theorique -= $stock_reception_fournisseur ;
}
if ( ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SUPPLIER_BILL )) {
$this -> stock_theorique += $stock_commande_fournisseur - $stock_reception_fournisseur ;
}
}
2008-07-27 22:47:13 +02:00
2015-04-12 17:08:14 +02:00
/**
* Load existing information about a serial
*
2015-04-12 19:20:48 +02:00
* @ param string $batch Lot / serial number
* @ return array Array with record into product_batch
2015-04-12 17:08:14 +02:00
* @ see load_stock , load_virtual_stock
*/
function loadBatchInfo ( $batch )
{
$result = array ();
$sql = " SELECT pb.batch, pb.eatby, pb.sellby, SUM(pb.qty) FROM " . MAIN_DB_PREFIX . " product_batch as pb, " . MAIN_DB_PREFIX . " product_stock as ps " ;
$sql .= " WHERE pb.fk_product_stock = ps.rowid AND ps.fk_product = " . $this -> id . " AND pb.batch = ' " . $this -> db -> escape ( $batch ) . " ' " ;
$sql .= " GROUP BY pb.batch, pb.eatby, pb.sellby " ;
dol_syslog ( get_class ( $this ) . " ::loadBatchInfo load first entry found for lot/serial = " . $batch , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
$result [] = array ( 'batch' => $batch , 'eatby' => $this -> db -> jdate ( $obj -> eatby ), 'sellby' => $this -> db -> jdate ( $obj -> sellby ), 'qty' => $obj -> qty );
$i ++ ;
}
return $result ;
}
else
{
dol_print_error ( $this -> db );
$this -> db -> rollback ();
return array ();
}
}
2008-07-27 22:47:13 +02:00
/**
2014-10-18 14:56:06 +02:00
* Move an uploaded file described into $file array into target directory $sdir .
2011-08-27 19:25:09 +02:00
*
2014-10-18 14:56:06 +02:00
* @ param string $sdir Target directory
* @ param string $file Array of file info of file to upload : array ( 'name' =>... , 'tmp_name' =>... )
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2015-03-06 13:12:33 +01:00
function add_photo ( $sdir , $file )
2008-07-27 22:47:13 +02:00
{
2015-02-06 21:26:41 +01:00
global $conf ;
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
2014-10-18 14:56:06 +02:00
$result = 0 ;
2015-02-06 21:26:41 +01:00
$dir = $sdir ;
if ( ! empty ( $conf -> global -> PRODUCT_USE_OLD_PATH_FOR_PHOTO )) $dir .= '/' . get_exdir ( $this -> id , 2 ) . $this -> id . " /photos " ;
2015-03-06 13:12:33 +01:00
else $dir .= '/' . dol_sanitizeFileName ( $this -> ref );
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 ;
2015-03-06 13:12:33 +01:00
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
2015-03-06 13:12:33 +01:00
$this -> add_thumb ( $originImage );
2008-07-27 22:47:13 +02:00
}
}
2014-10-18 14:56:06 +02:00
if ( is_numeric ( $result ) && $result > 0 ) return 1 ;
else return - 1 ;
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' ;
2015-05-03 21:15:08 +02:00
2015-04-27 16:44:12 +02:00
global $conf ;
2009-10-29 03:15:37 +01:00
2015-02-06 21:26:41 +01:00
$dir = $sdir ;
2015-04-27 16:44:12 +02:00
if ( ! empty ( $conf -> global -> PRODUCT_USE_OLD_PATH_FOR_PHOTO )) $dir .= '/' . get_exdir ( $this -> id , 2 ) . $this -> id . " /photos/ " ;
else $dir .= '/' . dol_sanitizeFileName ( $this -> ref ) . '/' ;
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 )
2014-07-04 15:06:45 +02:00
* @ param int $nbbyrow Number of image per line or - 1 to use div . Used only if size = 1.
2011-12-19 18:24:49 +01:00
* @ 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
2015-02-06 21:26:41 +01:00
$dir = $sdir . '/' ;
$pdir = '/' ;
if ( ! empty ( $conf -> global -> PRODUCT_USE_OLD_PATH_FOR_PHOTO ))
{
$dir .= get_exdir ( $this -> id , 2 ) . $this -> id . " /photos/ " ;
$pdir .= get_exdir ( $this -> id , 2 ) . $this -> id . " /photos/ " ;
}
else
{
$dir .= $this -> ref . '/' ;
$pdir .= $this -> ref . '/' ;
}
2008-07-27 22:47:13 +02:00
$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
2014-10-18 14:56:06 +02:00
if ( dol_is_file ( $dir . $file ) && preg_match ( '/(' . $this -> regeximgext . ')$/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 );
2014-07-04 15:06:45 +02:00
if ( $nbbyrow > 0 )
{
if ( $nbphoto == 1 ) $return .= '<table width="100%" valign="top" align="center" border="0" cellpadding="2" cellspacing="2">' ;
2010-12-15 19:15:08 +01:00
2014-07-04 15:06:45 +02:00
if ( $nbphoto % $nbbyrow == 1 ) $return .= '<tr align=center valign=middle border=1>' ;
$return .= '<td width="' . ceil ( 100 / $nbbyrow ) . '%" class="photo">' ;
}
else if ( $nbbyrow < 0 ) $return .= '<div class="inline-block">' ;
2010-12-15 19:15:08 +01:00
$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 -->' ;
2014-11-23 12:39:53 +01:00
$return .= '<img class="photo photowithmargin" border="0" ' . ( $conf -> dol_use_jmobile ? 'max-height' : '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 -->' ;
2014-11-23 12:39:53 +01:00
$return .= '<img class="photo photowithmargin" border="0" ' . ( $conf -> dol_use_jmobile ? 'max-height' : '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
2014-10-18 14:56:06 +02:00
if ( $photo_vignette && preg_match ( '/(' . $this -> regeximgext . ')$/i' , $photo ) && ( $product -> imgWidth > $maxWidth || $product -> imgHeight > $maxHeight ))
2010-12-15 19:15:08 +01:00
{
2014-07-19 16:41:14 +02:00
$return .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $this -> 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
2014-07-19 16:41:14 +02:00
$return .= '<a href="' . DOL_URL_ROOT . '/core/photos_resize.php?modulepart=' . urlencode ( 'produit|service' ) . '&id=' . $this -> 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> ' ;
2010-12-15 19:15:08 +01:00
// Link to delete
2014-07-19 16:41:14 +02:00
$return .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $this -> id . '&action=delete&file=' . urlencode ( $pdir . $viewfilename ) . '">' ;
2010-12-15 19:15:08 +01:00
$return .= img_delete () . '</a>' ;
}
}
$return .= " \n " ;
2014-07-04 15:06:45 +02:00
if ( $nbbyrow > 0 )
{
$return .= '</td>' ;
if (( $nbphoto % $nbbyrow ) == 0 ) $return .= '</tr>' ;
}
else if ( $nbbyrow < 0 ) $return .= '</div>' ;
2010-12-15 19:15:08 +01:00
}
if ( $size == 0 ) { // Format origine
2014-10-18 14:56:06 +02:00
$return .= '<img class="photo photowithmargin" 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
2014-07-19 16:41:14 +02:00
$return .= '<a href="' . DOL_URL_ROOT . '/core/photos_resize.php?modulepart=' . urlencode ( 'produit|service' ) . '&id=' . $this -> 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> ' ;
2010-12-15 19:15:08 +01:00
// Link to delete
2014-07-19 16:41:14 +02:00
$return .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $this -> id . '&action=delete&file=' . urlencode ( $pdir . $viewfilename ) . '">' ;
2010-12-15 19:15:08 +01:00
$return .= img_delete () . '</a>' ;
}
}
}
// On continue ou on arrete de boucler ?
if ( $nbmax && $nbphoto >= $nbmax ) break ;
}
}
}
2006-11-30 15:23:38 +01:00
2014-07-04 15:06:45 +02:00
if ( $size == 1 )
2009-07-22 19:02:49 +02:00
{
2014-07-04 15:06:45 +02:00
if ( $nbbyrow > 0 )
2010-02-20 16:07:46 +01:00
{
2014-07-04 15:06:45 +02:00
// Ferme tableau
while ( $nbphoto % $nbbyrow )
{
$return .= '<td width="' . ceil ( 100 / $nbbyrow ) . '%"> </td>' ;
$nbphoto ++ ;
}
2008-07-27 22:47:13 +02:00
2014-07-04 15:06:45 +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
2014-10-18 14:56:06 +02:00
if ( dol_is_file ( $dir . $file ) && preg_match ( '/(' . $this -> regeximgext . ')$/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 = '' ;
2014-10-18 14:56:06 +02:00
if ( preg_match ( '/(' . $this -> regeximgext . ')$/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
2014-10-18 14:56:06 +02:00
if ( preg_match ( '/(' . $this -> regeximgext . ')$/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 ()
{
2015-04-08 19:26:36 +02:00
return ( $this -> type == Product :: TYPE_PRODUCT ? 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
{
2015-03-30 03:14:07 +02:00
return ( $this -> type == Product :: TYPE_SERVICE ? true : false );
2008-07-27 22:47:13 +02:00
}
2007-01-31 18:52:54 +01:00
2014-02-10 00:24:20 +01:00
/**
2014-03-16 17:37:54 +01:00
* Get a barcode from the module to generate barcode values .
2014-02-10 00:24:20 +01:00
* Return value is stored into this -> barcode
*
* @ param Product $object Object product or service
2014-03-16 17:37:54 +01:00
* @ param string $type Barcode type ( ean , isbn , ... )
2014-02-10 00:24:20 +01:00
* @ return void
*/
function get_barcode ( $object , $type = '' )
{
global $conf ;
2014-04-13 02:08:57 +02:00
2014-03-16 17:37:54 +01:00
$result = '' ;
2014-02-10 00:24:20 +01:00
if ( ! empty ( $conf -> global -> BARCODE_PRODUCT_ADDON_NUM ))
{
$dirsociete = array_merge ( array ( '/core/modules/barcode/' ), $conf -> modules_parts [ 'barcode' ]);
foreach ( $dirsociete as $dirroot )
{
$res = dol_include_once ( $dirroot . $conf -> global -> BARCODE_PRODUCT_ADDON_NUM . '.php' );
if ( $res ) break ;
}
$var = $conf -> global -> BARCODE_PRODUCT_ADDON_NUM ;
$mod = new $var ;
2014-03-16 17:37:54 +01:00
$result = $mod -> getNextValue ( $object , $type );
2014-02-10 00:24:20 +01:00
2014-03-16 17:37:54 +01:00
dol_syslog ( get_class ( $this ) . " ::get_barcode barcode= " . $result . " module= " . $var );
2014-02-10 00:24:20 +01:00
}
2014-03-16 17:37:54 +01:00
return $result ;
2014-02-10 00:24:20 +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 ;
2014-03-04 18:55:55 +01:00
$this -> tobatch = 0 ;
2011-10-01 01:40:15 +02:00
$this -> note = 'This is a comment (private)' ;
2014-02-19 01:51:19 +01:00
$this -> barcode =- 1 ; // Create barcode automatically
2011-10-01 01:40:15 +02:00
}
2014-03-04 18:55:55 +01:00
2015-02-26 14:15:33 +01:00
/**
* Returns the text label from units dictionnary
*
* @ param string $type Label type ( long or short )
* @ return string | int < 0 if ko , label if ok
*/
2015-04-21 15:49:58 +02:00
function getLabelOfUnit ( $type = 'long' )
2015-02-26 14:15:33 +01:00
{
global $langs ;
if ( ! $this -> fk_unit ) {
return '' ;
}
$langs -> load ( 'products' );
$this -> db -> begin ();
$label_type = 'label' ;
if ( $type == 'short' )
{
$label_type = 'short_label' ;
}
$sql = 'select ' . $label_type . ' from ' . MAIN_DB_PREFIX . 'c_units where rowid=' . $this -> fk_unit ;
$resql = $this -> db -> query ( $sql );
if ( $resql && $this -> db -> num_rows ( $resql ) > 0 )
{
$res = $this -> db -> fetch_array ( $resql );
$label = $res [ $label_type ];
$this -> db -> free ( $resql );
return $label ;
}
else
{
$this -> error = $this -> db -> error () . ' sql=' . $sql ;
2015-04-21 15:49:58 +02:00
dol_syslog ( get_class ( $this ) . " ::getLabelOfUnit Error " . $this -> error , LOG_ERR );
2015-02-26 14:15:33 +01:00
return - 1 ;
}
}
2014-03-04 18:55:55 +01:00
/**
* Return if object has a sell - by date or eat - by date
*
* @ return boolean True if it ' s has
*/
function hasbatch ()
{
return ( $this -> status_batch == 1 ? true : false );
}
2014-03-05 10:30:06 +01:00
2014-09-07 13:18:56 +02:00
2014-08-29 19:19:26 +02:00
/**
2014-09-07 13:18:56 +02:00
* Return minimum product recommended price
2014-08-29 19:19:26 +02:00
*
2014-09-07 13:18:56 +02:00
* @ return int Minimum recommanded price that is higher price among all suppliers * PRODUCT_MINIMUM_RECOMMENDED_PRICE
2014-08-29 19:19:26 +02:00
*/
2014-09-07 13:18:56 +02:00
function min_recommended_price ()
2014-08-29 19:19:26 +02:00
{
global $conf ;
2014-09-07 13:18:56 +02:00
$maxpricesupplier = 0 ;
if ( ! empty ( $conf -> global -> PRODUCT_MINIMUM_RECOMMENDED_PRICE ))
2014-08-29 19:19:26 +02:00
{
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php' ;
$product_fourn = new ProductFournisseur ( $this -> db );
$product_fourn_list = $product_fourn -> list_product_fournisseur_price ( $this -> id , '' , '' );
2014-09-07 13:18:56 +02:00
2014-08-29 19:19:26 +02:00
if ( is_array ( $product_fourn_list ) && count ( $product_fourn_list ) > 0 )
2014-09-07 13:18:56 +02:00
{
2014-08-29 19:19:26 +02:00
foreach ( $product_fourn_list as $productfourn )
2014-09-07 13:18:56 +02:00
{
if ( $productfourn -> fourn_unitprice > $maxpricesupplier )
2014-08-29 19:19:26 +02:00
{
$maxpricesupplier = $productfourn -> fourn_unitprice ;
}
}
2014-09-07 13:18:56 +02:00
$maxpricesupplier *= $conf -> global -> PRODUCT_MINIMUM_RECOMMENDED_PRICE ;
2014-08-29 19:19:26 +02:00
}
}
2014-09-07 13:18:56 +02:00
return $maxpricesupplier ;
2014-08-29 19:19:26 +02:00
}
2015-04-18 18:11:01 +02:00
/**
* Function used to replace a thirdparty id with another one .
*
* @ param DoliDB $db Database handler
* @ param int $origin_id Old thirdparty id
* @ param int $dest_id New thirdparty id
* @ return bool
*/
public static function replaceThirdparty ( DoliDB $db , $origin_id , $dest_id )
{
$tables = array (
'product_customer_price' ,
'product_customer_price_log'
);
return CommonObject :: commonReplaceThirdparty ( $db , $origin_id , $dest_id , $tables );
}
2002-04-30 17:52:56 +02:00
}