<?php

// Plugin Check / PHPCS: category-level exclusions with reasons.
// Line-level findings in this file are handled individually below.
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- PaintOps keeps its records in its own custom tables (built by paintops_table()); $wpdb is the supported API for plugin-owned tables, there is no core CRUD alternative, and results are request-scoped.
/**
 * PaintOps - Profit Per Hour Rankings
 *
 * Self-contained helper for showing the top and bottom jobs by profit per hour.
 *
 * Usage:
 * require_once __DIR__ . '/job-profit-hour-rankings-v2.php';
 * echo wp_kses(paintops_render_profit_per_hour_rankings(3), paintops_kses_allowed_html(), paintops_kses_protocols());
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// end paintops_fs()->is__premium_only()
/**
 * Finds the quote attached to a job.
 * Checks estimateID first, then falls back to latest estimate by jobID.
 *
 * DELIBERATELY FREE (2026-07-22 audit, HIGH fix): this is the safety-net
 * fallback paintops_job_financial_summary() uses when no gross_amount was ever
 * saved, which on the FREE tier is every job (the Financials save is
 * premium). It was accidentally wrapped in the premium block during the
 * 07-21 restructure, which zeroed gross on free builds and killed the
 * free Balance Due badge + Mark as Paid flow. Keep it OUTSIDE any
 * is__premium_only block.
 */
if ( !function_exists( 'paintops_get_job_quote_for_financials' ) ) {
    function paintops_get_job_quote_for_financials(  $job_id, $estimate_id = 0  ) {
        global $wpdb;
        $job_id = (int) $job_id;
        $estimate_id = (int) $estimate_id;
        $quote = null;
        if ( $estimate_id > 0 ) {
            $quote = $wpdb->get_row( $wpdb->prepare( "SELECT estID, estTotal\n                     FROM %i\n                     WHERE estID = %d\n                     LIMIT 1", paintops_table( 'estimates' ), $estimate_id ) );
        }
        if ( !$quote && $job_id > 0 ) {
            $quote = $wpdb->get_row( $wpdb->prepare( "SELECT estID, estTotal\n                     FROM %i\n                     WHERE jobID = %d\n                     ORDER BY estID DESC\n                     LIMIT 1", paintops_table( 'estimates' ), $job_id ) );
        }
        if ( !$quote ) {
            return [
                'found'       => false,
                'estimate_id' => 0,
                'total'       => 0,
            ];
        }
        return [
            'found'       => true,
            'estimate_id' => (int) $quote->estID,
            'total'       => (float) $quote->estTotal,
        ];
    }

}
// end paintops_fs()->is__premium_only()