DO clos_data
IF m.issql
    DO open_database
    USE ("ps_rfooddat") ALIAS food_dat
    INDEX on id TO (m.gcpri_path + "id_food_dat")
ELSE
    use (m.gchom_path + "food_dat") noupdate order id
ENDIF 

set filt to ("MAN" $ pgm .and. (! "KAN" $ pgm_param))
DEFI POPUP strediska FROM 1,22 PROMPT FIELD " " + id_zkratka + " | " + id + " " FONT "Courier NEW"
ON SELE POPUP strediska DEACTI POPUP
ACTI POPUP strediska
IF .not. EMPTY(PROMPT())
    DO u_ex_obr2 with id, allt(dat_path), c_stredisk, pgm_param
ENDIF 
RELEASE POPUP strediska
DO clos_data


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
PROCEDURE u_ex_obr2
param id_man, src_path, c_stredisk, skl_param

ostatni = "___ostatni"
old_errh = ON("ERROR")
store "" to datum1, datum2

IF m.issql
    DO open_database
    m.cdata = 1
    USE (m.sqlprefix+"rdokladyneexportovane") ALIAS dokl_part
    INDEX on c_dokl TO (m.gcpri_path + "dokl_part_c_dokl") FOR datum_dokl > DATE()-77

ELSE
    fn_obra = m.src_path + "obraty"
    fn_dokl = m.src_path + "doklady"

    use (m.src_path + "partneri") Order c_partner IN 0 NOUPDATE 
    USE (fn_dokl) alias doklady   ORDER c_dokl    IN 0 NOUPDATE 
    SELECT c_dokl, c_stredisk, operace, partneri.partner, doklad, pz1, datum_dokl, doklady.c_partner ;
           WHERE datum_dokl > DATE()-77 AND c_stredisk = m.c_stredisk FROM (m.fn_dokl) LEFT JOIN partneri ON partneri.c_partner = doklady.c_partner INTO CURSOR dokl_part
ENDIF 

if ! prazdna(.t.)
  SET FILTER TO c_stredisk = m.c_stredisk .and. datum_dokl > DATE()-77

  DEFI POPUP datumy FROM 1,1 TITLE " Pocatek " font "Courier NEW" ;
       PROMPT FIELD " " + str(c_dokl) + " | " + operace + " " + IIF(operace # "KK", "| " + IIF(ISNULL(partner), PADC("##" + STR(c_partner, 9) + " ##", 30), partner) + " | " + STR(doklad, 6) + " |", PADC("  " + ALLTRIM(pz1) + "  ", 43, "*")) + " " + dtoc(datum_dokl)

  ON SELE POPUP datumy DEACTI POPUP
  ACTI POPUP datumy
  IF ! EMPTY(PROMPT())
    datum1 = c_dokl
    oper1  = operace + " "
    SET FILTER TO c_stredisk = m.c_stredisk .and. c_dokl >= m.datum1
    ON SELE POPUP datumy DEACTI POPUP
    ACTI POPUP datumy
    IF ! EMPTY(PROMPT())
      datum2 = c_dokl
      oper2  = operace + " "

      insert into (m.gcucto_path + "u_ex") (md, dal, hs_md, hs_dal, id_man, md2,        datum_dokl,                     pz,                                                                     pz3,    poznamka) ;
             values ("EXPORT", "EXPORT", m.id_man, m.id_man, m.id_man, str(seco()*50, 7), DATE(), m.oper1 + " " + ALLTRIM(str(m.datum1,9)) + "    " + m.oper2 + " " + ALLTRIM(str(m.datum2,9)), "OBR-", "OBR "+m.id_man)

      u_ex_obr_had = createobject("had", program())
      u_ex_obr_had.show()

      IF m.issql
          DO uexobr WITH m.id_man, 1, m.c_stredisk, m.skl_param, m.datum1, m.datum2
      ELSE
          do priprava
          u_ex_obr_had.set_had(44)
          do exp_obr
      ENDIF 
      u_ex_obr_had.set_had(88)

      DO generuj_souctovy_soubor IN ucto_exp
      release u_ex_obr_had

    ENDIF
  ENDIF
ENDIF
CLOSE DATABASES 


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc exp_obr
use (m.gcucto_path + "u_ex_obr") in 0

sele obr
scan
  scatt memvar
  sele u_ex_obr

  locate for id_man=m.id_man .and. operace=m.operace .and. partner=m.partner .and. cen_hlad=m.cen_hlad .and. INLIST(druh, m.druh, "*")

  if found()		&& konkretni partner
    do jeden_export with m.partner

  else			    && vsichni ostatni partneri

    locate for id_man=m.id_man .and. operace=m.operace .and. partner="*" .and. cen_hlad=m.cen_hlad .and. INLIST(druh, m.druh, "*")
    if ! found()
      insert into (m.gcucto_path + "u_ex_obr") ;
             (  id_man,   operace,   partner,   cen_hlad,   druh,   co,             pz,         poznamka) value ;
             (m.id_man, m.operace, m.partner, m.cen_hlad, m.druh, "prachy", "operace + partner", "OBR " + m.id_man + " " + m.operace + " " + m.partner)
    endif

    do jeden_export with "*"
  endif

  sele obr
endscan

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc jeden_export
param jedenpartner

locate for id_man=m.id_man .and. operace=m.operace .and. partner=m.jedenpartner .and. cen_hlad=m.cen_hlad .and. druh = m.druh
IF FOUND()
    scan for id_man=m.id_man .and. operace=m.operace .and. partner=m.jedenpartner .and. cen_hlad=m.cen_hlad .and. druh = m.druh
        scatt field md, dal, hs_md, hs_dal, md2, dal2, co, poznamka memvar

        TRY 
            pz = EVALUATE(pz)
        CATCH 
            pz = "ERR:" + m.pz
        ENDTRY 
 
        TRY 
            castka = EVALUATE(co)
        CATCH 
            castka = 99999.99
            pz = "ERR:" + m.co
        ENDTRY 

        pz3 = "OBR-" + m.operace + IIF( EMPTY(obr.pz3), STR(c_partner), obr.pz3)
        if m.castka # 0
          insert into (m.gcucto_path + "u_ex") from memvar
        endif
        sele u_ex_obr
    endscan
ELSE
    scan for id_man=m.id_man .and. operace=m.operace .and. partner=m.jedenpartner .and. cen_hlad=m.cen_hlad .and. druh = "*"
        scatt field md, dal, hs_md, hs_dal, md2, dal2, co, poznamka memvar

        TRY 
            pz = EVALUATE(pz)
        CATCH 
            pz = "ERR:" + m.pz
        ENDTRY 
 
        TRY 
            castka = EVALUATE(co)
        CATCH 
            castka = 99999.99
            pz = "ERR:" + m.co
        ENDTRY 

        pz3 = "OBR-" + m.operace + IIF( EMPTY(obr.pz3), STR(c_partner), obr.pz3)
        if m.castka # 0
          insert into (m.gcucto_path + "u_ex") from memvar
        endif
        sele u_ex_obr
    endscan
ENDIF


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc priprava

SELECT doklady
set relation to c_partner into partneri
SET ORDER TO c_dokl
use (m.src_path + "skupiny") Order druh    IN 0
use (m.src_path + "karty")   ORDER c_karty IN 0

sele c_dokl, operace, c_karty, dan, sum(mnozstvi * cena) as prachy, space(10) as druh, SPACE(5) as platba ;
     where c_stredisk = m.c_stredisk and betw(c_dokl, m.datum1, m.datum2) and inlist( left(operace, 1), "P", "V", "S") ;
     group by 1, 2, 3, 4 from (m.fn_obra) into table (m.gcpri_path + "obr_1")

set Relation To c_dokl into doklady
REPLACE FOR INLIST(operace, "VE", "SF") platba WITH doklady.platba
set Relation To c_karty into karty


*   *   *   *   *   *   UCTOVANI PODLE SKUPIN   *   *   *   *   *   *   * zacatek
if "SKL" $ m.skl_param
    REPL ALL druh WITH karty.druh_skl
    REPLACE FOR EMPTY(druh) druh WITH karty.druh
else
    REPL ALL druh WITH karty.druh
    REPLACE FOR EMPTY(druh) druh WITH karty.druh_skl
endif
*   *   *   *   *   *   UCTOVANI PODLE SKUPIN   *   *   *   *   *   *   * konec


*   *   *   *   *   *   UCTOVANI PODLE SPARTU   *   *   *   *   *   *   * zacatek
IF "_BUDVA" $ UPPER(m.gchom_path) .or. "_MASO" $ UPPER(m.gchom_path) .or. "\D_RESTLIPA" $ UPPER(m.gchom_path)
    SET RELATION TO druh INTO skupiny
    REPL ALL druh WITH skupiny.spart
ENDIF 
*   *   *   *   *   *   UCTOVANI PODLE SPARTU   *   *   *   *   *   *   * konec

set rela to
REPL FOR EMPTY(druh) druh WITH m.ostatni

sele str(c_dokl) as datum, operace, " " as cen_hlad, druh, sum(prachy) as prachy, m.id_man as id_man, ;
     space(25) as partner, space(15) as doklad, {^1111-11-11} as datum_dokl, space(20) as faktura, SPACE(15) as pz2, 00000 as c_partner, SPACE(20) as pz3 ;
     group by 1, 2, 3, 4 from (m.gcpri_path + "obr_1") into table (m.gcpri_path + "obr")

set rela to val(datum) into doklady
repl all partner with IIF(EMPTY(partneri.partner), "## " + ALLTRIM(STR(doklady.c_partner)), partneri.partner), ;
         doklad with str(doklady.doklad), datum_dokl with doklady.datum_dokl, ;
         faktura with allt(doklady.pz1), cen_hlad WITH doklady.cen_hlad, pz2 with allt(doklady.pz2), ;
         c_partner WITH doklady.c_partner, pz3 WITH "  " + partneri.naz6
set rela to
REPLACE FOR ! EMPTY(cen_hlad) faktura WITH ALLTRIM(faktura) + " (" + ALLTRIM(cen_hlad) + ")"

repl for operace # "VK" cen_hlad with ""
repl for operace = "VK" .and. empt(cen_hlad) cen_hlad with "1"
