CREATE OR REPLACE VIEW food600.zoznam_uctov_s_uza
 AS
 SELECT min(sk__plt.idriadok) AS id,
    sk__plt.ucet,
    sum(sk__plt.cena_pl) AS cena_pl,
        CASE
            WHEN sum(sk__plt.cena_pl) < 0::numeric THEN 1
            ELSE 0
        END AS storno,
    sk__plt.c_uzaverka,
    sk__uza.uzaverka,
    sk__uza.id_kas,
    sk__fin.stul,
    sk__fin.autor,
    sksez.uct_den,
    string_agg(concat(rtrim(druhy_pl.j0::text), ' ', sk__plt.cena_pl), ', '::text) AS platby,
    sk__fin.u_datetime,
    sk__fin.id_limit,
    sk__fis.fuciarkod,
    sk__fis.nedokonceny,
    sk__fis.prn_no,
    (btrim(sk__fin.text_sleva::text) || ','::text) || btrim(sk__fin.text_link::text)::character(16)::text AS text_sleva
   FROM food600.sk__plt
     LEFT JOIN food600.sk__uza ON sk__plt.c_uzaverka = sk__uza.idriadok
     LEFT JOIN food600.sk__fin ON sk__plt.ucet = sk__fin.ucet AND sk__plt.c_uzaverka = sk__fin.c_uzaverka
     LEFT JOIN food600.sk__fis ON sk__plt.ucet = sk__fis.ucet AND sk__plt.c_uzaverka = sk__fis.c_uzaverka
     LEFT JOIN ( SELECT DISTINCT sk__sez.c_uzaverka,
            sk__sez.uct_den
           FROM food600.sk__sez) sksez ON sksez.c_uzaverka = sk__uza.idriadok
     LEFT JOIN food600.druhy_pl ON sk__plt.druh_pl = druhy_pl.nazev
  WHERE sk__plt.kompez = 0
  GROUP BY sk__plt.ucet, sk__uza.uzaverka, sk__uza.id_kas, sk__plt.c_uzaverka, sk__fin.stul, sk__fin.u_datetime, sk__fin.autor, sksez.uct_den, sk__fin.id_limit, sk__fis.fuciarkod, sk__fis.nedokonceny, sk__fin.text_sleva, sk__fin.text_link, sk__fis.prn_no
  ORDER BY sk__uza.id_kas, sk__uza.uzaverka, sk__plt.ucet;
