grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026.
--------------------------------------------------------------------------
*/
/** @file
* @brief
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
// Relation between Contracts and Suppliers
class Contract_Supplier extends CommonDBRelation {
// From CommonDBRelation
static public $itemtype_1 = 'Contract';
static public $items_id_1 = 'contracts_id';
static public $itemtype_2 = 'Supplier';
static public $items_id_2 = 'suppliers_id';
/**
* @since version 0.84
**/
function getForbiddenStandardMassiveAction() {
$forbidden = parent::getForbiddenStandardMassiveAction();
$forbidden[] = 'update';
return $forbidden;
}
/**
* @param $item string Supplier object
**/
static function countForSupplier(Supplier $item) {
$restrict = "`glpi_contracts_suppliers`.`suppliers_id` = '".$item->getField('id') ."'
AND `glpi_contracts_suppliers`.`contracts_id` = `glpi_contracts`.`id` ".
getEntitiesRestrictRequest(" AND ", "glpi_contracts", '',
$_SESSION['glpiactiveentities']);
return countElementsInTable(array('glpi_contracts_suppliers', 'glpi_contracts'), $restrict);
}
/**
* @param $item string Contract object
**/
static function countForContract(Contract $item) {
$restrict = "`glpi_contracts_suppliers`.`contracts_id` = '".$item->getField('id') ."'
AND `glpi_contracts_suppliers`.`suppliers_id` = `glpi_suppliers`.`id` ".
getEntitiesRestrictRequest(" AND ", "glpi_suppliers", '',
$_SESSION['glpiactiveentities'], true);
return countElementsInTable(array('glpi_contracts_suppliers', 'glpi_suppliers'), $restrict);
}
function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
if (!$withtemplate) {
$nb = 0;
switch ($item->getType()) {
case 'Supplier' :
if (Contract::canView()) {
if ($_SESSION['glpishow_count_on_tabs']) {
$nb = self::countForSupplier($item);
}
return self::createTabEntry(Contract::getTypeName(Session::getPluralNumber()),
$nb);
}
break;
case 'Contract' :
if (Session::haveRight("contact_enterprise", READ)) {
if ($_SESSION['glpishow_count_on_tabs']) {
$nb = self::countForContract($item);
}
return self::createTabEntry(Supplier::getTypeName(Session::getPluralNumber()), $nb);
}
break;
}
}
return '';
}
static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
switch ($item->getType()) {
case 'Supplier' :
self::showForSupplier($item);
break;
case 'Contract' :
self::showForContract($item);
break;
}
return true;
}
/**
* Print an HTML array with contracts associated to the enterprise
*
* @since version 0.84
*
* @param $supplier Supplier object
*
* @return Nothing (display)
**/
static function showForSupplier(Supplier $supplier) {
global $DB, $CFG_GLPI;
$ID = $supplier->fields['id'];
if (!Contract::canView()
|| !$supplier->can($ID, READ)) {
return false;
}
$canedit = $supplier->can($ID, UPDATE);
$rand = mt_rand();
$query = "SELECT `glpi_contracts`.*,
`glpi_contracts_suppliers`.`id` AS assocID,
`glpi_entities`.`id` AS entity
FROM `glpi_contracts_suppliers`, `glpi_contracts`
LEFT JOIN `glpi_entities` ON (`glpi_entities`.`id`=`glpi_contracts`.`entities_id`)
WHERE `glpi_contracts_suppliers`.`suppliers_id` = '$ID'
AND `glpi_contracts_suppliers`.`contracts_id`=`glpi_contracts`.`id`".
getEntitiesRestrictRequest(" AND", "glpi_contracts", '', '', true)."
ORDER BY `glpi_entities`.`completename`,
`glpi_contracts`.`name`";
$result = $DB->query($query);
$contracts = array();
$used = array();
if ($number = $DB->numrows($result)) {
while ($data = $DB->fetch_assoc($result)) {
$contracts[$data['assocID']] = $data;
$used[$data['id']] = $data['id'];
}
}
if ($canedit) {
echo "
";
}
echo "";
if ($canedit && $number) {
Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
$massiveactionparams = array('container' => 'mass'.__CLASS__.$rand,
'num_displayed' => min($_SESSION['glpilist_limit'], $number));
Html::showMassiveActions($massiveactionparams);
}
echo "
";
$header_begin = "";
$header_top = '';
$header_bottom = '';
$header_end = '';
if ($canedit && $number) {
$header_top .= "| ".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header_top .= " | ";
$header_bottom .= "".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header_bottom .= " | ";
}
$header_end .= "".__('Name')." | ";
$header_end .= "".__('Entity')." | ";
$header_end .= ""._x('phone', 'Number')." | ";
$header_end .= "".__('Contract type')." | ";
$header_end .= "".__('Start date')." | ";
$header_end .= "".__('Initial contract period')." | ";
$header_end .= "
";
echo $header_begin.$header_top.$header_end;
$used = array();
foreach ($contracts as $data) {
$cID = $data["id"];
$used[$cID] = $cID;
$assocID = $data["assocID"];
echo "";
if ($canedit) {
echo "| ";
Html::showMassiveActionCheckBox(__CLASS__, $data["assocID"]);
echo " | ";
}
$name = $data["name"];
if ($_SESSION["glpiis_ids_visible"]
|| empty($data["name"])) {
$name = sprintf(__('%1$s (%2$s)'), $name, $data["id"]);
}
echo "
".$name."";
echo " | ";
echo "".Dropdown::getDropdownName("glpi_entities", $data["entity"]);
echo " | ".$data["num"]." | ";
echo "".
Dropdown::getDropdownName("glpi_contracttypes",$data["contracttypes_id"])." | ";
echo "".Html::convDate($data["begin_date"])." | ";
echo "";
sprintf(_n('%d month', '%d months', $data["duration"]), $data["duration"]);
if (($data["begin_date"] != '') && !empty($data["begin_date"])) {
echo " -> ".Infocom::getWarrantyExpir($data["begin_date"], $data["duration"], 0, true);
}
echo " | ";
echo "
";
}
if ($number) {
echo $header_begin.$header_bottom.$header_end;
}
echo "
";
if ($canedit && $number) {
$massiveactionparams['ontop'] =false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
echo "
";
}
/**
* Print the HTML array of suppliers for this contract
*
* @since version 0.84
*
* @param $contract Contract object
*
* @return Nothing (HTML display)
**/
static function showForContract(Contract $contract) {
global $DB, $CFG_GLPI;
$instID = $contract->fields['id'];
if (!$contract->can($instID, READ)
|| !Session::haveRight("contact_enterprise", READ)) {
return false;
}
$canedit = $contract->can($instID, UPDATE);
$rand = mt_rand();
$query = "SELECT `glpi_contracts_suppliers`.`id`,
`glpi_suppliers`.`id` AS entID,
`glpi_suppliers`.`name` AS name,
`glpi_suppliers`.`website` AS website,
`glpi_suppliers`.`phonenumber` AS phone,
`glpi_suppliers`.`suppliertypes_id` AS type,
`glpi_entities`.`id` AS entity
FROM `glpi_contracts_suppliers`,
`glpi_suppliers`
LEFT JOIN `glpi_entities` ON (`glpi_entities`.`id`=`glpi_suppliers`.`entities_id`)
WHERE `glpi_contracts_suppliers`.`contracts_id` = '$instID'
AND `glpi_contracts_suppliers`.`suppliers_id`=`glpi_suppliers`.`id`".
getEntitiesRestrictRequest(" AND","glpi_suppliers",'','',true). "
ORDER BY `glpi_entities`.`completename`, `name`";
$result = $DB->query($query);
$suppliers = array();
$used = array();
if ($number = $DB->numrows($result)) {
while ($data = $DB->fetch_assoc($result)) {
$suppliers[$data['id']] = $data;
$used[$data['entID']] = $data['entID'];
}
}
if ($canedit) {
echo "";
}
echo "";
if ($canedit && $number) {
Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
$massiveactionparams = array('num_displayed' => min($_SESSION['glpilist_limit'], $number),
'container' => 'mass'.__CLASS__.$rand);
Html::showMassiveActions($massiveactionparams);
}
echo "
";
$header_begin = "";
$header_top = '';
$header_bottom = '';
$header_end = '';
if ($canedit && $number) {
$header_top .= "| ".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header_top .= " | ";
$header_bottom .= "".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header_bottom .= " | ";
}
$header_end .= "".__('Supplier')." | ";
$header_end .= "".__('Entity')." | ";
$header_end .= "".__('Third party type')." | ";
$header_end .= "".__('Phone')." | ";
$header_end .= "".__('Website')." | ";
$header_end .= "
";
echo $header_begin.$header_top.$header_end;
$used = array();
foreach ($suppliers as $data) {
$ID = $data['id'];
$website = $data['website'];
if (!empty($website)) {
if (!preg_match("?https*://?",$website)) {
$website = "http://".$website;
}
$website = "".$data['website']."";
}
$entID = $data['entID'];
$entity = $data['entity'];
$used[$entID] = $entID;
$entname = Dropdown::getDropdownName("glpi_suppliers", $entID);
echo "";
if ($canedit) {
echo "| ";
Html::showMassiveActionCheckBox(__CLASS__, $data["id"]);
echo " | ";
}
echo "";
if ($_SESSION["glpiis_ids_visible"]
|| empty($entname)) {
$entname = sprintf(__('%1$s (%2$s)'), $entname, $entID);
}
echo "".$entname;
echo " | ";
echo "".Dropdown::getDropdownName("glpi_entities",$entity)." | ";
echo "";
echo Dropdown::getDropdownName("glpi_suppliertypes", $data['type'])." | ";
echo "".$data['phone']." | ";
echo "".$website." | ";
echo "
";
}
if ($number) {
echo $header_begin.$header_bottom.$header_end;
}
echo "
";
if ($canedit && $number) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
echo "
";
}
}
?>