BxPaymentDb.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Payment Payment
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxPaymentDb extends BxBaseModPaymentDb
  12. {
  13. function __construct(&$oConfig)
  14. {
  15. parent::__construct($oConfig);
  16. }
  17. public function getModulesWithPayments()
  18. {
  19. $sQuery = $this->prepare("SELECT `name` FROM `" . $this->_sPrefix . "modules`");
  20. return $this->getColumn($sQuery);
  21. }
  22. /**
  23. * Payment details methods
  24. */
  25. public function getForm()
  26. {
  27. $sQuery = "SELECT
  28. `tp`.`id` AS `provider_id`,
  29. `tp`.`name` AS `provider_name`,
  30. `tp`.`caption` AS `provider_caption`,
  31. `tp`.`description` AS `provider_description`,
  32. `tp`.`option_prefix` AS `provider_option_prefix`,
  33. `tp`.`for_owner_only` AS `provider_for_owner_only`,
  34. `tp`.`single_seller` AS `provider_single_seller`,
  35. `tpo`.`id` AS `id`,
  36. `tpo`.`name` AS `name`,
  37. `tpo`.`type` AS `type`,
  38. `tpo`.`caption` AS `caption`,
  39. `tpo`.`description` AS `description`,
  40. `tpo`.`extra` AS `extra`,
  41. `tpo`.`check_type` AS `check_type`,
  42. `tpo`.`check_params` AS `check_params`,
  43. `tpo`.`check_error` AS `check_error`
  44. FROM `" . $this->_sPrefix . "providers` AS `tp`
  45. LEFT JOIN `" . $this->_sPrefix . "providers_options` AS `tpo` ON `tp`.`id`=`tpo`.`provider_id`
  46. WHERE `tp`.`active`='1'
  47. ORDER BY `tp`.`order` ASC, `tpo`.`order` ASC";
  48. return $this->getAll($sQuery);
  49. }
  50. public function getProviders($aParams = array())
  51. {
  52. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  53. $sWhereClause = "";
  54. if(!empty($aParams['type']))
  55. switch($aParams['type']) {
  56. case 'by_name':
  57. $aMethod['name'] = 'getRow';
  58. $aMethod['params'][1] = array(
  59. 'name' => $aParams['name']
  60. );
  61. $sWhereClause = " AND `tp`.`name`=:name";
  62. break;
  63. case 'for_single':
  64. $aMethod['name'] = 'getAllWithKey';
  65. $aMethod['params'][1] = 'name';
  66. $sWhereClause = " AND `tp`.`for_single`='1'";
  67. break;
  68. case 'for_recurring':
  69. $aMethod['name'] = 'getAllWithKey';
  70. $aMethod['params'][1] = 'name';
  71. $sWhereClause = " AND `tp`.`for_recurring`='1'";
  72. break;
  73. case 'all':
  74. $aMethod['name'] = 'getAllWithKey';
  75. $aMethod['params'][1] = 'name';
  76. if(!empty($aParams['active']))
  77. $sWhereClause = " AND `tp`.`active`='1'";
  78. break;
  79. }
  80. $aMethod['params'][0] = "SELECT
  81. `tp`.`id` AS `id`,
  82. `tp`.`name` AS `name`,
  83. `tp`.`caption` AS `caption`,
  84. `tp`.`description` AS `description`,
  85. `tp`.`option_prefix` AS `option_prefix`,
  86. `tp`.`for_visitor` AS `for_visitor`,
  87. `tp`.`for_single` AS `for_single`,
  88. `tp`.`for_recurring` AS `for_recurring`,
  89. `tp`.`class_name` AS `class_name`,
  90. `tp`.`class_file` AS `class_file`
  91. FROM `" . $this->_sPrefix . "providers` AS `tp`
  92. WHERE 1" . $sWhereClause;
  93. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  94. }
  95. public function getOption($aParams)
  96. {
  97. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  98. $sSelectClause = "`tpo`.*";
  99. $sJoinClause = $sWhereClause = "";
  100. if(!empty($aParams['type']))
  101. switch($aParams['type']) {
  102. case 'by_pid_and_name':
  103. $aMethod['name'] = 'getRow';
  104. $aMethod['params'][1] = array(
  105. 'provider_id' => $aParams['provider_id'],
  106. 'name' => $aParams['name'],
  107. );
  108. $sWhereClause = " AND `tpo`.`provider_id`=:provider_id AND `tpo`.`name`=:name";
  109. break;
  110. }
  111. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $this->_sPrefix . "providers_options` AS `tpo` " . $sJoinClause . " WHERE 1" . $sWhereClause;
  112. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  113. }
  114. public function getOptions($iUserId = BX_PAYMENT_EMPTY_ID, $iProviderId = 0)
  115. {
  116. $aBinding = array(
  117. 'user_id' => $iUserId
  118. );
  119. if($iUserId == BX_PAYMENT_EMPTY_ID && empty($iProviderId))
  120. return $this->getAll("SELECT `id`, `name`, `type` FROM `" . $this->_sPrefix . "providers_options`");
  121. $sWhereAddon = "";
  122. if(!empty($iProviderId)) {
  123. $aBinding['provider_id'] = $iProviderId;
  124. $sWhereAddon = " AND `tpo`.`provider_id`=:provider_id";
  125. }
  126. $sQuery = "SELECT
  127. `tpo`.`name` AS `name`,
  128. `tuv`.`value` AS `value`
  129. FROM `" . $this->_sPrefix . "providers_options` AS `tpo`
  130. LEFT JOIN `" . $this->_sPrefix . "user_values` AS `tuv` ON `tpo`.`id`=`tuv`.`option_id`
  131. WHERE 1" . $sWhereAddon . " AND `tuv`.`user_id`=:user_id";
  132. return $this->getAllWithKey($sQuery, 'name', $aBinding);
  133. }
  134. public function updateOption($iUserId, $iOptionId, $sValue)
  135. {
  136. $sQuery = $this->prepare("REPLACE INTO `" . $this->_sPrefix . "user_values` SET `user_id`=?, `option_id`=?, `value`=?", $iUserId, $iOptionId, $sValue);
  137. return $this->query($sQuery);
  138. }
  139. /**
  140. * Shopping cart methods.
  141. */
  142. public function getCartItems($iId)
  143. {
  144. $sQuery = $this->prepare("SELECT `items` FROM `" . $this->_sPrefix . "cart` WHERE `client_id`=? LIMIT 1", $iId);
  145. return $this->getOne($sQuery);
  146. }
  147. public function getCartContent($iId)
  148. {
  149. $aCart = $this->getRow("SELECT * FROM `" . $this->_sPrefix . "cart` WHERE `client_id`=:client_id LIMIT 1", array(
  150. 'client_id' => $iId
  151. ));
  152. if(empty($aCart) || !is_array($aCart))
  153. $aCart = array('items' => '', 'customs' => '');
  154. return $aCart;
  155. }
  156. public function setCartItems($iId, $sItems, $aCustoms = array())
  157. {
  158. $sItems = trim($sItems, ":");
  159. if(empty($sItems))
  160. return $this->query("DELETE FROM `" . $this->_sPrefix . "cart` WHERE `client_id`=:client_id LIMIT 1", array(
  161. 'client_id' => $iId
  162. ));
  163. return $this->query("REPLACE INTO `" . $this->_sPrefix . "cart` SET `client_id`=:client_id, `items`=:items, `customs`=:customs", array(
  164. 'client_id' => $iId,
  165. 'items' => $sItems,
  166. 'customs' => !empty($aCustoms) && is_array($aCustoms) ? serialize($aCustoms) : ''
  167. ));
  168. }
  169. public function getVendorInfoProvidersSingle($iVendorId)
  170. {
  171. return $this->getVendorInfoProviders($iVendorId, array('type' => 'for_single'));
  172. }
  173. public function getVendorInfoProvidersRecurring($iVendorId)
  174. {
  175. return $this->getVendorInfoProviders($iVendorId, array('type' => 'for_recurring'));
  176. }
  177. public function getVendorInfoProviders($iVendorId, $aParams = array())
  178. {
  179. if(empty($aParams))
  180. $aParams = array('type' => 'all');
  181. $aProviders = $this->getProviders($aParams);
  182. $aOptions = $this->getOptions($iVendorId);
  183. $aResult = array();
  184. foreach($aProviders as $sProvider => $aProvider) {
  185. if(!isset($aOptions[$aProvider['option_prefix'] . 'active']) || $aOptions[$aProvider['option_prefix'] . 'active']['value'] != 'on')
  186. continue;
  187. if(isset($aOptions[$aProvider['option_prefix'] . 'hidden']) && $aOptions[$aProvider['option_prefix'] . 'hidden']['value'] == 'on')
  188. continue;
  189. foreach($aOptions as $sName => $aOption)
  190. if(strpos($sName, $aProvider['option_prefix']) !== false)
  191. $aProvider['options'][$sName] = $aOption;
  192. $aResult[$sProvider] = $aProvider;
  193. }
  194. return $aResult;
  195. }
  196. public function getAdminsIds()
  197. {
  198. $sQuery = "SELECT
  199. `tp`.`id` AS `id`,
  200. `tp`.`type` AS `type`
  201. FROM `sys_profiles` AS `tp`
  202. INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id` AND `ta`.`role`&" . BX_DOL_ROLE_ADMIN . "
  203. WHERE
  204. `tp`.`type`<>'system' AND `tp`.`status`='active'
  205. ORDER BY `tp`.`id` ASC";
  206. $aAdmins = $this->getAllWithKey($sQuery, 'id');
  207. foreach($aAdmins as $iId => $aAdmin)
  208. if(!BxDolService::call($aAdmin['type'], 'act_as_profile'))
  209. unset($aAdmins[$iId]);
  210. return array_keys($aAdmins);
  211. }
  212. /*
  213. * Pending Orders methods
  214. */
  215. public function getOrderPending($aParams)
  216. {
  217. $aMethod = array('name' => 'getRow', 'params' => array(0 => 'query'));
  218. $sWhereClause = $sLimitClause = '';
  219. switch($aParams['type']) {
  220. case 'id':
  221. $aMethod['params'][1] = array(
  222. 'id' => $aParams['id']
  223. );
  224. $sWhereClause = " AND `id`=:id";
  225. $sLimitClause = " LIMIT 1";
  226. break;
  227. case 'order':
  228. $aMethod['params'][1] = array(
  229. 'order' => $aParams['order']
  230. );
  231. $sWhereClause = " AND `order`=:order";
  232. $sLimitClause = " LIMIT 1";
  233. break;
  234. case 'mixed':
  235. $aMethod['name'] = 'getAll';
  236. $sWhereClause = ' AND ' . $this->arrayToSQL($aParams['conditions'], ' AND ');
  237. break;
  238. }
  239. $aMethod['params'][0] = "SELECT * FROM `" . $this->_sPrefix . "transactions_pending` WHERE 1 " . $sWhereClause . $sLimitClause;
  240. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  241. }
  242. public function insertOrderPending($iClientId, $sType, $sProvider, $aCartInfo, $aCustom = array())
  243. {
  244. $sItems = "";
  245. foreach($aCartInfo['items'] as $aItem) {
  246. $sItems .= $this->_oConfig->descriptorA2S(array($aItem['author_id'], $aItem['module_id'], $aItem['id'], $aItem['quantity'])) . ':';
  247. if(empty($aItem['addons']) || !is_array($aItem['addons']))
  248. continue;
  249. foreach($aItem['addons'] as $sAddon => $aAddon)
  250. $sItems .= $this->_oConfig->descriptorA2S(array($aAddon['author_id'], $aAddon['module_id'], $aAddon['id'], $aAddon['quantity'])) . ':';
  251. }
  252. return (int)$this->query("INSERT INTO `" . $this->_sPrefix . "transactions_pending` SET `client_id`=:client_id, `seller_id`=:seller_id, `type`=:type, `provider`=:provider, `items`=:items, `customs`=:customs, `amount`=:amount, `date`=UNIX_TIMESTAMP()", array(
  253. 'client_id' => $iClientId,
  254. 'seller_id' => $aCartInfo['vendor_id'],
  255. 'type' => $sType,
  256. 'provider' => $sProvider,
  257. 'items' => trim($sItems, ':'),
  258. 'customs' => !empty($aCustom) && is_array($aCustom) ? serialize($aCustom) : '',
  259. 'amount' => $aCartInfo['items_price']
  260. )) > 0 ? $this->lastId() : 0;
  261. }
  262. public function updateOrderPending($iId, $aValues)
  263. {
  264. $sQuery = $this->prepare("UPDATE `" . $this->_sPrefix . "transactions_pending` SET " . $this->arrayToSQL($aValues) . " WHERE `id`=?", $iId);
  265. return (int)$this->query($sQuery) > 0;
  266. }
  267. public function deleteOrderPending($mixedId)
  268. {
  269. if(!is_array($mixedId))
  270. $mixedId = array($mixedId);
  271. return (int)$this->query("DELETE FROM `" . $this->_sPrefix . "transactions_pending` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  272. }
  273. /*
  274. * Processed Orders
  275. */
  276. public function getOrderProcessed($aParams)
  277. {
  278. $aMethod = array('name' => 'getRow', 'params' => array(0 => 'query'));
  279. $sSelectClause = "`tt`.`id`, `tt`.`license`, `ttp`.`type`, `tt`.`client_id`, `tt`.`seller_id`, `tt`.`author_id`, `tt`.`module_id`, `tt`.`item_id`, `tt`.`item_count`, `tt`.`amount`, `tt`.`date`, `ttp`.`order`, `ttp`.`error_msg`, `ttp`.`provider`";
  280. $sWhereClause = $sGroupClause = "";
  281. switch($aParams['type']) {
  282. case 'id':
  283. $aMethod['params'][1] = array(
  284. 'id' => $aParams['id']
  285. );
  286. $sWhereClause = " AND `tt`.`id`=:id";
  287. break;
  288. case 'new':
  289. $aMethod['name'] = 'getAll';
  290. $aMethod['params'][1] = array(
  291. 'seller_id' => $aParams['seller_id']
  292. );
  293. $sWhereClause = " AND `tt`.`seller_id`=:seller_id AND `tt`.`new`='1'";
  294. break;
  295. case 'pending_id':
  296. if(empty($aParams['with_key'])) {
  297. $aMethod['name'] = 'getAll';
  298. $aMethod['params'][1] = array(
  299. 'pending_id' => $aParams['pending_id']
  300. );
  301. }
  302. else {
  303. $aMethod['name'] = 'getAllWithKey';
  304. $aMethod['params'][1] = $aParams['with_key'];
  305. $aMethod['params'][2] = array(
  306. 'pending_id' => $aParams['pending_id']
  307. );
  308. }
  309. $sWhereClause = " AND `tt`.`pending_id`=:pending_id";
  310. break;
  311. case 'clients':
  312. $aMethod['name'] = 'getColumn';
  313. $aMethod['params'][1] = array(
  314. 'seller_id' => $aParams['seller_id']
  315. );
  316. $sSelectClause = "DISTINCT `tt`.`client_id`";
  317. $sWhereClause = " AND `tt`.`seller_id`=:seller_id";
  318. break;
  319. case 'authors':
  320. $aMethod['name'] = 'getColumn';
  321. $aMethod['params'][1] = array(
  322. 'seller_id' => $aParams['seller_id']
  323. );
  324. $sSelectClause = "DISTINCT `tt`.`author_id`";
  325. $sWhereClause = " AND `tt`.`seller_id`=:seller_id";
  326. break;
  327. case 'license':
  328. $aMethod['name'] = 'getAll';
  329. $aMethod['params'][1] = array(
  330. 'license' => $aParams['license']
  331. );
  332. $sWhereClause = " AND `tt`.`license`=:license";
  333. break;
  334. case 'income':
  335. $aMethod['name'] = 'getAll';
  336. $aMethod['params'][1] = array(
  337. 'period_start' => $aParams['period_start'],
  338. 'period_end' => $aParams['period_end']
  339. );
  340. $sSelectClause = '`tt`.`author_id` AS `id`, SUM(`tt`.`amount`) AS `amount`';
  341. $sWhereClause = ' AND `tt`.`date`>=:period_start AND `tt`.`date`<=:period_end';
  342. $sGroupClause = '`tt`.`author_id`';
  343. break;
  344. case 'mixed':
  345. $aMethod['name'] = 'getAll';
  346. $sWhereClause = ' AND ' . $this->arrayToSQL($aParams['conditions'], ' AND ');
  347. break;
  348. }
  349. if(!empty($sGroupClause))
  350. $sGroupClause = ' GROUP BY ' . $sGroupClause;
  351. $aMethod['params'][0] = "SELECT
  352. " . $sSelectClause . "
  353. FROM `" . $this->_sPrefix . "transactions` AS `tt`
  354. LEFT JOIN `" . $this->_sPrefix . "transactions_pending` AS `ttp` ON `tt`.`pending_id`=`ttp`.`id`
  355. WHERE 1" . $sWhereClause . $sGroupClause;
  356. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  357. }
  358. public function insertOrderProcessed($aValues)
  359. {
  360. return $this->query("INSERT INTO `" . $this->_sPrefix . "transactions` SET " . $this->arrayToSQL($aValues) . ", `date`=UNIX_TIMESTAMP()");
  361. }
  362. public function updateOrderProcessed($iId, $aValues)
  363. {
  364. return $this->updateOrdersProcessed($aValues, array('id' => $iId));
  365. }
  366. public function updateOrdersProcessed($aValues, $aWhere)
  367. {
  368. if(empty($aValues) || !is_array($aValues) || empty($aWhere) || !is_array($aWhere))
  369. return false;
  370. return (int)$this->query("UPDATE `" . $this->_sPrefix . "transactions` SET " . $this->arrayToSQL($aValues) . " WHERE " . $this->arrayToSQL($aWhere, ' AND ')) > 0;
  371. }
  372. public function deleteOrderProcessed($mixedId)
  373. {
  374. if(!is_array($mixedId))
  375. $mixedId = array($mixedId);
  376. return (int)$this->query("DELETE FROM `" . $this->_sPrefix . "transactions` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  377. }
  378. public function getOrderHistory($aParams)
  379. {
  380. return $this->getOrderProcessed($aParams);
  381. }
  382. /*
  383. * Subscriptions methods
  384. */
  385. public function getOrderSubscription($aParams)
  386. {
  387. return $this->getOrderPending($aParams);
  388. }
  389. public function getSubscription($aParams)
  390. {
  391. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  392. $sSelectClause = "`ts`.*";
  393. $sJoinClause = $sWhereClause = $sLimitClause = '';
  394. switch($aParams['type']) {
  395. case 'pending_id':
  396. $aMethod['name'] = 'getRow';
  397. $aMethod['params'][1] = array(
  398. 'pending_id' => $aParams['pending_id']
  399. );
  400. $sWhereClause = " AND `ts`.`pending_id`=:pending_id";
  401. $sLimitClause = " LIMIT 1";
  402. break;
  403. case 'mixed':
  404. $sWhereClause = " AND " . $this->arrayToSQL($aParams['conditions'], ' AND ');
  405. break;
  406. case 'mixed_ext':
  407. $sSelectClause .= ", `ttp`.`client_id`, `ttp`.`seller_id`, `ttp`.`type`, `ttp`.`provider`, `ttp`.`order`";
  408. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "transactions_pending` AS `ttp` ON `ts`.`pending_id`=`ttp`.`id`";
  409. $sWhereClause = " AND " . $this->arrayToSQL($aParams['conditions'], ' AND ');
  410. break;
  411. case 'time_tracker':
  412. $aMethod['params'][1] = array(
  413. 'status_active' => $aParams['status_active'],
  414. 'status_trial' => $aParams['status_trial'],
  415. 'status_unpaid' => $aParams['status_unpaid'],
  416. 'pay_attempts_max' => $aParams['pay_attempts_max'],
  417. 'pay_attempts_interval' => $aParams['pay_attempts_interval']
  418. );
  419. $sSelectClause .= ", `ttp`.`client_id`, `ttp`.`seller_id`, `ttp`.`type`, `ttp`.`provider`, `ttp`.`amount`, `ttp`.`order`";
  420. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "transactions_pending` AS `ttp` ON `ts`.`pending_id`=`ttp`.`id` INNER JOIN `" . $this->_sPrefix . "providers` AS `tp` ON `ttp`.`provider`=`tp`.`name` AND `tp`.`time_tracker`='1'";
  421. $sWhereClause = " AND `ts`.`date_next`<=UNIX_TIMESTAMP() AND (`ts`.`status`=:status_active OR `ts`.`status`=:status_trial OR (`ts`.`status`=:status_unpaid AND `ts`.`pay_attempts`<:pay_attempts_max AND DATE_ADD(FROM_UNIXTIME(`ts`.`date_next`), INTERVAL `ts`.`pay_attempts`*:pay_attempts_interval SECOND)<=NOW()))";
  422. break;
  423. }
  424. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $this->_sPrefix . "subscriptions` AS `ts` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sLimitClause;
  425. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  426. }
  427. public function isSubscriptionByPending($iPending)
  428. {
  429. $aSubscription = $this->getSubscription(array(
  430. 'type' => 'pending_id',
  431. 'pending_id' => $iPending
  432. ));
  433. return !empty($aSubscription) && is_array($aSubscription);
  434. }
  435. public function insertSubscription($aValues)
  436. {
  437. if(empty($aValues) || !is_array($aValues))
  438. return false;
  439. $sSetClause = $this->arrayToSQL($aValues);
  440. if(empty($aValues['date_add']))
  441. $sSetClause .= ", `date_add`=UNIX_TIMESTAMP()";
  442. return (int)$this->query("INSERT INTO `" . $this->_sPrefix . "subscriptions` SET " . $sSetClause) > 0;
  443. }
  444. public function updateSubscription($aValues, $aWhere)
  445. {
  446. if(empty($aValues) || !is_array($aValues) || empty($aWhere) || !is_array($aWhere))
  447. return false;
  448. return (int)$this->query("UPDATE `" . $this->_sPrefix . "subscriptions` SET " . $this->arrayToSQL($aValues) . " WHERE " . $this->arrayToSQL($aWhere, ' AND ')) > 0;
  449. }
  450. public function deleteSubscription($mixedId, $sReason)
  451. {
  452. if(!is_array($mixedId))
  453. $mixedId = array($mixedId);
  454. //--- Move to deleted subscriptions table.
  455. $sQuery = "INSERT IGNORE INTO `" . $this->_sPrefix . "subscriptions_deleted` SELECT *, :reason AS `reason`, UNIX_TIMESTAMP() AS `deleted` FROM `" . $this->_sPrefix . "subscriptions` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")";
  456. $this->query($sQuery, array(
  457. 'reason' => $sReason
  458. ));
  459. return (int)$this->query("DELETE FROM `" . $this->_sPrefix . "subscriptions` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  460. }
  461. //--- Order Administration ---//
  462. public function onProfileDelete($iId)
  463. {
  464. $sQuery = $this->prepare("DELETE FROM `" . $this->_sPrefix . "cart` WHERE `client_id`=?", $iId);
  465. $this->query($sQuery);
  466. $sQuery = $this->prepare("DELETE FROM `" . $this->_sPrefix . "user_values` WHERE `user_id`=?", $iId);
  467. $this->query($sQuery);
  468. }
  469. /*
  470. * Commissions methods
  471. */
  472. public function getCommissions($aParams = array())
  473. {
  474. $CNF = &$this->_oConfig->CNF;
  475. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  476. $sSelectClause = "`tc`.*";
  477. $sWhereClause = $sOrderClause = "";
  478. if(!empty($aParams['type']))
  479. switch($aParams['type']) {
  480. case 'max_order':
  481. $aMethod['name'] = 'getOne';
  482. $aMethod['params'][1] = array();
  483. $sSelectClause = "IFNULL(MAX(`tc`.`order`), 0)";
  484. break;
  485. case 'id':
  486. $aMethod['name'] = 'getRow';
  487. $aMethod['params'][1] = array(
  488. 'id' => $aParams['id']
  489. );
  490. $sWhereClause = " AND `tc`.`id`=:id";
  491. break;
  492. case 'acl_id':
  493. $aMethod['name'] = 'getAll';
  494. $aMethod['params'][1] = array(
  495. 'acl_id' => $aParams['acl_id']
  496. );
  497. $sWhereClause = " AND (`tc`.`acl_id`=:acl_id OR `tc`.`acl_id`=0)";
  498. $sOrderClause = "`tc`.`order` ASC";
  499. break;
  500. case 'all':
  501. if(!empty($aParams['active']))
  502. $sWhereClause = " AND `tc`.`active`='1'";
  503. break;
  504. }
  505. if(!empty($sOrderClause))
  506. $sOrderClause = " ORDER BY " . $sOrderClause;
  507. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  508. FROM `" . $CNF['TABLE_COMMISSIONS'] . "` AS `tc`
  509. WHERE 1" . $sWhereClause . $sOrderClause;
  510. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  511. }
  512. public function getInvoices($aParams = array())
  513. {
  514. $CNF = &$this->_oConfig->CNF;
  515. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  516. $sSelectClause = "`ti`.*";
  517. $sWhereClause = "";
  518. if(!empty($aParams['type']))
  519. switch($aParams['type']) {
  520. case 'index':
  521. $aMethod['name'] = 'getOne';
  522. $aMethod['params'][1] = array(
  523. 'commissionaire_id' => $aParams['commissionaire_id'],
  524. 'committent_id' => $aParams['committent_id'],
  525. );
  526. $sSelectClause = "COUNT(`ti`.`id`) + 1";
  527. $sWhereClause = " AND `ti`.`commissionaire_id`=:commissionaire_id AND `ti`.`committent_id`=:committent_id";
  528. break;
  529. case 'id':
  530. $aMethod['name'] = 'getRow';
  531. $aMethod['params'][1] = array(
  532. 'id' => $aParams['id']
  533. );
  534. $sWhereClause = " AND `ti`.`id`=:id";
  535. break;
  536. case 'commissionaire_id':
  537. $aMethod['params'][1] = array(
  538. 'commissionaire_id' => $aParams['commissionaire_id']
  539. );
  540. $sWhereClause = " AND `ti`.`commissionaire_id`=:commissionaire_id";
  541. break;
  542. case 'committent_id':
  543. $aMethod['params'][1] = array(
  544. 'committent_id' => $aParams['committent_id']
  545. );
  546. $sWhereClause = " AND `ti`.`committent_id`=:committent_id";
  547. if(!empty($aParams['period_start'])) {
  548. $aMethod['params'][1]['period_start'] = (int)$aParams['period_start'];
  549. $sWhereClause .= " AND `ti`.`period_start`=:period_start";
  550. }
  551. if(!empty($aParams['period_end'])) {
  552. $aMethod['params'][1]['period_end'] = (int)$aParams['period_end'];
  553. $sWhereClause .= " AND `ti`.`period_end`=:period_end";
  554. }
  555. break;
  556. case 'expiring':
  557. $aMethod['params'][1] = array(
  558. 'notify_days' => $CNF['PARAM_CMSN_INVOICE_EXPIRATION_NOTIFY'],
  559. 'status' => BX_PAYMENT_INV_STATUS_UNPAID
  560. );
  561. $sWhereClause = " AND DATE_SUB(FROM_UNIXTIME(`ti`.`date_due`), INTERVAL :notify_days DAY) < NOW() AND DATE_SUB(FROM_UNIXTIME(`ti`.`date_due`), INTERVAL (:notify_days - 1) DAY) > NOW() AND `ti`.`status`=:status";
  562. break;
  563. case 'overdue':
  564. $aMethod['params'][1] = array(
  565. 'status_unpaid' => BX_PAYMENT_INV_STATUS_UNPAID,
  566. 'status_overdue' => BX_PAYMENT_INV_STATUS_OVERDUE
  567. );
  568. $sWhereClause = " AND `ti`.`date_due` < UNIX_TIMESTAMP() AND (`ti`.`status`=:status_unpaid OR (`ti`.`status`=:status_overdue AND `ti`.`ntf_due`='0'))";
  569. break;
  570. case 'status':
  571. $aMethod['params'][1] = array(
  572. 'status' => $aParams['status']
  573. );
  574. $sWhereClause = " AND `ti`.`status`=:status";
  575. if(isset($aParams['count']) && $aParams['count'] === true) {
  576. $sSelectClause = "COUNT(`ti`.`id`)";
  577. $aMethod['name'] = 'getOne';
  578. }
  579. if(isset($aParams['committent_id'])) {
  580. $aMethod['params'][1]['committent_id'] = $aParams['committent_id'];
  581. $sWhereClause .= " AND `ti`.`committent_id`=:committent_id";
  582. }
  583. break;
  584. case 'all_count':
  585. $sSelectClause = "COUNT(`ti`.`id`)";
  586. $aMethod['name'] = 'getOne';
  587. break;
  588. case 'all':
  589. break;
  590. }
  591. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  592. FROM `" . $CNF['TABLE_INVOICES'] . "` AS `ti`
  593. WHERE 1" . $sWhereClause;
  594. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  595. }
  596. public function insertInvoice($aValues)
  597. {
  598. $CNF = &$this->_oConfig->CNF;
  599. return $this->query("INSERT INTO `" . $CNF['TABLE_INVOICES'] . "` SET " . $this->arrayToSQL($aValues));
  600. }
  601. public function updateInvoice($mixedId, $aValues)
  602. {
  603. $CNF = &$this->_oConfig->CNF;
  604. if(!is_array($mixedId))
  605. $mixedId = array($mixedId);
  606. return (int)$this->query("UPDATE `" . $CNF['TABLE_INVOICES'] . "` SET " . $this->arrayToSQL($aValues) . " WHERE `id` IN (" . $this->implode_escape($mixedId) . ')') > 0;
  607. }
  608. public function deleteInvoice($mixedId)
  609. {
  610. $CNF = &$this->_oConfig->CNF;
  611. if(!is_array($mixedId))
  612. $mixedId = array($mixedId);
  613. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_INVOICES'] . "` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  614. }
  615. }
  616. /** @} */