<?php
/**
 * Test Invoice with FIXED Implementation
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "\n";
echo "===========================================\n";
echo "     ZATCA Phase 2 - VERSION         \n";
echo "===========================================\n\n";
require_once 'invoice_processor.php';

    $processor = new ZATCAInvoiceProcessor();
    
    // Simple test invoice
    $invoiceData = [
        'invoice_number' => 'INV-' . date('Ymd-His'),
        'issue_date' => date('Y-m-d\TH:i:s'),
        'icv_counter' => 1,
        'payment_by' => [
            'payment_method' => 'Cash',
            'payment_method_code' => '10'
        ],
        'invoice_type' => [
            'invoice_type_xml_code' => '0200000',
            'invoice_type_code' => '388',
            'is_b2c' => true
        ],
        'supplier' => [
            'name' => 'Smart Lines',
            'vat_number' => '310892800200003',
            'crn' => '2051040857',
            'address' => [
                'street' => 'King Fahd Road',
                'building_no' => '0123',
                'plot_id' => '45',
                'city' => 'Riyadh',
                'postal_code' => '12345',
                'country' => 'SA'
            ]
        ],
        'customer' => [
            'name' => 'Walk-in Customer',
            'vat_number' => '',
            'country' => 'SA'
        ],
        'items' => [
            [
                'name' => 'Test Product 1',
                'qty' => 2,
                'price' => 100.00,
                'uom' => 'PCE',
                'vat' => 15.00
            ],
            [
                'name' => 'Test Product 2',
                'qty' => 1,
                'price' => 50.00,
                'uom' => 'PCE',
                'vat' => 15.00
            ]
        ],
        'totals' => [
            'net_amount' => 250.00,
            'vat_total' => 37.50,
            'grand_total' => 287.50
        ]
    ];
    
    echo "Invoice Details:\n";
    echo "================\n";
    echo "Invoice Number: {$invoiceData['invoice_number']}\n";
    echo "Date: {$invoiceData['issue_date']}\n";
    echo "Supplier: {$invoiceData['supplier']['name']}\n";
    echo "VAT Number: {$invoiceData['supplier']['vat_number']}\n";
    echo "Items: " . count($invoiceData['items']) . "\n";
    echo "Net Amount: SAR " . number_format($invoiceData['totals']['net_amount'], 2) . "\n";
    echo "VAT (15%): SAR " . number_format($invoiceData['totals']['vat_total'], 2) . "\n";
    echo "Grand Total: SAR " . number_format($invoiceData['totals']['grand_total'], 2) . "\n";
    echo "\n";
    
    echo "Processing invoice...\n";
    echo "---------------------\n";
    
    $result = $processor->processInvoice($invoiceData);
?>