DELIMITER $$

/* ************************************************************************************************************************************************************************
	Numewracja oparta na `erp_obj_counters`
************************************************************************************************************************************************************************ */	

DROP PROCEDURE IF EXISTS `ht_SKOK_Pros_1`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ht_SKOK_Pros_1`(
	IN _MODE VARCHAR(150),
	IN _Key VARCHAR(38),
	IN _Param_A VARCHAR(255),
	IN _Param_B VARCHAR(255),
	IN _Param_C VARCHAR(255),
	IN _Param_D VARCHAR(255),
	IN _Param_E VARCHAR(255)
)
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		Inkrementowanie licznika
		CALL ht_SKOK_Pros_1('', @IdnOper, '2014-07-01', '2014-07-31', '', '', '');
		
Mechanizm działający w dwóch trybach:

I) Dla wybranego konta (237-01-xxxx (Visa) oraz 237-02-xxxx (MCard)) należy:
    -  dla zadanego zakresu dat (wstępnie miesiąc ale filtr od daty do daty) pogrupować zapisy wg opisu
    - uwzględnić wyłącznie pozycje posiadające znacznik `$` w treści opisu
    - dla zapisów po stronie Wn wygenerować fakturę (odrębny magazyn Visa i MCard) zawierającą tyle pozycji ile unikalnych opisów
    - dla strony wn wygenerować zestawienie (plik Txt ???)

    Kontrahent dla faktury to xxxx (symbol z kartoteki kontrahentów). Kwota z pogrupowanych zapisów to pozycja (netto), VAT 23%, zapłacono NETTO.

II) Mechanizm jak wyżej ale dla określonego kontrahenta (APLIT - symbol 01450).
  
     Konta: 239-03-17,18,27 (Visa) oraz 239-13-17,18 (MCard).
     Bez grupowania opisów - pojedyncze konto to usługa o nazwie równej nazwie konta w planie kont (także strona Wn).
		
		
	aIdnUser := User60.Login;
	  aOpis := RegistryInt.GetRegIntValueString('FK_FakturaOpis', '');
	  aUwagi := RegistryInt.GetRegIntValueString('FK_FakturaUwagi', '');
	  aTrybPlatn := RegistryInt.GetRegIntValueInteger('FK_SposobPlatn', 4); // 4 = "zaplacono"
																					// aTowrSymb :=  RegistryInt.GetRegIntValueString('FK_TowrSymb', '00181'); //'USLUGA';
	  aTowrSymbV :=  RegistryInt.GetRegIntValueString('FK_TowrSymbVISA', '00134');
	  aTowrSymbM :=  RegistryInt.GetRegIntValueString('FK_TowrSymbMasterCard', '00145');
	  aWzDekrSKOK := RegistryInt.GetRegIntValueString('FK_WzDekrSKOK', 'SU');
	  aWzdekrAplit := RegistryInt.GetRegIntValueString('FK_WzDekrAplit', 'SU');

	select guid from TowrNg where symbol =''%s'' - VISA (aTowrSymbV) A i C  i Mastercard (aTowrSymbM) B i D
	
	cWzorzec = 'FK_%s_%s_%s_%s'
	 aOpngGUID := Format(cWzorzec, [aKontrahDS.FieldByName('kontrah').AsString, '01', DateTimeToSQLNonQuoted(aOd), DateTimeToSQLNonQuoted(aDo)]);
	 
	 
	oppo.GUID = concat(''FK_'', md5('aTmp)) gdzie
	aTmp := OpngGUID + konto_fmt + opis;	 
 
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
BEGIN

	DECLARE _Date_Od, _Date_Do, _DataWystaw DATE;
	DECLARE _SQLTextInsert TEXT DEFAULT ''; 
	DECLARE _SQLText TEXT DEFAULT ''; 
	
	IF _MODE LIKE 'Init_t.%' THEN
		SET
			_Date_Od = _Param_A,
			_Date_Do = _Param_B,
			_DataWystaw = _Param_C
		;
		DROP TEMPORARY TABLE IF EXISTS `t_zapisy_SKOK_2_FV`;
		CALL _IReg_Get('', 'FK_FakturaOpis', 0, 'STRING', @session.HT.IR.FK_FakturaOpis, '');
		CALL _IReg_Get('', 'FK_FakturaUwagi', 0, 'STRING', @session.HT.IR.FK_FakturaUwagi, '');
		CALL _IReg_Get('', 'FK_SposobPlatn', 0, 'INTEGER', @session.HT.IR.FK_SposobPlatn, 4);	--	4 = "zaplacono"
		
		CALL _IReg_Get('', 'FK_TowrSymbVISA', 0, 'STRING', @session.HT.IR.FK_TowrSymbVISA, '00134');
		CALL _IReg_Get('', 'FK_TowrSymbMasterCard', 0, 'STRING', @session.HT.IR.FK_TowrSymbMasterCard, '00145');
		

		CALL _IReg_Get('', 'FK_WzDekrSKOK', 0, 'STRING', @session.HT.IR.FK_WzDekrSKOK, 'SU');
		CALL _IReg_Get('', 'FK_WzDekrAplit', 0, 'STRING', @session.HT.IR.FK_WzDekrAplit, 'SU');
		
		CALL _IReg_Get('', 'FK_MagVISA', 0, 'INTEGER', @session.HT.IR.FK_MagVISA, 1);
		CALL _IReg_Get('', 'FK_MagMasterCard', 0, 'INTEGER', @session.HT.IR.FK_MagMasterCard, 1);
		CALL _IReg_Get('', 'FK_AplitMagVISA', 0, 'INTEGER', @session.HT.IR.FK_AplitMagVISA, 1);
		CALL _IReg_Get('', 'FK_AplitMagMasterCard', 0, 'INTEGER', @session.HT.IR.FK_AplitMagMasterCard, 1);
		
		CALL _IReg_Get('', 'FK_RejVISA', 0, 'INTEGER', @session.HT.IR.FK_RejVISA, 2021);
		CALL _IReg_Get('', 'FK_RejMasterCard', 0, 'INTEGER', @session.HT.IR.FK_RejMasterCard, 2021);
		CALL _IReg_Get('', 'FK_AplitRejVISA', 0, 'INTEGER', @session.HT.IR.FK_AplitRejVISA, 2021);
		CALL _IReg_Get('', 'FK_AplitRejMasterCard', 0, 'INTEGER', @session.HT.IR.FK_AplitRejMasterCard, 2021);
		
		
		SET
			_SQLText = 
		"
			CREATE TEMPORARY TABLE `t_zapisy_SKOK_2_FV`
			SELECT 
				'#_MODE#' AS _MODE,
				CONCAT(k.GUID, NULL) AS oppo_GUID,
				CONCAT(k.GUID, NULL) AS oppo_IdnOper,
				SUBSTR(k.symbol_fmt, 5,2) AS _SubWzorzec,
				#ZMagazynu# AS ZMagazynu,
				#rv_id# AS rv_id,
				(SELECT kk.GUID FROM `kontrah` kk WHERE kk.Symbol = #KontrSymbol#) AS KontrGUID,
				#KontrSymbol# AS KontrSymbol, 
				(SELECT t.GUID FROM `TowrNg` t WHERE t.Symbol = '#FK_TowrSymb#' AND deleted=0 limit 1) AS IdnTowr,
				'#FK_TowrSymb#' AS TowrSymb,
				k.symbol_fmt, 
				'#wzordekr#' AS opng_wzordekr, -- @session.HT.IR.FK_WzDekrSKOK AS wzordekr,
				DATE('#_Date_Od#') AS Date_Od,
				DATE('#_Date_Do#') AS Date_Do,
				z.* 
			FROM `zapisy` z, `plankont` k
			WHERE 
				z.konto = k.symbol AND z.rok = k.rok AND stronawn = 1
				AND (z.data_dowodu BETWEEN '#_Date_Od#' AND '#_Date_Do#')
				AND #EXTRA#
		;"
		;
		SET _SQLText = REPLACE(_SQLText, '#_MODE#', _MODE);
		SET _SQLText = REPLACE(_SQLText, '#_Date_Od#', _Date_Od);
		SET _SQLText = REPLACE(_SQLText, '#_Date_Do#', _Date_Do);
		SET _SQLText = REPLACE(_SQLText, '#KontrSymbol#', 
			IF(_MODE IN ('Init_t.A', 'Init_t.B'), 
				"LPAD(SUBSTR(k.symbol_fmt, 8, 5), 5, '0')",
				"'01450'"
			)
		);
		SET _SQLText = REPLACE(_SQLText, '#FK_TowrSymb#', 
			IF(_MODE IN ('Init_t.A', 'Init_t.C'), 
				@session.HT.IR.FK_TowrSymbVISA,
				@session.HT.IR.FK_TowrSymbMasterCard
			)
		);
		SET _SQLText = REPLACE(_SQLText, '#wzordekr#', 
			IF(_MODE IN ('Init_t.A', 'Init_t.B'), 
				@session.HT.IR.FK_WzDekrSKOK,
				@session.HT.IR.FK_WzDekrAplit
			)
		);
		SET _SQLText = REPLACE(_SQLText, '#EXTRA#', 
			CASE _MODE 
			WHEN 'Init_t.A' THEN "k.symbol_fmt LIKE '237-01-%'   AND opis LIKE '%$%'"
			WHEN 'Init_t.B' THEN "k.symbol_fmt LIKE '237-02-%'   AND opis LIKE '%$%'"
			WHEN 'Init_t.C' THEN "( (k.symbol_fmt IN ('239-03-17', '239-03-18', '237-01-911', '237-01-913', '237-01-999') AND IF(k.symbol_fmt LIKE '237%', opis LIKE '%$%', TRUE) ) OR (k.symbol_fmt LIKE '239-03-27%') )"
			WHEN 'Init_t.D' THEN "k.symbol_fmt IN ('239-13-17', '239-13-18', '237-02-911', '237-02-913', '237-02-999') AND IF(k.symbol_fmt LIKE '237%', opis LIKE '%$%', TRUE) "
			END
		);
			
		SET _SQLText = REPLACE(_SQLText, '#ZMagazynu#', 
			CASE _MODE 
			WHEN 'Init_t.A' THEN @session.HT.IR.FK_MagVISA
			WHEN 'Init_t.B' THEN @session.HT.IR.FK_MagMasterCard
			WHEN 'Init_t.C' THEN @session.HT.IR.FK_AplitMagVISA
			WHEN 'Init_t.D' THEN @session.HT.IR.FK_AplitMagMasterCard
			END
		);
		SET _SQLText = REPLACE(_SQLText, '#rv_id#', 
			CASE _MODE 
			WHEN 'Init_t.A' THEN @session.HT.IR.FK_RejVISA
			WHEN 'Init_t.B' THEN @session.HT.IR.FK_RejMasterCard
			WHEN 'Init_t.C' THEN @session.HT.IR.FK_AplitRejVISA
			WHEN 'Init_t.D' THEN @session.HT.IR.FK_AplitRejMasterCard
			END
		);

		
		SET @_SQLText = _SQLText;
		CALL _ExecuteCmd(_SQLText, @Error);
		
		DELETE FROM `t_zapisy_SKOK_2_FV` WHERE KontrGUID IS NULL;
		
		UPDATE `t_zapisy_SKOK_2_FV` t
		SET
			t.oppo_IdnOper = CONCAT(
				'FK_', t.KontrSymbol, '_', _SubWzorzec, '_', _Date_Od, '_', _Date_Do
			)
		;
		UPDATE `t_zapisy_SKOK_2_FV` t
		SET
			t.oppo_GUID = CONCAT(
				'FK_', 
				-- bylo u maxa MD5(CONCAT(t.oppo_IdnOper, t.symbol_fmt, t.Opis))
				MD5(CONCAT(t.oppo_IdnOper, t.Opis))

			)
		;
		
		DROP TEMPORARY TABLE IF EXISTS `t_zapisy_SKOK_2_FV_P`;
		CREATE TEMPORARY TABLE `t_zapisy_SKOK_2_FV_P`
		SELECT 
			t.oppo_GUID AS GUID,  
			t.oppo_IdnOper AS IdnOper, 
			t.IdnTowr,
			1 AS Ilosc,  
			SUM(t.Kwota) AS CenaNet,
			t.Opis AS TowrNazwa
		FROM `t_zapisy_SKOK_2_FV` t
		GROUP BY t.oppo_GUID
		;
		DROP TEMPORARY TABLE IF EXISTS `t_zapisy_SKOK_2_FV_N`;
		CREATE TEMPORARY TABLE `t_zapisy_SKOK_2_FV_N`
		SELECT 
			t.oppo_IdnOper AS GUID,
			t.ZMagazynu,
			t.rv_id,
			t.KontrGUID,
			t.Date_Do, 
			DATE(_DataWystaw) AS DataWystaw,
			t.opng_wzordekr AS WzorDekr,
			@session.HT.IR.FK_FakturaOpis AS Opis,
			@session.HT.IR.FK_FakturaUwagi AS Uwagi,
			CONCAT('Dokument wyst. na podst. zapis. FK z okresu: od ', t.Date_Od, ' do ', t.Date_Do) AS uwagi2,
			@session.HT.IR.FK_SposobPlatn AS SposobPlatn
		FROM `t_zapisy_SKOK_2_FV` t
		GROUP BY t.oppo_IdnOper
		;
		
		
	END IF;	
	
	IF _MODE LIKE 'Init.%' THEN
		SET
			_Date_Od = _Param_A,
			_Date_Do = _Param_B
		;
		DROP TEMPORARY TABLE IF EXISTS `t_zapisy_2_FV_A`;
		CALL _IReg_Get('', 'FK_FakturaOpis', 0, 'STRING', @session.HT.IR.FK_FakturaOpis, '');
		CALL _IReg_Get('', 'FK_FakturaUwagi', 0, 'STRING', @session.HT.IR.FK_FakturaUwagi, '');
		CALL _IReg_Get('', 'FK_SposobPlatn', 0, 'INTEGER', @session.HT.IR.FK_SposobPlatn, 4);	--	4 = "zaplacono"
		CALL _IReg_Get('', 'FK_TowrSymbVISA', 0, 'STRING', @session.HT.IR.FK_TowrSymbVISA, '00134');
		CALL _IReg_Get('', 'FK_TowrSymbMasterCard', 0, 'STRING', @session.HT.IR.FK_TowrSymbMasterCard, '00145');
		

		CALL _IReg_Get('', 'FK_WzDekrSKOK', 0, 'STRING', @session.HT.IR.FK_WzDekrSKOK, 'SU');
		CALL _IReg_Get('', 'FK_WzDekrAplit', 0, 'STRING', @session.HT.IR.FK_WzDekrAplit, 'SU');
		
		
		
		
		IF _MODE = 'Init.A' THEN
			
			
			CREATE TEMPORARY TABLE `t_zapisy_2_FV_A`
			SELECT 
				_MODE AS _MODE,
				CONCAT(k.GUID, NULL) AS oppo_GUID,
				CONCAT(k.GUID, NULL) AS oppo_IdnOper,
				SUBSTR(k.symbol_fmt, 5,2) AS _SubWzorzec,
				(SELECT kk.GUID FROM `kontrah` kk WHERE kk.Symbol = LPAD(SUBSTR(k.symbol_fmt, 8, 5), 5, '0')) AS KontrGUID,
				LPAD(SUBSTR(k.symbol_fmt, 8, 5), 5, '0') AS KontrSymbol, 
				(SELECT t.GUID FROM `TowrNg` t WHERE t.Symbol = @session.HT.IR.FK_TowrSymbVISA) AS IdnTowr,
				@session.HT.IR.FK_TowrSymbVISA AS TowrSymb,
				k.symbol_fmt, 
				@session.HT.IR.FK_WzDekrSKOK AS opng_wzordekr,
				_Date_Od AS Date_Od,
				_Date_Do AS Date_Do,
				z.* 
			FROM `zapisy` z, `plankont` k
			WHERE 
				z.konto = k.symbol AND z.rok = k.rok AND stronawn = 1
				AND (z.data_dowodu BETWEEN _Date_Od AND _Date_Do)
				
				AND k.symbol_fmt LIKE '237-01-%'   AND opis LIKE '%$%' 
			ORDER BY symbol_fmt, z.data_dowodu DESC;
		END IF;


		IF _MODE = 'Init.B' THEN
			CREATE TEMPORARY TABLE `t_zapisy_2_FV_A`
			SELECT 
				_MODE AS _MODE,
				CONCAT(k.GUID, NULL) AS oppo_GUID,
				CONCAT(k.GUID, NULL) AS oppo_IdnOper,
				SUBSTR(k.symbol_fmt, 5,2) AS _SubWzorzec,
				(SELECT kk.GUID FROM `kontrah` kk WHERE kk.Symbol = LPAD(SUBSTR(k.symbol_fmt, 8, 5), 5, '0')) AS KontrGUID,
				LPAD(SUBSTR(k.symbol_fmt, 8, 5), 5, '0') AS KontrSymbol, 
				(SELECT t.GUID FROM `TowrNg` t WHERE t.Symbol = @session.HT.IR.FK_TowrSymbMasterCard) AS IdnTowr,
				@session.HT.IR.FK_TowrSymbMasterCard AS TowrSymb,
				k.symbol_fmt, 
				@session.HT.IR.FK_WzDekrSKOK AS opng_wzordekr,
				_Date_Od AS Date_Od,
				_Date_Do AS Date_Do,
				z.* 
			FROM `zapisy` z, `plankont` k
			WHERE 
				z.konto = k.symbol AND z.rok=k.rok AND stronawn=1
				AND (z.data_dowodu BETWEEN _Date_Od AND _Date_Do)
				AND k.symbol_fmt LIKE '237-02-%'   AND opis LIKE '%$%'
			ORDER BY z.data_dowodu DESC;
		END IF;

		IF _MODE = 'Init.C' THEN
			CREATE TEMPORARY TABLE `t_zapisy_2_FV_A`
			SELECT 
				_MODE AS _MODE,
				CONCAT(k.GUID, NULL) AS oppo_GUID,
				CONCAT(k.GUID, NULL) AS oppo_IdnOper,
				SUBSTR(k.symbol_fmt, 5,2) AS _SubWzorzec,
				(SELECT kk.GUID FROM `kontrah` kk WHERE kk.Symbol = '01450') AS KontrGUID,
				'01450' AS KontrSymbol, 
				(SELECT t.GUID FROM `TowrNg` t WHERE t.Symbol = @session.HT.IR.FK_TowrSymbVISA) AS IdnTowr,
				@session.HT.IR.FK_TowrSymbVISA AS TowrSymb,
				k.symbol_fmt, 
				@session.HT.IR.FK_WzDekrAplit AS opng_wzordekr,
				_Date_Od AS Date_Od,
				_Date_Do AS Date_Do,
				z.* 
			FROM `zapisy` z, `plankont` k
			WHERE 
				z.konto = k.symbol AND z.rok=k.rok AND stronawn=1
				AND (z.data_dowodu BETWEEN _Date_Od AND _Date_Do)
				
				AND k.symbol_fmt IN ('239-03-17', '239-03-18', '239-03-27') 
			ORDER BY z.localtime DESC, symbol_fmt;
		END IF;


		IF _MODE = 'Init.D' THEN
			CREATE TEMPORARY TABLE `t_zapisy_2_FV_A`
			SELECT 
				_MODE AS _MODE,
				CONCAT(k.GUID, NULL) AS oppo_GUID,
				CONCAT(k.GUID, NULL) AS oppo_IdnOper,
				SUBSTR(k.symbol_fmt, 5,2) AS _SubWzorzec,
				(SELECT kk.GUID FROM `kontrah` kk WHERE kk.Symbol = '01450') AS KontrGUID,
				'01450' AS KontrSymbol, 
				(SELECT t.GUID FROM `TowrNg` t WHERE t.Symbol = @session.HT.IR.FK_TowrSymbMasterCard) AS IdnTowr,
				@session.HT.IR.FK_TowrSymbMasterCard AS TowrSymb,
				k.symbol_fmt, 
				@session.HT.IR.FK_WzDekrAplit AS opng_wzordekr,
				_Date_Od AS Date_Od,
				_Date_Do AS Date_Do,
				z.* 
			FROM `zapisy` z, `plankont` k
			WHERE 
				z.konto = k.symbol AND z.rok=k.rok AND stronawn=1
				AND (z.data_dowodu BETWEEN _Date_Od AND _Date_Do)
				AND k.symbol_fmt IN ('239-13-17', '239-13-18')  
			ORDER BY z.localtime DESC, symbol_fmt;
		END IF;
		
		DELETE FROM `t_zapisy_2_FV_A` WHERE KontrGUID IS NULL;
		
		UPDATE `t_zapisy_2_FV_A` t
		SET
			t.oppo_IdnOper = CONCAT(
				'FK_', t.KontrSymbol, '_', _SubWzorzec, '_', _Date_Od, '_', _Date_Do
			)
		;
		UPDATE `t_zapisy_2_FV_A` t
		SET
			t.oppo_GUID = CONCAT(
				'FK_', 
				-- bylo u maxa MD5(CONCAT(t.oppo_IdnOper, t.symbol_fmt, t.Opis))
				MD5(CONCAT(t.oppo_IdnOper, t.Opis))

			)
		;
		
		DROP TEMPORARY TABLE IF EXISTS `t_zapisy_2_FV_P`;
		CREATE TEMPORARY TABLE `t_zapisy_2_FV_P`
		SELECT 
			t.oppo_GUID AS GUID,  
			t.oppo_IdnOper AS IdnOper, 
			t.IdnTowr,
			1 AS oppo_Ilosc,  
			SUM(t.Kwota) AS CenaNet
		FROM `t_zapisy_2_FV_A` t
		GROUP BY t.oppo_GUID
		;
		DROP TEMPORARY TABLE IF EXISTS `t_zapisy_2_FV_N`;
		CREATE TEMPORARY TABLE `t_zapisy_2_FV_N`
		SELECT 
			t.oppo_IdnOper AS GUID,  
			t.KontrGUID,
			t.Date_Do, 
			DATE(_DataWystaw) AS DataWystaw,
			t.WzorDekr,
			@session.HT.IR.FK_FakturaOpis AS Opis,
			@session.HT.IR.FK_FakturaUwagi AS Uwagi,
			CONCAT('Dokument wyst. na podst. zapis. FK z okresu: od ', t.Date_Od, ' do ', t.Date_Do) AS uwagi2,
 		    @session.HT.IR.FK_SposobPlatn AS SposobPlatn
		FROM `t_zapisy_2_FV_A` t
		GROUP BY t.oppo_IdnOper
		;

	END IF;
		  
	IF _MODE = 'Create.One' THEN
		CALL _Session_Refresh_Variables('', '', '');
		
		DELETE FROM `oppo` WHERE IdnOper = _Key;
		DELETE FROM `opng_vat` WHERE IdnOper = _Key;
		DELETE FROM `opng` WHERE GUID = _Key;

		DELETE FROM `oppo_e` WHERE IdnOper = _Key;
		DELETE FROM `opng_vat_e` WHERE IdnOper = _Key;
		DELETE FROM `opng_e` WHERE GUID = _Key;
		
		SELECT 
			t.KontrGUID, t.ZMagazynu AS ZMagazynu, t.rv_id, 
			t.DataWystaw, t.Date_Do, 
			0 AS i_Tymczasowa, 0 AS i_Waluta, '' AS i_OsobaGUID, '' AS i_AreaGUID
		FROM `t_zapisy_SKOK_2_FV_N` t 
		WHERE
			t.GUID = _Key
		INTO 
			@.KontrGUID, @.ZMagazynu, @.rv_id, 
			@.DataWystaw, @.Date_Do, 
			@.i_Tymczasowa, @.i_Waluta, @.i_OsobaGUID, @.i_AreaGUID
		;	
		CALL Opng_E_init('INSERT', _Key, '', '', '');
		-- CALL Opng_E_init('INSERT', _Key, @session.Registration.UserGUID, '', '');
		CALL Opng_E_init_set('', _Key, 1, 0, '', 3, @.KontrGUID, @.ZMagazynu, 0, @.i_Tymczasowa, @.i_Waluta, @.i_OsobaGUID, @.i_AreaGUID, '', 0, '', '', '');
		CALL Opng_E_Run('all', _Key, '', '', '');

		UPDATE `opng_e` n, `t_zapisy_SKOK_2_FV_N` t  
		SET 
			n.DataWystaw = t.DataWystaw, 
			n.DataWplywu = t.DataWystaw, 
			n.DataSprzed = t.Date_Do, 
			n.Data_Vat = t.Date_Do,
			n.sposobplatn = t.sposobplatn,
			n.rv_id = t.rv_id,
			
			n.WzorDekr = t.WzorDekr,
			n.Opis = t.Opis,
			n.Uwagi = t.Uwagi,
			n.uwagi2 = t.uwagi2,
			n.usluga_ciagla = 1, 
			n.rozrachunek = 1, 
			n.CWN = 1			
		WHERE 
			n.GUID = _Key
			AND
			t.GUID = _Key
		;
		
		INSERT INTO `oppo_e` 
		(GUID, IdnOper, IdnTowr, Ilosc, CenaNet)
		SELECT 
			GUID, IdnOper, IdnTowr, Ilosc, CenaNet
		FROM `t_zapisy_SKOK_2_FV_P`
		WHERE
			IdnOper = _Key
		;
		UPDATE `oppo_e` p, `t_zapisy_SKOK_2_FV_P` t
		SET 
			p.Tag = -1,
			p.TowrNazwa = t.TowrNazwa
		WHERE 
			p.IdnOper = _Key
			AND t.GUID = p.GUID
		;
		set @.x.y.Z = '1';
		CALL opng_E_Let('Post', _Key, '', '');
		
		CALL ng_vat_e_build_from_po('', 'OP', _Key, '', '');
		CALL ng_vat_e_TO_ng_e('', 'OP', _Key);
		
		set @.x.y.Z = '2';
		
		
		CALL opng_E_Let('ApplyAndClear', _Key, '', '1');
		set @.x.y.Z = '3';
		CALL opng_E_Let('Clear', _Key, '', '');
		set @.x.y.Z = '3';
		UPDATE `opng` SET Calc_Ver = 1 WHERE GUID = _Key;
		CALL opng_E_Let('Clear', _Key, '', '');
		CALL opng_E_Let('Clear', _Key, '', '');
	END IF;
	
	IF _MODE = 'Create' THEN
	
		BEGIN
			DECLARE _GUID VARCHAR(38);
			DECLARE _error BOOL DEFAULT FALSE;
			DECLARE done BOOL DEFAULT FALSE;
			DECLARE cOpngDocs CURSOR FOR
				SELECT 	
					t.GUID
				FROM `t_zapisy_SKOK_2_FV_N` t
			;
			DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
			OPEN cOpngDocs;
			Loop_OpngDocs: LOOP
				FETCH cOpngDocs INTO 
					_GUID
				;
				IF done THEN
					LEAVE Loop_OpngDocs;
				END IF;
			--	Tworz dok.
				CALL ht_SKOK_Pros_1('Create.One', _GUID, '', '', '', '', '');
			END LOOP;
			CLOSE cOpngDocs;
			
		END;
	
		
	
	END IF;

	IF _MODE = 'Create.ALL' THEN
		CALL ht_SKOK_Pros_1('Init_t.A', '', _Param_A, _Param_B, _Param_C, '', '');
		CALL ht_SKOK_Pros_1('Create', '', '', '', '', '', '');
		CALL ht_SKOK_Pros_1('Init_t.B', '', _Param_A, _Param_B, _Param_C, '', '');
		CALL ht_SKOK_Pros_1('Create', '', '', '', '', '', '');
		CALL ht_SKOK_Pros_1('Init_t.C', '', _Param_A, _Param_B, _Param_C, '', '');
		CALL ht_SKOK_Pros_1('Create', '', '', '', '', '', '');
		CALL ht_SKOK_Pros_1('Init_t.D', '', _Param_A, _Param_B, _Param_C, '', '');
		CALL ht_SKOK_Pros_1('Create', '', '', '', '', '', '');
	END IF;
	
END
$$


DELIMITER ;

/*
====================================================================================================================================================================================
	NOTATNIK
====================================================================================================================================================================================

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
