api.discounts.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * Basic payments-based discounts implementation
  4. */
  5. class Discounts {
  6. /**
  7. * Contains all available user discounts as login=>discountData
  8. *
  9. * @var array
  10. */
  11. protected $allDiscounts = array();
  12. /**
  13. * Discounts bindings database abstraction layer placeholder
  14. *
  15. * @var object
  16. */
  17. protected $discountsDb = '';
  18. /**
  19. * System messages helper placeholder
  20. *
  21. * @var object
  22. */
  23. protected $messages = '';
  24. /**
  25. * System caching object placeholder
  26. *
  27. * @var object
  28. */
  29. protected $cache = '';
  30. /**
  31. * Some predefined stuff here
  32. */
  33. const DISCOUNTS_TABLE = 'discounts';
  34. const PAYMENTS_TABLE = 'payments';
  35. const PROUTE_PERCENT = 'setdiscountpercent';
  36. const PROUTE_LOGIN = 'setdiscountlogin';
  37. const CACHE_KEY = 'DISCOUNTS';
  38. const CACHE_TIMEOUT = 86400;
  39. const OPTION_ENABLE = 'DISCOUNTS_ENABLED';
  40. public function __construct() {
  41. $this->initMessages();
  42. $this->initCache();
  43. $this->initDb();
  44. $this->loadAllDiscounts();
  45. }
  46. /**
  47. * Inits system messages helper
  48. *
  49. * @return void
  50. */
  51. protected function initMessages() {
  52. $this->messages = new UbillingMessageHelper();
  53. }
  54. /**
  55. * Inits system caching instance for further usage
  56. *
  57. * @return
  58. */
  59. protected function initCache() {
  60. $this->cache = new UbillingCache();
  61. }
  62. /**
  63. * Inits database abstraction layer for further usage
  64. *
  65. * @return void
  66. */
  67. protected function initDb() {
  68. $this->discountsDb = new NyanORM(self::DISCOUNTS_TABLE);
  69. }
  70. /**
  71. * Loads all available discounts data from cache or database into protected property
  72. *
  73. * @return void
  74. */
  75. protected function loadAllDiscounts() {
  76. $cachedData = $this->cache->get(self::CACHE_KEY, self::CACHE_TIMEOUT);
  77. if (!empty($cachedData)) {
  78. $this->allDiscounts = $cachedData;
  79. } else {
  80. $this->allDiscounts = $this->discountsDb->getAll('login');
  81. $this->cache->set(self::CACHE_KEY, $this->allDiscounts, self::CACHE_TIMEOUT);
  82. }
  83. }
  84. /**
  85. * Flushes cached data and loads new from database
  86. *
  87. * @return void
  88. */
  89. protected function flushCache() {
  90. $this->cache->delete(self::CACHE_KEY);
  91. $this->loadAllDiscounts();
  92. }
  93. /**
  94. * Returns current user discount
  95. *
  96. * @param string $login
  97. *
  98. * @return float
  99. */
  100. public function getUserDiscount($login) {
  101. $result = 0;
  102. if (isset($this->allDiscounts[$login])) {
  103. $result = $this->allDiscounts[$login]['percent'];
  104. }
  105. return ($result);
  106. }
  107. /**
  108. * Renders user discount editing form
  109. *
  110. * @param string $login
  111. *
  112. * @return string
  113. */
  114. public function renderUserEditDiscountForm($login) {
  115. $result = '';
  116. $currentDiscountPercent = $this->getUserDiscount($login);
  117. $inputs = wf_HiddenInput(self::PROUTE_LOGIN, $login);
  118. $inputs .= wf_TextInput(self::PROUTE_PERCENT, __('Discount') . ' (%)', $currentDiscountPercent, false, 4, 'digits');
  119. $inputs .= wf_Submit(__('Save'));
  120. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  121. return ($result);
  122. }
  123. /**
  124. * Renders user discount editing form
  125. *
  126. * @param string $login
  127. *
  128. * @return string
  129. */
  130. public function renderDiscountForm($login) {
  131. $result = '';
  132. $currentDiscountPercent = $this->getUserDiscount($login);
  133. $inputs = wf_HiddenInput(self::PROUTE_LOGIN, $login);
  134. $inputs .= wf_TextInput(self::PROUTE_PERCENT, __('Discount') . ' (%)', $currentDiscountPercent, false, 4, 'digits');
  135. $inputs .= wf_Submit(__('Save'));
  136. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  137. return ($result);
  138. }
  139. /**
  140. * Saves user discount to database
  141. *
  142. * @param string $login
  143. *
  144. * @return void
  145. */
  146. public function saveDiscount($login = '') {
  147. $userLogin = '';
  148. if ($login) {
  149. $userLogin = $login;
  150. } else {
  151. $userLogin = ubRouting::post(self::PROUTE_LOGIN);
  152. }
  153. if ($userLogin and ubRouting::checkPost(self::PROUTE_PERCENT, false)) {
  154. $userLoginF = ubRouting::filters($userLogin, 'mres');
  155. $newDiscountPercent = ubRouting::post(self::PROUTE_PERCENT, 'int');
  156. if (!empty($userLogin)) {
  157. //already have discount?
  158. if (isset($this->allDiscounts[$userLogin])) {
  159. $recordId = $this->allDiscounts[$userLogin]['id'];
  160. $this->discountsDb->data('percent', $newDiscountPercent);
  161. $this->discountsDb->where('id', '=', $recordId);
  162. $this->discountsDb->save();
  163. } else {
  164. //creating new discount record
  165. $this->discountsDb->data('login', $userLoginF);
  166. $this->discountsDb->data('percent', $newDiscountPercent);
  167. $this->discountsDb->create();
  168. }
  169. //load some new data for current instance
  170. $this->flushCache();
  171. log_register('DISCOUNT SET (' . $userLogin . ') PERCENT `' . $newDiscountPercent . '`');
  172. }
  173. }
  174. }
  175. /**
  176. * Sets user discount in database
  177. *
  178. * @param string $userLogin
  179. * @param int $discount
  180. *
  181. * @return void
  182. */
  183. public function setDiscount($userLogin, $discount = 0) {
  184. if ($userLogin and $discount) {
  185. $userLoginF = ubRouting::filters($userLogin, 'mres');
  186. $newDiscountPercent = ubRouting::filters($discount, 'int');
  187. if (!empty($userLogin)) {
  188. //already have discount?
  189. if (isset($this->allDiscounts[$userLogin])) {
  190. $recordId = $this->allDiscounts[$userLogin]['id'];
  191. $this->discountsDb->data('percent', $newDiscountPercent);
  192. $this->discountsDb->where('id', '=', $recordId);
  193. $this->discountsDb->save();
  194. } else {
  195. //creating new discount record
  196. $this->discountsDb->data('login', $userLoginF);
  197. $this->discountsDb->data('percent', $newDiscountPercent);
  198. $this->discountsDb->create();
  199. }
  200. //load some new data for current instance
  201. $this->flushCache();
  202. log_register('DISCOUNT SET (' . $userLogin . ') PERCENT `' . $newDiscountPercent . '`');
  203. }
  204. }
  205. }
  206. /**
  207. * Returns all users discounts as login=>percent
  208. *
  209. * @return array
  210. */
  211. protected function getAllUsersDiscounts() {
  212. $result = array();
  213. if (!empty($this->allDiscounts)) {
  214. foreach ($this->allDiscounts as $eachLogin => $eachDiscountData) {
  215. if ($eachDiscountData['percent']) {
  216. $result[$eachLogin] = $eachDiscountData['percent'];
  217. }
  218. }
  219. }
  220. return ($result);
  221. }
  222. /**
  223. * Returns array of all payments made during some optional period
  224. *
  225. * @return array
  226. */
  227. protected function getAllPeriodPayments() {
  228. global $ubillingConfig;
  229. $targetDate = ($ubillingConfig->getAlterParam('DISCOUNT_PREVMONTH')) ? prevmonth() : curmonth();
  230. if ($ubillingConfig->getAlterParam('DISCOUNT_DAILY')) {
  231. $targetDate = curdate();
  232. }
  233. $paymentsDb = new NyanORM(self::PAYMENTS_TABLE);
  234. $paymentsDb->where('date', 'LIKE', $targetDate . '%');
  235. $paymentsDb->where('summ', '>', '0');
  236. $paymentsDb->where('note', 'NOT LIKE', 'DISCOUNT:%');
  237. $allPayments = $paymentsDb->getAll();
  238. $result = array();
  239. if (!empty($allPayments)) {
  240. foreach ($allPayments as $io => $each) {
  241. //sum of user month payments
  242. if (isset($result[$each['login']])) {
  243. $result[$each['login']] = $result[$each['login']] + $each['summ'];
  244. } else {
  245. $result[$each['login']] = $each['summ'];
  246. }
  247. }
  248. }
  249. return ($result);
  250. }
  251. /**
  252. * Do the processing of discounts by the payments
  253. *
  254. * @param bool $debug
  255. */
  256. public function processPayments() {
  257. global $ubillingConfig;
  258. $cashtypeId = ($ubillingConfig->getAlterParam('DISCOUNT_CASHTYPEID')) ? $ubillingConfig->getAlterParam('DISCOUNT_CASHTYPEID') : 1;
  259. $operation = ($ubillingConfig->getAlterParam('DISCOUNT_OPERATION') == 'CORR') ? 'correct' : 'add';
  260. $allUserDiscounts = $this->getAllUsersDiscounts();
  261. $allMonthPayments = $this->getAllPeriodPayments();
  262. if ((!empty($allUserDiscounts) and (!empty($allMonthPayments)))) {
  263. foreach ($allMonthPayments as $login => $eachPayment) {
  264. //have this user any discount?
  265. if (isset($allUserDiscounts[$login])) {
  266. //yes it have
  267. $discountPercent = $allUserDiscounts[$login];
  268. $discountPayment = ($eachPayment / 100) * $discountPercent;
  269. zb_CashAdd($login, $discountPayment, $operation, $cashtypeId, 'DISCOUNT:' . $discountPercent);
  270. }
  271. }
  272. }
  273. }
  274. }