const fs = require('fs');

let subMod = fs.readFileSync('subcontracts.js', 'utf8');

const badCode = `    // Credit Advance
    if(advance > 0) {
        const advAcc = await window.resolveSystemAccount('دفعات مقدمة لمقاولي الباطن', ['أصول', 'دفعات'], 'assets');
        rows.push({ account_id: advAcc.id, debit: 0, credit: advance, project_id: cont.project_id }

    // Credit Other Deductions
    if(otherDed > 0) {
        const otherAcc = await window.resolveSystemAccount('استقطاعات متنوعة من مقاولي الباطن', ['إيرادات', 'أخرى'], 'revenues');
        rows.push({ account_id: otherAcc.id, debit: 0, credit: otherDed, project_id: cont.project_id });
    });
    }`;

const goodCode = `    // Credit Advance
    if(advance > 0) {
        const advAcc = await window.resolveSystemAccount('دفعات مقدمة لمقاولي الباطن', ['أصول', 'دفعات'], 'assets');
        rows.push({ account_id: advAcc.id, debit: 0, credit: advance, project_id: cont.project_id });
    }

    // Credit Other Deductions
    if(otherDed > 0) {
        const otherAcc = await window.resolveSystemAccount('استقطاعات متنوعة من مقاولي الباطن', ['إيرادات', 'أخرى'], 'revenues');
        rows.push({ account_id: otherAcc.id, debit: 0, credit: otherDed, project_id: cont.project_id });
    }`;

subMod = subMod.replace(badCode, goodCode);
fs.writeFileSync('subcontracts.js', subMod, 'utf8');

let indexHtml = fs.readFileSync('index.html', 'utf8');
indexHtml = indexHtml.replace(/v=20260610r/g, 'v=20260610s');
fs.writeFileSync('index.html', indexHtml, 'utf8');
console.log('Fixed syntax error');
