Files
html/api/accounting-api.php
2026-04-20 15:22:16 +02:00

93 lines
3.6 KiB
PHP

<?php
// WEVAL Accounting API — OPUS 20avr wire dormant accounting module
// Doctrine #2 ZERO simulation · #14 enrichissement · #4 honnêteté
// Expose P&L + invoices skeleton (stub avec data réelle si disponible)
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');
$action = $_GET['action'] ?? 'status';
$token = $_GET['token'] ?? '';
if ($token !== 'WEVADS2026' && !in_array($action, ['status','public'])) {
http_response_code(401);
die(json_encode(['error'=>'token required']));
}
try {
$db = @new PDO('pgsql:host=10.1.0.3;port=5432;dbname=adx_system', 'admin', 'admin123');
} catch (Exception $e) {
$db = null;
}
switch ($action) {
case 'status':
// Real P&L skeleton — data if tables exist, else honest empty
$out = [
'ok' => true,
'module' => 'accounting',
'status' => 'wired_stub',
'created_by' => 'opus_20avr_wire_dormants',
'note' => 'Module accounting wired — skeleton live. Fill tables accounting.pnl / accounting.invoices when ready.',
'tables_expected' => ['accounting.invoices', 'accounting.pnl_monthly', 'accounting.expenses', 'accounting.taxes'],
'ui_url' => '/accounting.html (to create)',
];
if ($db) {
$out['db_connected'] = true;
// Check if accounting schema exists
$r = $db->query("SELECT schema_name FROM information_schema.schemata WHERE schema_name='accounting'");
$out['schema_exists'] = (bool)$r->fetchColumn();
// CRM real data as proxy for revenue
try {
$r = $db->query("SELECT COUNT(*) FROM crm.deals WHERE status='won'");
$out['deals_won_count'] = (int)$r->fetchColumn();
$r = $db->query("SELECT COALESCE(SUM(value),0) FROM crm.deals WHERE status IN ('won','open')");
$out['pipeline_total_eur'] = (float)$r->fetchColumn();
} catch (Exception $e) { $out['crm_err'] = $e->getMessage(); }
} else {
$out['db_connected'] = false;
}
$out['ts'] = date('c');
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
break;
case 'pnl':
// P&L skeleton (return zero-data structure if no tables, doctrine #4 honest)
$pnl = [
'period' => $_GET['period'] ?? 'current_month',
'revenue' => 0,
'costs' => 0,
'margin' => 0,
'status' => 'no_data',
'note' => 'Fill accounting.pnl_monthly to populate. Doctrine #4: returning zeros, not simulations.',
];
if ($db) {
try {
$r = $db->query("SELECT * FROM accounting.pnl_monthly ORDER BY month DESC LIMIT 1");
if ($r && $row = $r->fetch(PDO::FETCH_ASSOC)) {
$pnl = array_merge($pnl, $row);
$pnl['status'] = 'live';
}
} catch (Exception $e) { /* schema not yet created */ }
}
echo json_encode($pnl, JSON_UNESCAPED_UNICODE);
break;
case 'invoices':
$invoices = [];
if ($db) {
try {
$r = $db->query("SELECT id, client, amount, date, status FROM accounting.invoices ORDER BY date DESC LIMIT 20");
while ($row = $r->fetch(PDO::FETCH_ASSOC)) $invoices[] = $row;
} catch (Exception $e) { /* schema not yet */ }
}
echo json_encode(['invoices' => $invoices, 'count' => count($invoices), 'status' => $invoices ? 'live' : 'empty_schema_pending']);
break;
case 'public':
echo json_encode(['module' => 'accounting', 'status' => 'wired_public', 'endpoints' => ['/api/accounting-api.php?action=status&token=WEVADS2026']]);
break;
default:
echo json_encode(['error' => 'unknown action', 'actions' => ['status', 'pnl', 'invoices', 'public']]);
}