File: //tmp/fix_invoice_ctrl.py
with open('/var/www/magobridge/src/Controllers/InvoiceController.php', 'r') as f:
content = f.read()
old = """ $this->flash('success', 'Fattura ' . $result['doc_no'] . ' creata in Mago (ID: ' . $result['sale_doc_id'] . ')');"""
new = """ // Auto-contabilizzazione
$postScript = dirname(__DIR__, 2) . '/scripts/mago_post.py';
$postCmd = "python3 " . escapeshellarg($postScript)
. " --action post"
. " --config " . escapeshellarg(dirname(__DIR__, 2) . '/config/mago.ini')
. " --sale-doc-id " . (int)$result['sale_doc_id']
. " 2>&1";
$postOutput = shell_exec($postCmd);
$postResult = json_decode($postOutput, true);
if ($postResult && ($postResult['success'] ?? false)) {
$this->flash('success', 'Fattura ' . $result['doc_no'] . ' creata e contabilizzata in Mago! (' . $postResult['message'] . ')');
} else {
$this->flash('warning', 'Fattura ' . $result['doc_no'] . ' creata in Mago ma contabilizzazione fallita: ' . ($postResult['message'] ?? 'Errore'));
}"""
content = content.replace(old, new)
with open('/var/www/magobridge/src/Controllers/InvoiceController.php', 'w') as f:
f.write(content)
print('OK')