dolibarr/htdocs/fourn/class/fournisseur.class.php

226 lines
6.1 KiB
PHP
Raw Normal View History

2005-04-11 17:08:50 +02:00
<?php
2007-07-10 09:07:47 +02:00
/* Copyright (C) 2004-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2006 Laurent Destailleur <eldy@users.sourceforge.net>
2018-10-27 14:43:12 +02:00
* Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
2011-12-30 14:45:07 +01:00
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
2008-10-19 22:34:46 +02:00
*
2005-04-11 17:08:50 +02:00
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2005-04-11 17:08:50 +02:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2005-04-11 17:08:50 +02:00
*/
/**
2010-07-21 13:57:52 +02:00
* \file htdocs/fourn/class/fournisseur.class.php
* \ingroup fournisseur,societe
2010-07-21 13:57:52 +02:00
* \brief File of class to manage suppliers
2008-10-19 22:34:46 +02:00
*/
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
/**
2013-07-13 16:54:17 +02:00
* Class to manage suppliers
2008-10-19 22:34:46 +02:00
*/
class Fournisseur extends Societe
{
public $next_prev_filter = "te.fournisseur = 1"; // Used to add a filter in Form::showrefnav method
2018-04-21 15:08:46 +02:00
2008-10-19 22:34:46 +02:00
/**
* Constructor
*
* @param DoliDB $db Database handler
2008-10-19 22:34:46 +02:00
*/
2019-03-01 23:08:57 +01:00
public function __construct($db)
2008-10-19 22:34:46 +02:00
{
$this->db = $db;
2018-04-21 15:08:46 +02:00
2008-10-19 22:34:46 +02:00
$this->client = 0;
2018-04-21 15:08:46 +02:00
$this->fournisseur = 1;
2008-10-19 22:34:46 +02:00
}
/**
* Return nb of orders
*
* @return int Nb of orders
*/
2019-03-01 23:08:57 +01:00
public function getNbOfOrders()
2008-10-19 22:34:46 +02:00
{
$sql = "SELECT rowid";
$sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as cf";
2021-08-27 18:18:50 +02:00
$sql .= " WHERE cf.fk_soc = ".((int) $this->id);
2008-10-19 22:34:46 +02:00
2010-08-09 18:07:24 +02:00
$resql = $this->db->query($sql);
2021-02-25 23:21:30 +01:00
if ($resql) {
2010-08-09 18:07:24 +02:00
$num = $this->db->num_rows($resql);
2008-10-19 22:34:46 +02:00
2021-02-25 23:21:30 +01:00
if ($num == 1) {
2010-08-09 18:07:24 +02:00
$row = $this->db->fetch_row($resql);
2008-10-19 22:34:46 +02:00
$this->single_open_commande = $row[0];
}
}
return $num;
}
/**
2011-12-30 14:45:07 +01:00
* Returns number of ref prices (not number of products).
*
* @return int Nb of ref prices, or <0 if error
*/
2019-03-01 23:08:57 +01:00
public function nbOfProductRefs()
2008-10-19 22:34:46 +02:00
{
global $conf;
2011-12-30 14:45:07 +01:00
$sql = "SELECT count(pfp.rowid) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql .= " WHERE pfp.entity = ".$conf->entity;
$sql .= " AND pfp.fk_soc = ".((int) $this->id);
2008-10-19 22:34:46 +02:00
$resql = $this->db->query($sql);
2021-02-25 23:21:30 +01:00
if ($resql) {
2011-12-30 14:45:07 +01:00
$obj = $this->db->fetch_object($resql);
return $obj->nb;
2020-05-21 15:05:19 +02:00
} else {
2008-10-19 22:34:46 +02:00
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-19 22:34:46 +02:00
/**
* Load statistics indicators
*
* @return int <0 if KO, >0 if OK
2008-10-19 22:34:46 +02:00
*/
2019-03-01 23:08:57 +01:00
public function load_state_board()
2008-10-19 22:34:46 +02:00
{
// phpcs:enable
global $conf, $user, $hookmanager;
2008-10-19 22:34:46 +02:00
$this->nb = array();
2008-10-19 22:34:46 +02:00
$clause = "WHERE";
$sql = "SELECT count(s.rowid) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
2021-10-22 22:15:59 +02:00
if (empty($user->rights->societe->client->voir) && !$user->socid) {
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
$sql .= " WHERE sc.fk_user = ".((int) $user->id);
2008-10-19 22:34:46 +02:00
$clause = "AND";
}
$sql .= " ".$clause." s.fournisseur = 1";
$sql .= " AND s.entity IN (".getEntity('societe').")";
// Add where from hooks
if (is_object($hookmanager)) {
$parameters = array();
$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
$sql .= $hookmanager->resPrint;
}
$resql = $this->db->query($sql);
2021-02-25 23:21:30 +01:00
if ($resql) {
while ($obj = $this->db->fetch_object($resql)) {
$this->nb["suppliers"] = $obj->nb;
2008-10-19 22:34:46 +02:00
}
$this->db->free($resql);
2008-10-19 22:34:46 +02:00
return 1;
2020-05-21 15:05:19 +02:00
} else {
dol_print_error($this->db);
$this->error = $this->db->error();
2008-10-19 22:34:46 +02:00
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Create a supplier category
*
2011-12-19 17:12:29 +01:00
* @param User $user User asking creation
* @param string $name Category name
* @return int <0 if KO, 0 if OK
2008-10-19 22:34:46 +02:00
*/
2019-03-01 23:08:57 +01:00
public function CreateCategory($user, $name)
{
// phpcs:enable
$sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie (label,visible,type)";
$sql .= " VALUES ";
$sql .= " ('".$this->db->escape($name)."',1,1)";
2014-06-12 11:31:53 +02:00
dol_syslog("Fournisseur::CreateCategory", LOG_DEBUG);
2010-05-21 00:23:58 +02:00
$resql = $this->db->query($sql);
2021-02-25 23:21:30 +01:00
if ($resql) {
dol_syslog("Fournisseur::CreateCategory : Success");
return 0;
2020-05-21 15:05:19 +02:00
} else {
$this->error = $this->db->lasterror();
2010-05-21 00:23:58 +02:00
dol_syslog("Fournisseur::CreateCategory : Failed (".$this->error.")");
return -1;
}
}
2007-07-10 09:07:47 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-19 22:34:46 +02:00
/**
2011-12-19 17:12:29 +01:00
* Return the suppliers list
2008-10-19 22:34:46 +02:00
*
* @return array Array of suppliers
2008-10-19 22:34:46 +02:00
*/
2019-03-01 23:08:57 +01:00
public function ListArray()
2008-10-19 22:34:46 +02:00
{
// phpcs:enable
global $conf;
global $user;
2008-10-19 22:34:46 +02:00
$arr = array();
$sql = "SELECT s.rowid, s.nom as name";
$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
2021-10-22 22:15:59 +02:00
if (empty($user->rights->societe->client->voir) && !$user->socid) {
2021-02-25 23:21:30 +01:00
$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
}
$sql .= " WHERE s.fournisseur = 1";
$sql .= " AND s.entity IN (".getEntity('societe').")";
2021-10-22 22:15:59 +02:00
if (empty($user->rights->societe->client->voir) && !$user->socid) {
$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2021-02-25 23:21:30 +01:00
}
2008-10-19 22:34:46 +02:00
$resql = $this->db->query($sql);
2008-10-19 22:34:46 +02:00
2021-02-25 23:21:30 +01:00
if ($resql) {
while ($obj = $this->db->fetch_object($resql)) {
$arr[$obj->rowid] = $obj->name;
}
2020-05-21 15:05:19 +02:00
} else {
dol_print_error($this->db);
$this->error = $this->db->lasterror();
2008-10-19 22:34:46 +02:00
}
return $arr;
}
2021-03-01 21:56:14 +01:00
/**
* Function used to replace a thirdparty id with another one.
*
* @param DoliDB $db Database handler
* @param int $origin_id Old third-party id
* @param int $dest_id New third-party id
* @return bool
*/
public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
{
$tables = array(
'facture_fourn'
);
return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
}
2005-04-11 17:08:50 +02:00
}