const fs = require('fs');

if (fs.existsSync('app_final.js')) {
    let js = fs.readFileSync('app_final.js', 'utf8');
    
    // Find the exact line
    // if ((t.type === 'sales' || t.type === 'purchases') && (t.status === 'draft' || t.status === 'pending_store')) return false;
    
    const lines = js.split('\n');
    let modified = false;
    for(let i=0; i<lines.length; i++) {
        if(lines[i].includes("if ((t.type === 'sales' || t.type === 'purchases') && (t.status === 'draft' || t.status === 'pending_store'))")) {
            lines.splice(i+1, 0, "        if (t.type === 'receipt' || t.type === 'payment' || t.type === 'money_transfer' || t.type === 'pay_client_abstract') return false;");
            modified = true;
            break;
        }
    }

    if(modified) {
        fs.writeFileSync('app_final.js', lines.join('\n'), 'utf8');
        console.log('Patched app_final.js successfully');
    } else {
        console.log('Could not find the target line');
    }
}

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