<?php

require("autentificare.php");
require("{$sitepath}pagecomponents.php");

if ($error) $errorMessage = $error;
       else $errorMessage = "";

$pagina = getuserdata('pagina', 'validare');

$data_lista = getuserdata('data_lista', '');
$text_cautat = getuserdata('text_cautat', '');

$start_time = time();
echo "<br>Start: ".strftime('%c', $start_time);
$connMain->query("SELECT
    c2.IdCasCompensare AS ID,
    m4.Cod AS CodCAS,
    m4.Denumire AS Medicament,
    m4.DCI,
    p4.Nume AS Producator,
    m4.CantitatePePachet,
    c2.Sublista,
    c2.CodBoala,
    c2.CodPNS,
    c2.PretMaximUT,
    c2.ValoareCompensare,
    c2.PretMaxim,
    c2.ValoareCompensare,
    c2.PretMaxim - c2.ValoareCompensare AS ValoareDePlata
FROM (
    SELECT
        m2.IdCasMedicament,
        m2.PretAmanuntUT
    FROM (
        SELECT
            m1.IdCasMedicament,
            MIN(c1.PretMaximUT) AS PretAmanuntUT
        FROM cas_medicamente m1
            JOIN cas_lista l1 ON l1.IdMedicament = m1.IdCasMedicament
            JOIN cas_compensari c1 ON c1.IdCasCompensare = l1.IdCompensare
        WHERE DATE('2010-07-05') BETWEEN m1.DataInceput AND IFNULL(m1.DataSfarsit, DATE('9999-12-31'))
        GROUP BY m1.IdCasMedicament, m1.Denumire
        ORDER BY PretAmanuntUT
        ) m2
    LIMIT 0, 100
    ) m3
    JOIN cas_medicamente m4 ON m4.IdCasMedicament = m3.IdCasMedicament
    JOIN cas_producatori p4 ON p4.IdProducator = m4.IdProducator
    JOIN cas_lista l2 ON l2.IdMedicament = m4.IdCasMedicament
    JOIN cas_compensari c2 ON c2.IdCasCompensare = l2.IdCompensare
ORDER BY m3.PretAmanuntUT, c2.Sublista, c2.CodBoala, c2.CodPNS");
$end_time = time();
echo "<br>End: ".strftime('%c', $end_time);
$duration = $end_time - $start_time;
$hour = floor($duration / 3600);
$duration = $duration % 3600;
$minute = floor($duration / 60);
$second = $duration % 60;
echo "<br>Time: ".str_pad($hour, 2, '0').':'.str_pad($minute, 2, '0').':'.str_pad($second, 2, '0');


?>