<?php
require_once __DIR__.'/_bootstrap.php';$auth=mobile_api_authenticate($conn);$agentId=(int)($auth['agent']['id']??0);$items=[];
if($agentId>0){$r=@$conn->query("SELECT l.id,l.direction,l.entry_type,l.amount,l.currency,l.balance_after,l.description,l.occurred_at,l.invoice_id,l.payment_id,s.full_name FROM agent_ledger_entries l LEFT JOIN subscribers s ON s.id=l.subscriber_id WHERE l.agent_id={$agentId} ORDER BY l.id DESC LIMIT 60");while($r&&($row=$r->fetch_assoc()))$items[]=['id'=>(int)$row['id'],'kind'=>(string)$row['entry_type'],'direction'=>(string)$row['direction'],'amount'=>mobile_api_money($row['amount']),'balance_after'=>mobile_api_money($row['balance_after']),'description'=>(string)$row['description'],'subscriber_name'=>(string)($row['full_name']??''),'invoice_id'=>(int)$row['invoice_id'],'payment_id'=>(int)$row['payment_id'],'occurred_at'=>(string)$row['occurred_at']];}else{$r=@$conn->query("SELECT p.id,p.invoice_id,p.amount,p.method,p.reference,p.payment_status,p.paid_at,p.created_at,s.full_name FROM payments p LEFT JOIN subscribers s ON s.id=p.subscriber_id ORDER BY p.id DESC LIMIT 60");while($r&&($row=$r->fetch_assoc()))$items[]=['id'=>(int)$row['id'],'kind'=>'PAYMENT','direction'=>'IN','amount'=>mobile_api_money($row['amount']),'description'=>(string)$row['method'].' - '.(string)$row['reference'],'subscriber_name'=>(string)($row['full_name']??''),'invoice_id'=>(int)$row['invoice_id'],'payment_id'=>(int)$row['id'],'status'=>(string)$row['payment_status'],'occurred_at'=>(string)($row['paid_at']?:$row['created_at'])];}
mobile_api_reply(200,['ok'=>true,'transactions'=>$items]);

