
<!-- Add this to the Action Buttons section in view_consultation.php -->
<!-- Around line 160-170, after the existing buttons -->

<?php
// Check if billing enabled for this patient type
$billing_settings = $db->prepare("SELECT billing_enabled_for_guests, billing_enabled_for_staff FROM branches WHERE id = ?");
$billing_settings->execute([$branch_id]);
$billing_config = $billing_settings->fetch();

$billing_enabled = ($consultation['patient_type'] === 'guest') 
    ? $billing_config['billing_enabled_for_guests'] 
    : $billing_config['billing_enabled_for_staff'];

// Check if bill already exists
$bill_check = $db->prepare("SELECT id, payment_status FROM billing WHERE consultation_id = ?");
$bill_check->execute([$consultation['id']]);
$existing_bill = $bill_check->fetch();

if ($billing_enabled && !empty($prescriptions)):
    if ($existing_bill): ?>
        <a href="../billing/view_bill.php?id=<?php echo $existing_bill['id']; ?>" class="btn btn-primary">
            <i class="fas fa-file-invoice-dollar"></i> View Bill
            <?php if ($existing_bill['payment_status'] === 'paid'): ?>
                <span class="badge badge-success" style="margin-left:5px">Paid</span>
            <?php elseif ($existing_bill['payment_status'] === 'complimentary'): ?>
                <span class="badge badge-primary" style="margin-left:5px">Complimentary</span>
            <?php else: ?>
                <span class="badge badge-warning" style="margin-left:5px">Pending</span>
            <?php endif; ?>
        </a>
    <?php else: ?>
        <a href="../billing/create_bill.php?consultation_id=<?php echo $consultation['id']; ?>" class="btn btn-success">
            <i class="fas fa-file-invoice-dollar"></i> Create Bill
        </a>
    <?php endif; ?>
<?php endif; ?>

