index.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. $user_ip = zbs_UserDetectIp('debug');
  3. $user_login = zbs_UserGetLoginByIp($user_ip);
  4. $us_config = zbs_LoadConfig();
  5. //tariff changing options
  6. $us_currency = $us_config['currency'];
  7. $tc_enabled = $us_config['TC_ENABLED'];
  8. $tc_priceup = $us_config['TC_PRICEUP'];
  9. $tc_pricedown = $us_config['TC_PRICEDOWN'];
  10. $tc_pricesimilar = $us_config['TC_PRICESIMILAR'];
  11. $tc_credit = $us_config['TC_CREDIT'];
  12. $tc_cashtypeid = $us_config['TC_CASHTYPEID'];
  13. $user_data = zbs_UserGetStargazerData($user_login);
  14. $user_cash = $user_data['Cash'];
  15. $user_credit = $user_data['Credit'];
  16. $user_credit_expire = $user_data['CreditExpire'];
  17. $user_tariff = zbs_UserGetTariff($user_login);
  18. $user_tariffnm = $user_data['TariffChange'];
  19. /////////////// Loading tariff move matrix API
  20. /**
  21. * Returns user move-allowed tariffs in tariff-matrix mode
  22. *
  23. * @param string $user_tariff
  24. * @return bool/array
  25. */
  26. function zbs_MatrixGetAllowed($user_tariff) {
  27. $matrix = parse_ini_file('config/tariffmatrix.ini');
  28. $result = false;
  29. if (!empty($matrix)) {
  30. if (isset($matrix[$user_tariff])) {
  31. //extract tariff movement rules
  32. $result = explode(',', $matrix[$user_tariff]);
  33. } else {
  34. //no tariff match
  35. $result = false;
  36. }
  37. } else {
  38. //no matrix entries
  39. $result = false;
  40. }
  41. return ($result);
  42. }
  43. /**
  44. * Returns array of allowed to move matrix tariffs
  45. *
  46. * @return string
  47. */
  48. function zbs_MatrixGetAllowedFrom() {
  49. $matrix = parse_ini_file('config/tariffmatrix.ini');
  50. $result = array();
  51. if (!empty($matrix)) {
  52. foreach ($matrix as $io => $each) {
  53. $result[] = $io;
  54. }
  55. }
  56. return ($result);
  57. }
  58. //////////////// allowed tariffs permissions
  59. $tc_extended_matrix = $us_config['TC_EXTENDED_MATRIX'];
  60. if (!$tc_extended_matrix) {
  61. //old 2 opts style
  62. $tc_tariffsallowed = explode(',', $us_config['TC_TARIFFSALLOWED']);
  63. $tc_tariffenabledfrom = explode(',', $us_config['TC_TARIFFENABLEDFROM']);
  64. } else {
  65. //extended tariffs move matrix
  66. $tc_tariffsallowed = zbs_MatrixGetAllowed($user_tariff);
  67. $tc_tariffenabledfrom = zbs_MatrixGetAllowedFrom();
  68. }
  69. ////////////////////////////////
  70. /**
  71. * Returns selector of allowed tariffs to move, except current tariff
  72. *
  73. * @param string $tc_tariffsallowed
  74. * @param string $user_tariff
  75. * @return string
  76. */
  77. function zbs_TariffSelector($tc_tariffsallowed, $user_tariff) {
  78. global $us_config;
  79. if ($us_config['SHOW_SPEED']) {
  80. $rawSpeedMbits = (@$us_config['SHOW_SPEED_MB']) ? true : false;
  81. $speedOffset = (@$us_config['SHOW_SPEED_1000B']) ? 1000 : 1024;
  82. $allSpeeds = zbs_TariffGetAllSpeeds($rawSpeedMbits, $speedOffset);
  83. }
  84. $params = array();
  85. if (!empty($tc_tariffsallowed)) {
  86. foreach ($tc_tariffsallowed as $io => $eachtariff) {
  87. if ($eachtariff != $user_tariff) {
  88. if ($us_config['SHOW_SPEED']) {
  89. $tariffSpeed = @$allSpeeds[$eachtariff];
  90. $params[trim($eachtariff)] = __($eachtariff) . ' - ' . $tariffSpeed;
  91. } else {
  92. $params[trim($eachtariff)] = __($eachtariff);
  93. }
  94. }
  95. }
  96. }
  97. $result = la_Selector('newtariff', $params, '', '', false);
  98. return ($result);
  99. }
  100. /**
  101. * Returns array of tariff change prices for allowed tariffs
  102. *
  103. * @param array $tc_tariffsallowed
  104. * @param string $user_tariff
  105. * @param float $tc_priceup
  106. * @param float $tc_pricedown
  107. * @param float $tc_pricesimilar
  108. *
  109. * @return array
  110. */
  111. function zbs_TariffGetChangePrice($tc_tariffsallowed, $user_tariff, $tc_priceup, $tc_pricedown, $tc_pricesimilar) {
  112. $allprices = zbs_TariffGetAllPrices();
  113. $current_fee = $allprices[$user_tariff];
  114. $result = array();
  115. if (!empty($tc_tariffsallowed)) {
  116. foreach ($tc_tariffsallowed as $eachtariff) {
  117. if (isset($allprices[$eachtariff])) {
  118. //if higer then current fee
  119. if ($allprices[$eachtariff] > $current_fee) {
  120. $result[$eachtariff] = $tc_priceup;
  121. }
  122. //if lower then current
  123. if ($allprices[$eachtariff] < $current_fee) {
  124. $result[$eachtariff] = $tc_pricedown;
  125. }
  126. // if eq
  127. if ($allprices[$eachtariff] == $current_fee) {
  128. $result[$eachtariff] = $tc_pricesimilar;
  129. }
  130. } else {
  131. $result[$eachtariff] = 0;
  132. }
  133. }
  134. }
  135. return ($result);
  136. }
  137. /**
  138. * Returns table with tariff change pricing
  139. *
  140. * @param array $tc_tariffsallowed
  141. * @param string $us_currency
  142. * @param string $user_tariff
  143. * @param float $tc_priceup
  144. * @param float $tc_pricedown
  145. * @param float $tc_pricesimilar
  146. * @return string
  147. */
  148. function zbs_TariffGetShowPrices($tc_tariffsallowed, $us_currency, $user_tariff, $tc_priceup, $tc_pricedown, $tc_pricesimilar) {
  149. global $us_config;
  150. $allprices = zbs_TariffGetAllPrices();
  151. $rawSpeedMbits = (@$us_config['SHOW_SPEED_MB']) ? true : false;
  152. $allSpeeds = zbs_TariffGetAllSpeeds($rawSpeedMbits);
  153. $allcosts = zbs_TariffGetChangePrice($tc_tariffsallowed, $user_tariff, $tc_priceup, $tc_pricedown, $tc_pricesimilar);
  154. $cells = la_TableCell(__('Tariff'));
  155. if ($us_config['SHOW_SPEED']) {
  156. $cells .= la_TableCell(__('Tariff speed'));
  157. }
  158. $cells .= la_TableCell(__('Monthly fee'));
  159. $cells .= la_TableCell(__('Cost of change'));
  160. $rows = la_TableRow($cells, 'row1');
  161. if (!empty($tc_tariffsallowed)) {
  162. foreach ($tc_tariffsallowed as $eachtariff) {
  163. $cells = la_TableCell(__($eachtariff));
  164. if ($us_config['SHOW_SPEED']) {
  165. $cells .= la_TableCell(@$allSpeeds[$eachtariff]);
  166. }
  167. $cells .= la_TableCell(@$allprices[$eachtariff] . ' ' . $us_currency);
  168. $cells .= la_TableCell(@$allcosts[$eachtariff] . ' ' . $us_currency);
  169. $rows .= la_TableRow($cells, 'row2');
  170. }
  171. }
  172. $result = la_TableBody($rows, '100%', 0);
  173. return ($result);
  174. }
  175. /**
  176. * Returns complete tariff moving form
  177. *
  178. * @param string $login
  179. * @param array $tc_tariffsallowed
  180. * @param float $tc_priceup
  181. * @param float $tc_pricedown
  182. * @param float $tc_pricesimilar
  183. * @param string $us_currency
  184. * @return string
  185. */
  186. function zbs_TariffChangeForm($login, $tc_tariffsallowed, $tc_priceup, $tc_pricedown, $tc_pricesimilar, $us_currency) {
  187. global $us_config;
  188. $user_tariff = zbs_UserGetTariff($login);
  189. $alltariffs = zbs_TariffGetAllPrices();
  190. $form = '
  191. ' . __('Your current tariff is') . ': ' . __($user_tariff) . ' ' . __('with monthly fee') . ' ' . $alltariffs[$user_tariff] . ' ' . $us_currency . '<br>
  192. ' . __('The cost of switching to a lower rate monthly fee') . ': ' . $tc_pricedown . ' ' . $us_currency . '<br>
  193. ' . __('The cost of switching to a higher monthly fee tariff') . ': ' . $tc_priceup . ' ' . $us_currency . '<br>
  194. ' . __('The cost of the transition rate for the same monthly fee') . ': ' . $tc_pricesimilar . ' ' . $us_currency . '<br>
  195. ' . la_tag('br') . '
  196. ' . zbs_TariffGetShowPrices($tc_tariffsallowed, $us_currency, $user_tariff, $tc_priceup, $tc_pricedown, $tc_pricesimilar) . '
  197. ' . la_tag('br') . '
  198. ';
  199. $inputs = __('New tariff') . ' ' . zbs_TariffSelector($tc_tariffsallowed, $user_tariff) . la_delimiter();
  200. $inputs .= la_CheckInput('agree', __('I am sure that I am an adult and have read everything that is written above'), false, false);
  201. $inputs .= la_delimiter();
  202. $nmChangeFlag = true;
  203. if (isset($us_config['TC_RIGHTNOW'])) {
  204. if ($us_config['TC_RIGHTNOW']) {
  205. $nmChangeFlag = false;
  206. }
  207. }
  208. $sumbitLabel = ($nmChangeFlag) ? __('I want this tariff next month') : __('I want this tariff right now');
  209. $inputs .= la_tag('input', false, '', 'type="submit" style="word-wrap: break-word; white-space: normal;" value="' . $sumbitLabel . '"');
  210. $form .= la_Form('', 'POST', $inputs, '');
  211. return ($form);
  212. }
  213. //check is tariff changing is enabled?
  214. if ($tc_enabled) {
  215. //check is TC allowed for current user tariff plan
  216. if (in_array($user_tariff, $tc_tariffenabledfrom)) {
  217. //tariff change subroutines
  218. if (isset($_POST['newtariff'])) {
  219. $change_prices = zbs_TariffGetChangePrice($tc_tariffsallowed, $user_tariff, $tc_priceup, $tc_pricedown, $tc_pricesimilar);
  220. if (in_array($_POST['newtariff'], $tc_tariffsallowed)) {
  221. // agreement check
  222. if (isset($_POST['agree'])) {
  223. // and not enought money, set credit
  224. if ($user_cash < $change_prices[$_POST['newtariff']]) {
  225. //if TC_CREDIT option enabled
  226. if ($tc_credit) {
  227. $newcredit = $change_prices[$_POST['newtariff']] + $user_credit;
  228. billing_setcredit($user_login, $newcredit);
  229. // check for current credit expirity - added in 0.5.7
  230. // without this check this conflicts with SC_ module
  231. if (!$user_credit_expire) {
  232. //set credit expire date for month from this moment
  233. $timestamp = time();
  234. $monthOffset = $timestamp + 2678400; // 31 days in seconds
  235. $creditend = date("Y-m-d", $monthOffset);
  236. billing_setcreditexpire($user_login, $creditend);
  237. }
  238. }
  239. }
  240. //TC change fee anyway
  241. zbs_PaymentLog($user_login, '-' . $change_prices[$_POST['newtariff']], $tc_cashtypeid, "TCHANGE:" . $_POST['newtariff']);
  242. billing_addcash($user_login, '-' . $change_prices[$_POST['newtariff']]);
  243. //nm set tariff routine
  244. $nextMonthTc = true;
  245. if (isset($us_config['TC_RIGHTNOW'])) {
  246. if ($us_config['TC_RIGHTNOW']) {
  247. $nextMonthTc = false;
  248. }
  249. }
  250. if ($nextMonthTc) {
  251. billing_settariffnm($user_login, mysql_real_escape_string($_POST['newtariff']));
  252. log_register('CHANGE TariffNM (' . $user_login . ') ON `' . $_POST['newtariff'] . '`');
  253. } else {
  254. billing_settariff($user_login, mysql_real_escape_string($_POST['newtariff']));
  255. log_register('CHANGE Tariff (' . $user_login . ') ON `' . $_POST['newtariff'] . '`');
  256. }
  257. rcms_redirect("index.php");
  258. } else {
  259. // agreement check fail
  260. show_window(__('Sorry'), __('You must accept our policy'));
  261. }
  262. //die if tariff not allowed
  263. } else {
  264. die("oO");
  265. }
  266. } // end of tariff change subroutine (POST processing)
  267. if (empty($user_tariffnm)) {
  268. show_window(__('Tariff change'), zbs_TariffChangeForm($user_login, $tc_tariffsallowed, $tc_priceup, $tc_pricedown, $tc_pricesimilar, $us_currency));
  269. } else {
  270. show_window(__('Sorry'), __('You already have planned tariff change'));
  271. }
  272. } else {
  273. show_window(__('Sorry'), __('Your current tariff does not allow tariff change on your own'));
  274. }
  275. //end of TC enabled check
  276. // or not enabled at all?
  277. } else {
  278. show_window(__('Sorry'), __('Unfortunately self tariff change is disabled'));
  279. }
  280. ?>