From 632d81b98b1bcb32666a9f57efd4d757b3496ae4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 12:58:30 -0400 Subject: [PATCH] Math in the rate and quantity boxes Typing arithmetic into a line's rate or quantity (33*1.3, (120+15)/2; + - * /, x for times, parentheses) works it out when leaving the box, and before saving; the amount updates while typing. Math in the rate is rounded to the nearest penny, half up and exactly (1.005 -> 1.01). It's parsed, never run as code; broken math is left as typed. A click right after a Tab no longer selects the box's text. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM --- README.md | 2 +- app/views/qbo_estimates/_script.html.erb | 42 +++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4fcc5cc..fdb4f43 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ Estimates and invoices keep their number, date, customer, total and **man-hours* * **New estimate** from the customer page: Create Estimate opens the Redmine editor. The customer page no longer links to QuickBooks' estimate or payment screens; the customer's name still opens their QuickBooks account page as a fallback. * **Edit** any estimate from the Edit link next to it on the customer page or the issue page. -* **Keyboard entry**: in a line's product box, Tab picks the product the text points to (an exact name, the only match, or the only name starting with it, e.g. "lab" → Labor) and fills the line. Tabbing into the description, quantity or rate selects it, so typing replaces it. +* **Keyboard entry**: in a line's product box, Tab picks the product the text points to (an exact name, the only match, or the only name starting with it, e.g. "lab" → Labor) and fills the line. Tabbing into the description, quantity or rate selects it, so typing replaces it. The quantity and rate boxes take simple math (e.g. `33*1.3`, `(120+15)/2`; `+ - * /`, `x` for times, parentheses), worked out when you leave the box; math in the rate is rounded to the nearest penny. * **Duplicate** an estimate to start a similar one: same customer, lines, subtotals, custom fields and messages, dated today, with the current user as sales person. * **Read-only once linked to an issue**: an estimate that an issue is linked to can be viewed, emailed and duplicated, but not changed, so the work doesn't drift from what was quoted. The list shows a View link for these instead of Edit. * **Read-only once billed**: an estimate that's Converted or Closed in QuickBooks, or linked to an invoice, can't be changed here either (including redmine_qbo_lineitems' estimates once they're invoiced). Its page lists the invoices it's on; it can still be viewed, emailed and duplicated, and it shows View instead of Edit in lists. diff --git a/app/views/qbo_estimates/_script.html.erb b/app/views/qbo_estimates/_script.html.erb index ef6818a..b9b35f5 100644 --- a/app/views/qbo_estimates/_script.html.erb +++ b/app/views/qbo_estimates/_script.html.erb @@ -44,13 +44,47 @@ $(function () { var dirty = false; // ---- numbers ---- + // A number, or simple arithmetic typed into a quantity or rate box (e.g. 33*1.3, (120+15)/2): + - * / and + // parentheses only, worked out here (never run as code). null when empty, NaN when it can't be read. + function isMath(text) { return /[*\/()+x×]/.test(text) || /.-/.test(text); } + function evaluateMath(text) { + var tokens = text.replace(/[x×]/g, '*').match(/\d*\.?\d+|[-+*\/()]/g); + if (!tokens || tokens.join('') !== text.replace(/[x×]/g, '*')) return NaN; + var i = 0; + var peek = function () { return tokens[i]; }; + var expr, term, factor; + factor = function () { + var t = tokens[i++]; + if (t === '-') return -factor(); + if (t === '+') return factor(); + if (t === '(') { var v = expr(); if (tokens[i++] !== ')') return NaN; return v; } + return t !== undefined && /^\d*\.?\d+$/.test(t) ? parseFloat(t) : NaN; + }; + term = function () { + var v = factor(); + while (peek() === '*' || peek() === '/') { var op = tokens[i++], r = factor(); v = op === '*' ? v * r : v / r; } + return v; + }; + expr = function () { + var v = term(); + while (peek() === '+' || peek() === '-') { var op = tokens[i++], r = term(); v = op === '+' ? v + r : v - r; } + return v; + }; + var result = expr(); + return i === tokens.length && isFinite(result) ? result : NaN; + } function parseNumber(value) { var text = String(value == null ? '' : value).replace(/[$,\s]/g, ''); if (text === '') return null; + if (isMath(text)) return evaluateMath(text); var n = Number(text); return isFinite(n) ? n : NaN; } - function round(n, places) { var f = Math.pow(10, places); return Math.round(n * f) / f; } + // Rounds half up exactly (1.005 -> 1.01; multiplying first would give 1.00) + function round(n, places) { + if (/e/i.test(String(n))) { var f = Math.pow(10, places); return Math.round(n * f) / f; } + return Number(Math.round(Number(n + 'e' + places)) + 'e-' + places); + } function plain(n, places) { return String(round(n, places)); } // ---- rows ---- @@ -263,6 +297,8 @@ $(function () { // still places the cursor) var tabbedAt = 0; $(document).on('keydown', function (event) { if (event.key === 'Tab') tabbedAt = Date.now(); }); + // A click right after a Tab is still a click + $(document).on('mousedown touchstart', function () { tabbedAt = 0; }); $tbody.on('focus', '.qbo-line-description-input, .qbo-line-qty-input, .qbo-line-rate-input', function () { if (Date.now() - tabbedAt > 500) return; var box = this; @@ -291,6 +327,8 @@ $(function () { var n = parseNumber(this.value); if (n === null || isNaN(n)) return; var isRate = $(this).hasClass('qbo-line-rate-input'); + // Math in the rate box is rounded to the nearest penny + if (isRate && isMath(this.value.replace(/[$,\s]/g, ''))) n = round(n, 2); this.value = isRate && round(n, 2) === round(n, 5) ? n.toFixed(2) : plain(n, 5); }); } @@ -633,6 +671,8 @@ $(function () { $email.trigger('focus'); return; } + // Math still in a box is worked out before saving + rows().each(function () { tidyNumbers($(this)); }); dirty = false; });