' . "KONFIGURATOR" . ''; switch ($task) { case 'KONFIGURATOR': { USERS2_OFFERS_GROUPS_konfigurator(); break; } case 'KONFIGURATOR2': { $subtask = V::get('subtask', '', $_REQUEST); switch ($subtask) { case 'ajax_save': { USERS2_OFFERS_GROUPS_konfigurator2_ajax_save(); break; } case 'ajax_fltrs': { USERS2_OFFERS_GROUPS_konfigurator2_ajax_fltrs(); break; } default: USERS2_OFFERS_GROUPS_konfigurator2(); } break; } default: WIEVTABLE( $params );// TODO: redirect to index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=1614 } } function USERS2_OFFERS_GROUPS_EDIT() { SEF('EDIT_TABLE_RECORD'); EDIT_TABLE_RECORD(); } function USERS2_OFFERS_GROUPS_HIST() { SEF('WIEVTABLE_HIST'); WIEVTABLE_HIST(); } function USERS2_OFFERS_GROUPS_group_status() { $group_id = V::get('group_id', '', $_REQUEST, 'int'); $change = V::get('change', '', $_REQUEST); if ($change == '-') { $change = 'OFF_HARD'; } else if ($change == '+') { $change = 'NORMAL'; } if ($group_id <= 0 || !in_array($change, array('NORMAL', 'OFF_HARD'))) { return; } $db = DB::getDB(); $sqlObj = new stdClass(); $sqlObj->ID = $group_id; $sqlObj->A_STATUS = $change; $db->UPDATE_OBJ('USERS2_OFFERS_GROUPS', $sqlObj); } function USERS2_OFFERS_GROUPS_group_www() { $group_id = V::get('group_id', '', $_REQUEST, 'int'); $change = V::get('change', '', $_REQUEST); if ($change == '-') { $change = 'NIE'; } else if ($change == '+') { $change = 'TAK'; } if ($group_id <= 0 || !in_array($change, array('NIE', 'TAK'))) { return; } $db = DB::getDB(); $sqlObj = new stdClass(); $sqlObj->ID = $group_id; $sqlObj->OFERTA_WWW = $change; $db->UPDATE_OBJ('USERS2_OFFERS_GROUPS', $sqlObj); } function USERS2_OFFERS_GROUPS_group_status_all() { echo'
(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_POST);echo'';
$change = V::get('change', '', $_REQUEST);
$sqlChange = '';
if ($change == '-') {
$sqlChange = 'OFF_HARD';
}
else if ($change == '+') {
$sqlChange = 'NORMAL';
}
if (!in_array($sqlChange, array('NORMAL', 'OFF_HARD'))) {
return;
}
$filters = array();// sql field -> form field name
$filters ['P_PROMOTION_NAME']= 'f_P_PROMOTION_NAME';
$filters ['P_OFFER_TERM_BEGIN']= 'f_P_OFFER_TERM_BEGIN';
$filters ['P_OFFER_TERM']= 'f_P_OFFER_TERM';
$filters ['P_SERVICE_COMPANY']= 'f_P_SERVICE_COMPANY';
$filters ['A_STATUS']= 'f_A_STATUS';
$filters ['S_ADDRESS_STREET']= 'f_S_ADDRESS_STREET';
$args = array();
foreach ($filters as $k_field_name => $v_req_field_name) {
$args[$k_field_name] = V::get($v_req_field_name, '%', $_REQUEST);
}
$filters_empty = true;
foreach ($args as $k => $v_value) {
if ($v_value != '%') {
$filters_empty = false;
}
}
if (!$filters_empty) {
$items = array();
$total = 0;
$totalAll = Offers_Groups_Model::get_total($args);
if ($totalAll > 0) {
$items = Offers_Groups_Model::get_items($args);
$params = Offers_Groups_Konfigurator_Model::get_params_from_items($items);
// user selected params
$user_params_empty = true;
$user_params = array();
foreach ($params as $k_type => $v_params) {
if (empty($v_params)) continue;
$k_field_name = "p_" . $k_type;// "[]"
$user_params[$k_type] = V::get($k_field_name, null, $_GET, 'array');
if (!empty($user_params[$k_type])) {
$user_params_empty = false;
}
}
$user_items = Offers_Groups_Konfigurator_Model::get_items_by_params($items, $user_params);
$total = count($user_items);
}
echo '' . "Do zmiany ({$total}) rekordów:"; echo '
'; echo ''; echo'';
if ($total > 0) {
$db = DB::getDB();
foreach ($user_items as $item) {
$sqlObj = new stdClass();
$sqlObj->ID = $item->ID;
$sqlObj->A_STATUS = $sqlChange;
echo "TODO: " . json_encode($sqlObj) . "...";
if ('YES' == V::get('confirm', '', $_REQUEST)) {
echo " TODO-SQL";
if (false) {
$ret = $db->UPDATE_OBJ('USERS2_OFFERS_GROUPS', $sqlObj);
if ($ret > 0) {
echo " DONE ({$ret})";
} else {
echo " ERR";
}
}
}
echo "\n";
}
}
echo'';
}
}
function USERS2_OFFERS_GROUPS_group_www_all() {
$change = V::get('change', '', $_REQUEST);
$sqlChange = '';
if ($change == '-') {
$sqlChange = 'NIE';
}
else if ($change == '+') {
$sqlChange = 'TAK';
}
if (!in_array($sqlChange, array('NIE', 'TAK'))) {
return;
}
$filters = array();// sql field -> form field name
$filters ['P_PROMOTION_NAME']= 'f_P_PROMOTION_NAME';
$filters ['P_OFFER_TERM_BEGIN']= 'f_P_OFFER_TERM_BEGIN';
$filters ['P_OFFER_TERM']= 'f_P_OFFER_TERM';
$filters ['P_SERVICE_COMPANY']= 'f_P_SERVICE_COMPANY';
$filters ['A_STATUS']= 'f_A_STATUS';
$filters ['S_ADDRESS_STREET']= 'f_S_ADDRESS_STREET';
$args = array();
foreach ($filters as $k_field_name => $v_req_field_name) {
$args[$k_field_name] = V::get($v_req_field_name, '%', $_REQUEST);
}
$filters_empty = true;
foreach ($args as $k => $v_value) {
if ($v_value != '%') {
$filters_empty = false;
}
}
if (!$filters_empty) {
$items = array();
$total = 0;
$totalAll = Offers_Groups_Model::get_total($args);
if ($totalAll > 0) {
$items = Offers_Groups_Model::get_items($args);
$params = Offers_Groups_Konfigurator_Model::get_params_from_items($items);
// user selected params
$user_params_empty = true;
$user_params = array();
foreach ($params as $k_type => $v_params) {
if (empty($v_params)) continue;
$k_field_name = "p_" . $k_type;// "[]"
$user_params[$k_type] = V::get($k_field_name, null, $_GET, 'array');
if (!empty($user_params[$k_type])) {
$user_params_empty = false;
}
}
$user_items = Offers_Groups_Konfigurator_Model::get_items_by_params($items, $user_params);
$total = count($user_items);
}
echo '' . "Do zmiany ({$total}) rekordów:"; echo '
'; echo ''; echo'';
if ($total > 0) {
$db = DB::getDB();
foreach ($user_items as $item) {
$sqlObj = new stdClass();
$sqlObj->ID = $item->ID;
$sqlObj->OFERTA_WWW = $sqlChange;
echo "TODO: " . json_encode($sqlObj) . "...";
if ('YES' == V::get('confirm', '', $_REQUEST)) {
echo " TODO-SQL";
if (false) {
$ret = $db->UPDATE_OBJ('USERS2_OFFERS_GROUPS', $sqlObj);
if ($ret > 0) {
echo " DONE ({$ret})";
} else {
echo " ERR";
}
}
}
echo "\n";
}
}
echo'';
}
}
function USERS2_OFFERS_GROUPS_konfigurator() {
global $thiss;
//echo '' . "USERS2_OFFERS_GROUPS" . '';
echo '[1614] USERS2_OFFERS_GROUPS';
$filters = array();// sql field -> form field name
$filters ['P_PROMOTION_NAME']= 'f_P_PROMOTION_NAME';
$filters ['P_OFFER_TERM_BEGIN']= 'f_P_OFFER_TERM_BEGIN';
$filters ['P_OFFER_TERM']= 'f_P_OFFER_TERM';
$filters ['P_SERVICE_COMPANY']= 'f_P_SERVICE_COMPANY';
$filters ['A_STATUS']= 'f_A_STATUS';
$filters ['S_ADDRESS_STREET']= 'f_S_ADDRESS_STREET';
$args = array();
foreach ($filters as $k_field_name => $v_req_field_name) {
$args[$k_field_name] = V::get($v_req_field_name, '%', $_REQUEST);
}
Offers_Groups_View::filters($filters, $args);
$filters_empty = true;
foreach ($args as $k => $v_value) {
if ($v_value != '%') {
$filters_empty = false;
}
}
if ($filters_empty) {
echo '' . "Wybierz parametry pakietów" . '
'; } else { Offers_Groups_View::show_css(); echo 'items: ';print_r($items);echo''; $params = Offers_Groups_Konfigurator_Model::get_params_from_items($items); //echo'
params: ';print_r($params);echo''; // user selected params $user_params_empty = true; $user_params = array(); foreach ($params as $k_type => $v_params) { if (empty($v_params)) continue; $k_field_name = "p_" . $k_type;// "[]" $user_params[$k_type] = V::get($k_field_name, null, $_GET, 'array'); if (!empty($user_params[$k_type])) { $user_params_empty = false; } } //echo'
$user_params: ';print_r($user_params);echo''; if (!$user_params_empty) { // tylko pakiety pasujące do wybranych parametrów } // konfigurator form echo ''; if (!$user_params_empty) { // tylko pakiety pasujące do wybranych parametrów $user_items = Offers_Groups_Konfigurator_Model::get_items_by_params($items, $user_params); //echo'
$user_items: ';print_r($user_items);echo''; if (empty($user_items)) { echo '
' . "Brak ofert pasujących do wybranych kryteriów." . '
'; } else { echo '| Oferty Filtr | [4968] OD | P SERVICE COMPANY | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Pakiety Filtr / Dodaj |
[1615] S ALIAS [1616] P PROMOTION NAME [1617] P DEALTERM MONTHS |
[1618] P OFFER TERM BEGIN [1619] P OFFER TERM [1627] M REWIR |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$params (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($params);echo'';}
if (empty($params['fltr_o_od'])) {
$data->err_msg = "Ustaw pole OD (fltr_o_od)";
echo json_encode($data);
exit;
}
$db = DB::getDB();
$sql = "select o.`ID`
, o.`P_NAME`
, o.`P_SERVICE_NAME`
, o.`P_S_PARAM1`
, o.`P_INSTALL_QUOTE`
, o.`P_CONTRACT_PRICE`
, o.`P_PROMO_MONTH_PRICE`
, o.`P_NORMAL_PRICE`
, o.`P_SERVICE`
, o.`P_SERVICE_COMPANY`
from `USERS2_OFFERS` as o
where o.`A_RECORD_CREATE_DATE`>'{$params['fltr_o_od']}'
and o.`A_STATUS` in('WAITING', 'NORMAL')
and o.`P_SERVICE` in('NET','TV','TVC','TEL')
and o.`P_SERVICE_COMPANY`='{$params['fltr_COM']}'
limit 1000
";
if(V::get('DBG', '', $_GET) > 0){echo'$sql-offers (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'';}
$res = $db->query($sql);
while ($r = $db->fetch($res)) {
$data->offers[$r->ID] = $r;
if ($r->P_SERVICE == 'NET') {
$data->services[$r->P_SERVICE][$r->P_S_PARAM1] = $r->P_S_PARAM1;
$data->srv_offer_ids[$r->P_SERVICE][$r->P_S_PARAM1][] = $r->ID;
}
else if ($r->P_SERVICE == 'TV') {
$data->services[$r->P_SERVICE][$r->P_SERVICE_NAME] = $r->P_SERVICE_NAME;
$data->srv_offer_ids[$r->P_SERVICE][$r->P_SERVICE_NAME][] = $r->ID;
}
else if ($r->P_SERVICE == 'TVC') {
$data->services[$r->P_SERVICE][$r->P_SERVICE_NAME] = $r->P_SERVICE_NAME;
$data->srv_offer_ids[$r->P_SERVICE][$r->P_SERVICE_NAME][] = $r->ID;
}
else if ($r->P_SERVICE == 'TEL') {
$data->services[$r->P_SERVICE][$r->P_SERVICE_NAME] = $r->P_SERVICE_NAME;
$data->srv_offer_ids[$r->P_SERVICE][$r->P_SERVICE_NAME][] = $r->ID;
}
}
foreach ($data->services as $k => $v) {
asort($data->services[$k]);
}
$sql = "select g.`ID`
, g.`S_ALIAS`
, g.`P_PROMOTION_NAME`
, g.`P_DEALTERM_MONTHS`
, g.`P_OFFER_TERM_BEGIN`
, g.`P_OFFER_TERM`
, g.`M_REWIR`
, g.`ID_NET`
, g.`ID_TV`
, g.`ID_TVC`
, g.`ID_TEL`
from `USERS2_OFFERS_GROUPS` as g
where g.`S_ALIAS` like '{$params['fltr_p_1615']}'
and g.`P_PROMOTION_NAME` like '{$params['fltr_p_1616']}'
and g.`P_DEALTERM_MONTHS` like '{$params['fltr_p_1617']}'
and g.`P_OFFER_TERM_BEGIN` like '{$params['fltr_p_1618']}'
and g.`P_OFFER_TERM` like '{$params['fltr_p_1619']}'
and g.`M_REWIR` like '{$params['fltr_p_1627']}'
and g.`A_STATUS` in('WAITING', 'NORMAL','MONITOR')
and g.`P_SERVICE_COMPANY`='{$params['fltr_COM']}'
limit 1000
";
if(V::get('DBG', '', $_GET) > 0){echo'$sql-offers (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'';}
$res = $db->query($sql);
while ($r = $db->fetch($res)) {
$r->_params = array();
if ($r->ID_NET > 0) {
if (empty($data->offers[$r->ID_NET])) {
continue;
}
$r->_params['NET'] = $data->offers[$r->ID_NET]->P_S_PARAM1;
}
if ($r->ID_TV > 0) {
if (empty($data->offers[$r->ID_TV])) {
continue;
}
$r->_params['TV'] = $data->offers[$r->ID_TV]->P_SERVICE_NAME;
}
if ($r->ID_TVC > 0) {
if (empty($data->offers[$r->ID_TVC])) {
continue;
}
$r->_params['TVC'] = $data->offers[$r->ID_TVC]->P_SERVICE_NAME;
}
if ($r->ID_TEL > 0) {
if (empty($data->offers[$r->ID_TEL])) {
continue;
}
$r->_params['TEL'] = $data->offers[$r->ID_TEL]->P_SERVICE_NAME;
}
$data->groups[$r->ID] = $r;
}
if(V::get('DBG', '', $_GET) > 0){foreach ($data as $kFld => $v) {echo'data:'.$kFld.'('.count($v).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($v);echo'';}}
echo json_encode($data);
exit;
}
class Offers_Groups_Konfigurator2_Model {
public function __construct() {
if (!isset($_SESSION['Offers_Groups_Konfigurator2_Model'])) {
$_SESSION['Offers_Groups_Konfigurator2_Model'] = array();
}
}
public function getCache($key, $default = '') {
return V::get($key, $default, $_SESSION['Offers_Groups_Konfigurator2_Model']);
}
public function setCache($key, $val) {
$_SESSION['Offers_Groups_Konfigurator2_Model'][$key] = $val;
}
}
class Offers_Groups_Konfigurator_Model {
function get_base_params() {
$ret = array();
$ret['NET'] = array();
$ret['TV'] = array();
$ret['TVC'] = array();
$ret['TVCP'] = array();// multi
$ret['TEL'] = array();
return $ret;
}
function get_params_from_items(&$items) {
$ret = self::get_base_params();
if (empty($items)) {
return $ret;
}
foreach ($items as $g_id => $v_group) {
foreach ($v_group->oferty as $o_id => $v_offer) {
switch ($v_offer->P_SERVICE) {
case 'NET':
$ret['NET'] [$v_offer->P_S_PARAM1] = true;
break;
case 'TV':
$ret['TV'] [$v_offer->P_SERVICE_NAME] = true;
break;
case 'TVC':
$ret['TVC'] [$v_offer->P_SERVICE_NAME] = true;
break;
case 'TVCP':
$ret['TVCP'] [$v_offer->P_SERVICE_NAME] = true;
break;
case 'TEL':
$ret['TEL'] [$v_offer->P_SERVICE_NAME] = true;
break;
default:
}
}
}
// reverse keys - values
foreach ($ret as $k_service => $v_arr) {
$ret [$k_service] = array_keys($v_arr);
}
uasort($ret['NET'], array('self', 'sort_net_by_param1_callback'));
return $ret;
}
public static function sort_net_by_param1_callback( $a1, $a2 ) {
// clean vars
$args = array($a1, $a2);
foreach ($args as $k => $v_arg) {
$arg = $v_arg;
if (strpos($arg, 'Mb/s') !== false) $arg = str_replace('Mb/s', '', $arg);
else if (strpos($arg, 'kb/s') !== false) $arg = str_replace('kb/s', '', $arg);
if (is_numeric($arg)) {
$args[$k] = $arg;
}
}
if ($args[0] < $args[1]) {
return -1;
} else if ($args[0] > $args[1]) {
return 1;
} else {
return 0;
}
}
function get_items_by_params(&$items, &$user_params) {
//echo'$user_params: ';print_r($user_params);echo''; $user_params_checked_ok = 0; foreach ($user_params as $k_type => $v_params) { $user_params_checked_ok += count($v_params); } $ret = array(); foreach ($items as $item) { $user_params_checked = 0; foreach ($user_params as $k_type => $v_params) { if (empty($v_params)) continue; // if $k_type != 'TVCP' - jesli BRAK to powinno nie byc danej oferty if ($k_type != 'TVCP') { $v_param = reset($v_params); if ($v_param == 'BRAK') { $field_id_offer = "ID_" . $k_type; if ($item->$field_id_offer <= 0) { $user_params_checked++; } continue; } } $v_offer = null; $field_id_offer = "ID_" . $k_type; if ($k_type == 'TVCP') {// ID_TVCP_1, ID_TVCP_2, ID_TVCP_3, ID_TVCP_4 for ($i = 1; $i <= 4; $i++) { $field_id_offer_tvcp = $field_id_offer . "_" . $i; if ($item->$field_id_offer_tvcp > 0) { $v_offer = V::get($item->$field_id_offer_tvcp, '', $item->oferty); if ($v_offer && in_array($v_offer->P_SERVICE_NAME, $v_params)) { $user_params_checked++; } } } } if ($item->$field_id_offer > 0) { $v_offer = V::get($item->$field_id_offer, '', $item->oferty); } if (!$v_offer) continue; switch ($k_type) { case 'NET': if (in_array($v_offer->P_S_PARAM1, $v_params)) { $user_params_checked++; } break; case 'TV': case 'TVC': //case 'TVCP': case 'TEL': if (in_array($v_offer->P_SERVICE_NAME, $v_params)) { $user_params_checked++; } break; default: } } if ($user_params_checked == $user_params_checked_ok) { $ret [$item->ID] = $item; } } return $ret; } } class Offers_Groups_View { function show_css() { ?> '; echo '
| '; echo $k_field_name; echo ' | '; } echo '' . " " . ' | '; echo '
|---|---|
| '; if ($k_field_name == 'A_STATUS') { $filter_values = array(); $filter_values['%'] = "%"; $filter_values['WAITING'] = "WAITING"; $filter_values['NORMAL'] = "NORMAL"; $filter_values['MONITOR'] = "MONITOR"; $filter_values['OFF_HARD'] = "OFF_HARD"; $filter_values['WAITING_NORMAL'] = "NORMAL + WAITING"; echo ''; } else { echo ''; } echo ' | '; } echo ''; echo ''; echo ''; echo ''; echo ' | '; echo '
| ' . str_replace('_', ' ', $field_name) . ' | '; } echo '' . "cena" . ' | '; echo '' . "cena instalacji" . ' | '; //echo '' . "średnia" . ' | '; echo '
|---|---|---|---|
| ' . $item->$field_name . ' | '; } echo '' . ' | ';
echo '' . ' | ';
//echo '' . ' | ';
echo '
' . "Nie odnaleziono mieszkania {$v_value}" . '
'; } } } else { $sql_where_and_arr []= "g.`" . $k_field . "` like '" . DB::_($v_value) . "'"; } } $sql_where = (!empty($sql_where_and_arr))? implode(" and ", $sql_where_and_arr) : ""; if (1 == V::get('DBG', '', $_GET)) { echo'sql_where (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql_where);echo'';
}
return $sql_where;
}
function get_total($args) {
$ret = 0;
$sql_where = self::_parse_where($args);
$sql = "select count(1) as cnt
from `USERS2_OFFERS_GROUPS` as g
where $sql_where
";
$res = DB::query($sql);
if ($r = DB::fetch($res)) {
$ret = $r->cnt;
}
return $ret;
}
function get_items($args) {
$db = DB::getDB();
$external_offer_fields = array();
$external_offer_fields []= 'ID_NET';
$external_offer_fields []= 'ID_TEL';
$external_offer_fields []= 'ID_TV';
$external_offer_fields []= 'ID_TVC';
$external_offer_fields []= 'ID_TVCP_1';
$external_offer_fields []= 'ID_TVCP_2';
$external_offer_fields []= 'ID_TVCP_3';
$external_offer_fields []= 'ID_TVCP_4';
$oferty_ids = array();
$ret = array();
$sql_where = self::_parse_where($args);
$sql_select = array();
$cols = self::cols_groups();
foreach ($cols as $v_col) {
$sql_select []= "g.`" . $v_col . "`";
}
$sql_select = implode(", ", $sql_select);
$sql = "select {$sql_select}
from `USERS2_OFFERS_GROUPS` as g
where {$sql_where}
order by ID DESC
";
$res = $db->query($sql);
while ($r = $db->fetch($res)) {
$ret [$r->ID] = $r;
foreach ($external_offer_fields as $external_offer_field) {
if ($r->$external_offer_field > 0) $oferty_ids [$r->$external_offer_field] []= $r->ID;
}
}
if (!empty($oferty_ids)) {
$sql_oferty_ids = array();
foreach ($oferty_ids as $k_offer_id => $v) {
$sql_oferty_ids []= "'" . $k_offer_id . "'";
}
$sql_select = array();
$cols = self::cols_offers();
foreach ($cols as $v_col) {
$sql_select []= "o.`" . $v_col . "`";
}
//$sql_select []= "'' as upust";
//$sql_select []= "o.P_NORMAL_PRICE*12 - o.P_DEAL_PROMO_MONTHS*o.P_PROMO_MONTH_PRICE - (12 - o.P_DEAL_PROMO_MONTHS)*o.P_CONTRACT_PRICE + (o.P_INSTALL_QUOTE_NORMAL - o.P_INSTALL_QUOTE) as `test_upust`";
//$sql_select []= "ROUND(( ((12 - o.P_DEAL_PROMO_MONTHS)*o.P_CONTRACT_PRICE) + (o.P_DEAL_PROMO_MONTHS*o.P_PROMO_MONTH_PRICE) + (o.P_INSTALL_QUOTE))/12, 2) as test_srednia";
//$sql_select []= "ROUND(( ((12 - o.P_DEAL_PROMO_MONTHS)*o.P_CONTRACT_PRICE_NETTO) + (o.P_DEAL_PROMO_MONTHS*o.P_PROMO_MONTH_PRICE_NETTO) + (o.P_INSTALL_QUOTE/1.23))/12, 2) as test_srednia_netto";
$sql_select = implode(", ", $sql_select);
$sql = "select {$sql_select}
from `USERS2_OFFERS` as o
where
o.`ID` in(" . implode(", ", $sql_oferty_ids) . ")
";
$res = $db->query($sql);
while ($r = $db->fetch($res)) {
$group_ids_to_add = $oferty_ids[$r->ID];
foreach ($group_ids_to_add as $v_group_id) {
if (!isset($ret [$v_group_id]->oferty)) $ret [$v_group_id]->oferty = array();
$ret [$v_group_id]->oferty [$r->ID] = clone $r;
}
}
}
return $ret;
}
function get_offers_costs_by_offer_ids($offer_ids = array()) {
$oferty_costs = array();
$sql_where = "";
if (!empty($offer_ids)) {
$sql_where = "where o.`ID` in (" . implode(",", $offer_ids) . ")";
}
$sql = "select
o.`ID` as ID_OFERTY
, c.`P_OFFER_COSTS`
from `USERS2_OFFERS` as o
join `USERS2_OFFERS_COSTS_GROUPS` as c on (c.`ID`=o.`ID_COSTS_GROUPS`)
$sql_where
";
$res = DB::query( $sql );
while ($r = DB::fetch( $res )) {
$oferty_costs[ $r->ID_OFERTY ] = $r->P_OFFER_COSTS;
}
return $oferty_costs;
}
}
class X_Column_Base {
var $_data;
var $_errors;
function __construct() {
$this->_errors = array();
$this->_data = array();
}
function get($field) {
return (array_key_exists($field, $this->_data))? $this->_data[$field] : '';
}
function set($field, $value) {
if (array_key_exists($field, $this->_data)) {
$this->_data[$field] = $value;
}
}
function get_cols() {
return array_keys($this->_data);
}
function get_data() {
return $this->_data;
}
function add_error($msg) {
$this->_errors []= $msg;
}
function get_errors() {
return $this->_errors;
}
function has_errors() {
return !empty($this->_errors);
}
}
class X_Column_users2_offers_groups extends X_Column_Base {
var $_offers;
var $_offers_by_id;
var $_id_options;
var $_group_external_offers_id;
var $suma_P_PROMO_INSTALL_PRICE = 0;
var $suma_P_PROMO_MONTH_PRICE = 0;
var $suma_P_CONTRACT_PRICE = 0;
var $suma_P_OFFER_SREDNIA = 0;
var $suma_P_OFFER_SREDNIA_NETTO = 0;
var $suma_P_OFFER_COSTS = 0;
function __construct() {
parent::__construct();
$this->_offers = array();
$this->_offers_by_id = array();
$this->_id_options = array();
$this->_group_external_offers_id = array('ID_NET'=>'NET','ID_TV'=>'TV','ID_TVC'=>'TVC','ID_TVCP_1'=>'TVC','ID_TVCP_2'=>'TVC','ID_TVCP_3'=>'TVC','ID_TVCP_4'=>'TVC','ID_TEL'=>'TEL');
$this->suma_P_PROMO_INSTALL_PRICE = 0;
$this->suma_P_PROMO_MONTH_PRICE = 0;
$this->suma_P_CONTRACT_PRICE = 0;
$this->suma_P_OFFER_SREDNIA = 0;
$this->suma_P_OFFER_SREDNIA_NETTO = 0;
$this->suma_P_OFFER_COSTS = 0;
}
function set_from_raw_data($db_row) {
foreach ($db_row as $k_field => $v_value) {
$this->_data[$k_field] = $v_value;
}
}
function has_offers() {
return !empty($this->_offers_by_id);
}
function get_offers() {
return $this->_offers;
}
function get_offers_by_id() {
return $this->_offers_by_id;
}
function set_offers($offers) {
$this->_offers_by_id = $offers;
$this->recount_costs();
$this->_convert_offers();
}
function set_id_options($id_options) {
$this->_id_options = $id_options;
}
function get_offers_ids() {
return array_keys($this->_offers_by_id);
}
function fetch_offers() {
$offers = array();
foreach ($this->_group_external_offers_id as $k_external_id => $v_p_service) {
if ($this->get($k_external_id) > 0) {
$offers[$this->get($k_external_id)] = null;
}
}
foreach ($this->_id_options as $k_service => $v_options) {
if (empty($v_options)) continue;
foreach ($v_options as $v_option_id) {
if ($v_option_id > 0) {
$offers[$v_option_id] = null;
}
}
}
//$offers = PokazOfertyHelper::get_offers_by_id(array_keys($offers), array('P_DEALTERM_MONTHS'=>$this->get('P_DEALTERM_MONTHS')));
$this->_offers_by_id = PokazOfertyHelper::get_offers_by_id(array_keys($offers), array('P_DEALTERM_MONTHS'=>$this->get('P_DEALTERM_MONTHS')));
$this->recount_costs();
$this->_convert_offers();
}
/**
* Fetch offers by given ids and set _id_options.
* @param array $ids_offers - array of int - offers id
*/
function fetch_offers_by_ids($ids_offers) {
$offers = array();
$options = array();
foreach ($this->_group_external_offers_id as $k_external_id => $v_p_service) {
if ($this->get($k_external_id) > 0) {
$offers[$this->get($k_external_id)] = null;
}
}
foreach ($ids_offers as $v_offer_id) {
if (!array_key_exists($v_offer_id, $offers)) {
$offers[$v_offer_id] = null;
$options []= $v_offer_id;
}
}
$this->_offers_by_id = PokazOfertyHelper::get_offers_by_id(array_keys($offers), array('P_DEALTERM_MONTHS'=>$this->get('P_DEALTERM_MONTHS')));
$this->recount_costs();
foreach ($this->_offers_by_id as $v_offer) {
if (in_array($v_offer->ID, $options)) {
$this->_id_options[$v_offer->P_SERVICE_PARENT] []= $v_offer->ID;
}
}
$this->_convert_offers();
}
function get_options_by_service($p_service) {
if (array_key_exists($p_service, $this->_id_options)) {
$options = array();
foreach ($this->_id_options[$p_service] as $v_id_option) {
$option = V::get($v_id_option, '', $this->_offers_by_id);
if ($option) {
$options [$v_id_option] = $option;
}
}
return $options;
}
return null;
}
/**
* Convert $this->_offers_by_id to $this->_offers.
* Note: first run fetch_offers() or set_offers($offers) to set up $this->_offers;
*/
function _convert_offers() {
$tmp_srv_id = array();// 'NET'=>1, 'TV'=>0, 'TEL'=>0, 'OPCJA_NET'=>2, 'OPCJA_TV'=>2
foreach ($this->_group_external_offers_id as $k_external_id => $v_p_service) {
if ($this->get($k_external_id) > 0) {
$v_offer = $this->_offers_by_id[$this->get($k_external_id)];
if ($v_offer) {
$v_offer->out_id = $v_p_service;
// wiele uslug tego samego typu np. TVC
$tmp_srv_id[$v_offer->out_id] = (!array_key_exists($v_offer->out_id, $tmp_srv_id))? 1 : $tmp_srv_id[$v_offer->out_id] + 1;
$v_offer->out_id = $v_offer->out_id . '_' . $tmp_srv_id[$v_offer->out_id];
$this->_offers [$v_offer->out_id] [$v_offer->ID] = $v_offer;
if (!empty($this->_id_options[$v_p_service])) {
foreach ($this->_id_options[$v_p_service] as $v_option_id) {
if ($v_option_id > 0) {
$v_option = V::get($v_option_id, null, $this->_offers);
if ($v_option) {
$v_option->out_id = 'OPCJA_' . $v_option->P_SERVICE_PARENT;
$tmp_srv_id[$v_option->out_id] = (!array_key_exists($v_option->out_id, $tmp_srv_id))? 1 : $tmp_srv_id[$v_option->out_id] + 1;
$v_option->out_id = $v_option->out_id . '_' . $tmp_srv_id[$v_option->out_id];
$this->_offers [$v_option->out_id] [$v_option->ID] = $v_option;
}
}
}
}
}
}
}
unset($tmp_srv_id);
}
function recount_costs() {
$this->suma_P_PROMO_INSTALL_PRICE = 0;
$this->suma_P_PROMO_MONTH_PRICE = 0;
$this->suma_P_CONTRACT_PRICE = 0;
$this->suma_P_OFFER_SREDNIA = 0;
$this->suma_P_OFFER_SREDNIA_NETTO = 0;
$this->suma_P_OFFER_COSTS = 0;
$oferty_costs = Offers_Groups_Model::get_offers_costs_by_offer_ids(array_keys($this->_offers_by_id));
foreach ($this->_offers_by_id as $v_offer) {
$this->suma_P_PROMO_INSTALL_PRICE += $v_offer->P_INSTALL_QUOTE;
$this->suma_P_PROMO_MONTH_PRICE += $v_offer->P_PROMO_MONTH_PRICE;
$this->suma_P_CONTRACT_PRICE += $v_offer->P_CONTRACT_PRICE;
if (!isset($v_offer->srednia)) {
$v_offer->srednia = round(( (($this->get('P_DEALTERM_MONTHS') - $v_offer->P_DEAL_PROMO_MONTHS) * $v_offer->P_CONTRACT_PRICE) + ($v_offer->P_DEAL_PROMO_MONTHS * $v_offer->P_PROMO_MONTH_PRICE) + ($v_offer->P_INSTALL_QUOTE)) / $this->get('P_DEALTERM_MONTHS'), 2);
}
$this->suma_P_OFFER_SREDNIA += $v_offer->srednia;
if (!isset($v_offer->srednia_netto)) {
$v_offer->srednia_netto = round(( (($this->get('P_DEALTERM_MONTHS') - $v_offer->P_DEAL_PROMO_MONTHS) * $v_offer->P_CONTRACT_PRICE_NETTO) + ($v_offer->P_DEAL_PROMO_MONTHS * $v_offer->P_PROMO_MONTH_PRICE_NETTO) + ($v_offer->P_INSTALL_QUOTE/1.23)) / $this->get('P_DEALTERM_MONTHS'), 2);
}
$this->suma_P_OFFER_SREDNIA_NETTO += $v_offer->srednia_netto;
if (isset($oferty_costs[$v_offer->ID])) {
$this->suma_P_OFFER_COSTS += $oferty_costs[$v_offer->ID];
}
}
}
function get_zysk_netto() {
$this->recount_costs();
return $this->suma_P_OFFER_SREDNIA_NETTO - $this->suma_P_OFFER_COSTS;
}
function has_service($p_service) {
foreach ($this->_group_external_offers_id as $k_external_id => $v_p_service) {
if ($v_p_service == $p_service) {
if ($this->get($k_external_id) > 0) {
return true;
}
}
}
return false;
}
}