
            INSERT INTO dbo.FRLLOExportJournalLog
            (FRLLOExportType_id,
             FRLLOExportJournalLog_DocID,
             Person_id,
             EvnRecept_id,
             ReceptOtov_id,
             relise_id,
             pmUser_insID,
             pmUser_updID,
             FRLLOExportJournalLog_insDT,
             FRLLOExportJournalLog_updDT)
            SELECT 3,
                   NewId(),
                   er.Person_id,
                   er.EvnRecept_id,
                   ro.ReceptOtov_id,
                   fr.relise_id,
                   '395071589897',
                   '395071589897',
                   dt.curDate,
                   dt.curDate
            FROM dbo.v_EvnRecept AS er
                     INNER JOIN (SELECT CAST(localtimestamp AS TIMESTAMP WITH TIME ZONE) AS curDate) AS dt ON 1 = 1
                     LEFT JOIN dbo.v_FRLLORecept AS fr ON fr.EvnRecept_id = er.EvnRecept_id
                     INNER JOIN dbo.ReceptOtov AS ro ON ro.EvnRecept_id = er.EvnRecept_id

                     LEFT JOIN dbo.v_Lpu AS vlpu ON vlpu.Lpu_id = er.Lpu_id
                     LEFT JOIN dbo.v_OrgNotExportFRLLO AS onef ON onef.Org_id = vlpu.Org_id

                     LEFT JOIN dbo.ReceptValid AS rv ON rv.ReceptValid_id = er.ReceptValid_id

                     LEFT JOIN dbo.v_FRLLOExportJournalLog AS fejl
                               ON fejl.Person_id = er.Person_id
                                   AND fejl.EvnRecept_id = er.EvnRecept_id
                                   AND fejl.FRLLOExportType_id = 3
                                   AND (fejl.FRLLOExportJournal_id IS NULL OR
                                        fejl.FRLLOExportJournal_id IN (910101000338664))
                     LEFT JOIN LATERAL (SELECT FRLLOExportJournalLog_id
                                        FROM dbo.v_FRLLOExportJournalLog
                                        WHERE EvnRecept_id = er.EvnRecept_id
                                          AND FRLLOExportType_id = 3
                                        ORDER BY v_FRLLOExportJournalLog.FRLLOExportJournalLog_id DESC
                                        LIMIT 1) AS fejls ON TRUE
                     LEFT JOIN LATERAL (SELECT FRLLOFlkErrorLog_id
                                        FROM dbo.v_FRLLOFlkErrorLog
                                        WHERE FRLLOExportJournalLog_id = fejls.FRLLOExportJournalLog_id
                                        LIMIT 1) AS ffel ON TRUE
            WHERE fejl.FRLLOExportJournalLog_id IS NULL
              AND ((
                           (ro.EvnRecept_OtpDate BETWEEN '2022-01-01 00:00:00.000' AND '2022-12-31 23:59:59.998')
                           AND ro.ReceptDelayType_id = 1
                       )
                OR (
                           fr.relise_id IS NULL
                           AND ro.EvnRecept_otpDate IS NULL
                           AND ro.EvnRecept_obrDate IS NULL
                           AND to_char(
                                   CASE
                                       WHEN rv.ReceptValidType_id = 1 THEN (er.EvnRecept_setDt + INTERVAL '1 day' * rv.ReceptValid_Value) + INTERVAL '1 day'
                                       WHEN rv.ReceptValidType_id = 2 THEN (er.EvnRecept_setDt + INTERVAL '1 day' * rv.ReceptValid_Value * 30) + INTERVAL '1 day'
                                       WHEN rv.ReceptValidType_id = 3 THEN (er.EvnRecept_setDt + INTERVAL '1 day' * rv.ReceptValid_Value * 360) + INTERVAL '1 day'
                                       END
                               , 'yyyy-mm-dd') BETWEEN '2022-01-01 00:00:00.000' AND '2022-12-31 23:59:59.998'
                       ))
            LIMIT 50000
        
