<?php
/**
 * One-shot: post JEs for any historical payments that don't yet have one.
 * Safe to re-run — JournalPosting::postForPayment() is idempotent.
 */
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__DIR__));
require_once ROOT . DS . 'config' . DS . 'config.php';
require_once ROOT . DS . 'core' . DS . 'Application.php';
require_once ROOT . DS . 'core' . DS . 'Model.php';
require_once ROOT . DS . 'core' . DS . 'DB.php';
require_once ROOT . DS . 'app' . DS . 'models' . DS . 'JournalPosting.php';

$db  = new DB();
$rs  = $db->mySelect("SELECT c0d, payment_no FROM payments WHERE deleted = 0 ORDER BY c0d ASC", []) ?: [];
$jp  = new JournalPosting();
foreach ($rs as $p) {
    $r = $jp->postForPayment((int)$p['c0d']);
    echo str_pad($p['payment_no'], 20) . " → " . json_encode($r) . "\n";
}
echo "Done.\n";
