File size: 9,590 Bytes
93efe54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
Invoice Generation System - Test Script

This script tests the complete invoice generation flow:
1. Get available tickets
2. Generate invoice
3. View invoice (public)
4. Export CSV
5. Regenerate token

Usage:
    python scripts/test_invoice_generation.py
"""
import requests
import json
import sys
from datetime import datetime

# Configuration
BASE_URL = "http://localhost:8000/api/v1"
AUTH_TOKEN = "YOUR_AUTH_TOKEN_HERE"  # Replace with actual token
CONTRACTOR_ID = "YOUR_CONTRACTOR_ID"  # Replace with actual UUID
CLIENT_ID = "YOUR_CLIENT_ID"  # Replace with actual UUID
PROJECT_ID = "YOUR_PROJECT_ID"  # Replace with actual UUID

HEADERS = {
    "Authorization": f"Bearer {AUTH_TOKEN}",
    "Content-Type": "application/json"
}


def print_section(title):
    """Print section header"""
    print("\n" + "=" * 60)
    print(f"  {title}")
    print("=" * 60)


def print_success(message):
    """Print success message"""
    print(f"βœ… {message}")


def print_error(message):
    """Print error message"""
    print(f"❌ {message}")


def print_info(message):
    """Print info message"""
    print(f"ℹ️  {message}")


def test_get_available_tickets():
    """Test getting available tickets"""
    print_section("1. Get Available Tickets")
    
    try:
        response = requests.get(
            f"{BASE_URL}/invoices/available-tickets",
            params={
                "contractor_id": CONTRACTOR_ID,
                "project_id": PROJECT_ID
            },
            headers=HEADERS
        )
        
        if response.status_code == 200:
            data = response.json()
            tickets = data.get("tickets", [])
            print_success(f"Found {len(tickets)} available tickets")
            
            if tickets:
                print_info("Sample ticket:")
                ticket = tickets[0]
                print(f"  - ID: {ticket['id']}")
                print(f"  - Name: {ticket.get('ticket_name', 'N/A')}")
                print(f"  - Type: {ticket['ticket_type']}")
                print(f"  - Images: {ticket['images_count']}")
                print(f"  - Sales Order: {ticket.get('sales_order_number', 'N/A')}")
                
                return [t['id'] for t in tickets[:2]]  # Return first 2 ticket IDs
            else:
                print_error("No available tickets found")
                print_info("Create some completed tickets first:")
                print("  UPDATE tickets SET status='completed', completed_at=NOW(), is_invoiced=false WHERE id='YOUR_TICKET_ID';")
                return None
        else:
            print_error(f"Failed: {response.status_code}")
            print(response.text)
            return None
            
    except Exception as e:
        print_error(f"Exception: {str(e)}")
        return None


def test_generate_invoice(ticket_ids):
    """Test generating invoice"""
    print_section("2. Generate Invoice")
    
    if not ticket_ids:
        print_error("No ticket IDs provided")
        return None
    
    try:
        payload = {
            "contractor_id": CONTRACTOR_ID,
            "client_id": CLIENT_ID,
            "project_id": PROJECT_ID,
            "ticket_ids": ticket_ids,
            "title": f"Test Invoice - {datetime.now().strftime('%Y-%m-%d %H:%M')}",
            "notes": "This is a test invoice generated by the test script"
        }
        
        response = requests.post(
            f"{BASE_URL}/invoices/generate",
            headers=HEADERS,
            json=payload
        )
        
        if response.status_code == 200:
            data = response.json()
            print_success("Invoice generated successfully")
            print(f"  - Invoice ID: {data['invoice_id']}")
            print(f"  - Invoice Number: {data['invoice_number']}")
            print(f"  - Tickets: {data['tickets_count']}")
            print(f"  - Viewing Link: {data['viewing_link']}")
            print(f"  - CSV Link: {data['csv_download_link']}")
            print(f"  - Notification Created: {data['notification_created']}")
            
            return data
        else:
            print_error(f"Failed: {response.status_code}")
            print(response.text)
            return None
            
    except Exception as e:
        print_error(f"Exception: {str(e)}")
        return None


def test_view_invoice(viewing_link):
    """Test viewing invoice (public - no auth)"""
    print_section("3. View Invoice (Public)")
    
    if not viewing_link:
        print_error("No viewing link provided")
        return False
    
    try:
        # Extract token from link
        token = viewing_link.split("token=")[1] if "token=" in viewing_link else None
        
        if not token:
            print_error("Could not extract token from link")
            return False
        
        response = requests.get(
            f"{BASE_URL}/invoices/view",
            params={"token": token}
        )
        
        if response.status_code == 200:
            data = response.json()
            invoice = data.get("invoice", {})
            print_success("Invoice viewed successfully")
            print(f"  - Invoice Number: {invoice.get('invoice_number')}")
            print(f"  - Line Items: {len(invoice.get('line_items', []))}")
            print(f"  - Times Viewed: {data.get('times_viewed')}")
            print(f"  - Token Expires: {data.get('token_expires_at')}")
            
            # Check if ticket details are enriched
            line_items = invoice.get('line_items', [])
            if line_items:
                first_item = line_items[0]
                if 'ticket_details' in first_item:
                    details = first_item['ticket_details']
                    print_info("Ticket details enriched:")
                    print(f"    - Images: {details.get('images_count', 0)}")
                    print(f"    - Completion Data: {bool(details.get('completion_data'))}")
                    print(f"    - Location: {bool(details.get('work_location'))}")
            
            return True
        else:
            print_error(f"Failed: {response.status_code}")
            print(response.text)
            return False
            
    except Exception as e:
        print_error(f"Exception: {str(e)}")
        return False


def test_export_csv(invoice_id):
    """Test CSV export"""
    print_section("4. Export CSV")
    
    if not invoice_id:
        print_error("No invoice ID provided")
        return False
    
    try:
        response = requests.get(
            f"{BASE_URL}/invoices/{invoice_id}/export/csv",
            headers=HEADERS
        )
        
        if response.status_code == 200:
            # Save CSV to file
            filename = f"invoice_{invoice_id}.csv"
            with open(filename, "wb") as f:
                f.write(response.content)
            
            print_success(f"CSV exported successfully: {filename}")
            
            # Show first few lines
            lines = response.content.decode('utf-8').split('\n')
            print_info("CSV Preview (first 3 lines):")
            for i, line in enumerate(lines[:3]):
                print(f"  {i+1}: {line[:100]}...")
            
            return True
        else:
            print_error(f"Failed: {response.status_code}")
            print(response.text)
            return False
            
    except Exception as e:
        print_error(f"Exception: {str(e)}")
        return False


def test_regenerate_token(invoice_id):
    """Test token regeneration"""
    print_section("5. Regenerate Token")
    
    if not invoice_id:
        print_error("No invoice ID provided")
        return False
    
    try:
        response = requests.post(
            f"{BASE_URL}/invoices/{invoice_id}/regenerate-token",
            headers=HEADERS,
            json={"expires_in_days": 30}
        )
        
        if response.status_code == 200:
            data = response.json()
            print_success("Token regenerated successfully")
            print(f"  - New Link: {data['viewing_link']}")
            print(f"  - Expires At: {data['expires_at']}")
            return True
        else:
            print_error(f"Failed: {response.status_code}")
            print(response.text)
            return False
            
    except Exception as e:
        print_error(f"Exception: {str(e)}")
        return False


def main():
    """Run all tests"""
    print("\n" + "πŸš€" * 30)
    print("  Invoice Generation System - Test Script")
    print("πŸš€" * 30)
    
    # Check configuration
    if AUTH_TOKEN == "YOUR_AUTH_TOKEN_HERE":
        print_error("Please configure AUTH_TOKEN in the script")
        sys.exit(1)
    
    if CONTRACTOR_ID == "YOUR_CONTRACTOR_ID":
        print_error("Please configure CONTRACTOR_ID in the script")
        sys.exit(1)
    
    # Run tests
    ticket_ids = test_get_available_tickets()
    
    if ticket_ids:
        invoice_data = test_generate_invoice(ticket_ids)
        
        if invoice_data:
            test_view_invoice(invoice_data['viewing_link'])
            test_export_csv(invoice_data['invoice_id'])
            test_regenerate_token(invoice_data['invoice_id'])
    
    # Summary
    print_section("Test Summary")
    print("βœ… All tests completed")
    print("\nNext steps:")
    print("1. Check notifications in the database")
    print("2. Verify tickets are marked as invoiced")
    print("3. Review audit logs")
    print("4. Test the viewing link in a browser")
    print("\n" + "πŸŽ‰" * 30 + "\n")


if __name__ == "__main__":
    main()