<?php
/**
 * Financial Year Bill Sequence Management
 * Handles bill number generation per financial year (April 1 - March 31)
 */

/**
 * Get current financial year details
 * @return array Financial year start, end, and label
 */
function getCurrentFinancialYear() {
    $currentYear = date('Y');
    $currentMonth = date('n');
    
    // Financial year: April (4) to March (3)
    if($currentMonth >= 4) {
        $fyStart = $currentYear . '-04-01';
        $fyEnd = ($currentYear + 1) . '-03-31';
        $fyLabel = $currentYear . '-' . ($currentYear + 1);
    } else {
        $fyStart = ($currentYear - 1) . '-04-01';
        $fyEnd = $currentYear . '-03-31';
        $fyLabel = ($currentYear - 1) . '-' . $currentYear;
    }
    
    return array(
        'start' => $fyStart,
        'end' => $fyEnd,
        'label' => $fyLabel
    );
}

/**
 * Get next bill number for current financial year
 * @param resource $con Database connection
 * @return int Next bill number
 */
function getNextBillNumber($con) {
    global $con;
    
    $fy = getCurrentFinancialYear();
    $fyLabel = $fy['label'];
    $fyStart = $fy['start'];
    $fyEnd = $fy['end'];
    
    // Check if financial year entry exists
    $checkFY = f(q("SELECT * FROM tbl_financial_year_bill_seq WHERE year_label='$fyLabel'"));
    
    if($checkFY['seq_id'] == '') {
        // Insert new financial year with bill number starting from 0 (will be incremented to 1)
        mysqli_query($con, "INSERT INTO tbl_financial_year_bill_seq 
            (financial_year_start, financial_year_end, year_label, current_bill_no) 
            VALUES ('$fyStart', '$fyEnd', '$fyLabel', 0)");
    }
    
    // Increment and get next bill number
    mysqli_query($con, "UPDATE tbl_financial_year_bill_seq 
        SET current_bill_no = current_bill_no + 1 
        WHERE year_label='$fyLabel'");
    
    $getFYSeq = f(q("SELECT current_bill_no FROM tbl_financial_year_bill_seq WHERE year_label='$fyLabel'"));
    
    return intval($getFYSeq['current_bill_no']);
}

/**
 * Get current bill number for financial year (without incrementing)
 * @param resource $con Database connection
 * @return int Current bill number
 */
function getCurrentBillNumber($con) {
    $fy = getCurrentFinancialYear();
    $fyLabel = $fy['label'];
    
    $getFYSeq = f(q("SELECT current_bill_no FROM tbl_financial_year_bill_seq WHERE year_label='$fyLabel'"));
    
    return intval($getFYSeq['current_bill_no']);
}

/**
 * Reset bill number for a financial year
 * @param resource $con Database connection
 * @param string $fyLabel Financial year label (e.g., '2025-2026')
 * @return bool Success status
 */
function resetBillNumber($con, $fyLabel = null) {
    if($fyLabel == null) {
        $fy = getCurrentFinancialYear();
        $fyLabel = $fy['label'];
    }
    
    return mysqli_query($con, "UPDATE tbl_financial_year_bill_seq 
        SET current_bill_no = 0 
        WHERE year_label='$fyLabel'");
}

/**
 * Get bill number statistics for all financial years
 * @param resource $con Database connection
 * @return array Financial year bill statistics
 */
function getFinancialYearStats($con) {
    $result = mysqli_query($con, "SELECT * FROM tbl_financial_year_bill_seq ORDER BY financial_year_start DESC");
    
    $stats = array();
    while($row = mysqli_fetch_assoc($result)) {
        $stats[] = $row;
    }
    
    return $stats;
}

?>
