<?php

class PokazOfertyView {

	private $args = array();

	public function set($k, $v) {
		$this->args[$k] = $v;
	}

	public function get($k) {
		return V::get($k, null, $this->args);
	}

	function print_css() {
		$col_silver = "#faf2f2";
		$col_silver_0 = "#ddd";//"#F2F2F2";
		$col_silver_1 = "#e6e6e6";//"#fff";
		$col_hover = "#daf9da";
		$col_sub_hover = "#ffe5b5";
		return '<style type="text/css">' . "
body{font-family:arial; font-size:12px;}
.tbl-view{}
 .tbl-view th,
 .tbl-view td{ padding:0 2px; font-size:12px; }
 .tbl-view thead th{ background:#F3F3F3; }
#tbl-oferty {width:98%; margin-top:3px; border-color:#aaa;}
 #tbl-oferty th,
 #tbl-oferty td{font-size:12px;}
 #tbl-oferty th{background-color:#C7C7E5; border-color:#aaa;}
 #tbl-oferty .tbl-row {cursor:pointer; margin:0;}
 #tbl-oferty .tbl-row-0 td{background-color:$col_silver_0;}
 #tbl-oferty .tbl-row-1 td{background-color:$col_silver_1;}
 #tbl-oferty .cell-id{width:3%; font-size:14px; font-weight:bold; font-family:monospace;}
 #tbl-oferty .active td.cell-id{background-color:$col_hover; border-bottom:none;}
 #tbl-oferty .sub td{background-color:$col_hover;}
 #tbl-oferty .tbl-row:hover td{background-color:$col_hover;}
 #tbl-oferty .sub-oferty{width:95%; margin:0 0 0 auto; padding:0; border:none;}
 #tbl-oferty .sub-oferty tr.selected,
 #tbl-oferty .sub-oferty tr:hover td{background-color:$col_sub_hover;color:#444;}
 #tbl-oferty .sub-oferty-wrap{padding:0; border-top:none; border-bottom:1px solid #333;}
 #tbl-oferty .sub-0 .sub-oferty-wrap{background-color:$col_hover;}
 #tbl-oferty .sub-1 .sub-oferty-wrap{background-color:$col_hover;}
 #tbl-oferty .sub-oferty th{background-color:#f1f1f1; border-color:#aaa; color:#000;}
 #tbl-oferty .sub-oferty td{background-color:#fff; border-color:#aaa; color:#444;}
 #tbl-oferty code{padding:0 2px;color:#666; font-size:10px;}

 #tbl-oferty .cell-suma_1 {text-align:right; white-space:nowrap;}
 #tbl-oferty .cell-suma_2 {text-align:right; white-space:nowrap;}
 #tbl-oferty .cell-cost {text-align:right; white-space:nowrap;}
 #tbl-oferty .cell-zysk {border-left:2px solid red; background:#FFB5B5; text-align:right; white-space:nowrap;}

#tbl-oferty .row-oferty-opcje td {background:#FBFFCF; color:#aaa;}
#tbl-oferty .row-oferty-opcje-srv td {background:#CFFCFF; color:#aaa;}
#tbl-oferty .row-oferty-opcje-selected td {background:#fff; color:#444;}
#tbl-oferty tbody.hide_options .row-oferty-opcje {display:none}

.podglad-users2_deals td.DATA_DOK,
.podglad-users2_deals td.P_DEAL_DATE_NEW,
.podglad-users2_deals td.P_DEALTERM_NEW,
.podglad-users2_deals td.P_DEAL_DATE_OLD,
.podglad-users2_deals td.P_DEALTERM_OLD,
.podglad-users2_deals td.P_DEALDATE,
.podglad-users2_deals td.P_DEALDATE_TERM {white-space:nowrap;}

.podglad-users2_deals .old-deal {display:none;}
.podglad-users2_deals .old-deal td {background:#eee;}

" . '</style>';
	}

	function print_jquery_ui() {
		$out = '<link rel="stylesheet" type="text/css" href="stuff/jquery-ui-smoothness/jquery-ui-1.10.1.custom.min.css">' . "\n";
		$out .= '<script type="text/javascript" src="stuff/jquery-ui.custom.min.js"></script>' . "\n";
		return $out;
	}

	function print_ui_widget_info($title, $msg = '') {
		$out .= '<div class="ui-widget">';
			$out .= '<div class="ui-state-highlight ui-corner-all" style="margin-top:20px; padding:0 .7em;">';
				$out .= '<p><span class="ui-icon ui-icon-info" style="float:left; margin-right:.3em;"></span>';
				$out .= '<strong></strong> ' . $title . '</p>';
				if (!empty($msg)) {
					$out .= '<p>' . $msg . '</p>';
				}
			$out .= '</div>';
		$out .= '</div>';
		return $out;
	}

	function print_ui_widget_ok($title, $msg = '') {
		$out .= '<div class="ui-widget">';
			$out .= '<div class="ui-state-highlight ui-corner-all" style="margin-top:20px; padding:0 .7em;">';
				$out .= '<p><span class="ui-icon ui-icon-check" style="float:left; margin-right:.3em;"></span>';
				$out .= '<strong></strong> ' . $title . '</p>';
				if (!empty($msg)) {
					$out .= '<p>' . $msg . '</p>';
				}
			$out .= '</div>';
		$out .= '</div>';
		return $out;
	}

	function print_ui_widget_errors($title, $errors_arr = array()) {
		$out = '';
		$out .= '<div class="ui-widget">';
		$out .= '<div class="ui-state-error ui-corner-all" style="padding:0 .7em;">';
			$out .= '<p><span class="ui-icon ui-icon-alert" style="float:left; margin-right:.3em;"></span>';
			$out .= '<strong>Error:</strong> ' . $title . '</p>';
			if (!empty($errors_arr)) {
				$out .= '<ul style="color:red"><li>' . implode('</li><li>', $errors_arr) . '</li></ul>';
			}
		$out .= '</div>';
		$out .= '</div>';
		return $out;
	}

	function print_simple_tbl_header($cols, $caption = '') {
		if (!empty($caption)) echo '<caption>' . $caption . '</caption>';
		$is_ID = in_array('ID', $cols);
		echo '<thead><tr>';
		if ($is_ID) {
			echo '<th>' . "ID" . '</th>';
		}
		foreach ($cols as $v_field_name) {
			if ($is_ID && $v_field_name == 'ID') continue;
			echo '<th>' . str_replace('_', ' ', $v_field_name) . '</th>';
		}
		echo '</tr></thead>';
	}

	function print_simple_tbl_body_row($obj, $cols, $params = array()) {
		$is_ID = in_array('ID', $cols);
		$cls = V::get('class', '', $params);
		if (!empty($cls)) $cls = ' class="' . $cls . '"';
		echo '<tr' . $cls . '>';
		if ($is_ID) {
			if (isset($obj->ID)) {
				echo '<td>' . $obj->ID . '</td>';
			} else {
				echo '<td>' . "&nbsp;" . '</td>';
			}
		}
		foreach ($cols as $v_field_name) {
			if ($is_ID && $v_field_name == 'ID') continue;
			if (isset($obj->$v_field_name)) {
				echo '<td>';
				if (strlen($obj->$v_field_name) > 20) {
					echo '<span title="' . htmlspecialchars($obj->$v_field_name) . '">' . substr($obj->$v_field_name, 0, 20) . "..." . '</span>';
				} else {
					echo $obj->$v_field_name;
				}
				echo '</td>';
			} else {
				echo '<td>' . "&nbsp;" . '</td>';
			}
		}
		echo '</tr>';
	}

	function print_simple_tbl($obj, $cols, $caption = '') {
		echo '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view">';
		if (!empty($caption)) echo '<caption>' . $caption . '</caption>';
		PokazOfertyView::print_simple_tbl_header($cols);
		echo '<tbody>';
		PokazOfertyView::print_simple_tbl_body_row($obj, $cols);
		echo '</tbody>';
		echo '</table>';
	}

	function printServices($user, $active_deals_by_id) {
		?>
		<h4>Aktualne umowy</h4>
		<div style="padding:6px; border-left:1px solid silver;">
			<?php if (empty($active_deals_by_id)) : ?>
				Brak aktualnych umów
			<?php else : ?>
				<table cellspacing="0" cellpadding="0" border="1" class="tbl-view-wrap" id="tbl-user-deals-info">
				<?php
				$has_not_active = false;
				foreach ($active_deals_by_id as $k_id_deal => $v_deal) {
					if (!empty($v_deal->services)) {
						foreach ($v_deal->services as $k_p_service => $v_srv_arr) {
							foreach ($v_srv_arr as $k_srv_id => $v_srv) {
								if ($v_srv->_status != 'NORMAL') {
									$has_not_active = true;
								}
							}
						}
					}
				}
				if ($has_not_active) : ?>
				<caption class="tbl-actions">
					<button class="all">Pokaż wszystkie usługi</button>
					<button class="active_only">Pokaż tylko aktywne usługi</button>
				</caption>
				<?php endif; ?>
				<thead><tr>
					<th>ID</th>
					<th>Numer umowy</th>
					<th>Data umowy</th>
					<th>Termin</th>
					<th>Adres</th>
					<th>Pakiet</th>
				</tr></thead>
				<tbody>
				<?php
				foreach ($active_deals_by_id as $k_id_deal => $v_deal) {
					echo '<tr class="tbl-row-0">';
						echo '<th>' . $v_deal->ID_DEALS . '</th>';
						echo '<td>' . "Numer: " . $v_deal->P_DEALNUMBER . '</td>';
						echo '<td>' . "Data: " . $v_deal->P_DEALDATE . '</td>';
						echo '<td>' . "Termin: " .$v_deal->P_DEALDATE_TERM . '</td>';
						echo '<td>' . $v_deal->SERVICES_S_ADDRESS_STREET . '</td>';
						echo '<td>' . "Pakiet: " . (($v_deal->ID_OFFERS_GROUP > 0)? $v_deal->ID_OFFERS_GROUP : '<span style="color:red">' . "nieznany" . '</span>') . '</td>';
					echo '</tr>';
					if (empty($v_deal->services)) {
						echo '<tr class="tbl-row-1"><td colspan="6">' . "Brak przypisanych usług" . '</td></tr>';
					} else {
						echo '<tr class="tbl-row-1">';
							echo '<td>' . "&nbsp;" . '</td>';
							echo '<td colspan="6" style="padding:0 0 7px 0;">';
								echo '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view">';
								echo '<thead>';
								echo '<tr>';
									echo '<th>' . "ID" . '</th>';
									echo '<th>' . "P_SERVICE" . '</th>';
									echo '<th>' . "ABONAMENT_VALUE" . '</th>';
									echo '<th>' . "ID_OFFERS" . '</th>';
									echo '<th>' . "description" . '</th>';
									echo '<th>' . "parametry (ses_x_a)" . '</th>';
									echo '<th>' . "T WHEN CONNECTED" . '</th>';
									echo '<th>' . "STATUS" . '</th>';
									echo '<th>' . "HANGUP" . '</th>';
								echo '</tr>';
								echo '</thead>';
								echo '<tbody>';
								foreach ($v_deal->services as $k_p_service => $v_srv_arr) {
									foreach ($v_srv_arr as $k_srv_id => $v_srv) {
										$ses_x_a = null;
										if (!array_key_exists($k_srv_id, $v_deal->ses_x_a)) {
											echo 'brak danych';
										} else {
											$ses_x_a = $v_deal->ses_x_a[$k_srv_id];
										}

										echo '<tr class="tbl-row-1 srv-status-' . $v_srv->_status . '">';
											echo '<td>' . $v_srv->ID . '</td>';
											echo '<td>' . $v_srv->P_SERVICE . '</td>';
											echo '<td>' . $v_srv->ABONAMENT_VALUE . '</td>';
											echo '<td>' . $v_srv->ID_OFFERS . '</td>';
											echo '<td>' . $v_srv->description . '</td>';
											echo '<td>';
												if ($ses_x_a) {
													// show params
													$srv_params = array();
													if ($v_srv->NAME_LIST_SERVICES == 'USERS2') {
														$srv_params []= '<span title="' . "Początkowa prędkość dostępu (dla danej uslugi) - P_SERVICE_QUOTA" . '">' . $ses_x_a->P_SERVICE_QUOTA . "kbit" . '</span>';
														if ($ses_x_a->CONN_DIV == 'YES') {
															$srv_params []= '<span title="' . "podział łącza" . '">' . "CONN_DIV" . '</span>';
														}
														if ($ses_x_a->S_PUBLIC_IP == 'YES') {
															$srv_params []= '<span title="' . "Zewnętrzne IP" . '">' . "S_PUBLIC_IP" . '</span>';
														}
														if ($ses_x_a->S_E_PORT_1293 == 'YES') {
															$srv_params []= '<span title="' . "Przekierowanie portu TCP/IP" . '">' . "PORT" . '</span>';
														}
														if ($ses_x_a->S_SOHO_WIFI_ROUTER_1292 == 'YES') {
															$srv_params []= '<span title="' . "Dzierżawa urządzenia WIFI z ROUTEREM" . '">' . "WIFI ROUTER" . '</span>';
														}
														//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TV (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($ses_x_a);echo'</pre>';
													}
													else if ($v_srv->NAME_LIST_SERVICES == 'TV') {
														$srv_params []= '<span title="' . "Typ usługi TV do ustalenia w systemie" . '">' . $ses_x_a->P_SERVICE_CLASS . '</span>';
														if ($ses_x_a->P_DEVICE != 'BRAK') {
															$srv_params []= '<span title="' . "Dzierżawa urządzenia do odbioru TV" . '">' . $ses_x_a->P_DEVICE . '</span>';
														}
														if ($ses_x_a->P_SERVICE_SOCKETS_AMMOUNT_1298 > 0) {
															$srv_params []= '<span title="' . "Ilość dodatkowych gniazd analogowych do zainstalowania u klienta" . '">' . "Dodatkowe gniazda: " . $ses_x_a->P_SERVICE_SOCKETS_AMMOUNT_1298 . '</span>';
														}
														if ($ses_x_a->ID_DOMAINS_PACKET_ID > 0) {
															$srv_params []= '<span title="' . "Numer pakietu/cennika z zewnętrznego systemu" . '">' . $ses_x_a->ID_DOMAINS . ": " . $ses_x_a->ID_DOMAINS_PACKET_ID . '</span>';
														}
														//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TV (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($ses_x_a);echo'</pre>';
													}
													else if ($v_srv->NAME_LIST_SERVICES == 'VOIP') {
														if (!empty($ses_x_a->S_PHONE_NUMBER)) {
															$srv_params []= '<span title="' . "Numer tel" . '">' . $ses_x_a->S_PHONE_NUMBER . '</span>';
														}
														if (!empty($ses_x_a->ID_DOMAINS)) {
															$srv_params []= '<span title="' . "Numer pakietu/cennika z zewnętrznego systemu" . '">' . $ses_x_a->ID_DOMAINS . ": " . $ses_x_a->ID_DOMAINS_PACKET_ID . '</span>';
														}
													}

													if (empty($srv_params)) {
														echo 'brak danych';
													} else {
														echo implode(', ', $srv_params);
													}
												}
											echo '</td>';
											echo '<td>';
												if ($ses_x_a) {
													echo $ses_x_a->T_WHEN_CONNECTED;
												}
											echo '</td>';
											echo '<td class="col-A_STATUS_DESC-'.$v_srv->_status.'">';
												echo ($v_srv->A_STATUS_DESC)? $v_srv->A_STATUS_DESC : $v_srv->_status;
											echo '</td>';
											echo '<td>';
												if ($v_srv->HANGUP_FROM || $v_srv->HANGUP_TILL) {
													if ($v_srv->HANGUP_STATUS) {
														echo ' <em>' . $v_srv->HANGUP_STATUS . '</em>';
													}
													echo ' (';
														echo ($v_srv->HANGUP_FROM)? $v_srv->HANGUP_FROM : '0000-00-00';
														echo ' - ';
														echo ($v_srv->HANGUP_TILL)? $v_srv->HANGUP_TILL : '0000-00-00';
													echo ')';
												}
											echo '</td>';
										echo '</tr>' . "\n";
									}
								}
								echo '</tbody>';
								echo '</table>';
							echo '</td>';
						echo '</tr>';
					}
				}
				?>
				</tbody>
				</table>
			<?php endif; ?>
		</div>
		<?php
	}

	function user_with_services($user, $active_deals_by_id) {
		?>
	<style type="text/css">
.tbl-view-wrap {}
 .tbl-view-wrap tr {background:#fff;}

#tbl-user-deals-info {}
 #tbl-user-deals-info .tbl-actions {text-align:left;}

 #tbl-user-deals-info th,
 #tbl-user-deals-info td {padding:0 2px; text-align:left;}
 #tbl-user-deals-info td {border-color:#eee;}
 #tbl-user-deals-info thead th,
 #tbl-user-deals-info thead td {background:#f3f3f3;}
 #tbl-user-deals-info tr.tbl-row-0 {background:#f3f3f3;}
 #tbl-user-deals-info tr.tbl-row-1 {background:#fff;}

#tbl-user-deals-info .tbl-actions .all {display:none;}
#tbl-user-deals-info .tbl-actions .with_offers_only {display:inline;}
#tbl-user-deals-info.active_only .tbl-actions .all {display:inline;}
#tbl-user-deals-info.active_only .tbl-actions .active_only {display:none;}

#tbl-user-deals-info.active_only .srv-status- {display:none;}
#tbl-user-deals-info.active_only .srv-status-WAITING {display:none;}
#tbl-user-deals-info.active_only .srv-status-NORMAL {}
#tbl-user-deals-info.active_only .srv-status-OFF_SOFT {display:none;}
#tbl-user-deals-info.active_only .srv-status-OFF_HARD {display:none;}

#tbl-user-deals-info .col-A_STATUS_DESC-OFF_HARD {background-color:#FFBABA;}
#tbl-user-deals-info .col-A_STATUS_DESC-OFF_SOFT {background-color:#D6D6D6;}
#tbl-user-deals-info .col-A_STATUS_DESC-NORMAL {background-color:#BDFDBD;}
	</style>
	<script type="text/javascript">
jQuery(document).ready(function(){
	jQuery('#tbl-user-deals-info').find('.tbl-actions button').click(function(e){
		console.log(e);
		var n=jQuery(e.target);
		if (n.hasClass('active_only')) {
			jQuery('#tbl-user-deals-info').removeClass('all');
			jQuery('#tbl-user-deals-info').addClass('active_only');
		} else {
			jQuery('#tbl-user-deals-info').removeClass('active_only');
			jQuery('#tbl-user-deals-info').addClass('all');
		}
	})
});
	</script>
		<div style="margin:10px 0;padding:6px 10px;border:1px solid blue;background:#C0DDFC;">
		<table cellspacing="0" cellpadding="0" border="0"><tr>
		<?php if ($user) : ?>
			<th style="text-align:left">
				Dane klienta <?php echo $user->ID_BILLING_USERS . (($user->is_firma)? ' <b style="color:red">' . "(firma)" . '</b>' : ''); ?>
				<?php if ($user->BILLING_OWNER == 1) : ?>
					<em style="float:right;padding:0 10px;">BIALL-NET</em>
				<?php elseif ($user->BILLING_OWNER == 2) : ?>
					<em style="float:right;padding:0 10px;">NETDAY</em>
				<?php endif; ?>
			</th>
		<?php endif; ?>
		<th style="padding:0 0 0 10px; text-align:left; border-left:1px solid silver">
			Aktualne umowy
		</th>
		</tr><tr>
			<?php $t = 0; if ($user) : ?>
				<td style="padding:0 10px 0 0; vertical-align:top;">
					<table cellspacing="0" cellpadding="0" border="1" class="tbl-view">
						<tr class="tbl-row-<?php echo ($t = 1 - $t); ?>"><td>Imię i nazwisko</td><td><b><?php echo $user->P_NAME . " " . $user->P_NAME_SECOND; ?></b></td></tr>
						<tr class="tbl-row-<?php echo ($t = 1 - $t); ?>"><td>adres</td><td>ul. <?php echo $user->P_ADDRESS_STREET . " " . $user->P_ADDRESS_HOUSE . "/" . $user->P_ADDRESS_HOME . ", " . $user->P_ADDRESS_POST_CODE . " " . $user->P_ADDRESS_CITY; ?></td></tr>
						<tr class="tbl-row-<?php echo ($t = 1 - $t); ?>"><td>tel.</td><td><?php echo $user->P_PHONE; ?></td></tr>
						<tr class="tbl-row-<?php echo ($t = 1 - $t); ?>"><td>mail</td><td><?php echo $user->user_mail_contact; ?></td></tr>
					</table>
				</td>
			<?php endif; ?>
			<td style="padding:0 0 0 10px; vertical-align:top; border-left:1px solid silver">
			<?php if (empty($active_deals_by_id)) : ?>
				Brak aktualnych umów
			<?php else : ?>
				<table cellspacing="0" cellpadding="0" border="1" class="tbl-view-wrap" id="tbl-user-deals-info">
				<?php
				$has_not_active = false;
				foreach ($active_deals_by_id as $k_id_deal => $v_deal) {
					if (!empty($v_deal->services)) {
						foreach ($v_deal->services as $k_p_service => $v_srv_arr) {
							foreach ($v_srv_arr as $k_srv_id => $v_srv) {
								if ($v_srv->_status != 'NORMAL') {
									$has_not_active = true;
								}
							}
						}
					}
				}
				if ($has_not_active) : ?>
				<caption class="tbl-actions">
					<button class="all">Pokaż wszystkie usługi</button>
					<button class="active_only">Pokaż tylko aktywne usługi</button>
				</caption>
				<?php endif; ?>
				<thead><tr>
					<th>ID</th>
					<th>Numer umowy</th>
					<th>Data umowy</th>
					<th>Termin</th>
					<th>Adres</th>
					<th>Pakiet</th>
				</tr></thead>
				<tbody>
				<?php
				foreach ($active_deals_by_id as $k_id_deal => $v_deal) {
					echo '<tr class="tbl-row-0">';
						echo '<th>' . $v_deal->ID_DEALS . '</th>';
						echo '<td>' . "Numer: " . $v_deal->P_DEALNUMBER . '</td>';
						echo '<td>' . "Data: " . $v_deal->P_DEALDATE . '</td>';
						echo '<td>' . "Termin: " .$v_deal->P_DEALDATE_TERM . '</td>';
						echo '<td>' . $v_deal->SERVICES_S_ADDRESS_STREET . '</td>';
						echo '<td>' . "Pakiet: " . (($v_deal->ID_OFFERS_GROUP > 0)? $v_deal->ID_OFFERS_GROUP : '<span style="color:red">' . "nieznany" . '</span>') . '</td>';
					echo '</tr>';
					if (empty($v_deal->services)) {
						echo '<tr class="tbl-row-1"><td colspan="6">' . "Brak przypisanych usług" . '</td></tr>';
					} else {
						echo '<tr class="tbl-row-1">';
							echo '<td>' . "&nbsp;" . '</td>';
							echo '<td colspan="6" style="padding:0 0 7px 0;">';
								echo '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view">';
								echo '<thead>';
								echo '<tr>';
									echo '<th>' . "ID" . '</th>';
									echo '<th>' . "P_SERVICE" . '</th>';
									echo '<th>' . "ABONAMENT_VALUE" . '</th>';
									echo '<th>' . "ID_OFFERS" . '</th>';
									echo '<th>' . "description" . '</th>';
									echo '<th>' . "parametry (ses_x_a)" . '</th>';
									echo '<th>' . "T WHEN CONNECTED" . '</th>';
									echo '<th>' . "STATUS" . '</th>';
									echo '<th>' . "HANGUP" . '</th>';
								echo '</tr>';
								echo '</thead>';
								echo '<tbody>';
								foreach ($v_deal->services as $k_p_service => $v_srv_arr) {
									foreach ($v_srv_arr as $k_srv_id => $v_srv) {
										$ses_x_a = null;
										if (!array_key_exists($k_srv_id, $v_deal->ses_x_a)) {
											echo 'brak danych';
										} else {
											$ses_x_a = $v_deal->ses_x_a[$k_srv_id];
										}

										echo '<tr class="tbl-row-1 srv-status-' . $v_srv->_status . '">';
											echo '<td>' . $v_srv->ID . '</td>';
											echo '<td>' . $v_srv->P_SERVICE . '</td>';
											echo '<td>' . $v_srv->ABONAMENT_VALUE . '</td>';
											echo '<td>' . $v_srv->ID_OFFERS . '</td>';
											echo '<td>' . $v_srv->description . '</td>';
											echo '<td>';
												if ($ses_x_a) {
													// show params
													$srv_params = array();
													if ($v_srv->NAME_LIST_SERVICES == 'USERS2') {
														$srv_params []= '<span title="' . "Początkowa prędkość dostępu (dla danej uslugi) - P_SERVICE_QUOTA" . '">' . $ses_x_a->P_SERVICE_QUOTA . "kbit" . '</span>';
														if ($ses_x_a->CONN_DIV == 'YES') {
															$srv_params []= '<span title="' . "podział łącza" . '">' . "CONN_DIV" . '</span>';
														}
														if ($ses_x_a->S_PUBLIC_IP == 'YES') {
															$srv_params []= '<span title="' . "Zewnętrzne IP" . '">' . "S_PUBLIC_IP" . '</span>';
														}
														if ($ses_x_a->S_E_PORT_1293 == 'YES') {
															$srv_params []= '<span title="' . "Przekierowanie portu TCP/IP" . '">' . "PORT" . '</span>';
														}
														if ($ses_x_a->S_SOHO_WIFI_ROUTER_1292 == 'YES') {
															$srv_params []= '<span title="' . "Dzierżawa urządzenia WIFI z ROUTEREM" . '">' . "WIFI ROUTER" . '</span>';
														}
														//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TV (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($ses_x_a);echo'</pre>';
													}
													else if ($v_srv->NAME_LIST_SERVICES == 'TV') {
														$srv_params []= '<span title="' . "Typ usługi TV do ustalenia w systemie" . '">' . $ses_x_a->P_SERVICE_CLASS . '</span>';
														if ($ses_x_a->P_DEVICE != 'BRAK') {
															$srv_params []= '<span title="' . "Dzierżawa urządzenia do odbioru TV" . '">' . $ses_x_a->P_DEVICE . '</span>';
														}
														if ($ses_x_a->P_SERVICE_SOCKETS_AMMOUNT_1298 > 0) {
															$srv_params []= '<span title="' . "Ilość dodatkowych gniazd analogowych do zainstalowania u klienta" . '">' . "Dodatkowe gniazda: " . $ses_x_a->P_SERVICE_SOCKETS_AMMOUNT_1298 . '</span>';
														}
														if ($ses_x_a->ID_DOMAINS_PACKET_ID > 0) {
															$srv_params []= '<span title="' . "Numer pakietu/cennika z zewnętrznego systemu" . '">' . $ses_x_a->ID_DOMAINS . ": " . $ses_x_a->ID_DOMAINS_PACKET_ID . '</span>';
														}
														//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TV (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($ses_x_a);echo'</pre>';
													}
													else if ($v_srv->NAME_LIST_SERVICES == 'VOIP') {
														if (!empty($ses_x_a->S_PHONE_NUMBER)) {
															$srv_params []= '<span title="' . "Numer tel" . '">' . $ses_x_a->S_PHONE_NUMBER . '</span>';
														}
														if (!empty($ses_x_a->ID_DOMAINS)) {
															$srv_params []= '<span title="' . "Numer pakietu/cennika z zewnętrznego systemu" . '">' . $ses_x_a->ID_DOMAINS . ": " . $ses_x_a->ID_DOMAINS_PACKET_ID . '</span>';
														}
													}

													if (empty($srv_params)) {
														echo 'brak danych';
													} else {
														echo implode(', ', $srv_params);
													}
												}
											echo '</td>';
											echo '<td>';
												if ($ses_x_a) {
													echo $ses_x_a->T_WHEN_CONNECTED;
												}
											echo '</td>';
											echo '<td class="col-A_STATUS_DESC-'.$v_srv->_status.'">';
												echo ($v_srv->A_STATUS_DESC)? $v_srv->A_STATUS_DESC : $v_srv->_status;
											echo '</td>';
											echo '<td>';
												if ($v_srv->HANGUP_FROM || $v_srv->HANGUP_TILL) {
													if ($v_srv->HANGUP_STATUS) {
														echo ' <em>' . $v_srv->HANGUP_STATUS . '</em>';
													}
													echo ' (';
														echo ($v_srv->HANGUP_FROM)? $v_srv->HANGUP_FROM : '0000-00-00';
														echo ' - ';
														echo ($v_srv->HANGUP_TILL)? $v_srv->HANGUP_TILL : '0000-00-00';
													echo ')';
												}
											echo '</td>';
										echo '</tr>' . "\n";
									}
								}
								echo '</tbody>';
								echo '</table>';
							echo '</td>';
						echo '</tr>';
					}
				}
				?>
				</tbody>
				</table>
			<?php endif; ?>
		</td></tr></table>
		</div>
		<?php
	}

	function print_js_umowa($cols = null) {
		$cols = (!$cols)? 10 : $cols;
		return '<script type="text/javascript">' . "
function show_opcje(cell_id, service){
	var n=jQuery('#' + cell_id);
	if (!n) return;
	if (n.find('tr').length === 1) {// init, fetch data
		var tbody_opcje=jQuery('#oferty-opcje-service-' + service).find('tr');
		if (tbody_opcje.length > 0) {
			for (var i = 0; i < tbody_opcje.length; i++) {
				n.append(jQuery(tbody_opcje[i]).clone());
			}
		} else {
			n.append('<tr class=\"row-oferty-opcje\"><td colspan=\"{$cols}\">Brak danych</td></tr>');
		}

		if ('NET' === service) {
			tbody_opcje=jQuery('#oferty-opcje-service-OPCJA_TVC').find('tr');
			if (tbody_opcje.length > 0) {
				for (var i = 0; i < tbody_opcje.length; i++) {
					n.append(jQuery(tbody_opcje[i]).clone());
				}
			}
			tbody_opcje=jQuery('#oferty-opcje-service-OPCJA_TEL').find('tr');
			if (tbody_opcje.length > 0) {
				for (var i = 0; i < tbody_opcje.length; i++) {
					n.append(jQuery(tbody_opcje[i]).clone());
				}
			}
		}
	} else {
		if (!n.hasClass('hide_options')) {
			n.addClass('hide_options');
			// remove selected rows and checked inputs
			n.find('.row-oferty-opcje-selected').removeClass('row-oferty-opcje-selected');
			n.find('input[type=\'checkbox\']:checked').attr('checked', false);
		} else {
			n.removeClass('hide_options');
		}
	}
	return false;
}
function select_option(n){
	n=jQuery(n);

	console.log('n.data(p_service):', n.data('p_service'), 'n.val', n.val());
	var p_service = n.data('p_service'), opcja_id = n.val();
	if ('OPCJA_' == p_service.substr(0, 6) && opcja_id > 0) {
		var parentTr = n.parent().parent(),
			service = p_service.substr(6);

		// TODO: hide/show if checked or not
		console.log('TODO: n.prop(checked): ', n.prop('checked'));
		if (n.prop('checked')) {
			console.log('TODO: p_service: ', p_service, 'n.val()', n.val(), 'service to load:', service, 'parentTr', parentTr);
			var tbody_opcje=jQuery('#oferty-opcje-service-' + service).find('tr');
			if (tbody_opcje.length > 0) {
				for (var i = 0; i < tbody_opcje.length; i++) {
					var tr = jQuery(tbody_opcje[i]).clone();
					tr.addClass('row-oferty-opcje-to-' + opcja_id);
					tr.insertAfter(parentTr);
				}
			} else {
				n.append('<tr class=\"row-oferty-opcje\"><td colspan=\"{$cols}\">Brak danych</td></tr>');
			}
		}
		else {
			parentTr.nextAll('.row-oferty-opcje-to-' + opcja_id).remove();
		}
	}
	// <a href=\"#\" onclick=\"return show_opcje('3072-2179', 'NET');\">opcje</a>
	n.parents('tr:first').toggleClass('row-oferty-opcje-selected');
}
function print_umowa(n){
	var add='';
	n=jQuery(n);
	n.parents('table').find('input[type=\'checkbox\']:checked').each(function(ind, o){
		if (o.value > 0) {
			add += '&' + o.name + '=' + o.value;
		}
	});
	n.attr('href', n.attr('href_base') + add);
	return true;
}
" . '</script>';
	}

	function get_oferty_opcje_visible_cols() {
		$cols = array();
		$cols ['ID'] = 'ID oferty';
		//$cols ['ID_PROCES' ] = '';
		$cols ['S_OTHER_INFO' ] = '<b title="' . "Uwagi" . '">&nbsp;!&nbsp;</b>';
		$cols ['P_SERVICE_NAME' ] = 'Nazwa oferty';
		$cols ['P_INSTALL_QUOTE' ] = 'Promocyjna cena instalacji (brutto)';
		$cols ['P_PROMO_MONTH_PRICE' ] = 'Wartość abonamentu w promocyjnych miesiącach (brutto)';
		$cols ['P_CONTRACT_PRICE' ] = 'Wartość abonamentu po promocji (brutto)';
		$cols ['P_DEAL_PROMO_MONTHS' ] = 'Ilość promocyjnych miesięcy';
		//$cols ['P_DEALTERM_MONTHS' ] = '';
		$cols ['P_NORMAL_PRICE' ] = 'Nominalna wartość abonamentu (brutto)';
		$cols ['srednia'] = 'Średnia (brutto)';
		$cols ['upust'] = 'Upust od ceny normalnej do wyliczeń kary (brutto)';
		$cols ['koszt'] = 'Koszt (netto)';
		return $cols;
	}

	function print_js_show_hide_groups() {
		return '<script type="text/javascript">' . "
jQuery(document).ready(function(){
	jQuery('#tbl-oferty').on('click', '.tbl-row', function(event){
		jQuery(this).toggleClass('active');
		jQuery(this).next('tr').toggle();
	});

	jQuery('#tbl-oferty-rowin-all').on('click', function(event){
		jQuery('#tbl-oferty .tbl-row').addClass('active');
		jQuery('#tbl-oferty .sub').show();
	});
	jQuery('#tbl-oferty-zwin-all').on('click', function(event){
		jQuery('#tbl-oferty .tbl-row').removeClass('active');
		jQuery('#tbl-oferty .sub').hide();
	});

	jQuery('#tbl-oferty .tbl-row').removeClass('active');
	jQuery('#tbl-oferty .sub').hide();
});
		" . '</script>';
	}

	function print_groups_info_header($params = array()) {
		echo self::print_js_umowa();
		$show_zysk = V::get('show_zysk', $this->get('show_zysk'), $params);
		$show_www = V::get('show_www', $this->get('show_www'), $params);
		$show_status = V::get('show_status', $this->get('show_status'), $params);
		$show_rewir = V::get('show_rewir', $this->get('show_rewir'), $params);
		echo '<table cellspacing="0" cellpadding="0" border="1" class="tbl tbl-hover" id="tbl-oferty">';
		echo '<thead><tr>';
			echo '<th class="cell-id">' . "ID <code>0_1380</code>" . '</th>';
			if ($show_www) echo '<th>' . '<nobr>' . "www" . $show_www . '</nobr>' . '</th>';
			if ($show_status) echo '<th>' . '<nobr>' . "status" . $show_status . '</nobr>' . '</th>';
			if ($show_rewir) echo '<th>' . "rewir" . '</th>';
			echo '<th>' . "nazwa promocji <code>0_451</code>" . '</th>';
			echo '<th>' . "okres umowy <code>0_456</code>" . '</th>';
			echo '<th>' . "suma opłat instalacyjnych (brutto)" . '</th>';
			echo '<th>' . "suma abonamentów w promocyjnych miesiącach / po promocji (brutto)" . '</th>';
			echo '<th>' . "średnia kwota (brutto)" . '</th>';
			if ($show_zysk) {
				echo '<th>' . "suma kosztów (netto)" . '</th>';
				echo '<th>' . "zysk (netto)" . '</th>';
			}
		echo '</tr></thead>';
		echo '<tbody>';
	}

	function print_groups_info_footer() {
		echo '</tbody>';
		echo '</table>';
	}

	function print_group_info($group, $params = array()) {
		$deal_add_opcje_frm = V::get('DealAddOptionsForm', null, $params);
		$deal_rm_opcje_frm = V::get('DealRmOptionsForm', null, $params);
		$hide_header = V::get('hide_header', null, $params);
		$show_zysk = V::get('show_zysk', $this->get('show_zysk'), $params);
		$show_www = V::get('show_www', $this->get('show_www'), $params);
		$show_status = V::get('show_status', $this->get('show_status'), $params);
		$show_rewir = V::get('show_rewir', $this->get('show_rewir'), $params);
		$opcje_cols = $this->get_oferty_opcje_visible_cols();
		if (isset($opcje_cols['koszt'])) {
			unset($opcje_cols['koszt']);
		}

		if (!$hide_header) {
			$this->print_groups_info_header();
		}
		echo '<tr class="tbl-row tbl-row-1">';
			echo '<td class="cell-id">'  . $group->get('ID') . '</td>';
			if ($show_www) echo '<td>' . '<nobr>' . $group->get('OFERTA_WWW') . $show_www . '</nobr>' . '</td>';
			if ($show_status) echo '<td>' . '<nobr>' . $group->get('A_STATUS') . $show_status . '</nobr>' . '</td>';
			if ($show_rewir) echo '<td>' . $group->get('M_REWIR') . '</td>';
			echo '<td>' . $group->get('S_ALIAS') . ' <em style="color:#555;">(' . $group->get('P_PROMOTION_NAME') . ')</em></td>';
			echo '<td>' . $group->get('P_DEALTERM_MONTHS') . '</td>';
			echo '<td class="cell-suma_1">' . number_format($group->suma_P_PROMO_INSTALL_PRICE, 2, ',', ' ') . " zł" . '</td>';
			echo '<td class="cell-suma_2">' . "<b>" . number_format($group->suma_P_PROMO_MONTH_PRICE, 2, ',', ' ') . "</b> zł" . " / " . number_format($group->suma_P_CONTRACT_PRICE, 2, ',', ' ') . " zł" . '</td>';
			echo '<td class="cell-srednia">' . "" . number_format($group->suma_P_OFFER_SREDNIA, 2, ',', ' ') . " zł" . '</td>';
			if ($show_zysk) {
				echo '<td class="cell-cost">' . "" . number_format($group->suma_P_OFFER_COSTS, 2, ',', ' ') . " zł" . '</td>';
				echo '<td class="cell-zysk">' . "" . number_format($group->suma_P_OFFER_SREDNIA_NETTO - $group->suma_P_OFFER_COSTS, 2, ',', ' ') . " zł" . '</td>';
			}
		echo '</tr>';
		echo '<tr class="sub sub-0">';
			echo '<td colspan="' . 99 . '" class="sub-oferty-wrap">';
				echo '<table class="sub-oferty" cellspacing="0" cellpadding="0" border="1">';
				echo '<thead>';
				echo '<tr>';
					echo '<th>';
						if (!V::get('hide_print', '', $params)) {
							$link_href = "./odt2xhtml/1379.umowa.php";
							$link_href .= "?id_group=" . $group->get('ID');
							if (($user_id = V::get('user_id', 0, $params, 'int')) > 0) {
								$link_href .= "&id_user={$user_id}";
							}
							$js = "return print_umowa(this);";
							echo '<a href="' . $link_href . '" href_base="' . $link_href . '" title="' . "Drukuj umowę [1379]" . '" target="_blank" onclick="' . $js . '">' . '<img src="icon/print.gif" height="24" />' . '</a>';
						}
					echo '</th>';
					foreach ($opcje_cols as $k_zasob_id => $v_label) {
						echo '<th>' . $v_label . '</th>';
					}
				echo '</tr>';
				echo '</thead>';
				foreach ($group->get_offers() as $k_p_service => $v_offers_arr) {
					foreach ($v_offers_arr as $k_offer_id => $v_offer) {
						echo '<tbody>';
						echo '<tr>';
							echo '<td>';
								echo $v_offer->P_SERVICE;
							echo '</td>';
							foreach ($opcje_cols as $k_field_name => $v_label) {
								echo '<td>' . $v_offer->$k_field_name . '</td>';
							}
						echo '</tr>';
						echo '</tbody>';

						$oferta_opcje = $group->get_options_by_service($v_offer->P_SERVICE);
						if (!empty($oferta_opcje)) {
							foreach ($oferta_opcje as $v_offer_opcja_id => $v_opcja) {
								echo '<tr class="row-oferty-opcje' . (($deal_add_opcje_frm)? ' row-oferty-opcje-selected' : '') . '">';
									echo '<td>' . "&nbsp;";
										if (!$deal_add_opcje_frm) {
											if ($deal_rm_opcje_frm) {
												echo '<input type="checkbox" name="id_options_'.$v_offer->P_SERVICE.'[]" value="' . $v_opcja->ID . '">';
											} else {
												echo '<span style="display:none">';
													echo '<input type="checkbox" name="id_options_'.$v_offer->P_SERVICE.'[]" value="' . $v_opcja->ID . '" checked="checked">';
												echo '</span>';
											}
										}
									echo '</td>';
									foreach ($opcje_cols as $k_field => $v_type_label) {
										if ($k_field == 'koszt') {
											echo '<td>' . number_format($v_opcja->$k_field, 2, ',', ' ') . '</td>';
										} else {
											echo '<td>' . $v_opcja->$k_field . '</td>';
										}
									}
/*
									echo '<td>' . $v_opcja->P_SERVICE_NAME . '</td>';
									echo '<td>' . $v_opcja->P_INSTALL_QUOTE . '</td>';
									echo '<td>' . $v_opcja->P_PROMO_MONTH_PRICE . '</td>';
									echo '<td>' . $v_opcja->P_CONTRACT_PRICE . '</td>';
									echo '<td>' . ((isset($v_opcja->P_DEAL_PROMO_MONTHS))? $v_opcja->P_DEAL_PROMO_MONTHS : 0) . '</td>';
									echo '<td>' . $v_opcja->P_NORMAL_PRICE . '</td>';
									echo '<td></td>';// Średnia
									echo '<td></td>';// Upust od ceny normalnej do wyliczeń kary
									//echo '<td></td>';
*/
								echo '</tr>';
							}
						}

						if ($deal_add_opcje_frm) {
							$add_oferty_opcje = $deal_add_opcje_frm->get_oferty_opcje_by_service($v_offer->P_SERVICE);
							foreach ($add_oferty_opcje as $k_id => $v_oferta) {
								if (array_key_exists($k_id, $oferta_opcje)) {
									continue;
								}
								echo '<tr class="row-oferty-opcje' . (($deal_add_opcje_frm->has_option($v_offer->P_SERVICE, $k_id))? ' row-oferty-opcje-selected' : '') . '">';
									echo '<td>';
										$chck = ($deal_add_opcje_frm->has_option($v_offer->P_SERVICE, $k_id))? ' checked="checked"' : '';
										$option_name = 'id_options_' . $v_offer->P_SERVICE . '[]';
										echo '<input type="checkbox" name="'.$option_name.'" value="' . $k_id . '" onclick="return select_option(this);" '.$chck.' />';
									echo '</td>';
									foreach ($opcje_cols as $k_field => $v_type_label) {
										if ($k_field == 'koszt') {
											echo '<td>' . number_format($v_oferta->$k_field, 2, ',', ' ') . '</td>';
										} else {
											echo '<td>' . $v_oferta->$k_field . '</td>';
										}
									}
								echo '</tr>';
							}
						}
					}
				}
				echo '</table>';
			echo '</td>';
		echo '</tr>';
		if (!$hide_header) {
			$this->print_groups_info_footer();
		}
	}

	function print_options_tree($options_tree, $cols, $title = 'Wybrane opcje') {
		echo '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view">';
		echo '<caption style="text-align:left;">' . $title . ':</caption>';
		echo '<thead>';
			echo '<tr>';
				echo '<td></td>';
				foreach ($cols as $k_field => $v_label) {
					echo '<td>' . $v_label . '</td>';
				}
			echo '</tr>';
		echo '</thead>';
		foreach ($options_tree as $k_service => $v_options) {
			echo '<tr>';
				echo '<td>';
					echo $k_service;
				echo '</td>';
				echo '<td colspan="' . count($cols) . '">';
				echo '</td>';
			echo '</tr>';
			foreach ($v_options as $v_option) {
				echo '<tr>';
					echo '<td></td>';
					foreach ($cols as $k_field => $v_label) {
						echo '<td>' . $v_option->$k_field . '</td>';
					}
				echo '</tr>';
			}
		}
		echo '</table>';
	}

}
