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 >
2015-05-07 11:59:45 +02:00
* Copyright ( C ) 2005 - 2015 Regis Houssin < regis . houssin @ capnetworks . com >
2014-10-02 09:30:31 +02:00
* 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 >
2016-03-17 09:40:05 +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 >
2016-03-30 11:38:39 +02:00
* Copyright ( C ) 2013 - 2016 Marcos García < marcosgdf @ gmail . com >
2017-11-01 07:19:36 +01:00
* Copyright ( C ) 2011 - 2017 Alexandre Spangaro < aspangaro @ zendsi . com >
2014-10-02 09:30:31 +02:00
* Copyright ( C ) 2014 Henry Florian < florian . henry @ open - concept . pro >
2016-03-17 09:40:05 +01:00
* Copyright ( C ) 2014 - 2016 Philippe Grand < philippe . grand @ atoo - net . com >
* Copyright ( C ) 2014 Ion agorria < ion @ agorria . com >
2018-10-17 10:01:02 +02:00
* Copyright ( C ) 2016 - 2018 Ferran Marcet < fmarcet @ 2 byte . es >
2017-06-09 12:50:56 +02:00
* Copyright ( C ) 2017 Gustavo Novaro
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' ;
2016-12-05 18:08:09 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/entrepot.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-11-15 18:23:42 +01:00
protected $childtables = array ( 'supplier_proposaldet' , 'propaldet' , 'commandedet' , 'facturedet' , 'contratdet' , 'facture_fourn_det' , 'commande_fournisseurdet' ); // To test if we can delete object
2017-10-25 22:02:07 +02:00
public $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' ;
2017-01-12 16:11:45 +01:00
public $regeximgext = '\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.xpm|\.xbm' ; // See also into images.lib.php
2014-10-18 14:56:06 +02:00
2015-04-23 23:21:06 +02:00
/*
* @ deprecated
* @ see label
*/
2017-01-12 16:11:45 +01:00
public $libelle ;
2015-04-23 23:21:06 +02:00
/**
* Product label
* @ var string
*/
2017-01-12 16:11:45 +01:00
public $label ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Product descripion
* @ var string
*/
public $description ;
2015-03-30 03:14:07 +02:00
/**
* Check TYPE constants
* @ var int
*/
2017-01-12 16:11:45 +01:00
public $type = self :: TYPE_PRODUCT ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Selling price
* @ var float
*/
public $price ; // Price net
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Price with tax
* @ var float
*/
public $price_ttc ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Minimum price net
* @ var float
*/
public $price_min ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Minimum price with tax
* @ var float
*/
public $price_min_ttc ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/*
* Base price ( 'TTC' for price including tax or 'HT' for net price )
* @ var float
*/
public $price_base_type ;
2017-06-09 12:50:56 +02:00
2011-10-09 17:37:27 +02:00
//! Arrays for multiprices
2015-11-06 11:24:48 +01:00
public $multiprices = array ();
public $multiprices_ttc = array ();
public $multiprices_base_type = array ();
public $multiprices_min = array ();
public $multiprices_min_ttc = array ();
public $multiprices_tva_tx = array ();
public $multiprices_recuperableonly = array ();
2017-06-09 12:50:56 +02:00
2012-10-22 17:47:15 +02:00
//! Price by quantity arrays
2017-01-12 16:11:45 +01:00
public $price_by_qty ;
public $prices_by_qty = array ();
public $prices_by_qty_id = array ();
public $prices_by_qty_list = array ();
2017-06-09 12:50:56 +02:00
2016-04-18 21:02:14 +02:00
//! Default VAT code for product (link to code into llx_c_tva but without foreign keys)
2017-01-12 16:11:45 +01:00
public $default_vat_code ;
2017-06-09 12:50:56 +02:00
2011-10-09 17:37:27 +02:00
//! Default VAT rate of product
2017-01-12 16:11:45 +01:00
public $tva_tx ;
2017-06-09 12:50:56 +02:00
2011-10-09 17:37:27 +02:00
//! French VAT NPR (0 or 1)
2017-02-27 00:56:44 +01:00
public $tva_npr = 0 ;
2017-06-09 12:50:56 +02:00
2016-02-19 02:41:00 +01:00
//! Other local taxes
2017-01-12 16:11:45 +01:00
public $localtax1_tx ;
public $localtax2_tx ;
public $localtax1_type ;
public $localtax2_type ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Stock real
* @ var int
*/
public $stock_reel = 0 ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Stock virtual
* @ var int
*/
public $stock_theorique ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Cost price
* @ var float
*/
public $cost_price ;
2017-06-09 12:50:56 +02:00
2011-05-18 18:20:43 +02:00
//! Average price value for product entry into stock (PMP)
2017-01-12 16:11:45 +01:00
public $pmp ;
2017-06-09 12:50:56 +02:00
2018-04-21 15:08:46 +02:00
/**
2017-01-12 16:11:45 +01:00
* Stock alert
* @ var int
*/
2018-04-21 15:08:46 +02:00
public $seuil_stock_alerte = 0 ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Ask for replenishment when $desiredstock < $stock_reel
*/
2018-04-21 15:08:46 +02:00
public $desiredstock = 0 ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/*
* Service expiration
*/
public $duration_value ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Exoiration unit
*/
public $duration_unit ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Status indicates whether the product is on sale '1' or not '0'
* @ var int
*/
2018-04-21 15:08:46 +02:00
public $status = 0 ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Status indicate whether the product is available for purchase '1' or not '0'
* @ var int
*/
2018-04-21 15:08:46 +02:00
public $status_buy = 0 ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* Status indicates whether the product is a finished product '1' or a raw material '0'
* @ var int
*/
public $finished ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
/**
* We must manage lot / batch number , sell - by date and so on : '1' : yes '0' : no
* @ var int
*/
2018-04-21 15:08:46 +02:00
public $status_batch = 0 ;
2017-01-12 16:11:45 +01:00
/**
* Customs code
* @ var
*/
public $customcode ;
2010-11-02 12:22:41 +01:00
2015-11-06 11:24:48 +01:00
/**
* Product URL
* @ var string
*/
public $url ;
2008-07-27 22:47:13 +02:00
//! Unites de mesure
2017-01-12 16:11:45 +01:00
public $weight ;
public $weight_units ;
public $length ;
public $length_units ;
public $surface ;
public $surface_units ;
public $volume ;
public $volume_units ;
public $accountancy_code_sell ;
2018-04-21 12:12:46 +02:00
public $accountancy_code_sell_intra ;
public $accountancy_code_sell_export ;
public $accountancy_code_buy ;
2008-07-27 22:47:13 +02:00
2017-01-12 16:11:45 +01:00
/**
* Main barcode
* barcode value
2017-06-09 12:50:56 +02:00
* @ var
2017-01-12 16:11:45 +01:00
*/
public $barcode ;
2010-03-20 19:39:32 +01:00
2017-01-12 16:11:45 +01:00
/**
* Additional barcodes ( Some products have different barcodes according to the country of origin of manufacture )
* @ var array
*/
public $barcodes_extra = array ();
2008-07-27 22:47:13 +02:00
2017-01-12 16:11:45 +01:00
public $stats_propale = array ();
public $stats_commande = array ();
public $stats_contrat = array ();
public $stats_facture = array ();
2017-02-27 00:56:44 +01:00
public $stats_commande_fournisseur = array ();
2015-03-14 14:55:41 +01:00
2017-01-12 16:11:45 +01:00
public $multilangs = array ();
2008-07-27 22:47:13 +02:00
//! Taille de l'image
2017-01-12 16:11:45 +01:00
public $imgWidth ;
public $imgHeight ;
2008-07-27 22:47:13 +02:00
2017-01-12 16:11:45 +01:00
public $date_creation ;
public $date_modification ;
2011-10-01 01:30:32 +02:00
2008-07-27 22:47:13 +02:00
//! Id du fournisseur
2017-01-12 16:11:45 +01:00
public $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
2017-01-12 16:11:45 +01:00
public $product_id_already_linked ;
2010-05-26 16:52:32 +02:00
2018-04-21 15:08:46 +02:00
public $nbphoto = 0 ;
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
2017-01-12 16:11:45 +01:00
public $stock_warehouse = array ();
2012-07-02 12:07:42 +02:00
2017-01-12 16:11:45 +01:00
public $oldcopy ;
2010-07-14 17:41:39 +02:00
2017-02-27 00:56:44 +01:00
public $fk_price_expression ;
2010-07-14 17:41:39 +02:00
2018-06-16 14:47:33 +02:00
/* To store supplier price found */
2015-04-23 23:21:06 +02:00
public $fourn_pu ;
2016-10-01 16:33:42 +02:00
public $fourn_price_base_type ;
2018-06-16 14:47:33 +02:00
public $fourn_socid ;
2017-06-09 11:02:34 +02:00
2015-04-23 23:21:06 +02:00
/**
* @ deprecated
* @ see ref_supplier
*/
2017-01-12 16:11:45 +01:00
public $ref_fourn ;
2015-04-23 23:21:06 +02:00
public $ref_supplier ;
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-10-19 15:13:19 +02:00
/**
* Price is generated using multiprice rules
* @ var int
*/
public $price_autogen = 0 ;
2017-06-09 12:50:56 +02:00
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 ;
2017-06-09 12:50:56 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-09-11 20:35:38 +02:00
* Constructor
2011-08-27 19:25:09 +02:00
*
2011-12-05 19:41:38 +01:00
* @ param DoliDB $db Database handler
2008-07-27 22:47:13 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db )
2008-07-27 22:47:13 +02:00
{
global $langs ;
2011-12-05 19:41:38 +01:00
$this -> db = $db ;
2008-07-27 22:47:13 +02:00
$this -> canvas = '' ;
}
2008-08-04 02:30:52 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Check that ref and label are ok
2011-08-27 19:25:09 +02:00
*
2011-06-10 13:39:01 +02:00
* @ return int > 1 if OK , <= 0 if KO
2008-07-27 22:47:13 +02:00
*/
function check ()
{
2009-04-29 20:02:50 +02:00
$this -> ref = dol_sanitizeFileName ( stripslashes ( $this -> ref ));
2008-07-27 22:47:13 +02:00
$err = 0 ;
2010-08-24 21:51:56 +02:00
if ( dol_strlen ( trim ( $this -> ref )) == 0 )
2008-07-27 22:47:13 +02:00
$err ++ ;
2015-05-04 03:03:24 +02:00
if ( dol_strlen ( trim ( $this -> label )) == 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
2017-01-12 16:11:45 +01: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 ));
2015-05-04 03:03:24 +02:00
$this -> label = trim ( $this -> label );
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 ;
2016-02-19 02:41:00 +01:00
if ( empty ( $this -> localtax1_type )) $this -> localtax1_type = '0' ;
if ( empty ( $this -> localtax2_type )) $this -> localtax2_type = '0' ;
2017-06-09 11:02:34 +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 ;
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
//
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' );
}
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
//
2008-08-04 02:30:52 +02:00
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
}
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01: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' );
}
2017-06-09 12:50:56 +02:00
2017-01-12 16:11:45 +01:00
//
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
2017-11-01 07:19:36 +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 );
2017-11-01 07:19:36 +01:00
$this -> accountancy_code_sell_intra = trim ( $this -> accountancy_code_sell_intra );
$this -> accountancy_code_sell_export = trim ( $this -> accountancy_code_sell_export );
2008-07-27 22:47:13 +02:00
2017-01-12 16:11:45 +01:00
// Barcode value
$this -> barcode = trim ( $this -> barcode );
2014-06-20 15:05:39 +02:00
2017-11-01 07:19:36 +01:00
// Check parameters
2015-05-04 03:03:24 +02:00
if ( empty ( $this -> label ))
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 ;
}
2017-06-09 12:50:56 +02:00
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 );
}
2017-06-09 12:50:56 +02:00
2013-07-27 12:22:00 +02:00
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
2017-11-01 07:19:36 +01: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
2017-11-01 07:19:36 +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
2017-11-01 07:19:36 +01:00
// If error, this->errors[] is filled
$result = $this -> verify ();
2014-06-20 15:05:39 +02:00
2017-11-01 07:19:36 +01:00
if ( $result >= 0 )
{
2014-02-19 01:51:19 +01:00
$sql = " SELECT count(*) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product " ;
2017-05-30 18:50:54 +02:00
$sql .= " WHERE entity IN ( " . getEntity ( 'product' ) . " ) " ;
2017-05-12 16:55:11 +02:00
$sql .= " AND ref = ' " . $this -> db -> escape ( $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 " ;
2017-11-01 07:19:36 +01:00
$sql .= " , accountancy_code_sell_intra " ;
$sql .= " , accountancy_code_sell_export " ;
2014-02-19 01:51:19 +01:00
$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 );
2015-05-04 03:03:24 +02:00
$sql .= " , " . ( ! empty ( $this -> label ) ? " ' " . $this -> db -> escape ( $this -> label ) . " ' " : " null " );
2014-02-19 01:51:19 +01:00
$sql .= " , " . $user -> id ;
$sql .= " , " . $this -> type ;
$sql .= " , " . price2num ( $price_ht );
$sql .= " , " . price2num ( $price_ttc );
2017-09-15 15:41:07 +02:00
$sql .= " , ' " . $this -> db -> escape ( $this -> price_base_type ) . " ' " ;
2014-02-19 01:51:19 +01:00
$sql .= " , " . $this -> status ;
$sql .= " , " . $this -> status_buy ;
2017-09-15 15:41:07 +02:00
$sql .= " , ' " . $this -> db -> escape ( $this -> accountancy_code_buy ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> accountancy_code_sell ) . " ' " ;
2017-11-01 07:19:36 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> accountancy_code_sell_intra ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> accountancy_code_sell_export ) . " ' " ;
2017-09-15 15:41:07 +02:00
$sql .= " , ' " . $this -> db -> escape ( $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 )
{
2017-11-01 07:19:36 +01:00
$error ++ ;
2014-02-19 01:51:19 +01:00
}
}
else
2007-06-30 15:06:03 +02:00
{
2014-02-19 01:51:19 +01:00
$error ++ ;
2017-11-01 07:19:36 +01:00
$this -> error = $this -> db -> lasterror ();
2007-06-30 15:06:03 +02:00
}
}
else
{
2011-07-29 22:47:34 +02:00
$error ++ ;
2017-11-01 07:19:36 +01:00
$this -> error = 'ErrorFailedToGetInsertedId' ;
2007-06-30 15:06:03 +02:00
}
}
else
{
2011-07-29 22:47:34 +02:00
$error ++ ;
2017-11-01 07:19:36 +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 ++ ;
2017-11-01 07:19:36 +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 )
{
2017-11-01 07:19:36 +01:00
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_CREATE' , $user );
if ( $result < 0 ) { $error ++ ; }
// 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 ;
}
2017-11-26 18:43:43 +01:00
$rescode = $this -> check_barcode ( $this -> barcode , $this -> barcode_type_code );
if ( $rescode )
2014-02-19 01:51:19 +01:00
{
if ( $rescode == - 1 )
{
$this -> errors [] = 'ErrorBadBarCodeSyntax' ;
}
2017-11-26 18:43:43 +01:00
elseif ( $rescode == - 2 )
2014-02-19 01:51:19 +01:00
{
$this -> errors [] = 'ErrorBarCodeRequired' ;
}
2017-11-26 18:43:43 +01:00
elseif ( $rescode == - 3 )
2014-02-19 01:51:19 +01:00
{
2017-11-26 18:43:43 +01:00
// Note: Common usage is to have barcode unique. For variants, we should have a different barcode.
2014-02-19 01:51:19 +01:00
$this -> errors [] = 'ErrorBarCodeAlreadyUsed' ;
}
2017-11-26 18:43:43 +01:00
2014-02-19 01:51:19 +01:00
$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
/**
2016-05-17 14:02:18 +02:00
* Update a record into database .
* If batch flag is set to on , we create records into llx_product_batch
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
2015-05-04 03:03:24 +02:00
if ( ! $this -> label ) $this -> label = '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 ));
2015-05-04 03:03:24 +02:00
$this -> label = trim ( $this -> label );
2007-06-30 15:06:03 +02:00
$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 );
2016-12-10 13:19:33 +01:00
$this -> width = price2num ( $this -> width );
$this -> width_units = trim ( $this -> width_units );
$this -> height = price2num ( $this -> height );
$this -> height_units = trim ( $this -> height_units );
2017-01-16 10:43:22 +01:00
// set unit not defined
2017-01-16 10:55:33 +01:00
if ( $this -> length_units ) $this -> width_units = $this -> length_units ; // Not used yet
if ( $this -> length_units ) $this -> height_units = $this -> length_units ; // Not used yet
2016-12-11 01:53:33 +01:00
// Automated compute surface and volume if not filled
2017-01-16 10:43:22 +01:00
if ( empty ( $this -> surface ) && ! empty ( $this -> length ) && ! empty ( $this -> width ) && $this -> length_units == $this -> width_units )
{
2016-12-11 01:53:33 +01:00
$this -> surface = $this -> length * $this -> width ;
2018-01-01 22:03:08 +01:00
$this -> surface_units = measuring_units_squared ( $this -> length_units );
2017-01-16 10:55:33 +01:00
}
if ( empty ( $this -> volume ) && ! empty ( $this -> surface_units ) && ! empty ( $this -> height ) && $this -> length_units == $this -> height_units )
{
$this -> volume = $this -> surface * $this -> height ;
2018-01-01 22:03:08 +01:00
$this -> volume_units = measuring_units_cubed ( $this -> height_units );
2016-12-11 01:53:33 +01:00
}
2017-06-09 12:50:56 +02:00
2010-03-13 00:40:39 +01:00
$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 ;
2016-02-19 02:41:00 +01:00
if ( empty ( $this -> localtax1_type )) $this -> localtax1_type = '0' ;
if ( empty ( $this -> localtax2_type )) $this -> localtax2_type = '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 );
2017-11-01 07:19:36 +01:00
$this -> accountancy_code_sell_intra = trim ( $this -> accountancy_code_sell_intra );
$this -> accountancy_code_sell_export = trim ( $this -> accountancy_code_sell_export );
2010-03-20 19:39:32 +01:00
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 ;
}
2017-06-09 11:02:34 +02:00
2016-05-17 14:02:18 +02:00
// Test if batch management is activated on existing product
// If yes, we create missing entries into product_batch
2014-07-15 11:22:16 +02:00
if ( $this -> hasbatch () && ! $this -> oldcopy -> hasbatch ())
{
2016-05-17 14:02:18 +02:00
//$valueforundefinedlot = 'Undefined'; // In previous version, 39 and lower
$valueforundefinedlot = '000000' ;
dol_syslog ( " Flag batch of product id= " . $this -> id . " is set to ON, so we will create missing records into product_batch " );
2014-07-15 11:22:16 +02:00
$this -> load_stock ();
2016-05-17 14:02:18 +02:00
foreach ( $this -> stock_warehouse as $idW => $ObjW ) // For each warehouse where we have stocks defined for this product (for each lines in product_stock)
2014-07-15 11:22:16 +02:00
{
$qty_batch = 0 ;
2016-05-17 14:02:18 +02:00
foreach ( $ObjW -> detail_batch as $detail ) // Each lines of detail in product_batch of the current $ObjW = product_stock
2014-07-15 11:22:16 +02:00
{
2017-06-09 11:02:34 +02:00
if ( $detail -> batch == $valueforundefinedlot || $detail -> batch == 'Undefined' )
2016-05-17 14:02:18 +02:00
{
// We discard this line, we will create it later
$sqlclean = " DELETE FROM " . MAIN_DB_PREFIX . " product_batch WHERE batch in('Undefined', ' " . $valueforundefinedlot . " ') AND fk_product_stock = " . $ObjW -> id ;
$result = $this -> db -> query ( $sqlclean );
if ( ! $result )
{
dol_print_error ( $this -> db );
exit ;
}
continue ;
}
2017-06-09 11:02:34 +02:00
2014-07-15 11:22:16 +02:00
$qty_batch += $detail -> qty ;
}
2016-05-17 14:02:18 +02:00
// Quantities in batch details are not same as stock quantity,
// so we add a default batch record to complete and get same qty in parent and child table
2014-07-15 11:22:16 +02:00
if ( $ObjW -> real <> $qty_batch )
{
$ObjBatch = new Productbatch ( $this -> db );
2017-06-09 11:02:34 +02:00
$ObjBatch -> batch = $valueforundefinedlot ;
2016-05-17 14:02:18 +02:00
$ObjBatch -> qty = ( $ObjW -> real - $qty_batch );
2014-07-15 11:22:16 +02:00
$ObjBatch -> fk_product_stock = $ObjW -> id ;
2016-05-17 14:02:18 +02:00
2014-07-15 11:22:16 +02:00
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
}
2016-05-17 17:55:41 +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 " ;
2015-05-04 03:03:24 +02:00
$sql .= " SET label = ' " . $this -> db -> escape ( $this -> label ) . " ' " ;
2015-09-12 11:23:44 +02:00
$sql .= " , ref = ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
2014-02-19 01:51:19 +01:00
$sql .= " , ref_ext = " . ( ! empty ( $this -> ref_ext ) ? " ' " . $this -> db -> escape ( $this -> ref_ext ) . " ' " : " null " );
2016-04-18 21:02:14 +02:00
$sql .= " , default_vat_code = " . ( $this -> default_vat_code ? " ' " . $this -> db -> escape ( $this -> default_vat_code ) . " ' " : " null " );
2014-02-19 01:51:19 +01:00
$sql .= " , tva_tx = " . $this -> tva_tx ;
$sql .= " , recuperableonly = " . $this -> tva_npr ;
$sql .= " , localtax1_tx = " . $this -> localtax1_tx ;
$sql .= " , localtax2_tx = " . $this -> localtax2_tx ;
2017-09-15 15:41:07 +02:00
$sql .= " , localtax1_type = " . ( $this -> localtax1_type != '' ? " ' " . $this -> db -> escape ( $this -> localtax1_type ) . " ' " : " '0' " );
$sql .= " , localtax2_type = " . ( $this -> localtax2_type != '' ? " ' " . $this -> db -> escape ( $this -> localtax2_type ) . " ' " : " '0' " );
2017-06-09 11:02:34 +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 ));
2018-12-22 17:03:50 +01:00
$sql .= " , tosell = " . ( int ) $this -> status ;
$sql .= " , tobuy = " . ( int ) $this -> status_buy ;
$sql .= " , tobatch = " . (( empty ( $this -> status_batch ) || $this -> status_batch < 0 ) ? '0' : ( int ) $this -> status_batch );
2014-03-08 14:25:59 +01:00
$sql .= " , finished = " . (( ! isset ( $this -> finished ) || $this -> finished < 0 ) ? " null " : ( int ) $this -> finished );
2017-09-15 15:41:07 +02:00
$sql .= " , weight = " . ( $this -> weight != '' ? " ' " . $this -> db -> escape ( $this -> weight ) . " ' " : 'null' );
$sql .= " , weight_units = " . ( $this -> weight_units != '' ? " ' " . $this -> db -> escape ( $this -> weight_units ) . " ' " : 'null' );
$sql .= " , length = " . ( $this -> length != '' ? " ' " . $this -> db -> escape ( $this -> length ) . " ' " : 'null' );
$sql .= " , length_units = " . ( $this -> length_units != '' ? " ' " . $this -> db -> escape ( $this -> length_units ) . " ' " : 'null' );
$sql .= " , width= " . ( $this -> width != '' ? " ' " . $this -> db -> escape ( $this -> width ) . " ' " : 'null' );
$sql .= " , width_units = " . ( $this -> width_units != '' ? " ' " . $this -> db -> escape ( $this -> width_units ) . " ' " : 'null' );
$sql .= " , height = " . ( $this -> height != '' ? " ' " . $this -> db -> escape ( $this -> height ) . " ' " : 'null' );
$sql .= " , height_units = " . ( $this -> height_units != '' ? " ' " . $this -> db -> escape ( $this -> height_units ) . " ' " : 'null' );
$sql .= " , surface = " . ( $this -> surface != '' ? " ' " . $this -> db -> escape ( $this -> surface ) . " ' " : 'null' );
$sql .= " , surface_units = " . ( $this -> surface_units != '' ? " ' " . $this -> db -> escape ( $this -> surface_units ) . " ' " : 'null' );
$sql .= " , volume = " . ( $this -> volume != '' ? " ' " . $this -> db -> escape ( $this -> volume ) . " ' " : 'null' );
$sql .= " , volume_units = " . ( $this -> volume_units != '' ? " ' " . $this -> db -> escape ( $this -> volume_units ) . " ' " : 'null' );
2018-04-10 22:23:21 +02:00
$sql .= " , fk_default_warehouse = " . ( $this -> fk_default_warehouse > 0 ? $this -> db -> escape ( $this -> fk_default_warehouse ) : 'null' );
2017-09-15 15:41:07 +02:00
$sql .= " , seuil_stock_alerte = " . (( isset ( $this -> seuil_stock_alerte ) && $this -> seuil_stock_alerte != '' ) ? " ' " . $this -> db -> escape ( $this -> seuil_stock_alerte ) . " ' " : " null " );
2014-02-19 01:51:19 +01:00
$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 ) . " ' " ;
2018-12-22 17:03:50 +01:00
$sql .= " , fk_country = " . ( $this -> country_id > 0 ? ( int ) $this -> country_id : 'null' );
2014-05-10 14:57:22 +02:00
$sql .= " , note = " . ( isset ( $this -> note ) ? " ' " . $this -> db -> escape ( $this -> note ) . " ' " : 'null' );
2015-09-12 11:23:44 +02:00
$sql .= " , duration = ' " . $this -> db -> escape ( $this -> duration_value . $this -> duration_unit ) . " ' " ;
$sql .= " , accountancy_code_buy = ' " . $this -> db -> escape ( $this -> accountancy_code_buy ) . " ' " ;
$sql .= " , accountancy_code_sell= ' " . $this -> db -> escape ( $this -> accountancy_code_sell ) . " ' " ;
2017-11-01 07:19:36 +01:00
$sql .= " , accountancy_code_sell_intra= ' " . $this -> db -> escape ( $this -> accountancy_code_sell_intra ) . " ' " ;
$sql .= " , accountancy_code_sell_export= ' " . $this -> db -> escape ( $this -> accountancy_code_sell_export ) . " ' " ;
2018-12-22 17:03:50 +01:00
$sql .= " , desiredstock = " . (( isset ( $this -> desiredstock ) && $this -> desiredstock != '' ) ? ( int ) $this -> desiredstock : " null " );
2016-02-23 19:50:33 +01:00
$sql .= " , cost_price = " . ( $this -> cost_price != '' ? $this -> db -> escape ( $this -> cost_price ) : 'null' );
2018-12-22 17:03:50 +01:00
$sql .= " , fk_unit= " . (( ! $this -> fk_unit ) ? 'NULL' : ( int ) $this -> fk_unit );
2015-10-19 15:13:19 +02:00
$sql .= " , price_autogen = " . ( ! $this -> price_autogen ? 0 : 1 );
2018-12-22 17:03:50 +01:00
$sql .= " , fk_price_expression = " . ( $this -> fk_price_expression != 0 ? ( int ) $this -> fk_price_expression : 'NULL' );
2015-11-19 14:26:32 +01:00
$sql .= " , fk_user_modif = " . ( $user -> id > 0 ? $user -> id : 'NULL' );
2016-05-17 14:02:18 +02:00
// stock field is not here because it is a denormalized value from product_stock.
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
{
2015-11-16 19:27:51 +01:00
if ( $this -> setMultiLangs ( $user ) < 0 )
2014-02-19 01:51:19 +01:00
{
$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' ;
2018-04-10 12:03:01 +02:00
// Actions on extra fields
2018-08-12 16:29:26 +02:00
if ( ! $error && empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))
2012-11-09 19:23:46 +01:00
{
2018-04-10 12:03:01 +02:00
$result = $this -> insertExtraFields ();
if ( $result < 0 )
2012-11-09 19:23:46 +01:00
{
2018-04-10 12:03:01 +02:00
$error ++ ;
2012-11-09 19:23:46 +01:00
}
}
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
2017-03-03 12:57:52 +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
{
2015-10-14 22:28:03 +02:00
//include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
//$res = dol_move($olddir, $newdir);
// do not use dol_move with directory
$res = @ rename ( $olddir , $newdir );
2014-02-19 01:51:19 +01:00
if ( ! $res )
{
2016-05-17 21:55:39 +02:00
$langs -> load ( " errors " );
$this -> error = $langs -> trans ( 'ErrorFailToRenameDir' , $olddir , $newdir );
2014-02-19 01:51:19 +01:00
$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 )
{
2017-02-08 12:37:38 +01:00
if ( $conf -> variants -> enabled ) {
2016-07-23 16:37:21 +02:00
2017-02-08 12:37:38 +01:00
require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductCombination.class.php' ;
2016-07-23 16:37:21 +02:00
$comb = new ProductCombination ( $this -> db );
foreach ( $comb -> fetchAllByFkProductParent ( $this -> id ) as $currcomb ) {
$currcomb -> updateProperties ( $this );
}
}
2014-02-19 01:51:19 +01:00
$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' )
{
2017-11-26 18:43:43 +01:00
$langs -> load ( " errors " );
2018-06-12 16:38:42 +02:00
if ( empty ( $conf -> barcode -> enabled ) || empty ( $this -> barcode )) $this -> error = $langs -> trans ( " Error " ) . " : " . $langs -> trans ( " ErrorProductAlreadyExists " , $this -> ref );
2015-06-30 16:10:03 +02:00
else $this -> error = $langs -> trans ( " Error " ) . " : " . $langs -> trans ( " ErrorProductBarCodeAlreadyExists " , $this -> barcode );
2016-04-18 21:02:14 +02:00
$this -> errors [] = $this -> error ;
2014-02-19 01:51:19 +01:00
$this -> db -> rollback ();
return - 1 ;
}
else
{
$this -> error = $langs -> trans ( " Error " ) . " : " . $this -> db -> error () . " - " . $sql ;
2016-04-18 21:02:14 +02:00
$this -> errors [] = $this -> error ;
2014-02-19 01:51:19 +01:00
$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
*
2017-04-09 15:02:55 +02:00
* @ param User $user Product id ( usage of this is deprecated , delete should be called without parameters on a fetched object )
2016-06-07 20:51:06 +02:00
* @ param int $notrigger Do not execute trigger
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
*/
2017-04-09 15:02:55 +02:00
function delete ( User $user , $notrigger = 0 )
2008-02-29 01:00:54 +01:00
{
2015-04-23 23:21:06 +02:00
// Deprecation warning
2015-10-14 23:50:36 +02:00
if ( $id > 0 ) {
2015-04-23 23:21:06 +02:00
dol_syslog ( __METHOD__ . " with parameter is deprecated " , LOG_WARNING );
}
2017-04-09 15:02:55 +02:00
global $conf , $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
2016-06-07 20:51:06 +02:00
if ( ! $error && empty ( $notrigger ))
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
}
2017-06-09 11:02:34 +02:00
// Delete from product_batch on product delete
2016-11-20 22:30:40 +01:00
if ( ! $error )
2016-11-15 11:29:12 +01:00
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . 'product_batch' ;
$sql .= " WHERE fk_product_stock IN ( " ;
$sql .= " SELECT rowid FROM " . MAIN_DB_PREFIX . 'product_stock' ;
$sql .= " WHERE fk_product = " . $id . " ) " ;
2018-10-19 14:16:53 +02:00
2016-11-15 11:29:12 +01:00
$result = $this -> db -> query ( $sql );
if ( ! $result )
{
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ();
}
}
2017-06-09 11:02:34 +02:00
2014-07-04 15:54:20 +02:00
// Delete all child tables
if ( ! $error )
2013-08-27 09:36:54 +02:00
{
2017-05-17 16:05:26 +02:00
$elements = array ( 'product_fournisseur_price' , 'product_price' , 'product_lang' , 'categorie_product' , 'product_stock' , 'product_customer_price' , 'product_lot' ); // product_batch is done before
2014-07-04 15:54:20 +02:00
foreach ( $elements as $table )
{
if ( ! $error )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE fk_product = " . $id ;
2018-10-19 14:16:53 +02:00
2014-07-04 15:54:20 +02:00
$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
2016-07-23 16:37:21 +02:00
if ( ! $error ) {
2017-02-08 12:37:38 +01:00
require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductCombination.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductCombination2ValuePair.class.php' ;
2016-07-23 16:37:21 +02:00
//If it is a parent product, then we remove the association with child products
$prodcomb = new ProductCombination ( $this -> db );
2018-05-02 22:33:23 +02:00
if ( $prodcomb -> deleteByFkProductParent ( $user , $id ) < 0 ) {
2016-07-23 16:37:21 +02:00
$error ++ ;
$this -> errors [] = 'Error deleting combinations' ;
}
//We also check if it is a child product
2017-11-26 18:43:43 +01:00
if ( ! $error && ( $prodcomb -> fetchByFkProductChild ( $id ) > 0 ) && ( $prodcomb -> delete ( $user ) < 0 )) {
2016-07-23 16:37:21 +02:00
$error ++ ;
$this -> errors [] = 'Error deleting child combination' ;
}
}
2018-10-17 10:01:02 +02:00
// Delete from product_association
if ( ! $error ){
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " product_association " ;
2018-10-17 10:06:06 +02:00
$sql .= " WHERE fk_product_pere = " . $id . " OR fk_product_fils = " . $id ;
2018-10-19 14:16:53 +02:00
2018-10-17 10:01:02 +02:00
$result = $this -> db -> query ( $sql );
if ( ! $result )
{
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ();
}
}
2013-08-27 09:36:54 +02:00
// Delete product
if ( ! $error )
{
$sqlz = " DELETE FROM " . MAIN_DB_PREFIX . " product " ;
$sqlz .= " WHERE rowid = " . $id ;
2018-10-19 14:16:53 +02:00
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
*
2016-07-28 13:53:17 +02:00
* @ param User $user Object user making update
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2015-11-16 19:27:51 +01:00
function setMultiLangs ( $user )
2008-07-27 22:47:13 +02:00
{
2016-07-28 13:53:17 +02:00
global $conf , $langs ;
2017-06-09 11:02:34 +02:00
2016-02-23 19:50:33 +01:00
$langs_available = $langs -> get_available_languages ( DOL_DOCUMENT_ROOT , 0 , 2 );
2008-07-27 22:47:13 +02:00
$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 " ;
2016-07-28 13:53:17 +02:00
$sql2 .= " SET " ;
$sql2 .= " label=' " . $this -> db -> escape ( $this -> label ) . " ', " ;
$sql2 .= " description=' " . $this -> db -> escape ( $this -> description ) . " ' " ;
2018-03-22 10:46:54 +01:00
if ( ! empty ( $conf -> global -> PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION )) $sql2 .= " , note=' " . $this -> db -> escape ( $this -> other ) . " ' " ;
2017-05-12 16:55:11 +02:00
$sql2 .= " WHERE fk_product= " . $this -> id . " AND lang=' " . $this -> db -> escape ( $key ) . " ' " ;
2009-06-08 12:56:54 +02:00
}
else
{
2016-07-28 13:53:17 +02:00
$sql2 = " INSERT INTO " . MAIN_DB_PREFIX . " product_lang (fk_product, lang, label, description " ;
if ( ! empty ( $conf -> global -> PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION )) $sql2 .= " , note " ;
$sql2 .= " ) " ;
2017-05-12 16:55:11 +02:00
$sql2 .= " VALUES( " . $this -> id . " ,' " . $this -> db -> escape ( $key ) . " ',' " . $this -> db -> escape ( $this -> label ) . " ', " ;
2016-07-28 13:53:17 +02:00
$sql2 .= " ' " . $this -> db -> escape ( $this -> description ) . " ' " ;
2018-03-22 10:46:54 +01:00
if ( ! empty ( $conf -> global -> PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION )) $sql2 .= " , ' " . $this -> db -> escape ( $this -> other ) . " ' " ;
2016-07-28 13:53:17 +02:00
$sql2 .= " ) " ;
2009-06-08 12:56:54 +02:00
}
2016-02-23 19:50:33 +01:00
dol_syslog ( get_class ( $this ) . '::setMultiLangs key = current_lang = ' . $key );
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 " ;
2016-07-28 13:53:17 +02:00
$sql2 .= " SET " ;
$sql2 .= " label=' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " label " ]) . " ', " ;
$sql2 .= " description=' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " description " ]) . " ' " ;
2018-03-22 10:46:54 +01:00
if ( ! empty ( $conf -> global -> PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION )) $sql2 .= " , note=' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " other " ]) . " ' " ;
2017-05-12 16:55:11 +02:00
$sql2 .= " WHERE fk_product= " . $this -> id . " AND lang=' " . $this -> db -> escape ( $key ) . " ' " ;
2009-06-08 12:56:54 +02:00
}
else
{
2016-07-28 13:53:17 +02:00
$sql2 = " INSERT INTO " . MAIN_DB_PREFIX . " product_lang (fk_product, lang, label, description " ;
if ( ! empty ( $conf -> global -> PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION )) $sql2 .= " , note " ;
$sql2 .= " ) " ;
2017-05-12 16:55:11 +02:00
$sql2 .= " VALUES( " . $this -> id . " ,' " . $this -> db -> escape ( $key ) . " ',' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " label " ]) . " ', " ;
2016-07-28 13:53:17 +02:00
$sql2 .= " ' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " description " ]) . " ' " ;
2018-03-22 10:46:54 +01:00
if ( ! empty ( $conf -> global -> PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION )) $sql2 .= " , ' " . $this -> db -> escape ( $this -> multilangs [ " $key " ][ " other " ]) . " ' " ;
2016-07-28 13:53:17 +02:00
$sql2 .= " ) " ;
2009-06-08 12:56:54 +02:00
}
2009-06-16 20:41:01 +02:00
2016-07-28 13:53:17 +02:00
// We do not save if main fields are empty
if ( $this -> multilangs [ " $key " ][ " label " ] || $this -> multilangs [ " $key " ][ " description " ])
2012-08-28 23:01:04 +02:00
{
2016-07-28 13:53:17 +02:00
if ( ! $this -> db -> query ( $sql2 ))
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
2012-08-28 23:01:04 +02:00
}
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
}
2015-11-16 19:27:51 +01:00
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_SET_MULTILANGS' , $user );
if ( $result < 0 ) {
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
// End call triggers
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
2015-11-17 00:20:58 +01:00
* @ param User $user Object user making delete
*
2014-06-20 15:05:39 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2015-11-17 00:20:58 +01:00
function delMultiLangs ( $langtodelete , $user )
2014-06-20 15:05:39 +02:00
{
$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 )
{
2015-11-17 00:20:58 +01:00
// Call trigger
$result = $this -> call_trigger ( 'PRODUCT_DEL_MULTILANGS' , $user );
if ( $result < 0 ) {
$this -> error = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . '::delMultiLangs error=' . $this -> error , LOG_ERR );
return - 1 ;
}
// End call triggers
2014-06-20 15:05:39 +02:00
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
*
2017-11-01 07:19:36 +01:00
* @ param string $type It can be 'buy' , 'sell' , 'sell_intra' or 'sell_export'
2014-09-14 13:59:06 +02:00
* @ 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' ;
2017-11-01 07:19:36 +01:00
} elseif ( $type == 'sell_intra' ) {
$field = 'accountancy_code_sell_intra' ;
} elseif ( $type == 'sell_export' ) {
$field = 'accountancy_code_sell_export' ;
2014-09-14 13:59:06 +02:00
} 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
2016-07-28 13:53:17 +02:00
$sql = " SELECT lang, label, description, note as other " ;
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 ;
2016-07-28 13:53:17 +02:00
$this -> other = $obj -> other ;
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 ;
2016-07-28 13:53:17 +02:00
$this -> multilangs [ " $obj->lang " ][ " other " ] = $obj -> other ;
2009-06-08 12:56:54 +02:00
}
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
2017-06-23 12:09:21 +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, default_vat_code, recuperableonly, " ;
2016-02-19 02:41:00 +01:00
$sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, price_min,price_min_ttc,price_by_qty,entity,fk_price_expression) " ;
2017-09-15 15:41:07 +02:00
$sql .= " VALUES( " . ( $level ? $level : 1 ) . " , ' " . $this -> db -> idate ( $now ) . " ', " . $this -> id . " , " . $user -> id . " , " . $this -> price . " , " . $this -> price_ttc . " ,' " . $this -> db -> escape ( $this -> price_base_type ) . " ', " . $this -> status . " , " . $this -> tva_tx . " , " . ( $this -> default_vat_code ? ( " ' " . $this -> db -> escape ( $this -> default_vat_code ) . " ' " ) : " null " ) . " , " . $this -> tva_npr . " , " ;
$sql .= " " . $this -> localtax1_tx . " , " . $this -> localtax2_tx . " , ' " . $this -> db -> escape ( $this -> localtax1_type ) . " ', ' " . $this -> db -> escape ( $this -> localtax2_type ) . " ', " . $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
*/
2017-12-22 10:56:52 +01:00
function log_price_delete ( $user , $rowid )
2008-10-25 14:14:16 +02:00
{
2017-12-22 10:56:52 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " product_price_by_qty " ;
$sql .= " WHERE fk_product_price= " . $rowid ;
$resql = $this -> db -> query ( $sql );
2009-01-09 21:53:03 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " product_price " ;
$sql .= " WHERE rowid= " . $rowid ;
$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
/**
2016-09-11 16:05:38 +02:00
* Read price used by a provider .
* We enter as input couple prodfournprice / qty or triplet qty / product_id / fourn_ref .
* This also set some properties on product like -> buyprice , -> fourn_pu , ...
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
2016-10-01 16:33:42 +02:00
* @ param double $qty Quantity asked or - 1 to get first entry found
2011-12-19 18:24:49 +01:00
* @ param int $product_id Filter on a particular product id
2016-12-13 13:22:24 +01:00
* @ param string $fourn_ref Filter on a supplier price ref . 'none' to exclude ref in search .
* @ param int $fk_soc If of supplier
2016-05-17 22:59:46 +02:00
* @ return int <- 1 if KO , - 1 if qty not enough , 0 if OK but nothing found , id_product if OK and found . May also initialize some properties like ( -> ref_supplier , buyprice , fourn_pu , vatrate_supplier ... )
2008-07-27 22:47:13 +02:00
*/
2016-12-13 13:22:24 +01:00
function get_buyprice ( $prodfournprice , $qty , $product_id = 0 , $fourn_ref = '' , $fk_soc = 0 )
2006-12-05 01:47:40 +01:00
{
2016-02-14 14:26:04 +01:00
global $conf ;
2006-12-05 01:47:40 +01:00
$result = 0 ;
2013-10-27 02:23:25 +02:00
2016-05-17 22:59:46 +02:00
// We do a first seach with a select by searching with couple prodfournprice and qty only (later we will search on triplet qty/product_id/fourn_ref)
2017-01-31 23:13:58 +01:00
$sql = " SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent, " ;
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 " ;
2017-11-14 09:53:06 +01:00
$sql .= " ,pfp.default_vat_code " ;
2018-02-23 11:06:07 +01:00
$sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code " ;
2011-10-04 01:26:07 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_fournisseur_price as pfp " ;
$sql .= " WHERE pfp.rowid = " . $prodfournprice ;
2016-09-11 16:05:38 +02:00
if ( $qty > 0 ) $sql .= " AND pfp.quantity <= " . $qty ;
$sql .= " ORDER BY pfp.quantity DESC " ;
2017-06-09 11:02:34 +02:00
2016-05-17 22:59:46 +02:00
dol_syslog ( get_class ( $this ) . " ::get_buyprice first search by prodfournprice/qty " , LOG_DEBUG );
2006-12-05 01:47:40 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
2016-10-01 16:33:42 +02:00
if ( $obj && $obj -> quantity > 0 ) // If we found a supplier prices from the id of supplier price
2006-12-05 01:47:40 +01:00
{
2016-02-14 14:26:04 +01:00
if ( ! empty ( $conf -> dynamicprices -> enabled ) && ! empty ( $obj -> fk_supplier_price_expression ))
2015-02-01 15:08:56 +01:00
{
2015-03-14 16:50:02 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/dynamic_price/class/price_parser.class.php' ;
2016-02-14 17:28:31 +01:00
$prod_supplier = new ProductFournisseur ( $this -> db );
$prod_supplier -> product_fourn_price_id = $obj -> rowid ;
$prod_supplier -> id = $obj -> fk_product ;
$prod_supplier -> fourn_qty = $obj -> quantity ;
$prod_supplier -> fourn_tva_tx = $obj -> tva_tx ;
$prod_supplier -> fk_supplier_price_expression = $obj -> fk_supplier_price_expression ;
$priceparser = new PriceParser ( $this -> db );
$price_result = $priceparser -> parseProductSupplier ( $prod_supplier );
2014-11-29 06:31:35 +01:00
if ( $price_result >= 0 ) {
$obj -> price = $price_result ;
}
}
2017-09-20 16:57:38 +02:00
$this -> product_fourn_price_id = $obj -> rowid ;
2015-04-23 23:21:06 +02:00
$this -> buyprice = $obj -> price ; // deprecated
2016-10-01 16:33:42 +02:00
$this -> fourn_pu = $obj -> price / $obj -> quantity ; // Unit price of product of supplier
$this -> fourn_price_base_type = 'HT' ; // Price base type
2018-06-16 14:47:33 +02:00
$this -> fourn_socid = $obj -> fk_soc ; // Company that offer this price
2016-05-29 13:45:26 +02:00
$this -> ref_fourn = $obj -> ref_fourn ; // deprecated
$this -> ref_supplier = $obj -> ref_fourn ; // Ref supplier
2017-01-31 23:13:58 +01:00
$this -> remise_percent = $obj -> remise_percent ; // remise percent if present and not typed
2012-05-08 21:55:52 +02:00
$this -> vatrate_supplier = $obj -> tva_tx ; // Vat ref supplier
2017-11-14 09:53:06 +01:00
$this -> default_vat_code = $obj -> default_vat_code ; // Vat code supplier
2018-02-23 11:06:07 +01:00
$this -> fourn_multicurrency_price = $obj -> multicurrency_price ;
$this -> fourn_multicurrency_unitprice = $obj -> multicurrency_unitprice ;
$this -> fourn_multicurrency_tx = $obj -> multicurrency_tx ;
$this -> fourn_multicurrency_id = $obj -> fk_multicurrency ;
$this -> fourn_multicurrency_code = $obj -> multicurrency_code ;
2007-09-27 01:19:39 +02:00
$result = $obj -> fk_product ;
return $result ;
2006-12-05 01:47:40 +01:00
}
2016-05-17 22:59:46 +02:00
else // If not found
2006-12-05 01:47:40 +01:00
{
2016-12-13 13:22:24 +01:00
// We do a second search by doing a select again but searching with less reliable criteria: couple qty/id product, and if set fourn_ref or fk_soc.
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 " ;
2017-11-14 09:53:06 +01:00
$sql .= " ,pfp.default_vat_code " ;
2018-02-23 11:06:07 +01:00
$sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code " ;
2011-10-04 01:26:07 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_fournisseur_price as pfp " ;
2016-09-11 16:05:38 +02:00
$sql .= " WHERE pfp.fk_product = " . $product_id ;
if ( $fourn_ref != 'none' ) $sql .= " AND pfp.ref_fourn = ' " . $fourn_ref . " ' " ;
2016-12-13 13:22:24 +01:00
if ( $fk_soc > 0 ) $sql .= " AND pfp.fk_soc = " . $fk_soc ;
2016-09-11 16:05:38 +02:00
if ( $qty > 0 ) $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
2016-05-17 22:59:46 +02:00
dol_syslog ( get_class ( $this ) . " ::get_buyprice second search from qty/ref/product_id " , 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
{
2016-02-14 14:26:04 +01:00
if ( ! empty ( $conf -> dynamicprices -> enabled ) && ! empty ( $obj -> fk_supplier_price_expression ))
2015-02-01 15:08:56 +01:00
{
2015-03-14 16:50:02 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/dynamic_price/class/price_parser.class.php' ;
2016-02-14 17:28:31 +01:00
$prod_supplier = new ProductFournisseur ( $this -> db );
$prod_supplier -> product_fourn_price_id = $obj -> rowid ;
$prod_supplier -> id = $obj -> fk_product ;
$prod_supplier -> fourn_qty = $obj -> quantity ;
$prod_supplier -> fourn_tva_tx = $obj -> tva_tx ;
$prod_supplier -> fk_supplier_price_expression = $obj -> fk_supplier_price_expression ;
$priceparser = new PriceParser ( $this -> db );
$price_result = $priceparser -> parseProductSupplier ( $prod_supplier );
2014-11-29 06:31:35 +01:00
if ( $result >= 0 ) {
$obj -> price = $price_result ;
}
}
2017-09-20 16:57:38 +02:00
$this -> product_fourn_price_id = $obj -> rowid ;
2014-05-25 03:57:13 +02:00
$this -> buyprice = $obj -> price ; // deprecated
2016-10-01 16:33:42 +02:00
$this -> fourn_qty = $obj -> quantity ; // min quantity for price for a virtual supplier
$this -> fourn_pu = $obj -> price / $obj -> quantity ; // Unit price of product for a virtual supplier
$this -> fourn_price_base_type = 'HT' ; // Price base type for a virtual supplier
2018-06-16 14:47:33 +02:00
$this -> fourn_socid = $obj -> fk_soc ; // Company that offer this price
2014-05-25 03:57:13 +02:00
$this -> ref_fourn = $obj -> ref_supplier ; // deprecated
$this -> ref_supplier = $obj -> ref_supplier ; // Ref supplier
2017-01-31 23:13:58 +01:00
$this -> remise_percent = $obj -> remise_percent ; // remise percent if present and not typed
2012-05-08 21:55:52 +02:00
$this -> vatrate_supplier = $obj -> tva_tx ; // Vat ref supplier
2017-11-14 09:53:06 +01:00
$this -> default_vat_code = $obj -> default_vat_code ; // Vat code supplier
2018-02-23 11:06:07 +01:00
$this -> fourn_multicurrency_price = $obj -> multicurrency_price ;
$this -> fourn_multicurrency_unitprice = $obj -> multicurrency_unitprice ;
$this -> fourn_multicurrency_tx = $obj -> multicurrency_tx ;
$this -> fourn_multicurrency_id = $obj -> fk_multicurrency ;
$this -> fourn_multicurrency_code = $obj -> multicurrency_code ;
2007-09-27 01:19:39 +02:00
$result = $obj -> fk_product ;
return $result ;
2006-12-05 01:47:40 +01:00
}
else
{
2016-09-11 16:05:38 +02:00
return - 1 ; // Ce produit n'existe pas avec cet id tarif fournisseur ou existe mais qte insuffisante, ni pour le couple produit/ref fournisseur dans la quantité.
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
/**
2016-04-18 21:02:14 +02:00
* Modify customer price of a product / Service
2011-08-27 19:25:09 +02:00
*
2016-04-18 21:02:14 +02: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 ( For example 8.5 . Should not be a string )
* @ 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
2017-12-22 10:56:52 +01:00
* @ param int $newpbq 1 if it has price by quantity
2016-04-18 21:02:14 +02:00
* @ param int $ignore_autogen Used to avoid infinite loops
2016-02-19 02:41:00 +01:00
* @ param array $localtaxes_array Array with localtaxes info array ( '0' => type1 , '1' => rate1 , '2' => type2 , '3' => rate2 ) ( loaded by getLocalTaxesFromRate ( vatrate , 0 , ... ) function ) .
2016-04-18 21:02:14 +02:00
* @ param string $newdefaultvatcode Default vat code
2016-02-19 02:41:00 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2018-02-12 08:21:35 +01:00
function updatePrice ( $newprice , $newpricebase , $user , $newvat = '' , $newminprice = 0 , $level = 0 , $newnpr = 0 , $newpbq = 0 , $ignore_autogen = 0 , $localtaxes_array = array (), $newdefaultvatcode = '' )
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 ;
2016-04-25 15:04:33 +02:00
dol_syslog ( get_class ( $this ) . " ::update_price id= " . $id . " newprice= " . $newprice . " newpricebase= " . $newpricebase . " newminprice= " . $newminprice . " level= " . $level . " npr= " . $newnpr . " newdefaultvatcode= " . $newdefaultvatcode );
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 ;
2015-10-19 15:13:19 +02:00
2015-11-02 10:02:09 +01:00
// If multiprices are enabled, then we check if the current product is subject to price autogeneration
// Price will be modified ONLY when the first one is the one that is being modified
2018-05-23 17:05:08 +02:00
if (( ! empty ( $conf -> global -> PRODUIT_MULTIPRICES ) || ! empty ( $conf -> global -> PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES )) && ! $ignore_autogen && $this -> price_autogen && ( $level == 1 ))
2015-11-02 10:02:09 +01:00
{
2017-12-22 10:56:52 +01:00
return $this -> generateMultiprices ( $user , $newprice , $newpricebase , $newvat , $newnpr , $newpbq );
2015-10-19 15:13:19 +02:00
}
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
2018-03-10 15:48:40 +01: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
2018-02-12 08:21:35 +01: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
2018-03-10 15:48:40 +01:00
if ( $newminprice !== '' || $newminprice === 0 )
2018-03-14 20:08:44 +01: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
2016-02-19 02:41:00 +01:00
if ( count ( $localtaxes_array ) > 0 )
{
$localtaxtype1 = $localtaxes_array [ '0' ];
$localtax1 = $localtaxes_array [ '1' ];
$localtaxtype2 = $localtaxes_array [ '2' ];
$localtax2 = $localtaxes_array [ '3' ];
}
else // old method. deprecated because ot can't retreive type
{
$localtaxtype1 = '0' ;
$localtax1 = get_localtax ( $newvat , 1 );
$localtaxtype2 = '0' ;
$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
2017-06-09 11:02:34 +02:00
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' ) . " , " ;
2016-02-19 02:41:00 +01:00
$sql .= " localtax1_type= " . ( $localtaxtype1 != '' ? " ' " . $localtaxtype1 . " ' " : " '0' " ) . " , " ;
$sql .= " localtax2_type= " . ( $localtaxtype2 != '' ? " ' " . $localtaxtype2 . " ' " : " '0' " ) . " , " ;
2016-04-18 21:02:14 +02:00
$sql .= " default_vat_code= " . ( $newdefaultvatcode ? " ' " . $this -> db -> escape ( $newdefaultvatcode ) . " ' " : " 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 )
{
2015-11-06 11:08:41 +01:00
$this -> multiprices [ $level ] = $price ;
$this -> multiprices_ttc [ $level ] = $price_ttc ;
$this -> multiprices_min [ $level ] = $price_min ;
$this -> multiprices_min_ttc [ $level ] = $price_min_ttc ;
$this -> multiprices_base_type [ $level ] = $newpricebase ;
2016-04-18 21:02:14 +02:00
$this -> multiprices_default_vat_code [ $level ] = $newdefaultvatcode ;
2015-11-06 11:08:41 +01:00
$this -> multiprices_tva_tx [ $level ] = $newvat ;
$this -> multiprices_recuperableonly [ $level ] = $newnpr ;
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 ;
2016-04-18 21:02:14 +02:00
$this -> default_vat_code = $newdefaultvatcode ;
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 ;
2016-02-19 02:41:00 +01:00
$this -> localtax1_type = $localtaxtype1 ;
$this -> localtax2_type = $localtaxtype2 ;
2017-06-09 11:02:34 +02:00
2012-10-22 17:47:15 +02:00
// Price by quantity
2017-12-22 10:56:52 +01:00
$this -> price_by_qty = $newpbq ;
2012-10-22 17:47:15 +02:00
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
2015-11-06 11:35:39 +01:00
* @ deprecated Use Product :: update instead
2014-12-28 01:26:41 +01:00
*/
function setPriceExpression ( $expression_id )
{
2015-11-06 11:35:39 +01:00
global $user ;
2014-12-28 01:26:41 +01:00
2015-11-06 11:35:39 +01:00
$this -> fk_price_expression = $expression_id ;
2014-12-28 01:26:41 +01:00
2015-11-06 11:35:39 +01:00
return $this -> update ( $this -> id , $user );
2014-12-28 01:26:41 +01:00
}
2006-09-12 00:26:58 +02:00
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Load a product in memory from database
2011-08-27 19:25:09 +02:00
*
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
*/
2016-05-17 14:02:18 +02: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' ;
2018-06-01 17:39:07 +02:00
dol_syslog ( 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
2016-07-28 13:53:17 +02:00
$sql = " SELECT rowid, ref, ref_ext, label, description, url, note as note_private, customcode, fk_country, price, price_ttc, " ;
2016-04-18 21:02:14 +02:00
$sql .= " price_min, price_min_ttc, price_base_type, cost_price, default_vat_code, tva_tx, recuperableonly as tva_npr, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, tosell, " ;
2018-03-28 11:54:21 +02:00
$sql .= " tobuy, fk_product_type, duration, fk_default_warehouse, seuil_stock_alerte, canvas, weight, weight_units, " ;
2016-12-10 13:19:33 +01:00
$sql .= " length, length_units, width, width_units, height, height_units, " ;
$sql .= " surface, surface_units, volume, volume_units, barcode, fk_barcode_type, finished, " ;
2017-11-01 07:19:36 +01:00
$sql .= " accountancy_code_buy, accountancy_code_sell, accountancy_code_sell_intra, accountancy_code_sell_export, stock, pmp, " ;
2016-02-19 02:41:00 +01:00
$sql .= " datec, tms, import_key, entity, desiredstock, tobatch, fk_unit, " ;
$sql .= " fk_price_expression, price_autogen " ;
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
{
2017-11-23 15:06:16 +01:00
$sql .= " WHERE entity IN ( " . getEntity ( $this -> element ) . " ) " ;
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
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 );
2017-11-01 07:19:36 +01:00
$this -> id = $obj -> rowid ;
$this -> ref = $obj -> ref ;
$this -> ref_ext = $obj -> ref_ext ;
$this -> label = $obj -> label ;
$this -> description = $obj -> description ;
$this -> url = $obj -> url ;
$this -> note_private = $obj -> note_private ;
$this -> note = $obj -> note_private ; // deprecated
$this -> type = $obj -> fk_product_type ;
$this -> status = $obj -> tosell ;
$this -> status_buy = $obj -> tobuy ;
$this -> status_batch = $obj -> tobatch ;
$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 -> cost_price = $obj -> cost_price ;
$this -> default_vat_code = $obj -> default_vat_code ;
$this -> tva_tx = $obj -> tva_tx ;
2011-05-21 10:01:23 +02:00
//! French VAT NPR
2017-11-01 07:19:36 +01:00
$this -> tva_npr = $obj -> tva_npr ;
$this -> recuperableonly = $obj -> tva_npr ; // For backward compatibility
2016-02-19 02:41:00 +01:00
//! Local taxes
2017-11-01 07:19:36 +01:00
$this -> localtax1_tx = $obj -> localtax1_tx ;
$this -> localtax2_tx = $obj -> localtax2_tx ;
$this -> localtax1_type = $obj -> localtax1_type ;
$this -> localtax2_type = $obj -> localtax2_type ;
$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 -> width = $obj -> width ;
$this -> width_units = $obj -> width_units ;
$this -> height = $obj -> height ;
$this -> height_units = $obj -> height_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 -> accountancy_code_sell_intra = $obj -> accountancy_code_sell_intra ;
$this -> accountancy_code_sell_export = $obj -> accountancy_code_sell_export ;
2018-03-28 11:54:21 +02:00
$this -> fk_default_warehouse = $obj -> fk_default_warehouse ;
2017-11-01 07:19:36 +01:00
$this -> seuil_stock_alerte = $obj -> seuil_stock_alerte ;
$this -> desiredstock = $obj -> desiredstock ;
$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 ;
$this -> ref_ext = $obj -> ref_ext ;
$this -> fk_price_expression = $obj -> fk_price_expression ;
$this -> fk_unit = $obj -> fk_unit ;
$this -> price_autogen = $obj -> price_autogen ;
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
2018-02-20 19:38:18 +01:00
// Retreive all extrafield
2014-07-05 11:17:50 +02:00
// fetch optionals attributes and labels
2018-02-20 19:38:18 +01:00
$this -> fetch_optionals ();
2011-05-22 04:14:01 +02: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 ();
2017-06-09 11:02:34 +02:00
2011-05-21 10:01:23 +02:00
// Load multiprices array
2018-01-14 15:58:04 +01:00
if ( ! empty ( $conf -> global -> PRODUIT_MULTIPRICES )) // prices per segment
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, " ;
2017-03-04 21:45:19 +01:00
$sql .= " price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly " ;
2011-05-21 10:01:23 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price " ;
2017-05-30 18:50:54 +02:00
$sql .= " WHERE entity IN ( " . getEntity ( 'productprice' ) . " ) " ;
2013-06-24 17:10:11 +02:00
$sql .= " AND price_level= " . $i ;
2017-05-12 16:55:11 +02:00
$sql .= " AND fk_product = " . $this -> id ;
2016-01-27 10:57:04 +01:00
$sql .= " ORDER BY date_price DESC, rowid DESC " ;
2011-05-21 10:01:23 +02:00
$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 " ];
2017-03-04 21:45:19 +01:00
// Next two fields are used only if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on
$this -> multiprices_tva_tx [ $i ] = $result [ " tva_tx " ]; // TODO Add ' ('.$result['default_vat_code'].')'
2013-06-14 12:48:27 +02:00
$this -> multiprices_recuperableonly [ $i ] = $result [ " recuperableonly " ];
2012-10-22 17:47:15 +02:00
// Price by quantity
2018-01-14 15:58:04 +01:00
/*
2012-10-22 17:47:15 +02:00
$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 )
{
2017-12-22 10:56:52 +01:00
$sql = " SELECT rowid, price, unitprice, quantity, remise_percent, remise, price_base_type " ;
2012-10-22 17:47:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price_by_qty " ;
2017-05-12 16:55:11 +02:00
$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 " ];
2017-12-22 10:56:52 +01:00
$resultat [ $ii ][ " remise " ] = $result [ " remise " ]; // deprecated
$resultat [ $ii ][ " price_base_type " ] = $result [ " price_base_type " ];
2012-10-22 17:47:15 +02:00
$ii ++ ;
}
$this -> prices_by_qty_list [ $i ] = $resultat ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
2018-01-14 15:58:04 +01:00
} */
2011-05-21 10:01:23 +02:00
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
2008-07-27 22:47:13 +02:00
}
2017-12-22 10:56:52 +01:00
}
2018-01-14 15:58:04 +01:00
elseif ( ! empty ( $conf -> global -> PRODUIT_CUSTOMER_PRICES )) // prices per customers
{
// Nothing loaded by default. List may be very long.
}
else if ( ! empty ( $conf -> global -> PRODUIT_CUSTOMER_PRICES_BY_QTY )) // prices per quantity
2012-10-22 17:47:15 +02:00
{
$sql = " SELECT price, price_ttc, price_min, price_min_ttc, " ;
2017-03-04 21:45:19 +01:00
$sql .= " price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid " ;
2012-10-22 17:47:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price " ;
2017-05-12 16:55:11 +02:00
$sql .= " WHERE fk_product = " . $this -> id ;
2016-01-23 20:32:22 +01:00
$sql .= " ORDER BY date_price DESC, rowid DESC " ;
2012-10-22 17:47:15 +02:00
$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 )
{
2017-12-22 10:56:52 +01:00
$sql = " SELECT rowid,price, unitprice, quantity, remise_percent, remise, remise, price_base_type " ;
2012-10-22 17:47:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price_by_qty " ;
2017-05-12 16:55:11 +02:00
$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 " ];
2018-01-14 15:58:04 +01:00
//$resultat[$ii]["remise"]= $result["remise"]; // deprecated
2017-12-22 10:56:52 +01:00
$resultat [ $ii ][ " price_base_type " ] = $result [ " price_base_type " ];
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
}
2018-01-14 15:58:04 +01:00
else if ( ! empty ( $conf -> global -> PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES )) // prices per customer and quantity
{
2018-05-23 17:05:08 +02:00
for ( $i = 1 ; $i <= $conf -> global -> PRODUIT_MULTIPRICES_LIMIT ; $i ++ )
{
$sql = " SELECT price, price_ttc, price_min, price_min_ttc, " ;
$sql .= " price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price " ;
$sql .= " WHERE entity IN ( " . getEntity ( 'productprice' ) . " ) " ;
$sql .= " AND price_level= " . $i ;
$sql .= " AND fk_product = " . $this -> id ;
$sql .= " ORDER BY date_price DESC, rowid DESC " ;
$sql .= " LIMIT 1 " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$result = $this -> db -> fetch_array ( $resql );
$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 " ];
// Next two fields are used only if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on
$this -> multiprices_tva_tx [ $i ] = $result [ " tva_tx " ]; // TODO Add ' ('.$result['default_vat_code'].')'
$this -> multiprices_recuperableonly [ $i ] = $result [ " recuperableonly " ];
// 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 )
{
$sql = " SELECT rowid, price, unitprice, quantity, remise_percent, remise, price_base_type " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_price_by_qty " ;
$sql .= " WHERE fk_product_price = " . $this -> prices_by_qty_id [ $i ];
$sql .= " ORDER BY quantity ASC " ;
$resultat = array ();
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$ii = 0 ;
while ( $result = $this -> db -> fetch_array ( $resql )) {
$resultat [ $ii ] = array ();
$resultat [ $ii ][ " rowid " ] = $result [ " rowid " ];
$resultat [ $ii ][ " price " ] = $result [ " price " ];
$resultat [ $ii ][ " unitprice " ] = $result [ " unitprice " ];
$resultat [ $ii ][ " quantity " ] = $result [ " quantity " ];
$resultat [ $ii ][ " remise_percent " ] = $result [ " remise_percent " ];
$resultat [ $ii ][ " remise " ] = $result [ " remise " ]; // deprecated
$resultat [ $ii ][ " price_base_type " ] = $result [ " price_base_type " ];
$ii ++ ;
}
$this -> prices_by_qty_list [ $i ] = $resultat ;
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
2018-01-14 15:58:04 +01:00
}
2011-05-22 04:14:01 +02:00
2016-02-14 14:26:04 +01:00
if ( ! empty ( $conf -> dynamicprices -> enabled ) && ! 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 ;
2017-03-04 21:45:19 +01:00
// Calculate the VAT
2014-12-28 01:26:41 +01:00
$this -> price_ttc = price2num ( $this -> price ) * ( 1 + ( $this -> tva_tx / 100 ));
$this -> price_ttc = price2num ( $this -> price_ttc , 'MU' );
}
}
2016-04-28 10:24:43 +02:00
// We should not load stock during the fetch. If someone need stock of product, he must call load_stock after fetching product.
2018-01-14 15:58:04 +01:00
// Instead we just init the stock_warehouse array
2014-04-13 02:08:57 +02:00
$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 " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND p.entity IN ( " . getEntity ( 'propal' ) . " ) " ;
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 ;
}
}
2017-09-17 18:08:23 +02:00
/**
* Charge tableau des stats propale pour le produit / service
*
* @ param int $socid Id thirdparty
* @ return array Tableau des stats
*/
function load_stats_proposal_supplier ( $socid = 0 )
{
global $conf ;
global $user ;
$sql = " SELECT COUNT(DISTINCT p.fk_soc) as nb_suppliers, COUNT(DISTINCT p.rowid) as nb, " ;
$sql .= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " supplier_proposaldet as pd " ;
$sql .= " , " . MAIN_DB_PREFIX . " supplier_proposal as p " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE p.rowid = pd.fk_supplier_proposal " ;
$sql .= " AND p.fk_soc = s.rowid " ;
$sql .= " AND p.entity IN ( " . getEntity ( 'supplier_proposal' ) . " ) " ;
$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 ;
//$sql.= " AND pr.fk_statut != 0";
if ( $socid > 0 ) $sql .= " AND p.fk_soc = " . $socid ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
$this -> stats_proposal_supplier [ 'suppliers' ] = $obj -> nb_suppliers ;
$this -> stats_proposal_supplier [ 'nb' ] = $obj -> nb ;
$this -> stats_proposal_supplier [ 'rows' ] = $obj -> nb_rows ;
$this -> stats_proposal_supplier [ 'qty' ] = $obj -> qty ? $obj -> qty : 0 ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2008-07-27 22:47:13 +02:00
/**
2011-06-10 13:39:01 +02:00
* Charge tableau des stats commande client pour le produit / service
2011-08-27 19:25:09 +02:00
*
2017-06-30 17:02:50 +02:00
* @ param int $socid Id societe pour filtrer sur une societe
* @ param string $filtrestatut Id statut pour filtrer sur un statut
* @ param int $forVirtualStock Ignore rights filter for virtual stock calculation .
* @ return array Array of stats ( nb = nb of order , qty = qty ordered )
2008-07-27 22:47:13 +02:00
*/
2017-06-30 17:02:50 +02:00
function load_stats_commande ( $socid = 0 , $filtrestatut = '' , $forVirtualStock = 0 )
2008-07-27 22:47:13 +02:00
{
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 " ;
2017-06-30 17:02:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid && ! $forVirtualStock ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2009-04-27 22:37:50 +02:00
$sql .= " WHERE c.rowid = cd.fk_commande " ;
$sql .= " AND c.fk_soc = s.rowid " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND c.entity IN ( " . getEntity ( 'commande' ) . " ) " ;
2009-04-27 22:37:50 +02:00
$sql .= " AND cd.fk_product = " . $this -> id ;
2017-06-30 17:02:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid && ! $forVirtualStock ) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2009-04-27 22:37:50 +02:00
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 ;
2015-07-24 11:37:15 +02:00
2017-06-09 11:02:34 +02:00
// if it's a virtual product, maybe it is in order by extension
2016-11-11 14:17:11 +01:00
if ( ! empty ( $conf -> global -> ORDER_ADD_ORDERS_WITH_PARENT_PROD_IF_INCDEC ))
2017-06-09 11:02:34 +02:00
{
2016-11-11 14:17:11 +01:00
$TFather = $this -> getFather ();
if ( is_array ( $TFather ) && ! empty ( $TFather )) {
foreach ( $TFather as & $fatherData ) {
$pFather = new Product ( $this -> db );
2017-06-09 11:02:34 +02:00
$pFather -> id = $fatherData [ 'id' ];
2016-11-11 14:17:11 +01:00
$qtyCoef = $fatherData [ 'qty' ];
2017-06-09 11:02:34 +02:00
2016-11-11 14:17:11 +01:00
if ( $fatherData [ 'incdec' ]) {
$pFather -> load_stats_commande ( $socid , $filtrestatut );
2017-06-09 11:02:34 +02:00
2016-11-11 14:17:11 +01:00
$this -> stats_commande [ 'customers' ] += $pFather -> stats_commande [ 'customers' ];
$this -> stats_commande [ 'nb' ] += $pFather -> stats_commande [ 'nb' ];
$this -> stats_commande [ 'rows' ] += $pFather -> stats_commande [ 'rows' ];
$this -> stats_commande [ 'qty' ] += $pFather -> stats_commande [ 'qty' ] * $qtyCoef ;
2017-06-09 11:02:34 +02:00
2016-11-11 14:17:11 +01:00
}
2015-07-24 11:37:15 +02:00
}
}
}
2018-03-14 20:08:44 +01:00
// If stock decrease is on invoice validation, the theorical stock continue to
2018-03-10 18:57:55 +01:00
// count the orders to ship in theorical stock when some are already removed b invoice validation.
// If option DECREASE_ONLY_UNINVOICEDPRODUCTS is on, we make a compensation.
if ( ! empty ( $conf -> global -> STOCK_CALCULATE_ON_BILL ))
2018-03-10 14:17:52 +01:00
{
2018-03-10 18:57:55 +01:00
if ( ! empty ( $conf -> global -> DECREASE_ONLY_UNINVOICEDPRODUCTS ))
2018-03-10 14:17:52 +01:00
{
2018-03-10 18:57:55 +01:00
$adeduire = 0 ;
$sql = " SELECT sum(fd.qty) as count FROM " . MAIN_DB_PREFIX . " facturedet fd " ;
$sql .= " JOIN " . MAIN_DB_PREFIX . " facture f ON fd.fk_facture = f.rowid " ;
$sql .= " JOIN " . MAIN_DB_PREFIX . " element_element el ON el.fk_target = f.rowid and el.targettype = 'facture' and sourcetype = 'commande' " ;
$sql .= " JOIN " . MAIN_DB_PREFIX . " commande c ON el.fk_source = c.rowid " ;
$sql .= " WHERE c.fk_statut IN ( " . $filtrestatut . " ) AND c.facture = 0 AND fd.fk_product = " . $this -> id ;
dol_syslog ( __METHOD__ . " :: sql $sql " , LOG_NOTICE );
$resql = $this -> db -> query ( $sql );
if ( $resql )
2018-03-10 14:17:52 +01:00
{
2018-03-10 18:57:55 +01:00
if ( $this -> db -> num_rows ( $resql ) > 0 )
{
$obj = $this -> db -> fetch_object ( $resql );
$adeduire += $obj -> count ;
}
2018-03-10 14:17:52 +01:00
}
2018-03-10 18:57:55 +01:00
$this -> stats_commande [ 'qty' ] -= $adeduire ;
}
2018-03-10 14:17:52 +01:00
}
2017-06-09 11:02:34 +02:00
2008-07-27 22:47:13 +02:00
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
*
2017-06-30 17:02:50 +02:00
* @ param int $socid Id societe pour filtrer sur une societe
* @ param string $filtrestatut Id des statuts pour filtrer sur des statuts
* @ param int $forVirtualStock Ignore rights filter for virtual stock calculation .
* @ return array Tableau des stats
2008-07-27 22:47:13 +02:00
*/
2017-06-30 17:02:50 +02:00
function load_stats_commande_fournisseur ( $socid = 0 , $filtrestatut = '' , $forVirtualStock = 0 )
2008-07-27 22:47:13 +02:00
{
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 " ;
2017-06-30 17:02:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid && ! $forVirtualStock ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2009-04-27 22:37:50 +02:00
$sql .= " WHERE c.rowid = cd.fk_commande " ;
$sql .= " AND c.fk_soc = s.rowid " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND c.entity IN ( " . getEntity ( 'supplier_order' ) . " ) " ;
2009-04-27 22:37:50 +02:00
$sql .= " AND cd.fk_product = " . $this -> id ;
2017-06-30 17:02:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid && ! $forVirtualStock ) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2009-04-27 22:37:50 +02:00
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
*
2017-06-30 17:02:50 +02:00
* @ param int $socid Id societe pour filtrer sur une societe
* @ param string $filtrestatut Id statut pour filtrer sur un statut
* @ param int $forVirtualStock Ignore rights filter for virtual stock calculation .
* @ return array Tableau des stats
2008-10-23 23:54:30 +02:00
*/
2017-06-30 17:02:50 +02:00
function load_stats_sending ( $socid = 0 , $filtrestatut = '' , $forVirtualStock = 0 )
2008-10-23 23:54:30 +02:00
{
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 " ;
2017-06-30 17:02:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid && ! $forVirtualStock ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2009-04-27 22:37:50 +02:00
$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 " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND e.entity IN ( " . getEntity ( 'expedition' ) . " ) " ;
2009-04-27 22:37:50 +02:00
$sql .= " AND ed.fk_origin_line = cd.rowid " ;
$sql .= " AND cd.fk_product = " . $this -> id ;
2017-06-30 17:02:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid && ! $forVirtualStock ) $sql .= " AND e.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2009-04-27 22:37:50 +02:00
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
*
2017-06-30 17:02:50 +02:00
* @ param int $socid Id societe pour filtrer sur une societe
* @ param string $filtrestatut Id statut pour filtrer sur un statut
* @ param int $forVirtualStock Ignore rights filter for virtual stock calculation .
* @ return array Tableau des stats
2014-07-03 10:24:29 +02:00
*/
2017-06-30 17:02:50 +02:00
function load_stats_reception ( $socid = 0 , $filtrestatut = '' , $forVirtualStock = 0 )
2014-07-03 10:24:29 +02:00
{
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 " ;
2017-06-30 17:02:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid && ! $forVirtualStock ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2014-07-03 10:24:29 +02:00
$sql .= " WHERE cf.rowid = fd.fk_commande " ;
$sql .= " AND cf.fk_soc = s.rowid " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND cf.entity IN ( " . getEntity ( 'supplier_order' ) . " ) " ;
2014-07-03 10:24:29 +02:00
$sql .= " AND fd.fk_product = " . $this -> id ;
2017-06-30 17:02:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid && ! $forVirtualStock ) $sql .= " AND cf.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2014-07-03 10:24:29 +02:00
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 " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND c.entity IN ( " . getEntity ( 'contract' ) . " ) " ;
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 " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND f.entity IN ( " . getEntity ( 'facture' ) . " ) " ;
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 " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND f.entity IN ( " . getEntity ( 'facture_fourn' ) . " ) " ;
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
2017-05-22 15:20:52 +02:00
* @ param int $year Year ( 0 = current year )
2011-12-19 18:24:49 +01:00
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2017-05-22 15:20:52 +02:00
function _get_stats ( $sql , $mode , $year = 0 )
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 ;
}
2017-06-09 12:50:56 +02:00
if ( empty ( $year ))
2017-05-22 15:20:52 +02:00
{
$year = strftime ( '%Y' , time ());
$month = strftime ( '%m' , time ());
}
else
{
$month = 12 ; // We imagine we are at end of year, so we get last 12 month before, so all correct year.
}
2008-07-27 22:47:13 +02:00
$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
*
2015-11-08 00:35:30 +01:00
* @ param int $socid Limit count on a particular third party id
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ param int $filteronproducttype 0 = To filter on product only , 1 = To filter on services only
2017-05-22 15:20:52 +02:00
* @ param int $year Year ( 0 = last 12 month )
* @ param string $morefilter More sql filters
2015-11-08 00:35:30 +01:00
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2017-05-22 15:20:52 +02:00
function get_nb_vente ( $socid , $mode , $filteronproducttype =- 1 , $year = 0 , $morefilter = '' )
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 " ;
2015-11-08 00:35:30 +01:00
if ( $filteronproducttype >= 0 ) $sql .= " , " . MAIN_DB_PREFIX . " product as p " ;
2009-04-27 22:37:50 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE f.rowid = d.fk_facture " ;
2015-11-07 15:13:51 +01:00
if ( $this -> id > 0 ) $sql .= " AND d.fk_product = " . $this -> id ;
2016-05-17 19:15:44 +02:00
else $sql .= " AND d.fk_product > 0 " ;
2015-11-08 00:35:30 +01:00
if ( $filteronproducttype >= 0 ) $sql .= " AND p.rowid = d.fk_product AND p.fk_product_type = " . $filteronproducttype ;
2009-04-27 22:37:50 +02:00
$sql .= " AND f.fk_soc = s.rowid " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND f.entity IN ( " . getEntity ( 'facture' ) . " ) " ;
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 " ;
2017-05-22 15:20:52 +02:00
$sql .= $morefilter ;
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
2017-05-22 15:20:52 +02:00
return $this -> _get_stats ( $sql , $mode , $year );
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
*
2015-11-08 00:35:30 +01:00
* @ param int $socid Limit count on a particular third party id
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ param int $filteronproducttype 0 = To filter on product only , 1 = To filter on services only
2017-05-22 15:20:52 +02:00
* @ param int $year Year ( 0 = last 12 month )
* @ param string $morefilter More sql filters
2015-11-08 00:35:30 +01:00
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2017-05-22 15:20:52 +02:00
function get_nb_achat ( $socid , $mode , $filteronproducttype =- 1 , $year = 0 , $morefilter = '' )
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 " ;
2015-11-08 00:35:30 +01:00
if ( $filteronproducttype >= 0 ) $sql .= " , " . MAIN_DB_PREFIX . " product as p " ;
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 f.rowid = d.fk_facture_fourn " ;
2015-11-07 15:13:51 +01:00
if ( $this -> id > 0 ) $sql .= " AND d.fk_product = " . $this -> id ;
2016-05-17 19:15:44 +02:00
else $sql .= " AND d.fk_product > 0 " ;
2015-11-08 00:35:30 +01:00
if ( $filteronproducttype >= 0 ) $sql .= " AND p.rowid = d.fk_product AND p.fk_product_type = " . $filteronproducttype ;
2009-04-27 22:37:50 +02:00
$sql .= " AND f.fk_soc = s.rowid " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND f.entity IN ( " . getEntity ( 'facture_fourn' ) . " ) " ;
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 " ;
2017-05-22 15:20:52 +02:00
$sql .= $morefilter ;
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
2017-05-22 15:20:52 +02:00
return $this -> _get_stats ( $sql , $mode , $year );
2008-07-27 22:47:13 +02:00
}
/**
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
*
2015-11-08 00:35:30 +01:00
* @ param int $socid Limit count on a particular third party id
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ param int $filteronproducttype 0 = To filter on product only , 1 = To filter on services only
2017-05-22 15:20:52 +02:00
* @ param int $year Year ( 0 = last 12 month )
* @ param string $morefilter More sql filters
2015-11-08 00:35:30 +01:00
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2017-05-22 15:20:52 +02:00
function get_nb_propal ( $socid , $mode , $filteronproducttype =- 1 , $year = 0 , $morefilter = '' )
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 " ;
2017-06-09 11:02:34 +02:00
if ( $filteronproducttype >= 0 ) $sql .= " , " . MAIN_DB_PREFIX . " product as prod " ;
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 " ;
2015-11-07 15:13:51 +01:00
if ( $this -> id > 0 ) $sql .= " AND d.fk_product = " . $this -> id ;
2016-05-17 19:15:44 +02:00
else $sql .= " AND d.fk_product > 0 " ;
2015-11-08 00:35:30 +01:00
if ( $filteronproducttype >= 0 ) $sql .= " AND prod.rowid = d.fk_product AND prod.fk_product_type = " . $filteronproducttype ;
2009-04-27 22:37:50 +02:00
$sql .= " AND p.fk_soc = s.rowid " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND p.entity IN ( " . getEntity ( 'propal' ) . " ) " ;
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 ;
2017-05-22 15:20:52 +02:00
$sql .= $morefilter ;
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
2017-05-22 15:20:52 +02:00
return $this -> _get_stats ( $sql , $mode , $year );
2008-07-27 22:47:13 +02:00
}
2016-05-17 19:15:44 +02:00
/**
* Return nb of units or proposals 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
* @ param int $filteronproducttype 0 = To filter on product only , 1 = To filter on services only
2017-05-22 15:20:52 +02:00
* @ param int $year Year ( 0 = last 12 month )
* @ param string $morefilter More sql filters
2016-05-17 19:15:44 +02:00
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
*/
2017-05-22 15:20:52 +02:00
function get_nb_propalsupplier ( $socid , $mode , $filteronproducttype =- 1 , $year = 0 , $morefilter = '' )
2016-05-17 19:15:44 +02:00
{
global $conf ;
global $user ;
$sql = " SELECT sum(d.qty), date_format(p.date_valid, '%Y%m') " ;
if ( $mode == 'bynumber' ) $sql .= " , count(DISTINCT p.rowid) " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " supplier_proposaldet as d, " . MAIN_DB_PREFIX . " supplier_proposal as p, " . MAIN_DB_PREFIX . " societe as s " ;
2017-06-09 11:02:34 +02:00
if ( $filteronproducttype >= 0 ) $sql .= " , " . MAIN_DB_PREFIX . " product as prod " ;
2016-05-17 19:15:44 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE p.rowid = d.fk_supplier_proposal " ;
if ( $this -> id > 0 ) $sql .= " AND d.fk_product = " . $this -> id ;
else $sql .= " AND d.fk_product > 0 " ;
if ( $filteronproducttype >= 0 ) $sql .= " AND prod.rowid = d.fk_product AND prod.fk_product_type = " . $filteronproducttype ;
$sql .= " AND p.fk_soc = s.rowid " ;
2018-04-21 14:41:31 +02:00
$sql .= " AND p.entity IN ( " . getEntity ( 'supplier_proposal' ) . " ) " ;
2016-05-17 19:15:44 +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 ;
2017-05-22 15:20:52 +02:00
$sql .= $morefilter ;
2016-05-17 19:15:44 +02:00
$sql .= " GROUP BY date_format(p.date_valid,'%Y%m') " ;
$sql .= " ORDER BY date_format(p.date_valid,'%Y%m') DESC " ;
2017-05-22 15:20:52 +02:00
return $this -> _get_stats ( $sql , $mode , $year );
2016-05-17 19:15:44 +02:00
}
2017-06-09 11:02:34 +02:00
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
*
2015-11-08 00:35:30 +01:00
* @ param int $socid Limit count on a particular third party id
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ param int $filteronproducttype 0 = To filter on product only , 1 = To filter on services only
2017-05-22 15:20:52 +02:00
* @ param int $year Year ( 0 = last 12 month )
* @ param string $morefilter More sql filters
2015-11-08 00:35:30 +01:00
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2008-07-27 22:47:13 +02:00
*/
2017-05-22 15:20:52 +02:00
function get_nb_order ( $socid , $mode , $filteronproducttype =- 1 , $year = 0 , $morefilter = '' )
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 " ;
2015-11-08 00:35:30 +01:00
if ( $filteronproducttype >= 0 ) $sql .= " , " . MAIN_DB_PREFIX . " product as p " ;
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 " ;
2015-11-07 15:13:51 +01:00
if ( $this -> id > 0 ) $sql .= " AND d.fk_product = " . $this -> id ;
2016-05-17 19:15:44 +02:00
else $sql .= " AND d.fk_product > 0 " ;
2015-11-08 00:35:30 +01:00
if ( $filteronproducttype >= 0 ) $sql .= " AND p.rowid = d.fk_product AND p.fk_product_type = " . $filteronproducttype ;
2009-04-27 22:37:50 +02:00
$sql .= " AND c.fk_soc = s.rowid " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND c.entity IN ( " . getEntity ( 'commande' ) . " ) " ;
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 ;
2017-05-22 15:20:52 +02:00
$sql .= $morefilter ;
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
2017-05-22 15:20:52 +02:00
return $this -> _get_stats ( $sql , $mode , $year );
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
*
2015-11-08 00:35:30 +01:00
* @ param int $socid Limit count on a particular third party id
* @ param string $mode 'byunit' = number of unit , 'bynumber' = nb of entities
* @ param int $filteronproducttype 0 = To filter on product only , 1 = To filter on services only
2017-05-22 15:20:52 +02:00
* @ param int $year Year ( 0 = last 12 month )
* @ param string $morefilter More sql filters
2015-11-08 00:35:30 +01:00
* @ return array < 0 if KO , result [ month ] = array ( valuex , valuey ) where month is 0 to 11
2013-11-22 14:47:16 +01:00
*/
2017-05-22 15:20:52 +02:00
function get_nb_ordersupplier ( $socid , $mode , $filteronproducttype =- 1 , $year = 0 , $morefilter = '' )
2013-11-22 14:47:16 +01:00
{
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 " ;
2017-06-09 11:02:34 +02:00
if ( $filteronproducttype >= 0 ) $sql .= " , " . MAIN_DB_PREFIX . " product as p " ;
2015-06-17 16:33:18 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2013-11-22 14:47:16 +01:00
$sql .= " WHERE c.rowid = d.fk_commande " ;
2015-11-07 15:13:51 +01:00
if ( $this -> id > 0 ) $sql .= " AND d.fk_product = " . $this -> id ;
2016-05-17 19:15:44 +02:00
else $sql .= " AND d.fk_product > 0 " ;
2015-11-08 00:35:30 +01:00
if ( $filteronproducttype >= 0 ) $sql .= " AND p.rowid = d.fk_product AND p.fk_product_type = " . $filteronproducttype ;
2013-11-22 14:47:16 +01:00
$sql .= " AND c.fk_soc = s.rowid " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND c.entity IN ( " . getEntity ( 'supplier_order' ) . " ) " ;
2013-11-22 14:47:16 +01: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 ;
2017-05-22 15:20:52 +02:00
$sql .= $morefilter ;
2013-11-22 14:47:16 +01:00
$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
2017-05-22 15:20:52 +02:00
return $this -> _get_stats ( $sql , $mode , $year );
2013-11-22 14:47:16 +01:00
}
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 ();
2016-05-17 22:59:46 +02:00
dol_syslog ( get_class ( $this ) . " ::add_fournisseur id_fourn = " . $id_fourn . " ref_fourn= " . $ref_fourn . " quantity= " . $quantity , LOG_DEBUG );
2017-06-09 11:02:34 +02:00
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 ;
2016-03-31 21:47:32 +02:00
$sql .= " AND ref_fourn = ' " . $this -> db -> escape ( $ref_fourn ) . " ' " ;
2011-10-05 12:51:55 +02:00
$sql .= " AND fk_product != " . $this -> id ;
2018-07-07 08:48:26 +02:00
$sql .= " AND entity IN ( " . getEntity ( 'productsupplierprice' ) . " ) " ;
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 ;
2016-03-31 21:47:32 +02:00
if ( $ref_fourn ) $sql .= " AND ref_fourn = ' " . $this -> db -> escape ( $ref_fourn ) . " ' " ;
2011-10-05 12:51:55 +02:00
else $sql .= " AND (ref_fourn = '' OR ref_fourn IS NULL) " ;
$sql .= " AND quantity = ' " . $quantity . " ' " ;
$sql .= " AND fk_product = " . $this -> id ;
2018-07-07 08:48:26 +02:00
$sql .= " AND entity IN ( " . getEntity ( 'productsupplierprice' ) . " ) " ;
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 ;
2016-03-31 21:47:32 +02:00
$sql .= " , ' " . $this -> db -> escape ( $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
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 ();
2016-11-18 08:23:12 +01:00
$sql = " SELECT DISTINCT p.fk_soc " ;
2011-10-04 01:26:07 +02:00
$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
*
2016-11-14 01:46:59 +01:00
* @ param int $fromId Product id
* @ param int $toId Product id
* @ return int < 0 if KO , > 0 if OK
2014-05-13 10:12:45 +02:00
*/
2013-06-10 00:11:25 +02:00
function clone_associations ( $fromId , $toId )
{
$this -> db -> begin ();
2016-11-14 01:46:59 +01:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'product_association (fk_product_pere, fk_product_fils, qty)' ;
$sql .= " SELECT " . $toId . " , fk_product_fils, qty FROM " . MAIN_DB_PREFIX . " product_association " ;
2016-11-14 01:47:34 +01:00
$sql .= " WHERE fk_product_pere = " . $fromId ;
2013-06-10 00:11:25 +02:00
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
2016-04-28 10:24:43 +02:00
$this -> load_stock ( 'nobatch,novirtual' ); // Load stock to get true this->stock_reel
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 ,
2015-10-29 12:28:28 +01:00
'incdec' => $incdec ,
'entity' => $this -> entity
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
/**
2018-03-22 18:01:06 +01:00
* Build the tree of subproducts into an array
* this -> sousprods is loaded by this -> get_sousproduits_arbo ()
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
2017-12-01 17:48:51 +01:00
/**
* Return all parent products for current product ( first level only )
*
* @ return int Nb of father + child
*/
function hasFatherOrChild ()
{
$nb = 0 ;
$sql = " SELECT COUNT(pa.rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product_association as pa " ;
$sql .= " WHERE pa.fk_product_fils = " . $this -> id . " OR pa.fk_product_pere = " . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj ) $nb = $obj -> nb ;
}
else
{
return - 1 ;
}
return $nb ;
}
2011-08-18 18:16:04 +02:00
/**
2016-05-19 18:52:12 +02:00
* Return all parent products for current product ( first level only )
2011-08-27 19:25:09 +02:00
*
2015-05-03 21:15:08 +02:00
* @ return array Array of product
2011-08-18 18:16:04 +02:00
*/
function getFather ()
{
2015-10-29 12:28:28 +01: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, p.entity " ;
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
{
2016-05-04 13:51:47 +02:00
// $record['id'] = $record['rowid'] = id of father
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' ];
2015-10-29 12:28:28 +01:00
$prods [ $record [ 'id' ]][ 'entity' ] = $record [ 'entity' ];
2011-08-18 18:16:04 +02:00
}
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 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
2016-05-04 13:49:05 +02:00
* @ param int $level Level of recursing call ( start to 1 )
2018-02-26 21:03:52 +01:00
* @ return array Return array ( prodid => array ( 0 = prodid , 1 => qty , 2 => ... )
2008-07-27 22:47:13 +02:00
*/
2016-05-04 13:49:05 +02:00
function getChildsArbo ( $id , $firstlevelonly = 0 , $level = 1 )
2008-07-27 22:47:13 +02:00
{
2016-05-12 19:44:49 +02:00
global $alreadyfound ;
2017-06-09 11:02:34 +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
2016-05-04 13:49:05 +02:00
dol_syslog ( get_class ( $this ) . '::getChildsArbo id=' . $id . ' level=' . $level , LOG_DEBUG );
2017-06-09 11:02:34 +02:00
2016-05-12 19:44:49 +02:00
if ( $level == 1 ) $alreadyfound = array ( $id => 1 ); // We init array of found object to start of tree, so if we found it later (should not happened), we stop immediatly
2016-05-04 13:49:05 +02:00
// Protection against infinite loop
if ( $level > 30 ) return array ();
2017-06-09 11:02:34 +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 ();
2010-02-20 15:24:47 +01:00
while ( $rec = $this -> db -> fetch_array ( $res ))
2009-04-05 21:01:43 +02:00
{
2017-06-09 11:02:34 +02:00
if ( ! empty ( $alreadyfound [ $rec [ 'rowid' ]]))
2016-05-12 19:44:49 +02:00
{
dol_syslog ( get_class ( $this ) . '::getChildsArbo the product id=' . $rec [ 'rowid' ] . ' was already found at a higher level in tree. We discard to avoid infinite loop' , LOG_WARNING );
continue ;
}
$alreadyfound [ $rec [ 'rowid' ]] = 1 ;
2015-02-23 12:19:19 +01:00
$prods [ $rec [ 'rowid' ]] = array (
2016-05-12 19:44:49 +02:00
0 => $rec [ 'rowid' ],
2015-02-23 12:19:19 +01:00
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
{
2016-05-12 19:44:49 +02:00
$listofchilds = $this -> getChildsArbo ( $rec [ 'rowid' ], 0 , $level + 1 );
2015-03-11 14:55:45 +01:00
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
{
2016-05-08 11:33:46 +02:00
$parent = array ();
2016-05-04 13:15:00 +02:00
2016-05-12 19:44:49 +02:00
foreach ( $this -> getChildsArbo ( $this -> id ) as $keyChild => $valueChild ) // Warning. getChildsArbo can call getChildsArbo recursively. Starting point is $value[0]=id of product
2008-07-27 22:47:13 +02:00
{
2016-05-12 19:44:49 +02:00
$parent [ $this -> label ][ $keyChild ] = $valueChild ;
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
*
2017-10-03 16:00:52 +02:00
* @ param int $withpicto Add picto into link
* @ param string $option Where point the link ( 'stock' , 'composition' , 'category' , 'supplier' , '' )
* @ param int $maxlength Maxlength of ref
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
* @ return string String with URL
2008-07-27 22:47:13 +02:00
*/
2017-10-03 16:00:52 +02:00
function getNomUrl ( $withpicto = 0 , $option = '' , $maxlength = 0 , $save_lastsearch_value =- 1 )
2008-07-27 22:47:13 +02:00
{
2016-07-15 11:52:59 +02:00
global $conf , $langs , $hookmanager ;
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' );
2017-06-09 11:02:34 +02:00
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
2016-11-04 15:43:30 +01:00
if ( $this -> type == Product :: TYPE_PRODUCT )
2016-10-30 16:19:31 +01:00
{
2016-11-04 15:43:30 +01:00
if ( $this -> weight ) $label .= " <br><b> " . $langs -> trans ( " Weight " ) . '</b>: ' . $this -> weight . ' ' . measuring_units_string ( $this -> weight_units , " weight " );
if ( $this -> length ) $label .= " <br><b> " . $langs -> trans ( " Length " ) . '</b>: ' . $this -> length . ' ' . measuring_units_string ( $this -> length_units , 'length' );
if ( $this -> surface ) $label .= " <br><b> " . $langs -> trans ( " Surface " ) . '</b>: ' . $this -> surface . ' ' . measuring_units_string ( $this -> surface_units , 'surface' );
if ( $this -> volume ) $label .= " <br><b> " . $langs -> trans ( " Volume " ) . '</b>: ' . $this -> volume . ' ' . measuring_units_string ( $this -> volume_units , 'volume' );
2017-12-01 13:40:24 +01:00
}
if ( $this -> type == Product :: TYPE_PRODUCT || ! empty ( $conf -> global -> STOCK_SUPPORTS_SERVICES ))
{
if ( ! empty ( $conf -> productbatch -> enabled ))
2016-11-04 15:43:30 +01:00
{
2017-11-12 15:30:21 +01:00
$langs -> load ( " productbatch " );
2016-11-04 15:43:30 +01:00
$label .= " <br><b> " . $langs -> trans ( " ManageLotSerial " ) . '</b>: ' . $this -> getLibStatut ( 0 , 2 );
}
}
if ( $this -> type == Product :: TYPE_SERVICE )
{
//
2016-10-30 16:19:31 +01:00
}
2018-04-08 23:54:04 +02:00
if ( ! empty ( $conf -> accounting -> enabled ) && $this -> status )
{
include_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php' ;
$label .= '<br><b>' . $langs -> trans ( 'ProductAccountancySellCode' ) . ':</b> ' . length_accountg ( $this -> accountancy_code_sell );
$label .= '<br><b>' . $langs -> trans ( 'ProductAccountancySellIntraCode' ) . ':</b> ' . length_accountg ( $this -> accountancy_code_sell_export );
$label .= '<br><b>' . $langs -> trans ( 'ProductAccountancySellExportCode' ) . ':</b> ' . length_accountg ( $this -> accountancy_code_sell_intra );
}
if ( ! empty ( $conf -> accounting -> enabled ) && $this -> status_buy )
{
2018-05-04 18:12:18 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php' ;
2018-04-08 23:54:04 +02:00
$label .= '<br><b>' . $langs -> trans ( 'ProductAccountancyBuyCode' ) . ':</b> ' . length_accountg ( $this -> accountancy_code_buy );
}
2017-06-09 11:02:34 +02:00
if ( ! empty ( $this -> entity ))
2016-11-14 13:40:41 +01:00
{
2018-03-22 11:46:35 +01:00
$tmpphoto = $this -> show_photos ( 'product' , $conf -> product -> multidir_output [ $this -> entity ], 1 , 1 , 0 , 0 , 0 , 80 );
2017-06-09 11:02:34 +02:00
if ( $this -> nbphoto > 0 ) $label .= '<br>' . $tmpphoto ;
2016-11-14 13:40:41 +01:00
}
2016-10-30 16:19:31 +01:00
$linkclose = '' ;
if ( empty ( $notooltip ))
{
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER ))
{
$label = $langs -> trans ( " ShowOrder " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
}
2016-12-10 23:34:32 +01:00
$linkclose .= ' title="' . dol_escape_htmltag ( $label , 1 , 1 ) . '"' ;
2017-10-03 16:00:52 +02:00
$linkclose .= ' class="classfortooltip"' ;
2017-06-09 11:02:34 +02:00
2018-06-04 12:55:55 +02:00
/*
2016-10-30 16:19:31 +01:00
$hookmanager -> initHooks ( array ( 'productdao' ));
$parameters = array ( 'id' => $this -> id );
$reshook = $hookmanager -> executeHooks ( 'getnomurltooltip' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) $linkclose = $hookmanager -> resPrint ;
2018-03-15 20:46:55 +01:00
*/
2016-10-30 16:19:31 +01:00
}
2016-07-14 18:40:49 +02:00
2016-08-29 21:31:27 +02:00
if ( $option == 'supplier' || $option == 'category' ) {
2016-10-30 16:19:31 +01:00
$url = DOL_URL_ROOT . '/product/fournisseurs.php?id=' . $this -> id ;
2015-01-20 20:45:38 +01:00
} else if ( $option == 'stock' ) {
2016-10-30 16:19:31 +01:00
$url = DOL_URL_ROOT . '/product/stock/product.php?id=' . $this -> id ;
2015-01-20 20:45:38 +01:00
} else if ( $option == 'composition' ) {
2016-10-30 16:19:31 +01:00
$url = DOL_URL_ROOT . '/product/composition/card.php?id=' . $this -> id ;
2015-01-20 20:45:38 +01:00
} else {
2016-10-30 16:19:31 +01:00
$url = DOL_URL_ROOT . '/product/card.php?id=' . $this -> id ;
2011-09-07 21:18:28 +02:00
}
2008-07-27 22:47:13 +02:00
2017-10-03 16:00:52 +02:00
if ( $option !== 'nolink' )
{
// Add param to save lastsearch_values or not
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) $add_save_lastsearch_values = 1 ;
if ( $add_save_lastsearch_values ) $url .= '&save_lastsearch_values=1' ;
}
2016-10-30 16:19:31 +01:00
$linkstart = '<a href="' . $url . '"' ;
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2017-06-09 11:02:34 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
2008-07-27 22:47:13 +02:00
if ( $withpicto ) {
2017-11-02 15:03:09 +01:00
if ( $this -> type == Product :: TYPE_PRODUCT ) $result .= ( img_object (( $notooltip ? '' : $label ), 'product' , ( $notooltip ? 'class="paddingright"' : 'class="paddingright classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 ));
if ( $this -> type == Product :: TYPE_SERVICE ) $result .= ( img_object (( $notooltip ? '' : $label ), 'service' , ( $notooltip ? 'class="paddinright"' : 'class="paddingright classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 ));
2008-07-27 22:47:13 +02:00
}
2017-11-02 15:03:09 +01:00
$result .= $newref ;
$result .= $linkend ;
2018-03-15 20:46:55 +01:00
global $action ;
$hookmanager -> initHooks ( array ( 'productdao' ));
$parameters = array ( 'id' => $this -> id , 'getnomurl' => $result );
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) $result = $hookmanager -> resPrint ;
else $result .= $hookmanager -> resPrint ;
2008-07-27 22:47:13 +02:00
return $result ;
}
2017-01-06 16:44:09 +01:00
/**
* Create a document onto disk according to template module .
*
* @ param string $modele Force model to use ( '' to not force )
* @ param Translate $outputlangs Object langs to use for output
* @ param int $hidedetails Hide details of lines
* @ param int $hidedesc Hide description
* @ param int $hideref Hide ref
* @ return int 0 if KO , 1 if OK
*/
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 )
{
global $conf , $user , $langs ;
$langs -> load ( " products " );
// Positionne le modele sur le nom du modele a utiliser
if ( ! dol_strlen ( $modele ))
{
if ( ! empty ( $conf -> global -> PRODUCT_ADDON_PDF ))
{
$modele = $conf -> global -> PRODUCT_ADDON_PDF ;
}
else
{
$modele = 'strato' ;
}
}
$modelpath = " core/modules/product/doc/ " ;
return $this -> commonGenerateDocument ( $modelpath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref );
}
2008-07-27 22:47:13 +02:00
/**
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
{
2017-11-27 14:48:36 +01:00
case 0 :
return $this -> LibStatut ( $this -> status , $mode , $type );
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
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
{
2015-10-18 20:57:28 +02:00
global $conf , $langs ;
2017-06-09 11:02:34 +02:00
2008-07-27 22:47:13 +02:00
$langs -> load ( 'products' );
2015-12-19 18:03:08 +01: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 :
2017-10-07 20:21:47 +02:00
if ( $status == 0 )
2014-03-04 18:55:55 +01:00
{
return img_picto ( $langs -> trans ( 'ProductStatusNotOnBatch' ), 'statut5' );
}
2017-10-07 20:21:47 +02:00
return img_picto ( $langs -> trans ( 'ProductStatusOnBatch' ), 'statut4' );
2014-03-04 18:55:55 +01:00
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 )
{
2016-09-10 19:44:55 +02:00
if ( $status == 0 ) return img_picto ( $langs -> trans ( 'ProductStatusNotOnSell' ), 'statut5' , 'class="pictostatus"' ) . ' ' . ( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSellShort' ) : $langs -> trans ( 'ProductStatusNotOnBuyShort' ));
if ( $status == 1 ) return img_picto ( $langs -> trans ( 'ProductStatusOnSell' ), 'statut4' , 'class="pictostatus"' ) . ' ' . ( $type == 0 ? $langs -> trans ( 'ProductStatusOnSellShort' ) : $langs -> trans ( 'ProductStatusOnBuyShort' ));
2008-07-27 22:47:13 +02:00
}
if ( $mode == 3 )
{
2016-09-10 19:44:55 +02:00
if ( $status == 0 ) return img_picto (( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSell' ) : $langs -> trans ( 'ProductStatusNotOnBuy' )), 'statut5' , 'class="pictostatus"' );
if ( $status == 1 ) return img_picto (( $type == 0 ? $langs -> trans ( 'ProductStatusOnSell' ) : $langs -> trans ( 'ProductStatusOnBuy' )), 'statut4' , 'class="pictostatus"' );
2008-07-27 22:47:13 +02:00
}
if ( $mode == 4 )
{
2016-09-10 19:44:55 +02:00
if ( $status == 0 ) return img_picto ( $langs -> trans ( 'ProductStatusNotOnSell' ), 'statut5' , 'class="pictostatus"' ) . ' ' . ( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSell' ) : $langs -> trans ( 'ProductStatusNotOnBuy' ));
if ( $status == 1 ) return img_picto ( $langs -> trans ( 'ProductStatusOnSell' ), 'statut4' , 'class="pictostatus"' ) . ' ' . ( $type == 0 ? $langs -> trans ( 'ProductStatusOnSell' ) : $langs -> trans ( 'ProductStatusOnBuy' ));
2008-07-27 22:47:13 +02:00
}
if ( $mode == 5 )
{
2016-09-10 19:44:55 +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' , 'class="pictostatus"' );
2017-04-07 11:01:12 +02:00
if ( $status == 1 ) return ( $type == 0 ? $langs -> trans ( 'ProductStatusOnSellShort' ) : $langs -> trans ( 'ProductStatusOnBuyShort' )) . ' ' . img_picto (( $type == 0 ? $langs -> trans ( 'ProductStatusOnSell' ) : $langs -> trans ( 'ProductStatusOnBuy' )), 'statut4' , 'class="pictostatus"' );
}
if ( $mode == 6 )
{
if ( $status == 0 ) return ( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSellShort' ) : $langs -> trans ( 'ProductStatusNotOnBuyShort' )) . ' ' . img_picto (( $type == 0 ? $langs -> trans ( 'ProductStatusNotOnSell' ) : $langs -> trans ( 'ProductStatusNotOnBuy' )), 'statut5' , 'class="pictostatus"' );
2016-09-10 19:44:55 +02:00
if ( $status == 1 ) return ( $type == 0 ? $langs -> trans ( 'ProductStatusOnSellShort' ) : $langs -> trans ( 'ProductStatusOnBuyShort' )) . ' ' . img_picto (( $type == 0 ? $langs -> trans ( 'ProductStatusOnSell' ) : $langs -> trans ( 'ProductStatusOnBuy' )), 'statut4' , 'class="pictostatus"' );
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
2017-01-29 13:04:06 +01:00
* @ param string $origin_element Origin element type
* @ param int $origin_id Origin id of element
2011-12-19 18:24:49 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2017-01-29 13:04:06 +01:00
function correct_stock ( $user , $id_entrepot , $nbpiece , $movement , $label = '' , $price = 0 , $inventorycode = '' , $origin_element = '' , $origin_id = null )
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 );
2017-01-29 13:04:06 +01:00
$movementstock -> setOrigin ( $origin_element , $origin_id );
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
2011-12-19 18:24:49 +01:00
* @ param double $price Price to use for stock eval
2015-02-11 22:54:06 +01:00
* @ param date $dlc eat - by date
* @ param date $dluo sell - by date
* @ param string $lot Lot number
* @ param string $inventorycode Inventory code
2017-01-29 13:04:06 +01:00
* @ param string $origin_element Origin element type
* @ param int $origin_id Origin id of element
2011-12-19 18:24:49 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2017-01-29 13:04:06 +01:00
function correct_stock_batch ( $user , $id_entrepot , $nbpiece , $movement , $label = '' , $price = 0 , $dlc = '' , $dluo = '' , $lot = '' , $inventorycode = '' , $origin_element = '' , $origin_id = null )
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 );
2017-01-29 13:04:06 +01:00
$movementstock -> setOrigin ( $origin_element , $origin_id );
2015-02-11 22:54:06 +01:00
$result = $movementstock -> _create ( $user , $this -> id , $id_entrepot , $op [ $movement ], $movement , $price , $label , $inventorycode , '' , $dlc , $dluo , $lot );
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-02-11 22:54:06 +01: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
2008-03-31 07:56:45 +02:00
/**
2017-02-27 00:56:44 +01:00
* Load information about stock of a product into -> stock_reel , -> stock_warehouse [] ( including stock_warehouse [ idwarehouse ] -> detail_batch for batch products )
2017-06-09 12:50:56 +02:00
* This function need a lot of load . If you use it on list , use a cache to execute it once for each product id .
2016-12-05 18:08:09 +01:00
* If ENTREPOT_EXTRA_STATUS set , filtering on warehouse status possible .
2011-08-27 19:25:09 +02:00
*
2017-06-09 11:02:34 +02:00
* @ param string $option '' = Load all stock info , also from closed and internal warehouses ,
* 'nobatch' = Do not load batch information ,
2016-12-05 18:08:09 +01:00
* 'novirtual' = Do not load virtual stock ,
2017-02-27 00:56:44 +01:00
* 'warehouseopen' = Load stock from open warehouses only ,
2017-06-09 12:50:56 +02:00
* 'warehouseclosed' = Load stock from closed warehouses only ,
2017-02-27 00:56:44 +01:00
* 'warehouseinternal' = Load stock from warehouses for internal correction / transfer only
* @ return int < 0 if KO , > 0 if OK
2016-04-27 14:19:06 +02:00
* @ see load_virtual_stock , getBatchInfo
2008-07-27 22:47:13 +02:00
*/
2016-04-27 14:19:06 +02:00
function load_stock ( $option = '' )
2008-07-27 22:47:13 +02:00
{
2016-12-05 18:08:09 +01:00
global $conf ;
2017-06-09 11:02:34 +02:00
2008-07-27 22:47:13 +02:00
$this -> stock_reel = 0 ;
2014-04-12 01:45:18 +02:00
$this -> stock_warehouse = array ();
2016-04-27 14:19:06 +02:00
$this -> stock_theorique = 0 ;
2016-12-05 18:08:09 +01:00
$warehouseStatus = array ();
2017-06-09 11:02:34 +02:00
if ( preg_match ( '/warehouseclosed/' , $option ))
2016-12-05 18:08:09 +01:00
{
$warehouseStatus [] = Entrepot :: STATUS_CLOSED ;
}
2017-06-09 11:02:34 +02:00
if ( preg_match ( '/warehouseopen/' , $option ))
2016-12-05 18:08:09 +01:00
{
$warehouseStatus [] = Entrepot :: STATUS_OPEN_ALL ;
}
2017-06-09 11:02:34 +02:00
if ( preg_match ( '/warehouseinternal/' , $option ))
2016-12-05 18:08:09 +01:00
{
$warehouseStatus [] = Entrepot :: STATUS_OPEN_INTERNAL ;
}
2017-06-09 11:02:34 +02:00
2016-04-09 10:01:22 +02:00
$sql = " SELECT ps.rowid, ps.reel, ps.fk_entrepot " ;
2012-07-02 12:07:42 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_stock as ps " ;
$sql .= " , " . MAIN_DB_PREFIX . " entrepot as w " ;
2017-05-30 18:50:54 +02:00
$sql .= " WHERE w.entity IN ( " . getEntity ( 'stock' ) . " ) " ;
2012-07-02 12:07:42 +02:00
$sql .= " AND w.rowid = ps.fk_entrepot " ;
$sql .= " AND ps.fk_product = " . $this -> id ;
2017-09-12 19:12:39 +02:00
if ( $conf -> global -> ENTREPOT_EXTRA_STATUS && count ( $warehouseStatus )) $sql .= " AND w.statut IN ( " . $this -> db -> escape ( implode ( ',' , $warehouseStatus )) . " ) " ;
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 ;
2014-07-15 11:22:16 +02:00
$this -> stock_warehouse [ $row -> fk_entrepot ] -> id = $row -> rowid ;
2017-12-02 10:43:01 +01:00
if (( ! preg_match ( '/nobatch/' , $option )) && $this -> hasbatch ()) $this -> stock_warehouse [ $row -> fk_entrepot ] -> detail_batch = Productbatch :: findAll ( $this -> db , $row -> rowid , 1 , $this -> id );
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
2017-06-09 11:02:34 +02:00
if ( ! preg_match ( '/novirtual/' , $option ))
2016-04-27 14:19:06 +02:00
{
$this -> load_virtual_stock (); // This also load stats_commande_fournisseur, ...
}
2015-03-01 16:04:34 +01:00
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
/**
2016-04-27 13:38:34 +02:00
* Load value -> stock_theorique of a product . Property this -> id must be defined .
2017-06-09 11:02:34 +02:00
* This function need a lot of load . If you use it on list , use a cache to execute it one for each product id .
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 ))
{
2017-06-30 17:02:50 +02:00
$result = $this -> load_stats_commande ( 0 , '1,2' , 1 );
2015-10-18 20:58:39 +02:00
if ( $result < 0 ) dol_print_error ( $this -> db , $this -> error );
2014-11-01 21:38:50 +01:00
$stock_commande_client = $this -> stats_commande [ 'qty' ];
}
2015-03-01 16:04:34 +01:00
if ( ! empty ( $conf -> expedition -> enabled ))
{
2017-06-30 17:02:50 +02:00
$result = $this -> load_stats_sending ( 0 , '1,2' , 1 );
2015-10-18 20:58:39 +02:00
if ( $result < 0 ) dol_print_error ( $this -> db , $this -> error );
2014-11-01 21:38:50 +01:00
$stock_sending_client = $this -> stats_expedition [ 'qty' ];
}
2015-03-01 16:04:34 +01:00
if ( ! empty ( $conf -> fournisseur -> enabled ))
{
2017-06-30 17:02:50 +02:00
$result = $this -> load_stats_commande_fournisseur ( 0 , '1,2,3,4' , 1 );
2015-10-18 20:58:39 +02:00
if ( $result < 0 ) dol_print_error ( $this -> db , $this -> error );
2014-11-01 21:38:50 +01:00
$stock_commande_fournisseur = $this -> stats_commande_fournisseur [ 'qty' ];
2017-06-30 17:02:50 +02:00
$result = $this -> load_stats_reception ( 0 , '4' , 1 );
2015-10-18 20:58:39 +02:00
if ( $result < 0 ) dol_print_error ( $this -> db , $this -> error );
2014-11-01 21:38:50 +01:00
$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
2016-05-14 19:22:27 +02:00
if ( ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SHIPMENT ) || ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SHIPMENT_CLOSE )) {
2014-11-01 21:38:50 +01:00
$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 ;
2015-05-18 22:25:51 +02:00
if ( ! empty ( $conf -> global -> PRODUCT_USE_OLD_PATH_FOR_PHOTO )) $dir .= '/' . get_exdir ( $this -> id , 2 , 0 , 0 , $this , 'product' ) . $this -> id . " /photos " ;
2015-11-02 17:56:00 +01:00
else $dir .= '/' . get_exdir ( 0 , 0 , 0 , 0 , $this , 'product' ) . 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
{
2016-04-09 14:12:21 +02:00
// Create thumbs
$this -> addThumbs ( $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
}
/**
2016-12-31 16:16:20 +01:00
* Return if at least one photo is available
2011-08-27 19:25:09 +02:00
*
2016-12-31 16:16:20 +01:00
* @ param string $sdir Directory to scan
* @ return boolean True if at least one photo is available , False if not
2008-07-27 22:47:13 +02:00
*/
function is_photo_available ( $sdir )
{
2016-12-31 16:16:20 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php' ;
2015-05-07 10:52:41 +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-05-18 22:25:51 +02:00
if ( ! empty ( $conf -> global -> PRODUCT_USE_OLD_PATH_FOR_PHOTO )) $dir .= '/' . get_exdir ( $this -> id , 2 , 0 , 0 , $this , 'product' ) . $this -> id . " /photos/ " ;
2015-11-02 17:56:00 +01:00
else $dir .= '/' . get_exdir ( 0 , 0 , 0 , 0 , $this , 'product' ) . 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
{
2016-04-09 16:04:07 +02:00
while (( $file = readdir ( $handle )) !== false )
2010-12-15 19:15:08 +01:00
{
if ( ! utf8_check ( $file )) $file = utf8_encode ( $file ); // To be sure data is stored in UTF8 in memory
2016-12-31 16:16:20 +01:00
if ( dol_is_file ( $dir . $file ) && image_format_supported ( $file ) > 0 ) return true ;
2010-12-15 19:15:08 +01:00
}
2008-07-27 22:47:13 +02:00
}
}
return false ;
}
/**
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 )
{
2016-12-31 16:16:20 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/images.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
{
2016-04-09 16:04:07 +02:00
while (( $file = readdir ( $handle )) !== false )
2008-07-27 22:47:13 +02:00
{
2009-10-04 19:18:09 +02:00
if ( ! utf8_check ( $file )) $file = utf8_encode ( $file ); // readdir returns ISO
2016-12-31 16:16:20 +01:00
if ( dol_is_file ( $dir . $file ) && image_format_supported ( $file ) >= 0 )
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 )
{
2016-12-31 16:16:20 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/images.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
2015-05-07 11:59:45 +02:00
dol_delete_file ( $file , 0 , 0 , 0 , $this ); // For triggers
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
}
2015-06-16 09:28:31 +02:00
$photo_vignette = preg_replace ( '/' . $regs [ 0 ] . '/i' , '' , $filename ) . '_mini' . $regs [ 0 ];
if ( file_exists ( dol_osencode ( $dirthumb . $photo_vignette )))
{
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
}
/**
2017-02-28 12:35:55 +01:00
* Load indicators this -> nb for the dashboard
2011-08-27 19:25:09 +02:00
*
2017-02-27 00:56:44 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-07-27 22:47:13 +02:00
*/
2017-02-28 12:35:55 +01:00
function load_state_board ()
2008-07-27 22:47:13 +02:00
{
2017-06-09 11:02:34 +02:00
global $conf , $user , $hookmanager ;
2008-07-27 22:47:13 +02:00
$this -> nb = array ();
2017-02-28 12:35:55 +01:00
$sql = " SELECT count(p.rowid) as nb, fk_product_type " ;
2008-07-27 22:47:13 +02:00
$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 ) . ')' ;
2017-06-09 11:02:34 +02:00
// Add where from hooks
if ( is_object ( $hookmanager ))
{
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'printFieldListWhere' , $parameters ); // Note that $action and $object may have been modified by hook
$sql .= $hookmanager -> resPrint ;
}
2017-06-09 12:50:56 +02:00
$sql .= ' GROUP BY fk_product_type' ;
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 ))
{
2017-02-28 12:35:55 +01:00
if ( $obj -> fk_product_type == 1 ) $this -> nb [ " services " ] = $obj -> nb ;
2017-02-27 00:56:44 +01:00
else $this -> nb [ " products " ] = $obj -> nb ;
2008-07-27 22:47:13 +02:00
}
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
*/
2015-11-07 14:37:47 +01:00
function isProduct ()
2010-02-20 15:24:47 +01:00
{
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
*/
2015-11-07 14:37:47 +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
2017-04-12 19:30:01 +02:00
$this -> specimen = 1 ;
2011-10-01 01:40:15 +02:00
$this -> id = 0 ;
$this -> ref = 'PRODUCT_SPEC' ;
2015-05-04 03:03:24 +02:00
$this -> label = 'PRODUCT SPECIMEN' ;
2017-05-04 20:16:55 +02:00
$this -> description = 'This is description of this product specimen that was created the ' . dol_print_date ( $now , 'dayhourlog' ) . '.' ;
2011-10-01 01:40:15 +02:00
$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)' ;
2017-05-04 20:16:55 +02:00
$this -> date_creation = $now ;
$this -> date_modification = $now ;
2017-06-09 12:50:56 +02:00
2017-05-04 20:16:55 +02:00
$this -> weight = 4 ;
$this -> weight_unit = 1 ;
$this -> length = 5 ;
$this -> length_unit = 1 ;
$this -> width = 6 ;
$this -> width_unit = 0 ;
$this -> height = null ;
$this -> height_unit = null ;
2017-06-09 12:50:56 +02:00
2017-05-04 20:16:55 +02:00
$this -> surface = 30 ;
$this -> surface_unit = 0 ;
$this -> volume = 300 ;
$this -> volume_unit = 0 ;
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
2014-03-05 10:30:06 +01:00
/**
2016-03-17 09:40:05 +01:00
* Returns the text label from units dictionary
2014-03-05 10:30:06 +01:00
*
2015-02-26 14:15:33 +01:00
* @ param string $type Label type ( long or short )
* @ return string | int < 0 if ko , label if ok
2014-03-05 10:30:06 +01:00
*/
2015-04-21 15:49:58 +02:00
function getLabelOfUnit ( $type = 'long' )
2014-03-05 10:30:06 +01:00
{
2015-02-26 14:15:33 +01:00
global $langs ;
2014-03-05 10:30:06 +01:00
2015-02-26 14:15:33 +01:00
if ( ! $this -> fk_unit ) {
return '' ;
}
2014-03-05 10:30:06 +01:00
2015-02-26 14:15:33 +01:00
$langs -> load ( 'products' );
2014-03-05 10:30:06 +01:00
2015-02-26 14:15:33 +01:00
$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-05 10:30:06 +01:00
}
}
2014-09-07 13:18:56 +02:00
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
2015-09-30 18:46:51 +02:00
/**
* Sets object to supplied categories .
*
* Deletes object from existing categories not supplied .
* Adds it to non existing supplied categories .
* Existing categories are left untouch .
*
* @ param int [] | int $categories Category or categories IDs
*/
public function setCategories ( $categories ) {
// Handle single category
if ( ! is_array ( $categories )) {
$categories = array ( $categories );
}
// Get current categories
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
$c = new Categorie ( $this -> db );
$existing = $c -> containing ( $this -> id , Categorie :: TYPE_PRODUCT , 'id' );
// Diff
if ( is_array ( $existing )) {
$to_del = array_diff ( $existing , $categories );
$to_add = array_diff ( $categories , $existing );
} else {
$to_del = array (); // Nothing to delete
$to_add = $categories ;
}
// Process
foreach ( $to_del as $del ) {
2015-10-15 10:13:52 +02:00
if ( $c -> fetch ( $del ) > 0 ) {
$c -> del_type ( $this , 'product' );
}
2015-09-30 18:46:51 +02:00
}
foreach ( $to_add as $add ) {
2015-10-15 10:13:52 +02:00
if ( $c -> fetch ( $add ) > 0 ) {
$c -> add_type ( $this , 'product' );
}
2015-09-30 18:46:51 +02:00
}
return ;
}
2015-04-18 18:11:01 +02:00
/**
* Function used to replace a thirdparty id with another one .
*
2015-06-05 14:46:30 +02:00
* @ param DoliDB $db Database handler
* @ param int $origin_id Old thirdparty id
* @ param int $dest_id New thirdparty id
2015-04-18 18:11:01 +02:00
* @ 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 );
}
2015-10-19 15:13:19 +02:00
/**
* Generates prices for a product based on product multiprice generation rules
*
* @ param User $user User that updates the prices
* @ param float $baseprice Base price
* @ param string $price_type Base price type
* @ param float $price_vat VAT % tax
* @ param int $npr NPR
* @ param string $psq ¿ ?
* @ return int - 1 KO , 1 OK
*/
public function generateMultiprices ( User $user , $baseprice , $price_type , $price_vat , $npr , $psq )
{
global $conf , $db ;
2016-02-03 01:46:30 +01:00
$sql = " SELECT rowid, level, fk_level, var_percent, var_min_percent FROM " . MAIN_DB_PREFIX . " product_pricerules " ;
2015-10-19 15:13:19 +02:00
$query = $db -> query ( $sql );
$rules = array ();
while ( $result = $db -> fetch_object ( $query )) {
$rules [ $result -> level ] = $result ;
}
//Because prices can be based on other level's prices, we temporarily store them
$prices = array (
1 => $baseprice
);
for ( $i = 1 ; $i <= $conf -> global -> PRODUIT_MULTIPRICES_LIMIT ; $i ++ ) {
$price = $baseprice ;
2016-03-08 12:15:49 +01:00
$price_min = $baseprice ;
2015-10-19 15:13:19 +02:00
2016-03-08 12:15:49 +01:00
//We have to make sure it does exist and it is > 0
//First price level only allows changing min_price
if ( $i > 1 && isset ( $rules [ $i ] -> var_percent ) && $rules [ $i ] -> var_percent ) {
$price = $prices [ $rules [ $i ] -> fk_level ] * ( 1 + ( $rules [ $i ] -> var_percent / 100 ));
2015-10-19 15:13:19 +02:00
}
$prices [ $i ] = $price ;
//We have to make sure it does exist and it is > 0
if ( isset ( $rules [ $i ] -> var_min_percent ) && $rules [ $i ] -> var_min_percent ) {
$price_min = $price * ( 1 - ( $rules [ $i ] -> var_min_percent / 100 ));
}
2016-03-30 11:38:39 +02:00
//Little check to make sure the price is modified before triggering generation
2016-03-30 14:04:22 +02:00
$check_amount = (( $price == $this -> multiprices [ $i ]) && ( $price_min == $this -> multiprices_min [ $i ]));
$check_type = ( $baseprice == $this -> multiprices_base_type [ $i ]);
2016-03-30 11:38:39 +02:00
if ( $check_amount && $check_type ) {
2015-10-19 15:13:19 +02:00
continue ;
}
if ( $this -> updatePrice ( $price , $price_type , $user , $price_vat , $price_min , $i , $npr , $psq , true ) < 0 ) {
return - 1 ;
}
}
return 1 ;
}
2015-11-07 12:19:48 +01:00
2015-11-08 14:54:29 +01:00
/**
* Returns the rights used for this class
* @ return stdClass
*/
2015-11-07 12:19:48 +01:00
public function getRights ()
{
global $user ;
2015-11-08 14:54:29 +01:00
if ( $this -> isProduct ()) {
2015-11-07 12:19:48 +01:00
return $user -> rights -> produit ;
} else {
return $user -> rights -> service ;
}
}
2017-06-09 11:02:34 +02:00
2015-11-19 14:26:32 +01:00
/**
* Load information for tab info
*
* @ param int $id Id of thirdparty to load
* @ return void
*/
function info ( $id )
{
2015-11-19 14:43:00 +01:00
$sql = " SELECT p.rowid, p.ref, p.datec as date_creation, p.tms as date_modification, " ;
2015-11-19 14:26:32 +01:00
$sql .= " p.fk_user_author, p.fk_user_modif " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as p " ;
$sql .= " WHERE p.rowid = " . $id ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
if ( $this -> db -> num_rows ( $result ))
{
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
if ( $obj -> fk_user_author ) {
$cuser = new User ( $this -> db );
$cuser -> fetch ( $obj -> fk_user_author );
$this -> user_creation = $cuser ;
}
if ( $obj -> fk_user_modif ) {
$muser = new User ( $this -> db );
$muser -> fetch ( $obj -> fk_user_modif );
$this -> user_modification = $muser ;
}
2015-11-19 14:43:00 +01:00
$this -> ref = $obj -> ref ;
2015-11-19 14:26:32 +01:00
$this -> date_creation = $this -> db -> jdate ( $obj -> date_creation );
$this -> date_modification = $this -> db -> jdate ( $obj -> date_modification );
}
$this -> db -> free ( $result );
}
else
{
dol_print_error ( $this -> db );
}
}
2017-06-09 12:50:56 +02:00
2002-04-30 17:52:56 +02:00
}