public static function quote_intake($atts){
$a = shortcode_atts([
‘sheet’ => ‘SxS_FLAT’, // initial view label → sheet map below
‘rate_set’ => ‘2025-Q1’, // fallback; real rate_set will be derived from effective when possible
], $atts);
/* ———- Load counties/areas from CSV (TX-only) ———- */
$COUNTY_OPTIONS = [];
$COUNTY_AREA_MAP = [];
try {
$upload = function_exists(‘wp_get_upload_dir’) ? wp_get_upload_dir() : [‘basedir’ => WP_CONTENT_DIR . ‘/uploads’];
$basedir = rtrim($upload[‘basedir’], ‘/\\’);
$candidates = [
$basedir . ‘/stq-data/county_areas_tx.csv’,
$basedir . ‘/stq-data/county_areas.csv’, // fallback if you rename later
];
foreach ($candidates as $csvPath) {
if (!is_readable($csvPath)) continue;
if (($fh = fopen($csvPath, ‘r’)) === false) continue;
$hdr = fgetcsv($fh);
if (!$hdr) { fclose($fh); continue; }
$idx = array_change_key_case(array_flip($hdr), CASE_LOWER);
$col_county = $idx[‘county’] ?? null;
$col_area = $idx[‘area’] ?? $idx[‘rating area’] ?? $idx[‘rating_area’] ?? null;
if ($col_county === null || $col_area === null) { fclose($fh); continue; }
while (($row = fgetcsv($fh)) !== false) {
$co = trim((string)$row[$col_county]);
$ar = (int)trim((string)$row[$col_area]);
if ($co !== ” && $ar > 0) $COUNTY_AREA_MAP[$co] = $ar;
}
fclose($fh);
break; // first good file wins
}
} catch (\Throwable $e) { /* ignore and fall back */ }
if (empty($COUNTY_AREA_MAP)) {
// let theme provide a fallback if CSV not found
$COUNTY_OPTIONS = apply_filters(‘stq_county_options’, []);
$COUNTY_AREA_MAP = apply_filters(‘stq_county_area_map’, []);
} else {
$COUNTY_OPTIONS = array_keys($COUNTY_AREA_MAP);
}
/* ———- Effective dates (1st/15th only) ———- */
$EFFECTIVE_OPTIONS = apply_filters(‘stq_effective_options’, []);
if (empty($EFFECTIVE_OPTIONS) && class_exists(‘STQ_Calc’) && method_exists(‘STQ_Calc’,’effective_options’)) {
$EFFECTIVE_OPTIONS = (array) STQ_Calc::effective_options(); // expected [‘YYYY-MM-DD’,… only 1st/15th]
}
if (empty($EFFECTIVE_OPTIONS)) {
$out = [];
// figure out current quarter
$y = (int)date(‘Y’);
$m = (int)date(‘n’);
$q = intdiv(($m-1),3) + 1; // 1–4
$qStartMonth = ($q-1)*3 + 1; // 1,4,7,10
// build current quarter dates
for ($i=0;$i<3;$i++) {
$mm = $qStartMonth + $i;
$out[] = sprintf(‘%04d-%02d-01’,$y,$mm);
$out[] = sprintf(‘%04d-%02d-15’,$y,$mm);
}
// build next quarter dates (only if that rate set exists)
$nextQ = ($q==4)?1:$q+1;
$nextY = ($q==4)?$y+1:$y;
$nextQStartMonth = ($nextQ-1)*3 + 1;
$nextRateSet = sprintf(‘%04dQ%d’,$nextY,$nextQ);
if (class_exists(‘STQ_Calc’) && method_exists(‘STQ_Calc’,’rate_set_exists’) && STQ_Calc::rate_set_exists($nextRateSet)) {
for ($i=0;$i<3;$i++) {
$mm = $nextQStartMonth + $i;
$out[] = sprintf(‘%04d-%02d-01’,$nextY,$mm);
$out[] = sprintf(‘%04d-%02d-15’,$nextY,$mm);
}
}
$EFFECTIVE_OPTIONS = $out;
}
// TEMP: lock the intake menu to 2025 Q1 (Jan/Feb/Mar, 1st & 15th)
$EFFECTIVE_OPTIONS = [
‘2025-01-01′,’2025-01-15’,
‘2025-02-01′,’2025-02-15’,
‘2025-03-01′,’2025-03-15’,
];
/* ———- Derivers ———- */
$rate_set_for_effective = function($effective, $defaultRS){
if (class_exists(‘STQ_Calc’) && method_exists(‘STQ_Calc’,’rate_set_for_effective’)) {
$rs = STQ_Calc::rate_set_for_effective($effective);
if (!empty($rs)) return $rs;
}
return $defaultRS;
};
$area_for = function($rate_set, $county) use ($COUNTY_AREA_MAP){
if (class_exists(‘STQ_Calc’) && method_exists(‘STQ_Calc’,’rating_area_for_county’)) {
$d = STQ_Calc::rating_area_for_county($rate_set, $county);
if (!empty($d)) return intval($d);
}
if ($county && isset($COUNTY_AREA_MAP[$county])) return intval($COUNTY_AREA_MAP[$county]);
return 1;
};
/* ———- Carriers & renewal plan list ———- */
$CARRIERS = [‘BCBSTX’=>’BCBSTX’,’BSW’=>’BSW’,’UHC’=>’UHC’,’MH’=>’Memorial Hermann (MH)’];
$plans_by_carrier = [];
if (class_exists(‘STQ_Calc’)) {
global $wpdb;
$rate_set_id = STQ_Calc::rate_set_id($a[‘rate_set’]);
$rows = $wpdb->get_results($wpdb->prepare(
“SELECT r.carrier, r.plan_code, p.marketing_name
FROM {$wpdb->prefix}stq_plan_rates r
JOIN {$wpdb->prefix}stq_plans p
ON p.year=%d AND p.carrier=r.carrier AND p.plan_code=r.plan_code
WHERE r.rate_set_id=%d
GROUP BY r.carrier, r.plan_code, p.marketing_name
ORDER BY r.carrier ASC, CAST(SUBSTRING(r.plan_code,2) AS UNSIGNED) ASC”,
2025, $rate_set_id
), ARRAY_A);
foreach ((array)$rows as $row){
$c = (string)$row[‘carrier’];
$plans_by_carrier[$c][] = [‘plan_code’=>$row[‘plan_code’], ‘marketing_name’=>$row[‘marketing_name’]];
}
}
/* ———- View labels → sheet ———- */
$VIEW_MAP = [
‘Side-by-side’ => ‘SxS_FLAT’,
‘Runtime (Lite)’ => ‘SxS_RUNTIME’,
];
$initial_view_label = array_search($a[‘sheet’], $VIEW_MAP, true);
if ($initial_view_label===false) $initial_view_label = ‘Side-by-side’;
/* ———- Handle submit ———- */
if ($_SERVER[‘REQUEST_METHOD’]===’POST’ && ( isset($_POST[‘stq_intake_submit’]) || isset($_POST[‘stq_compare_submit’]) )) {
$employer = sanitize_text_field($_POST[’employer’] ?? ”);
$state = strtoupper(sanitize_text_field($_POST[‘state’] ?? ($GLOBALS[‘__STQ_STATE_NOW__’] ?? ‘TX’)));
$county = sanitize_text_field($_POST[‘county’] ?? ”);
$effective = sanitize_text_field($_POST[‘effective’]?? ”);
$how = sanitize_text_field($_POST[‘how’] ?? ‘both’);
$rate_style = sanitize_text_field($_POST[‘rate_style’] ?? ‘composite’);
$sheetLbl = sanitize_text_field($_POST[‘sheet_label’] ?? $initial_view_label);
$sheet = $VIEW_MAP[$sheetLbl] ?? $a[‘sheet’];
$rate_set = $rate_set_for_effective($effective, $a[‘rate_set’]);
// area from selected county within selected state map (fallback to helper)
$map_by_state = $GLOBALS[‘__STQ_COUNTY_MAP_BY_STATE__’] ?? [];
$area = 1;
if (!empty($map_by_state[$state]) && !empty($map_by_state[$state][$county])) {
$area = intval($map_by_state[$state][$county]);
} else {
$area = $area_for($rate_set, $county);
}
// renewal (optional)
$renewal = [
‘carrier’ => sanitize_text_field($_POST[‘renewal_carrier’] ?? ”),
‘plan’ => sanitize_text_field($_POST[‘renewal_plan’] ?? ”),
‘eo’ => floatval($_POST[‘renewal_eo’] ?? 0),
‘EO’ => intval($_POST[‘cnt_eo’] ?? 0),
‘ES’ => intval($_POST[‘cnt_es’] ?? 0),
‘EC’ => intval($_POST[‘cnt_ec’] ?? 0),
‘EF’ => intval($_POST[‘cnt_ef’] ?? 0),
];
// If a census was uploaded, prefer its counts (unifies manual vs census path)
$counts_from_csv = self::parse_counts_from_upload();
if (is_array($counts_from_csv) && !empty($counts_from_csv[‘TOTAL’])) {
$renewal[‘EO’] = intval($counts_from_csv[‘EO’] ?? $renewal[‘EO’]);
$renewal[‘ES’] = intval($counts_from_csv[‘ES’] ?? $renewal[‘ES’]);
$renewal[‘EC’] = intval($counts_from_csv[‘EC’] ?? $renewal[‘EC’]);
$renewal[‘EF’] = intval($counts_from_csv[‘EF’] ?? $renewal[‘EF’]);
}
// Render the picker, carrying fields forward (JS appends hidden inputs)
echo ‘<div id=”stq-picker-anchor”></div>’;
// Small confirmation strip (employer/effective/area/rating + counts if any)
echo ‘<div style=”margin:8px 0 12px 0;padding:10px;border:1px solid #e5e7eb;border-radius:6px;background:#fafafa”>’;
echo ‘<div><strong>Employer:</strong> ‘.esc_html($employer ?: ‘(not set)’).'</div>’;
echo ‘<div><strong>Effective:</strong> ‘.esc_html($effective ?: ‘(not set)’).’ <strong>Area:</strong> ‘.esc_html($area ?: ‘(n/a)’).’ <strong>Rating:</strong> ‘.esc_html($rate_style ?: ‘(n/a)’).'</div>’;
if (!empty($renewal[‘EO’]) || !empty($renewal[‘ES’]) || !empty($renewal[‘EC’]) || !empty($renewal[‘EF’])) {
$tot = intval($renewal[‘EO’]) + intval($renewal[‘ES’]) + intval($renewal[‘EC’]) + intval($renewal[‘EF’]);
echo ‘<div style=”opacity:.9;margin-top:4px”>Counts → EO ‘.intval($renewal[‘EO’]).’ / ES ‘.intval($renewal[‘ES’]).’ / EC ‘.intval($renewal[‘EC’]).’ / EF ‘.intval($renewal[‘EF’]).’ (Total ‘.$tot.’)</div>’;
}
echo ‘</div>’;
// Render the picker (now also passes rate_style)
echo do_shortcode(
‘
);
?>
<script>
// ===== STQ intake → picker hidden fields injection =====
(function(){
function input(n,v){ const i=document.createElement(‘input’); i.type=’hidden’; i.name=n; i.value=v; return i; }
const btn = document.querySelector(‘button[name=”stq_compare_submit”]’);
if (!btn || !btn.form) return;
const f = btn.form;
f.appendChild(input(’employer’, <?php echo json_encode($employer); ?>));
f.appendChild(input(‘state’, <?php echo json_encode($state); ?>));
f.appendChild(input(‘county’, <?php echo json_encode($county); ?>));
f.appendChild(input(‘sheet’, <?php echo json_encode($sheet); ?>));
f.appendChild(input(‘rate_set’, <?php echo json_encode($rate_set); ?>));
f.appendChild(input(‘effective’, <?php echo json_encode($effective); ?>));
f.appendChild(input(‘area’, <?php echo json_encode($area); ?>));
f.appendChild(input(‘how’, <?php echo json_encode($how); ?>));
f.appendChild(input(‘rate_style’, <?php echo json_encode($rate_style); ?>));
f.appendChild(input(‘renewal_carrier’, <?php echo json_encode($renewal[‘carrier’]); ?>));
f.appendChild(input(‘renewal_plan’, <?php echo json_encode($renewal[‘plan’]); ?>));
f.appendChild(input(‘renewal_eo’, <?php echo json_encode($renewal[‘eo’]); ?>));
f.appendChild(input(‘cnt_eo’, <?php echo json_encode($renewal[‘EO’]); ?>));
f.appendChild(input(‘cnt_es’, <?php echo json_encode($renewal[‘ES’]); ?>));
f.appendChild(input(‘cnt_ec’, <?php echo json_encode($renewal[‘EC’]); ?>));
f.appendChild(input(‘cnt_ef’, <?php echo json_encode($renewal[‘EF’]); ?>));
})();
</script>
<?php
return ”;
}
/* ———- Render intake form ———- */
ob_start();
?>
<style>
.stq-grid{display:grid;grid-template-columns:1fr 1fr;gap:16px;}
.stq-card{border:1px solid #e2e4e7;padding:14px;border-radius:10px;margin:12px 0;}
.stq-row{display:grid;grid-template-columns:1fr 1fr;gap:12px;align-items:end;}
.stq-row-3{display:grid;grid-template-columns:1fr 1fr 1fr;gap:12px;align-items:end;}
.stq-row-4{display:grid;grid-template-columns:repeat(4,1fr);gap:12px;align-items:end;}
@media (max-width: 860px){
.stq-grid{grid-template-columns:1fr;}
.stq-row,.stq-row-3,.stq-row-4{grid-template-columns:1fr;}
}
.stq-wide input,.stq-wide select{width:100%;}
.stq-label{font-weight:600;margin-bottom:6px;display:block;}
.stq-muted{color:#6b7280;font-size:12px;margin-top:4px;}
.stq-btn-primary{background:#d7263d;color:#fff;border:none;border-radius:8px;padding:12px 18px;font-weight:700;cursor:pointer;}
.stq-radios{display:flex;gap:28px;align-items:center;flex-wrap:wrap;margin:6px 0 10px;}
.stq-radio{display:flex;gap:8px;align-items:center;}
.stq-radio span{white-space:nowrap;}
</style>
<form method=”post” enctype=”multipart/form-data” class=”stq-wide”>
<div class=”stq-grid”>
<div>
<label class=”stq-label”>Employer Name</label>
<input type=”text” name=”employer” placeholder=”Company Inc.” />
</div>
<div>
<label class=”stq-label”>Effective Date</label>
<select name=”effective”>
<?php foreach($EFFECTIVE_OPTIONS as $d): ?>
<option value=”<?php echo esc_attr($d); ?>”><?php echo esc_html(date(‘M j, Y’, strtotime($d))); ?></option>
<?php endforeach; ?>
</select>
<div class=”stq-muted”>(Only 1st or 15th of a month)</div>
</div>
<div>
<label class=”stq-label”>County</label>
<?php if (!empty($COUNTY_OPTIONS)): ?>
<select name=”county” id=”stq-county”>
<option value=””>— Select County —</option>
<?php foreach($COUNTY_OPTIONS as $co): ?>
<option value=”<?php echo esc_attr($co); ?>”><?php echo esc_html($co); ?></option>
<?php endforeach; ?>
</select>
<?php else: ?>
<input type=”text” id=”stq-county” name=”county” placeholder=”County” />
<?php endif; ?>
<div id=”stq-area-line” class=”stq-muted” style=”display:none;”>
Rating Area: <span id=”stq-area-val”></span>
</div>
<input type=”hidden” name=”stq_area” id=”stq-area-hidden” value=””>
</div>
<div>
<label class=”stq-label”>Initial View</label>
<select name=”sheet_label”>
<?php foreach($VIEW_MAP as $label=>$sheetName): ?>
<option value=”<?php echo esc_attr($label); ?>” <?php selected($label,$initial_view_label); ?>>
<?php echo esc_html($label); ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class=”stq-card”>
<div class=”stq-label”>How do you want to quote?</div>
<div class=”stq-radios”>
<label class=”stq-radio”><input type=”radio” name=”how” value=”renewal” /> <span>Renewal only (composite)</span></label>
<label class=”stq-radio”><input type=”radio” name=”how” value=”census” /> <span>Census only</span></label>
<label class=”stq-radio”><input type=”radio” name=”how” value=”both” checked /> <span>Both (renewal + census)</span></label>
</div>
<div class=”stq-muted” style=”margin-top:6px;”>
<details>
<summary style=”cursor:pointer;font-weight:600;”>Which option should I choose?</summary>
<div style=”margin-top:8px;line-height:1.45;”>
<p><strong>Quote from Renewal (Composite):</strong> Use when the employer has a fully-insured, composite-rated small group plan. No census is needed—enter the employee-only renewal rate for any plan they offer (it doesn’t have to be the current plan).</p>
<p><strong>Quote from Census:</strong> Use for new groups, groups without a fully-insured plan (e.g., level-funded), or when you want age-rate detail on the side-by-side.</p>
<p><strong>Both:</strong> Use when the group is composite-rated but the enrollment census has changed since the carrier issued renewal. Carriers generally don’t re-tier composites after issuing renewal, so enter the composite rate to lock it in, and also provide the new census so other carriers can derive composites from the updated enrollment.</p>
</div>
</details>
</div>
<!– Current rating method (used for renewal math) –>
<div class=”stq-label” style=”margin-top:12px;”><strong>How is the group currently rated?</strong></div>
<div class=”stq-radios” style=”margin-top:4px;”>
<label class=”stq-radio”>
<input type=”radio” name=”rate_style” value=”composite” checked />
<span>Composite (tiered)</span>
</label>
<label class=”stq-radio”>
<input type=”radio” name=”rate_style” value=”age” />
<span>Age-rated (per member)</span>
</label>
</div>
<div class=”stq-muted” style=”margin-top:4px;”>
This is used to calculate the renewal increase; it doesn’t limit what you compare.
</div>
<!– Census row: toggled by radios –>
<div id=”stq-census-row” style=”margin-top:6px; display:none;”>
<span class=”stq-label”>Optional Census CSV:</span>
<input type=”file” name=”stq_census” accept=”.csv”>
</div>
</div>
<?php
// Live preview of counts if a CSV is posted to the intake form
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’ && isset($_POST[‘stq_intake_submit’])) {
$__counts_preview = self::parse_counts_from_upload();
if ($__counts_preview) {
echo self::header_counts($__counts_preview);
}
}
?>
<div class=”stq-card” id=”stq-renewal-block”>
<div class=”stq-row-3″>
<div>
<label class=”stq-label”>Current Carrier</label>
<select name=”renewal_carrier” id=”stq-renewal-carrier”>
<option value=””>— Select Carrier —</option>
<?php foreach($CARRIERS as $key=>$label): ?>
<option value=”<?php echo esc_attr($key); ?>”><?php echo esc_html($label); ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label class=”stq-label”>Select Any Renewal Plan</label>
<select name=”renewal_plan” id=”stq-renewal-plan”>
<option value=””>— Select Plan —</option>
</select>
</div>
<div>
<label class=”stq-label”>Renewal EO Rate</label>
<input type=”number” step=”0.01″ name=”renewal_eo” placeholder=”e.g., 475.00″ />
</div>
</div>
<div class=”stq-row-4″ style=”margin-top:10px;”>
<div><label class=”stq-label”>EO Count</label><input type=”number” name=”cnt_eo” value=”0″ min=”0″ /></div>
<div><label class=”stq-label”>ES Count</label><input type=”number” name=”cnt_es” value=”0″ min=”0″ /></div>
<div><label class=”stq-label”>EC Count</label><input type=”number” name=”cnt_ec” value=”0″ min=”0″ /></div>
<div><label class=”stq-label”>EF Count</label><input type=”number” name=”cnt_ef” value=”0″ min=”0″ /></div>
</div>
</div>
<div style=”margin-top:12px;”>
<button class=”stq-btn-primary” type=”submit” name=”stq_intake_submit” value=”1″>NEXT: CHOOSE PLANS</button>
</div>
</form>
<script>
// County → Area preview + hidden (TX-only)
(function(){
const countyEl = document.getElementById(‘stq-county’);
const line = document.getElementById(‘stq-area-line’);
const out = document.getElementById(‘stq-area-val’);
const hidden = document.getElementById(‘stq-area-hidden’);
if (!countyEl) return;
const MAP = <?php echo json_encode($COUNTY_AREA_MAP ?: new stdClass()); ?>;
function updateArea(){
const co = countyEl.value || ”;
const area = MAP && MAP[co] ? MAP[co] : ”;
if (area){
line.style.display = ‘block’;
out.textContent = area;
hidden.value = area;
} else {
line.style.display = ‘none’;
out.textContent = ”;
hidden.value = ”;
}
}
countyEl.addEventListener(‘change’, updateArea);
updateArea();
})();
// Renewal plan dropdown depends on carrier
(function(){
const M = <?php echo json_encode($plans_by_carrier); ?>;
const cSel = document.getElementById(‘stq-renewal-carrier’);
const pSel = document.getElementById(‘stq-renewal-plan’);
function fill(){
if(!cSel||!pSel) return;
const c = cSel.value || ”;
pSel.innerHTML = ‘<option value=””>— Select Plan —</option>’;
if(!c || !M[c]) return;
M[c].forEach(function(row){
const opt=document.createElement(‘option’);
opt.value=row.plan_code; opt.textContent=row.marketing_name;
pSel.appendChild(opt);
});
}
if (cSel){ cSel.addEventListener(‘change’, fill); fill(); }
})();
// Radios: toggle renewal block & census row
(function(){
const renewalBlock = document.getElementById(‘stq-renewal-block’);
const censusRow = document.getElementById(‘stq-census-row’);
function update(){
const v = (document.querySelector(‘input[name=”how”]:checked’)||{}).value || ‘both’;
if (renewalBlock) renewalBlock.style.display = (v === ‘census’) ? ‘none’ : ‘block’;
if (censusRow) censusRow.style.display = (v === ‘census’ || v === ‘both’) ? ‘block’ : ‘none’;
}
document.querySelectorAll(‘input[name=”how”]’).forEach(r=>r.addEventListener(‘change’, update));
update();
})();
</script>
<?php
return ob_get_clean();
}