const fs = require('fs');

const filesToPatch = [
    'vouchers_returns.js',
    'app_final.js',
    'subcontracts.js',
    'sales_purchases_module.js'
];

filesToPatch.forEach(file => {
    if (fs.existsSync(file)) {
        let js = fs.readFileSync(file, 'utf8');
        
        // 1. Fix toFixed(2) -> toLocaleString('en-US')
        js = js.replace(/\.toFixed\(2\)/g, ".toLocaleString('en-US')");
        js = js.replace(/: '0\.00'/g, ": '0'");
        js = js.replace(/: "0\.00"/g, ': "0"');
        
        // 2. Fix description: -> desc: for journal generation in vouchers_returns.js
        if (file === 'vouchers_returns.js') {
            js = js.replace(/description:/g, "desc:");
        }
        
        fs.writeFileSync(file, js, 'utf8');
        console.log(`Patched ${file}`);
    }
});

// Cache buster
if (fs.existsSync('index.html')) {
    let html = fs.readFileSync('index.html', 'utf8');
    html = html.replace(/v=\d+/g, 'v=' + Date.now());
    fs.writeFileSync('index.html', html, 'utf8');
}
