Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patient dashboard loading is delayed due to the slow execution of demographics.php #7869

Open
Valcz opened this issue Dec 11, 2024 · 2 comments

Comments

@Valcz
Copy link

Valcz commented Dec 11, 2024

Describe the bug
The patient dashboard takes 2-3 minutes to load when a patient has over 500 encounters, causing significant delays in workflow. The issue appears to be related to processing a large volume of encounter data during dashboard loading. Optimizing performance in such cases is essential to improve usability and efficiency.

Ref:
https://community.open-emr.org/t/patient-dashboard-loading-is-delayed-due-to-the-slow-execution-of-demographics-php/23986

To Reproduce
Steps to reproduce the behavior:

Open Patient Dashboard having 700+ encounter & visits
Expected behavior
takes 2-3 min load dashboard and demographic view

@Valcz
Copy link
Author

Valcz commented Dec 11, 2024

Replace the get_patient_balance method in library/patient.inc.php file. In this method optimized select statement to load in 1-2 seconds.

function get_patient_balance($pid, $with_insurance = false, $eid = false)
{
    $bindingValue = [];
    if ($with_insurance) {
        $bindingValue[] = 1;
    } else {
        $bindingValue[] = 0;
    }
    $bindingValue[] = $pid;

    $appendSql = '';
    if ($eid) {
        $appendSql = " AND encounter = ?";
        $bindingValue[] = $eid;
    }
    $sql = "select
      sum(
        CASE WHEN ? = 1
        AND frresult.last_level_closed < frresult.insurance_data_count
        AND frresult.stmt_count = 0 THEN (
          SELECT
            COALESCE(
              SUM(b.fee),
              0.00
            ) - COALESCE(
              SUM(a.pay_amount),
              0.00
            ) AS ptbal
          FROM
            billing b
            LEFT JOIN ar_activity a ON b.pid = a.pid
            AND b.encounter = a.encounter
            AND a.deleted IS NULL
            AND a.payer_type = 0
          WHERE
            b.pid = frresult.pid
            AND b.encounter = frresult.encounter
            AND b.code_type = 'copay'
            AND b.activity = 1
        ) ELSE (
SELECT
 ( (
    SELECT
      COALESCE(SUM(fee), 0)
    FROM
      billing
    WHERE
      pid = frresult.pid
      AND encounter = frresult.encounter
      AND activity = 1
  ) + (
    SELECT
      COALESCE(SUM(fee), 0)
    FROM
      drug_sales
    WHERE
      pid = frresult.pid
      AND encounter = frresult.encounter
  ) - (
    SELECT
      COALESCE(SUM(pay_amount), 0) + COALESCE(SUM(adj_amount), 0)
    FROM
      ar_activity
    WHERE
      deleted IS NULL
      AND pid = frresult.pid
      AND encounter = frresult.encounter
  )
) as amount
) END
      ) AS balance
    from
      (
        SELECT
          fe.pid,
          fe.encounter,
          fe.last_level_billed,
          fe.last_level_closed,
          fe.stmt_count,
          (
            SELECT
              count(idata.id)
            FROM
              insurance_data idata
            WHERE
              idata.pid = fe.pid
              AND idata.type IN (
                'primary', 'secondary', 'tertiary'
              )
              AND (
                idata.date <= DATE(fe.date)
                OR idata.date IS NULL
              )
              AND idata.provider IS NOT NULL
              AND idata.provider != ''
            ORDER BY
              FIELD(
                idata.type, 'primary', 'secondary',
                'tertiary'
              ),
              idata.date DESC
            LIMIT
              3
          ) as insurance_data_count
        FROM
          form_encounter fe
        WHERE
          pid = ? " . $appendSql . "
      ) as frresult ;";

    $result = sqlQuery($sql, $bindingValue);
    $balance = !empty($result['balance']) ? number_format($result['balance'], 2, '.', '') : "0.00";
    return sprintf('%01.2f', $balance);
}

@stephenwaite
Copy link
Member

thanks @Valcz , maybe breaking out this function into a class and method in the src/Billing aka OpenEMR\Billing namespace might be a better way to proceed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants