<?php
/** تست سریع — صدور سند election از تراکنش */
define('ACC_BASE_PATH', __DIR__);
require __DIR__ . '/standalone/bootstrap.php';
require_once ACC_PATH . 'includes/helpers/acc-portal-accounting.php';

global $wpdb;
$pre = $wpdb->prefix;
$tt = $pre . 'election_transactions';

$tx_id = (int) $wpdb->get_var("SELECT id FROM {$tt} ORDER BY id DESC LIMIT 1");
if (!$tx_id) {
    $wpdb->insert($tt, [
        'direction' => 'out', 'category' => 'media', 'amount' => 500000,
        'description' => 'تست حسابداری پورتال', 'entry_date' => date('Y-m-d'),
        'created_by' => 1, 'created_at' => current_time('mysql'),
    ]);
    $tx_id = (int) $wpdb->insert_id;
}

Acc_Portal_Accounting::ensure_chart('election');
$res = Acc_Portal_Accounting::post_election_transaction($tx_id);
$docs = Acc_Portal_Accounting::list_documents('election', 5);
$chart = Acc_Portal_Accounting::chart_tree('election');

echo json_encode([
    'tx_id' => $tx_id,
    'post' => $res,
    'documents' => count($docs),
    'chart_accounts' => count($chart),
    'sample_chart' => array_slice($chart, 0, 3),
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
