* Vendor Templates, so it posts to the agent handler by its full path. A * client that already has a current vendor made from the template is shown * as having it and is left alone. ?client_ids[]= starts with those clients * picked; ?missing=1 with the clients the template is required of that don't * have it (vendorTemplateCoverage()). */ require_once '../../../includes/modal_header.php'; enforceUserPermission('module_client', 2); $templates = []; $sql = mysqli_query($mysqli, "SELECT vendor_template_id, vendor_template_name, vendor_template_description FROM vendor_templates WHERE vendor_template_archived_at IS NULL ORDER BY vendor_template_name ASC"); while ($row = mysqli_fetch_assoc($sql)) { $templates[] = $row; } if (!$templates) { echo json_encode(['error' => 'There are no vendor templates yet - an admin adds them under Admin > Vendor Templates.']); exit; } $template_id = intval($_GET['template_id'] ?? 0); $template_ids = array_map(function ($t) { return intval($t['vendor_template_id']); }, $templates); if (!in_array($template_id, $template_ids, true)) { $template_id = $template_ids[0]; } // Which clients already have a current vendor from each template $has = []; $sql = mysqli_query($mysqli, "SELECT DISTINCT vendor_template_id, vendor_client_id FROM vendors WHERE vendor_template_id > 0 AND vendor_client_id > 0 AND vendor_archived_at IS NULL"); while ($row = mysqli_fetch_row($sql)) { $has[intval($row[0])][intval($row[1])] = true; } // The clients to start with picked $picked = array_map('intval', array_filter((array) ($_GET['client_ids'] ?? []), 'is_scalar')); if (!empty($_GET['missing'])) { $required_template = vendorTemplateInfo($template_id); if ($required_template && intval($required_template['vendor_template_required'])) { $picked = array_merge($picked, array_keys(vendorTemplateCoverage($template_id, $required_template['vendor_template_required'], array_keys(vendorTemplateRequiredTags([$template_id])[$template_id]))['missing'])); } } $picked = array_flip($picked); $clients = []; $sql = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation FROM clients WHERE client_archived_at IS NULL " . clientScopeSql('clients.client_id') . " ORDER BY client_name ASC"); while ($row = mysqli_fetch_assoc($sql)) { $clients[] = $row; } ob_start(); ?>