From 214bbdd9a63669be81a97633586aa352f6d118a3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 27 Jul 2008 20:47:13 +0000 Subject: [PATCH] Fix: Pb with supplier orders --- htdocs/admin/societe.php | 7 +- htdocs/fourn/commande/fiche.php | 134 +- htdocs/fourn/facture/fiche.php | 24 +- htdocs/fourn/fournisseur.commande.class.php | 14 +- htdocs/fourn/fournisseur.product.class.php | 30 +- htdocs/html.form.class.php | 21 +- htdocs/langs/en_US/suppliers.lang | 2 +- htdocs/langs/fr_BE/suppliers.lang | 2 +- htdocs/langs/fr_FR/suppliers.lang | 2 +- htdocs/product.class.php | 3684 +++++++++---------- 10 files changed, 1982 insertions(+), 1938 deletions(-) diff --git a/htdocs/admin/societe.php b/htdocs/admin/societe.php index 2b8416405e3..eca79f38dd3 100644 --- a/htdocs/admin/societe.php +++ b/htdocs/admin/societe.php @@ -86,8 +86,7 @@ if ($_POST["action"] == 'updateMask') /* - * Affichage page configuration module societe - * + * View */ $form=new Form($db); @@ -132,11 +131,11 @@ if ($handle) require_once(DOL_DOCUMENT_ROOT ."/includes/modules/societe/".$file.".php"); $modCodeTiers = new $file; - + // Show modules according to features level if ($modCodeTiers->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue; if ($modCodeTiers->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue; - + $var = !$var; print "\n ".$modCodeTiers->nom."\n "; print $modCodeTiers->info($langs); diff --git a/htdocs/fourn/commande/fiche.php b/htdocs/fourn/commande/fiche.php index 28873f461d3..c653a45d06b 100644 --- a/htdocs/fourn/commande/fiche.php +++ b/htdocs/fourn/commande/fiche.php @@ -99,62 +99,90 @@ if ($_POST['action'] == 'addligne' && $user->rights->fournisseur->commande->cree exit; } - $soc = new Societe($db, $commande->socid); - $result=$soc->fetch($commande->socid); - //print $result; - // Ecrase $pu par celui du produit // Ecrase $desc par celui du produit // Ecrase $txtva par celui du produit - if ($_POST["idprodfournprice"] > 0) + if ($_POST["idprodfournprice"]) // >0 or -1 { $prodfournprice = new ProductFournisseur($db); - $prodfournprice->fetch_product_fournisseur_price($_POST["idprodfournprice"]); + $idprod=$prodfournprice->get_buyprice($_POST['idprodfournprice'], $_POST['qty']); + if ($idprod > 0) + { + $prodfournprice->fetch($idprod); + + // cas special pour lequel on a les meme reference que le fournisseur + // $label = '['.$nv_prod->ref.'] - '. $nv_prod->libelle; + $label = $prodfournprice->libelle; + + $societe=''; + if ($commande->socid) + { + $societe=new Societe($db); + $societe->fetch($commande->socid); + } + + $desc = $prodfournprice->description; + $desc.= $prodfournprice->description && $_POST['np_desc'] ? "\n" : ""; + $desc.= $_POST['np_desc']; + + $tva_tx = get_default_tva($societe,$mysoc,$prodfournprice->tva_tx,$prodfournprice->id); - $prod = new Product($db, $prodfournprice->product_id); - $prod->fetch($prodfournprice->product_id); + $result=$commande->addline( + $desc, + $pu, + $_POST['qty'], + $tva_tx, + $prodfournprice->id, + $_POST['idprodfournprice'], + $prodfournprice->fourn_ref, + $_POST['remise_percent'], + 'HT' + ); - $libelle = $prod->libelle; - - $desc = $prod->description; - $desc.= $prod->description && $_POST['np_desc'] ? "\n" : ""; - $desc.= $_POST['np_desc']; - - $tva_tx = get_default_tva($soc,$mysoc,$prod->tva_tx,$prodfournprice->product_id); + } + if ($idprod == -1) + { + // Quantity too low + $mesg='
'.$langs->trans("ErrorQtyTooLowForThisSupplier").'
'; + } } else { - $pu=$_POST['pu']; - $tva_tx=$_POST['tva_tx']; - $desc=$_POST['dp_desc']; + $tauxtva = price2num($_POST['tva_tx']); + if (! $_POST['dp_desc']) + { + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label")).'
'; + } + else + { + if (!empty($_POST['pu'])) + { + $ht = price2num($_POST['pu']); + $result=$commande->addline($_POST['dp_desc'], $ht, $_POST['qty'], $tauxtva); + } + else + { + $ttc = price2num($_POST['amountttc']); + $ht = $ttc / (1 + ($tauxtva / 100)); + $result=$commande->addline($_POST['dp_desc'], $ht, $_POST['qty'], $tauxtva); + } + } } + //print "xx".$tva_tx; exit; - - $result=$commande->addline( - $desc, - $pu, - $_POST['qty'], - $tva_tx, - $prodfournprice->product_id, - $_POST['idprodfournprice'], - $prodfournprice->fourn_ref, - $_POST['remise_percent'], - 'HT' - ); - - if ($result > 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - supplier_order_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); - } - else - { - $mesg='
'.$commande->error.'
'; - } + if ($result > 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + supplier_order_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); + } + else + { + $mesg='
'.$commande->error.'
'; + } } } @@ -217,7 +245,7 @@ if ($_POST['action'] == 'confirm_deleteproductline' && $_POST['confirm'] == 'yes if ($_POST['action'] == 'confirm_valid' && $_POST['confirm'] == 'yes' && $user->rights->fournisseur->commande->valider) { $commande = new CommandeFournisseur($db); - + $commande->fetch($id); $commande->date_commande=time(); @@ -437,7 +465,7 @@ if ($id > 0) { //if ($mesg) print $mesg.'
'; $commande = new CommandeFournisseur($db); - + if ($commande->fetch($id) >= 0) { $soc = new Societe($db); @@ -499,10 +527,10 @@ if ($id > 0) $text=$langs->trans('ConfirmValidateOrder',$newref); if ($conf->notification->enabled) { - require_once(DOL_DOCUMENT_ROOT ."/notify.class.php"); - $notify=new Notify($db); - $text.='
'; - $text.=$notify->confirmMessage(3,$commande->socid); + require_once(DOL_DOCUMENT_ROOT ."/notify.class.php"); + $notify=new Notify($db); + $text.='
'; + $text.=$notify->confirmMessage(3,$commande->socid); } $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id, $langs->trans('ValidateOrder'), $text, 'confirm_valid'); @@ -665,7 +693,7 @@ if ($id > 0) if ($num) { print ''; - print ''.$langs->trans('Description').''; + print ''.$langs->trans('Label').''; print ''.$langs->trans('VAT').''; print ''.$langs->trans('PriceUHT').''; print ''.$langs->trans('Qty').''; @@ -800,7 +828,7 @@ if ($id > 0) print ''; print ''; print ''; // ancre - print $langs->trans('Description').''; + print $langs->trans('Label').''; print ''.$langs->trans('VAT').''; print ''.$langs->trans('PriceUHT').''; print ''.$langs->trans('Qty').''; @@ -868,7 +896,7 @@ if ($id > 0) $var=!$var; print ''; print ''; - $html->select_produits_fournisseurs($commande->fourn_id,'','idprodfournprice'); + $html->select_produits_fournisseurs($commande->fourn_id,'','idprodfournprice',2,$filtre); if (! $conf->global->PRODUIT_USE_SEARCH_TO_SELECT) print '
'; diff --git a/htdocs/fourn/facture/fiche.php b/htdocs/fourn/facture/fiche.php index 9cf64aa10fe..d4bb1b03faf 100644 --- a/htdocs/fourn/facture/fiche.php +++ b/htdocs/fourn/facture/fiche.php @@ -242,24 +242,30 @@ if ($_REQUEST['action'] == 'update_line') if ($_GET['action'] == 'add_ligne') { $facfou = new FactureFournisseur($db, '', $_GET['facid']); - - if ($_POST['prodfournpriceid']) + $ret=$facfou->fetch($_GET['facid']); + if ($ret < 0) { - $nv_prod = new Product($db); + dolibarr_print_error($db,$facfou->error); + exit; + } + + if ($_POST['prodfournpriceid']) // > 0 or -1 + { + $nv_prod = new ProductFournisseur($db); $idprod=$nv_prod->get_buyprice($_POST['prodfournpriceid'], $_POST['qty']); if ($idprod > 0) { $result=$nv_prod->fetch($idprod); - // cas sp�cial pour lequel on a les meme r�f�rence que le fournisseur + // cas special pour lequel on a les meme reference que le fournisseur // $label = '['.$nv_prod->ref.'] - '. $nv_prod->libelle; $label = $nv_prod->libelle; $societe=''; - if ($_POST['socid']) + if ($facfou->socid) { $societe=new Societe($db); - $societe->fetch($_POST['socid']); + $societe->fetch($facfou->socid); } $tvatx=get_default_tva($societe,$mysoc,$nv_prod->tva_tx); @@ -268,7 +274,7 @@ if ($_GET['action'] == 'add_ligne') } if ($idprod == -1) { - // Quantit� insuffisante + // Quantity too low $mesg='
'.$langs->trans("ErrorQtyTooLowForThisSupplier").'
'; } } @@ -854,7 +860,7 @@ else print ''; print ''; - // Ajout de produits/services pr�d�finis + // Ajout de produits/services predefinis if ($conf->produit->enabled) { print '
'; @@ -864,7 +870,7 @@ else $var=! $var; print ''; print ''; - $html->select_produits_fournisseurs($fac->socid,'','prodfournpriceid',$filtre); + $html->select_produits_fournisseurs($fac->socid,'','prodfournpriceid',2,$filtre); print ''; print ''; print ' '; diff --git a/htdocs/fourn/fournisseur.commande.class.php b/htdocs/fourn/fournisseur.commande.class.php index 06e2d01c648..9ba240097af 100644 --- a/htdocs/fourn/fournisseur.commande.class.php +++ b/htdocs/fourn/fournisseur.commande.class.php @@ -673,13 +673,13 @@ class CommandeFournisseur extends Commande * \param fk_product Id produit * \param remise_percent Remise * \param price_base_type HT or TTC - * \param int <0 si ko, >0 si ok + * \param int <=0 si ko, >0 si ok */ function addline($desc, $pu_ht, $qty, $txtva, $fk_product=0, $fk_prod_fourn_price=0, $fourn_ref='', $remise_percent=0, $price_base_type='HT', $pu_ttc=0) { global $langs,$mysoc; - dolibarr_syslog("Fournisseur.Commande::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent"); + dolibarr_syslog("FournisseurCommande::addline $desc, $pu_ht, $qty, $txtva, $fk_product, $fk_prod_fourn_price, $fourn_ref, $remise_percent, $price_base_type, $pu_ttc"); include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); // Clean parameters @@ -726,14 +726,14 @@ class CommandeFournisseur extends Commande { $this->error="Aucun tarif trouvé pour cette quantité. Quantité saisie insuffisante ?"; $this->db->rollback(); - dolibarr_syslog("Fournisseur.commande::addline result=".$result." - ".$this->error); + dolibarr_syslog("FournisseurCommande::addline result=".$result." - ".$this->error, LOG_DEBUG); return -1; } if ($result < -1) { $this->error=$prod->error; $this->db->rollback(); - dolibarr_syslog("Fournisseur.commande::addline result=".$result." - ".$this->error); + dolibarr_syslog("Fournisseur.commande::addline result=".$result." - ".$this->error, LOG_ERR); return -1; } } @@ -754,7 +754,7 @@ class CommandeFournisseur extends Commande $total_ttc = $tabprice[2]; $subprice = price2num($pu,'MU'); - + // \TODO A virer // Anciens indicateurs: $price, $remise (a ne plus utiliser) $remise = 0; @@ -777,7 +777,8 @@ class CommandeFournisseur extends Commande $sql.= "'".price2num($total_tva)."',"; $sql.= "'".price2num($total_ttc)."'"; $sql.= ")"; - dolibarr_syslog('Fournisseur.commande::addline sql='.$sql); + + dolibarr_syslog('FournisseurCommande::addline sql='.$sql); $resql=$this->db->query($sql); //print $sql; if ($resql) @@ -791,6 +792,7 @@ class CommandeFournisseur extends Commande { $this->error=$this->db->error(); $this->db->rollback(); + dolibarr_syslog('FournisseurCommande::addline '.$this->error, LOG_ERR); return -1; } } diff --git a/htdocs/fourn/fournisseur.product.class.php b/htdocs/fourn/fournisseur.product.class.php index 37b6ef9cfc0..d63a32f38bd 100644 --- a/htdocs/fourn/fournisseur.product.class.php +++ b/htdocs/fourn/fournisseur.product.class.php @@ -295,7 +295,7 @@ class ProductFournisseur extends Product /** * \brief Charge les informations relatives à un prix de fournisseur * \param rowid id ligne - * \return int < 0 si erreur, > 0 si ok + * \return int < 0 if KO, 0 if OK but not found, > 0 if OK */ function fetch_product_fournisseur_price($rowid) { @@ -304,24 +304,32 @@ class ProductFournisseur extends Product $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product_fournisseur as pf"; $sql.= " WHERE pfp.rowid = ".$rowid." AND pf.rowid = pfp.fk_product_fournisseur"; - dolibarr_syslog("Product::fetch_product_fournisseur_price sql=".$sql); + dolibarr_syslog("Product::fetch_product_fournisseur_price sql=".$sql, LOG_DEBUG); $resql = $this->db->query($sql) ; if ($resql) { $obj = $this->db->fetch_object($resql); - $this->product_fourn_price_id = $rowid; - $this->product_fourn_id = $obj->product_fourn_id; - $this->fourn_ref = $obj->ref_fourn; - $this->fourn_price = $obj->price; - $this->fourn_qty = $obj->quantity; - $this->fourn_unitprice = $obj->unitprice; - $this->product_id = $obj->fk_product; - return 1; + if ($obj) + { + $this->product_fourn_price_id = $rowid; + $this->product_fourn_id = $obj->product_fourn_id; + $this->fourn_ref = $obj->ref_fourn; + $this->fourn_price = $obj->price; + $this->fourn_qty = $obj->quantity; + $this->fourn_unitprice = $obj->unitprice; + $this->product_id = $obj->fk_product; // deprecated + $this->fk_product = $obj->fk_product; + return 1; + } + else + { + return 0; + } } else { $this->error=$this->db->error(); - dolibarr_syslog("Product::fetch_product_fournisseur_price error=".$this->error); + dolibarr_syslog("Product::fetch_product_fournisseur_price error=".$this->error, LOG_ERR); return -1; } } diff --git a/htdocs/html.form.class.php b/htdocs/html.form.class.php index 2a8c0e518c5..11c9ba45785 100644 --- a/htdocs/html.form.class.php +++ b/htdocs/html.form.class.php @@ -1127,13 +1127,14 @@ class Form } /** - \brief Retourne la liste des produits fournisseurs en Ajax si ajax activé ou renvoie à select_produits_fournisseurs_do - \param selected Produit pré-sélectionné - \param htmlname Nom de la zone select - \param filtretype Pour filtre sur type de produit - \param filtre Pour filtre sql + \brief Retourne la liste des produits fournisseurs en Ajax si ajax activé ou renvoie à select_produits_fournisseurs_do + \param socid Id third party + \param selected Produit pré-sélectionné + \param htmlname Nom de la zone select + \param filtretype Filter on product type (1=customer, 2=supplier) + \param filtre Pour filtre sql */ - function select_produits_fournisseurs($socid,$selected='',$htmlname='productid',$filtretype='',$filtre='') + function select_produits_fournisseurs($socid,$selected='',$htmlname='productid',$filtretype=2,$filtre) { global $langs,$conf; if ($conf->global->PRODUIT_USE_SEARCH_TO_SELECT) @@ -1143,7 +1144,7 @@ class Form } else { - $this->select_produits_fournisseurs_do($socid,$selected,$htmlname,$filtretype,$filtre); + $this->select_produits_fournisseurs_do($socid,$selected,$htmlname,$filtretype,$filtre,''); } } @@ -1226,7 +1227,7 @@ class Form { $opt.= strtolower($langs->trans("Units")); } - if ($objp->quantity > 1) + if ($objp->quantity >= 1) { $opt.=" ("; $opt.= price($objp->unitprice).$langs->trans("Currency".$conf->monnaie)."/".strtolower($langs->trans("Unit")); @@ -1255,8 +1256,8 @@ class Form } /** - \brief Retourne la liste des tarifs fournisseurs pour un produit - \param productid Id du produit + \brief Retourne la liste des tarifs fournisseurs pour un produit + \param productid Id product */ function select_product_fourn_price($productid,$htmlname='productfournpriceid') { diff --git a/htdocs/langs/en_US/suppliers.lang b/htdocs/langs/en_US/suppliers.lang index 3338db30a00..e3d112be5fd 100644 --- a/htdocs/langs/en_US/suppliers.lang +++ b/htdocs/langs/en_US/suppliers.lang @@ -13,7 +13,7 @@ OrderDate=Order date BuyingPrice=Buying price AddSupplierPrice=Add supplier price ChangeSupplierPrice=Change supplier price -ErrorQtyTooLowForThisSupplier=Quantity too low for this supplier +ErrorQtyTooLowForThisSupplier=Quantity too low for this supplier or no price defined on this product for this supplier ErrorSupplierCountryIsNotDefined=Country for this supplier is not defined. Correct this first. ProductHasAlreadyReferenceInThisSupplier=This product has already a reference in this supplier NoRecordedSuppliers=No suppliers recorded diff --git a/htdocs/langs/fr_BE/suppliers.lang b/htdocs/langs/fr_BE/suppliers.lang index 1532b092099..bfc7943cdae 100644 --- a/htdocs/langs/fr_BE/suppliers.lang +++ b/htdocs/langs/fr_BE/suppliers.lang @@ -13,6 +13,6 @@ OrderDate=Date commande BuyingPrice=Prix d'achat AddSupplierPrice=Ajouter prix fournisseur ChangeSupplierPrice=Modifier prix fournisseur -ErrorQtyTooLowForThisSupplier=Quantité insuffisante pour ce fournisseur +ErrorQtyTooLowForThisSupplier=Quantité insuffisante pour ce fournisseur ou aucun tarif défini sur ce produit pour ce fournisseur ErrorSupplierCountryIsNotDefined=Le pays de ce fournisseur n'est pas défini. Corriger sur sa fiche. ProductHasAlreadyReferenceInThisSupplier=Ce produit a déjà une référence chez ce fournisseur diff --git a/htdocs/langs/fr_FR/suppliers.lang b/htdocs/langs/fr_FR/suppliers.lang index 2e92269e4e8..dd42e5126d6 100644 --- a/htdocs/langs/fr_FR/suppliers.lang +++ b/htdocs/langs/fr_FR/suppliers.lang @@ -13,7 +13,7 @@ OrderDate=Date commande BuyingPrice=Prix d'achat AddSupplierPrice=Ajouter prix fournisseur ChangeSupplierPrice=Modifier prix fournisseur -ErrorQtyTooLowForThisSupplier=Quantité insuffisante pour ce fournisseur +ErrorQtyTooLowForThisSupplier=Quantité insuffisante pour ce fournisseur ou aucun tarif défini sur ce produit pour ce fournisseur ErrorSupplierCountryIsNotDefined=Le pays de ce fournisseur n'est pas défini. Corriger sur sa fiche. ProductHasAlreadyReferenceInThisSupplier=Ce produit a déjà une référence chez ce fournisseur NoRecordedSuppliers=Pas de fournisseurs enregistrés diff --git a/htdocs/product.class.php b/htdocs/product.class.php index 5ecbe07ffae..0312cb50ea9 100644 --- a/htdocs/product.class.php +++ b/htdocs/product.class.php @@ -21,138 +21,138 @@ */ /** - \file htdocs/product.class.php - \ingroup produit - \brief Fichier de la classe des produits pr�d�finis - \version $Id$ -*/ + \file htdocs/product.class.php + \ingroup produit + \brief Fichier de la classe des produits pr�d�finis + \version $Id$ + */ require_once(DOL_DOCUMENT_ROOT ."/commonobject.class.php"); /** - \class Product - \brief Classe permettant la gestion des produits pr�d�finis -*/ + \class Product + \brief Classe permettant la gestion des produits pr�d�finis + */ class Product extends CommonObject { - var $db; - var $error; - var $element='product'; - var $table_element='product'; + var $db; + var $error; + var $element='product'; + var $table_element='product'; //! Identifiant unique - var $id ; - //! Ref - var $ref; - var $libelle; - var $description; - //! Prix de vente - var $price; - var $price_ttc; - //! Base de prix (ttc ou ht) - var $price_base_type; - //! Tableau des prix multiples - var $multiprices=array(); - var $multiprices_ttc=array(); - var $multiprices_base_type=array(); - //! Taux de TVA - var $tva_tx; - //! Type 0 for regural product, 1 for service, 2 for assembly kit, 3 for stock kit - var $type; - var $typestring; - var $stock_reel; - var $seuil_stock_alerte; - //! Duree de validite du service - var $duration_value; - //! Unite de duree - var $duration_unit; - // Statut indique si le produit est en vente '1' ou non '0' - var $status; - - //! Unites de mesure - var $weight; - var $weight_units; - var $volume; - var $volume_units; + var $id ; + //! Ref + var $ref; + var $libelle; + var $description; + //! Prix de vente + var $price; + var $price_ttc; + //! Base de prix (ttc ou ht) + var $price_base_type; + //! Tableau des prix multiples + var $multiprices=array(); + var $multiprices_ttc=array(); + var $multiprices_base_type=array(); + //! Taux de TVA + var $tva_tx; + //! Type 0 for regural product, 1 for service, 2 for assembly kit, 3 for stock kit + var $type; + var $typestring; + var $stock_reel; + var $seuil_stock_alerte; + //! Duree de validite du service + var $duration_value; + //! Unite de duree + var $duration_unit; + // Statut indique si le produit est en vente '1' ou non '0' + var $status; - //! Codes barres - var $barcode; - var $barcode_type; - var $barcode_type_code; - var $barcode_type_label; - var $barcode_type_coder; - - var $stats_propale=array(); - var $stats_commande=array(); - var $stats_contrat=array(); - var $stats_facture=array(); - var $multilangs=array(); - - //! Taille de l'image - var $imgWidth; - var $imgHeight; + //! Unites de mesure + var $weight; + var $weight_units; + var $volume; + var $volume_units; - //! Numero de l'erreur - //! Numero d'erreur Plage 0256-0511 - var $errno = 0; - //! Canevas a utiliser si le produit n'est pas un produit generique - var $canvas; - //! Nombre de piece en commande, non expedie - var $stock_in_command; - - //! Id du fournisseur - var $product_fourn_id; - - /** - * \brief Constructeur de la classe - * \param DB Handler acc�s base de donn�es - * \param id Id produit (0 par defaut) - */ - function Product($DB, $id=0) - { - global $langs; - - $this->db = $DB; - $this->id = $id ; - $this->status = 0; - $this->stock_reel = 0; - $this->seuil_stock_alerte = 0; - - $this->canvas = ''; - if ($id>0) $this->fetch($id); - } - /** - * \brief V�rifie que la r�f�rence et libell� du produit est non null - * \return int 1 si ok, 0 sinon - */ - function check() - { - $this->ref = ereg_replace("'","",stripslashes($this->ref)); - $this->ref = ereg_replace("\"","",stripslashes($this->ref)); - - $err = 0; - if (strlen(trim($this->ref)) == 0) - $err++; - - if (strlen(trim($this->libelle)) == 0) - $err++; - - if ($err > 0) - { - return 0; - } - else - { - return 1; - } - } + //! Codes barres + var $barcode; + var $barcode_type; + var $barcode_type_code; + var $barcode_type_label; + var $barcode_type_coder; - /** - \brief Insert product in database - \param user Utilisateur qui effectue l'insertion - \return int id du produit ou numero d'erreur < 0 - */ + var $stats_propale=array(); + var $stats_commande=array(); + var $stats_contrat=array(); + var $stats_facture=array(); + var $multilangs=array(); + + //! Taille de l'image + var $imgWidth; + var $imgHeight; + + //! Numero de l'erreur + //! Numero d'erreur Plage 0256-0511 + var $errno = 0; + //! Canevas a utiliser si le produit n'est pas un produit generique + var $canvas; + //! Nombre de piece en commande, non expedie + var $stock_in_command; + + //! Id du fournisseur + var $product_fourn_id; + + /** + * \brief Constructeur de la classe + * \param DB Handler acc�s base de donn�es + * \param id Id produit (0 par defaut) + */ + function Product($DB, $id=0) + { + global $langs; + + $this->db = $DB; + $this->id = $id ; + $this->status = 0; + $this->stock_reel = 0; + $this->seuil_stock_alerte = 0; + + $this->canvas = ''; + if ($id>0) $this->fetch($id); + } + /** + * \brief V�rifie que la r�f�rence et libell� du produit est non null + * \return int 1 si ok, 0 sinon + */ + function check() + { + $this->ref = ereg_replace("'","",stripslashes($this->ref)); + $this->ref = ereg_replace("\"","",stripslashes($this->ref)); + + $err = 0; + if (strlen(trim($this->ref)) == 0) + $err++; + + if (strlen(trim($this->libelle)) == 0) + $err++; + + if ($err > 0) + { + return 0; + } + else + { + return 1; + } + } + + /** + \brief Insert product in database + \param user Utilisateur qui effectue l'insertion + \return int id du produit ou numero d'erreur < 0 + */ function create($user) { $this->errno = 0; @@ -178,23 +178,23 @@ class Product extends CommonObject $price_ttc = price2num($this->price * (1 + ($this->tva_tx / 100)),'MU'); } } - + // Check parameters - if (empty($this->libelle)) + if (empty($this->libelle)) { $this->error='ErrorWrongParameters'; return -1; } - + dolibarr_syslog("Product::Create ref=".$this->ref." price=".$this->price." tva_tx=".$this->tva_tx." Categorie : ".$this->catid); - + if ($this->ref) { $this->db->begin(); - + $sql = "SELECT count(*)"; $sql .= " FROM ".MAIN_DB_PREFIX."product WHERE ref = '" .$this->ref."'"; - + $result = $this->db->query($sql) ; if ($result) { @@ -216,13 +216,13 @@ class Product extends CommonObject $sql.= price2num($price_ttc).","; $sql.= "'".$this->price_base_type."',"; $sql.= "'".$this->canvas."')"; - + dolibarr_syslog("Product::Create sql=".$sql); $result = $this->db->query($sql); if ( $result ) { $id = $this->db->last_insert_id(MAIN_DB_PREFIX."product"); - + if ($id > 0) { $this->id = $id; @@ -236,7 +236,7 @@ class Product extends CommonObject { if ($this->catid > 0) { - require_once(DOL_DOCUMENT_ROOT ."/categories/categorie.class.php"); + require_once(DOL_DOCUMENT_ROOT ."/categories/categorie.class.php"); $cat = new Categorie ($this->db, $this->catid); $cat->add_type($this,"product"); } @@ -274,9 +274,9 @@ class Product extends CommonObject $this->_setErrNo("Create",263); } - /* - * END COMMIT - */ + /* + * END COMMIT + */ if ($this->errno === 0) { @@ -288,7 +288,7 @@ class Product extends CommonObject $this->db->rollback(); $this->_setErrNo("Create",265); return -1; - } + } } else { @@ -296,40 +296,40 @@ class Product extends CommonObject return -2; } - + return -1; } - - /** - \brief Positionne le numero d'erreur - \param func Nom de la fonction - \param num Numero de l'erreur - \param error string - */ - function _setErrNo($func, $num, $error='') - { - $this->errno = $num; - dolibarr_syslog(get_class($this)."::".$func." - ERRNO(".$this->errno.")".($error?' - '.$error:''), LOG_ERR); - } - /** - \brief Retourne le texte de l'erreur - */ - function error() - { - $errs[257] = "ErrorProductAlreadyExists"; - $errs[262] = "ErrorProductBadRefOrLabel"; + /** + \brief Positionne le numero d'erreur + \param func Nom de la fonction + \param num Numero de l'erreur + \param error string + */ + function _setErrNo($func, $num, $error='') + { + $this->errno = $num; + dolibarr_syslog(get_class($this)."::".$func." - ERRNO(".$this->errno.")".($error?' - '.$error:''), LOG_ERR); + } + + /** + \brief Retourne le texte de l'erreur + */ + function error() + { + $errs[257] = "ErrorProductAlreadyExists"; + $errs[262] = "ErrorProductBadRefOrLabel"; + + return $errs[$this->errno]; + } - return $errs[$this->errno]; - } - /** \brief Mise � jour du produit en base \param id id du produit \param user utilisateur qui effectue l'insertion \return int 1 si ok, -1 si ref deja existante, -2 autre erreur - */ + */ function update($id, $user) { global $langs, $conf; @@ -392,81 +392,81 @@ class Product extends CommonObject } } - /** - * \brief V�rification de l'utilisation du produit en base - * \param id id du produit - */ - function verif_prod_use($id) - { - $sqr = 0; + /** + * \brief V�rification de l'utilisation du produit en base + * \param id id du produit + */ + function verif_prod_use($id) + { + $sqr = 0; - $sql = "SELECT rowid"; - $sql.= " FROM ".MAIN_DB_PREFIX."propaldet"; - $sql.= " WHERE fk_product = ".$id; + $sql = "SELECT rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."propaldet"; + $sql.= " WHERE fk_product = ".$id; - $result = $this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows($result); - if ($num != 0) + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + if ($num != 0) { - $sqr++; + $sqr++; } - } + } - $sql = "SELECT rowid"; - $sql.= " FROM ".MAIN_DB_PREFIX."facturedet"; - $sql.= " WHERE fk_product = ".$id; + $sql = "SELECT rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."facturedet"; + $sql.= " WHERE fk_product = ".$id; - $result = $this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows($result); - if ($num != 0) + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + if ($num != 0) { - $sqr++; + $sqr++; } - } + } - $sql = "SELECT rowid"; - $sql.= " FROM ".MAIN_DB_PREFIX."commandedet"; - $sql.= " WHERE fk_product = ".$id; + $sql = "SELECT rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."commandedet"; + $sql.= " WHERE fk_product = ".$id; - $result = $this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows($result); - if ($num != 0) + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + if ($num != 0) { - $sqr++; + $sqr++; } - } + } - $sql = "SELECT rowid"; - $sql.= " FROM ".MAIN_DB_PREFIX."contratdet"; - $sql.= " WHERE fk_product = ".$id; + $sql = "SELECT rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."contratdet"; + $sql.= " WHERE fk_product = ".$id; - $result = $this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows($result); - if ($num != 0) + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + if ($num != 0) { - $sqr++; + $sqr++; } - } + } - if ($sqr == 0) - { - return 0; - } - else - { - return -1; - } - } + if ($sqr == 0) + { + return 0; + } + else + { + return -1; + } + } /** @@ -477,7 +477,7 @@ class Product extends CommonObject function delete($id) { global $conf,$user; - + if ($user->rights->produit->supprimer) { $prod_use = $this->verif_prod_use($id); @@ -486,19 +486,19 @@ class Product extends CommonObject $sqla = "DELETE from ".MAIN_DB_PREFIX."product"; $sqla.= " WHERE rowid = ".$id; $resulta = $this->db->query($sqla); - + $sqlb = "DELETE from ".MAIN_DB_PREFIX."product_price"; $sqlb.= " WHERE fk_product = ".$id; $resultb = $this->db->query($sqlb); - + $sqlc = "DELETE from ".MAIN_DB_PREFIX."product_det"; $sqlc.= " WHERE fk_product = ".$id; $resultc = $this->db->query($sqlc); - + $sqld = "DELETE from ".MAIN_DB_PREFIX."categorie_product"; $sqld.= " WHERE fk_product = ".$id; $resultd = $this->db->query($sqld); - + return 0; } else @@ -509,118 +509,118 @@ class Product extends CommonObject } } - /** - * \brief update ou cr�e les traductions des infos produits - */ - function setMultiLangs() - { - global $langs; - $langs_available = $langs->get_available_languages(); - $current_lang = $langs->getDefaultLang(); + /** + * \brief update ou cr�e les traductions des infos produits + */ + function setMultiLangs() + { + global $langs; + $langs_available = $langs->get_available_languages(); + $current_lang = $langs->getDefaultLang(); - foreach ($langs_available as $value) - { - $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product_det"; - $sql.= " WHERE fk_product=".$this->id." AND lang='".$value."'"; + foreach ($langs_available as $value) + { + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product_det"; + $sql.= " WHERE fk_product=".$this->id." AND lang='".$value."'"; - $result = $this->db->query($sql); + $result = $this->db->query($sql); if ($value == $current_lang) { - if ($this->db->num_rows($result)) // si aucune ligne dans la base - { - $sql2 = "UPDATE ".MAIN_DB_PREFIX."product_det"; - $sql2.= " SET label='".addslashes($this->libelle)."',"; - $sql2.= " description='".addslashes($this->description)."',"; - $sql2.= " note='".addslashes($this->note)."'"; - $sql2.= " WHERE fk_product=".$this->id." AND lang='".$value."'"; - } - else - { - $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."product_det (fk_product, lang, label, description, note)"; - $sql2.= " VALUES(".$this->id.",'".$value."','". addslashes($this->libelle); - $sql2.= "','".addslashes($this->description); - $sql2.= "','".addslashes($this->note)."')"; - } - if (!$this->db->query($sql2)) return -1; + if ($this->db->num_rows($result)) // si aucune ligne dans la base + { + $sql2 = "UPDATE ".MAIN_DB_PREFIX."product_det"; + $sql2.= " SET label='".addslashes($this->libelle)."',"; + $sql2.= " description='".addslashes($this->description)."',"; + $sql2.= " note='".addslashes($this->note)."'"; + $sql2.= " WHERE fk_product=".$this->id." AND lang='".$value."'"; + } + else + { + $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."product_det (fk_product, lang, label, description, note)"; + $sql2.= " VALUES(".$this->id.",'".$value."','". addslashes($this->libelle); + $sql2.= "','".addslashes($this->description); + $sql2.= "','".addslashes($this->note)."')"; + } + if (!$this->db->query($sql2)) return -1; } else { - if ($this->db->num_rows($result)) // si aucune ligne dans la base - { - $sql2 = "UPDATE ".MAIN_DB_PREFIX."product_det"; - $sql2.= " SET label='".addslashes($this->multilangs["$value"]["libelle"])."',"; - $sql2.= " description='".addslashes($this->multilangs["$value"]["description"])."',"; - $sql2.= " note='".addslashes($this->multilangs["$value"]["note"])."'"; - $sql2.= " WHERE fk_product=".$this->id." AND lang='".$value."'"; - } - else - { - $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."product_det (fk_product, lang, label, description, note)"; - $sql2.= " VALUES(".$this->id.",'".$value."','". addslashes($this->multilangs["$value"]["libelle"]); - $sql2.= "','".addslashes($this->multilangs["$value"]["description"]); - $sql2.= "','".addslashes($this->multilangs["$value"]["note"])."')"; - } + if ($this->db->num_rows($result)) // si aucune ligne dans la base + { + $sql2 = "UPDATE ".MAIN_DB_PREFIX."product_det"; + $sql2.= " SET label='".addslashes($this->multilangs["$value"]["libelle"])."',"; + $sql2.= " description='".addslashes($this->multilangs["$value"]["description"])."',"; + $sql2.= " note='".addslashes($this->multilangs["$value"]["note"])."'"; + $sql2.= " WHERE fk_product=".$this->id." AND lang='".$value."'"; + } + else + { + $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."product_det (fk_product, lang, label, description, note)"; + $sql2.= " VALUES(".$this->id.",'".$value."','". addslashes($this->multilangs["$value"]["libelle"]); + $sql2.= "','".addslashes($this->multilangs["$value"]["description"]); + $sql2.= "','".addslashes($this->multilangs["$value"]["note"])."')"; + } - // on ne sauvegarde pas des champs vides - if ( $this->multilangs["$value"]["libelle"] || $this->multilangs["$value"]["description"] || $this->multilangs["$value"]["note"] ) - if (!$this->db->query($sql2)) return -1; + // on ne sauvegarde pas des champs vides + if ( $this->multilangs["$value"]["libelle"] || $this->multilangs["$value"]["description"] || $this->multilangs["$value"]["note"] ) + if (!$this->db->query($sql2)) return -1; } - } - return 1; - } + } + return 1; + } - /** - * \ brief Charge toutes les traductions du produit - */ - function getMultiLangs($langue='') - { - global $langs; - $langs_available = $langs->get_available_languages(); + /** + * \ brief Charge toutes les traductions du produit + */ + function getMultiLangs($langue='') + { + global $langs; + $langs_available = $langs->get_available_languages(); - if ( $langue != '') - foreach ($langs_available as $value) - if ( $value == $langue ) $current_lang = $value; // si $langue est une valeur correcte + if ( $langue != '') + foreach ($langs_available as $value) + if ( $value == $langue ) $current_lang = $value; // si $langue est une valeur correcte - if ( !$current_lang ) - $current_lang = $langs->getDefaultLang(); // sinon on choisi la langue par defaut + if ( !$current_lang ) + $current_lang = $langs->getDefaultLang(); // sinon on choisi la langue par defaut - $sql = "SELECT lang, label, description, note"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_det"; - $sql.= " WHERE fk_product=".$this->id; + $sql = "SELECT lang, label, description, note"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_det"; + $sql.= " WHERE fk_product=".$this->id; - $result = $this->db->query($sql); - if ($result) - { - while ( $obj = $this->db->fetch_object($result) ) + $result = $this->db->query($sql); + if ($result) + { + while ( $obj = $this->db->fetch_object($result) ) { - if( $obj->lang == $current_lang ) // si on a les traduct. dans la langue courant on les charge en infos principales. - { - $this->libelle = $obj->label; - $this->description = $obj->description; - $this->note = $obj->note; - } - $this->multilangs["$obj->lang"]["libelle"] = $obj->label; - $this->multilangs["$obj->lang"]["description"] = $obj->description; - $this->multilangs["$obj->lang"]["note"] = $obj->note; + if( $obj->lang == $current_lang ) // si on a les traduct. dans la langue courant on les charge en infos principales. + { + $this->libelle = $obj->label; + $this->description = $obj->description; + $this->note = $obj->note; + } + $this->multilangs["$obj->lang"]["libelle"] = $obj->label; + $this->multilangs["$obj->lang"]["description"] = $obj->description; + $this->multilangs["$obj->lang"]["note"] = $obj->note; } - } - else - { - $this->error=$langs->trans("Error")." : ".$this->db->error()." - ".$sql; - return -1; - } - } + } + else + { + $this->error=$langs->trans("Error")." : ".$this->db->error()." - ".$sql; + return -1; + } + } /** - * \brief Ajoute un changement de prix en base dans l'historique des prix - * \param user Objet utilisateur qui modifie le prix - * \return int <0 si KO, >0 si OK - */ + * \brief Ajoute un changement de prix en base dans l'historique des prix + * \param user Objet utilisateur qui modifie le prix + * \return int <0 si KO, >0 si OK + */ function _log_price($user) { // MultiPrix : si activ�, on g�re tout ici, m�me le prix standard @@ -695,12 +695,12 @@ class Product extends CommonObject /** - * \brief Lit le prix pratiqu� par un fournisseur - * On renseigne le couple prodfournprice/qty ou le triplet qty/product_id/fourn_ref) - * \param prodfournprice Id du tarif = rowid table product_fournisseur_price - * \param qty Quantit� du produit - * \return int <0 si ko, 0 si ok mais rien trouv�, id_product si ok et trouv� - */ + * \brief Lit le prix pratiqu� par un fournisseur + * On renseigne le couple prodfournprice/qty ou le triplet qty/product_id/fourn_ref) + * \param prodfournprice Id du tarif = rowid table product_fournisseur_price + * \param qty Quantit� du produit + * \return int <0 si ko, 0 si ok mais rien trouv�, id_product si ok et trouv� + */ function get_buyprice($prodfournprice,$qty,$product_id=0,$fourn_ref=0) { $result = 0; @@ -710,7 +710,7 @@ class Product extends CommonObject $sql.= " WHERE pf.rowid = pfp.fk_product_fournisseur"; $sql.= " AND pfp.rowid = ".$prodfournprice; $sql.= " AND pfp.quantity <= ".$qty; - + dolibarr_syslog("Product::get_buyprice sql=".$sql); $resql = $this->db->query($sql); if ($resql) @@ -733,8 +733,8 @@ class Product extends CommonObject $sql.= " WHERE pf.rowid = pfp.fk_product_fournisseur"; $sql.= " AND pf.ref_fourn = '".$fourn_ref."'"; $sql.= " AND pf.fk_product = ".$product_id; - $sql.= " AND quantity <= ".$qty; - $sql.= " ORDER BY pfp.quantity DESC"; + $sql.= " AND quantity <= ".$qty; + $sql.= " ORDER BY pfp.quantity DESC"; $sql.= " LIMIT 1"; dolibarr_syslog("Product::get_buyprice sql=".$sql); @@ -772,14 +772,14 @@ class Product extends CommonObject /** - * \brief Modifie le prix d'un produit/service - * \param id Id du produit/service a modifier - * \param newprice Nouveau prix - * \param newpricebase HT ou TTC - * \param user Objet utilisateur qui modifie le prix - * \param newvat New VAT Rate - * \return int <0 if KO, >0 if OK - */ + * \brief Modifie le prix d'un produit/service + * \param id Id du produit/service a modifier + * \param newprice Nouveau prix + * \param newpricebase HT ou TTC + * \param user Objet utilisateur qui modifie le prix + * \param newvat New VAT Rate + * \return int <0 if KO, >0 if OK + */ function update_price($id, $newprice, $newpricebase, $user, $newvat='') { //multiprix @@ -787,7 +787,7 @@ class Product extends CommonObject dolibarr_syslog("Product::update_price id=".$id." newprice=".$newprice." newpricebase=".$newpricebase); if ($newvat == '') $newvat=$this->tva_tx; - + if ($newprice) { if ($newpricebase == 'TTC') @@ -802,7 +802,7 @@ class Product extends CommonObject $price_ttc = price2num($newprice) * (1 + ($newvat / 100)); $price_ttc = price2num($price_ttc,'MU'); } - + // Ne pas mettre de quote sur le num�riques decimaux. // Ceci provoque des sotckage avec arrondis en base au lieu des valeurs exactes. $sql = "UPDATE ".MAIN_DB_PREFIX."product SET"; @@ -816,11 +816,11 @@ class Product extends CommonObject $resql=$this->db->query($sql); if ($resql) { - $this->price = $price; - $this->price_ttc = $price_ttc; + $this->price = $price; + $this->price_ttc = $price_ttc; $this->price_base_type = $newpricebase; $this->tva_tx = $newvat; - + $this->_log_price($user); return 1; } @@ -843,139 +843,139 @@ class Product extends CommonObject } - /** - * \brief Charge le produit/service en m�moire - * \param id Id du produit/service � charger - * \param ref Ref du produit/service � charger - * \return int <0 si ko, >0 si ok - */ - function fetch($id='',$ref='') - { - global $langs; - global $conf; - - dolibarr_syslog("Product::fetch id=$id ref=$ref"); - - // Verification parametres - if (! $id && ! $ref) - { - $this->error=$langs->trans('ErrorWrongParameters'); - dolibarr_print_error("Product::fetch ".$this->error); - return -1; - } - - $sql = "SELECT rowid, ref, label, description, note, price, price_ttc, price_base_type, tva_tx, envente,"; - $sql.= " nbvente, fk_product_type, duration, seuil_stock_alerte,canvas,"; - $sql.= " stock_commande, stock_loc, weight, weight_units, volume, volume_units, barcode, fk_barcode_type"; - $sql.= " FROM ".MAIN_DB_PREFIX."product"; - if ($id) $sql.= " WHERE rowid = '".$id."'"; - if ($ref) $sql.= " WHERE ref = '".addslashes($ref)."'"; - - dolibarr_syslog("Product::fetch sql=".$sql); - $result = $this->db->query($sql); - if ( $result ) - { - $result = $this->db->fetch_array(); - - $this->id = $result["rowid"]; - $this->ref = $result["ref"]; - $this->libelle = $result["label"]; - $this->description = $result["description"]; - $this->note = $result["note"]; - $this->price = $result["price"]; - $this->price_ttc = $result["price_ttc"]; - $this->price_base_type = $result["price_base_type"]; - $this->tva_tx = $result["tva_tx"]; - $this->type = $result["fk_product_type"]; - $this->nbvente = $result["nbvente"]; - $this->status = $result["envente"]; - $this->duration = $result["duration"]; - $this->duration_value = substr($result["duration"],0,strlen($result["duration"])-1); - $this->duration_unit = substr($result["duration"],-1); - $this->seuil_stock_alerte = $result["seuil_stock_alerte"]; - $this->canvas = $result["canvas"]; - $this->stock_loc = $result["stock_loc"]; - $this->weight = $result["weight"]; - $this->weight_units = $result["weight_units"]; - $this->volume = $result["volume"]; - $this->volume_units = $result["volume_units"]; - $this->barcode = $result["barcode"]; - $this->barcode_type = $result["fk_barcode_type"]; - - $this->stock_in_command = $result["stock_commande"]; - - $this->label_url = ''.$this->libelle.''; - - $this->db->free(); - // multilangs - if ($conf->global->MAIN_MULTILANGS) $this->getMultiLangs(); - - // Barcode - if ($conf->barcode->enabled && $conf->global->PRODUIT_USE_BARCODE) + /** + * \brief Charge le produit/service en m�moire + * \param id Id du produit/service � charger + * \param ref Ref du produit/service � charger + * \return int <0 si ko, >0 si ok + */ + function fetch($id='',$ref='') { - if ($this->barcode_type == 0) - { - $this->barcode_type = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE; - } - - if ($this->barcode_type > 0) - { - $sql = "SELECT code, libelle, coder"; - $sql.= " FROM ".MAIN_DB_PREFIX."c_barcode_type"; - $sql.= " WHERE rowid = ".$this->barcode_type; - $result = $this->db->query($sql); - if ($result) - { - $result = $this->db->fetch_array(); - $this->barcode_type_code = $result["code"]; - $this->barcode_type_label = $result["libelle"]; - $this->barcode_type_coder = $result["coder"]; - } - else - { - dolibarr_print_error($this->db); - return -1; - } - } - } + global $langs; + global $conf; - // multiprix - if ($conf->global->PRODUIT_MULTIPRICES) - { - if ($ref) - { - $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product "; - $sql.= "WHERE ref = '".addslashes($ref)."'"; - $result = $this->db->query($sql) ; - if ($result) - { + dolibarr_syslog("Product::fetch id=$id ref=$ref"); + + // Verification parametres + if (! $id && ! $ref) + { + $this->error=$langs->trans('ErrorWrongParameters'); + dolibarr_print_error("Product::fetch ".$this->error); + return -1; + } + + $sql = "SELECT rowid, ref, label, description, note, price, price_ttc, price_base_type, tva_tx, envente,"; + $sql.= " nbvente, fk_product_type, duration, seuil_stock_alerte,canvas,"; + $sql.= " stock_commande, stock_loc, weight, weight_units, volume, volume_units, barcode, fk_barcode_type"; + $sql.= " FROM ".MAIN_DB_PREFIX."product"; + if ($id) $sql.= " WHERE rowid = '".$id."'"; + if ($ref) $sql.= " WHERE ref = '".addslashes($ref)."'"; + + dolibarr_syslog("Product::fetch sql=".$sql); + $result = $this->db->query($sql); + if ( $result ) + { + $result = $this->db->fetch_array(); + + $this->id = $result["rowid"]; + $this->ref = $result["ref"]; + $this->libelle = $result["label"]; + $this->description = $result["description"]; + $this->note = $result["note"]; + $this->price = $result["price"]; + $this->price_ttc = $result["price_ttc"]; + $this->price_base_type = $result["price_base_type"]; + $this->tva_tx = $result["tva_tx"]; + $this->type = $result["fk_product_type"]; + $this->nbvente = $result["nbvente"]; + $this->status = $result["envente"]; + $this->duration = $result["duration"]; + $this->duration_value = substr($result["duration"],0,strlen($result["duration"])-1); + $this->duration_unit = substr($result["duration"],-1); + $this->seuil_stock_alerte = $result["seuil_stock_alerte"]; + $this->canvas = $result["canvas"]; + $this->stock_loc = $result["stock_loc"]; + $this->weight = $result["weight"]; + $this->weight_units = $result["weight_units"]; + $this->volume = $result["volume"]; + $this->volume_units = $result["volume_units"]; + $this->barcode = $result["barcode"]; + $this->barcode_type = $result["fk_barcode_type"]; + + $this->stock_in_command = $result["stock_commande"]; + + $this->label_url = ''.$this->libelle.''; + + $this->db->free(); + // multilangs + if ($conf->global->MAIN_MULTILANGS) $this->getMultiLangs(); + + // Barcode + if ($conf->barcode->enabled && $conf->global->PRODUIT_USE_BARCODE) + { + if ($this->barcode_type == 0) + { + $this->barcode_type = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE; + } + + if ($this->barcode_type > 0) + { + $sql = "SELECT code, libelle, coder"; + $sql.= " FROM ".MAIN_DB_PREFIX."c_barcode_type"; + $sql.= " WHERE rowid = ".$this->barcode_type; + $result = $this->db->query($sql); + if ($result) + { + $result = $this->db->fetch_array(); + $this->barcode_type_code = $result["code"]; + $this->barcode_type_label = $result["libelle"]; + $this->barcode_type_coder = $result["coder"]; + } + else + { + dolibarr_print_error($this->db); + return -1; + } + } + } + + // multiprix + if ($conf->global->PRODUIT_MULTIPRICES) + { + if ($ref) + { + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product "; + $sql.= "WHERE ref = '".addslashes($ref)."'"; + $result = $this->db->query($sql) ; + if ($result) + { $result = $this->db->fetch_array(); $prodid = $result["rowid"]; - } - else - { + } + else + { dolibarr_print_error($this->db); return -1; - } - } - - $this->multiprices[1] = $this->price; - $this->multiprices_ttc[1] = $this->price_ttc; - $this->multiprices_base_type[1] = $this->price_base_type; - - for($i=2;$i<=$conf->global->PRODUIT_MULTIPRICES_LIMIT;$i++) - { - $sql= "SELECT price, price_ttc, price_base_type, tva_tx, envente "; - $sql.= "FROM ".MAIN_DB_PREFIX."product_price "; - $sql.= "where price_level=".$i." and "; - if ($id) $sql.= "fk_product = '".$id."' "; - if ($ref) $sql.= "fk_product = '".$prodid."' "; - $sql.= "order by date_price DESC limit 1"; - $result = $this->db->query($sql) ; - if ( $result ) - { + } + } + + $this->multiprices[1] = $this->price; + $this->multiprices_ttc[1] = $this->price_ttc; + $this->multiprices_base_type[1] = $this->price_base_type; + + for($i=2;$i<=$conf->global->PRODUIT_MULTIPRICES_LIMIT;$i++) + { + $sql= "SELECT price, price_ttc, price_base_type, tva_tx, envente "; + $sql.= "FROM ".MAIN_DB_PREFIX."product_price "; + $sql.= "where price_level=".$i." and "; + if ($id) $sql.= "fk_product = '".$id."' "; + if ($ref) $sql.= "fk_product = '".$prodid."' "; + $sql.= "order by date_price DESC limit 1"; + $result = $this->db->query($sql) ; + if ( $result ) + { $result = $this->db->fetch_array(); - + if($result["price"] != "" && $result["price"] != "0.00") { $this->multiprices[$i]=$result["price"]; @@ -988,1029 +988,1029 @@ class Product extends CommonObject $this->multiprices_ttc[$i]=$this->price_ttc; $this->multiprices_base_type[$i] = $this->price_base_type; } - } - else - { + } + else + { dolibarr_print_error($this->db); return -1; - } - } - + } + } + } - $res=$this->load_stock(); - - return $res; - } - else - { - dolibarr_print_error($this->db); - return -1; - } - } - + $res=$this->load_stock(); - /** - * \brief Charge tableau des stats propale pour le produit/service - * \param socid Id societe - * \return array Tableau des stats - */ - function load_stats_propale($socid=0) - { - global $conf; - global $user; - - $sql = "SELECT COUNT(DISTINCT pr.fk_soc) as nb_customers, COUNT(DISTINCT pr.rowid) as nb,"; - $sql.= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty"; - $sql.= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."propal as pr"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql.= " WHERE pr.rowid = pd.fk_propal AND pd.fk_product = ".$this->id; - if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND pr.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; - //$sql.= " AND pr.fk_statut != 0"; - if ($socid > 0) - { - $sql .= " AND pr.fk_soc = $socid"; - } - - $result = $this->db->query($sql) ; - 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; - } - } - - - /** - * \brief Charge tableau des stats commande client pour le produit/service - * \param socid Id societe pour filtrer sur une soci�t� - * \param filtrestatut Id statut pour filtrer sur un statut - * \return array Tableau des stats - */ - function load_stats_commande($socid=0,$filtrestatut='') - { - global $conf,$user; - - $sql = "SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,"; - $sql.= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty"; - $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd,"; - $sql.= " ".MAIN_DB_PREFIX."commande as c"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql.= " WHERE c.rowid = cd.fk_commande AND cd.fk_product = ".$this->id; - if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; - if ($socid > 0) - { - $sql.= " AND c.fk_soc = ".$socid; - } + return $res; + } + else + { + dolibarr_print_error($this->db); + return -1; + } + } - if ($filtrestatut <> '') - { - $sql.= " AND c.fk_statut in (".$filtrestatut.")"; - } - $result = $this->db->query($sql) ; - if ( $result ) - { - $obj=$this->db->fetch_object($result); - $this->stats_commande['customers']=$obj->nb_customers; - $this->stats_commande['nb']=$obj->nb; - $this->stats_commande['rows']=$obj->nb_rows; - $this->stats_commande['qty']=$obj->qty?$obj->qty:0; - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - - /** - * \brief Charge tableau des stats commande fournisseur pour le produit/service - * \param socid Id societe pour filtrer sur une soci�t� - * \param filtrestatut Id des statuts pour filtrer sur des statuts - * \return array Tableau des stats - */ - function load_stats_commande_fournisseur($socid=0,$filtrestatut='') - { - global $conf,$user; + /** + * \brief Charge tableau des stats propale pour le produit/service + * \param socid Id societe + * \return array Tableau des stats + */ + function load_stats_propale($socid=0) + { + global $conf; + global $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"; - $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd,"; - $sql.= " ".MAIN_DB_PREFIX."commande_fournisseur as c"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql.= " WHERE c.rowid = cd.fk_commande AND cd.fk_product = ".$this->id; - if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; - if ($socid > 0) - { - $sql.= " AND c.fk_soc = ".$socid; - } - if ($filtrestatut != '') // Peut valoir 0 - { - $sql.= " AND c.fk_statut in (".$filtrestatut.")"; - } + $sql = "SELECT COUNT(DISTINCT pr.fk_soc) as nb_customers, COUNT(DISTINCT pr.rowid) as nb,"; + $sql.= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty"; + $sql.= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."propal as pr"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql.= " WHERE pr.rowid = pd.fk_propal AND pd.fk_product = ".$this->id; + if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND pr.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; + //$sql.= " AND pr.fk_statut != 0"; + if ($socid > 0) + { + $sql .= " AND pr.fk_soc = $socid"; + } - $result = $this->db->query($sql) ; - 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; - } - } + $result = $this->db->query($sql) ; + 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; + } + } - /** - * \brief Charge tableau des stats contrat pour le produit/service - * \param socid Id societe - * \return array Tableau des stats - */ - 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"; - $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as cd,"; - $sql.= " ".MAIN_DB_PREFIX."contrat as c"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql.= " WHERE c.rowid = cd.fk_contrat 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; - //$sql.= " AND c.statut != 0"; - if ($socid > 0) - { - $sql .= " AND c.fk_soc = $socid"; - } + /** + * \brief Charge tableau des stats commande client pour le produit/service + * \param socid Id societe pour filtrer sur une soci�t� + * \param filtrestatut Id statut pour filtrer sur un statut + * \return array Tableau des stats + */ + function load_stats_commande($socid=0,$filtrestatut='') + { + global $conf,$user; - $result = $this->db->query($sql) ; - 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; - } - } + $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"; + $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd,"; + $sql.= " ".MAIN_DB_PREFIX."commande as c"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql.= " WHERE c.rowid = cd.fk_commande AND cd.fk_product = ".$this->id; + if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; + if ($socid > 0) + { + $sql.= " AND c.fk_soc = ".$socid; + } - /** - * \brief Charge tableau des stats facture pour le produit/service - * \param socid Id societe - * \return array Tableau des stats - */ - function load_stats_facture($socid=0) - { - global $conf; - global $user; + if ($filtrestatut <> '') + { + $sql.= " AND c.fk_statut in (".$filtrestatut.")"; + } - $sql = "SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,"; - $sql.= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty"; - $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as pd,"; - $sql.= " ".MAIN_DB_PREFIX."facture as f"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql.= " WHERE f.rowid = pd.fk_facture AND pd.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; - //$sql.= " AND f.fk_statut != 0"; - if ($socid > 0) - { - $sql .= " AND f.fk_soc = $socid"; - } + $result = $this->db->query($sql) ; + if ( $result ) + { + $obj=$this->db->fetch_object($result); + $this->stats_commande['customers']=$obj->nb_customers; + $this->stats_commande['nb']=$obj->nb; + $this->stats_commande['rows']=$obj->nb_rows; + $this->stats_commande['qty']=$obj->qty?$obj->qty:0; + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } - $result = $this->db->query($sql) ; - 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; - } - } + /** + * \brief Charge tableau des stats commande fournisseur pour le produit/service + * \param socid Id societe pour filtrer sur une soci�t� + * \param filtrestatut Id des statuts pour filtrer sur des statuts + * \return array Tableau des stats + */ + function load_stats_commande_fournisseur($socid=0,$filtrestatut='') + { + global $conf,$user; - /** - * \brief Charge tableau des stats facture pour le produit/service - * \param socid Id societe - * \return array Tableau des stats - */ - function load_stats_facture_fournisseur($socid=0) - { - global $conf; - global $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"; + $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd,"; + $sql.= " ".MAIN_DB_PREFIX."commande_fournisseur as c"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql.= " WHERE c.rowid = cd.fk_commande AND cd.fk_product = ".$this->id; + if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; + if ($socid > 0) + { + $sql.= " AND c.fk_soc = ".$socid; + } + if ($filtrestatut != '') // Peut valoir 0 + { + $sql.= " AND c.fk_statut in (".$filtrestatut.")"; + } - $sql = "SELECT COUNT(DISTINCT f.fk_soc) as nb_suppliers, COUNT(DISTINCT f.rowid) as nb,"; - $sql.= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty"; - $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as pd,"; - $sql.= " ".MAIN_DB_PREFIX."facture_fourn as f"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql.= " WHERE f.rowid = pd.fk_facture_fourn AND pd.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; - //$sql.= " AND f.fk_statut != 0"; - if ($socid > 0) - { - $sql .= " AND f.fk_soc = $socid"; - } + $result = $this->db->query($sql) ; + 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; + } + } - $result = $this->db->query($sql) ; - 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; - } - } + /** + * \brief Charge tableau des stats contrat pour le produit/service + * \param socid Id societe + * \return array Tableau des stats + */ + function load_stats_contrat($socid=0) + { + global $conf; + global $user; - /** - * \brief Return an array formated for showing graphs - * \param sql Request to execute - * \return array <0 if KO, result[i]=array(valuex,valuey); - */ - function _get_stats($sql) - { - $result = $this->db->query($sql) ; - if ($result) - { - $num = $this->db->num_rows($result); - $i = 0; - while ($i < $num) + $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"; + $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as cd,"; + $sql.= " ".MAIN_DB_PREFIX."contrat as c"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql.= " WHERE c.rowid = cd.fk_contrat 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; + //$sql.= " AND c.statut != 0"; + if ($socid > 0) + { + $sql .= " AND c.fk_soc = $socid"; + } + + $result = $this->db->query($sql) ; + 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; + } + } + + /** + * \brief Charge tableau des stats facture pour le produit/service + * \param socid Id societe + * \return array Tableau des stats + */ + 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,"; + $sql.= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty"; + $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as pd,"; + $sql.= " ".MAIN_DB_PREFIX."facture as f"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql.= " WHERE f.rowid = pd.fk_facture AND pd.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; + //$sql.= " AND f.fk_statut != 0"; + if ($socid > 0) + { + $sql .= " AND f.fk_soc = $socid"; + } + + $result = $this->db->query($sql) ; + 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; + } + } + + /** + * \brief Charge tableau des stats facture pour le produit/service + * \param socid Id societe + * \return array Tableau des stats + */ + 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,"; + $sql.= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty"; + $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as pd,"; + $sql.= " ".MAIN_DB_PREFIX."facture_fourn as f"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql.= " WHERE f.rowid = pd.fk_facture_fourn AND pd.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; + //$sql.= " AND f.fk_statut != 0"; + if ($socid > 0) + { + $sql .= " AND f.fk_soc = $socid"; + } + + $result = $this->db->query($sql) ; + 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; + } + } + + /** + * \brief Return an array formated for showing graphs + * \param sql Request to execute + * \return array <0 if KO, result[i]=array(valuex,valuey); + */ + function _get_stats($sql) + { + $result = $this->db->query($sql) ; + if ($result) + { + $num = $this->db->num_rows($result); + $i = 0; + while ($i < $num) { - $arr = $this->db->fetch_array($result); - $tab[$arr[1]] = $arr[0]; - $i++; + $arr = $this->db->fetch_array($result); + $tab[$arr[1]] = $arr[0]; + $i++; } - } - else - { - $this->error=$this->db->error().' sql='.$sql; - return -1; - } + } + else + { + $this->error=$this->db->error().' sql='.$sql; + return -1; + } - $year = strftime('%Y',time()); - $month = strftime('%m',time()); - $result = array(); + $year = strftime('%Y',time()); + $month = strftime('%m',time()); + $result = array(); - for ($j = 0 ; $j < 12 ; $j++) - { - $idx=ucfirst(substr( strftime("%b",mktime(12,0,0,$month,1,$year)) ,0,3) ); - $monthnum=sprintf("%02s",$month); + for ($j = 0 ; $j < 12 ; $j++) + { + $idx=ucfirst(substr( strftime("%b",mktime(12,0,0,$month,1,$year)) ,0,3) ); + $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); + $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); - if (strlen($month) == 3) + $month = "0".($month - 1); + if (strlen($month) == 3) { - $month = substr($month,1); + $month = substr($month,1); } - if ($month == 0) + if ($month == 0) { - $month = 12; - $year = $year - 1; + $month = 12; + $year = $year - 1; } - } + } - return array_reverse($result); + return array_reverse($result); - } + } - /** - * \brief Renvoie le nombre de factures clients du produit/service par mois - * \param socid id societe - * \return array nombre de vente par mois - */ - function get_nb_vente($socid=0) - { - global $conf; - global $user; + /** + * \brief Renvoie le nombre de factures clients du produit/service par mois + * \param socid id societe + * \return array nombre de vente par mois + */ + function get_nb_vente($socid=0) + { + global $conf; + global $user; - $sql = "SELECT sum(d.qty), date_format(f.datef, '%Y%m') "; - $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as d, ".MAIN_DB_PREFIX."facture as f"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql .= " WHERE f.rowid = d.fk_facture and d.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; - if ($socid > 0) - { - $sql .= " AND f.fk_soc = $socid"; - } - $sql .= " GROUP BY date_format(f.datef,'%Y%m') DESC ;"; + $sql = "SELECT sum(d.qty), date_format(f.datef, '%Y%m') "; + $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as d, ".MAIN_DB_PREFIX."facture as f"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE f.rowid = d.fk_facture and d.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; + if ($socid > 0) + { + $sql .= " AND f.fk_soc = $socid"; + } + $sql .= " GROUP BY date_format(f.datef,'%Y%m') DESC ;"; - return $this->_get_stats($sql); - } + return $this->_get_stats($sql); + } - /** - * \brief Renvoie le nombre de factures fournisseurs du produit/service par mois - * \param socid id societe - * \return array nombre d'achat par mois - */ - function get_nb_achat($socid=0) - { - global $conf; - global $user; + /** + * \brief Renvoie le nombre de factures fournisseurs du produit/service par mois + * \param socid id societe + * \return array nombre d'achat par mois + */ + function get_nb_achat($socid=0) + { + global $conf; + global $user; - $sql = "SELECT sum(d.qty), date_format(f.datef, '%Y%m') "; - $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as d, ".MAIN_DB_PREFIX."facture_fourn as f"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql .= " WHERE f.rowid = d.fk_facture_fourn and d.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; - if ($socid > 0) - { - $sql .= " AND f.fk_soc = $socid"; - } - $sql .= " GROUP BY date_format(f.datef,'%Y%m') DESC ;"; + $sql = "SELECT sum(d.qty), date_format(f.datef, '%Y%m') "; + $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as d, ".MAIN_DB_PREFIX."facture_fourn as f"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE f.rowid = d.fk_facture_fourn and d.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; + if ($socid > 0) + { + $sql .= " AND f.fk_soc = $socid"; + } + $sql .= " GROUP BY date_format(f.datef,'%Y%m') DESC ;"; - $resarray=$this->_get_stats($sql); - return $resarray; - } + $resarray=$this->_get_stats($sql); + return $resarray; + } - /** - * \brief Renvoie le nombre de propales dans lesquelles figure le produit par mois - * \param socid id societe - * \return array nombre de propales par mois - */ - function get_nb_propal($socid=0) - { - global $conf; - global $user; + /** + * \brief Renvoie le nombre de propales dans lesquelles figure le produit par mois + * \param socid id societe + * \return array nombre de propales par mois + */ + function get_nb_propal($socid=0) + { + global $conf; + global $user; - $sql = "SELECT sum(d.qty), date_format(p.datep, '%Y%m') "; - $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as d, ".MAIN_DB_PREFIX."propal as p"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql .= " WHERE p.rowid = d.fk_propal and d.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; - if ($socid > 0) - { - $sql .= " AND p.fk_soc = $socid"; - } - $sql .= " GROUP BY date_format(p.datep,'%Y%m') DESC ;"; + $sql = "SELECT sum(d.qty), date_format(p.datep, '%Y%m') "; + $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as d, ".MAIN_DB_PREFIX."propal as p"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE p.rowid = d.fk_propal and d.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; + if ($socid > 0) + { + $sql .= " AND p.fk_soc = $socid"; + } + $sql .= " GROUP BY date_format(p.datep,'%Y%m') DESC ;"; - return $this->_get_stats($sql); - } + return $this->_get_stats($sql); + } - /** - * \brief Renvoie le nombre de commandes dans lesquelles figure le produit par mois - * \param socid id societe - * \return array nombre de commandes par mois - */ - function get_nb_order($socid=0) - { - global $conf, $user; + /** + * \brief Renvoie le nombre de commandes dans lesquelles figure le produit par mois + * \param socid id societe + * \return array nombre de commandes par mois + */ + function get_nb_order($socid=0) + { + global $conf, $user; - $sql = "SELECT sum(d.qty), date_format(p.date_commande, '%Y%m') "; - $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as d, ".MAIN_DB_PREFIX."commande as p"; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql .= " WHERE p.rowid = d.fk_commande and d.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; - if ($socid > 0) - { - $sql .= " AND p.fk_soc = $socid"; - } - $sql .= " GROUP BY date_format(p.date_commande,'%Y%m') DESC ;"; + $sql = "SELECT sum(d.qty), date_format(p.date_commande, '%Y%m') "; + $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as d, ".MAIN_DB_PREFIX."commande as p"; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE p.rowid = d.fk_commande and d.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; + if ($socid > 0) + { + $sql .= " AND p.fk_soc = $socid"; + } + $sql .= " GROUP BY date_format(p.date_commande,'%Y%m') DESC ;"; - return $this->_get_stats($sql); - } + return $this->_get_stats($sql); + } - /** - * \brief Lie un produit associe au produit/service - * \param id_pere Id du produit auquel sera li� le produit � lier - * \param id_fils Id du produit � lier - * \return int < 0 si erreur, > 0 si ok - */ - function add_sousproduit($id_pere, $id_fils,$qty) - { - $sql = 'delete from '.MAIN_DB_PREFIX.'product_association'; - $sql .= ' WHERE fk_product_pere = "'.$id_pere.'" and fk_product_fils = "'.$id_fils.'"'; - if (! $this->db->query($sql)) - { - dolibarr_print_error($this->db); - return -1; - } - else - { - $sql = 'select fk_product_pere from '.MAIN_DB_PREFIX.'product_association'; - $sql .= ' WHERE fk_product_pere = "'.$id_fils.'" and fk_product_fils = "'.$id_pere.'"'; - if (! $this->db->query($sql)) + /** + * \brief Lie un produit associe au produit/service + * \param id_pere Id du produit auquel sera li� le produit � lier + * \param id_fils Id du produit � lier + * \return int < 0 si erreur, > 0 si ok + */ + function add_sousproduit($id_pere, $id_fils,$qty) + { + $sql = 'delete from '.MAIN_DB_PREFIX.'product_association'; + $sql .= ' WHERE fk_product_pere = "'.$id_pere.'" and fk_product_fils = "'.$id_fils.'"'; + if (! $this->db->query($sql)) + { + dolibarr_print_error($this->db); + return -1; + } + else + { + $sql = 'select fk_product_pere from '.MAIN_DB_PREFIX.'product_association'; + $sql .= ' WHERE fk_product_pere = "'.$id_fils.'" and fk_product_fils = "'.$id_pere.'"'; + if (! $this->db->query($sql)) { - dolibarr_print_error($this->db); - return -1; + dolibarr_print_error($this->db); + return -1; } - else + else { - $result = $this->db->query($sql) ; - if ($result) - { - $num = $this->db->num_rows($result); - if($num > 0) - { + $result = $this->db->query($sql) ; + if ($result) + { + $num = $this->db->num_rows($result); + if($num > 0) + { $this->error="isFatherOfThis"; return -1; - } - else - { + } + else + { $sql = 'insert into '.MAIN_DB_PREFIX.'product_association(fk_product_pere,fk_product_fils,qty)'; $sql .= ' VALUES ("'.$id_pere.'","'.$id_fils.'","'.$qty.'")'; if (! $this->db->query($sql)) - { + { + dolibarr_print_error($this->db); + return -1; + } + else + { + return 1; + } + } + } + } + + } + } + + /** + * \brief retire le lien entre un sousproduit et un produit/service + * \param id_pere Id du produit auquel ne sera plus li� le produit li + * \param id_fils Id du produit � ne plus li + * \return int < 0 si erreur, > 0 si ok + */ + function del_sousproduit($id_pere, $id_fils) + { + $sql = 'delete from '.MAIN_DB_PREFIX.'product_association'; + $sql .= ' WHERE fk_product_pere = "'.$id_pere.'" and fk_product_fils = "'.$id_fils.'"'; + if (! $this->db->query($sql)) + { dolibarr_print_error($this->db); return -1; - } - else - { - return 1; - } - } - } - } + } + else + return 1; + } - } - } - - /** - * \brief retire le lien entre un sousproduit et un produit/service - * \param id_pere Id du produit auquel ne sera plus li� le produit li - * \param id_fils Id du produit � ne plus li - * \return int < 0 si erreur, > 0 si ok - */ - function del_sousproduit($id_pere, $id_fils) - { - $sql = 'delete from '.MAIN_DB_PREFIX.'product_association'; - $sql .= ' WHERE fk_product_pere = "'.$id_pere.'" and fk_product_fils = "'.$id_fils.'"'; - if (! $this->db->query($sql)) - { - dolibarr_print_error($this->db); - return -1; - } - else - return 1; - } - - /** - * \brief retire le lien entre un sousproduit et un produit/service - * \param id_pere Id du produit auquel ne sera plus li� le produit li - * \param id_fils Id du produit � ne plus li - * \return int < 0 si erreur, > 0 si ok - */ - function is_sousproduit($id_pere, $id_fils) - { - $sql = 'select fk_product_pere,qty from '.MAIN_DB_PREFIX.'product_association'; - $sql .= ' WHERE fk_product_pere = "'.$id_pere.'" and fk_product_fils = "'.$id_fils.'"'; - if (! $this->db->query($sql)) - { - dolibarr_print_error($this->db); - return -1; - } - else - { - $result = $this->db->query($sql) ; - if ($result) - { - $num = $this->db->num_rows($result); - if($num > 0) - { - $obj = $this->db->fetch_object($result); - $this->is_sousproduit_qty = $obj->qty; - - return true; - } - else - return false; - } - } - } - - /** - * \brief Remplit le tableau des sous-produits - * \return int < 0 si erreur, > 0 si ok - */ - function load_subproduct() - { - $this->subproducts_id = array(); - $i = 0; - - $sql = "SELECT fk_product_subproduct FROM ".MAIN_DB_PREFIX."product_subproduct"; - $sql .= " WHERE fk_product=$this->id;"; - - if ($result = $this->db->query($sql)) + /** + * \brief retire le lien entre un sousproduit et un produit/service + * \param id_pere Id du produit auquel ne sera plus li� le produit li + * \param id_fils Id du produit � ne plus li + * \return int < 0 si erreur, > 0 si ok + */ + function is_sousproduit($id_pere, $id_fils) { + $sql = 'select fk_product_pere,qty from '.MAIN_DB_PREFIX.'product_association'; + $sql .= ' WHERE fk_product_pere = "'.$id_pere.'" and fk_product_fils = "'.$id_fils.'"'; + if (! $this->db->query($sql)) + { + dolibarr_print_error($this->db); + return -1; + } + else + { + $result = $this->db->query($sql) ; + if ($result) + { + $num = $this->db->num_rows($result); + if($num > 0) + { + $obj = $this->db->fetch_object($result); + $this->is_sousproduit_qty = $obj->qty; + + return true; + } + else + return false; + } + } + } + + /** + * \brief Remplit le tableau des sous-produits + * \return int < 0 si erreur, > 0 si ok + */ + function load_subproduct() + { + $this->subproducts_id = array(); + $i = 0; + + $sql = "SELECT fk_product_subproduct FROM ".MAIN_DB_PREFIX."product_subproduct"; + $sql .= " WHERE fk_product=$this->id;"; + + if ($result = $this->db->query($sql)) + { while ($row = $this->db->fetch_row($result) ) { - $this->subproducts_id[$i] = $row[0]; - $i++; + $this->subproducts_id[$i] = $row[0]; + $i++; } $this->db->free($result); return 0; - } - else - { + } + else + { return -1; + } } - } - /** - * \brief Lie un sous produit au produit/service - * \param id_sub Id du produit � lier - * \return int < 0 si erreur, > 0 si ok - */ - function add_subproduct($id_sub) - { - if ($id_sub) - { - $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'product_subproduct(fk_product,fk_product_subproduct)'; - $sql .= ' VALUES ("'.$this->id.'","'.$id_sub.'")'; - if (! $this->db->query($sql)) - { - dolibarr_print_error($this->db); - return -1; - } - else - { - return 0; - } - } - else - { - return -2; - } - } - - /** - * \brief Lie un fournisseur au produit/service - * \param user Utilisateur qui fait le lien - * \param id_fourn Id du fournisseur - * \param ref_fourn Reference chez le fournisseur - * \return int < 0 si erreur, > 0 si ok - */ - function add_fournisseur($user, $id_fourn, $ref_fourn) - { - $sql = "SELECT count(*) as nb"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; - $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn." AND ref_fourn = '".$ref_fourn."'"; - - $resql=$this->db->query($sql); - if ($resql) - { - $obj = $this->db->fetch_object($resql); - if ($obj->nb == 0) - { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur "; - $sql .= " (datec, fk_product, fk_soc, ref_fourn, fk_user_author)"; - $sql .= " VALUES (now(), ".$this->id.", ".$id_fourn.", '".$ref_fourn."', ".$user->id.")"; - - if ($this->db->query($sql)) - { - $this->product_fourn_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur"); - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - else - { - $sql = "SELECT rowid"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; - $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn." AND ref_fourn = '".$ref_fourn."'"; - - $resql=$this->db->query($sql); - if ($resql) - { - $obj = $this->db->fetch_object($resql); - $this->product_fourn_id = $obj->rowid; - } - } - $this->db->free($resql); - } - else - { - $this->error=$this->db->error(); - return -2; - } - } - - - /** - * \brief Renvoie la liste des fournisseurs du produit/service - * \return array Tableau des id de fournisseur - */ - function list_suppliers() - { - $list = array(); - - $sql = "SELECT fk_soc"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur as p"; - $sql.= " WHERE p.fk_product = ".$this->id; - - $result = $this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows($result); - $i=0; - while ($i < $num) - { - $obj = $this->db->fetch_object($result); - $list[$i] = $obj->fk_soc; - $i++; - } - } - - return $list; - } - - /** - * \brief Saisie une commande fournisseur - * \param user Objet user de celui qui demande - * \return int <0 si ko, >0 si ok - */ - function fastappro($user) - { - include_once DOL_DOCUMENT_ROOT."/fourn/fournisseur.class.php"; - - $list = $this->list_suppliers(); - if (sizeof($list) > 0) - { - dolibarr_syslog("Product::fastappro"); - $fournisseur = new Fournisseur($this->db); - $fournisseur->fetch($this->fourn_appro_open); - - $fournisseur->ProductCommande($user, $this->id); - } - return 1; - } - - - /** - * \brief Supprime un tarif fournisseur - * \param user utilisateur qui d�fait le lien - * \param id_fourn id du fournisseur - * \param qty quantit - * \return int < 0 si erreur, > 0 si ok - */ - function remove_price($user, $id_fourn, $qty) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; - $sql.= " WHERE fk_product = $this->id AND fk_soc = $id_fourn and quantity = '".$qty."';"; - - if ($this->db->query($sql) ) - { - return 1; - } - else - { - dolibarr_print_error($this->db); - return -1; - } - } - - /** - * \brief Recopie les prix d'un produit/service sur un autre - * \param fromId Id produit source - * \param toId Id produit cible - * \return int < 0 si erreur, > 0 si ok - */ - function clone_price($fromId, $toId) - { - global $db; - - $db->begin(); - - // les prix - $sql = "insert " .MAIN_DB_PREFIX."product_price (" - . " fk_product, date_price, price, tva_tx, fk_user_author, envente )" - . " select ".$toId . ", date_price, price, tva_tx, fk_user_author, envente " - . " from ".MAIN_DB_PREFIX."product_price " - . " where fk_product = ". $fromId . ";" ; - if ( ! $db->query($sql ) ) { - $db->rollback(); - return -1; - } - $db->commit(); - return 1; - } - - /** - * \brief Recopie les fournisseurs et prix fournisseurs d'un produit/service sur un autre - * \param fromId Id produit source - * \param toId Id produit cible - * \return int < 0 si erreur, > 0 si ok - */ - function clone_fournisseurs($fromId, $toId) - { - global $db; - - $db->begin(); - - // les fournisseurs - $sql = "insert ".MAIN_DB_PREFIX."product_fournisseur (" - . " datec, fk_product, fk_soc, ref_fourn, fk_user_author )" - . " select now(), ".$toId.", fk_soc, ref_fourn, fk_user_author" - . " from ".MAIN_DB_PREFIX."product_fournisseur " - . " where fk_product = ".$fromId .";" ; - if ( ! $db->query($sql ) ) { - $db->rollback(); - return -1; - } - // les prix de fournisseurs. - $sql = "insert ".MAIN_DB_PREFIX."product_fournisseur_price (" - . " datec, fk_product, fk_soc, price, quantity, fk_user )" - . " select now(), ".$toId. ", fk_soc, price, quantity, fk_user" - . " from ".MAIN_DB_PREFIX."product_fournisseur_price" - . " where fk_product = ".$fromId.";"; - if ( ! $db->query($sql ) ) { - $db->rollback(); - return -1; - } - $db->commit(); - return 1; - } - /** - * \brief fonction r�cursive uniquement utilis�e par get_arbo_each_prod, recompose l'arborescence des sousproduits - * \return void - */ - function fetch_prod_arbo($prod,$compl_path="") - { - $this->res; - $this->pere_encours; - foreach($prod as $nom_pere => $desc_pere) - { - // on est dans une sous-cat�gorie - if(is_array($desc_pere)) - $this->res[]= array($compl_path.stripslashes($nom_pere)." (".$desc_pere[1].")",$desc_pere[0]); - else if($nom_pere != "0" && $nom_pere != "1") - $this->res[]= array($compl_path.stripslashes($nom_pere),$desc_pere); - if(sizeof($desc_pere) >1) - { - $this ->fetch_prod_arbo($desc_pere,stripslashes($nom_pere)." -> "); - } - } - } - /** - * \brief fonction r�cursive uniquement utilis�e par get_each_prod, ajoute chaque sousproduits dans le tableau res - * \return void - */ - function fetch_prods($prod) - { - $this->res; - foreach($prod as $nom_pere => $desc_pere) - { - // on est dans une sous-cat�gorie - if(is_array($desc_pere)) - $this->res[]= array($desc_pere[1],$desc_pere[0]); - if(sizeof($desc_pere) >1) - { - $this ->fetch_prods($desc_pere); - } - } - } - /** - * \brief reconstruit l'arborescence des cat�gorie sous la forme d'un tableau - * \return array $this->res - */ - function get_arbo_each_prod() - { - $this->res = array(); - if(is_array($this -> sousprods)) - { - foreach($this -> sousprods as $nom_pere => $desc_pere) - { - if(sizeof($desc_pere) >1) - $this ->fetch_prod_arbo($desc_pere); - - } - sort($this->res); - } - return $this->res; - } - /** - * \brief renvoie tous les sousproduits dans le tableau res, chaque ligne de res contient : id -> qty - * \return array $this->res - */ - function get_each_prod() - { - $this->res = array(); - if(is_array($this -> sousprods)) - { - foreach($this -> sousprods as $nom_pere => $desc_pere) - { - if(sizeof($desc_pere) >1) - $this ->fetch_prods($desc_pere); - - } - sort($this->res); - } - return $this->res; - } - - /** - * \brief Retourne les cat�gories p�res - * \return array prod - */ - function get_pere() - { - - $sql = "SELECT p.label as label,p.rowid,pa.fk_product_pere as id FROM "; - $sql .= MAIN_DB_PREFIX."product_association as pa,"; - $sql .= MAIN_DB_PREFIX."product as p"; - $sql .= " where p.rowid=pa.fk_product_pere and p.rowid = '".$this->id."'"; - $res = $this->db->query ($sql); - if ($res) - { - $prods = array (); - while ($record = $this->db->fetch_array ($res)) - { - $prods[addslashes($record['label'])] = array(0=>$record['id']); - } - return $prods; - } - else - { - dolibarr_print_error ($this->db); - return -1; - } - } - - /** - * \brief Retourne les fils de la cat�gorie structur�s pour l'arbo - * \return array prod - */ - function get_fils_arbo ($id_pere) - { - $sql = "SELECT p.rowid, p.label as label,pa.qty as qty,pa.fk_product_fils as id FROM "; - $sql .= MAIN_DB_PREFIX."product as p,".MAIN_DB_PREFIX."product_association as pa"; - $sql .= " WHERE p.rowid = pa.fk_product_fils and pa.fk_product_pere = '".$id_pere."'"; - $res = $this->db->query ($sql); - - if ($res) - { - $prods = array(); - while ($rec = $this->db->fetch_array ($res)) - { - $prods[addslashes($rec['label'])]= array(0=>$rec['id'],1=>$rec['qty']); - foreach($this -> get_fils_arbo($rec['id']) as $kf=>$vf) - $prods[addslashes($rec['label'])][$kf] = $vf; - } - return $prods; - } - else - { - dolibarr_print_error ($this->db); - return -1; - } - } - /** - * \brief compose l'arborescence des sousproduits, id, nom et quantit� sous la forme d'un tableau associatif - * \return void - */ - function get_sousproduits_arbo () - { - - $peres = $this -> get_pere(); - foreach($peres as $k=>$v) - { - foreach($this -> get_fils_arbo($v[0]) as $kf=>$vf) - $peres[$k][$kf] = $vf; - } - // on concat�ne tout �a - foreach($peres as $k=>$v) - { - $this -> sousprods[$k]=$v; - } - } - - /** - * \brief Renvoie nom clicable (avec eventuellement le picto) - * \param withpicto Inclut le picto dans le lien - * \param option Sur quoi pointe le lien - * \param maxlength Maxlength of ref - * \return string Chaine avec URL - */ - function getNomUrl($withpicto=0,$option='',$maxlength=0) - { - global $langs; - - $result=''; - - $lien = ''; - $lienfin=''; - $newref=$this->ref; - if ($maxlength) $newref=dolibarr_trunc($newref,$maxlength); - - if ($withpicto) { - if ($this->type == 0) $result.=($lien.img_object($langs->trans("ShowProduct").' '.$this->ref,'product').$lienfin.' '); - if ($this->type == 1) $result.=($lien.img_object($langs->trans("ShowService").' '.$this->ref,'service').$lienfin.' '); - } - $result.=$lien.$newref.$lienfin; - return $result; - } - - /** - * \brief Retourne le libell� du statut d'une facture (brouillon, valid�e, abandonn�e, pay�e) - * \param mode 0=libell� long, 1=libell� court, 2=Picto + Libell� court, 3=Picto, 4=Picto + Libell� long, 5=Libell� court + Picto - * \return string Libelle - */ - function getLibStatut($mode=0) - { - return $this->LibStatut($this->status,$mode); - } - - /** - * \brief Renvoi le libell� d'un statut donne - * \param status Statut - * \param mode 0=libell� long, 1=libell� court, 2=Picto + Libell� court, 3=Picto, 4=Picto + Libell� long, 5=Libell� court + Picto - * \return string Libell� du statut - */ - function LibStatut($status,$mode=0) - { - global $langs; - $langs->load('products'); - if ($mode == 0) - { - if ($status == 0) return $langs->trans('ProductStatusNotOnSellShort'); - if ($status == 1) return $langs->trans('ProductStatusOnSellShort'); - } - if ($mode == 1) - { - if ($status == 0) return $langs->trans('ProductStatusNotOnSell'); - if ($status == 1) return $langs->trans('ProductStatusOnSell'); - } - if ($mode == 2) - { - if ($status == 0) return img_picto($langs->trans('ProductStatusNotOnSell'),'statut5').' '.$langs->trans('ProductStatusNotOnSell'); - if ($status == 1) return img_picto($langs->trans('ProductStatusOnSell'),'statut4').' '.$langs->trans('ProductStatusOnSell'); - } - if ($mode == 3) - { - if ($status == 0) return img_picto($langs->trans('ProductStatusNotOnSell'),'statut5'); - if ($status == 1) return img_picto($langs->trans('ProductStatusOnSell'),'statut4'); - } - if ($mode == 4) - { - if ($status == 0) return img_picto($langs->trans('ProductStatusNotOnSell'),'statut5').' '.$langs->trans('ProductStatusNotOnSell'); - if ($status == 1) return img_picto($langs->trans('ProductStatusOnSell'),'statut4').' '.$langs->trans('ProductStatusOnSell'); - } - if ($mode == 5) - { - if ($status == 0) return $langs->trans('ProductStatusNotOnSell').' '.img_picto($langs->trans('ProductStatusNotOnSell'),'statut5'); - if ($status == 1) return $langs->trans('ProductStatusOnSell').' '.img_picto($langs->trans('ProductStatusOnSell'),'statut4'); - } - return $langs->trans('Unknown'); - } - /** - * \brief Entre un nombre de piece du produit en stock dans un entrep�t - * \param id_entrepot id de l'entrepot - * \param nbpiece nombre de pieces - */ + * \brief Lie un sous produit au produit/service + * \param id_sub Id du produit � lier + * \return int < 0 si erreur, > 0 si ok + */ + function add_subproduct($id_sub) + { + if ($id_sub) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'product_subproduct(fk_product,fk_product_subproduct)'; + $sql .= ' VALUES ("'.$this->id.'","'.$id_sub.'")'; + if (! $this->db->query($sql)) + { + dolibarr_print_error($this->db); + return -1; + } + else + { + return 0; + } + } + else + { + return -2; + } + } + + /** + * \brief Lie un fournisseur au produit/service + * \param user Utilisateur qui fait le lien + * \param id_fourn Id du fournisseur + * \param ref_fourn Reference chez le fournisseur + * \return int < 0 si erreur, > 0 si ok + */ + function add_fournisseur($user, $id_fourn, $ref_fourn) + { + $sql = "SELECT count(*) as nb"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; + $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn." AND ref_fourn = '".$ref_fourn."'"; + + $resql=$this->db->query($sql); + if ($resql) + { + $obj = $this->db->fetch_object($resql); + if ($obj->nb == 0) + { + $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur "; + $sql .= " (datec, fk_product, fk_soc, ref_fourn, fk_user_author)"; + $sql .= " VALUES (now(), ".$this->id.", ".$id_fourn.", '".$ref_fourn."', ".$user->id.")"; + + if ($this->db->query($sql)) + { + $this->product_fourn_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur"); + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + else + { + $sql = "SELECT rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; + $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn." AND ref_fourn = '".$ref_fourn."'"; + + $resql=$this->db->query($sql); + if ($resql) + { + $obj = $this->db->fetch_object($resql); + $this->product_fourn_id = $obj->rowid; + } + } + $this->db->free($resql); + } + else + { + $this->error=$this->db->error(); + return -2; + } + } + + + /** + * \brief Renvoie la liste des fournisseurs du produit/service + * \return array Tableau des id de fournisseur + */ + function list_suppliers() + { + $list = array(); + + $sql = "SELECT fk_soc"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur as p"; + $sql.= " WHERE p.fk_product = ".$this->id; + + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + $i=0; + while ($i < $num) + { + $obj = $this->db->fetch_object($result); + $list[$i] = $obj->fk_soc; + $i++; + } + } + + return $list; + } + + /** + * \brief Saisie une commande fournisseur + * \param user Objet user de celui qui demande + * \return int <0 si ko, >0 si ok + */ + function fastappro($user) + { + include_once DOL_DOCUMENT_ROOT."/fourn/fournisseur.class.php"; + + $list = $this->list_suppliers(); + if (sizeof($list) > 0) + { + dolibarr_syslog("Product::fastappro"); + $fournisseur = new Fournisseur($this->db); + $fournisseur->fetch($this->fourn_appro_open); + + $fournisseur->ProductCommande($user, $this->id); + } + return 1; + } + + + /** + * \brief Supprime un tarif fournisseur + * \param user utilisateur qui d�fait le lien + * \param id_fourn id du fournisseur + * \param qty quantit + * \return int < 0 si erreur, > 0 si ok + */ + function remove_price($user, $id_fourn, $qty) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; + $sql.= " WHERE fk_product = $this->id AND fk_soc = $id_fourn and quantity = '".$qty."';"; + + if ($this->db->query($sql) ) + { + return 1; + } + else + { + dolibarr_print_error($this->db); + return -1; + } + } + + /** + * \brief Recopie les prix d'un produit/service sur un autre + * \param fromId Id produit source + * \param toId Id produit cible + * \return int < 0 si erreur, > 0 si ok + */ + function clone_price($fromId, $toId) + { + global $db; + + $db->begin(); + + // les prix + $sql = "insert " .MAIN_DB_PREFIX."product_price (" + . " fk_product, date_price, price, tva_tx, fk_user_author, envente )" + . " select ".$toId . ", date_price, price, tva_tx, fk_user_author, envente " + . " from ".MAIN_DB_PREFIX."product_price " + . " where fk_product = ". $fromId . ";" ; + if ( ! $db->query($sql ) ) { + $db->rollback(); + return -1; + } + $db->commit(); + return 1; + } + + /** + * \brief Recopie les fournisseurs et prix fournisseurs d'un produit/service sur un autre + * \param fromId Id produit source + * \param toId Id produit cible + * \return int < 0 si erreur, > 0 si ok + */ + function clone_fournisseurs($fromId, $toId) + { + global $db; + + $db->begin(); + + // les fournisseurs + $sql = "insert ".MAIN_DB_PREFIX."product_fournisseur (" + . " datec, fk_product, fk_soc, ref_fourn, fk_user_author )" + . " select now(), ".$toId.", fk_soc, ref_fourn, fk_user_author" + . " from ".MAIN_DB_PREFIX."product_fournisseur " + . " where fk_product = ".$fromId .";" ; + if ( ! $db->query($sql ) ) { + $db->rollback(); + return -1; + } + // les prix de fournisseurs. + $sql = "insert ".MAIN_DB_PREFIX."product_fournisseur_price (" + . " datec, fk_product, fk_soc, price, quantity, fk_user )" + . " select now(), ".$toId. ", fk_soc, price, quantity, fk_user" + . " from ".MAIN_DB_PREFIX."product_fournisseur_price" + . " where fk_product = ".$fromId.";"; + if ( ! $db->query($sql ) ) { + $db->rollback(); + return -1; + } + $db->commit(); + return 1; + } + /** + * \brief fonction r�cursive uniquement utilis�e par get_arbo_each_prod, recompose l'arborescence des sousproduits + * \return void + */ + function fetch_prod_arbo($prod,$compl_path="") + { + $this->res; + $this->pere_encours; + foreach($prod as $nom_pere => $desc_pere) + { + // on est dans une sous-cat�gorie + if(is_array($desc_pere)) + $this->res[]= array($compl_path.stripslashes($nom_pere)." (".$desc_pere[1].")",$desc_pere[0]); + else if($nom_pere != "0" && $nom_pere != "1") + $this->res[]= array($compl_path.stripslashes($nom_pere),$desc_pere); + if(sizeof($desc_pere) >1) + { + $this ->fetch_prod_arbo($desc_pere,stripslashes($nom_pere)." -> "); + } + } + } + /** + * \brief fonction r�cursive uniquement utilis�e par get_each_prod, ajoute chaque sousproduits dans le tableau res + * \return void + */ + function fetch_prods($prod) + { + $this->res; + foreach($prod as $nom_pere => $desc_pere) + { + // on est dans une sous-cat�gorie + if(is_array($desc_pere)) + $this->res[]= array($desc_pere[1],$desc_pere[0]); + if(sizeof($desc_pere) >1) + { + $this ->fetch_prods($desc_pere); + } + } + } + /** + * \brief reconstruit l'arborescence des cat�gorie sous la forme d'un tableau + * \return array $this->res + */ + function get_arbo_each_prod() + { + $this->res = array(); + if(is_array($this -> sousprods)) + { + foreach($this -> sousprods as $nom_pere => $desc_pere) + { + if(sizeof($desc_pere) >1) + $this ->fetch_prod_arbo($desc_pere); + + } + sort($this->res); + } + return $this->res; + } + /** + * \brief renvoie tous les sousproduits dans le tableau res, chaque ligne de res contient : id -> qty + * \return array $this->res + */ + function get_each_prod() + { + $this->res = array(); + if(is_array($this -> sousprods)) + { + foreach($this -> sousprods as $nom_pere => $desc_pere) + { + if(sizeof($desc_pere) >1) + $this ->fetch_prods($desc_pere); + + } + sort($this->res); + } + return $this->res; + } + + /** + * \brief Retourne les cat�gories p�res + * \return array prod + */ + function get_pere() + { + + $sql = "SELECT p.label as label,p.rowid,pa.fk_product_pere as id FROM "; + $sql .= MAIN_DB_PREFIX."product_association as pa,"; + $sql .= MAIN_DB_PREFIX."product as p"; + $sql .= " where p.rowid=pa.fk_product_pere and p.rowid = '".$this->id."'"; + $res = $this->db->query ($sql); + if ($res) + { + $prods = array (); + while ($record = $this->db->fetch_array ($res)) + { + $prods[addslashes($record['label'])] = array(0=>$record['id']); + } + return $prods; + } + else + { + dolibarr_print_error ($this->db); + return -1; + } + } + + /** + * \brief Retourne les fils de la cat�gorie structur�s pour l'arbo + * \return array prod + */ + function get_fils_arbo ($id_pere) + { + $sql = "SELECT p.rowid, p.label as label,pa.qty as qty,pa.fk_product_fils as id FROM "; + $sql .= MAIN_DB_PREFIX."product as p,".MAIN_DB_PREFIX."product_association as pa"; + $sql .= " WHERE p.rowid = pa.fk_product_fils and pa.fk_product_pere = '".$id_pere."'"; + $res = $this->db->query ($sql); + + if ($res) + { + $prods = array(); + while ($rec = $this->db->fetch_array ($res)) + { + $prods[addslashes($rec['label'])]= array(0=>$rec['id'],1=>$rec['qty']); + foreach($this -> get_fils_arbo($rec['id']) as $kf=>$vf) + $prods[addslashes($rec['label'])][$kf] = $vf; + } + return $prods; + } + else + { + dolibarr_print_error ($this->db); + return -1; + } + } + /** + * \brief compose l'arborescence des sousproduits, id, nom et quantit� sous la forme d'un tableau associatif + * \return void + */ + function get_sousproduits_arbo () + { + + $peres = $this -> get_pere(); + foreach($peres as $k=>$v) + { + foreach($this -> get_fils_arbo($v[0]) as $kf=>$vf) + $peres[$k][$kf] = $vf; + } + // on concat�ne tout �a + foreach($peres as $k=>$v) + { + $this -> sousprods[$k]=$v; + } + } + + /** + * \brief Renvoie nom clicable (avec eventuellement le picto) + * \param withpicto Inclut le picto dans le lien + * \param option Sur quoi pointe le lien + * \param maxlength Maxlength of ref + * \return string Chaine avec URL + */ + function getNomUrl($withpicto=0,$option='',$maxlength=0) + { + global $langs; + + $result=''; + + $lien = ''; + $lienfin=''; + $newref=$this->ref; + if ($maxlength) $newref=dolibarr_trunc($newref,$maxlength); + + if ($withpicto) { + if ($this->type == 0) $result.=($lien.img_object($langs->trans("ShowProduct").' '.$this->ref,'product').$lienfin.' '); + if ($this->type == 1) $result.=($lien.img_object($langs->trans("ShowService").' '.$this->ref,'service').$lienfin.' '); + } + $result.=$lien.$newref.$lienfin; + return $result; + } + + /** + * \brief Retourne le libell� du statut d'une facture (brouillon, valid�e, abandonn�e, pay�e) + * \param mode 0=libell� long, 1=libell� court, 2=Picto + Libell� court, 3=Picto, 4=Picto + Libell� long, 5=Libell� court + Picto + * \return string Libelle + */ + function getLibStatut($mode=0) + { + return $this->LibStatut($this->status,$mode); + } + + /** + * \brief Renvoi le libell� d'un statut donne + * \param status Statut + * \param mode 0=libell� long, 1=libell� court, 2=Picto + Libell� court, 3=Picto, 4=Picto + Libell� long, 5=Libell� court + Picto + * \return string Libell� du statut + */ + function LibStatut($status,$mode=0) + { + global $langs; + $langs->load('products'); + if ($mode == 0) + { + if ($status == 0) return $langs->trans('ProductStatusNotOnSellShort'); + if ($status == 1) return $langs->trans('ProductStatusOnSellShort'); + } + if ($mode == 1) + { + if ($status == 0) return $langs->trans('ProductStatusNotOnSell'); + if ($status == 1) return $langs->trans('ProductStatusOnSell'); + } + if ($mode == 2) + { + if ($status == 0) return img_picto($langs->trans('ProductStatusNotOnSell'),'statut5').' '.$langs->trans('ProductStatusNotOnSell'); + if ($status == 1) return img_picto($langs->trans('ProductStatusOnSell'),'statut4').' '.$langs->trans('ProductStatusOnSell'); + } + if ($mode == 3) + { + if ($status == 0) return img_picto($langs->trans('ProductStatusNotOnSell'),'statut5'); + if ($status == 1) return img_picto($langs->trans('ProductStatusOnSell'),'statut4'); + } + if ($mode == 4) + { + if ($status == 0) return img_picto($langs->trans('ProductStatusNotOnSell'),'statut5').' '.$langs->trans('ProductStatusNotOnSell'); + if ($status == 1) return img_picto($langs->trans('ProductStatusOnSell'),'statut4').' '.$langs->trans('ProductStatusOnSell'); + } + if ($mode == 5) + { + if ($status == 0) return $langs->trans('ProductStatusNotOnSell').' '.img_picto($langs->trans('ProductStatusNotOnSell'),'statut5'); + if ($status == 1) return $langs->trans('ProductStatusOnSell').' '.img_picto($langs->trans('ProductStatusOnSell'),'statut4'); + } + return $langs->trans('Unknown'); + } + + /** + * \brief Entre un nombre de piece du produit en stock dans un entrep�t + * \param id_entrepot id de l'entrepot + * \param nbpiece nombre de pieces + */ function create_stock($id_entrepot, $nbpiece) { global $user; - + $op[0] = "+".trim($nbpiece); $op[1] = "-".trim($nbpiece); $mouvement=0; // We add pieces - + $this->db->begin(); - + $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_stock "; $sql .= " (fk_product, fk_entrepot, reel)"; $sql .= " VALUES ($this->id, $id_entrepot, $nbpiece)"; @@ -2021,7 +2021,7 @@ class Product extends CommonObject { $sql = "INSERT INTO ".MAIN_DB_PREFIX."stock_mouvement (datem, fk_product, fk_entrepot, value, type_mouvement, fk_user_author)"; $sql .= " VALUES (now(), ".$this->id.", ".$id_entrepot.", ".$nbpiece.", 0, ".$user->id.")"; - + dolibarr_syslog("Product::create_stock sql=".$sql); $resql=$this->db->query($sql); if ($resql) @@ -2045,12 +2045,12 @@ class Product extends CommonObject /** - * \brief Ajuste le stock d'un entrepot pour le produit a une valeure donnee - * \param user utilisateur qui demande l'ajustement - * \param id_entrepot id de l'entrepot - * \param nbpiece nombre de pieces - * \param mouvement 0 = ajout, 1 = suppression - */ + * \brief Ajuste le stock d'un entrepot pour le produit a une valeure donnee + * \param user utilisateur qui demande l'ajustement + * \param id_entrepot id de l'entrepot + * \param nbpiece nombre de pieces + * \param mouvement 0 = ajout, 1 = suppression + */ function correct_stock($user, $id_entrepot, $nbpiece, $mouvement) { if ($id_entrepot) @@ -2083,12 +2083,12 @@ class Product extends CommonObject } /** - * \brief Augmente ou r�duit la valeur de stock pour le produit - * \param user utilisateur qui demande l'ajustement - * \param id_entrepot id de l'entrepot - * \param nbpiece nombre de pieces - * \param mouvement 0 = ajout, 1 = suppression - */ + * \brief Augmente ou r�duit la valeur de stock pour le produit + * \param user utilisateur qui demande l'ajustement + * \param id_entrepot id de l'entrepot + * \param nbpiece nombre de pieces + * \param mouvement 0 = ajout, 1 = suppression + */ function ajust_stock($user, $id_entrepot, $nbpiece, $mouvement) { $op[0] = "+".trim($nbpiece); @@ -2099,14 +2099,14 @@ class Product extends CommonObject $sql = "UPDATE ".MAIN_DB_PREFIX."product_stock"; $sql.= " SET reel = reel ".$op[$mouvement]; $sql.= " WHERE fk_product = ".$this->id." AND fk_entrepot = ".$id_entrepot; - + dolibarr_syslog("Product::ajust_stock sql=".$sql); $resql=$this->db->query($sql); if ($resql) { $sql = "INSERT INTO ".MAIN_DB_PREFIX."stock_mouvement (datem, fk_product, fk_entrepot, value, type_mouvement, fk_user_author)"; $sql .= " VALUES (now(), ".$this->id.", ".$id_entrepot.", ".$op[$mouvement].", 0, ".$user->id.")"; - + dolibarr_syslog("Product::ajust_stock sql=".$sql); $resql=$this->db->query($sql); if ($resql) @@ -2126,422 +2126,422 @@ class Product extends CommonObject dolibarr_print_error($this->db); $this->db->rollback(); return -1; - } + } } - - /** - * \brief Augmente ou r�duit le nombre de piece en commande a expedier - * \param nbpiece nombre de pieces - * \param mouvement 0 = ajout, 1 = suppression - * \return int < 0 si erreur, > 0 si ok - */ - function ajust_stock_commande($nbpiece, $mouvement) - { - $op[0] = "+" . trim($nbpiece); - $op[1] = "-" . trim($nbpiece); - if ($this->db->begin()) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."product "; - $sql .= " SET stock_commande = stock_commande ".$op[$mouvement]; - $sql .= " WHERE rowid = '".$this->id ."';"; - - if ($this->db->query($sql) ) - { - - $this->load_subproduct(); - - for ($i = 0 ; $i < sizeof($this->subproducts_id) ; $i++) - { - $product = new Product($this->db); - $product->id = $this->subproducts_id[$i]; - $product->ajust_stock_commande($nbpiece, $mouvement); - } - - $this->db->commit(); - return 1; - } - else - { - dolibarr_print_error($this->db); - $this->db->rollback(); - return -2; - } - } - else - { - dolibarr_print_error($this->db); - $this->db->rollback(); - return -3; - } - } - - - /** - * \brief Charge les informations en stock du produit - * \return int < 0 si erreur, > 0 si ok - */ - function load_stock() - { - $this->stock_reel = 0; - - $sql = "SELECT reel, fk_entrepot"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_stock"; - $sql.= " WHERE fk_product = '".$this->id."'"; - $result = $this->db->query($sql) ; - if ($result) - { - $num = $this->db->num_rows($result); - $i=0; - if ($num > 0) - { - while ($i < $num ) - { - $row = $this->db->fetch_row($result); - $this->stock_entrepot[$row[1]] = $row[0]; - $this->stock_reel = $this->stock_reel + $row[0]; - $i++; - } - - $this->no_stock = 0; - } - else - { - $this->no_stock = 1; - } - $this->db->free($result); - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - - - /** - * \brief D�place fichier upload� sous le nom $files dans le r�pertoire sdir - * \param sdir R�pertoire destination finale - * \param $file Nom du fichier upload� - * \param maxWidth Largeur maximum que dois faire la miniature (160 par d�faut) - * \param maxHeight Hauteur maximum que dois faire la miniature (120 par d�faut) - */ - function add_photo($sdir, $file, $maxWidth = 160, $maxHeight = 120) - { - $dir = $sdir .'/'. get_exdir($this->id,2) . $this->id ."/"; - $dir .= "photos/"; - - if (! file_exists($dir)) - { - dolibarr_syslog("Product Create $dir"); - create_exdir($dir); - } - - if (file_exists($dir)) - { - $originImage = $dir . $file['name']; - - // Cree fichier en taille origine - $result=dol_move_uploaded_file($file['tmp_name'], $originImage, 1); - - if (file_exists($originImage)) - { - // Cree fichier en taille vignette - $this->add_thumb($originImage,$maxWidth,$maxHeight); - } - } - } - /** - * \brief Build thumb - * \param sdir Repertoire destination finale - * \param file Chemin du fichier d'origine - * \param maxWidth Largeur maximum que dois faire la miniature (160 par defaut) - * \param maxHeight Hauteur maximum que dois faire la miniature (120 par defaut) - */ + * \brief Augmente ou r�duit le nombre de piece en commande a expedier + * \param nbpiece nombre de pieces + * \param mouvement 0 = ajout, 1 = suppression + * \return int < 0 si erreur, > 0 si ok + */ + function ajust_stock_commande($nbpiece, $mouvement) + { + $op[0] = "+" . trim($nbpiece); + $op[1] = "-" . trim($nbpiece); + + if ($this->db->begin()) + { + $sql = "UPDATE ".MAIN_DB_PREFIX."product "; + $sql .= " SET stock_commande = stock_commande ".$op[$mouvement]; + $sql .= " WHERE rowid = '".$this->id ."';"; + + if ($this->db->query($sql) ) + { + + $this->load_subproduct(); + + for ($i = 0 ; $i < sizeof($this->subproducts_id) ; $i++) + { + $product = new Product($this->db); + $product->id = $this->subproducts_id[$i]; + $product->ajust_stock_commande($nbpiece, $mouvement); + } + + $this->db->commit(); + return 1; + } + else + { + dolibarr_print_error($this->db); + $this->db->rollback(); + return -2; + } + } + else + { + dolibarr_print_error($this->db); + $this->db->rollback(); + return -3; + } + } + + + /** + * \brief Charge les informations en stock du produit + * \return int < 0 si erreur, > 0 si ok + */ + function load_stock() + { + $this->stock_reel = 0; + + $sql = "SELECT reel, fk_entrepot"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_stock"; + $sql.= " WHERE fk_product = '".$this->id."'"; + $result = $this->db->query($sql) ; + if ($result) + { + $num = $this->db->num_rows($result); + $i=0; + if ($num > 0) + { + while ($i < $num ) + { + $row = $this->db->fetch_row($result); + $this->stock_entrepot[$row[1]] = $row[0]; + $this->stock_reel = $this->stock_reel + $row[0]; + $i++; + } + + $this->no_stock = 0; + } + else + { + $this->no_stock = 1; + } + $this->db->free($result); + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + + + /** + * \brief D�place fichier upload� sous le nom $files dans le r�pertoire sdir + * \param sdir R�pertoire destination finale + * \param $file Nom du fichier upload� + * \param maxWidth Largeur maximum que dois faire la miniature (160 par d�faut) + * \param maxHeight Hauteur maximum que dois faire la miniature (120 par d�faut) + */ + function add_photo($sdir, $file, $maxWidth = 160, $maxHeight = 120) + { + $dir = $sdir .'/'. get_exdir($this->id,2) . $this->id ."/"; + $dir .= "photos/"; + + if (! file_exists($dir)) + { + dolibarr_syslog("Product Create $dir"); + create_exdir($dir); + } + + if (file_exists($dir)) + { + $originImage = $dir . $file['name']; + + // Cree fichier en taille origine + $result=dol_move_uploaded_file($file['tmp_name'], $originImage, 1); + + if (file_exists($originImage)) + { + // Cree fichier en taille vignette + $this->add_thumb($originImage,$maxWidth,$maxHeight); + } + } + } + + /** + * \brief Build thumb + * \param sdir Repertoire destination finale + * \param file Chemin du fichier d'origine + * \param maxWidth Largeur maximum que dois faire la miniature (160 par defaut) + * \param maxHeight Hauteur maximum que dois faire la miniature (120 par defaut) + */ function add_thumb($file, $maxWidth = 160, $maxHeight = 120) { require_once(DOL_DOCUMENT_ROOT ."/lib/images.lib.php"); - + if (file_exists($file)) { vignette($file,$maxWidth,$maxHeight); } } - /** - * \brief Deplace fichier recupere sur internet (utilise pour interface avec OSC) - * \param sdir Repertoire destination finale - * \param $files url de l'image - * \author Jean Heimburger juin 2007 - */ - function add_photo_web($sdir, $files) - { - $dir = $sdir .'/'. get_exdir($this->id,2) . $this->id ."/"; - $dir .= "photos/"; + /** + * \brief Deplace fichier recupere sur internet (utilise pour interface avec OSC) + * \param sdir Repertoire destination finale + * \param $files url de l'image + * \author Jean Heimburger juin 2007 + */ + function add_photo_web($sdir, $files) + { + $dir = $sdir .'/'. get_exdir($this->id,2) . $this->id ."/"; + $dir .= "photos/"; - if (! file_exists($dir)) - { - dolibarr_syslog("Product Create $dir"); - create_exdir($dir); - } - - if (file_exists($dir)) - { - // Cree fichier en taille vignette - // \todo A faire - - // Cree fichier en taille origine + if (! file_exists($dir)) + { + dolibarr_syslog("Product Create $dir"); + create_exdir($dir); + } + + if (file_exists($dir)) + { + // Cree fichier en taille vignette + // \todo A faire + + // Cree fichier en taille origine $content = file_get_contents($files); - + $nom = basename($files); $im = fopen($dir.$nom,'wb'); fwrite($im, $content); - fclose($im); -// } - } - } - - /** - * \brief Affiche la premi�re photo du produit - * \param sdir R�pertoire � scanner - * \return boolean true si photo dispo, false sinon - */ - function is_photo_available($sdir) - { - $pdir = get_exdir($this->id,2) . $this->id ."/photos/"; - $dir = $sdir . '/'. $pdir; - - $nbphoto=0; - if (file_exists($dir)) - { - $handle=opendir($dir); - - while (($file = readdir($handle)) != false) - { - if (is_file($dir.$file)) return true; - } - } - return false; - } - - - /** - * \brief Affiche toutes les photos du produit (nbmax maximum) - * \param sdir R�pertoire � scanner - * \param size 0=taille origine, 1 taille vignette - * \param nbmax Nombre maximum de photos (0=pas de max) - * \param nbbyrow Nombre vignettes par ligne (si mode vignette) - * \return int Nombre de photos affich�es - */ - function show_photos($sdir,$size=0,$nbmax=0,$nbbyrow=5) - { - $pdir = get_exdir($this->id,2) . $this->id ."/photos/"; - $dir = $sdir . '/'. $pdir; - $dirthumb = $dir.'thumbs/'; - $pdirthumb = $pdir.'thumbs/'; - - $nbphoto=0; - if (file_exists($dir)) - { - $handle=opendir($dir); - - while (($file = readdir($handle)) != false) - { - $photo=''; - - if (is_file($dir.$file)) - { - $nbphoto++; - $photo = $file; - - if ($size == 1) { // Format vignette - // On determine nom du fichier vignette - $photo_vignette=''; - if (eregi('(\.jpg|\.bmp|\.gif|\.png|\.tiff)$',$photo,$regs)) { - $photo_vignette=eregi_replace($regs[0],'',$photo)."_small".$regs[0]; - } - - - if ($nbbyrow && $nbphoto == 1) print ''; - - if ($nbbyrow && ($nbphoto % $nbbyrow == 1)) print ''; - if ($nbbyrow) print ''; - if ($nbbyrow && ($nbphoto % $nbbyrow == 0)) print ''; - - } - - if ($size == 0) // Format origine - print ''; - - // On continue ou on arrete de boucler ? - if ($nbmax && $nbphoto >= $nbmax) break; - } - } - - if ($nbbyrow && $size==1) - { - // Ferme tableau - while ($nbphoto % $nbbyrow) { - print ''; - $nbphoto++; - } - - if ($nbphoto) print '
'; - - print ''; - - // Si fichier vignette disponible, on l'utilise, sinon on utilise photo origine - if ($photo_vignette && is_file($dirthumb.$photo_vignette)) { - print ''; - } - else { - print ''; - } - - print ''; - - if ($nbbyrow) print '
 
'; - } - - closedir($handle); - } - - return $nbphoto; - } - - /** - * \brief Retourne tableau de toutes les photos du produit - * \param dir R�pertoire � scanner - * \param nbmax Nombre maximum de photos (0=pas de max) - * \return array Tableau de photos - */ - function liste_photos($dir,$nbmax=0) - { - $nbphoto=0; - $tabobj=array(); - - $dirthumb = $dir.'thumbs/'; - - if (file_exists($dir)) - { - $handle=opendir($dir); - - while (($file = readdir($handle)) != false) - { - if (is_file($dir.$file)) - { - $nbphoto++; - $photo = $file; - - // On determine nom du fichier vignette - $photo_vignette=''; - if (eregi('(\.jpg|\.bmp|\.gif|\.png|\.tiff)$',$photo,$regs)) - { - $photo_vignette=eregi_replace($regs[0],'',$photo).'_small'.$regs[0]; - } - - // Objet - $obj=array(); - $obj['photo']=$photo; - if ($photo_vignette && is_file($dirthumb.$photo_vignette)) $obj['photo_vignette']=$photo_vignette; - else $obj['photo_vignette']=""; - - $tabobj[$nbphoto-1]=$obj; - - // On continue ou on arrete de boucler ? - if ($nbmax && $nbphoto >= $nbmax) break; - } - } - - closedir($handle); - } - - return $tabobj; - } - - /** - * \brief Efface la photo du produit et sa vignette - * \param file Chemin de l'image - */ - function delete_photo($file) - { - $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine - $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette - $filename = eregi_replace($dir,'',$file); // Nom du fichier - - // On efface l'image d'origine - unlink($file); - - // Si elle existe, on efface la vignette - if (eregi('(\.jpg|\.bmp|\.gif|\.png|\.tiff)$',$filename,$regs)) - { - $photo_vignette=eregi_replace($regs[0],'',$filename).'_small'.$regs[0]; - if (file_exists($dirthumb.$photo_vignette)) - { - unlink($dirthumb.$photo_vignette); - } - } + fclose($im); + // } + } } - + /** - * \brief R�cup�re la taille de l'image - * \param file Chemin de l'image - */ - function get_image_size($file) - { - $infoImg = getimagesize($file); // R�cup�ration des infos de l'image - $this->imgWidth = $infoImg[0]; // Largeur de l'image - $this->imgHeight = $infoImg[1]; // Hauteur de l'image - } + * \brief Affiche la premi�re photo du produit + * \param sdir R�pertoire � scanner + * \return boolean true si photo dispo, false sinon + */ + function is_photo_available($sdir) + { + $pdir = get_exdir($this->id,2) . $this->id ."/photos/"; + $dir = $sdir . '/'. $pdir; - /** - * \brief Charge indicateurs this->nb de tableau de bord - * \return int <0 si ko, >0 si ok - */ - function load_state_board() - { - global $conf, $user; + $nbphoto=0; + if (file_exists($dir)) + { + $handle=opendir($dir); + + while (($file = readdir($handle)) != false) + { + if (is_file($dir.$file)) return true; + } + } + return false; + } - $this->nb=array(); - $sql = "SELECT count(p.rowid) as nb"; - $sql.= " FROM ".MAIN_DB_PREFIX."product as p"; - if ($conf->categorie->enabled && !$user->rights->categorie->voir) - { - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_product as cp ON cp.fk_product = p.rowid"; - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie as c ON cp.fk_categorie = c.rowid"; - } - $sql.= " WHERE p.fk_product_type <> 1"; - if ($conf->categorie->enabled && !$user->rights->categorie->voir) - { - $sql.= " AND IFNULL(c.visible,1)=1"; - } - $resql=$this->db->query($sql); - if ($resql) - { - while ($obj=$this->db->fetch_object($resql)) - { - $this->nb["products"]=$obj->nb; - } - return 1; - } - else - { - dolibarr_print_error($this->db); - $this->error=$this->db->error(); - return -1; - } - } - - /** - * \brief Mise � jour du code barre - * \param user Utilisateur qui fait la modification - */ - function update_barcode($user) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."product"; + /** + * \brief Affiche toutes les photos du produit (nbmax maximum) + * \param sdir R�pertoire � scanner + * \param size 0=taille origine, 1 taille vignette + * \param nbmax Nombre maximum de photos (0=pas de max) + * \param nbbyrow Nombre vignettes par ligne (si mode vignette) + * \return int Nombre de photos affich�es + */ + function show_photos($sdir,$size=0,$nbmax=0,$nbbyrow=5) + { + $pdir = get_exdir($this->id,2) . $this->id ."/photos/"; + $dir = $sdir . '/'. $pdir; + $dirthumb = $dir.'thumbs/'; + $pdirthumb = $pdir.'thumbs/'; + + $nbphoto=0; + if (file_exists($dir)) + { + $handle=opendir($dir); + + while (($file = readdir($handle)) != false) + { + $photo=''; + + if (is_file($dir.$file)) + { + $nbphoto++; + $photo = $file; + + if ($size == 1) { // Format vignette + // On determine nom du fichier vignette + $photo_vignette=''; + if (eregi('(\.jpg|\.bmp|\.gif|\.png|\.tiff)$',$photo,$regs)) { + $photo_vignette=eregi_replace($regs[0],'',$photo)."_small".$regs[0]; + } + + + if ($nbbyrow && $nbphoto == 1) print ''; + + if ($nbbyrow && ($nbphoto % $nbbyrow == 1)) print ''; + if ($nbbyrow) print ''; + if ($nbbyrow && ($nbphoto % $nbbyrow == 0)) print ''; + + } + + if ($size == 0) // Format origine + print ''; + + // On continue ou on arrete de boucler ? + if ($nbmax && $nbphoto >= $nbmax) break; + } + } + + if ($nbbyrow && $size==1) + { + // Ferme tableau + while ($nbphoto % $nbbyrow) { + print ''; + $nbphoto++; + } + + if ($nbphoto) print '
'; + + print ''; + + // Si fichier vignette disponible, on l'utilise, sinon on utilise photo origine + if ($photo_vignette && is_file($dirthumb.$photo_vignette)) { + print ''; + } + else { + print ''; + } + + print ''; + + if ($nbbyrow) print '
 
'; + } + + closedir($handle); + } + + return $nbphoto; + } + + /** + * \brief Retourne tableau de toutes les photos du produit + * \param dir R�pertoire � scanner + * \param nbmax Nombre maximum de photos (0=pas de max) + * \return array Tableau de photos + */ + function liste_photos($dir,$nbmax=0) + { + $nbphoto=0; + $tabobj=array(); + + $dirthumb = $dir.'thumbs/'; + + if (file_exists($dir)) + { + $handle=opendir($dir); + + while (($file = readdir($handle)) != false) + { + if (is_file($dir.$file)) + { + $nbphoto++; + $photo = $file; + + // On determine nom du fichier vignette + $photo_vignette=''; + if (eregi('(\.jpg|\.bmp|\.gif|\.png|\.tiff)$',$photo,$regs)) + { + $photo_vignette=eregi_replace($regs[0],'',$photo).'_small'.$regs[0]; + } + + // Objet + $obj=array(); + $obj['photo']=$photo; + if ($photo_vignette && is_file($dirthumb.$photo_vignette)) $obj['photo_vignette']=$photo_vignette; + else $obj['photo_vignette']=""; + + $tabobj[$nbphoto-1]=$obj; + + // On continue ou on arrete de boucler ? + if ($nbmax && $nbphoto >= $nbmax) break; + } + } + + closedir($handle); + } + + return $tabobj; + } + + /** + * \brief Efface la photo du produit et sa vignette + * \param file Chemin de l'image + */ + function delete_photo($file) + { + $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine + $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette + $filename = eregi_replace($dir,'',$file); // Nom du fichier + + // On efface l'image d'origine + unlink($file); + + // Si elle existe, on efface la vignette + if (eregi('(\.jpg|\.bmp|\.gif|\.png|\.tiff)$',$filename,$regs)) + { + $photo_vignette=eregi_replace($regs[0],'',$filename).'_small'.$regs[0]; + if (file_exists($dirthumb.$photo_vignette)) + { + unlink($dirthumb.$photo_vignette); + } + } + } + + /** + * \brief R�cup�re la taille de l'image + * \param file Chemin de l'image + */ + function get_image_size($file) + { + $infoImg = getimagesize($file); // R�cup�ration des infos de l'image + $this->imgWidth = $infoImg[0]; // Largeur de l'image + $this->imgHeight = $infoImg[1]; // Hauteur de l'image + } + + /** + * \brief Charge indicateurs this->nb de tableau de bord + * \return int <0 si ko, >0 si ok + */ + function load_state_board() + { + global $conf, $user; + + $this->nb=array(); + + $sql = "SELECT count(p.rowid) as nb"; + $sql.= " FROM ".MAIN_DB_PREFIX."product as p"; + if ($conf->categorie->enabled && !$user->rights->categorie->voir) + { + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_product as cp ON cp.fk_product = p.rowid"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie as c ON cp.fk_categorie = c.rowid"; + } + $sql.= " WHERE p.fk_product_type <> 1"; + if ($conf->categorie->enabled && !$user->rights->categorie->voir) + { + $sql.= " AND IFNULL(c.visible,1)=1"; + } + $resql=$this->db->query($sql); + if ($resql) + { + while ($obj=$this->db->fetch_object($resql)) + { + $this->nb["products"]=$obj->nb; + } + return 1; + } + else + { + dolibarr_print_error($this->db); + $this->error=$this->db->error(); + return -1; + } + } + + /** + * \brief Mise � jour du code barre + * \param user Utilisateur qui fait la modification + */ + function update_barcode($user) + { + $sql = "UPDATE ".MAIN_DB_PREFIX."product"; $sql .= " SET barcode = '".$this->barcode."'"; $sql .= " WHERE rowid = ".$this->id; @@ -2557,14 +2557,14 @@ class Product extends CommonObject return -1; } } - -/** - * \brief Mise � jour du type de code barre - * \param user Utilisateur qui fait la modification - */ - function update_barcode_type($user) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."product"; + + /** + * \brief Mise � jour du type de code barre + * \param user Utilisateur qui fait la modification + */ + function update_barcode_type($user) + { + $sql = "UPDATE ".MAIN_DB_PREFIX."product"; $sql .= " SET fk_barcode_type = '".$this->barcode_type."'"; $sql .= " WHERE rowid = ".$this->id; @@ -2581,31 +2581,31 @@ class Product extends CommonObject } } - /** - \brief Affecte les valeurs smarty - \remarks Rodolphe : pour l'instant la fonction est vide mais necessaire pour compatibilite - avec les canvas A terme la fiche produit utilisera aussi smarty - */ - function assign_smarty_values(&$smarty) - { + /** + \brief Affecte les valeurs smarty + \remarks Rodolphe : pour l'instant la fonction est vide mais necessaire pour compatibilite + avec les canvas A terme la fiche produit utilisera aussi smarty + */ + function assign_smarty_values(&$smarty) + { - } + } - function isproduct() { - if ($this->type != 1) { - return 1; - } else { - return 0; - } - } + function isproduct() { + if ($this->type != 1) { + return 1; + } else { + return 0; + } + } - function isservice() { - if ($this->type==1) { - return 1; - } else { - return 0; - } - } + function isservice() { + if ($this->type==1) { + return 1; + } else { + return 0; + } + } } ?>