api.reminder.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. <?php
  2. /**
  3. * User SMS notification class
  4. */
  5. class Reminder {
  6. /**
  7. * Contains all of available user logins with reminder tag
  8. *
  9. * @var array
  10. */
  11. protected $AllLogin = array();
  12. /**
  13. * Contains system alter config as key=>value
  14. *
  15. * @var array
  16. */
  17. // protected $AltCfg = array();
  18. /**
  19. * Contains all of available user phones data
  20. *
  21. * @var array
  22. */
  23. protected $AllPhones = array();
  24. /**
  25. * Placeholder for UbillingSMS object
  26. *
  27. * @var object
  28. */
  29. protected $sms = '';
  30. /**
  31. * Placeholder for FundsFlow object
  32. *
  33. * @var object
  34. */
  35. protected $money = '';
  36. /**
  37. * Contains data for native templating messages
  38. *
  39. * @var array
  40. */
  41. protected $AllTemplates = array();
  42. /**
  43. * Placeholder for REMINDER_ENABLED alter.ini option
  44. *
  45. * @var int
  46. */
  47. protected $rmdMode = 0;
  48. /**
  49. * Placeholder for REMINDER_TAGID alter.ini option
  50. *
  51. * @var int
  52. */
  53. protected $rmdTagID = 0;
  54. /**
  55. * Placeholder for REMINDER_DAYS_THRESHOLD alter.ini option
  56. *
  57. * @var int
  58. */
  59. protected $rmdDaysThreshold = 2;
  60. /**
  61. * Placeholder for REMINDER_PREFIX alter.ini option
  62. *
  63. * @var string
  64. */
  65. protected $rmdPhonePrefix = '';
  66. /**
  67. * Placeholder for REMINDER_TEMPLATE alter.ini option
  68. *
  69. * @var string
  70. */
  71. protected $rmdTemplate = '';
  72. /**
  73. * Placeholder for REMINDER_FORCE_TRANSLIT alter.ini option
  74. */
  75. protected $rmdForceTranslit = true;
  76. /**
  77. * Placeholder for REMINDER_USE_EXTMOBILES alter.ini option
  78. *
  79. * @var bool
  80. */
  81. protected $rmdUseExtMobiles = false;
  82. /**
  83. * Placeholder for REMINDER_CONSIDER_CREDIT alter.ini option
  84. *
  85. * @var bool
  86. */
  87. protected $rmdConsiderCredits = 0;
  88. /**
  89. * Placeholder for REMINDER_DAYS_THRESHOLD_CREDIT alter.ini option
  90. *
  91. * @var int
  92. */
  93. protected $rmdDaysThresholdCredit = 0;
  94. /**
  95. * Placeholder for REMINDER_TEMPLATE_CREDIT alter.ini option
  96. *
  97. * @var string
  98. */
  99. protected $rmdTemplateCredit = '';
  100. /**
  101. * Placeholder for REMINDER_CONSIDER_CAP alter.ini option
  102. *
  103. * @var bool
  104. */
  105. protected $rmdConsiderCAP = 0;
  106. /**
  107. * Placeholder for CAP_DAYLIMIT alter.ini option
  108. *
  109. * @var int
  110. */
  111. protected $rmdCAPDayLimit = 0;
  112. /**
  113. * Placeholder for REMINDER_DAYS_THRESHOLD_CAP alter.ini option
  114. *
  115. * @var int
  116. */
  117. protected $rmdDaysThresholdCAP = 0;
  118. /**
  119. * Placeholder for REMINDER_TEMPLATE_CAP alter.ini option
  120. *
  121. * @var string
  122. */
  123. protected $rmdTemplateCAP = '';
  124. /**
  125. * Placeholder for REMINDER_CONSIDER_FROZEN alter.ini option
  126. *
  127. * @var bool
  128. */
  129. protected $rmdConsiderFrozen = 0;
  130. /**
  131. * Placeholder for REMINDER_DAYS_THRESHOLD_FROZEN alter.ini option
  132. *
  133. * @var int
  134. */
  135. protected $rmdDaysThresholdFrozen = 0;
  136. /**
  137. * Placeholder for REMINDER_TEMPLATE_FROZEN alter.ini option
  138. *
  139. * @var string
  140. */
  141. protected $rmdTemplateFrozen = '';
  142. /**
  143. * Placeholder for REMINDER_DEBUG_ENABLED alter.ini option
  144. *
  145. * @var bool
  146. */
  147. protected $rmdDebugON = false;
  148. /**
  149. * Placeholder for REMINDER_PRIVATBANK_INVOICE_PUSH alter.ini option
  150. *
  151. * @var bool
  152. */
  153. protected $rmdPrivatBankInvoicesON = false;
  154. /**
  155. * Placeholder for REMINDER_PBI_AUTH_LOGIN alter.ini option
  156. *
  157. * @var string
  158. */
  159. protected $rmdPBIAuthLogin = '';
  160. /**
  161. * Placeholder for REMINDER_PBI_URL alter.ini option
  162. *
  163. * @var string
  164. */
  165. protected $rmdPBIURL= '';
  166. /**
  167. * Placeholder for REMINDER_PBI_ONLY_TAG_ID alter.ini option
  168. *
  169. * @var int
  170. */
  171. protected $rmdPBIOnlyTagID = 0;
  172. /**
  173. * Placeholder for REMINDER_PBI_AND_SMS_TAG_ID alter.ini option
  174. *
  175. * @var int
  176. */
  177. protected $rmdPBIAndSMSTagID = 0;
  178. /**
  179. * Placeholder for REMINDER_PBI_DAY_TARIFF_MULTIPLIER alter.ini option
  180. *
  181. * @var int
  182. */
  183. protected $rmdPBIDayTariffMultiplier = 1;
  184. /**
  185. * Placeholder for REMINDER_PBI_USER_FILTER_PAYSYS_LIST alter.ini option
  186. *
  187. * @var array
  188. */
  189. protected $rmdPBIUserFilterPaysysList = '';
  190. /**
  191. * Contains array of user logins filtered by OpenPayz payment systems listed in $rmdPBIUserFilterPaysysList
  192. *
  193. * @var array
  194. */
  195. protected $rmdPBIPaysysFilteredUsersList = array();
  196. /**
  197. * Contains data of the "contragents" which have PRIVAT_INVOICE_PUSH service in their "external info"
  198. *
  199. * @var array
  200. */
  201. protected $rmdPBIContragentsData = array();
  202. /**
  203. * Placeholder for UbillingConfig object
  204. *
  205. * @var null
  206. */
  207. protected $ubConfig = null;
  208. /**
  209. * Placeholder for MobilesExt object
  210. *
  211. * @var null
  212. */
  213. protected $extMobilesObj = null;
  214. /**
  215. * OMAEURL instance placeholder
  216. *
  217. * @var null
  218. */
  219. protected $omaeURL = null;
  220. /**
  221. * OMAEURL verbose logging stream
  222. *
  223. * @var string
  224. */
  225. protected $omaeVerboseLoggingStream = '';
  226. const FLAGPREFIX = 'exports/REMINDER.';
  227. const CREDITPREFIX = 'CREDIT.';
  228. const CAPPREFIX = 'CAP.';
  229. const FROZENPREFIX = 'FROZEN.';
  230. const PI_INVOICE = 'PRIVATBANK_INVOICE';
  231. const OPAYZ_TRANSACTIONS_TABLE = 'op_transactions';
  232. const OPAYZ_CUSTOMERS_TABLE = 'op_customers';
  233. const CONTRAGENTS_SQL_WHERE_RAW = " `internal_paysys_name` = 'PRIVAT_INVOICE_PUSH' ";
  234. const OMAEURL_DEBUG_FILE = 'exports/REMINDER_OMAEURL_DEBUG';
  235. /**
  236. * it's a magic
  237. */
  238. public function __construct() {
  239. global $ubillingConfig;
  240. $this->ubConfig = $ubillingConfig;
  241. //$this->loadAlter();
  242. $this->loadOptions();
  243. $this->loadAllTemplates();
  244. $this->loadRemindLogin();
  245. $this->sms = new UbillingSMS();
  246. $this->extMobilesObj = new MobilesExt();
  247. $this->money = new FundsFlow();
  248. $this->money->runDataLoders();
  249. if ($this->rmdPrivatBankInvoicesON) {
  250. if (empty($this->rmdPBIAuthLogin) or empty($this->rmdPBIURL)) {
  251. $this->debugReminderRAW('ERROR: PRIVAT INVOICE service intended for use, but no login/URL provided - thus regular SMSes will be used');
  252. $this->rmdPrivatBankInvoicesON = false;
  253. } else {
  254. $this->getUsersFilteredByPaysys();
  255. $this->rmdPBIContragentsData = zb_GetAgentExtInfo('', '', true, self::CONTRAGENTS_SQL_WHERE_RAW, 'agentid');
  256. }
  257. }
  258. }
  259. protected function initOmaeURL() {
  260. $this->omaeURL = new OmaeUrl($this->rmdPBIURL);
  261. $this->omaeURL->setVerboseLog($this->rmdDebugON, self::OMAEURL_DEBUG_FILE);
  262. $this->omaeURL->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  263. $this->omaeURL->setOpt(CURLOPT_USERPWD, $this->rmdPBIAuthLogin);
  264. $this->omaeURL->setOpt(CURLOPT_POST, true);
  265. $this->omaeURL->dataHeader('Content-type', 'application/json;charset=utf-8');
  266. }
  267. /**
  268. * Loads essential options values
  269. *
  270. * @throws Exception
  271. */
  272. protected function loadOptions() {
  273. $this->rmdMode = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_ENABLED'), 'int');
  274. $this->rmdTagID = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_TAGID'), 'int');
  275. $this->rmdDaysThreshold = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_DAYS_THRESHOLD'), 'int');
  276. $this->rmdUseExtMobiles = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_USE_EXTMOBILES'), 'fi', FILTER_VALIDATE_BOOLEAN);
  277. $this->rmdDebugON = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_DEBUG_ENABLED'), 'fi', FILTER_VALIDATE_BOOLEAN);
  278. $this->rmdConsiderCredits = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_CONSIDER_CREDIT'), 'fi', FILTER_VALIDATE_BOOLEAN);
  279. $this->rmdDaysThresholdCredit = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_DAYS_THRESHOLD_CREDIT'), 'int');
  280. $this->rmdCAPDayLimit = ubRouting::filters($this->ubConfig->getAlterParam('CAP_DAYLIMIT'), 'int');
  281. $this->rmdConsiderCAP = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_CONSIDER_CAP'), 'fi', FILTER_VALIDATE_BOOLEAN);
  282. $this->rmdDaysThresholdCAP = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_DAYS_THRESHOLD_CAP'), 'int');
  283. $this->rmdConsiderFrozen = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_CONSIDER_FROZEN'), 'fi', FILTER_VALIDATE_BOOLEAN);
  284. $this->rmdDaysThresholdFrozen = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_DAYS_THRESHOLD_FROZEN'), 'int');
  285. $this->rmdPhonePrefix = $this->ubConfig->getAlterParam('REMINDER_PREFIX');
  286. $this->rmdPhonePrefix = empty($this->rmdPhonePrefix) ? '' : $this->rmdPhonePrefix;
  287. $this->rmdTemplate = $this->ubConfig->getAlterParam('REMINDER_TEMPLATE');
  288. $this->rmdTemplateCredit = $this->ubConfig->getAlterParam('REMINDER_TEMPLATE_CREDIT');
  289. $this->rmdTemplateCAP = $this->ubConfig->getAlterParam('REMINDER_TEMPLATE_CAP');
  290. $this->rmdTemplateFrozen = $this->ubConfig->getAlterParam('REMINDER_TEMPLATE_FROZEN');
  291. $this->rmdForceTranslit = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_FORCE_TRANSLIT', true), 'fi', FILTER_VALIDATE_BOOLEAN);
  292. // PrivatBank Invoices options
  293. $this->rmdPrivatBankInvoicesON = $this->ubConfig->getAlterParam('REMINDER_PRIVATBANK_INVOICE_PUSH', false);
  294. $this->rmdPBIAuthLogin = $this->ubConfig->getAlterParam('REMINDER_PBI_AUTH_LOGIN', '');
  295. $this->rmdPBIURL = $this->ubConfig->getAlterParam('REMINDER_PBI_URL', '');
  296. $this->rmdPBIOnlyTagID = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_PBI_ONLY_TAG_ID', 0), 'int');
  297. $this->rmdPBIAndSMSTagID = ubRouting::filters($this->ubConfig->getAlterParam('REMINDER_PBI_AND_SMS_TAG_ID', 0), 'int');
  298. $this->rmdPBIDayTariffMultiplier = $this->ubConfig->getAlterParam('REMINDER_PBI_DAY_TARIFF_MULTIPLIER', 1);
  299. $this->rmdPBIUserFilterPaysysList = $this->ubConfig->getAlterParam('REMINDER_PBI_USER_FILTER_PAYSYS_LIST', '');
  300. if (!ubRouting::filters($this->ubConfig->getAlterParam('CAP_ENABLED'), 'fi', FILTER_VALIDATE_BOOLEAN)
  301. or empty($this->rmdCAPDayLimit)) {
  302. $this->rmdConsiderCAP = false;
  303. log_register('REMINDER WARNING: CAP CONSIDERING DISABLED BECAUSE CAP SERVICE IS OFF');
  304. }
  305. if (!ubRouting::filters($this->ubConfig->getAlterParam('FREEZE_DAYS_CHARGE_ENABLED'), 'fi', FILTER_VALIDATE_BOOLEAN)) {
  306. $this->rmdConsiderFrozen = false;
  307. log_register('REMINDER WARNING: FROZEN CONSIDERING DISABLED BECAUSE FREEZE DAYS CHARGE SERVICE IS OFF');
  308. }
  309. if (empty($this->rmdTemplate)) {
  310. $this->rmdTemplate = '';
  311. log_register('REMINDER WARNING: TEMPLATE IS EMPTY');
  312. }
  313. if (empty($this->rmdTemplateCredit)) {
  314. $this->rmdTemplateCredit = '';
  315. if ($this->rmdConsiderCredits) {
  316. log_register('REMINDER WARNING: CREDIT TEMPLATE IS EMPTY');
  317. }
  318. }
  319. if (empty($this->rmdTemplateCAP)) {
  320. $this->rmdTemplateCAP = '';
  321. if ($this->rmdConsiderCAP) {
  322. log_register('REMINDER WARNING: CAP TEMPLATE IS EMPTY');
  323. }
  324. }
  325. if (empty($this->rmdTemplateFrozen)) {
  326. $this->rmdTemplateFrozen = '';
  327. if ($this->rmdConsiderFrozen) {
  328. log_register('REMINDER WARNING: FROZEN TEMPLATE IS EMPTY');
  329. }
  330. }
  331. }
  332. /**
  333. * Load all users templates
  334. *
  335. * @return void
  336. */
  337. protected function loadAllTemplates() {
  338. $this->AllTemplates = zb_TemplateGetAllUserData();
  339. }
  340. /**
  341. * load all logins whith cash >=0 and with set tagid to $alllogin
  342. *
  343. * @return void
  344. */
  345. protected function loadRemindLogin() {
  346. if (!empty($this->rmdTagID)) {
  347. $creditFields = '';
  348. $capFields = '';
  349. $capJOIN = '';
  350. $frozenFields = '';
  351. $frozenJOIN = '';
  352. $pbiFields = '';
  353. $pbiJOIN = '';
  354. $whereString = " WHERE `users`.`Passive` != '1' ";
  355. // check if credits considering enabled
  356. if ($this->rmdConsiderCredits > 0) {
  357. $creditFields = " `users`.`Credit`, `users`.`CreditExpire`, ";
  358. }
  359. // check if CAP considering enabled
  360. if ($this->rmdConsiderCAP) {
  361. $capFields = " `capdata`.`days`, ";
  362. $capJOIN = " LEFT JOIN `capdata` ON `t_login`.`login` = `capdata`.`login`
  363. AND `capdata`.`days` < " . $this->rmdCAPDayLimit;
  364. }
  365. // check if frozen considering enabled
  366. if ($this->rmdConsiderFrozen) {
  367. $whereString = "";
  368. $frozenFields = " `users`.`Passive`, `frozen_charge_days`.`freeze_days_amount`, `frozen_charge_days`.`freeze_days_used`, ";
  369. $frozenJOIN = " LEFT JOIN `frozen_charge_days` ON `t_login`.`login` = `frozen_charge_days`.`login`
  370. AND `frozen_charge_days`.`freeze_days_used` < `frozen_charge_days`.`freeze_days_amount` ";
  371. }
  372. // add some PBI SQL clauses, if ON
  373. if ($this->rmdPrivatBankInvoicesON) {
  374. $pbiFields = " `pbi_tags`.`pbi_only`, `pbi_tags`.`pbi_sms`, ";
  375. $pbiJOIN = " LEFT JOIN (SELECT IF(`tagid` = " . $this->rmdPBIOnlyTagID . ", 1, 0) AS `pbi_only`,
  376. IF(`tagid` = " . $this->rmdPBIAndSMSTagID . ", 1, 0) AS `pbi_sms`,
  377. `login`
  378. FROM `tags`
  379. WHERE `tagid` IN (" . $this->rmdPBIOnlyTagID . ", " . $this->rmdPBIAndSMSTagID . ")
  380. ) AS `pbi_tags` ON `t_login`.`login` = `pbi_tags`.`login` \n";
  381. }
  382. $query = "
  383. SELECT `users`.`login`, `users`.`Cash`, " . $creditFields . $capFields . $frozenFields . $pbiFields . " `phones`.`mobile`
  384. FROM (SELECT `tags`.`login` FROM `tags` WHERE tags.tagid = '" . $this->rmdTagID . "') as t_login
  385. INNER JOIN `users` ON `t_login`.`login` = `users`.`login`
  386. INNER JOIN (SELECT `phones`.`login`, `phones`.`mobile` FROM `phones`) `phones` ON `t_login`.`login` = `phones`.`login` \n"
  387. . $capJOIN
  388. . $frozenJOIN
  389. . $pbiJOIN
  390. . $whereString;
  391. $tmp = simple_queryall($query);
  392. if (!empty($tmp)) {
  393. $this->AllLogin = $tmp;
  394. }
  395. } else {
  396. log_register('REMINDER FAILED: EMPTY TAG ID');
  397. }
  398. }
  399. /**
  400. * Creates a new remind message actually
  401. *
  402. * @param string $login
  403. * @param array $numbers
  404. * @param string $filePrefix
  405. * @param bool $forced
  406. * @param string $remindTemplate
  407. */
  408. protected function createRemindMsg($login, $numbers, $filePrefix, $forced = false, $remindTemplate = '') {
  409. if (!empty($numbers)) {
  410. $template = (empty($remindTemplate)) ? $this->rmdTemplate : $remindTemplate;
  411. if (!empty($template)) {
  412. $message = zb_TemplateReplace($login, $template, $this->AllTemplates);
  413. if (!empty($message)) {
  414. foreach ($numbers as $number) {
  415. $number = trim($number);
  416. $number = str_replace($this->rmdPhonePrefix, '', $number);
  417. $number = ubRouting::filters($number, 'int');
  418. $number = $this->rmdPhonePrefix . $number;
  419. $queueFile = $this->sms->sendSMS($number, $message, $this->rmdForceTranslit, 'REMINDER');
  420. $this->sms->setDirection($queueFile, 'user_login', $login);
  421. if ($forced) {
  422. log_register('REMINDER FORCE SEND SMS (' . $login . ') NUMBER `' . $number . '`');
  423. } else {
  424. file_put_contents($filePrefix . $login, '');
  425. }
  426. }
  427. }
  428. }
  429. } else {
  430. log_register('REMINDER EMPTY NUMBER (' . $login . ')');
  431. }
  432. }
  433. protected function createPBInvoice($login) {
  434. $invoiceArray = array();
  435. $userAddress = empty($this->AllTemplates[$login]['address']) ? '' : $this->AllTemplates[$login]['address'];
  436. $userAgent = zb_AgentAssignedGetDataFast($login, $userAddress);
  437. if (!empty($this->rmdPBIContragentsData[$userAgent['id']])) {
  438. $userAgentFullData = $this->rmdPBIContragentsData[$userAgent['id']];
  439. $userTariff = $this->AllTemplates[$login]['tariff'];
  440. $userTariffData = zb_TariffGetData($userTariff);
  441. $userTariffPeriod = $userTariffData['period'];
  442. $userTariffPrice = ($userTariffPeriod == 'day') ? $userTariffData['Fee'] * $this->rmdPBIDayTariffMultiplier : $userTariffData['Fee'];
  443. $invoiceCloseDate = new DateTime('+2 weeks');
  444. $invoiceCloseDate = $invoiceCloseDate->format('Y-m-d');
  445. $userCellPhoneNum = $this->AllTemplates[$login]['mobile'];
  446. $userCellPhoneNum = preg_match('/^(\+38|38)/', $userCellPhoneNum) ? $userCellPhoneNum : $this->rmdPhonePrefix . $userCellPhoneNum;
  447. $invoiceArray = array(
  448. 'invoicetype' => 'S',
  449. 'comctype' => '8',
  450. 'company' => $userAgentFullData['paysys_secret_key'],
  451. 'companyid' => $userAgentFullData['internal_paysys_id'],
  452. 'servicecod' => $userAgentFullData['internal_paysys_srv_id'],
  453. 'serviceid' => $userAgentFullData['paysys_token'],
  454. 'invname' => $userAgentFullData['paysys_secret_key'],
  455. 'destname' => $userAgentFullData['paysys_password'],
  456. 'mfod' => $userAgentFullData['bankcode'],
  457. 'okpod' => $userAgentFullData['edrpo'],
  458. 'amount' => floatval(number_format($userTariffPrice, 2, '.', '')),
  459. 'clphone' => $userCellPhoneNum,
  460. 'invclosingdate' => $invoiceCloseDate,
  461. 'extparams' => array('param' => array(array(
  462. 'name' => 'bill_identifier',
  463. 'value' => $this->AllTemplates[$login]['contract']
  464. )
  465. )
  466. )
  467. );
  468. $this->debugReminderRAW('invoiceArray ' . print_r($invoiceArray, true));
  469. $invoiceArray = json_encode($invoiceArray);
  470. $this->debugReminderRAW('invoiceJSON ' . $invoiceArray);
  471. }
  472. return ($invoiceArray);
  473. }
  474. /**
  475. * Make queue for sms send
  476. *
  477. * @return void
  478. */
  479. public function remindUsers() {
  480. if ($this->rmdPrivatBankInvoicesON) {
  481. $this->initOmaeURL();
  482. }
  483. $paysysFilterForUsersNeeded = !empty($this->rmdPBIUserFilterPaysysList);
  484. $paysysFilteredUsersFound = !empty($this->rmdPBIPaysysFilteredUsersList);
  485. $this->debugReminderRAW('rmdPrivatBankInvoicesON: ' . $this->rmdPrivatBankInvoicesON);
  486. $this->debugReminderRAW('rmdPBIUserFilterPaysysList: ' . $this->rmdPBIUserFilterPaysysList);
  487. $this->debugReminderRAW('rmdPBIPaysysFilteredUsersList: ' . print_r($this->rmdPBIPaysysFilteredUsersList, true));
  488. if ($paysysFilterForUsersNeeded and !$paysysFilteredUsersFound) {
  489. log_register('REMINDER: NO LOGINS FOUND BY OPAYZ PAYSYS FILTER FOR PRIVATBANK INVOICE');
  490. }
  491. foreach ($this->AllLogin as $userLoginData) {
  492. // yep, we evaluate $liveDays, $liveTime and $cacheTime on every iteration
  493. // 'cause they may be re-assigned below, depending on processing type
  494. $liveDays = $this->rmdDaysThreshold;
  495. $liveTime = $liveDays * 24 * 60 * 60;
  496. $cacheTime = time() - $liveTime;
  497. $eachLogin = $userLoginData['login'];
  498. $numbers = array($userLoginData['mobile']);
  499. $onlineDaysLeft = $this->money->getOnlineLeftCountFast($eachLogin);
  500. if ($this->rmdUseExtMobiles) {
  501. $userExtMobs = $this->extMobilesObj->getUserMobiles($eachLogin, true);
  502. $userExtMobs = (empty($userExtMobs[$eachLogin])) ? array() : $userExtMobs[$eachLogin];
  503. $numbers = $numbers + $userExtMobs;
  504. }
  505. $this->debugReminderRAW('--------');
  506. $this->debugReminderRAW('processing login: (' . $eachLogin . ')');
  507. $this->debugReminderRAW('onlineDaysLeft: ' . $onlineDaysLeft);
  508. $this->debugReminderRAW('CHECKFILE exists: ' . (file_exists(self::FLAGPREFIX . $eachLogin) ? 'YES (user will be skipped from processing)' : 'NO'));
  509. $this->debugReminderRAW('BASE service processing is: ' . (($onlineDaysLeft <= $liveDays and $onlineDaysLeft >= 0 and $this->rmdMode != 2 and empty($userLoginData['Passive'])) ? 'ON' : 'OFF'));
  510. $this->debugReminderRAW('CREDIT EXPIRATION processing is: ' . (($this->rmdConsiderCredits and $onlineDaysLeft == -1 and empty($userLoginData['Passive'])) ? 'ON' : 'OFF'));
  511. $this->debugReminderRAW('CAP users processing is: ' . (($this->rmdConsiderCAP and $onlineDaysLeft == -1 and ! empty($userLoginData['days']) and empty($userLoginData['Passive'])) ? 'ON' : 'OFF'));
  512. $this->debugReminderRAW('FROZEN users processing is: ' . (($this->rmdConsiderFrozen and ! empty($userLoginData['Passive'])) ? 'ON' : 'OFF'));
  513. // process base service expiration
  514. // certain user must not be a debtor and not to be frozen and processing mode must not be equal to 2
  515. if ($onlineDaysLeft <= $liveDays and $onlineDaysLeft >= 0 and $this->rmdMode != 2 and empty($userLoginData['Passive'])) {
  516. if (!file_exists(self::FLAGPREFIX . $eachLogin)) {
  517. // check, if all conditions for sending PB invoice to current user are met
  518. $curUserPBIOnly = !empty($userLoginData['pbi_only']);
  519. $curUserPBISMS = !empty($userLoginData['pbi_sms']);
  520. $needToSendPBI = ($this->rmdPrivatBankInvoicesON
  521. and ($curUserPBIOnly or $curUserPBISMS)
  522. and (!$paysysFilterForUsersNeeded
  523. or ($paysysFilterForUsersNeeded and $paysysFilteredUsersFound))
  524. );
  525. $this->debugReminderRAW('PBI params: pbi_only: ' . $curUserPBIOnly . ' pbi_sms: ' . $curUserPBISMS);
  526. $this->debugReminderRAW('PBI invoice will be send: ' . ($needToSendPBI ? 'YES' : 'NO'));
  527. if ($needToSendPBI) {
  528. $proceedInvoice = true;
  529. $sendResult = '';
  530. // if REMINDER_PBI_USER_FILTER_PAYSYS_LIST is used and we've found some users which fall under the filter -
  531. // check, if current user is in that list
  532. if ($paysysFilterForUsersNeeded) {
  533. $proceedInvoice = in_array($eachLogin, $this->rmdPBIPaysysFilteredUsersList);
  534. }
  535. // PB invoice sending
  536. if ($proceedInvoice) {
  537. $invoice = $this->createPBInvoice($eachLogin);
  538. $this->omaeURL->dataPostRaw($invoice);
  539. $sendResult = $this->omaeURL->response();
  540. file_put_contents(self::FLAGPREFIX . $eachLogin, '');
  541. } elseif ($paysysFilterForUsersNeeded) {
  542. $this->debugReminderRAW(' ERROR sending PB invoice for user: ' . $eachLogin . ' - login not found by OPAYZ PAYSYS filter');
  543. }
  544. // PB invoice send result debugging
  545. $this->debugReminderRAW(' PBI send result ' . $sendResult);
  546. $this->debugReminderRAW(' PBI OMAEURL lastRequestInfo ' . print_r($this->omaeURL->lastRequestInfo(), true));
  547. $this->debugReminderRAW(' PBI OMAEURL error ' . print_r($this->omaeURL->error(), true));
  548. }
  549. if (!$curUserPBIOnly or $curUserPBISMS) {
  550. $this->createRemindMsg($eachLogin, $numbers, self::FLAGPREFIX);
  551. $this->debugReminder('CONSIDER BASE SERVICE', $eachLogin, $userLoginData['Cash'], print_r($numbers, true), $liveDays, $liveTime, $cacheTime, 'online days left: ' . $onlineDaysLeft);
  552. }
  553. }
  554. }
  555. // process credit expiration date
  556. // certain user must be a debtor, and must have active non-expired and non-eternal credit
  557. if ($this->rmdConsiderCredits and $onlineDaysLeft == -1 and empty($userLoginData['Passive'])) {
  558. if (!empty($this->rmdDaysThresholdCredit)) {
  559. $liveDays = $this->rmdDaysThresholdCredit;
  560. $liveTime = $liveDays * 24 * 60 * 60;
  561. $cacheTime = time() - $liveTime;
  562. }
  563. if (!file_exists(self::FLAGPREFIX . self::CREDITPREFIX . $eachLogin)) {
  564. $creditSum = $userLoginData['Credit'];
  565. $creditExpireTime = $userLoginData['CreditExpire'];
  566. if (!empty($creditSum) and empty($creditExpireTime)) {
  567. log_register('REMINDER IGNORING ETERNAL CREDIT FOR (' . $eachLogin . ')');
  568. } else {
  569. $remindStartTime = $creditExpireTime - $liveTime;
  570. $curTime = time();
  571. if ($remindStartTime <= $curTime and $creditExpireTime > $curTime) {
  572. $this->createRemindMsg($eachLogin, $numbers, self::FLAGPREFIX . self::CREDITPREFIX, false, $this->rmdTemplateCredit);
  573. $this->debugReminder('CONSIDER CREDIT', $eachLogin, $userLoginData['Cash'], print_r($numbers, true), $liveDays, $liveTime, $cacheTime, 'remind start time: ' . $remindStartTime . ' current time: ' . $curTime . ' credit expire time: ' . $creditExpireTime);
  574. }
  575. }
  576. }
  577. }
  578. // process CAP users
  579. // certain user must be a debtor, not to be frozen and to have a CAP record
  580. if ($this->rmdConsiderCAP and $onlineDaysLeft == -1 and ! empty($userLoginData['days']) and empty($userLoginData['Passive'])) {
  581. if (!empty($this->rmdDaysThresholdCAP)) {
  582. $liveDays = $this->rmdDaysThresholdCAP;
  583. $liveTime = $liveDays * 24 * 60 * 60;
  584. $cacheTime = time() - $liveTime;
  585. }
  586. if (!file_exists(self::FLAGPREFIX . self::CAPPREFIX . $eachLogin)) {
  587. $capDaysLeft = $this->rmdCAPDayLimit - $userLoginData['days'];
  588. if ($capDaysLeft <= $liveDays) {
  589. $this->createRemindMsg($eachLogin, $numbers, self::FLAGPREFIX . self::CAPPREFIX, false, $this->rmdTemplateCAP);
  590. $this->debugReminder('CONSIDER CAP', $eachLogin, $userLoginData['Cash'], print_r($numbers, true), $liveDays, $liveTime, $cacheTime, 'days till CAP left: ' . $capDaysLeft);
  591. }
  592. }
  593. }
  594. // process frozen users
  595. // certain user must be frozen
  596. if ($this->rmdConsiderFrozen and ! empty($userLoginData['Passive'])
  597. and ! empty($userLoginData['freeze_days_amount']) and ! empty($userLoginData['freeze_days_used'])) {
  598. if (!empty($this->rmdDaysThresholdFrozen)) {
  599. $liveDays = $this->rmdDaysThresholdFrozen;
  600. $liveTime = $liveDays * 24 * 60 * 60;
  601. $cacheTime = time() - $liveTime;
  602. }
  603. if (!file_exists(self::FLAGPREFIX . self::FROZENPREFIX . $eachLogin)) {
  604. $freezeDaysLeft = $userLoginData['freeze_days_amount'] - $userLoginData['freeze_days_used'];
  605. if ($freezeDaysLeft <= $liveDays) {
  606. $this->createRemindMsg($eachLogin, $numbers, self::FLAGPREFIX . self::FROZENPREFIX, false, $this->rmdTemplateFrozen);
  607. $this->debugReminder('CONSIDER FROZEN', $eachLogin, $userLoginData['Cash'], print_r($numbers, true), $liveDays, $liveTime, $cacheTime, 'freeze days left: ' . $freezeDaysLeft);
  608. }
  609. }
  610. }
  611. // make free tariff ignorance notice
  612. if ($onlineDaysLeft == -2) {
  613. log_register('REMINDER IGNORING FREE TARIFF (' . $eachLogin . ')');
  614. }
  615. $this->checkFlagFiles($eachLogin, $cacheTime);
  616. }
  617. }
  618. /**
  619. * Make queue for sms send for all users with remind tag
  620. *
  621. * @return void
  622. */
  623. public function forceRemind() {
  624. if ($this->rmdPrivatBankInvoicesON) {
  625. $this->initOmaeURL();
  626. }
  627. $paysysFilterForUsersNeeded = !empty($this->rmdPBIUserFilterPaysysList);
  628. $paysysFilteredUsersFound = !empty($this->rmdPBIPaysysFilteredUsersList);
  629. $this->debugReminderRAW('rmdPrivatBankInvoicesON ' . $this->rmdPrivatBankInvoicesON);
  630. $this->debugReminderRAW('rmdPBIUserFilterPaysysList ' . $this->rmdPBIUserFilterPaysysList);
  631. $this->debugReminderRAW('rmdPBIPaysysFilteredUsersList ' . print_r($this->rmdPBIPaysysFilteredUsersList, true));
  632. if ($paysysFilterForUsersNeeded and !$paysysFilteredUsersFound) {
  633. log_register('REMINDER FORCED: NO LOGINS FOUND BY OPAYZ PAYSYS FILTER FOR PRIVATBANK INVOICE');
  634. }
  635. foreach ($this->AllLogin as $userLoginData) {
  636. $eachLogin = $userLoginData['login'];
  637. $numbers = array($userLoginData['mobile']);
  638. if ($this->rmdUseExtMobiles) {
  639. $userExtMobs = $this->extMobilesObj->getUserMobiles($eachLogin, true);
  640. $userExtMobs = (empty($userExtMobs[$eachLogin])) ? array() : $userExtMobs[$eachLogin];
  641. $numbers = $numbers + $userExtMobs;
  642. }
  643. // check, if all conditions for sending PB invoice to current user are met
  644. $curUserPBIOnly = !empty($userLoginData['pbi_only']);
  645. $curUserPBISMS = !empty($userLoginData['pbi_sms']);
  646. $needToSendPBI = ($this->rmdPrivatBankInvoicesON
  647. and ($curUserPBIOnly or $curUserPBISMS)
  648. and (!$paysysFilterForUsersNeeded
  649. or ($paysysFilterForUsersNeeded and $paysysFilteredUsersFound))
  650. );
  651. $this->debugReminderRAW('--------');
  652. $this->debugReminderRAW('processing login: (' . $eachLogin . ')');
  653. $this->debugReminderRAW('PBI params: pbi_only: ' . $curUserPBIOnly . ' pbi_sms: ' . $curUserPBISMS);
  654. $this->debugReminderRAW('PBI invoice will be send: ' . ($needToSendPBI ? 'YES' : 'NO'));
  655. if ($needToSendPBI) {
  656. $proceedInvoice = true;
  657. $sendResult = '';
  658. // if REMINDER_PBI_USER_FILTER_PAYSYS_LIST is used and we've found some users which fall under the filter -
  659. // check, if current user is in that list
  660. if ($paysysFilterForUsersNeeded) {
  661. $proceedInvoice = in_array($eachLogin, $this->rmdPBIPaysysFilteredUsersList);
  662. }
  663. // PB invoice sending
  664. if ($proceedInvoice) {
  665. $invoice = $this->createPBInvoice($eachLogin);
  666. $this->omaeURL->dataPostRaw($invoice);
  667. $sendResult = $this->omaeURL->response();
  668. } elseif ($paysysFilterForUsersNeeded) {
  669. $this->debugReminderRAW(' ERROR sending PB invoice for user: ' . $eachLogin . ' - login not found by OPAYZ PAYSYS filter');
  670. }
  671. // PB invoice send result debugging
  672. $this->debugReminderRAW(' PBI send result ' . $sendResult);
  673. $this->debugReminderRAW(' PBI OMAEURL lastRequestInfo ' . print_r($this->omaeURL->lastRequestInfo(), true));
  674. $this->debugReminderRAW(' PBI OMAEURL error ' . print_r($this->omaeURL->error(), true));
  675. }
  676. if (!$curUserPBIOnly or $curUserPBISMS) {
  677. $this->createRemindMsg($eachLogin, $numbers, '', true);
  678. }
  679. }
  680. }
  681. /**
  682. * Checks if user's flag file exists and it's lifetime expired and the file needs to be removed
  683. *
  684. * @param $login
  685. * @param $cacheTime
  686. *
  687. * @return void
  688. */
  689. public function checkFlagFiles($login, $cacheTime) {
  690. $flagFilePaths = array(self::FLAGPREFIX . $login,
  691. self::FLAGPREFIX . self::CREDITPREFIX . $login,
  692. self::FLAGPREFIX . self::CAPPREFIX . $login,
  693. self::FLAGPREFIX . self::FROZENPREFIX . $login
  694. );
  695. foreach ($flagFilePaths as $filePath) {
  696. if (file_exists($filePath)) {
  697. if ($cacheTime > filemtime($filePath)) {
  698. unlink($filePath);
  699. }
  700. }
  701. }
  702. }
  703. /**
  704. * Retrieves user logins from OPAYZ_CUSTOMERS_TABLE which are filtered by $this->rmdPBIUserFilterPaysysList
  705. *
  706. * @return array|string[]
  707. */
  708. protected function getUsersFilteredByPaysys() {
  709. $result = array();
  710. if (!empty($this->rmdPBIUserFilterPaysysList)) {
  711. $whereStr = zb_DelimitedStringToSQLWHEREIN($this->rmdPBIUserFilterPaysysList, ',', true);
  712. if (!empty($whereStr)) {
  713. $opCustomersTable = new NyanORM(self::OPAYZ_CUSTOMERS_TABLE);
  714. $opCustomersTable->selectable('realid');
  715. $opCustomersTable->joinOn('INNER', self::OPAYZ_TRANSACTIONS_TABLE,
  716. self::OPAYZ_CUSTOMERS_TABLE . ".`virtualid` = " .
  717. self::OPAYZ_TRANSACTIONS_TABLE . ".`customerid`" .
  718. " AND " . self::OPAYZ_TRANSACTIONS_TABLE . ".`paysys` IN (" . $whereStr . ") "
  719. );
  720. $result = $opCustomersTable->getAll('realid', true, true);
  721. $result = array_keys($result);
  722. }
  723. $this->rmdPBIPaysysFilteredUsersList = $result;
  724. }
  725. return ($result);
  726. }
  727. /**
  728. * Provides debugging of reminder processing
  729. *
  730. * @param $topic
  731. * @param $login
  732. * @param $cash
  733. * @param $phones
  734. * @param $liveDays
  735. * @param $liveTime
  736. * @param $cacheTime
  737. * @param $leftTime
  738. */
  739. protected function debugReminder($topic, $login, $cash, $phones, $liveDays, $liveTime, $cacheTime, $leftTime) {
  740. if ($this->rmdDebugON) {
  741. log_register('REMINDER ' . $topic);
  742. log_register('REMINDER login: (' . $login . '). Cash: ' . $cash);
  743. log_register('REMINDER phones: ' . print_r($phones, true));
  744. log_register('REMINDER liveDays: ' . $liveDays);
  745. log_register('REMINDER liveTime: ' . $liveTime);
  746. log_register('REMINDER cacheTime: ' . $cacheTime);
  747. log_register('REMINDER ' . $leftTime);
  748. }
  749. }
  750. /**
  751. * Provides simple debugging of reminder processing
  752. *
  753. * @param $logmsg
  754. *
  755. * @return void
  756. */
  757. private function debugReminderRAW($logmsg) {
  758. if ($this->rmdDebugON) { log_register('REMINDER: ' . $logmsg); }
  759. }
  760. }