dkanavis

services model

Feb 20th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.21 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. // Client services model
  5. class Services_model extends LK_Model {
  6.     public function __construct() {
  7.         parent::__construct();
  8.  
  9.         $this->loaddb('request', 'r_db');
  10.         $this->load->config('dict_request');
  11.     }
  12.  
  13.     public function get_client_services($user_id, $ulogin, $blogin) {
  14.         $result = [
  15.             'totalPrice' => 0,
  16.             'serviceGroups' => [
  17.                 'inet_tvma' => false,
  18.                 'tvls' => false,
  19.             ]
  20.         ];
  21.  
  22.         ///////// Get inet
  23.         // Get basic tariff
  24.         $this->r_db->select('
  25.             r.tariff AS legacy_tariff,
  26.             r.numtariff AS legacy_tariff_price,
  27.             r.ulogin,
  28.             r.dlogin,
  29.             r.blogin,
  30.             t.name AS tariff_name,
  31.             t.price AS tariff_price,
  32.             t.lim_type AS tariff_limited,
  33.             t.bitrate AS tariff_bitrate,
  34.             t.archive AS tariff_archive,
  35.             t.ip_type AS tariff_extip,
  36.             t.router_class AS tariff_router_class,
  37.             t.router_price AS tariff_router_price
  38.         ');
  39.         $this->r_db->from('requestbase AS r');
  40.         $this->r_db->join('tarifftable AS t', 't.tariff = SUBSTRING_INDEX(r.tariff, \'+\', 1)', 'LEFT OUTER', false);
  41.         $this->r_db->where('r.id', $user_id);
  42.         if (!sizeof($r = $this->r_db->get()->result_array())) return false;
  43.         $r = $r[0];
  44.  
  45.         if (isset($r['tariff_name'])) {
  46.             // If found tariff in tarifftable
  47.             $tariff_name = $r['tariff_name']; // Name
  48.             if ($r['tariff_archive']) $tariff_name .= ' (архивный)';
  49.  
  50.             $tariff_price = $r['tariff_price']; // Price
  51.             $tariff_price_inet_only = true;
  52.  
  53.             if (!$r['tariff_limited']) {
  54.                 $tariff_desc = $r['tariff_bitrate'].' Мбит/с'; // Desc (bitrate)
  55.             } else {
  56.                 $tariff_desc = '';
  57.             }
  58.         } else {
  59.             // If not found tariff in tarifftable
  60.             $tariff_name = $r['legacy_tariff'];
  61.             $tariff_price = $r['legacy_tariff_price'];
  62.             $tariff_price_inet_only = false;
  63.             $tariff_desc = '';
  64.         }
  65.         $inetTvMaFree = ($tariff_price == 0);
  66.  
  67.         // Check if price is numeric
  68.         if (!is_numeric($tariff_price)) throw new LK_Exception('Inet tariff price is not numeric for client id '.$user_id.': "'.$tariff_price.'"');
  69.  
  70.         // Save basic tariff
  71.         $result['serviceGroups']['inet_tvma'] = [
  72.             'statsServiceId' => SERVICEID_INET,
  73.             'totalPrice' => 0,
  74.             'label' => 'Интернет, Цифровое ТВ, Оборудование',
  75.             'services' => [
  76.                 'inet' => NULL, // For correct ordering
  77.                 'rent' => [
  78.                     'label' => 'Аренда оборудования',
  79.                     'subservices' => []
  80.                 ]
  81.             ]
  82.         ];
  83.         $result['totalPrice'] += $tariff_price;
  84.         $result['serviceGroups']['inet_tvma']['totalPrice'] += $tariff_price;
  85.         $result['serviceGroups']['inet_tvma']['ulogin'] = $r['ulogin'];
  86.         $result['serviceGroups']['inet_tvma']['dlogin'] = $r['dlogin'];
  87.         $result['serviceGroups']['inet_tvma']['blogin'] = $r['blogin'];
  88.         $result['serviceGroups']['inet_tvma']['services']['inet'] = [
  89.             'label' => 'Интернет',
  90.             'subservices' => [
  91.                 'tariff' => [
  92.                     'type' => 'inet',
  93.                     'billingType' => 'stoppable',
  94.                     'service' => SERVICEID_INET,
  95.                     'label' => $tariff_name,
  96.                     'price' => $tariff_price,
  97.                     'desc' => $tariff_desc
  98.                 ]
  99.             ]
  100.         ];
  101.  
  102.         // Add static ip
  103.         if ($r['tariff_extip']) { // Non-null (joined table) and true (1)
  104.             if (!$inetTvMaFree) $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= EXT_IP_PRICE; // decrement inet tariff price
  105.             if ($result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] < 0) $this->ci->w('Inet price is < 0 after decrementing on EXT_IP_PRICE');
  106.             $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['extIp'] = [
  107.                 'type' => 'extIp',
  108.                 'billingType' => 'stoppable',
  109.                 'service' => SERVICEID_INET,
  110.                 'label' => 'Внешний IP-адрес',
  111.                 'price' => ($inetTvMaFree)? 0: EXT_IP_PRICE,
  112.                 'desc' => ''
  113.             ];
  114.         }
  115.  
  116.         // Add router
  117.         if ($r['tariff_router_class'] &&
  118.             ($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
  119.             if (!$inetTvMaFree) $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= $router_price; // decrement inet tariff price
  120.             if ($result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] < 0) $this->ci->w('Inet price is < 0 after decrementing on router price');
  121.             $router_class = $this->dict_translate('router_class', $r['tariff_router_class']);
  122.             $result['serviceGroups']['inet_tvma']['services']['rent']['subservices']['router'] = [
  123.                 'type' => 'router',
  124.                 'billingType' => 'unstoppable',
  125.                 'service' => SERVICEID_INET,
  126.                 'label' => $router_class.' роутер',
  127.                 'price' => ($inetTvMaFree)? 0 :$router_price,
  128.                 'desc' => ''
  129.             ];
  130.         }
  131.  
  132.         ///////// Get TV MA
  133.         $this->r_db->select('
  134.             i.id,
  135.             i.stbflag,
  136.             tm.name AS main_pack_name,
  137.             tm.total AS main_pack_price,
  138.             ta.name AS add_pack_name,
  139.             ta.total AS add_pack_price
  140.         ');
  141.         $this->r_db->from('iptvnew AS i');
  142.         $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);
  143.         $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);
  144.         $this->r_db->where('i.request', $user_id);
  145.         if (sizeof($res = $this->r_db->get()->result_array())) {
  146.             // Normalize data to an array of [ id => [ mainpack, addpacks[] ] ] ]
  147.             $packs = [];
  148.             foreach ($res as $i => $row) {
  149.                 if (!isset($packs[$row['id']])) { // Push new mainpack for id and add STB if stbflag = 1 (STB rental)
  150.                     $packs[$row['id']] = [ // mainpack
  151.                         'mainpack' => [
  152.                             'type' => 'mainpack',
  153.                             'billingType' => 'stoppable',
  154.                             'service' => SERVICEID_TV_MA,
  155.                             'label' => $row['main_pack_name'],
  156.                             'price' => $row['main_pack_price'],
  157.                             'desc' => ''
  158.                         ],
  159.                         'stb' => false,
  160.                         'addpacks' => []
  161.                     ];
  162.                     if ($row['stbflag'] == 1) { // stb is rented
  163.                         $result['serviceGroups']['inet_tvma']['services']['rent']['subservices']['stb'.($i+1)] = [
  164.                             'type' => 'stb',
  165.                             'billingType' => 'unstoppable',
  166.                             'service' => SERVICEID_TV_MA,
  167.                             'label' => 'ТВ-приставка'.((sizeof($res) > 1)? ' '.($i+1) : ''),
  168.                             'price' => ($inetTvMaFree)? 0 : STB_RENT_PRICE,
  169.                             'desc' => ''
  170.                         ];
  171.                         // Increment full total cost or decrement inet cost depending on tariff count style
  172.                         if ($tariff_price_inet_only) {
  173.                             // Only inet in tariff price (new style): increment total
  174.                             if (!$inetTvMaFree) {
  175.                                 $result['totalPrice'] += STB_RENT_PRICE;
  176.                                 $result['serviceGroups']['inet_tvma']['totalPrice'] += STB_RENT_PRICE;
  177.                             }
  178.                         } else {
  179.                             // Inet + tv in tariff price (old style): decrement inet price
  180.                             if (!$inetTvMaFree) $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= STB_RENT_PRICE; // decrement inet tariff price
  181.                             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)');
  182.                         }
  183.                     }
  184.                 }
  185.                 if (isset($row['add_pack_name'])) { // If additional pack joined
  186.                     $packs[$row['id']]['addpacks'][] = [
  187.                         'type' => 'addpack',
  188.                         'billingType' => 'stoppable',
  189.                         'service' => SERVICEID_TV_MA,
  190.                         'label' => $row['add_pack_name'],
  191.                         'price' => $row['add_pack_price'],
  192.                         'desc' => ''
  193.                     ];
  194.                 }
  195.             }
  196.  
  197.             // Parse normalized data into array of iptv services
  198.             $result['serviceGroups']['inet_tvma']['services']['ma'] = [
  199.                 'label' => 'Цифровое ТВ',
  200.                 'subservices' => []
  201.             ];
  202.             $serviceNum = 0;
  203.             $packsNum = sizeof($packs);
  204.             foreach ($packs as $id => $pack) {
  205.                 $serviceNum++;
  206.                 // If packs num is > 1 - add header
  207.                 if ($packsNum > 1) {
  208.                     $result['serviceGroups']['inet_tvma']['services']['ma']['subservices']['header'.$serviceNum] = [
  209.                         'type' => 'header',
  210.                         'label' => 'Цифровое ТВ - Пакет '.$serviceNum
  211.                     ];
  212.                 }
  213.                 if ($inetTvMaFree) $pack['mainpack']['price'] = 0;
  214.                 // Increment full total cost or decrement inet cost depending on tariff count style
  215.                 if ($tariff_price_inet_only) {
  216.                     // Only inet in tariff price (new style): increment total
  217.                     $result['totalPrice'] += $pack['mainpack']['price'];
  218.                     $result['serviceGroups']['inet_tvma']['totalPrice'] += $pack['mainpack']['price'];
  219.                 } else {
  220.                     // Inet + tv in tariff price (old style): decrement inet price
  221.                     $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= $pack['mainpack']['price']; // decrement inet tariff price
  222.                     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)');
  223.                 }
  224.  
  225.                 // Push mainpack
  226.                 $result['serviceGroups']['inet_tvma']['services']['ma']['subservices']['mainpack'.$serviceNum] = $pack['mainpack'];
  227.                 // Push addpacks
  228.                 $subserviceNum = 0;
  229.                 foreach ($pack['addpacks'] as $addpack) {
  230.                     $subserviceNum += 1;
  231.                     if ($inetTvMaFree) $addpack['price'] = 0;
  232.                     // Increment full total cost or decrement inet cost depending on tariff count style
  233.                     if ($tariff_price_inet_only) {
  234.                         // Only inet in tariff price (new style): increment total
  235.                         $result['totalPrice'] += $addpack['price'];
  236.                         $result['serviceGroups']['inet_tvma']['totalPrice'] += $addpack['price'];
  237.                     } else {
  238.                         // Inet + tv in tariff price (old style): decrement inet price
  239.                         $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= $addpack['price']; // decrement inet tariff price
  240.                         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)');
  241.                     }
  242.                     // Push addpack
  243.                     $result['serviceGroups']['inet_tvma']['services']['ma']['subservices']['addpack'.$subserviceNum] = $addpack;
  244.                 }
  245.             }
  246.  
  247.         }
  248.  
  249.         // Compile inet_tvma label
  250.         $parts = [];
  251.         $labeldict = ['inet' => 'Интернет', 'ma' => 'Цифровое ТВ', 'rent' => 'Оборудование'];
  252.         foreach ($labeldict as $field => $label) {
  253.             if (isset($result['serviceGroups']['inet_tvma']['services'][$field])) {
  254.                 if (sizeof($result['serviceGroups']['inet_tvma']['services'][$field]['subservices'])) {
  255.                     $parts[] = $label;
  256.                 }
  257.             }
  258.         }
  259.         if (sizeof($parts) == 2) {
  260.             $result['serviceGroups']['inet_tvma']['label'] = implode(' и ', $parts);
  261.         } else {
  262.             $result['serviceGroups']['inet_tvma']['label'] = implode(', ', $parts);
  263.         }
  264.  
  265.  
  266.         ///////// Get TV LS
  267.         $this->r_db->select('
  268.             t.ulogin,
  269.             t.dlogin,
  270.             i.id,
  271.             i.stbflag,
  272.             i.ipflag,
  273.             tm.name AS main_pack_name,
  274.             tm.total AS main_pack_price,
  275.             ta.name AS add_pack_name,
  276.             ta.total AS add_pack_price
  277.         ');
  278.         $this->r_db->from('iptvls AS i');
  279.         $this->r_db->join('tvls AS t', 't.id = i.request', 'LEFT OUTER');
  280.         $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);
  281.         $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);
  282.         $this->r_db->where('i.request', $user_id);
  283.         $res = $this->r_db->get()->result_array();
  284.  
  285.         if (sizeof($res)) {
  286.             // Get primary data
  287.             $result['serviceGroups']['tvls'] = [
  288.                 'label' => 'Интерактивное ТВ - сервис «СмотрЁшка»',
  289.                 'ulogin' => $res[0]['ulogin'],
  290.                 'dlogin' => $res[0]['dlogin'],
  291.                 'blogin' => $res[0]['ulogin'],
  292.                 'statsServiceId' => SERVICEID_TV_LS,
  293.                 'totalPrice' => 0,
  294.                 'services' => [
  295.                     'tvls' => [
  296.                         'label' => 'Интерактивное ТВ',
  297.                         'subservices' => []
  298.                     ]
  299.                 ]
  300.             ];
  301.             // Normalize data to an array of [ id => [ mainpack, addpacks[] ] ] ]
  302.             $packs = [];
  303.             $stbn = 0;
  304.             $multistb = false;
  305.             $i = 0;
  306.             foreach ($res as $row) {
  307.                 if ($row['stbflag'] == 1) $i += $row['ipflag'];
  308.                 if ($i > 1) {
  309.                     $multistb = true;
  310.                     break;
  311.                 }
  312.             }
  313.             foreach ($res as $i => $row) {
  314.                 if (!isset($packs[$row['id']])) { // Push new mainpack for id and add STB if stbflag = 1 (STB rental)
  315.                     // Push packs
  316.                     $packs[$row['id']] = [ // mainpack
  317.                         'mainpack' => [
  318.                             'type' => 'mainpack',
  319.                             'billingType' => 'stoppable',
  320.                             'service' => SERVICEID_TV_LS,
  321.                             'label' => $row['main_pack_name'],
  322.                             'price' => $row['main_pack_price'],
  323.                             'desc' => ''
  324.                         ],
  325.                         'stb' => false,
  326.                         'addpacks' => []
  327.                     ];
  328.                     if ($row['stbflag'] == 1) { // stb is rented
  329.                         if ($row['ipflag'] == 0) $row['ipflag'] = 1;
  330.                         for ($n = 0; $n < $row['ipflag']; $n++) { // foreach rented stb: push them to inet
  331.                             // Push stb
  332.                             $result['serviceGroups']['inet_tvma']['services']['rent']['subservices']['stbls'.($stbn++)] = [
  333.                                 'type' => 'stb',
  334.                                 'billingType' => 'unstoppable',
  335.                                 'service' => SERVICEID_TV_LS,
  336.                                 'label' => 'ТВ-приставка для Интерактивного ТВ'.($multistb? ' '.$stbn : ''),
  337.                                 'price' => ($inetTvMaFree)? 0 : STB_RENT_PRICE,
  338.                                 'desc' => ''
  339.                             ];
  340.                             // Increment full total cost or decrement inet cost depending on tariff count style
  341.                             if ($tariff_price_inet_only) {
  342.                                 // Only inet in tariff price (new style): increment total
  343.                                 if (!$inetTvMaFree) {
  344.                                     $result['totalPrice'] += STB_RENT_PRICE;
  345.                                     $result['serviceGroups']['inet_tvma']['totalPrice'] += STB_RENT_PRICE;
  346.                                 }
  347.                             } else {
  348.                                 // Inet + tv in tariff price (old style): decrement inet price
  349.                                 if (!$inetTvMaFree) $result['serviceGroups']['inet_tvma']['services']['inet']['subservices']['tariff']['price'] -= STB_RENT_PRICE; // decrement inet tariff price
  350.                                 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)');
  351.                             }
  352.                         }
  353.                     }
  354.                 }
  355.                 if (isset($row['add_pack_name'])) { // If additional pack joined
  356.                     $packs[$row['id']]['addpacks'][] = [
  357.                         'type' => 'addpack',
  358.                         'billingType' => 'stoppable',
  359.                         'service' => SERVICEID_TV_LS,
  360.                         'label' => $row['add_pack_name'],
  361.                         'price' => $row['add_pack_price'],
  362.                         'desc' => ''
  363.                     ];
  364.                 }
  365.             }
  366.  
  367.             // Parse normalized data into array of iptv services
  368.             $serviceNum = 0;
  369.             $packsNum = sizeof($packs);
  370.             foreach ($packs as $id => $pack) {
  371.                 $serviceNum++;
  372.  
  373.                 // If packs num is > 1 - add header
  374.                 if ($packsNum > 1) {
  375.                     $result['serviceGroups']['tvls']['services']['tvls']['subservices']['header'.$serviceNum] = [
  376.                         'type' => 'header',
  377.                         'label' => 'Интерактивное ТВ - Пакет '.$serviceNum
  378.                     ];
  379.                 }
  380.  
  381.                 // Push mainpack
  382.                 $result['totalPrice'] += $pack['mainpack']['price']; // Price
  383.                 $result['serviceGroups']['tvls']['totalPrice'] += $pack['mainpack']['price'];
  384.                 $result['serviceGroups']['tvls']['services']['tvls']['subservices']['mainpack'.$serviceNum] = $pack['mainpack']; // Pack
  385.                 // Push addpacks
  386.                 $subserviceNum = 0;
  387.                 foreach ($pack['addpacks'] as $addpack) {
  388.                     $subserviceNum += 1;
  389.                     $result['totalPrice'] += $addpack['price'];
  390.                     $result['serviceGroups']['tvls']['totalPrice'] += $addpack['price'];
  391.                     $result['serviceGroups']['tvls']['services']['tvls']['subservices']['addpack'.$subserviceNum] = $addpack;
  392.                 }
  393.             }
  394.         } else {
  395.             if ($this->auth->ltype == 1) {
  396.                 $result['serviceGroups']['tvls'] = [
  397.                     'label' => 'Интерактивное ТВ - сервис «СмотрЁшка»',
  398.                     'replace' => true
  399.                 ];
  400.             }
  401.         }
  402.  
  403.         return $result;
  404.     }
  405.  
  406.  
  407.     // Get service names for client
  408.     public function get_service_name_and_login($user_id, $service_id) {
  409.         if ($service_id == 1) { // Internet + MA
  410.             $this->r_db->select('ulogin, dlogin');
  411.             $this->r_db->from('requestbase');
  412.             $this->r_db->where('id', $user_id);
  413.             if (!sizeof($r = $this->r_db->get()->result_array())) {
  414.                 throw new LK_Exception('User '.$user_id.' not found in client db');
  415.             } else {
  416.                 $ulogin = $r[0]['ulogin'];
  417.                 $dlogin = $r[0]['dlogin'];
  418.             }
  419.             $this->r_db->select('1', false);
  420.             $this->r_db->from('iptvnew');
  421.             $this->r_db->where('request', $user_id);
  422.             if (sizeof($this->r_db->get()->result_array())) {
  423.                 $service_name = 'Интернет и Цифровое ТВ';
  424.             } else {
  425.                 $service_name = 'Интернет';
  426.             }
  427.             return ['service_name' => $service_name, 'ulogin' => $ulogin, 'dlogin' => $dlogin];
  428.         } elseif ($service_id == 21) { // LS
  429.             $this->r_db->select('ulogin, dlogin');
  430.             $this->r_db->from('tvls');
  431.             $this->r_db->where('id', $user_id);
  432.             if (!sizeof($r = $this->r_db->get()->result_array())) return false;
  433.             return ['service_name' => 'Интерактивное ТВ - сервис «СмотрЁшка»', 'ulogin' => $r[0]['ulogin'], 'dlogin' => $r[0]['dlogin']];
  434.         } else {
  435.             return false;
  436.         }
  437.     }
  438. }
  439.  
  440. ?>
Advertisement
Add Comment
Please, Sign In to add comment