api.autocredit.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * Automatic user credits setting class
  4. */
  5. class AutoCredit {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Custom field ID for mark of required credit setup
  14. *
  15. * @var int
  16. */
  17. protected $cfId = 0;
  18. /**
  19. * Contains array of available CFs of required type for all users as login=>day of month
  20. *
  21. * @var array
  22. */
  23. protected $cfData = array();
  24. /**
  25. * Contains all of available users in database
  26. *
  27. * @var array
  28. */
  29. protected $allUsers = array();
  30. /**
  31. * Contains all available tariff prices as name=>Fee
  32. *
  33. * @var string
  34. */
  35. protected $allTariffPrices = array();
  36. /**
  37. * Contains available virtual services as tagid=>price
  38. *
  39. * @var array
  40. */
  41. protected $allVservices = array();
  42. /**
  43. * Contains all vservices tags assigned for users as login=>tagIds=>tagCount
  44. *
  45. * @var array
  46. */
  47. protected $allUserTags = array();
  48. /**
  49. * Contains preprocessed users virtual services prices as login=>price summary
  50. *
  51. * @var array
  52. */
  53. protected $allUserServices = array();
  54. /**
  55. * Contains alter option name with CF ID
  56. */
  57. const OPTION_CFID = 'AUTOCREDIT_CFID';
  58. /**
  59. * Creates new automatic creditor instance
  60. */
  61. public function __construct() {
  62. $this->loadAter();
  63. $this->setOptions();
  64. $this->loadUsers();
  65. $this->loadTariffs();
  66. $this->loadVirtualServices();
  67. $this->loadTags();
  68. $this->preprocessVservices();
  69. if (!empty($this->cfId)) {
  70. $this->loadCfs();
  71. }
  72. }
  73. /**
  74. * Preloads alter config into protected prop for further usage
  75. *
  76. * @global object $ubillingConfig
  77. *
  78. * @return void
  79. */
  80. protected function loadAter() {
  81. global $ubillingConfig;
  82. $this->altCfg = $ubillingConfig->getAlter();
  83. }
  84. /**
  85. * Sets initial options due billing configuration files
  86. *
  87. * @return void
  88. */
  89. protected function setOptions() {
  90. if (isset($this->altCfg[self::OPTION_CFID])) {
  91. $optionRaw = $this->altCfg[self::OPTION_CFID];
  92. $optionRaw = ubRouting::filters($optionRaw, 'int');
  93. if (!empty($optionRaw)) {
  94. $this->cfId = $optionRaw;
  95. }
  96. }
  97. }
  98. /**
  99. * Loads available virtual services and their prices
  100. *
  101. * @return void
  102. */
  103. protected function loadVirtualServices() {
  104. $servicesRaw = zb_VserviceGetAllData();
  105. if (!empty($servicesRaw)) {
  106. foreach ($servicesRaw as $io => $eachService) {
  107. $this->allVservices[$eachService['tagid']] = $eachService['price'];
  108. }
  109. }
  110. }
  111. /**
  112. * Loads all tags assigned for users
  113. *
  114. * @return void
  115. */
  116. protected function loadTags() {
  117. $tagsDb = new NyanORM('tags');
  118. $allTagsRaw = $tagsDb->getAll();
  119. if (!empty($allTagsRaw)) {
  120. foreach ($allTagsRaw as $io => $each) {
  121. if (isset($this->allVservices[$each['tagid']])) {
  122. //only vservices tags
  123. if (isset($this->allUserTags[$each['login']][$each['tagid']])) {
  124. $this->allUserTags[$each['login']][$each['tagid']] += 1;
  125. } else {
  126. $this->allUserTags[$each['login']][$each['tagid']] = 1;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Performs preprocessing of all user virtual services prices into allUserServices prop
  134. *
  135. * @return void
  136. */
  137. protected function preprocessVservices() {
  138. if (!empty($this->allUserTags)) {
  139. foreach ($this->allUserTags as $eachLogin => $eachUserTags) {
  140. $servicesPrice = 0;
  141. if (!empty($eachUserTags)) {
  142. foreach ($eachUserTags as $tagId => $tagsCount) {
  143. $servicesPrice += ($this->allVservices[$tagId] * $tagsCount);
  144. }
  145. $this->allUserServices[$eachLogin] = $servicesPrice;
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * Loads all available users from database
  152. *
  153. * @return void
  154. */
  155. protected function loadUsers() {
  156. $this->allUsers = zb_UserGetAllStargazerDataAssoc();
  157. }
  158. /**
  159. * Loads all available tariff fees
  160. *
  161. * @return void
  162. */
  163. protected function loadTariffs() {
  164. $this->allTariffPrices = zb_TariffGetPricesAll();
  165. }
  166. /**
  167. * Loads all avaialble CFs content from database for all of existing users
  168. *
  169. * @return void
  170. */
  171. protected function loadCfs() {
  172. if (!empty($this->cfId)) {
  173. $cf = new CustomFields();
  174. $cfsRaw = $cf->getAllFieldsData();
  175. if (!empty($cfsRaw)) {
  176. foreach ($cfsRaw as $io => $each) {
  177. if ($each['typeid'] == $this->cfId) {
  178. $userLogin = $each['login'];
  179. if (isset($this->allUsers[$userLogin])) {
  180. //user is available
  181. $cfContent = ubRouting::filters($each['content'], 'int');
  182. if (is_numeric($cfContent) AND $cfContent > 0 AND $cfContent < 32) {
  183. //is valid day of month value
  184. $this->cfData[$userLogin] = $cfContent;
  185. } else {
  186. log_register('AUTOCREDIT (' . $userLogin . ') FAIL WRONG CFDAY `' . $cfContent . '`');
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. /**
  195. * Returns next Year-month number with leading zero in format Y-m-
  196. *
  197. * @return string
  198. */
  199. protected function getNextMonth() {
  200. $curYear = date("Y");
  201. $nextYear = $curYear;
  202. $curMonth = date("n");
  203. if ($curMonth == 12) {
  204. //December increases year and sets next month to January
  205. $nextMonth = 1;
  206. $nextYear = $nextYear + 1;
  207. } else {
  208. $nextMonth = $curMonth + 1;
  209. }
  210. if ($nextMonth < 10) {
  211. $nextMonth = '0' . $nextMonth;
  212. }
  213. $result = $nextYear . '-' . $nextMonth . '-';
  214. return($result);
  215. }
  216. /**
  217. * Performs automatic credit setup
  218. *
  219. * @global object $billing
  220. *
  221. * @param string $mode - user marker for credit setup
  222. *
  223. * @return int
  224. */
  225. public function processing($mode = 'cf') {
  226. global $billing;
  227. $count = 0;
  228. if ($mode == 'cf') {
  229. //default processing mode. Left for extending in future on tags, triggers etc.
  230. if (!empty($this->cfData)) {
  231. $nextMonth = $this->getNextMonth();
  232. foreach ($this->cfData as $userLogin => $dayRaw) {
  233. $userData = $this->allUsers[$userLogin];
  234. $userTariff = $userData['Tariff'];
  235. $userTariffFee = $this->allTariffPrices[$userTariff];
  236. $userServicesFee = (isset($this->allUserServices[$userLogin])) ? $this->allUserServices[$userLogin] : 0;
  237. $userCreditSumm = $userTariffFee + $userServicesFee;
  238. if ($dayRaw < 10) {
  239. //fixing leading zero
  240. $dayRaw = '0' . $dayRaw;
  241. }
  242. $creditExpireDay = $nextMonth . $dayRaw;
  243. if ($userTariffFee > 0) {
  244. //not free tariff
  245. if (zb_checkDate($creditExpireDay)) {
  246. $billing->setcredit($userLogin, $userCreditSumm);
  247. $billing->setcreditexpire($userLogin, $creditExpireDay);
  248. log_register('AUTOCREDIT (' . $userLogin . ') ON `' . $userCreditSumm . '` TO `' . $creditExpireDay . '`');
  249. $count++;
  250. } else {
  251. log_register('AUTOCREDIT (' . $userLogin . ') FAIL WRONG CFDAY `' . $creditExpireDay . '`');
  252. }
  253. }
  254. }
  255. }
  256. }
  257. return($count);
  258. }
  259. }