FUNCTION nacitajinventurustrediska
PARAMETERS rodic
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_inventuru')
LOCAL m.ret, m.at1, m.at2, m.c_karty, m.mnozstvi
m.ret=.t.
lsnakex.DRAW()
SELECT buffer
DO CASE 
	CASE m.food_daemon_typ="TCPIP"
	CASE m.food_daemon_typ="CSV"
		LOCAL cesta, pokracuj, m.cesta_zal
		m.cesta = get_ini(m.machine, "InventuraExportPath")
		m.cesta_zal=m.cesta
		IF ! EMPTY(m.cesta)
		    backslash( @m.cesta )
		    DO mk_dir WITH m.cesta
		    IF STRTOFILE(m.cesta, m.cesta + "testrautexportpath.txt") = 0
		        m.cesta = ""
		    ELSE
		        DELETE FILE m.cesta + "testrautexportpath.txt"
		    ENDIF 
		ENDIF
		IF EMPTY(m.cesta)
			m.cesta = GETDIR("..\", jaz("form_adr_na_import_inventury"), "")
		ELSE 
			m.cesta = GETDIR(m.cesta, jaz("form_adr_na_import_inventury"), "")
		ENDIF 
		IF ! EMPTY(m.cesta)
			IF FILE(m.cesta+"inv"+m.gcpgm_id+".csv")
	            fh = FOPEN(m.cesta+"inv"+m.gcpgm_id+".csv")
		        IF m.fh > 0 
        			oddelovac = ";"
					SELECT * FROM buffer INTO CURSOR buffricek READWRITE 
					INDEX on c_karty TAG c_karty
					REPLACE ALL mnozstvi WITH 0
					DO WHILE !FEOF(m.fh)
						m.radek    = FGETS(m.fh,333)
						m.at1=AT(m.oddelovac,m.radek,1)
						m.at2=AT(m.oddelovac,m.radek,2)
						m.c_karty  = VAL(SUBSTR(m.radek, m.at1+1,m.at2-m.at1-1))
						m.at1=AT(m.oddelovac,m.radek,4)
						m.at2=AT(m.oddelovac,m.radek,5)
						m.mnozstvi  = VAL(SUBSTR(m.radek, m.at1+1,m.at2-m.at1-1))
						IF .not. EMPTY(m.c_karty)
							SELECT buffricek
							WAIT m.radek WINDOW NOWAIT AT 22,22
							IF SEEK(m.c_karty)
								REPLACE mnozstvi WITH buffricek.mnozstvi + m.mnozstvi
							ELSE
								INSERT INTO buffricek (c_karty, mnozstvi) VALUES (m.c_karty, m.mnozstvi)
							ENDIF 
						ENDIF 
					ENDDO 
					FCLOSE(m.fh)
					ERASE (m.cesta+"inv"+m.gcpgm_id+".csv")
					UPDATE buffer SET mnozstvi=buffricek.mnozstvi from buffricek WHERE buffer.c_karty=buffricek.c_karty
					INSERT INTO buffer (c_karty,   mnozstvi,       menobuf,          c_stredisk, operace) ;
						SELECT c_karty, mnozstvi, rodic.meno, rodic.c_stredisk,   "PI" FROM buffricek WHERE !(c_karty in (select c_karty FROM buffer))
					DO pozavirej WITH "buffricek"
					WAIT CLEAR 
					SELECT buffer
					DELETE FOR c_karty = 0
					UPDATE buffer SET nazev=menaz.nazev, druh=menaz.druh from menaz WHERE buffer.c_karty=menaz.c_karty AND EMPTY(buffer.nazev)
					REPLACE FOR EMPTY(nazev) nazev with "-- karta # " + ALLTRIM(STR(c_karty)) + " --"
					REPLACE ALL cena_celk WITH (mnozstvi - na_sklade) * cena
					SUM ALL cena_celk TO rodic.hodnota
					rodic.txthodnota.caption=ALLTRIM(STR(rodic.hodnota,15,2))
					GO TOP 
					IF LOWER(m.cesta)<>LOWER(m.cesta_zal)
						= put_ini(m.machine, "InventuraExportPath", m.cesta)
					ENDIF 
				ELSE
					=MESSAGEBOX(jazparam("form_inv_subor_neda_otvorit",UPPER(m.cesta)+"INV"+m.gcpgm_id),64,"FoodMan")
				ENDIF 
			ELSE
				=MESSAGEBOX(jazparam("form_inv_subor_neexistuje",UPPER(m.cesta),"INV"+m.gcpgm_id),64,"FoodMan")
			ENDIF 
		ELSE
			=MESSAGEBOX(jaz("form_inv_nevybrana_cesta"),64,"FoodMan")
		ENDIF 
ENDCASE 
RETURN m.ret 
