1+ <?php
2+ // Debug version of export.php with detailed error reporting
3+ header ('Content-Type: application/json ' );
4+ header ('Access-Control-Allow-Origin: * ' );
5+ header ('Access-Control-Allow-Methods: POST, OPTIONS ' );
6+ header ('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With ' );
7+
8+ // Handle preflight requests
9+ if ($ _SERVER ['REQUEST_METHOD ' ] === 'OPTIONS ' ) {
10+ http_response_code (200 );
11+ exit ();
12+ }
13+
14+ // Enable error reporting
15+ error_reporting (E_ALL );
16+ ini_set ('display_errors ' , 0 ); // Don't display errors to prevent header issues
17+ ini_set ('log_errors ' , 1 );
18+
19+ $ debug_info = [
20+ 'timestamp ' => date ('Y-m-d H:i:s ' ),
21+ 'method ' => $ _SERVER ['REQUEST_METHOD ' ],
22+ 'step ' => 'start ' ,
23+ 'errors ' => []
24+ ];
25+
26+ try {
27+ $ debug_info ['step ' ] = 'reading_input ' ;
28+ $ input = file_get_contents ('php://input ' );
29+ $ debug_info ['raw_input ' ] = $ input ;
30+
31+ $ debug_info ['step ' ] = 'parsing_json ' ;
32+ $ data = json_decode ($ input , true );
33+ $ debug_info ['parsed_data ' ] = $ data ;
34+
35+ if (!$ data ) {
36+ throw new Exception ('Invalid JSON input: ' . json_last_error_msg ());
37+ }
38+
39+ $ debug_info ['step ' ] = 'validating_input ' ;
40+ if (!isset ($ data ['session_ids ' ]) || !isset ($ data ['format ' ])) {
41+ throw new Exception ('Missing session_ids or format parameter ' );
42+ }
43+
44+ $ debug_info ['step ' ] = 'connecting_database ' ;
45+ require_once '../config/database.php ' ;
46+
47+ $ debug_info ['step ' ] = 'creating_database_object ' ;
48+ $ db = new Database ();
49+
50+ $ debug_info ['step ' ] = 'getting_connection ' ;
51+ $ connection = $ db ->getConnection ();
52+
53+ $ debug_info ['step ' ] = 'preparing_query ' ;
54+ $ sessionIds = $ data ['session_ids ' ];
55+ $ placeholders = str_repeat ('?, ' , count ($ sessionIds ) - 1 ) . '? ' ;
56+
57+ $ query = "
58+ SELECT
59+ b.id,
60+ b.value as barcode_value,
61+ b.symbology,
62+ b.symbology_name,
63+ b.quantity,
64+ b.timestamp as scan_date,
65+ COALESCE(b.processed, 0) as processed,
66+ COALESCE(b.notes, '') as notes,
67+ s.device_info,
68+ s.device_ip,
69+ s.session_timestamp as session_created
70+ FROM barcodes b
71+ JOIN capture_sessions s ON b.session_id = s.id
72+ WHERE b.session_id IN ( $ placeholders)
73+ ORDER BY s.session_timestamp DESC, b.timestamp ASC
74+ LIMIT 10
75+ " ;
76+
77+ $ debug_info ['step ' ] = 'executing_query ' ;
78+ $ debug_info ['query ' ] = $ query ;
79+ $ debug_info ['session_ids ' ] = $ sessionIds ;
80+
81+ $ stmt = $ connection ->prepare ($ query );
82+ $ stmt ->execute ($ sessionIds );
83+
84+ $ debug_info ['step ' ] = 'fetching_results ' ;
85+ $ results = $ stmt ->fetchAll (PDO ::FETCH_ASSOC );
86+ $ debug_info ['result_count ' ] = count ($ results );
87+ $ debug_info ['sample_results ' ] = array_slice ($ results , 0 , 2 ); // First 2 results for debugging
88+
89+ $ debug_info ['step ' ] = 'success ' ;
90+ $ debug_info ['status ' ] = 'Export would work with ' . count ($ results ) . ' records ' ;
91+
92+ } catch (Exception $ e ) {
93+ $ debug_info ['step ' ] = 'error ' ;
94+ $ debug_info ['error_message ' ] = $ e ->getMessage ();
95+ $ debug_info ['error_trace ' ] = $ e ->getTraceAsString ();
96+ $ debug_info ['errors ' ][] = [
97+ 'message ' => $ e ->getMessage (),
98+ 'file ' => $ e ->getFile (),
99+ 'line ' => $ e ->getLine ()
100+ ];
101+ }
102+
103+ // Return debug information as JSON
104+ echo json_encode ($ debug_info , JSON_PRETTY_PRINT );
105+ ?>
0 commit comments