*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
* program generuje konzumace stravniku do CSV
* ceny jidel se berou z karty.dbf
*
*
* 2023-04-30 - pred exportem se smazou soubory z minula
* 2024-04-05 - jen jedno jidlo denne muze byt dotovano
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2024-04-05 <#       *    *    *    *    *    *    *    *    *    *    *    *    *


WAIT PROGRAM() WINDOW AT 22,22 NOWAIT 

DO clos_data
ACTIVATE SCREEN 
CLEAR 

STORE "" TO datsk1, datsk2, mesicek, s_trz_dat1, s_trz_dat2
nastav_obdobi()

zacatek_nazvu_souboru = m.gcexportpath + "faurecia_nyrany_"

DO ppgm_http 
SET PROCEDURE TO ppgm_stravnici ADDITIVE 

DO get_courses      IN stravovani_ppgm
DO get_clients      IN stravovani_ppgm

DO get_meals        IN stravovani_ppgm
DO get_orders       IN stravovani_ppgm

USE (m.gccen_path + "karty") order c_karty NOUPDATE IN 0

SELECT client_id, LEFT(clientname, 44) as clientname, firma, date as cas_odberu, course, name, ordered as pocet, eaten, c_karty, ;
       0000.00 as dotovana, 0000.00 as plna, 0000.00 as dotace ;
       ORDER BY clientname, date, course, number ;
       FROM orders INTO TABLE (m.zacatek_nazvu_souboru)

SET RELATION TO c_karty INTO karty

INDEX ON client_id + cas_odberu TAG cc UNIQUE 
REPLACE ALL dotace WITH karty.cena03
SET ORDER TO 
REPLACE ALL plna WITH  karty.cena01, dotovana WITH plna - dotace

REPLACE ALL firma WITH alpha_digit_only(firma)

ERASE                                       (m.zacatek_nazvu_souboru + "kontro*.*")
ERASE                                       (m.zacatek_nazvu_souboru + "strava*.*")
SELECT disti firma FROM (m.zacatek_nazvu_souboru) INTO CURSOR firmy 
SCAN
    SCATTER MEMVAR 
    ? m.zacatek_nazvu_souboru + ALLTRIM(m.firma)
    SELECT * FROM (m.zacatek_nazvu_souboru) ;
           WHERE firma = m.firma INTO TABLE (m.zacatek_nazvu_souboru + "kontrola_" + ALLTRIM(m.firma))
    SET POINT TO "."
    dbf2csv()

    SELECT client_id, clientname, SUM(dotace) as dotace, SUM(pocet) as pocet ;
           GROUP BY 1 FROM (m.zacatek_nazvu_souboru + "kontrola_" + ALLTRIM(m.firma)) INTO TABLE (m.zacatek_nazvu_souboru + "strava_" + ALLTRIM(m.firma))
    dbf2csv()

    USE 
ENDSCAN 

DO clos_data
WAIT CLEAR 



*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
PROCEDURE nastav_obdobi
IF DAY(DATE()) < 20
    s_trz_dat2 = DATE(YEAR(DATE()   ), MONTH(DATE()   ), 1) - 1
ELSE
    s_trz_dat2 = DATE(YEAR(DATE()+27), MONTH(DATE()+27), 1) - 1
ENDIF
s_trz_dat1     = DATE(YEAR(m.s_trz_dat2), MONTH(m.s_trz_dat2), 1)

datsk1 = LEFT(TTOC(m.s_trz_dat1, 3), 10)
datsk2 = LEFT(TTOC(m.s_trz_dat2, 3), 10)

mesicek = SUBSTR(datsk1, 3, 2) + SUBSTR(datsk1, 6, 2)                     && "2108", cili ctyrmistne RRMM

ACTIVATE SCREEN 
?    "exportuje se obdobi: " + m.datsk1 + " - " + m.datsk2 + "  ( " + m.mesicek + " )"
