api.vservices.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. <?php
  2. /**
  3. * Deletes virtual service from database
  4. *
  5. * @param int $vservId
  6. *
  7. * @return void
  8. */
  9. function zb_VsericeDelete($vservId) {
  10. $vservId = ubRouting::filters($vservId, 'int');
  11. $vservDb = new NyanORM('vservices');
  12. $vservDb->where('id', '=', $vservId);
  13. $vservDb->delete();
  14. log_register('VSERVICE DELETE [' . $vservId . ']');
  15. }
  16. /**
  17. * Gets all available virtual services from database
  18. *
  19. * @return array
  20. */
  21. function zb_VserviceGetAllData($excludeArchived = false, $onlyArchived = false) {
  22. $vservDb = new NyanORM('vservices');
  23. if ($excludeArchived and $onlyArchived) {
  24. // No, no, no, no
  25. // Don't phunk with my heart
  26. } elseif ($excludeArchived) {
  27. $vservDb->where('archived', '=', '0');
  28. } elseif ($onlyArchived) {
  29. $vservDb->where('archived', '=', '1');
  30. }
  31. $result = $vservDb->getAll();
  32. return ($result);
  33. }
  34. /**
  35. * Returns array of virtual services names as vserviceId=>tagName
  36. *
  37. * @return array
  38. */
  39. function zb_VservicesGetAllNames() {
  40. $result = array();
  41. $allservices = zb_VserviceGetAllData();
  42. $alltagnames = stg_get_alltagnames();
  43. if (!empty($allservices)) {
  44. foreach ($allservices as $io => $eachservice) {
  45. @$result[$eachservice['id']] = $alltagnames[$eachservice['tagid']];
  46. }
  47. }
  48. return ($result);
  49. }
  50. /**
  51. * Returns array of available virtualservices as Service:id=>tagname
  52. *
  53. * @return array
  54. */
  55. function zb_VservicesGetAllNamesLabeled() {
  56. $result = array();
  57. $allservices = zb_VserviceGetAllData();
  58. $alltagnames = stg_get_alltagnames();
  59. if (!empty($allservices)) {
  60. foreach ($allservices as $io => $eachservice) {
  61. @$result['Service:' . $eachservice['id']] = $alltagnames[$eachservice['tagid']];
  62. }
  63. }
  64. return ($result);
  65. }
  66. /**
  67. * Returns virtual service creation form
  68. *
  69. * @return string
  70. */
  71. function web_VserviceAddForm() {
  72. global $ubillingConfig;
  73. $serviceFeeTypes = array('stargazer' => __('stargazer user cash'));
  74. if ($ubillingConfig->getAlterParam('VCASH_ENABLED')) {
  75. $serviceFeeTypes['virtual'] = __('virtual services cash');
  76. }
  77. $inputs = wf_Selector('newtagid', stg_tagid_selector(true), __('Tag'), '', false, false, '', '', '', true);
  78. $inputs .= wf_Selector('newcashtype', $serviceFeeTypes, __('Cash type'), '', false, false, '', '', '', true);
  79. $inputs .= wf_Selector('newpriority', web_priority_selector(6, true), __('Priority'), '', false, false, '', '', '', true);
  80. $inputs .= wf_TextInput('newfee', __('Fee'), '', false, '', '', '', '', '', true);
  81. $inputs .= wf_TextInput('newperiod', __('Charge period in days'), '', false, '', '', '', '', '', true);
  82. $inputs .= wf_TextInput('newexcludetags', __('Excluded user tags') . '*', '', false, '', '', '', '', '', true);
  83. $inputs .= wf_tag('span', false, 'full-width-occupy');
  84. $inputs .= '*' . __('Users with this tag IDs will be excluded form current service processing. Tag IDs should be separated with coma.');
  85. $inputs .= wf_tag('span', true);
  86. $inputs .= wf_CheckInput('feechargealways', __('Always charge fee, even if balance cash < 0'), false, false, '', '', 'style="margin-left: auto;"', 'style="text-align: left;"');
  87. $inputs .= wf_CheckInput('newarchived', __('Mark the service as "Archived"'), false, false, '', '', 'style="margin-left: auto;"', 'style="text-align: left;"');
  88. $inputs .= wf_SubmitClassed(true, 'ubButton', '', __('Create'));
  89. $result = wf_Form("", 'POST', $inputs, 'glamour form-grid-2cols form-grid-2cols-label-right labels-top');
  90. return($result);
  91. }
  92. /**
  93. * Returns virtual service editing form
  94. *
  95. * @param int $vserviceId
  96. *
  97. * @return string
  98. */
  99. function web_VserviceEditForm($vserviceId) {
  100. $vserviceId = ubRouting::filters($vserviceId, 'int');
  101. $allservicesRaw = zb_VserviceGetAllData();
  102. $serviceData = array();
  103. $result = '';
  104. if (!empty($allservicesRaw)) {
  105. foreach ($allservicesRaw as $io => $each) {
  106. if ($each['id'] == $vserviceId) {
  107. $serviceData = $each;
  108. }
  109. }
  110. }
  111. if (!empty($serviceData)) {
  112. $serviceFeeTypes = array('stargazer' => __('stargazer user cash'), 'virtual' => __('virtual services cash'));
  113. $allTags = stg_get_alltagnames();
  114. $priorities = array();
  115. for ($i = 6; $i >= 1; $i--) {
  116. $priorities[$i] = $i;
  117. }
  118. $feeIsChargedAlways = ($serviceData['fee_charge_always'] == 1) ? true : false;
  119. $isArchived = ($serviceData['archived'] == 1) ? true : false;
  120. $inputs = wf_Selector('edittagid', $allTags, __('Tag'), $serviceData['tagid'], false, false, '', '', '', true);
  121. $inputs .= wf_Selector('editcashtype', $serviceFeeTypes, __('Cash type'), $serviceData['cashtype'], false, false, '', '', '', true);
  122. $inputs .= wf_Selector('editpriority', $priorities, __('Priority'), $serviceData['priority'], false, false, '', '', '', true);
  123. $inputs .= wf_TextInput('editfee', __('Fee'), $serviceData['price'], false, '5', '', '', '', '', true);
  124. $inputs .= wf_TextInput('editperiod', __('Charge period in days'), $serviceData['charge_period_days'], false, '5', 'digits', '', '', '', true);
  125. $inputs .= wf_TextInput('editexcludetags', __('Excluded user tags'), $serviceData['exclude_tags'], false, '5', 'digits', '', '', '', true);
  126. $inputs .= wf_tag('span', false, 'full-width-occupy');
  127. $inputs .= '*' . __('Users with this tag IDs will be excluded form current service processing. Tag IDs should be separated with coma.');
  128. $inputs .= wf_tag('span', true);
  129. $inputs .= wf_CheckInput('editfeechargealways', __('Always charge fee, even if balance cash < 0'), false, $feeIsChargedAlways, '', '', 'style="margin-left: auto;"', 'style="text-align: left;"');
  130. $inputs .= wf_CheckInput('editarchived', __('Mark the service as "Archived"'), false, $isArchived, '', '', 'style="margin-left: auto;"', 'style="text-align: left;"');
  131. $inputs .= wf_SubmitClassed(true, 'ubButton', '', __('Save'));
  132. $result .= wf_Form("", 'POST', $inputs, 'glamour form-grid-2cols form-grid-2cols-label-right labels-top');
  133. $result .= wf_delimiter();
  134. $result .= wf_BackLink('?module=vservices');
  135. } else {
  136. $messages = new UbillingMessageHelper();
  137. $result .= $messages->getStyledMessage(__('Virtual service') . ' [' . $vserviceId . '] ' . __('Not exists'), 'error');
  138. $result .= wf_delimiter();
  139. $result .= wf_BackLink('?module=vservices');
  140. }
  141. return($result);
  142. }
  143. /**
  144. * Shows available virtual services list with some controls
  145. *
  146. * @return void
  147. */
  148. function web_VservicesShow() {
  149. $allvservices = zb_VserviceGetAllData();
  150. $allTagTypes = stg_get_alltagnames();
  151. //construct editor
  152. $titles = array(
  153. 'ID',
  154. 'Tag',
  155. 'Fee',
  156. 'Cash type',
  157. 'Priority',
  158. 'Always charge fee',
  159. 'Charge period in days',
  160. 'Excluded user tags',
  161. 'Archived',
  162. );
  163. $keys = array('id',
  164. 'tagid',
  165. 'price',
  166. 'cashtype',
  167. 'priority',
  168. 'fee_charge_always',
  169. 'charge_period_days',
  170. 'exclude_tags',
  171. 'archived'
  172. );
  173. show_window(__('Virtual services'), web_GridEditorVservices($titles, $keys, $allvservices, 'vservices'));
  174. if (!empty($allTagTypes)) {
  175. show_window(__('Add virtual service'), web_VserviceAddForm());
  176. } else {
  177. $messages = new UbillingMessageHelper();
  178. show_window('', $messages->getStyledMessage(__('Any user tags not exists'), 'warning'));
  179. }
  180. }
  181. /**
  182. * Returns virtual services editor grid
  183. *
  184. * @param array $titles
  185. * @param array $keys
  186. * @param array $alldata
  187. * @param string $module
  188. *
  189. * @return string
  190. */
  191. function web_GridEditorVservices($titles, $keys, $alldata, $module) {
  192. $result = '';
  193. $messages = new UbillingMessageHelper();
  194. if (!empty($alldata)) {
  195. $alltagnames = stg_get_alltagnames();
  196. $cells = '';
  197. foreach ($titles as $eachtitle) {
  198. $cells .= wf_TableCell(__($eachtitle), '', '', 'style="height: 20px; font-weight: 600; text-align: center;"');
  199. }
  200. $cells .= wf_TableCell(__('Actions'), '', '', 'style="height: 20px; font-weight: 600; text-align: center;"');
  201. $rows = wf_TableRow($cells, 'row1');
  202. foreach ($alldata as $io => $eachdata) {
  203. $cells = '';
  204. foreach ($keys as $eachkey) {
  205. $curTagID = $eachdata['tagid'];
  206. if (array_key_exists($eachkey, $eachdata)) {
  207. switch ($eachkey) {
  208. case 'tagid':
  209. @$tagname = $alltagnames[$curTagID];
  210. $cells .= wf_TableCell(wf_Link('?module=tagcloud&tagid=' . $curTagID, $tagname));
  211. break;
  212. case 'charge_period_days':
  213. $cells .= wf_TableCell($eachdata[$eachkey], '240px', '', 'style="text-align: center;"');
  214. break;
  215. case 'fee_charge_always':
  216. $cells .= wf_TableCell(web_bool_led($eachdata[$eachkey]), '210px', '', 'style="text-align: center;"');
  217. break;
  218. case 'archived':
  219. $cells .= wf_TableCell(web_bool_led($eachdata[$eachkey]), '', '', 'style="text-align: center;"');
  220. break;
  221. case 'exclude_tags':
  222. $cell_links = '';
  223. $excludedTagsArr = zb_DelimitedStringToArray($eachdata['exclude_tags']);
  224. foreach ($excludedTagsArr as $eachID) {
  225. if (array_key_exists($eachID, $alltagnames)) {
  226. $cell_links .= wf_nbsp(2) . wf_Link('?module=tagcloud&tagid=' . $eachID, $alltagnames[$eachID], true, '', 'style="line-height: 20px;"');
  227. } else {
  228. $cell_links .= wf_nbsp(2) . wf_Link('?module=tagcloud&tagid=' . $eachID, __('Non-existent tag with ID') . ': ' . $eachID, true, '', 'style="color: darkred; line-height: 20px;"');
  229. }
  230. }
  231. $cells .= wf_TableCell($cell_links);
  232. break;
  233. default:
  234. $cells .= wf_TableCell($eachdata[$eachkey], '', '', 'style="text-align: center;"');
  235. }
  236. /*if ($eachkey == 'tagid') {
  237. @$tagname = $alltagnames[$curTagID];
  238. $cells .= wf_TableCell(wf_Link('?module=tagcloud&tagid=' . $curTagID) $tagname);
  239. } else {
  240. if ($eachkey == 'fee_charge_always') {
  241. $cells .= wf_TableCell(web_bool_led($eachdata[$eachkey]));
  242. } else {
  243. $cells .= wf_TableCell($eachdata[$eachkey]);
  244. }
  245. }*/
  246. }
  247. }
  248. $delUrl = '?module=' . $module . '&delete=' . $eachdata['id'];
  249. $cancelUrl = '?module=' . $module;
  250. $delTitle = __('Delete') . '?';
  251. $delAlert = __('Delete') . ' ' . __('Virtual service') . ' `' . $tagname . '`? ';
  252. $delAlert .= $messages->getDeleteAlert();
  253. $deletecontrol = wf_ConfirmDialog($delUrl, web_delete_icon(), $delAlert, '', $cancelUrl, $delTitle);
  254. $editcontrol = wf_JSAlert('?module=' . $module . '&edit=' . $eachdata['id'], web_edit_icon(), $messages->getEditAlert());
  255. $cells .= wf_TableCell($deletecontrol . ' ' . $editcontrol);
  256. $rows .= wf_TableRow($cells, 'row5');
  257. }
  258. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  259. } else {
  260. $result = $messages->getStyledMessage(__('Nothing to show'), 'info');
  261. }
  262. return($result);
  263. }
  264. /**
  265. * Flushes virtual cash account for some user
  266. *
  267. * @param string $login
  268. *
  269. * @return void
  270. */
  271. function zb_VserviceCashClear($login) {
  272. $login = ubRouting::filters($login, 'mres');
  273. $vcashDb = new NyanORM('vcash');
  274. $vcashDb->where('login', '=', $login);
  275. $vcashDb->delete();
  276. }
  277. /**
  278. * Creates new vcash account for user
  279. *
  280. * @param string $login
  281. * @param float $cash
  282. *
  283. * @return void
  284. */
  285. function zb_VserviceCashCreate($login, $cash) {
  286. $loginF = ubRouting::filters($login, 'mres');
  287. $cashF = ubRouting::filters($cash, 'mres');
  288. $vcashDb = new NyanORM('vcash');
  289. $vcashDb->data('login', $loginF);
  290. $vcashDb->data('cash', $cashF);
  291. $vcashDb->create();
  292. log_register('VCASH CREATE (' . $login . ') `' . $cash . '`');
  293. }
  294. /**
  295. * Sets virtual account cash for some login
  296. *
  297. * @param string $login
  298. * @param float $cash
  299. *
  300. * @return void
  301. */
  302. function zb_VserviceCashSet($login, $cash) {
  303. $loginF = ubRouting::filters($login, 'mres');
  304. $cashF = ubRouting::filters($cash, 'mres');
  305. $vcashDb = new NyanORM('vcash');
  306. $vcashDb->data('cash', $cashF);
  307. $vcashDb->where('login', '=', $loginF);
  308. $vcashDb->save();
  309. log_register('VCASH CHANGE (' . $login . ') `' . $cash . '`');
  310. }
  311. /**
  312. * Returns virtual account cash amount for some login
  313. *
  314. * @param string $login
  315. *
  316. * @return float
  317. */
  318. function zb_VserviceCashGet($login) {
  319. $result = 0;
  320. $loginF = ubRouting::filters($login, 'mres');
  321. $vcashDb = new NyanORM('vcash');
  322. $vcashDb->where('login', '=', $loginF);
  323. $rawCash = $vcashDb->getAll('login');
  324. if (empty($rawCash)) {
  325. zb_VserviceCashCreate($login, 0);
  326. } else {
  327. $result = $rawCash[$login]['cash'];
  328. }
  329. return($result);
  330. }
  331. /**
  332. * Pushes an record into vcash log
  333. *
  334. * @param string $login
  335. * @param float $balance
  336. * @param float $cash
  337. * @param string $cashtype
  338. * @param string $note
  339. *
  340. * @return void
  341. */
  342. function zb_VserviceCashLog($login, $balance, $cash, $cashtype, $note = '') {
  343. $login = ubRouting::filters($login, 'mres');
  344. $cash = ubRouting::filters($cash, 'mres');
  345. $cashtype = ubRouting::filters($cashtype, 'mres');
  346. $note = ubRouting::filters($note, 'mres');
  347. $balance = zb_VserviceCashGet($login);
  348. $vcashLogDb = new NyanORM('vcashlog');
  349. $vcashLogDb->data('login', $login);
  350. $vcashLogDb->data('date', curdatetime());
  351. $vcashLogDb->data('balance', $balance);
  352. $vcashLogDb->data('summ', $cash);
  353. $vcashLogDb->data('cashtypeid', $cashtype);
  354. $vcashLogDb->data('note', $note);
  355. $vcashLogDb->create();
  356. }
  357. /**
  358. * Performs an vcash fee
  359. *
  360. * @param string $login
  361. * @param float $fee
  362. * @param int $vserviceid
  363. *
  364. * @return void
  365. */
  366. function zb_VserviceCashFee($login, $fee, $vserviceid) {
  367. $login = ubRouting::filters($login, 'mres');
  368. $fee = ubRouting::filters($fee, 'mres');
  369. $vserviceid = ubRouting::filters($vserviceid, 'int');
  370. $balance = zb_VserviceCashGet($login);
  371. if ($fee >= 0) {
  372. $newcash = $balance - $fee;
  373. } else {
  374. $newcash = $balance + abs($fee);
  375. }
  376. zb_VserviceCashSet($login, $newcash);
  377. zb_VserviceCashLog($login, $balance, $newcash, $vserviceid);
  378. }
  379. /**
  380. * Adds cash to virtual cash balance
  381. *
  382. * @param string $login
  383. * @param float $cash
  384. * @param int $vserviceid
  385. *
  386. * @return void
  387. */
  388. function zb_VserviceCashAdd($login, $cash, $vserviceid) {
  389. $login = ubRouting::filters($login, 'mres');
  390. $cash = ubRouting::filters($cash, 'mres');
  391. $vserviceid = ubRouting::filters($vserviceid, 'int');
  392. $balance = zb_VserviceCashGet($login);
  393. $newcash = $balance + $cash;
  394. zb_VserviceCashSet($login, $newcash);
  395. zb_VserviceCashLog($login, $balance, $newcash, $vserviceid);
  396. }
  397. /**
  398. * Returns virtual service selector
  399. *
  400. * @return string
  401. */
  402. function web_VservicesSelector() {
  403. $allservices = zb_VserviceGetAllData();
  404. $alltags = stg_get_alltagnames();
  405. $tmpArr = array();
  406. if (!empty($allservices)) {
  407. foreach ($allservices as $io => $eachservice) {
  408. $tmpArr[$eachservice['id']] = @$alltags[$eachservice['tagid']];
  409. }
  410. }
  411. $result = wf_Selector('vserviceid', $tmpArr, '', '', false);
  412. return ($result);
  413. }
  414. /**
  415. * Performs an virtual services payments processing
  416. *
  417. * @param bool $log_payment
  418. * @param bool $charge_frozen
  419. * @param string $whereString
  420. *
  421. * @return void
  422. */
  423. function zb_VservicesProcessAll($log_payment = true, $charge_frozen = true, $whereString = '') {
  424. global $ubillingConfig;
  425. $alterconf = $ubillingConfig->getAlter();
  426. $considerCreditFlag = $ubillingConfig->getAlterParam('VSERVICES_CONSIDER_CREDIT', 0);
  427. $frozenUsers = array();
  428. $paymentTypeId = 1;
  429. $allUserData = zb_UserGetAllStargazerDataAssoc();
  430. $vservDb = new NyanORM('vservices');
  431. if ($whereString) {
  432. $vservDb->whereRaw($whereString);
  433. }
  434. $vservDb->orderBy('priority', 'DESC');
  435. $allServices = $vservDb->getAll();
  436. //custom payment type ID optional option
  437. if (isset($alterconf['VSERVICES_CASHTYPEID'])) {
  438. if (!empty($alterconf['VSERVICES_CASHTYPEID'])) {
  439. $paymentTypeId = $alterconf['VSERVICES_CASHTYPEID'];
  440. }
  441. }
  442. if (!empty($allServices)) {
  443. if (!$charge_frozen) {
  444. $frozen_query = "SELECT `login` from `users` WHERE `Passive`='1';";
  445. $allFrozen = simple_queryall($frozen_query);
  446. if (!empty($allFrozen)) {
  447. foreach ($allFrozen as $ioFrozen => $eachFrozen) {
  448. $frozenUsers[$eachFrozen['login']] = $eachFrozen['login'];
  449. }
  450. }
  451. }
  452. foreach ($allServices as $io => $eachService) {
  453. $excludedTags = zb_DelimitedStringToSQLWHEREIN($eachService['exclude_tags']);
  454. $excludedTagsWhereStr = empty($excludedTags) ? "" : " AND `login` NOT IN (SELECT `login` FROM `tags` WHERE `tagid` IN (" . $excludedTags . "))";
  455. $users_query = "SELECT `login` from `tags` WHERE `tagid`='" . $eachService['tagid'] . "'" . $excludedTagsWhereStr;
  456. $allUsers = simple_queryall($users_query);
  457. if (!empty($allUsers)) {
  458. foreach ($allUsers as $io2 => $eachUser) {
  459. //virtual cash charging (DEPRECATED)
  460. if ($eachService['cashtype'] == 'virtual') {
  461. $current_cash = zb_VserviceCashGet($eachUser['login']);
  462. $current_credit = $allUserData[$eachUser['login']]['Credit'];
  463. //charge fee is allowed?
  464. if ($eachService['fee_charge_always']) {
  465. $feeChargeAllowed = true;
  466. } else {
  467. if ($considerCreditFlag) {
  468. $feeChargeAllowed = ($current_cash >= '-' . $current_credit) ? true : false;
  469. } else {
  470. $feeChargeAllowed = ($current_cash > 0) ? true : false;
  471. }
  472. }
  473. if ($feeChargeAllowed) {
  474. zb_VserviceCashFee($eachUser['login'], $eachService['price'], $eachService['id']);
  475. }
  476. }
  477. //stargazer balance charging
  478. if ($eachService['cashtype'] == 'stargazer') {
  479. if (empty($allUserData[$eachUser['login']])) {
  480. log_register('VSERVICE_CHARGE_FEE: user (' . $eachUser['login'] . ') not found in overall existent users list');
  481. } else {
  482. $current_cash = $allUserData[$eachUser['login']]['Cash'];
  483. $current_credit = $allUserData[$eachUser['login']]['Credit'];
  484. //charge fee is allowed?
  485. if ($eachService['fee_charge_always']) {
  486. $feeChargeAllowed = true;
  487. } else {
  488. if ($considerCreditFlag) {
  489. $feeChargeAllowed = ($current_cash >= '-' . $current_credit) ? true : false;
  490. } else {
  491. $feeChargeAllowed = ($current_cash > 0) ? true : false;
  492. }
  493. }
  494. if ($feeChargeAllowed) {
  495. $fee = $eachService['price'];
  496. if ($fee >= 0) {
  497. //charge cash from user balance
  498. $fee = "-" . $eachService['price'];
  499. } else {
  500. //add some cash to balance
  501. $fee = abs($eachService['price']);
  502. }
  503. if ($log_payment) {
  504. $method = 'add';
  505. } else {
  506. $method = 'correct';
  507. }
  508. if ($charge_frozen) {
  509. zb_CashAdd($eachUser['login'], $fee, $method, $paymentTypeId, 'Service:' . $eachService['id']);
  510. $allUserData[$eachUser['login']]['Cash'] += $fee; //updating preloaded cash values
  511. } else {
  512. if (!isset($frozenUsers[$eachUser['login']])) {
  513. zb_CashAdd($eachUser['login'], $fee, $method, $paymentTypeId, 'Service:' . $eachService['id']);
  514. $allUserData[$eachUser['login']]['Cash'] += $fee; //updating preloaded cash values
  515. }
  516. }
  517. }
  518. }
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }
  525. /**
  526. * Returns array of all available virtual services as tagid=>price
  527. *
  528. * @return array
  529. */
  530. function zb_VservicesGetAllPrices() {
  531. $result = array();
  532. $vservDb = new NyanORM('vservices');
  533. $all = $vservDb->getAll();
  534. if (!empty($all)) {
  535. foreach ($all as $io => $each) {
  536. $result[$each['tagid']] = $each['price'];
  537. }
  538. }
  539. return ($result);
  540. }
  541. /**
  542. * Returns array of all available virtual services as tagid => array('price' => $price, 'period' => $period);
  543. *
  544. * @return array
  545. */
  546. function zb_VservicesGetAllPricesPeriods() {
  547. $result = array();
  548. $vservDb = new NyanORM('vservices');
  549. $all = $vservDb->getAll();
  550. if (!empty($all)) {
  551. foreach ($all as $io => $each) {
  552. $result[$each['tagid']] = array('price' => $each['price'], 'daysperiod' => $each['charge_period_days']);
  553. }
  554. }
  555. return ($result);
  556. }
  557. /**
  558. * Returns price summary of all virtual services fees assigned to user
  559. *
  560. * @param string $login
  561. *
  562. * @return float
  563. */
  564. function zb_VservicesGetUserPrice($login) {
  565. $result = 0;
  566. $allUserTags = zb_UserGetAllTagsUnique($login);
  567. //user have some tags assigned?
  568. if (!empty($allUserTags[$login])) {
  569. $vservicePrices = zb_VservicesGetAllPrices();
  570. foreach ($allUserTags[$login] as $tagRecordId => $tagId) {
  571. if (isset($vservicePrices[$tagId])) {
  572. $result += $vservicePrices[$tagId];
  573. }
  574. }
  575. }
  576. return ($result);
  577. }
  578. /**
  579. * Returns price total of all virtual services fees assigned to user, considering services fee charge periods
  580. * $defaultPeriod - specifies the default period to count the prices for: 'month' or 'day', if certain service has no fee charge period set
  581. *
  582. * @param string $login
  583. * @param string $defaultPeriod
  584. *
  585. * @return float
  586. */
  587. function zb_VservicesGetUserPricePeriod($login, $defaultPeriod = 'month') {
  588. $totalVsrvPrice = 0;
  589. $allUserVsrvs = zb_VservicesGetUsersAll($login, true);
  590. $curMonthDays = date('t');
  591. if (!empty($allUserVsrvs)) {
  592. $allUserVsrvs = $allUserVsrvs[$login];
  593. foreach ($allUserVsrvs as $eachTagDBID => $eachSrvData) {
  594. $curVsrvPrice = $eachSrvData['price'];
  595. $curVsrvDaysPeriod = $eachSrvData['daysperiod'];
  596. $dailyVsrvPrice = 0;
  597. // getting daily vservice price
  598. if (!empty($curVsrvDaysPeriod)) {
  599. $dailyVsrvPrice = ($curVsrvDaysPeriod > 1) ? $curVsrvPrice / $curVsrvDaysPeriod : $curVsrvPrice;
  600. }
  601. // if vservice has no charge period set and $dailyVsrvPrice == 0
  602. // then virtual service price is considered as for global $defaultPeriod period
  603. if ($defaultPeriod == 'month') {
  604. $totalVsrvPrice += (empty($dailyVsrvPrice)) ? $curVsrvPrice : $dailyVsrvPrice * $curMonthDays;
  605. } else {
  606. $totalVsrvPrice += (empty($dailyVsrvPrice)) ? $curVsrvPrice : $dailyVsrvPrice;
  607. }
  608. }
  609. }
  610. return ($totalVsrvPrice);
  611. }
  612. /**
  613. * Returns all users with assigned virtual services as array:
  614. * login => array($tagDBID => vServicePrice1)
  615. *
  616. * if $includePeriod is true returned array will look like this:
  617. * login => array($tagDBID => array('price' => vServicePrice1, 'daysperiod' => vServicePeriod1))
  618. *
  619. * if $includeVSrvName is true 'vsrvname' => tagname is added to the end of the array
  620. *
  621. * @param string $login
  622. * @param bool $includePeriod
  623. * @param bool $includeVSrvName
  624. *
  625. * @return array
  626. */
  627. function zb_VservicesGetUsersAll($login = '', $includePeriod = false, $includeVSrvName = false) {
  628. $result = array();
  629. $allTagNames = array();
  630. $allUserTags = zb_UserGetAllTagsUnique($login);
  631. if ($includeVSrvName) {
  632. $allTagNames = stg_get_alltagnames();
  633. }
  634. //user have some tags assigned
  635. if (!empty($allUserTags)) {
  636. $vservicePrices = ($includePeriod) ? zb_VservicesGetAllPricesPeriods() : zb_VservicesGetAllPrices();
  637. foreach ($allUserTags as $eachLogin => $data) {
  638. $tmpArr = array();
  639. foreach ($data as $tagDBID => $tagID) {
  640. if (isset($vservicePrices[$tagID])) {
  641. if ($includeVSrvName) {
  642. $tmpArr[$tagDBID] = $vservicePrices[$tagID] + array('vsrvname' => $allTagNames[$tagID]);
  643. } else {
  644. $tmpArr[$tagDBID] = $vservicePrices[$tagID];
  645. }
  646. }
  647. }
  648. if (!empty($tmpArr)) {
  649. $result[$eachLogin] = $tmpArr;
  650. }
  651. }
  652. }
  653. return ($result);
  654. }
  655. /**
  656. * Creates new virtual service
  657. *
  658. * @param int $tagid
  659. * @param float $price
  660. * @param string $cashtype
  661. * @param int $priority
  662. * @param int $feechargealways
  663. * @param int $feechargeperiod
  664. *
  665. * @return void
  666. */
  667. function zb_VserviceCreate($tagid, $price, $cashtype, $priority, $feechargealways = 0, $feechargeperiod = 0, $excludedtags = '', $archived = 0) {
  668. $tagid = ubRouting::filters($tagid, 'int');
  669. $price = ubRouting::filters($price, 'mres');
  670. $cashtype = ubRouting::filters($cashtype, 'mres');
  671. $priority = ubRouting::filters($priority, 'int');
  672. $feechargealways = ubRouting::filters($feechargealways, 'int');
  673. $feechargeperiod = ubRouting::filters($feechargeperiod, 'int');
  674. $vservDb = new NyanORM('vservices');
  675. $vservDb->data('tagid', $tagid);
  676. $vservDb->data('price', $price);
  677. $vservDb->data('cashtype', $cashtype);
  678. $vservDb->data('priority', $priority);
  679. $vservDb->data('fee_charge_always', $feechargealways);
  680. $vservDb->data('charge_period_days', $feechargeperiod);
  681. $vservDb->data('exclude_tags', $excludedtags);
  682. $vservDb->data('archived', $archived);
  683. $vservDb->create();
  684. $newId = $vservDb->getLastId();
  685. log_register('VSERVICE CREATE TAG [' . $tagid . '] PRICE `' . $price . '` [' . $cashtype . '] `' . $priority . '` [' . $feechargealways . '] `' . '` [' . $feechargeperiod . '] ` AS [' . $newId . ']');
  686. }
  687. /**
  688. * Edits virtual service
  689. *
  690. * @param int $vserviceId
  691. * @param int $tagid
  692. * @param float $price
  693. * @param string $cashtype
  694. * @param int $priority
  695. * @param int $feechargealways
  696. * @param int $feechargeperiod
  697. *
  698. * @return void
  699. */
  700. function zb_VserviceEdit($vserviceId, $tagid, $price, $cashtype, $priority, $feechargealways = 0, $feechargeperiod = 0, $excludedtags = '', $archived = 0) {
  701. $vserviceId = ubRouting::filters($vserviceId, 'int');
  702. $tagid = ubRouting::filters($tagid, 'int');
  703. $price = ubRouting::filters($price, 'mres');
  704. $cashtype = ubRouting::filters($cashtype, 'mres');
  705. $priority = ubRouting::filters($priority, 'int');
  706. $feechargealways = ubRouting::filters($feechargealways, 'int');
  707. $feechargeperiod = ubRouting::filters($feechargeperiod, 'int');
  708. $vservDb = new NyanORM('vservices');
  709. $vservDb->data('tagid', $tagid);
  710. $vservDb->data('price', $price);
  711. $vservDb->data('cashtype', $cashtype);
  712. $vservDb->data('priority', $priority);
  713. $vservDb->data('fee_charge_always', $feechargealways);
  714. $vservDb->data('charge_period_days', $feechargeperiod);
  715. $vservDb->data('exclude_tags', $excludedtags);
  716. $vservDb->data('archived', $archived);
  717. $vservDb->where('id', '=', $vserviceId);
  718. $vservDb->save();
  719. log_register('VSERVICE CHANGE [' . $vserviceId . '] PRICE `' . $price . '`');
  720. }
  721. /**
  722. * Returns excluded tags IDs for a certain virtual service or for all of them
  723. * as an array, like [SRVID => array_of_tags_ids] or [SRVID => 'coma_delimited_string_of_tags_ids']
  724. *
  725. * @param $vserviceID
  726. * @param $returnAsString
  727. *
  728. * @return array
  729. */
  730. function zb_VserviceGetExcludedTagsIDs($vserviceID = 0, $returnAsString = false) {
  731. $result = array();
  732. $allservices = zb_VserviceGetAllData();
  733. if (!empty($allservices)) {
  734. foreach ($allservices as $io => $eachservice) {
  735. if (!empty($vserviceID) and $vserviceID != $eachservice['id']) {
  736. continue;
  737. }
  738. $result[$eachservice['id']] = ($returnAsString) ? $eachservice['exclude_tags'] : zb_DelimitedStringToArray($eachservice['exclude_tags']);
  739. }
  740. }
  741. return ($result);
  742. }