<?php
/**
 * Fix existing voucher transactions that have price = 0
 * This script updates voucher transactions to show actual plan prices
 */

require_once 'system/autoload/PEAR2/Autoload.php';

// Get all voucher transactions with price = 0
$voucher_transactions = ORM::for_table('tbl_transactions')
    ->where('method', 'Voucher')
    ->where('price', 0)
    ->find_many();

echo "Found " . count($voucher_transactions) . " voucher transactions with price 0\n";

$updated_count = 0;

foreach ($voucher_transactions as $transaction) {
    // Get the plan details to find the actual price
    $plan = ORM::for_table('tbl_plans')
        ->where('id', $transaction['plan_id'])
        ->find_one();
    
    if ($plan && $plan['price'] > 0) {
        // Update the transaction with the actual plan price
        $transaction->price = $plan['price'];
        $transaction->save();
        $updated_count++;
        
        echo "Updated transaction ID {$transaction['id']}: {$transaction['plan_name']} - Price: {$plan['price']}\n";
    }
}

echo "\nSuccessfully updated $updated_count voucher transactions\n";
echo "All voucher transactions now show correct plan prices!\n";
?>
