api.powertariffs.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. <?php
  2. /**
  3. * Alternative tariffication model
  4. */
  5. class PowerTariffs {
  6. /**
  7. * System alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Most essential property for this Porno Tariffs mechanics
  14. *
  15. * @var int
  16. */
  17. protected $currentDay = 0;
  18. /**
  19. * Default maximum day of month which will be rounded to 1st.
  20. * Configurable with PT_MAXDAY option.
  21. *
  22. * @var int
  23. */
  24. protected $maxDay = 26;
  25. /**
  26. *
  27. * @var bool
  28. */
  29. protected $chargeOnRegister = true;
  30. /**
  31. * Contains names and prices of system tariffs as name=>fee
  32. *
  33. * @var array
  34. */
  35. protected $systemTariffs = array();
  36. /**
  37. * Contains available power tariffs as tariffname=>recordData
  38. *
  39. * @var array
  40. */
  41. protected $allTariffs = array();
  42. /**
  43. * Contains all existing power users as login=>day
  44. *
  45. * @var array
  46. */
  47. protected $allUsers = array();
  48. /**
  49. * Contains system users data as login=>userdata
  50. *
  51. * @var array
  52. */
  53. protected $systemUsers = array();
  54. /**
  55. * Power tariffs database abstraction placeholder
  56. *
  57. * @var object
  58. */
  59. protected $tariffsDb = '';
  60. /**
  61. * Users affected by power tariffs database abstraction placeholder
  62. *
  63. * @var object
  64. */
  65. protected $usersDb = '';
  66. /**
  67. * All stargazer users abstraction layer placeholder
  68. *
  69. * @var object
  70. */
  71. protected $stgDb = '';
  72. /**
  73. * Users day offset switching log database abstraction placeholder
  74. *
  75. * @var object
  76. */
  77. protected $journalDb = '';
  78. /**
  79. * Users fee charge database abstraction placeholder
  80. *
  81. * @var object
  82. */
  83. protected $feeDb = '';
  84. /**
  85. * System message helper object placeholder
  86. *
  87. * @var object
  88. */
  89. protected $messages = '';
  90. /**
  91. * Contains current administrator login
  92. *
  93. * @var string
  94. */
  95. protected $currentAdministrator = '';
  96. /**
  97. * Routes, tables, etc
  98. */
  99. const URL_ME = '?module=pt';
  100. const TABLE_TARIFFS = 'pt_tariffs';
  101. const TABLE_USERS = 'pt_users';
  102. const TABLE_PAYLOG = 'paymentscorr';
  103. const TABLE_LOG = 'pt_log';
  104. const ROUTE_DELETE = 'deletept';
  105. const ROUTE_EDIT = 'editpt';
  106. /**
  107. * Some config options here
  108. */
  109. const OPTION_MAXDAY = 'PT_MAXDAY';
  110. const OPTION_CHARGEON = 'PT_CHARGEONREG';
  111. const PROUTE_EDITOFFSET = 'newptdayoffset';
  112. const PROUTE_AGREE = 'newptdayoffsetagree';
  113. const ROUTE_BACK = '?module=pl_pt';
  114. /**
  115. * Creates new PT instance
  116. *
  117. * @param bool $loadAll Load system users and tariffs too.
  118. */
  119. public function __construct($loadAll = true) {
  120. $this->initMessages();
  121. $this->loadAlter();
  122. $this->setOptions();
  123. $this->setCurrentDate();
  124. $this->setCurrentAdmin();
  125. $this->initPowerBase();
  126. if ($loadAll) {
  127. $this->loadSystemTariffs();
  128. $this->loadSystemUsers();
  129. }
  130. $this->loadPowerTariffs(); //Go Go Power Rangers
  131. $this->loadPowerUsers();
  132. }
  133. /**
  134. * Sets current day into protected prop
  135. *
  136. * @return void
  137. */
  138. protected function setCurrentDate() {
  139. $currentDayOfMonth = date("d");
  140. if ($currentDayOfMonth >= $this->maxDay) {
  141. $currentDayOfMonth = 1;
  142. }
  143. $this->currentDay = $currentDayOfMonth;
  144. }
  145. /**
  146. * Sets administrator login for current PT instance once
  147. *
  148. * @return void
  149. */
  150. protected function setCurrentAdmin() {
  151. $this->currentAdministrator = whoami();
  152. }
  153. /**
  154. * Inits system message helper
  155. *
  156. * @return void
  157. */
  158. protected function initMessages() {
  159. $this->messages = new UbillingMessageHelper();
  160. }
  161. /**
  162. * Preloads system alter config into protected property for further usage
  163. *
  164. * @global object $ubillingConfig
  165. *
  166. * @return void
  167. */
  168. protected function loadAlter() {
  169. global $ubillingConfig;
  170. $this->altCfg = $ubillingConfig->getAlter();
  171. }
  172. /**
  173. * Sets some custom PowerTariffs options
  174. *
  175. * @return void
  176. */
  177. protected function setOptions() {
  178. //custom maximum day of month
  179. if (isset($this->altCfg[self::OPTION_MAXDAY])) {
  180. if ($this->altCfg[self::OPTION_MAXDAY]) {
  181. $this->maxDay = $this->altCfg[self::OPTION_MAXDAY];
  182. }
  183. }
  184. //charge on register flag setup
  185. if (isset($this->altCfg[self::OPTION_CHARGEON])) {
  186. $this->chargeOnRegister = ($this->altCfg[self::OPTION_CHARGEON]) ? true : false;
  187. }
  188. }
  189. /**
  190. * Loads available system tariffs from database
  191. *
  192. * @return void
  193. */
  194. protected function loadSystemTariffs() {
  195. $this->systemTariffs = zb_TariffGetPricesAll();
  196. }
  197. /**
  198. * Inits all required database abstraction layers into internal props
  199. *
  200. * @return void
  201. */
  202. protected function initPowerBase() {
  203. $this->tariffsDb = new NyanORM(self::TABLE_TARIFFS);
  204. $this->usersDb = new NyanORM(self::TABLE_USERS);
  205. $this->journalDb = new NyanORM(self::TABLE_LOG);
  206. $this->feeDb = new NyanORM(self::TABLE_PAYLOG);
  207. }
  208. /**
  209. * Loads all existing power users to protected property
  210. *
  211. * @return void
  212. */
  213. protected function loadPowerUsers() {
  214. $usersTmp = $this->usersDb->getAll();
  215. if (!empty($usersTmp)) {
  216. foreach ($usersTmp as $io => $each) {
  217. $this->allUsers[$each['login']] = $each['day'];
  218. }
  219. }
  220. }
  221. /**
  222. * Loads all existing system users data to protected property
  223. *
  224. * @return void
  225. */
  226. protected function loadSystemUsers() {
  227. $this->stgDb = new NyanORM('users');
  228. $this->stgDb->selectable(array('login', 'Tariff', 'Cash', 'Credit', 'Passive'));
  229. $this->systemUsers = $this->stgDb->getAll('login');
  230. }
  231. /**
  232. * Loads available power tariffs from database into protected prop
  233. *
  234. * @return void
  235. */
  236. protected function loadPowerTariffs() {
  237. $this->allTariffs = $this->tariffsDb->getAll('tariff');
  238. }
  239. /**
  240. * Renders available power tariffs list with some controls
  241. *
  242. * @return string
  243. */
  244. public function renderTariffsList() {
  245. $result = '';
  246. if (!empty($this->allTariffs)) {
  247. $cells = wf_TableCell(__('Tariff name'));
  248. $cells .= wf_TableCell(__('Tariff fee'));
  249. $cells .= wf_TableCell(__('Actions'));
  250. $rows = wf_TableRow($cells, 'row1');
  251. foreach ($this->allTariffs as $io => $each) {
  252. $cells = wf_TableCell($each['tariff']);
  253. $cells .= wf_TableCell($each['fee']);
  254. $tariffControls = wf_JSAlert(self::URL_ME . '&' . self::ROUTE_DELETE . '=' . $each['tariff'], web_delete_icon(), $this->messages->getDeleteAlert());
  255. $tariffControls .= wf_modalAuto(web_edit_icon(), __('Edit') . ' ' . $each['tariff'], $this->renderTariffEditForm($each['tariff']));
  256. $cells .= wf_TableCell($tariffControls);
  257. $rowClass = (isset($this->systemTariffs[$each['tariff']])) ? 'row5' : 'sigdeleteduser';
  258. $rows .= wf_TableRow($cells, $rowClass);
  259. }
  260. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  261. } else {
  262. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  263. }
  264. return($result);
  265. }
  266. /**
  267. * Returns new power tariff creation form
  268. *
  269. * @return string
  270. */
  271. public function renderTariffCreateForm() {
  272. $result = '';
  273. $tariffsTmp = array();
  274. if (!empty($this->systemTariffs)) {
  275. foreach ($this->systemTariffs as $eachTariff => $eachFee) {
  276. //only tariffs with no Stargazer processed fee can be so powerfull
  277. if ($eachFee == 0) {
  278. if (!isset($this->allTariffs[$eachTariff])) {
  279. //not power tariff assigned yet
  280. $tariffsTmp[$eachTariff] = $eachTariff;
  281. }
  282. }
  283. }
  284. }
  285. if (!empty($tariffsTmp)) {
  286. $inputs = wf_Selector('creatept', $tariffsTmp, __('Tariff name'), '', false) . ' ';
  287. $inputs .= wf_TextInput('createptfee', __('Fee'), '', false, 5, 'finance') . ' ';
  288. $inputs .= wf_Submit(__('Create'));
  289. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  290. }
  291. return($result);
  292. }
  293. /**
  294. * Returns existing power tariff editing form
  295. *
  296. * @param string $tariffName
  297. *
  298. * @return string
  299. */
  300. public function renderTariffEditForm($tariffName) {
  301. $result = '';
  302. if (isset($this->allTariffs[$tariffName])) {
  303. $tariffData = $this->allTariffs[$tariffName];
  304. $inputs = wf_HiddenInput('editpt', $tariffName);
  305. $inputs .= wf_TextInput('editptfee', __('Fee'), $tariffData['fee'], false, 5, 'finance');
  306. $inputs .= wf_Submit(__('Save'));
  307. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  308. }
  309. return($result);
  310. }
  311. /**
  312. * Creates new power tariff in database
  313. *
  314. * @param string $tariffName
  315. * @param float $fee
  316. *
  317. * @return void/string on error
  318. */
  319. public function createTariff($tariffName, $fee) {
  320. $result = '';
  321. $tariffNameF = ubRouting::filters($tariffName, 'mres');
  322. $feeF = ubRouting::filters($fee, 'mres');
  323. if (!isset($this->allTariffs[$tariffName])) {
  324. if ($feeF > 0) {
  325. if (isset($this->systemTariffs[$tariffName])) {
  326. //seems ok, lets create new power tariff
  327. $this->tariffsDb->data('tariff', $tariffNameF);
  328. $this->tariffsDb->data('fee', $feeF);
  329. $this->tariffsDb->create();
  330. $newId = $this->tariffsDb->getLastId();
  331. log_register('PT CREATE TARIFF [' . $newId . '] NAME `' . $tariffName . '` FEE `' . $fee . '`');
  332. } else {
  333. $result .= 'System tariff not found';
  334. }
  335. } else {
  336. $result .= 'Power tariff price cant be zero';
  337. }
  338. } else {
  339. $result .= 'Tariff already exists';
  340. }
  341. return($result);
  342. }
  343. /**
  344. * Saves existing power tariff in database
  345. *
  346. * @param string $tariffName
  347. * @param float $fee
  348. *
  349. * @return void/string on error
  350. */
  351. public function saveTariff($tariffName, $fee) {
  352. $result = '';
  353. $tariffNameF = ubRouting::filters($tariffName, 'mres');
  354. $feeF = ubRouting::filters($fee, 'mres');
  355. if (isset($this->allTariffs[$tariffName])) {
  356. $tariffData = $this->allTariffs[$tariffName];
  357. if ($feeF > 0) {
  358. $tariffId = $tariffData['id'];
  359. //seems ok, lets save power tariff
  360. $this->tariffsDb->data('fee', $feeF);
  361. $this->tariffsDb->where('tariff', '=', $tariffNameF);
  362. $this->tariffsDb->save();
  363. log_register('PT EDIT TARIFF [' . $tariffId . '] NAME `' . $tariffName . '` FEE `' . $fee . '`');
  364. } else {
  365. $result .= 'Power tariff price cant be zero';
  366. }
  367. } else {
  368. $result .= 'Tariff not exists';
  369. }
  370. return($result);
  371. }
  372. /**
  373. * Deletes some existing power tariff from database
  374. *
  375. * @param string $tariffName
  376. *
  377. * @return void/string on error
  378. */
  379. public function deleteTariff($tariffName) {
  380. $result = '';
  381. $tariffNameF = ubRouting::filters($tariffName, 'mres');
  382. if (isset($this->allTariffs[$tariffName])) {
  383. $tariffData = $this->allTariffs[$tariffName];
  384. $this->tariffsDb->where('tariff', '=', $tariffNameF);
  385. $this->tariffsDb->delete();
  386. log_register('PT DELETE TARIFF [' . $tariffData['id'] . '] NAME `' . $tariffData['tariff'] . '` FEE `' . $tariffData['fee'] . '`');
  387. } else {
  388. $result .= 'Tariff not exists';
  389. }
  390. return($result);
  391. }
  392. /**
  393. * Checks is some tariff really have the power?
  394. *
  395. * @param string $tariffName
  396. *
  397. * @return bool
  398. */
  399. public function isPowerTariff($tariffName) {
  400. $result = false;
  401. if (isset($this->allTariffs[$tariffName])) {
  402. $result = true;
  403. }
  404. return($result);
  405. }
  406. /**
  407. * Returns existing power tariff price
  408. *
  409. * @param string $tariffName
  410. *
  411. * @return float
  412. */
  413. public function getPowerTariffPrice($tariffName) {
  414. $result = 0;
  415. if ($this->isPowerTariff($tariffName)) {
  416. $result = $this->allTariffs[$tariffName]['fee'];
  417. }
  418. return($result);
  419. }
  420. /**
  421. * Returns user personal day offset
  422. *
  423. * @param string $userLogin
  424. *
  425. * @return int / -2 - not power user issue
  426. */
  427. public function getUserOffsetDay($userLogin) {
  428. $result = 0;
  429. if (isset($this->allUsers[$userLogin])) {
  430. $result = $this->allUsers[$userLogin];
  431. } else {
  432. $result = -2;
  433. }
  434. return($result);
  435. }
  436. /**
  437. * Returns object maxDay value set by configuration
  438. *
  439. * @return int
  440. */
  441. public function getMaxDay() {
  442. return($this->maxDay);
  443. }
  444. /**
  445. * Check is user using one of power tariffs?
  446. *
  447. * @param array $userData
  448. *
  449. * @return bool
  450. */
  451. protected function userHavePowerTariff($userData) {
  452. $result = false;
  453. $userTariff = $userData['Tariff'];
  454. if (isset($this->allTariffs[$userTariff])) {
  455. $result = true;
  456. }
  457. return($result);
  458. }
  459. /**
  460. * Checks is user active now?
  461. *
  462. * @param array $userData
  463. *
  464. * @return bool
  465. */
  466. protected function isUserActive($userData) {
  467. $result = false;
  468. //dont check credit state to avoid fee day offset change
  469. if (($userData['Cash'] >= 0) AND ( $userData['Passive'] == 0)) {
  470. $result = true;
  471. }
  472. return($result);
  473. }
  474. /**
  475. * Logs user day offset switching into
  476. *
  477. * @param string $userLogin
  478. * @param string $userTariff
  479. * @param int $dayOffset
  480. *
  481. * @return void
  482. */
  483. protected function logUser($userLogin, $userTariff, $dayOffset) {
  484. $curDateTime = curdatetime();
  485. $this->journalDb->data('date', $curDateTime);
  486. $this->journalDb->data('login', $userLogin);
  487. $this->journalDb->data('tariff', $userTariff);
  488. $this->journalDb->data('day', $dayOffset);
  489. $this->journalDb->create();
  490. }
  491. /**
  492. * Runs for detecting of newly registered users or users which need to be power-users
  493. *
  494. * @return
  495. */
  496. public function registerNewUsers() {
  497. if (!empty($this->systemUsers)) {
  498. foreach ($this->systemUsers as $userLogin => $userData) {
  499. //not registered yet
  500. if (!isset($this->allUsers[$userLogin])) {
  501. //need to do something with this user at all?
  502. if ($this->userHavePowerTariff($userData)) {
  503. //user is not dead at all
  504. if ($this->isUserActive($userData)) {
  505. //write that user as real power users to DB
  506. $this->usersDb->data('login', $userLogin);
  507. $this->usersDb->data('day', $this->currentDay);
  508. $this->usersDb->create();
  509. $this->logUser($userLogin, $userData['Tariff'], $this->currentDay);
  510. //charging fee on user detection if required
  511. if ($this->chargeOnRegister) {
  512. $realCurrentDay = date("d");
  513. //avoid double tax rates :P
  514. if ($realCurrentDay < $this->maxDay) {
  515. $tariffData = $this->allTariffs[$userData['Tariff']];
  516. $tariffFee = $tariffData['fee'];
  517. $this->chargeFee($userLogin, $tariffFee, $userData['Cash']);
  518. } else {
  519. log_register('PT USER (' . $userLogin . ') SKIP FEE ON DAY `' . $realCurrentDay . '`');
  520. }
  521. }
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. /**
  529. * Charges fee from user account. Using this instead zb_CashAdd for avoid unnecessary logging.
  530. *
  531. * @global object $billing
  532. * @param string $userLogin
  533. * @param float $fee
  534. * @param float $balance
  535. *
  536. * @return void
  537. */
  538. protected function chargeFee($userLogin, $fee, $balance) {
  539. global $billing;
  540. $fee = '-' . abs($fee); //fee is negative i guess?
  541. $curDateTime = curdatetime(); //fee datetime is changing on each operation
  542. //charge fee from user balance
  543. $billing->addcash($userLogin, $fee);
  544. //logging financial operation
  545. $this->feeDb->data('login', $userLogin);
  546. $this->feeDb->data('date', $curDateTime);
  547. $this->feeDb->data('admin', $this->currentAdministrator);
  548. $this->feeDb->data('balance', $balance);
  549. $this->feeDb->data('summ', $fee);
  550. $this->feeDb->data('cashtypeid', '1');
  551. $this->feeDb->data('note', 'PTFEE');
  552. $this->feeDb->create();
  553. }
  554. /**
  555. * Performs user burial on cash exceed
  556. *
  557. * @param string $userLogin
  558. *
  559. * @return void
  560. */
  561. protected function userBurial($userLogin) {
  562. $this->usersDb->data('day', 0); //set offset day to zero
  563. $this->usersDb->where('login', '=', $userLogin);
  564. $this->usersDb->save();
  565. }
  566. /**
  567. * Performs user resurrection on restoring cash
  568. *
  569. * @param string $userLogin
  570. *
  571. * @return void
  572. */
  573. protected function userResurrect($userLogin) {
  574. $newOffsetDay = date("d");
  575. if ($newOffsetDay >= $this->maxDay) {
  576. $newOffsetDay = $this->maxDay - 1;
  577. }
  578. $this->usersDb->data('day', $newOffsetDay); //set offset day to current with round to "next month" if required
  579. $this->usersDb->where('login', '=', $userLogin);
  580. $this->usersDb->save();
  581. }
  582. /**
  583. * Renders user day offset modification form
  584. *
  585. * @param string $userLogin
  586. *
  587. * @return string
  588. */
  589. public function renderUserOffsetEditForm($userLogin) {
  590. $result = '';
  591. if (isset($this->allUsers[$userLogin])) {
  592. $currentOffset = $this->allUsers[$userLogin];
  593. $daysParams = array();
  594. for ($i = 1; $i < $this->maxDay; $i++) {
  595. $daysParams[$i] = $i;
  596. }
  597. $inputs = wf_Selector(self::PROUTE_EDITOFFSET, $daysParams, __('Day'), $currentOffset, false) . ' ';
  598. $inputs .= wf_CheckInput(self::PROUTE_AGREE, __('I`m ready'), false, false) . ' ';
  599. $inputs .= wf_Submit(__('Save'));
  600. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  601. $result .= $this->messages->getStyledMessage(__('Current day of fee') . ': ' . $currentOffset, 'info') . wf_delimiter(0);
  602. } else {
  603. $result .= $this->messages->getStyledMessage(__('This is user without power tariff'), 'warning');
  604. }
  605. return($result);
  606. }
  607. /**
  608. * Saves new user offset day in database
  609. *
  610. * @param string $userLogin
  611. * @param int $day
  612. *
  613. * @return void
  614. */
  615. public function saveUserOffsetDay($userLogin, $day) {
  616. $userLoginF = ubRouting::filters($userLogin, 'mres');
  617. $day = ubRouting::filters($day, 'int');
  618. if (!empty($day)) {
  619. if (isset($this->systemUsers[$userLogin])) {
  620. $userData = $this->systemUsers[$userLogin];
  621. $userTariff = $userData['Tariff'];
  622. $this->usersDb->where('login', '=', $userLoginF);
  623. $this->usersDb->data('day', $day);
  624. $this->usersDb->save();
  625. log_register('PT USER (' . $userLogin . ') SET DAY `' . $day . '`');
  626. $this->logUser($userLogin, $userTariff, $day);
  627. }
  628. }
  629. }
  630. /**
  631. * Renders powertariffs internal log data for some user
  632. *
  633. * @param string $userLogin
  634. *
  635. * @return string
  636. */
  637. public function renderPowerUserLog($userLogin) {
  638. $result = '';
  639. $userLoginF = ubRouting::filters($userLogin, 'mres');
  640. $this->journalDb->where('login', '=', $userLoginF);
  641. $this->journalDb->orderBy('id', 'DESC');
  642. $allEvents = $this->journalDb->getAll();
  643. if (!empty($allEvents)) {
  644. $cells = wf_TableCell(__('Date'));
  645. $cells .= wf_TableCell(__('Tariff'));
  646. $cells .= wf_TableCell(__('Day'));
  647. $rows = wf_TableRow($cells, 'row1');
  648. foreach ($allEvents as $io => $each) {
  649. $cells = wf_TableCell($each['date']);
  650. $cells .= wf_TableCell($each['tariff']);
  651. $cells .= wf_TableCell($each['day']);
  652. $rows .= wf_TableRow($cells, 'row5');
  653. }
  654. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  655. } else {
  656. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  657. }
  658. return($result);
  659. }
  660. /**
  661. * Performs fee processing for users affected by power tariffs
  662. *
  663. * @return void
  664. */
  665. public function processingFee() {
  666. if (!empty($this->systemUsers)) {
  667. $realCurrentDay = date("d");
  668. foreach ($this->systemUsers as $userLogin => $userData) {
  669. //user is affected by some power tariff
  670. if (isset($this->allUsers[$userLogin])) {
  671. $userDayOffset = $this->allUsers[$userLogin];
  672. //now user is on the power tariff
  673. if ($this->userHavePowerTariff($userData)) {
  674. $tariffData = $this->allTariffs[$userData['Tariff']];
  675. $tariffFee = $tariffData['fee'];
  676. //now is user personal date for fee charge
  677. if ($userDayOffset == $this->currentDay AND $realCurrentDay == $this->currentDay) { // A-A-A!!!!
  678. //user is active, and we can charge some fee from him
  679. if ($this->isUserActive($userData)) {
  680. //charge some fee from this user
  681. $this->chargeFee($userLogin, $tariffFee, $userData['Cash']);
  682. //new user balance state after fee charge
  683. $newBalanceState = $userData['Cash'] - $tariffFee;
  684. if ($newBalanceState < '-' . $userData['Credit']) { //not <= because zero cash is valid value
  685. $this->userBurial($userLogin); //settin offset to zero
  686. $this->logUser($userLogin, $userData['Tariff'], 0); //log user burial
  687. }
  688. }
  689. } else {
  690. //not current user day or user is buried
  691. if ($userDayOffset == 0) {
  692. //yeah, he is really buried
  693. if ($this->isUserActive($userData)) {
  694. //but he restored his account balance
  695. $this->userResurrect($userLogin); //set new offset day to current
  696. $this->logUser($userLogin, $userData['Tariff'], $this->currentDay); //log resurrection miracle
  697. }
  698. }
  699. }
  700. }
  701. }
  702. }
  703. }
  704. }
  705. }