freezedayscharge.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * running freeze days charge if FREEZE_DAYS_CHARGE_ENABLED
  4. */
  5. if (ubRouting::checkGet('action') and ubRouting::get('action') == 'freezedayscharge') {
  6. if (isset($alterconf['FREEZE_DAYS_CHARGE_ENABLED']) && $alterconf['FREEZE_DAYS_CHARGE_ENABLED']) {
  7. $processingDebug = (ubRouting::checkGet('param') and ubRouting::get('param') == 'debug2ublog');
  8. $verboseDebug = ubRouting::checkGet('verbosedebug');
  9. $freezeDaysInitAmnt = $alterconf['FREEZE_DAYS_INITIAL_AMOUNT'];
  10. $wrkDaysToRestoreFrzDaysInitAmnt = $alterconf['FREEZE_DAYS_WORK_TO_RESTORE'];
  11. class users extends NyanORM { }
  12. class frozen_charge_days extends NyanORM { }
  13. $tabUsers = new users();
  14. $tabFrozenChargeDays = new frozen_charge_days();
  15. $tabUsers->setDebug($processingDebug, $verboseDebug);
  16. $tabFrozenChargeDays->setDebug($processingDebug, $verboseDebug);
  17. $tabUsers->join('LEFT', 'frozen_charge_days', 'login');
  18. $tabUsers->where('users.Passive', '=', '1');
  19. $tabUsers->where('frozen_charge_days.login', 'IS', 'NULL');
  20. $tabUsers->selectable('users.login');
  21. $tabUsers->orderBy('id');
  22. $allFrozenNotInCountTab = $tabUsers->getAll();
  23. if (!empty($allFrozenNotInCountTab)) {
  24. foreach ($allFrozenNotInCountTab as $usr => $eachlogin) {
  25. $exceptionRaised = false;
  26. $field_value = array('login' => $eachlogin['login'],
  27. 'freeze_days_amount' => $freezeDaysInitAmnt,
  28. 'work_days_restore' => $wrkDaysToRestoreFrzDaysInitAmnt
  29. );
  30. $tabFrozenChargeDays->dataArr($field_value);
  31. try {
  32. $tabFrozenChargeDays->create();
  33. } catch (Exception $e) {
  34. $exceptionRaised = true;
  35. log_register('FREEZE DAYS CHARGE: error - ' . $e->getMessage() . ' | ' . $e->getTraceAsString());
  36. }
  37. if ($processingDebug and !$exceptionRaised) {
  38. log_register('FREEZE DAYS CHARGE: login `' . $eachlogin['login'] . '` added to freeze days charge processing table');
  39. }
  40. }
  41. }
  42. $tabFrozenChargeDays->join('LEFT', 'users', 'login');
  43. $tabFrozenChargeDays->selectable(array('`frozen_charge_days`.*', '`users`.`Passive`', '`users`.`Down`', '`users`.`Credit`', '`users`.`Cash`'));
  44. $tabFrozenChargeDays->orderBy('id');
  45. $frozenAll = $tabFrozenChargeDays->getAll();
  46. if (!empty($frozenAll)) {
  47. $usrPassive = 0;
  48. $frozenProcessed = 0;
  49. log_register('FREEZE DAYS CHARGE: frozen users to process charge total amount [' . count($frozenAll) . ']');
  50. foreach ($frozenAll as $usr => $eachlogin) {
  51. $exceptionRaised = false;
  52. $usrLogin = $eachlogin['login'];
  53. $usrPassive = $eachlogin['Passive'];
  54. $usrDown = $eachlogin['Down'];
  55. $usrCredit = $eachlogin['Credit'];
  56. $usrCash = $eachlogin['Cash'];
  57. $frzDaysAmount = $eachlogin['freeze_days_amount'];
  58. $frzDaysUsed = $eachlogin['freeze_days_used'];
  59. $daysWorked = $eachlogin['days_worked'];
  60. $wrkDaysToRestoreFrzDays = $eachlogin['work_days_restore'];
  61. if ($usrPassive) {
  62. $frzDaysUsedIncr = $frzDaysUsed + 1;
  63. $frozenProcessed++;
  64. if ($frzDaysUsed >= $frzDaysAmount) {
  65. $billing->setpassive($usrLogin, '0');
  66. }
  67. $tabFrozenChargeDays->data('freeze_days_used', $frzDaysUsedIncr);
  68. $tabFrozenChargeDays->data('last_freeze_charge_dt', curdatetime());
  69. $tabFrozenChargeDays->where('login', '=', $usrLogin);
  70. try {
  71. $tabFrozenChargeDays->save(true, true);
  72. } catch (Exception $e) {
  73. $exceptionRaised = true;
  74. log_register('FREEZE DAYS CHARGE: error - ' . $e->getMessage() . ' | ' . $e->getTraceAsString());
  75. }
  76. if ($processingDebug and !$exceptionRaised) {
  77. log_register('FREEZE DAYS CHARGE: used freeze days amount increased from [' . $frzDaysUsed . '] to [' . $frzDaysUsedIncr . '] for login `' . $usrLogin . '`');
  78. }
  79. } elseif ($frzDaysUsed >= $frzDaysAmount && $usrCash > '-' . $usrCredit && !$usrDown) {
  80. $daysWorkedIncr = $daysWorked + 1;
  81. $frozenProcessed++;
  82. if ($daysWorkedIncr >= $wrkDaysToRestoreFrzDays) {
  83. $daysWorkedIncr = 0;
  84. $frzDaysUsed = 0;
  85. $tabFrozenChargeDays->data('freeze_days_used', $frzDaysUsed);
  86. if ($processingDebug) {
  87. log_register('FREEZE DAYS CHARGE: used freeze days and worked days amount reset to 0, service freezing activated for login `' . $usrLogin . '`');
  88. }
  89. }
  90. $tabFrozenChargeDays->data('days_worked', $daysWorkedIncr);
  91. $tabFrozenChargeDays->data('last_workdays_upd_dt', curdatetime());
  92. $tabFrozenChargeDays->where('login', '=', $usrLogin);
  93. try {
  94. $tabFrozenChargeDays->save(true, true);
  95. } catch (Exception $e) {
  96. $exceptionRaised = true;
  97. log_register('FREEZE DAYS CHARGE: error - ' . $e->getMessage() . ' | ' . $e->getTraceAsString());
  98. }
  99. if ($processingDebug and !$exceptionRaised) {
  100. log_register('FREEZE DAYS CHARGE: worked days amount increased from [' . $daysWorked . '] to [' . $daysWorkedIncr . '] for login `' . $usrLogin . '`');
  101. }
  102. } elseif ($processingDebug and $verboseDebug) {
  103. log_register('FREEZE DAYS CHARGE: skipping processing for login `' . $usrLogin . '`');
  104. }
  105. }
  106. log_register('FREEZE DAYS CHARGE: processed frozen users amount [' . $frozenProcessed . ']');
  107. die('OK:FREEZE_DAYS_CHARGE');
  108. } else {
  109. die('OK:FREEZE_DAYS_CHARGE_NO_USERS');
  110. }
  111. }
  112. }