Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- // Client services model
- class Services_model extends LK_Model {
- public function __construct() {
- parent::__construct();
- $this->loaddb('request', 'r_db');
- $this->load->config('dict_request');
- }
- public function get_client_services($user_id, $ulogin, $blogin) {
- $result = [
- 'totalPrice' => 0,
- 'serviceGroups' => [
- 'inet_tvma' => false,
- 'tvls' => false,
- ]
- ];
- ///////// Get inet
- // Get basic tariff
- $this->r_db->select('
- r.tariff AS legacy_tariff,
- r.numtariff AS legacy_tariff_price,
- r.ulogin,
- r.dlogin,
- r.blogin,
- t.name AS tariff_name,
- t.price AS tariff_price,
- t.lim_type AS tariff_limited,
- t.bitrate AS tariff_bitrate,
- t.archive AS tariff_archive,
- t.ip_type AS tariff_extip,
- t.router_class AS tariff_router_class,
- t.router_price AS tariff_router_price
- ');
- $this->r_db->from('requestbase AS r');
- $this->r_db->join('tarifftable AS t', 't.tariff = SUBSTRING_INDEX(r.tariff, \'+\', 1)', 'LEFT OUTER', false);
- $this->r_db->where('r.id', $user_id);
- if (!sizeof($r = $this->r_db->get()->result_array())) return false;
- $r = $r[0];
- if (isset($r['tariff_name'])) {
- // If found tariff in tarifftable
- $tariff_name = $r['tariff_name']; // Name
- if ($r['tariff_archive']) $tariff_name .= ' (архивный)';
- $tariff_price = $r['tariff_price']; // Price
- $tariff_price_inet_only = true;
- if (!$r['tariff_limited']) {
- $tariff_desc = $r['tariff_bitrate'].' Мбит/с'; // Desc (bitrate)
- } else {
- $tariff_desc = '';
- }
- } else {
- // If not found tariff in tarifftable
- $tariff_name = $r['legacy_tariff'];
- $tariff_price = $r['legacy_tariff_price'];
- $tariff_price_inet_only = false;
- $tariff_desc = '';
- }
- $inetTvMaFree = ($tariff_price == 0);
- // Check if price is numeric
- if (!is_numeric($tariff_price)) throw new LK_Exception('Inet tariff price is not numeric for client id '.$user_id.': "'.$tariff_price.'"');
- // Save basic tariff
- $result['serviceGroups']['inet_tvma'] = [
- 'statsServiceId' => SERVICEID_INET,
- 'totalPrice' => 0,
- 'label' => 'Интернет, Цифровое ТВ, Оборудование',
- 'services' => [
- 'inet' => NULL, // For correct ordering
- 'rent' => [
- 'label' => 'Аренда оборудования',
- 'subservices' => []
- ]
- ]
- ];
- $result['totalPrice'] += $tariff_price;
- $result['serviceGroups']['inet_tvma']['totalPrice'] += $tariff_price;
- $result['serviceGroups']['inet_tvma']['ulogin'] = $r['ulogin'];
- $result['serviceGroups']['inet_tvma']['dlogin'] = $r['dlogin'];
- $result['serviceGroups']['inet_tvma']['blogin'] = $r['blogin'];
- $result['serviceGroups']['inet_tvma']['services']['inet'] = [
- 'label' => 'Интернет',
- 'subservices' => [
- 'tariff' => [
- 'type' => 'inet',
- 'billingType' => 'stoppable',
- 'service' => SERVICEID_INET,
- 'label' => $tariff_name,
- 'price' => $tariff_price,
- 'desc' => $tariff_desc
- ]
- ]
- ];
- // Add static ip
- if ($r['tariff_extip']) { // Non-null (joined table) and true (1)
- if (!$inetTvMaFree) $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= EXT_IP_PRICE; // decrement inet tariff price
- if ($result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] < 0) $this->ci->w('Inet price is < 0 after decrementing on EXT_IP_PRICE');
- $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['extIp'] = [
- 'type' => 'extIp',
- 'billingType' => 'stoppable',
- 'service' => SERVICEID_INET,
- 'label' => 'Внешний IP-адрес',
- 'price' => ($inetTvMaFree)? 0: EXT_IP_PRICE,
- 'desc' => ''
- ];
- }
- // Add router
- if ($r['tariff_router_class'] &&
- ($router_price = $this->dict_translate('router_price', $r['tariff_router_price'], false, NULL)) !== NULL) { // Non-null (joined table), true (1), found router_price in dict
- if (!$inetTvMaFree) $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= $router_price; // decrement inet tariff price
- if ($result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] < 0) $this->ci->w('Inet price is < 0 after decrementing on router price');
- $router_class = $this->dict_translate('router_class', $r['tariff_router_class']);
- $result['serviceGroups']['inet_tvma']['services']['rent']['subservices']['router'] = [
- 'type' => 'router',
- 'billingType' => 'unstoppable',
- 'service' => SERVICEID_INET,
- 'label' => $router_class.' роутер',
- 'price' => ($inetTvMaFree)? 0 :$router_price,
- 'desc' => ''
- ];
- }
- ///////// Get TV MA
- $this->r_db->select('
- i.id,
- i.stbflag,
- tm.name AS main_pack_name,
- tm.total AS main_pack_price,
- ta.name AS add_pack_name,
- ta.total AS add_pack_price
- ');
- $this->r_db->from('iptvnew AS i');
- $this->r_db->join('teletar AS tm', 'i.pack = tm.num AND tm.type = \'m\' AND (tm.packid < 400 OR tm.packid > 499)', NULL, false);
- $this->r_db->join('teletar AS ta', 'i.addpack & ta.num AND ta.type = \'a\' AND (ta.packid < 400 OR ta.packid > 499)', 'LEFT OUTER', false);
- $this->r_db->where('i.request', $user_id);
- if (sizeof($res = $this->r_db->get()->result_array())) {
- // Normalize data to an array of [ id => [ mainpack, addpacks[] ] ] ]
- $packs = [];
- foreach ($res as $i => $row) {
- if (!isset($packs[$row['id']])) { // Push new mainpack for id and add STB if stbflag = 1 (STB rental)
- $packs[$row['id']] = [ // mainpack
- 'mainpack' => [
- 'type' => 'mainpack',
- 'billingType' => 'stoppable',
- 'service' => SERVICEID_TV_MA,
- 'label' => $row['main_pack_name'],
- 'price' => $row['main_pack_price'],
- 'desc' => ''
- ],
- 'stb' => false,
- 'addpacks' => []
- ];
- if ($row['stbflag'] == 1) { // stb is rented
- $result['serviceGroups']['inet_tvma']['services']['rent']['subservices']['stb'.($i+1)] = [
- 'type' => 'stb',
- 'billingType' => 'unstoppable',
- 'service' => SERVICEID_TV_MA,
- 'label' => 'ТВ-приставка'.((sizeof($res) > 1)? ' '.($i+1) : ''),
- 'price' => ($inetTvMaFree)? 0 : STB_RENT_PRICE,
- 'desc' => ''
- ];
- // Increment full total cost or decrement inet cost depending on tariff count style
- if ($tariff_price_inet_only) {
- // Only inet in tariff price (new style): increment total
- if (!$inetTvMaFree) {
- $result['totalPrice'] += STB_RENT_PRICE;
- $result['serviceGroups']['inet_tvma']['totalPrice'] += STB_RENT_PRICE;
- }
- } else {
- // Inet + tv in tariff price (old style): decrement inet price
- if (!$inetTvMaFree) $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= STB_RENT_PRICE; // decrement inet tariff price
- if ($result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] < 0) $this->ci->w('Inet price is < 0 after decrementing MediaAlliance '.$id.' STB_RENT_PRICE (old style tariff record)');
- }
- }
- }
- if (isset($row['add_pack_name'])) { // If additional pack joined
- $packs[$row['id']]['addpacks'][] = [
- 'type' => 'addpack',
- 'billingType' => 'stoppable',
- 'service' => SERVICEID_TV_MA,
- 'label' => $row['add_pack_name'],
- 'price' => $row['add_pack_price'],
- 'desc' => ''
- ];
- }
- }
- // Parse normalized data into array of iptv services
- $result['serviceGroups']['inet_tvma']['services']['ma'] = [
- 'label' => 'Цифровое ТВ',
- 'subservices' => []
- ];
- $serviceNum = 0;
- $packsNum = sizeof($packs);
- foreach ($packs as $id => $pack) {
- $serviceNum++;
- // If packs num is > 1 - add header
- if ($packsNum > 1) {
- $result['serviceGroups']['inet_tvma']['services']['ma']['subservices']['header'.$serviceNum] = [
- 'type' => 'header',
- 'label' => 'Цифровое ТВ - Пакет '.$serviceNum
- ];
- }
- if ($inetTvMaFree) $pack['mainpack']['price'] = 0;
- // Increment full total cost or decrement inet cost depending on tariff count style
- if ($tariff_price_inet_only) {
- // Only inet in tariff price (new style): increment total
- $result['totalPrice'] += $pack['mainpack']['price'];
- $result['serviceGroups']['inet_tvma']['totalPrice'] += $pack['mainpack']['price'];
- } else {
- // Inet + tv in tariff price (old style): decrement inet price
- $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= $pack['mainpack']['price']; // decrement inet tariff price
- if ($result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] < 0) $this->ci->w('Inet price is < 0 after decrementing MediaAlliance mainpack '.$id.' (old style tariff record)');
- }
- // Push mainpack
- $result['serviceGroups']['inet_tvma']['services']['ma']['subservices']['mainpack'.$serviceNum] = $pack['mainpack'];
- // Push addpacks
- $subserviceNum = 0;
- foreach ($pack['addpacks'] as $addpack) {
- $subserviceNum += 1;
- if ($inetTvMaFree) $addpack['price'] = 0;
- // Increment full total cost or decrement inet cost depending on tariff count style
- if ($tariff_price_inet_only) {
- // Only inet in tariff price (new style): increment total
- $result['totalPrice'] += $addpack['price'];
- $result['serviceGroups']['inet_tvma']['totalPrice'] += $addpack['price'];
- } else {
- // Inet + tv in tariff price (old style): decrement inet price
- $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= $addpack['price']; // decrement inet tariff price
- if ($result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] < 0) $this->ci->w('Inet price is < 0 after decrementing MediaAlliance '.$id.' addpack '.$subserviceNum.' (old style tariff record)');
- }
- // Push addpack
- $result['serviceGroups']['inet_tvma']['services']['ma']['subservices']['addpack'.$subserviceNum] = $addpack;
- }
- }
- }
- // Compile inet_tvma label
- $parts = [];
- $labeldict = ['inet' => 'Интернет', 'ma' => 'Цифровое ТВ', 'rent' => 'Оборудование'];
- foreach ($labeldict as $field => $label) {
- if (isset($result['serviceGroups']['inet_tvma']['services'][$field])) {
- if (sizeof($result['serviceGroups']['inet_tvma']['services'][$field]['subservices'])) {
- $parts[] = $label;
- }
- }
- }
- if (sizeof($parts) == 2) {
- $result['serviceGroups']['inet_tvma']['label'] = implode(' и ', $parts);
- } else {
- $result['serviceGroups']['inet_tvma']['label'] = implode(', ', $parts);
- }
- ///////// Get TV LS
- $this->r_db->select('
- t.ulogin,
- t.dlogin,
- i.id,
- i.stbflag,
- i.ipflag,
- tm.name AS main_pack_name,
- tm.total AS main_pack_price,
- ta.name AS add_pack_name,
- ta.total AS add_pack_price
- ');
- $this->r_db->from('iptvls AS i');
- $this->r_db->join('tvls AS t', 't.id = i.request', 'LEFT OUTER');
- $this->r_db->join('teletarls AS tm', 'i.pack = tm.num AND tm.type = \'m\' AND (tm.packid < 400 OR tm.packid > 499)', NULL, false);
- $this->r_db->join('teletarls AS ta', 'i.addpack & ta.num AND ta.type = \'a\' AND (ta.packid < 400 OR ta.packid > 499)', 'LEFT OUTER', false);
- $this->r_db->where('i.request', $user_id);
- $res = $this->r_db->get()->result_array();
- if (sizeof($res)) {
- // Get primary data
- $result['serviceGroups']['tvls'] = [
- 'label' => 'Интерактивное ТВ - сервис «СмотрЁшка»',
- 'ulogin' => $res[0]['ulogin'],
- 'dlogin' => $res[0]['dlogin'],
- 'blogin' => $res[0]['ulogin'],
- 'statsServiceId' => SERVICEID_TV_LS,
- 'totalPrice' => 0,
- 'services' => [
- 'tvls' => [
- 'label' => 'Интерактивное ТВ',
- 'subservices' => []
- ]
- ]
- ];
- // Normalize data to an array of [ id => [ mainpack, addpacks[] ] ] ]
- $packs = [];
- $stbn = 0;
- $multistb = false;
- $i = 0;
- foreach ($res as $row) {
- if ($row['stbflag'] == 1) $i += $row['ipflag'];
- if ($i > 1) {
- $multistb = true;
- break;
- }
- }
- foreach ($res as $i => $row) {
- if (!isset($packs[$row['id']])) { // Push new mainpack for id and add STB if stbflag = 1 (STB rental)
- // Push packs
- $packs[$row['id']] = [ // mainpack
- 'mainpack' => [
- 'type' => 'mainpack',
- 'billingType' => 'stoppable',
- 'service' => SERVICEID_TV_LS,
- 'label' => $row['main_pack_name'],
- 'price' => $row['main_pack_price'],
- 'desc' => ''
- ],
- 'stb' => false,
- 'addpacks' => []
- ];
- if ($row['stbflag'] == 1) { // stb is rented
- if ($row['ipflag'] == 0) $row['ipflag'] = 1;
- for ($n = 0; $n < $row['ipflag']; $n++) { // foreach rented stb: push them to inet
- // Push stb
- $result['serviceGroups']['inet_tvma']['services']['rent']['subservices']['stbls'.($stbn++)] = [
- 'type' => 'stb',
- 'billingType' => 'unstoppable',
- 'service' => SERVICEID_TV_LS,
- 'label' => 'ТВ-приставка для Интерактивного ТВ'.($multistb? ' '.$stbn : ''),
- 'price' => ($inetTvMaFree)? 0 : STB_RENT_PRICE,
- 'desc' => ''
- ];
- // Increment full total cost or decrement inet cost depending on tariff count style
- if ($tariff_price_inet_only) {
- // Only inet in tariff price (new style): increment total
- if (!$inetTvMaFree) {
- $result['totalPrice'] += STB_RENT_PRICE;
- $result['serviceGroups']['inet_tvma']['totalPrice'] += STB_RENT_PRICE;
- }
- } else {
- // Inet + tv in tariff price (old style): decrement inet price
- if (!$inetTvMaFree) $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= STB_RENT_PRICE; // decrement inet tariff price
- if ($result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] < 0) $this->ci->w('Inet price is < 0 after decrementing Lifestream '.$row['id'].' STB_RENT_PRICE (old style tariff record)');
- }
- }
- }
- }
- if (isset($row['add_pack_name'])) { // If additional pack joined
- $packs[$row['id']]['addpacks'][] = [
- 'type' => 'addpack',
- 'billingType' => 'stoppable',
- 'service' => SERVICEID_TV_LS,
- 'label' => $row['add_pack_name'],
- 'price' => $row['add_pack_price'],
- 'desc' => ''
- ];
- }
- }
- // Parse normalized data into array of iptv services
- $serviceNum = 0;
- $packsNum = sizeof($packs);
- foreach ($packs as $id => $pack) {
- $serviceNum++;
- // If packs num is > 1 - add header
- if ($packsNum > 1) {
- $result['serviceGroups']['tvls']['services']['tvls']['subservices']['header'.$serviceNum] = [
- 'type' => 'header',
- 'label' => 'Интерактивное ТВ - Пакет '.$serviceNum
- ];
- }
- // Push mainpack
- $result['totalPrice'] += $pack['mainpack']['price']; // Price
- $result['serviceGroups']['tvls']['totalPrice'] += $pack['mainpack']['price'];
- $result['serviceGroups']['tvls']['services']['tvls']['subservices']['mainpack'.$serviceNum] = $pack['mainpack']; // Pack
- // Push addpacks
- $subserviceNum = 0;
- foreach ($pack['addpacks'] as $addpack) {
- $subserviceNum += 1;
- $result['totalPrice'] += $addpack['price'];
- $result['serviceGroups']['tvls']['totalPrice'] += $addpack['price'];
- $result['serviceGroups']['tvls']['services']['tvls']['subservices']['addpack'.$subserviceNum] = $addpack;
- }
- }
- } else {
- if ($this->auth->ltype == 1) {
- $result['serviceGroups']['tvls'] = [
- 'label' => 'Интерактивное ТВ - сервис «СмотрЁшка»',
- 'replace' => true
- ];
- }
- }
- return $result;
- }
- // Get service names for client
- public function get_service_name_and_login($user_id, $service_id) {
- if ($service_id == 1) { // Internet + MA
- $this->r_db->select('ulogin, dlogin');
- $this->r_db->from('requestbase');
- $this->r_db->where('id', $user_id);
- if (!sizeof($r = $this->r_db->get()->result_array())) {
- throw new LK_Exception('User '.$user_id.' not found in client db');
- } else {
- $ulogin = $r[0]['ulogin'];
- $dlogin = $r[0]['dlogin'];
- }
- $this->r_db->select('1', false);
- $this->r_db->from('iptvnew');
- $this->r_db->where('request', $user_id);
- if (sizeof($this->r_db->get()->result_array())) {
- $service_name = 'Интернет и Цифровое ТВ';
- } else {
- $service_name = 'Интернет';
- }
- return ['service_name' => $service_name, 'ulogin' => $ulogin, 'dlogin' => $dlogin];
- } elseif ($service_id == 21) { // LS
- $this->r_db->select('ulogin, dlogin');
- $this->r_db->from('tvls');
- $this->r_db->where('id', $user_id);
- if (!sizeof($r = $this->r_db->get()->result_array())) return false;
- return ['service_name' => 'Интерактивное ТВ - сервис «СмотрЁшка»', 'ulogin' => $r[0]['ulogin'], 'dlogin' => $r[0]['dlogin']];
- } else {
- return false;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment