<?php
// Test script to verify item creation creates accounting entry
header('Content-Type: application/json');

require_once 'config/database.php';
require_once 'models/Item.php';
require_once 'models/AccountingEntry.php';

try {
    $database = new Database();
    $db = $database->getConnection();
    
    if (!$db) {
        echo json_encode(['success' => false, 'message' => 'Database connection failed']);
        exit;
    }

    // Test data for item creation
    $test_item_data = [
        'name' => 'Test Item for Accounting',
        'description' => 'Test item to verify accounting entry creation',
        'quantity' => 2,
        'price' => 100.00,
        'date_of_purchase' => '2024-06-15'
    ];

    // Create item
    $item = new Item($db);
    $item->name = $test_item_data['name'];
    $item->description = $test_item_data['description'];
    $item->quantity = $test_item_data['quantity'];
    $item->price = $test_item_data['price'];
    $item->date_of_purchase = $test_item_data['date_of_purchase'];

    if ($item->create()) {
        // Create corresponding accounting entry
        $accounting_entry = new AccountingEntry($db);
        
        // Calculate accounting entry fields
        $total_amount = floatval($test_item_data['price']) * intval($test_item_data['quantity']);
        $vat_percentage = 25.50;
        $net_amount = $total_amount / (1 + ($vat_percentage / 100));
        $vat_amount = $total_amount - $net_amount;
        $tax_free_amount = $net_amount;
        
        // Set accounting entry properties
        $accounting_entry->entry_date = $test_item_data['date_of_purchase'];
        $accounting_entry->description = $test_item_data['name'];
        $accounting_entry->entry_type = 'Kulu';
        $accounting_entry->category = '222';
        $accounting_entry->tax_free_amount = $tax_free_amount;
        $accounting_entry->vat_percentage = $vat_percentage;
        $accounting_entry->vat_25_5 = $vat_amount;
        $accounting_entry->vat_14 = 0;
        $accounting_entry->vat_10 = 0;
        $accounting_entry->total_amount = $total_amount;
        $accounting_entry->net_amount = $net_amount;
        $accounting_entry->vat_amount = $vat_amount;
        $accounting_entry->reference_number = '';
        
        // Create accounting entry
        if ($accounting_entry->create()) {
            echo json_encode([
                'success' => true,
                'message' => 'Item and accounting entry created successfully',
                'test_data' => [
                    'total_amount' => $total_amount,
                    'net_amount' => $net_amount,
                    'vat_amount' => $vat_amount,
                    'tax_free_amount' => $tax_free_amount
                ]
            ]);
        } else {
            echo json_encode([
                'success' => false,
                'message' => 'Item created but accounting entry creation failed'
            ]);
        }
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'Item creation failed'
        ]);
    }
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>
