// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
global $config;
if (check_login() != 0) {
    audit_db("Noauth", $config["REMOTE_ADDR"], "No authenticated access", "Trying to access inventory viewer");
    require "general/noaccess.php";
    exit;
}
$id = (int) get_parameter('id');
if (!give_acl($config['id_user'], get_inventory_group($id), 'VR')) {
    audit_db($config['id_user'], $config["REMOTE_ADDR"], "ACL Violation", "Trying to access to inventory " . $id);
    include "general/noaccess.php";
    return;
}
echo '<h3>' . __('Contract details on inventory object') . ' #' . $id . '</h3>';
$contracts = get_inventory_contracts($id, false);
$table->class = 'inventory-contracts databox';
$table->width = '740px';
$table->colspan = array();
$table->colspan[1][1] = 3;
$table->style = array();
$table->style[0] = 'font-weight: bold';
$table->style[2] = 'font-weight: bold';
foreach ($contracts as $contract) {
    $table->data = array();
    $table->id = 'inventory-contracts-table-' . $contract['id'];
    $table->data[0][0] = __('Company');
    $table->data[0][1] = get_db_value('name', 'tcompany', 'id', $contract['id_company']);
    $table->data[0][2] = __('Contract');
    $table->data[0][3] = $contract['name'];
    $table->data[1][0] = __('Description');
/**
 * Get all the contacts relative to an inventory object.
 *
 * There are two ways to get the list. By default, all the contacts in the
 * company that has the inventory contract will be returned. Anyway, if the
 * contacts list was changed manually when updating or creating the
 * inventory object, then these are the contacts of the object.
 *
 * @param int Inventory id.
 * @param bool Whether to return only contact names (default) or all the fields.
 *
 * @return array List of contacts relative to an inventory object.
 */
function get_inventory_contacts($id_inventory, $only_names = false)
{
    global $config;
    include_once "include/functions_crm.php";
    /* First try to get only defined contacts */
    $owner = get_db_value('owner', 'tinventory', 'id', $id_inventory);
    $owner_info = get_db_row("tusuario", "id_usuario", $owner);
    $all_contacts = array();
    $contact = array("id" => $owner, "type" => "user", "id_company" => $owner_info["id_company"], "fullname" => $owner_info["nombre_real"], "email" => $owner_info["direccion"], "phone" => $owner_info["telefono"], "mobile" => __("N/A"), "position" => __("N/A"), "description" => $owner_info["comentarios"], "disabled" => $owner_info["disabled"]);
    $all_contacts[$contact["id"]] = $contact;
    //Get all users associated to the inventory object
    $inv_users = enterprise_hook('inventory_get_users', array($id_inventory, false));
    if ($inv_users === ENTERPRISE_NOT_HOOK) {
        $inv_users = array();
    }
    foreach ($inv_users as $user) {
        $contact = array("id" => $user["id_usuario"], "type" => "user", "id_company" => $user["id_company"], "fullname" => $user["nombre_real"], "email" => $user["direccion"], "phone" => $user["telefono"], "mobile" => __("N/A"), "position" => __("N/A"), "description" => $user["comentarios"], "disabled" => $user["disabled"]);
        $all_contacts[$contact["id"]] = $contact;
    }
    $inv_companies = enterprise_hook('inventory_get_companies', array($id_inventory, false));
    if ($inv_companies === ENTERPRISE_NOT_HOOK) {
        $inv_companies = array();
    }
    foreach ($inv_companies as $comp) {
        $where_clause = sprintf("WHERE id_company = %d", $comp["id"]);
        $contacts = crm_get_all_contacts($where_clause);
        if (!$contacts) {
            $contacts = array();
        }
        foreach ($contacts as $contact) {
            $all_contacts[$contact['id']] = $contact;
        }
    }
    $contracts = get_inventory_contracts($id_inventory, false);
    if ($contracts === false) {
        return array();
    }
    foreach ($contracts as $contract) {
        $company = get_company($contract['id_company']);
        if ($company === false) {
            continue;
        }
        if (!give_acl($config['id_user'], $contract['id_group'], "IR")) {
            continue;
        }
        $contacts = get_company_contacts($company['id'], false);
        foreach ($contacts as $contact) {
            if (isset($all_contacts[$contact['id']])) {
                continue;
            }
            $all_contacts[$contact['id']] = $contact;
        }
    }
    if (!$only_names) {
        return $all_contacts;
    }
    $retval = array();
    foreach ($all_contacts as $contact) {
        $retval[$contact['id']] = $contact['fullname'];
    }
    return $retval;
}