*:*********************************************************************************************************
*:     Procedure file: SK2OBRATY.PRG
*:
*:      program z eskacek vezme nadefinovane kasy a vyrobi z nich prijemky. vydejce (c_partner) vezme z id_man, prijemce (c_stredisk) z druhu platby
*:      zpracovane uzaverky si oznaci v sk_sez.priznak, takze opakovane spustenei nenapacha zadnou skodu
*:      vzdy z 1 uctu se udela 1 prijemka, do sk_plt.bkp se zapise c_dokl
*:
*:
*: 2022-10-17 - prepocet se vola jen tehdy, kdyz se vubec neco insertovalo do doklady/obraty
*: 2022-10-31 - cena se NEDELI sazbou DPH
*: 2022-11-01 - pridano bourani
*: 2023-08-30 - osetreno nulove mnozstvi
*:            - pridano detailnejsi logovani
*: 2023-09-05 - opraveno bourani
*:            - upraveno logovani
*:
*:*********************************************************************************************************
pocet_dni = 11

#DEFINE VERZE "2023-09-06"
#DEFINE CRLF CHR(13)+CHR(10)
#DEFINE JMENO_LOGU "kasy2obraty"

small_log ("CRLF" , JMENO_LOGU)
small_log ("ver." + VERZE , JMENO_LOGU)
CLOSE DATABASES 

LOCAL skin_oErr AS Exception
sk_otevreno = ""
TRY 
   USE (m.gchom_path + "food_dat ") IN 0 NOUPDATE
   USE (m.gcdat_path + "sk_pol "  ) IN 0 NOUPDATE 
   USE (m.gcdat_path + "sk_plt "  ) IN 0
   USE (m.gcdat_path + "sk_sez "  ) IN 0

   USE (m.gcdat_path + "bourak "  ) IN 0
   USE (m.gcdat_path + "doklady " ) IN 0 ORDER c_dokl 
   USE (m.gcdat_path + "obraty "  ) IN 0
CATCH TO skin_oErr 
   sk_otevreno = "nelze otevrit databazi " + ALLTRIM(STR(skin_oErr.ErrorNo)) + " " + skin_oErr.Message + " Line:" + ALLTRIM(STR(skin_oErr.LineNo))
ENDTRY 

IF EMPTY(m.sk_otevreno) 

    neco_se_insertovalo = .F.
    SELECT doklady
    GO BOTTOM 
    SKIP -99 
    posledni_c_dokl_pred = doklady.c_dokl
    
    SELECT id, c_stredisk, id_zkratka, bour_skup FROM food_dat INTO CURSOR fddat
    INDEX on id TAG id
    USE IN food_dat
    
    SELECT sk_sez
    SET FILTER TO uct_den > DATE() - m.pocet_dni .and. INLIST(id_kas, "Z1", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9") .and. EMPTY(priznak)

    SCAN FOR      uct_den > DATE() - m.pocet_dni .and. INLIST(id_kas, "Z1", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9") .and. EMPTY(priznak)
        SCATTER FIELDS id_kas, uzaverka, uct_den MEMVAR 
        zpracuj_jednu_uzaverku()
        SELECT sk_sez
        REPLACE priznak WITH "PRIJEMKA"
    ENDSCAN 

    DO clos_data
    IF m.neco_se_insertovalo

*        DO gen_stav    WITH m.posledni_c_dokl_pred
        DO prep_all_od WITH 0    && m.posledni_c_dokl_pred
        small_log ("          prepocteno od c_dokl = " + ALLTRIM(STR(m.posledni_c_dokl_pred)), JMENO_LOGU)
    ENDIF 
    small_log ("          hotovo", JMENO_LOGU)
ELSE
    small_log (m.sk_otevreno, JMENO_LOGU)
ENDIF
DO clos_data


*:*********************************************************************************************************
PROCEDURE zpracuj_jednu_uzaverku

kdy      = DATETIME()
bour_skup = "xxx"

SELECT sk_plt
INDEX on druh_pl TO qwe FOR LEFT(druh_pl, 2) == "N_"
SCAN FOR uzaverka == m.uzaverka .and. id_kas == m.id_kas .and. LEFT(druh_pl, 2) == "N_" .and. LEFT(bkp, 8) # "PRIJEMKA"
    
    SCATTER FIELDS u_cis, druh_pl MEMVAR 
    c_stredisk  = - ABS(VAL(SUBSTR(druh_pl, 3)))
    bour_skup = bouraci_skupina(m.c_stredisk)
    small_log (CRLF + " uz/ucet: " + m.uzaverka + "/" + m.u_cis + " stred:" + STR(m.c_stredisk,6) + " bourak:" + m.bour_skup + CRLF, JMENO_LOGU)

    SELECT id_man, c_karty, nazev, mnozstvi, prachy/mnozstvi as cena WHERE uzaverka = m.uzaverka AND u_cis = m.u_cis AND mnozstvi # 0 FROM sk_pol INTO CURSOR obr1 
    GO TOP 
    SCATTER MEMVAR 
    c_partner = cislo_partnera(m.id_man)
    IF m.c_partner # 0
        c_dokl     = i_max("c_dokl")
        doklad     = m.c_dokl
        INSERT INTO doklady (  c_stredisk,   c_dokl,   c_partner,   doklad, datum_dokl, operace,      pz1       ,   kdy,  platba, c_mena, provedl) ;
                     VALUES (m.c_stredisk, m.c_dokl, m.c_partner, m.doklad, m.uct_den ,   "PE" , "SK " + m.u_cis, m.kdy, "KASOU",   1   , "S-K"  ) 
        DO zpracuj_jednu_vydejku
        SELECT sk_plt
        REPLACE bkp WITH "PRIJEMKA " + STR(m.c_dokl), fik WITH TTOC(m.kdy, 3)
        neco_se_insertovalo = .T.
    ENDIF 
ENDSCAN 


*:*********************************************************************************************************
PROCEDURE zpracuj_jednu_vydejku

DO sk_bour
SELECT obr2
SCAN
    SCATTER MEMVAR 
    poradi = RECNO()
    INSERT INTO obraty (  c_stredisk, operace,   c_dokl,   c_karty ,   mnozstvi ,   cena , dan,   poradi,   serial_no) ;
                VALUES (m.c_stredisk,  "PE"  , m.c_dokl, m.c_karty2, m.mnozstvi2, m.cena2, 1.0, m.poradi, m.serial_no) 
ENDSCAN 


*:*********************************************************************************************************
FUNCTION cislo_partnera(id_man)
PRIVATE o_sel, c_par
o_sel = SELECT()
SELECT fddat
LOCATE FOR id = m.id_man
IF FOUND()
    c_par     = fddat.c_stredisk
ELSE
    c_par = 0
    small_log ("   nezname stredisko " + m.id_man + CRLF, JMENO_LOGU)
ENDIF 
SELECT (o_sel)
RETURN m.c_par


*:*********************************************************************************************************
FUNCTION bouraci_skupina(c_str)
PRIVATE o_sel, bour_sk
o_sel = SELECT()
SELECT fddat
LOCATE FOR c_stredisk = m.c_str
IF FOUND()
    bour_sk = fddat.bour_skup
ELSE
    bour_sk = "@"
    small_log ("   nezname stredisko " + ALLTRIM(STR(m.c_str)) + CRLF, JMENO_LOGU)
ENDIF 
SELECT (o_sel)
RETURN m.bour_sk


*:*********************************************************************************************************
PROC sk_bour
SELE b.c_karty, b.mnozstvi, b.cena, b.nazev, ;
     bourak.c_karty2, ROUND(b.mnozstvi * bourak.koef_mnoz, 4) AS mnozstvi2, SPACE(18) + "SK" as serial_no, ;
     ROUND(bourak.koef_ceny, 4) AS cena2 ;
     FROM obr1 b ;
     LEFT JOIN bourak ON bourak.c_karty == b.c_karty AND bourak.bour_skup = m.bour_skup ;
     INTO TABLE (m.gcpri_path + "obr2")
** bacha, v cena2 je koef_ceny !!

REPL FOR ISNULL(c_karty2) c_karty2 WITH c_karty, cena2 WITH cena, mnozstvi2 WITH mnozstvi

SCAN 
*    small_log (ALLTRIM(STR(c_karty)) + " " + nazev + " cena:" + ALLTRIM(STR(cena, 11,2)) + " mnoz:" + ALLTRIM(STR(mnozstvi, 11,3)), JMENO_LOGU)
    txt_do_logu = STR(c_karty,6) + " " + nazev + " cena:" + ALLTRIM(STR(cena, 11,2)) + " mnoz:" + ALLTRIM(STR(mnozstvi, 11,3))
    DO CASE
    CASE c_karty2 = c_karty
        small_log (m.txt_do_logu + "   neboura se", JMENO_LOGU)

    CASE cena2 = 0
        REPL c_karty2 WITH c_karty, cena2 WITH cena, mnozstvi2 WITH mnozstvi
        small_log (m.txt_do_logu + "    cena2(koefceny) = 0 - neboura se", JMENO_LOGU)

    OTHERWISE
        small_log (m.txt_do_logu + "  --> c_karty2:" + ALLTRIM(STR(c_karty2)) + " koef:" + ALLTRIM(STR(cena2, 11,3)), JMENO_LOGU)
        TRY 
            REPL cena2 WITH ROUND(cena / cena2, 4)
        CATCH
            small_log (" koef:" + ALLTRIM(STR(cena2, 11,3)) + " CHYBICKA DELENI", JMENO_LOGU)
        ENDTRY 

    ENDCASE

ENDSCAN 

REPLACE FOR c_karty2 # c_karty .and. mnozstvi2 # mnozstvi serial_no WITH ALLTRIM(STR(c_karty)) + " " + ALLTRIM(STR(mnozstvi,9,2)) + " SK"
