<?php

error_reporting(E_ALL & ~E_NOTICE);

define('DS', DIRECTORY_SEPARATOR);
define('APP_PATH_ROOT', realpath(dirname(__FILE__) . DS . '..'));
define('APP_PATH_WWW', realpath(dirname(__FILE__) . DS . '..'));
define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');

//session_save_path("../session") ;
session_start();

date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions

header('Content-Type: text/html; charset=utf-8');

// SEF - SE file loader
function SEF($ARG1) {
	if (!function_exists($ARG1)) {
		if (file_exists(APP_PATH_ROOT . DS . "superedit-".$ARG1.".php")) include(APP_PATH_ROOT . DS . "superedit-".$ARG1.".php");
		else if (file_exists(APP_PATH_ROOT . DS . "billing-".$ARG1.".php")) include(APP_PATH_ROOT . DS . "billing-".$ARG1.".php");
		else if (file_exists(APP_PATH_ROOT . DS . $ARG1.".php")) include(APP_PATH_ROOT . DS . $ARG1.".php");
		else echo ("\n<br>Function SEF : can't find function ".$ARG1);
	}
}
SEF("DEBUG_S");

if (file_exists(APP_PATH_ROOT . DS . ".config.php")) include(APP_PATH_ROOT . DS . ".config.php");

require_once APP_PATH_ROOT . '/' . 'se-lib' . '/' . 'Lib.php';
Lib::loadClass('V');
Lib::loadClass('User');
Lib::loadClass('Config');
Lib::loadClass('App');
Lib::loadClass('DB');
Lib::loadClass('S');


if (!User::logged()) {
	die("Require login!");
}

//if (User::get('ADM_ADMIN_LEVEL') != 0) {
//	die("Access denied!");
//}

$umowa_zasob_id = 1581;
$umowa_template_file = dirname(__FILE__) . '/' . '1581.druk_promocji-template.xsl';

// TODO: path from config, like $FOLDERS['CRM_LISTA_ZASOBOW_COLUMN']="/home/samba/DRUKI/";
$druki_path = '/home/samba/DRUKI';
if ($_SERVER['SERVER_NAME'] == 'se.dev.webone.pl.devpl') {
	$druki_path = realpath(APP_PATH_ROOT . '/../../samba/DRUKI/');
}

if (V::get('test', 0, $_GET, 'int') == 1) {
	$druki = glob($druki_path . "/" . $umowa_zasob_id . ".*/STARE/test.odt", GLOB_NOSORT);
} else {
	$druki = glob($druki_path . "/" . $umowa_zasob_id . ".*/*.odt", GLOB_NOSORT);
}
//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($druki);echo'</pre>';

if (empty($druki)) {
	die("Error: cant find file");
}
if (count($druki) == 1) {
	$umowa_path = reset($druki);
} else {
	// TODO: sort by date and get first?
	$umowa_path = end($druki);
}

if (!$umowa_path) {
	die("Error: cant find file");
}


Lib::loadClass('Odt2xhtml');

if (!class_exists('DOMDocument')) {
	die('require class DOMDocument');
}
if (!class_exists('XSLTProcessor')) {
	die('require class XSLTProcessor');
}
if (!function_exists('zip_open')) {
	die('NO ZIP FUNCTIONS DETECTED. Do you have the PECL ZIP extensions loaded?');
}

if (!file_exists($umowa_template_file)) {
	die("Template file not exists!");
}

$conv = new Odt2xhtml;
$umowa_druk = $conv->oo_convert($conv->oo_unzip($umowa_path), $umowa_template_file);
if (empty($umowa_druk)) {
	die("Conversion odt to html error!");
}

$allowed_options = array('NET', 'TEL', 'TV');//, 'TVC', 'TVCP');

$args = array();
$args['id_group'] = V::get('id_group', 0, $_GET, 'int');
$args['id_options'] = array();
foreach ($allowed_options as $v_option) {
	$args['id_options'][$v_option] = V::get('id_options_' . $v_option . '', 0, $_GET, 'array');
}

if ($args['id_group'] > 0) {
	$sql = "
SELECT
 date_format(now(),'%Y-%m-%d') as `779` ,
 t0.`ID` as `0_1380` ,
 t0.`S_ALIAS` as `0_1534` ,
 t0.`P_SERVICE_COMPANY` as `0_COMPANY`,
 t0.`P_PROMOTION_NAME` as `0_451` ,
 t0.`P_DEALTERM_MONTHS` as `0_456` ,
 t0.`P_OFFER_TERM_BEGIN` as `0_1579`, t0.P_OFFER_TERM as `0_464` ,
 t0.`M_REWIR` as `0_447`  ,
 (select group_concat(S_ADDRESS_STREET, ' ') from BUILDINGS where M_REWIR like t0.M_REWIR limit 1) as `x_410`,
 t0.`ID_NET`  as `0_1743_ID_NET` ,
 t0.`ID_TV`   as `0_1744_ID_TV` ,
 t0.`ID_TVC`  as `0_1745_ID_TVC` ,
 -- t0.`ID_TVCP` as `0_1746_ID_TVCP` ,
 CAST(concat_ws(',', IF(t0.`ID_TVCP_1`>0,t0.`ID_TVCP_1`,NULL), IF(t0.`ID_TVCP_2`>0,t0.`ID_TVCP_2`,NULL), IF(t0.`ID_TVCP_3`>0,t0.`ID_TVCP_3`,NULL)) as char) as `0_1746_ID_TVCP` ,
 t0.`ID_TVCP_1` as `0_2621_ID_TVCP_1` ,
 t0.`ID_TVCP_2` as `0_2622_ID_TVCP_2` ,
 t0.`ID_TVCP_3` as `0_2623_ID_TVCP_3` ,
 t0.`ID_TVCP_4` as `0_2624_ID_TVCP_4` ,
 t0.`ID_TEL`  as `0_1747_ID_TEL` ,
 t0.`M_REWIR` as `0_1627` ,

-- internet: 
 t1.ID as `1_1385` ,
 t1.P_SERVICE as `1_449` ,
 t1.P_SERVICE_NAME as `1_450` ,
 t1.P_INSTALL_QUOTE as `1_458` , t1.P_INSTALL_QUOTE_NORMAL as `t1_1578`,
 t1.P_CONTRACT_PRICE as `1_459` ,
 t1.P_PROMO_MONTH_PRICE as `1_460` ,
 t1.P_NORMAL_PRICE as `1_461` ,
 t1.P_DEAL_PROMO_MONTHS as `1_457` ,
-- (t1.P_NORMAL_PRICE-t1.P_PROMO_MONTH_PRICE)*t0.P_DEALTERM_MONTHS+(t1.P_INSTALL_QUOTE_NORMAL-t1.P_INSTALL_QUOTE) as `t1_1580`,
-- (t1.P_NORMAL_PRICE-t1.P_CONTRACT_PRICE)*t0.P_DEALTERM_MONTHS+(t1.P_INSTALL_QUOTE_NORMAL-t1.P_INSTALL_QUOTE) as `t1_1580`,
 t1.P_NORMAL_PRICE*t0.P_DEALTERM_MONTHS - t1.P_DEAL_PROMO_MONTHS*t1.P_PROMO_MONTH_PRICE - (t0.P_DEALTERM_MONTHS-t1.P_DEAL_PROMO_MONTHS)*t1.P_CONTRACT_PRICE + (t1.P_INSTALL_QUOTE_NORMAL-t1.P_INSTALL_QUOTE) as `t1_1580`,
 t1.P_S_PARAM1 as `[1_452]` , t1.P_S_PARAM2 as `[1_453]` , t1.P_S_LIMIT as `[1_454]`, t1.P_S_MAILBOX_SIZE as `[1_462]`, t1.P_S_WWW_SIZE as `[1_463]` , 
 if(t1.ID,t0.P_PROMOTION_NAME,'') as `1_451` , 
	if(t1.ID>0,'X','') as `1_499_JEST` ,

-- telewizja analogowa
 t2.ID as `2_1385` ,
 t2.P_SERVICE as `2_449` ,
 t2.P_SERVICE_NAME as `2_450` ,
 t2.P_INSTALL_QUOTE as `2_458` , t2.P_INSTALL_QUOTE_NORMAL as `t2_1578`,
 t2.P_CONTRACT_PRICE as `2_459` ,
 t2.P_PROMO_MONTH_PRICE as `2_460` ,
 t2.P_NORMAL_PRICE as `2_461` ,
 t2.P_DEAL_PROMO_MONTHS as `2_457` ,
-- (t2.P_NORMAL_PRICE-t2.P_PROMO_MONTH_PRICE)*t0.P_DEALTERM_MONTHS+(t2.P_INSTALL_QUOTE_NORMAL-t2.P_INSTALL_QUOTE) as `t2_1580`,
-- (t2.P_NORMAL_PRICE-t2.P_CONTRACT_PRICE)*t0.P_DEALTERM_MONTHS+(t2.P_INSTALL_QUOTE_NORMAL-t2.P_INSTALL_QUOTE) as `t2_1580`,
 t2.P_NORMAL_PRICE*t0.P_DEALTERM_MONTHS - t2.P_DEAL_PROMO_MONTHS*t2.P_PROMO_MONTH_PRICE - (t0.P_DEALTERM_MONTHS-t2.P_DEAL_PROMO_MONTHS)*t2.P_CONTRACT_PRICE + (t2.P_INSTALL_QUOTE_NORMAL-t2.P_INSTALL_QUOTE) as `t2_1580`,
 if(t0.ID='1','',if(t2.ID,'TAK','NIE')) as `2_IF` ,
 if(t2.ID,t0.P_PROMOTION_NAME,'') as `2_451` ,  
	if(t2.ID>0,'X','') as `2_499_JEST` ,
-- telewizja cyfrowa
 t3.ID as `3_1385` ,
 t3.P_SERVICE as `3_449` ,
 t3.P_SERVICE_NAME as `3_450` ,
 t3.P_INSTALL_QUOTE as `3_458` , t3.P_INSTALL_QUOTE_NORMAL as `3_1578`,
 t3.P_CONTRACT_PRICE as `3_459` ,
 t3.P_PROMO_MONTH_PRICE as `3_460` ,
 t3.P_NORMAL_PRICE as `3_461` ,
 t3.P_DEAL_PROMO_MONTHS as `3_457` ,
-- (t3.P_NORMAL_PRICE-t3.P_PROMO_MONTH_PRICE)*t0.P_DEALTERM_MONTHS+(t3.P_INSTALL_QUOTE_NORMAL-t3.P_INSTALL_QUOTE) as `t3_1580`,
-- (t3.P_NORMAL_PRICE-t3.P_CONTRACT_PRICE)*t0.P_DEALTERM_MONTHS+(t3.P_INSTALL_QUOTE_NORMAL-t3.P_INSTALL_QUOTE) as `t3_1580`,
 t3.P_NORMAL_PRICE*t0.P_DEALTERM_MONTHS - t3.P_DEAL_PROMO_MONTHS*t3.P_PROMO_MONTH_PRICE - (t0.P_DEALTERM_MONTHS-t3.P_DEAL_PROMO_MONTHS)*t3.P_CONTRACT_PRICE + (t3.P_INSTALL_QUOTE_NORMAL-t3.P_INSTALL_QUOTE) as `t3_1580`,
 if(t0.ID='1','',if(t3.ID,'TAK','NIE')) as `3_IF` ,
 if(t3.ID,t0.P_PROMOTION_NAME,'') as `3_451` ,  
-- 2169 S_DEKODER_SD_OPTION
-- 2170 S_MODUL_PCMCIA_OPTION
-- 2171 S_DEKODER_HD_OPTION
	if(t2.S_DEKODER_SD_OPTION='TAK','X','') as `1_2169_TAK` ,
	if(t2.S_DEKODER_SD_OPTION='NIE','X','') as `1_2169_NIE` ,
	if(t2.S_MODUL_PCMCIA_OPTION='TAK','X','') as `1_2170_TAK` ,
	if(t2.S_MODUL_PCMCIA_OPTION='NIE','X','') as `1_2170_NIE` ,
	if(t2.S_DEKODER_HD_OPTION='TAK','X','') as `1_2171_TAK` ,
	if(t2.S_DEKODER_HD_OPTION='NIE','X','') as `1_2171_NIE` ,

-- telewizja cyfrowa pakiety 
 t4.ID as `4_1385` ,
 t4.P_SERVICE as `4_449` ,
 t4.P_SERVICE_NAME as `4_450` ,
 t4.P_INSTALL_QUOTE as `4_458` , t4.P_INSTALL_QUOTE_NORMAL as `t4_1578`,
 t4.P_CONTRACT_PRICE as `4_459` ,
 t4.P_PROMO_MONTH_PRICE as `4_460` ,
 t4.P_NORMAL_PRICE as `4_461` ,
 t4.P_DEAL_PROMO_MONTHS as `4_457` ,
-- (t4.P_NORMAL_PRICE-t4.P_PROMO_MONTH_PRICE)*t0.P_DEALTERM_MONTHS+(t4.P_INSTALL_QUOTE_NORMAL-t4.P_INSTALL_QUOTE) as `t4_1580`,
-- (t4.P_NORMAL_PRICE-t4.P_CONTRACT_PRICE)*t0.P_DEALTERM_MONTHS+(t4.P_INSTALL_QUOTE_NORMAL-t4.P_INSTALL_QUOTE) as `t4_1580`,
 t4.P_NORMAL_PRICE*t0.P_DEALTERM_MONTHS - t4.P_DEAL_PROMO_MONTHS*t4.P_PROMO_MONTH_PRICE - (t0.P_DEALTERM_MONTHS-t4.P_DEAL_PROMO_MONTHS)*t4.P_CONTRACT_PRICE + (t4.P_INSTALL_QUOTE_NORMAL-t4.P_INSTALL_QUOTE) as `t4_1580`,
 if(t0.ID='1','',if(t4.ID,'TAK','NIE')) as `4_IF` ,
 if(t4.ID,t0.P_PROMOTION_NAME,'') as `4_451` ,  

-- telefon
 t5.ID as `5_1385` ,
 t5.P_SERVICE as `5_449` ,
 t5.P_SERVICE_NAME as `5_450` ,
 t5.P_INSTALL_QUOTE as `5_458` , t5.P_INSTALL_QUOTE_NORMAL as `t5_1578`,
 t5.P_CONTRACT_PRICE as `5_459` ,
 t5.P_PROMO_MONTH_PRICE as `5_460` ,
 t5.P_NORMAL_PRICE as `5_461` ,
 t5.P_DEAL_PROMO_MONTHS as `5_457` ,
-- (t5.P_NORMAL_PRICE-t5.P_PROMO_MONTH_PRICE)*t0.P_DEALTERM_MONTHS+(t5.P_INSTALL_QUOTE_NORMAL-t5.P_INSTALL_QUOTE) as `t5_1580`,
-- (t5.P_NORMAL_PRICE-t5.P_CONTRACT_PRICE)*t0.P_DEALTERM_MONTHS+(t5.P_INSTALL_QUOTE_NORMAL-t5.P_INSTALL_QUOTE) as `t5_1580`,
 t5.P_NORMAL_PRICE*t0.P_DEALTERM_MONTHS - t5.P_DEAL_PROMO_MONTHS*t5.P_PROMO_MONTH_PRICE - (t0.P_DEALTERM_MONTHS-t5.P_DEAL_PROMO_MONTHS)*t5.P_CONTRACT_PRICE + (t5.P_INSTALL_QUOTE_NORMAL-t5.P_INSTALL_QUOTE) as `t5_1580`,
 if(t0.ID='1','',if(t5.ID,'TAK','NIE')) as `5_IF` ,
 if(t5.ID,t0.P_PROMOTION_NAME,'') as `5_451`,
 if(t5.ID>0,'X','') as `5_499_JEST` ,


-- telewizja cyfrowa pakiety 2
 t6.ID as `6_1385` ,
 t6.P_SERVICE as `6_449` ,
 t6.P_SERVICE_NAME as `6_450` ,
 t6.P_INSTALL_QUOTE as `6_458` , t6.P_INSTALL_QUOTE_NORMAL as `t6_1578`,
 t6.P_CONTRACT_PRICE as `6_459` ,
 t6.P_PROMO_MONTH_PRICE as `6_460` ,
 t6.P_NORMAL_PRICE as `6_461` ,
 t6.P_DEAL_PROMO_MONTHS as `6_457` ,
 t6.P_NORMAL_PRICE*t0.P_DEALTERM_MONTHS - t6.P_DEAL_PROMO_MONTHS*t6.P_PROMO_MONTH_PRICE - (t0.P_DEALTERM_MONTHS-t6.P_DEAL_PROMO_MONTHS)*t6.P_CONTRACT_PRICE + (t6.P_INSTALL_QUOTE_NORMAL-t6.P_INSTALL_QUOTE) as `t6_1580`,
 if(t0.ID='1','',if(t6.ID,'TAK','NIE')) as `6_IF` ,
 if(t6.ID,t0.P_PROMOTION_NAME,'') as `6_451` ,  

-- telewizja cyfrowa pakiety 3
 t7.ID as `7_1385` ,
 t7.P_SERVICE as `7_449` ,
 t7.P_SERVICE_NAME as `7_450` ,
 t7.P_INSTALL_QUOTE as `7_458` , t7.P_INSTALL_QUOTE_NORMAL as `t7_1578`,
 t7.P_CONTRACT_PRICE as `7_459` ,
 t7.P_PROMO_MONTH_PRICE as `7_460` ,
 t7.P_NORMAL_PRICE as `7_461` ,
 t7.P_DEAL_PROMO_MONTHS as `7_457` ,
 t7.P_NORMAL_PRICE*t0.P_DEALTERM_MONTHS - t7.P_DEAL_PROMO_MONTHS*t7.P_PROMO_MONTH_PRICE - (t0.P_DEALTERM_MONTHS-t7.P_DEAL_PROMO_MONTHS)*t7.P_CONTRACT_PRICE + (t7.P_INSTALL_QUOTE_NORMAL-t7.P_INSTALL_QUOTE) as `t7_1580`,
 if(t0.ID='1','',if(t7.ID,'TAK','NIE')) as `7_IF` ,
 if(t7.ID,t0.P_PROMOTION_NAME,'') as `7_451`

-- TV - dane z ID_TV lub ID_TVC jesli nie ma ID_TV
, if (t0.`ID_TV`>0, t0.`ID_TV`, IF(t0.`ID_TVC`>0, t0.`ID_TVC`, null)) as TV_ID
, if (t0.`ID_TV`>0, 'X', IF(t0.`ID_TVC`>0, 'X', '')) as TV_JEST
, if (t2.`ID`>0, t2.`P_SERVICE_NAME`, IF(t3.`ID`>0, t3.`P_SERVICE_NAME`, null)) as TV_P_SERVICE_NAME

, if(t0.`P_SERVICE_COMPANY`='BN','BIALL-NET Sp. z o.o., Otomin, ul. Sloneczna 43, 80-174 Gdansk, zrejestrowana w rejestrze przedsiebiorcow prowadzonym przez Sad Rejonowy Gdansk-Polnoc w Gdansku, VII Wydzial Gospodarczy KRS pod nr 0000210311 NIP 593-22-68-67, Regon: 192120212','NETDAY S.C. ul. Kopeckiego 9/24 80-809 Gdansk') as PSCN

FROM `USERS2_OFFERS_GROUPS` as t0
 left join `USERS2_OFFERS` as t1 on (t0.ID_NET!=0 and t1.ID=t0.ID_NET and t1.A_STATUS!='DELETED' and t1.P_SERVICE='NET'  )
 left join `USERS2_OFFERS` as t2 on (t0.ID_TV!=0  and t2.ID=t0.ID_TV  and t2.A_STATUS!='DELETED' and t2.P_SERVICE='TV'   )
 left join `USERS2_OFFERS` as t5 on (t0.ID_TEL!=0 and t5.ID=t0.ID_TEL and t5.A_STATUS!='DELETED' and t5.P_SERVICE='TEL'  )
 left join `USERS2_OFFERS` as t3 on (t0.ID_TVC!=0 and t3.ID=t0.ID_TVC and t3.A_STATUS!='DELETED' and t3.P_SERVICE='TVC'  )
 left join `USERS2_OFFERS` as t4 on (t0.ID_TVCP_1!=0 and t4.ID=t0.ID_TVCP_1 and t4.A_STATUS!='DELETED' and t4.P_SERVICE='TVCP' )
 left join `USERS2_OFFERS` as t6 on (t0.ID_TVCP_2!=0 and t6.ID=t0.ID_TVCP_2 and t6.A_STATUS!='DELETED' and t6.P_SERVICE='TVCP' )
 left join `USERS2_OFFERS` as t7 on (t0.ID_TVCP_3!=0 and t7.ID=t0.ID_TVCP_3 and t7.A_STATUS!='DELETED' and t7.P_SERVICE='TVCP' ) 

WHERE
--	t0.`P_SERVICE_COMPANY`='BN'
--	AND ( t0.`A_STATUS` = 'NORMAL'  OR t0.`A_STATUS`='WAITING' )
	t0.`ID`='" . DB::_($args['id_group']) . "'
GROUP BY t0.ID
ORDER BY t0.ID DESC
;

	";
	$res = DB::query($sql);
	if (!$res) {
		die("Query error!");
	}
	$data = DB::fetch_assoc($res);
	if (!$data || $data['0_1380'] != $args['id_group']) {
		die("DB error!");
	}

	$to_convert_encoding = array();
	$to_convert_encoding []= '0_S_ALIAS';
	$to_convert_encoding []= '0_451';
	$to_convert_encoding []= '1_450';
	$to_convert_encoding []= '1_451';
	$to_convert_encoding []= '2_450';
	$to_convert_encoding []= '2_451';
	$to_convert_encoding []= '3_450';
	$to_convert_encoding []= '3_451';
	$to_convert_encoding []= '4_450';
	$to_convert_encoding []= '4_451';
	$to_convert_encoding []= '5_450';
	$to_convert_encoding []= '5_451';
	$to_convert_encoding []= '6_450';
	$to_convert_encoding []= '6_451';
	$to_convert_encoding []= '7_450';
	$to_convert_encoding []= '7_451';
	$to_convert_encoding []= 'TV_P_SERVICE_NAME';

	foreach ($to_convert_encoding as $v_field) {
		if (array_key_exists($v_field, $data)) {
			$data[$v_field] = iconv('latin2', 'utf-8', $data[$v_field]);
		}
	}

	if (V::get('DBG', 0, $_GET, 'int')) {echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">data: ';print_r($data);echo'</pre>';}
	foreach ($data as $k_field => $v_value) {
		$umowa_druk = str_replace('<span id="cell-'.$k_field.'"></span>', $v_value, $umowa_druk);
	}
}


if (!empty($args['id_options'])) {
/*
 * <span id="cell-OPCJA_NET_1_450"></span>
 * <span id="cell-OPCJA_NET_1_1385"></span>
 * <span id="cell-OPCJA_NET_1_459"></span>
 * <span id="cell-OPCJA_NET_1_1580"></span>
 * <span id="cell-OPCJA_NET_1_457"></span>
 * <span id="cell-OPCJA_NET_1_460"></span>
 * <span id="cell-OPCJA_NET_1_458"></span>
 * 
 * <span id="cell-OPCJA_TEL_1_450"></span>
 * 
 * <span id="cell-OPCJA_TV_1_450"></span>
 * 
 * , '' as OPCJA_NET_1_450 -- P_SERVICE_NAME - nazwa
 * , '' as OPCJA_NET_1_1385 -- ID
 * , '' as OPCJA_NET_1_459 -- P_CONTRACT_PRICE - abonament
 * -- , t4.P_NORMAL_PRICE * t0.P_DEALTERM_MONTHS - t4.P_DEAL_PROMO_MONTHS * t4.P_PROMO_MONTH_PRICE - (t0.P_DEALTERM_MONTHS - t4.P_DEAL_PROMO_MONTHS) * t4.P_CONTRACT_PRICE + (t4.P_INSTALL_QUOTE_NORMAL - t4.P_INSTALL_QUOTE) as `t4_1580`
 * , '' as OPCJA_NET_1_1580 -- upust od ceny nominalnej
 * , '' as OPCJA_NET_1_457 -- P_DEAL_PROMO_MONTHS - liczba miesiecy promocyjnych
 * , '' as OPCJA_NET_1_460 -- P_PROMO_MONTH_PRICE - cena abonamentu w promocyjnych miesiacach
 * , '' as OPCJA_NET_1_458 -- P_INSTALL_QUOTE - cena promocyjna
 */

	$options_to_load = array();
	foreach ($args['id_options'] as $k_service => $v_options) {
		if (empty($v_options)) continue;
		foreach ($v_options as $v_option_id) {
			if ($v_option_id > 0) {
				$options_to_load[$k_service] []= $v_option_id;
			}
		}
	}

	if (V::get('DBG', 0, $_GET, 'int')) {
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">args: ';print_r($args);echo'</pre>';
		if (!empty($options_to_load)) {
			echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">options_to_load: ';print_r($options_to_load);echo'</pre>';
		}
	}

	$sql_ids = array();
	foreach ($options_to_load as $k_service => $v_options_arr) {
		foreach ($v_options_arr as $v_offerr_id) {
			$sql_ids []= "'" . $v_offerr_id . "'";
		}
	}

	if (!empty($sql_ids)) {
		$options_to_load_counter = array();
		foreach ($options_to_load as $k_service => $v_arr) {
			$options_to_load_counter[$k_service] = 0;
		}

		$to_convert_encoding = array();
		$to_convert_encoding []= 'z_450';

		$oferty_opcje = array();
		$sql = "select
				t.`ID` as z_1385
				, t.`P_SERVICE` as `z_449`
				, t.`P_SERVICE_PARENT` as `z_3280`
				, t.`P_SERVICE_NAME` as z_450
				, t.`P_CONTRACT_PRICE` as z_459
				, '' as z_1580
				, t.`P_DEAL_PROMO_MONTHS` as z_457
				, t.`P_PROMO_MONTH_PRICE` as z_460
				, t.`P_INSTALL_QUOTE` as z_458
			FROM `USERS2_OFFERS` as t
			WHERE
				t.`ID` in(" . implode(",", $sql_ids) . ")
				and t.`P_SERVICE`='OPCJE'
				--	t.`P_SERVICE_COMPANY`='BN'
				--	AND ( t.`A_STATUS` = 'NORMAL'  OR t.`A_STATUS`='WAITING' )
		";
		$res = DB::query($sql);
		while ($h = DB::fetch_assoc($res)) {
			foreach ($to_convert_encoding as $v_field) {
				if (array_key_exists($v_field, $h)) {
					$h[$v_field] = iconv('latin2', 'utf-8', $h[$v_field]);
				}
			}
			$oferty_opcje[ $h['z_1385'] ] = $h;
		}
		if (V::get('DBG', 0, $_GET, 'int')) {echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">oferty_opcje: ';print_r($oferty_opcje);echo'</pre>';}

		$replace_fields = array();
		foreach ($oferty_opcje as $k_id => $h) {
			$cur_option_service = $h['z_3280'];
			if (!array_key_exists($cur_option_service, $options_to_load)) {
				// TODO: error
				continue;
			}

			$options_to_load_counter[$cur_option_service]++;

			$cur_option_index = $options_to_load_counter[$cur_option_service];

			$replace_key_prefix = 'OPCJA_' . $cur_option_service . '_' . $cur_option_index . '_';
			$replace_fields[$replace_key_prefix . '450'] = $h['z_450'];
			$replace_fields[$replace_key_prefix . '1385'] = $h['z_1385'];
			$replace_fields[$replace_key_prefix . '459'] = $h['z_459'];
			$replace_fields[$replace_key_prefix . '1580'] = $h['z_1580'];
			$replace_fields[$replace_key_prefix . '457'] = $h['z_457'];
			$replace_fields[$replace_key_prefix . '460'] = $h['z_460'];
			$replace_fields[$replace_key_prefix . '458'] = $h['z_458'];
		}
		if (V::get('DBG', 0, $_GET, 'int')) {echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">replace_fields: ';print_r($replace_fields);echo'</pre>';}

		if (!empty($replace_fields)) {
			foreach ($replace_fields as $k_field => $v_value) {
				$umowa_druk = str_replace('<span id="cell-'.$k_field.'"></span>', $v_value, $umowa_druk);
			}
		}
	}
}

echo $umowa_druk;
