<?php
/**
 * MJPOS -> MJ ERP Hub: Cheque Tracking Endpoint
 * Drop at: api/erp/cheques.php inside MJPOS project root
 *
 * GET /api/erp/cheques.php
 * Returns ALL cheques regardless of status (pending/cleared/bounced) - the
 * ERP's Finance > Cheque Management tab filters client-side by status.
 *
 * Response:
 * { "status": "success", "data": [
 *     { "id": 1, "transaction_type": "purchase", "party_name": "M.R.M.ASKAR", "party_phone": "0772329121",
 *       "cheque_no": "024351", "bank_name": "DFCC", "branch": "RUWANWELLA", "cheque_date": "2026-08-14",
 *       "amount": 41228.63, "status": "pending", "cleared_date": null, "note": null }
 * ]}
 */

require_once __DIR__ . '/erp_auth.php';
require_once __DIR__ . '/../../mjpos/config/config.php';

erp_require_auth();

try {
    // Confirmed against MJPOS schema: `cheque_tracking` table
    $stmt = erp_pdo()->query(
        "SELECT id, transaction_type, party_name, party_phone, cheque_no, bank_name, branch, cheque_date, amount, status, cleared_date, note
         FROM cheque_tracking
         ORDER BY cheque_date DESC, id DESC"
    );
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    erp_json_success($rows);

} catch (Exception $e) {
    erp_json_error('Query failed: ' . $e->getMessage(), 500);
}
