dolibarr/htdocs/core/class/html.formcontract.class.php

216 lines
7.7 KiB
PHP
Raw Normal View History

<?php
/* Copyright (C) 2012-2026 Charlene BENKE <charlene@patas-monkey.com>
2025-01-20 23:33:58 +00:00
* Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
*
* 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
* (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 19:55:30 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* or see https://www.gnu.org/
*/
/**
* \file htdocs/core/class/html.formcontract.class.php
2013-06-10 17:04:04 +00:00
* \ingroup core
* \brief File of class with all html predefined components
*/
/**
2013-06-10 17:04:04 +00:00
* Class to manage generation of HTML components for contract module
*/
2013-06-10 17:04:04 +00:00
class FormContract
{
/**
* @var DoliDB Database handler.
*/
public $db;
/**
2018-08-22 08:37:16 +00:00
* @var string Error code (or message)
*/
public $error = '';
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
}
2013-06-10 17:04:04 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Show a combo list with contracts qualified for a third party
* TODO This is a bugged function. It downloads all contracts into the select hanging the browser on large database.
* To allow its use, you must set the constant CONTRACT_CAN_USE_THE_BUGGED_SELECT_COMPONENT.
*
* @param int $socid Id third party (-1=all, 0=only contracts not linked to a third party, id=contracts not linked or linked to third party id)
* @param int $selected Id contract preselected
* @param string $htmlname Nom de la zone html
* @param int $maxlength Maximum length of label
2013-06-10 17:04:04 +00:00
* @param int $showempty Show empty line
* @param int $showRef Show customer and supplier reference on each contract (when found)
* @param int $noouput 1=Return the output instead of display
* @param string $morecss More CSS
* @return int|string If nooutput = 0: Nbr of contract if OK, <0 if KO, If nooutput = 1: The HTML select string
*/
2024-01-14 19:53:22 +00:00
public function select_contract($socid = -1, $selected = 0, $htmlname = 'contrattid', $maxlength = 16, $showempty = 1, $showRef = 0, $noouput = 0, $morecss = 'minwidth150')
{
// phpcs:enable
global $user, $conf, $langs;
2013-06-10 17:04:04 +00:00
$hideunselectables = false;
2023-11-27 10:24:19 +00:00
if (getDolGlobalString('CONTRACT_HIDE_UNSELECTABLES')) {
2021-02-23 21:03:23 +00:00
$hideunselectables = true;
}
2013-06-10 17:04:04 +00:00
$ret = '';
// Search all contacts
2026-04-27 21:21:13 +00:00
$sql = "SELECT c.rowid, c.ref, c.fk_soc, c.statut as status, s.nom,";
$sql .= " c.ref_customer, c.ref_supplier";
$sql .= " FROM ".$this->db->prefix()."contrat as c";
2026-02-23 18:31:44 +00:00
$sql .= " INNER JOIN ".$this->db->prefix()."societe as s ON s.rowid = c.fk_soc";
$sql .= " WHERE c.entity = ".((int) $conf->entity);
2021-03-22 11:47:23 +00:00
//if ($contratListId) $sql.= " AND c.rowid IN (".$this->db->sanitize($contratListId).")";
2021-02-23 21:03:23 +00:00
if ($socid > 0) {
2018-05-13 14:59:03 +00:00
// CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY is 'all' or a list of ids separated by coma.
2023-11-27 10:24:19 +00:00
if (!getDolGlobalString('CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY')) {
2021-08-23 16:56:46 +00:00
$sql .= " AND (c.fk_soc=".((int) $socid)." OR c.fk_soc IS NULL)";
2023-12-13 14:20:53 +00:00
} elseif (getDolGlobalString('CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY') != 'all') {
$sql .= " AND (c.fk_soc IN (".$this->db->sanitize(((int) $socid).",".getDolGlobalInt('CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY')).")";
$sql .= " OR c.fk_soc IS NULL)";
}
2018-08-13 15:26:32 +00:00
}
2021-02-23 21:03:23 +00:00
if ($socid == 0) {
$sql .= " AND (c.fk_soc = 0 OR c.fk_soc IS NULL)";
}
$sql .= " ORDER BY c.ref ";
2013-06-10 17:04:04 +00:00
2014-06-12 09:31:53 +00:00
dol_syslog(get_class($this)."::select_contract", LOG_DEBUG);
2020-09-19 21:30:29 +00:00
$resql = $this->db->query($sql);
2021-02-23 21:03:23 +00:00
if ($resql) {
2024-04-24 00:56:04 +00:00
$ret .= '<select class="flat'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'" id="'.$htmlname.'">';
2021-02-23 21:03:23 +00:00
if ($showempty) {
2026-04-27 21:21:13 +00:00
$ret .= '<option value="0">&nbsp;</option>'."\n";
2021-02-23 21:03:23 +00:00
}
2020-09-19 21:30:29 +00:00
$num = $this->db->num_rows($resql);
$i = 0;
2021-02-23 21:03:23 +00:00
if ($num) {
while ($i < $num) {
2020-09-19 21:30:29 +00:00
$obj = $this->db->fetch_object($resql);
// If we ask to filter on a company and user has no permission to see all companies and project is linked to another company, we hide project.
2022-09-10 09:14:25 +00:00
if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && !$user->hasRight('societe', 'lire')) {
// Do nothing
2020-05-21 13:05:19 +00:00
} else {
$labeltoshow = dol_trunc($obj->ref, 18);
2021-02-23 21:03:23 +00:00
if ($showRef) {
$labeltoshow = $labeltoshow." - ".$obj->nom;
2021-02-23 21:03:23 +00:00
if ($obj->ref_customer) {
$labeltoshow = $labeltoshow." - ".$obj->ref_customer;
}
if ($obj->ref_supplier) {
$labeltoshow = $labeltoshow." - ".$obj->ref_supplier;
}
}
//if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
//else $labeltoshow.=' ('.$langs->trans("Private").')';
2026-04-27 21:21:13 +00:00
if (!empty($selected) && $selected == $obj->rowid && $obj->status > 0) {
$ret .= '<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
2020-05-21 13:05:19 +00:00
} else {
$disabled = 0;
2026-04-27 21:21:13 +00:00
if ($obj->status == 0) {
$disabled = 1;
$labeltoshow .= ' ('.$langs->transnoentitiesnoconv("Draft").')';
}
2023-11-27 10:24:19 +00:00
if (!getDolGlobalString('CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY') && $socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
$disabled = 1;
$labeltoshow .= ' - '.$langs->transnoentitiesnoconv("LinkedToAnotherCompany");
}
2013-06-10 17:04:04 +00:00
2021-02-23 21:03:23 +00:00
if ($hideunselectables && $disabled) {
$resultat = '';
2020-05-21 13:05:19 +00:00
} else {
$resultat = '<option value="'.$obj->rowid.'"';
2021-02-23 21:03:23 +00:00
if ($disabled) {
$resultat .= ' disabled';
}
//if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
//else $labeltoshow.=' ('.$langs->trans("Private").')';
$resultat .= '>'.dolPrintHTML($labeltoshow);
2026-04-27 21:21:13 +00:00
$resultat .= '</option>'."\n";
}
$ret .= $resultat;
}
}
$i++;
}
}
$ret .= '</select>';
2020-09-19 21:30:29 +00:00
$this->db->free($resql);
2018-08-13 15:26:32 +00:00
2021-02-23 21:03:23 +00:00
if (!empty($conf->use_javascript_ajax)) {
2017-04-07 12:55:59 +00:00
// Make select dynamic
include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
$ret .= ajax_combobox($htmlname);
}
if ($noouput) {
return $ret;
2017-04-07 12:55:59 +00:00
}
2018-08-13 15:26:32 +00:00
print $ret;
return $num;
2020-05-21 13:05:19 +00:00
} else {
2020-09-19 21:30:29 +00:00
dol_print_error($this->db);
return -1;
}
}
/**
* Show a form to select a contract
*
2025-01-20 23:33:58 +00:00
* @param string $page Page
* @param int $socid Id third party (-1=all, 0=only contracts not linked to a third party, id=contracts not linked or linked to third party id)
* @param int $selected Id contract preselected
* @param string $htmlname Nom de la zone html
* @param int $maxlength Maximum length of label
* @param int $showempty Show empty line
* @param int $showRef Show customer and supplier reference on each contract (when found)
2025-01-20 23:33:58 +00:00
* @param int<0,1> $nooutput 1=Return the output instead of display
2023-09-04 14:38:27 +00:00
* @return string|void html string
*/
2025-01-20 23:33:58 +00:00
public function formSelectContract($page, $socid = -1, $selected = 0, $htmlname = 'contrattid', $maxlength = 16, $showempty = 1, $showRef = 0, $nooutput = 0)
{
global $langs;
2023-09-04 14:38:27 +00:00
$ret = '<form method="post" action="'.$page.'">';
$ret .= '<input type="hidden" name="action" value="setcontract">';
$ret .= '<input type="hidden" name="token" value="'.newToken().'">';
$ret .= $this->select_contract($socid, $selected, $htmlname, $maxlength, $showempty, $showRef, 1);
$ret .= '<input type="submit" class="button smallpaddingimp valignmiddle" value="'.$langs->trans("Modify").'">';
$ret .= '</form>';
2025-01-20 23:33:58 +00:00
if ($nooutput) {
return $ret;
}
print $ret;
}
}