datum1 = DATE(YEAR(DATE()), MONTH(DATE()), 25)
CREATE TABLE (m.gcpri_path + "mesice") (hranice D, hranice1 C(7))
FOR iii = 1 TO 16
     datum1 = GOMONTH(m.datum1, -1)
     INSERT INTO (m.gcpri_path + "mesice") (hranice, hranice1) ;
            VALUES (m.datum1, STR(YEAR(m.datum1),4) + "-" + TRANSFORM(month(m.datum1), "@L 99"))
ENDFOR
GO TOP 
STORE hranice TO hranice_zac, hranice_kon
d1_txt = hranice1 + "-00"
d2_txt = hranice1 + "-33"

DEFINE POPUP datumy FROM 1,55 PROMPT FIELD hranice1 TITLE jaz("form_vyber_zaciatok")
ON SELECTION POPUP datumy deacti popup
ACTIVATE POPUP datumy
IF .not. EMPTY(PROMPT())
   d1_txt = hranice1 + "-00"         && pro hlavicky reportu
   hranice_zac = hranice
   SET FILTER TO hranice >= m.hranice_zac
   DEFINE POPUP datumy FROM 1,55 PROMPT FIELD hranice1 TITLE jaz("form_vyber_koniec")
   ON SELECTION POPUP datumy deacti popup
   GO BOTTOM 
   ACTIVATE POPUP datumy
   IF .not. EMPTY(PROMPT())
      d2_txt = hranice1 + "-33"         && pro hlavicky reportu
      hranice_kon = hranice
   ENDIF 
ENDIF 
RELEASE POPUPS datumy
USE 
fn_obra_s = m.gcpri_path + "obra_spoj"
fn_dokl_s = m.gcpri_path + "dokl_spoj"
c_partner_all = -987654321

ERASE (m.gcpri_path + "????_spoj")

SELECT 0
use (m.fn_partneri) again alias p_p_p order c_partner
LOCATE FOR c_partner = m.c_partner_all
IF .not. FOUND()
    INSERT INTO (m.fn_partneri) (c_partner, partner) VALUES (m.c_partner_all, ">>SKUPINA<<")
ENDIF 

use (m.fn_obraty)   again alias o_o_o noupdate in 0
use (m.fn_doklady)  again alias d_d_d noupdate in 0

create table (m.gcpri_path + "konce") ( c_stredisk I, id_zkratka C(30), ;
             pz1_zac C(25), pz1_kon C(25), c_dokl_zac I, c_dokl_kon I, ddokl_zac D, ddokl_kon D, lbl L, nabidka C(77))

select c_dokl, c_stredisk, pz1, datum_dokl where operace = "KK" ;
       order by c_dokl from (m.fn_doklady) into table (m.gcpri_path + "konce_mesice")
SCAN
    SCATTER MEMVAR 
    SELECT konce
    LOCATE FOR c_stredisk = m.c_stredisk
    IF .not. FOUND()
        APPEND BLANK 
        REPLACE c_stredisk WITH m.c_stredisk
    ENDIF 
    DO CASE 
    CASE m.datum_dokl < m.hranice_zac
         REPLACE c_dokl_zac WITH m.c_dokl, pz1_zac WITH m.pz1, ddokl_zac WITH m.datum_dokl, c_dokl_kon WITH 0
    CASE EMPTY(c_dokl_kon) .and. m.c_dokl > c_dokl_zac .and. m.datum_dokl >= m.hranice_kon
         REPLACE c_dokl_kon WITH m.c_dokl, pz1_kon WITH m.pz1, ddokl_kon WITH m.datum_dokl
    ENDCASE
ENDSCAN 
SELECT konce
REPLACE FOR EMPTY(c_dokl_kon) c_dokl_kon WITH 99999999, pz1_kon WITH "--- DO DNES ---"
set relation to c_stredisk into p_p_p
replace all id_zkratka with p_p_p.partner
REPLACE FOR ISNULL(id_zkratka) id_zkratka WITH ""
REPLACE FOR EMPT(id_zkratka)   id_zkratka WITH "###  " + ALLTRIM(STR(c_stredisk)) + "  ###"
REPLACE ALL nabidka WITH id_zkratka + ALLTRIM(pz1_zac) + " - " + ALLTRIM(pz1_kon), lbl WITH .T.

INDEX on id_zkratka TAG id_zkratka
*go top
*browse field c_stredisk, id_zkratka, pz1_zac, pz1_kon, c_dokl_zac, c_dokl_kon, ddokl_zac, ddokl_kon noappe noedit
DO FORM selpartn WITH "konce","nabidka"

gcid_zkratka = ""
select konce
had_sovs = createobject("had", jaz("form_zlucenie_obratov"))
had_sovs.show()
had_sovs.set_max(recc())
SCAN FOR lbl
    gcid_zkratka = gcid_zkratka + LEFT(konce.id_zkratka, 2) + " "
    had_sovs.increment(jaz("form stredisko")+": " + konce.id_zkratka)
    c_dokl_zac   = konce.c_dokl_zac 
    c_dokl_kon   = konce.c_dokl_kon
    gic_stredisk = konce.c_stredisk

    IF LEN(m.operace)=2 
        select *, doklad as ndoklad, c_stredisk as o_c_stred where c_stredisk = m.gic_stredisk and between(c_dokl, m.c_dokl_zac, m.c_dokl_kon) and (operace LIKE m.operace) ;
               from d_d_d into table (m.gcpri_path + "dd1str")
    ELSE
        select *, doklad as ndoklad, c_stredisk as o_c_stred where c_stredisk = m.gic_stredisk and between(c_dokl, m.c_dokl_zac, m.c_dokl_kon) and inlist(operace, &operace__txt) ;
               from d_d_d into table (m.gcpri_path + "dd1str")
    ENDIF 
    REPLACE ALL c_stredisk WITH m.c_partner_all
    ALTER TABLE (m.gcpri_path + "dd1str") alter doklad C(9)
    select *, c_stredisk as o_c_stred where c_dokl in (SELECT c_dokl FROM (m.gcpri_path + "dd1str")) ;
           from o_o_o into table (m.gcpri_path + "oo1str")
    REPLACE ALL c_stredisk WITH m.c_partner_all

    if select ("dokl_spoj") > 0
        select dokl_spoj
        append from (m.gcpri_path + "dd1str")
        select obra_spoj
        append from (m.gcpri_path + "oo1str")
    else
        select * from (m.gcpri_path + "dd1str") into table (m.gcpri_path + "dokl_spoj")
        INDEX on c_dokl TAG c_dokl
        select * from (m.gcpri_path + "oo1str") into table (m.gcpri_path + "obra_spoj")
        INDEX on c_dokl TAG c_dokl
    endif
   
ENDSCAN
gic_stredisk = c_partner_all    &&&& m.o_gic_stredisk
DO pozavirej WITH "d_d_d, o_o_o, p_p_p, dd1str, oo1str, konce, dokl_spoj, obra_spoj"
release had_sovs
