← back to Filemaker Mcp

scan-grs-wrong-mfr.mjs

37 lines

import fs from 'fs';
const Q = String.fromCharCode(34);
function parseCSV(t) {
  const R = []; let i = 0, f = '', r = [], q = false;
  while (i < t.length) {
    const c = t[i];
    if (q) { if (c === Q) { if (t[i + 1] === Q) { f += Q; i++; } else q = false; } else f += c; }
    else { if (c === Q) q = true; else if (c === ',') { r.push(f); f = ''; } else if (c === '\n') { r.push(f); R.push(r); r = []; f = ''; } else if (c !== '\r') f += c; }
    i++;
  }
  if (f.length || r.length) { r.push(f); R.push(r); }
  return R;
}
const raw = parseCSV(fs.readFileSync(process.env.HOME + '/Desktop/GRS_fm_all_records_20260819.csv', 'utf8'));
const h = raw.shift(); const ix = Object.fromEntries(h.map((x, i) => [x, i]));
const norm = s => (s || '').toUpperCase().replace(/[^A-Z0-9]/g, '');
let wrong = [], blank = 0, total = 0;
for (const r of raw) {
  if (!r.length || !r[ix.combo_sku]) continue; total++;
  const combo = norm(r[ix.combo_sku]);   // GRS43023
  const num = norm(r[ix.js_pattern]);    // 43023
  const mfr = norm(r[ix.mfr_pattern]);   // SC5823 / 43023 / GRS43023
  if (!mfr) { blank++; continue; }
  if (mfr === num || mfr === combo || mfr === 'GRS' + num) {
    wrong.push([r[ix.combo_sku], r[ix.mfr_pattern], r[ix.js_pattern], r[ix.supplier], r[ix.fm_record_id]]);
  }
}
console.log('total GRS FM records:', total);
console.log('records with DW-SKU-in-Mfr (WRONG):', wrong.length);
console.log('records with BLANK Mfr:', blank);
const out = process.env.HOME + '/Desktop/GRS_fm_WRONG_mfr_is_dwsku_20260819.csv';
const qq = x => Q + String(x ?? '').replace(/"/g, Q + Q) + Q;
fs.writeFileSync(out, 'combo_sku,mfr_pattern,js_pattern,supplier,fm_record_id\n' + wrong.map(p => p.map(qq).join(',')).join('\n') + '\n');
console.log('wrote ->', out);
console.log('\nsample WRONG records:');
for (const p of wrong.slice(0, 15)) console.log('  ' + p[0] + ' | Mfr=' + JSON.stringify(p[1]) + ' | supplier=' + JSON.stringify(p[3]) + ' | recId=' + p[4]);