Fix NCalc case sensitivity and add formula validation

- Normalize IF/Abs/Pow/etc. to lowercase before evaluation so AI-generated
  or manually typed uppercase function names no longer cause "Function not
  found" errors
- Add NormalizeAndValidate() which normalizes then does a parse-only check
  on save — invalid formulas are rejected with a clear error before storing
- Update AI system prompt to list all functions in lowercase and explicitly
  call out case-sensitivity; add if() to the supported function list
- Add collapsible NCalc quick-reference panel in the formula editor showing
  all operators, functions (lowercase), built-in variables, and an example

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 22:09:43 -04:00
parent ca7e905832
commit efc4e9dadf
4 changed files with 97 additions and 3 deletions
@@ -2343,6 +2343,40 @@
<span class="me-1">Variables (click to insert):</span>
<span id="cfVariablePills"></span>
</div>
<div class="mt-2">
<a class="small text-decoration-none" data-bs-toggle="collapse" href="#cfFormulaRef" role="button">
<i class="bi bi-question-circle me-1"></i>Formula reference
</a>
<div class="collapse" id="cfFormulaRef">
<div class="card card-body py-2 px-3 mt-1 small border-secondary-subtle" style="font-size:.8rem">
<div class="row g-2">
<div class="col-md-6">
<strong class="d-block mb-1 text-muted text-uppercase" style="font-size:.65rem;letter-spacing:.05em">Operators</strong>
<code>+ &nbsp;- &nbsp;* &nbsp;/ &nbsp;%</code><br>
<code>&lt; &nbsp;&gt; &nbsp;&lt;= &nbsp;&gt;= &nbsp;== &nbsp;!=</code><br>
<code>&amp;&amp; &nbsp;|| &nbsp;!</code>
<strong class="d-block mt-2 mb-1 text-muted text-uppercase" style="font-size:.65rem;letter-spacing:.05em">Built-in variables (auto-injected)</strong>
<code>rate</code> &mdash; template&rsquo;s default rate<br>
<code>standard_labor_rate</code><br>
<code>markup_pct</code><br>
<code>additional_coat_labor_pct</code>
</div>
<div class="col-md-6">
<strong class="d-block mb-1 text-muted text-uppercase" style="font-size:.65rem;letter-spacing:.05em">Functions (must be lowercase)</strong>
<code>if(cond, a, b)</code> &mdash; conditional<br>
<code>abs(x)</code><br>
<code>round(x, digits)</code><br>
<code>max(a, b)</code> / <code>min(a, b)</code><br>
<code>pow(base, exp)</code><br>
<code>sqrt(x)</code>
<strong class="d-block mt-2 mb-1 text-muted text-uppercase" style="font-size:.65rem;letter-spacing:.05em">Example</strong>
<code class="d-block text-break">if(qty &gt; 10, qty * rate * 0.9, qty * rate)</code>
<span class="text-muted">10% discount over 10 units</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="mb-3">
<label class="form-label">Notes</label>