BxPaymentGridProcessed.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 BxPaymentGridProcessed extends BxBaseModPaymentGridOrders
  12. {
  13. public function __construct ($aOptions, $oTemplate = false)
  14. {
  15. $this->MODULE = 'bx_payment';
  16. parent::__construct ($aOptions, $oTemplate);
  17. $this->_sOrdersType = 'processed';
  18. $this->_sJsObject = $this->_oModule->_oConfig->getJsObject('processed');
  19. }
  20. public function getFormBlockTitleAPI($sAction, $iId = 0)
  21. {
  22. $sResult = '';
  23. switch($sAction) {
  24. case 'add':
  25. $sResult = _t($this->_sLangsPrefix . 'popup_title_ods_order_' . $this->_sOrdersType . '_add');
  26. break;
  27. }
  28. return $sResult;
  29. }
  30. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  31. {
  32. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&id=' . $iId;
  33. }
  34. public function performActionAdd()
  35. {
  36. $sType = BX_PAYMENT_TYPE_SINGLE;
  37. $sAction = 'add';
  38. $sModule = $this->_oModule->_oConfig->getName();
  39. $sJsObject = $this->_oModule->_oConfig->getJsObject('processed');
  40. $sFormObject = $this->_oModule->_oConfig->getObject('form_processed');
  41. $sFormDisplay = $this->_oModule->_oConfig->getObject('form_display_processed_add');
  42. $oForm = BxDolForm::getObjectInstance($sFormObject, $sFormDisplay, $this->_oModule->_oTemplate);
  43. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction;
  44. $oForm->aInputs['seller_id']['value'] = $this->_aQueryAppend['seller_id'];
  45. $oForm->aInputs['client_id']['attrs'] = array(
  46. 'id' => $this->_oModule->_oConfig->getHtmlIds('processed', 'order_processed_client_id'),
  47. );
  48. $oForm->aInputs['client']['attrs'] = array(
  49. 'id' => $this->_oModule->_oConfig->getHtmlIds('processed', 'order_processed_client')
  50. );
  51. $oForm->aInputs['client']['ajax_get_suggestions'] = BX_DOL_URL_ROOT . 'modules/?r=' . $this->_oModule->_oConfig->getUri() . '/get_clients';
  52. if($this->_bIsApi)
  53. $oForm->aInputs['client']['ajax_get_suggestions'] = $sModule . "/get_clients&params[]=";
  54. $oForm->aInputs['client']['custom'] = array(
  55. 'only_once' => true,
  56. 'on_select' => "function(oObject){{$sJsObject}.showPopup(oObject);}"
  57. );
  58. $oForm->aInputs['module_id']['attrs'] = array(
  59. 'onchange' => 'javascript:' . $sJsObject . '.selectModule(this);'
  60. );
  61. $oForm->aInputs['module_id']['values'] = array(
  62. array('key' => '0', 'value' => _t('_Select_one'))
  63. );
  64. $aModules = $this->_oModule->_oDb->getModules();
  65. foreach($aModules as $aModule)
  66. $oForm->aInputs['module_id']['values'][] = ['key' => $aModule['id'], 'value' => $aModule['title']];
  67. if($this->_bIsApi)
  68. $oForm->aInputs['module_id']['request_url'] = '/api.php?r=' . $sModule . "/get_items&params[]=" . $sType . "&params[]=";
  69. $oForm->aInputs['items']['content'] = $this->_oModule->_oTemplate->displayItems($this->_aQueryAppend['seller_id'], $sType);
  70. $oForm->initChecker();
  71. if($oForm->isSubmittedAndValid()) {
  72. $sFormMethod = $oForm->aFormAttrs['method'];
  73. $aItemIds = $oForm->getSubmittedValue('items', $sFormMethod);
  74. if($this->_bIsApi && is_string($aItemIds))
  75. $aItemIds = explode(',', $aItemIds);
  76. if(empty($aItemIds) || !is_array($aItemIds))
  77. return ($sMsg = $this->_sLangsPrefix . 'err_empty_items') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : echoJson(['msg' => $sMsg]);
  78. $aItems = [];
  79. $sPriceKey = $this->_oModule->_oConfig->getKey('KEY_ARRAY_PRICE_SINGLE');
  80. foreach($aItemIds as $iItemId) {
  81. $iItemId = (int)$iItemId;
  82. $fItemPrice = (float)$oForm->getSubmittedValue('item-price-' . $iItemId, $sFormMethod);
  83. $iItemQuantity = (int)$oForm->getSubmittedValue('item-quantity-' . $iItemId, $sFormMethod);
  84. if($iItemQuantity <= 0)
  85. return ($sMsg = $this->_sLangsPrefix . 'err_wrong_quantity') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : echoJson(['msg' => $sMsg]);
  86. $aItems[$iItemId] = [
  87. 'id' => $iItemId,
  88. $sPriceKey => $fItemPrice,
  89. 'quantity' => $iItemQuantity
  90. ];
  91. }
  92. $mixedResult = $this->_oModule->getObjectOrders()->addOrder([
  93. 'client_id' => $oForm->getCleanValue('client_id'),
  94. 'seller_id' => $oForm->getCleanValue('seller_id'),
  95. 'provider' => 'manual',
  96. 'type' => $sType,
  97. 'order' => $oForm->getCleanValue('order'),
  98. 'error_code' => 0,
  99. 'error_msg' => 'Manually processed',
  100. 'module_id' => $oForm->getCleanValue('module_id'),
  101. 'items' => array_values($aItems)
  102. ]);
  103. if(is_array($mixedResult))
  104. return $this->_bIsApi ? [bx_api_get_msg($mixedResult['msg'])] : echoJson($mixedResult);
  105. $iPendingId = (int)$mixedResult;
  106. if(!$this->_oModule->registerPayment($iPendingId))
  107. return ($sMsg = _t($this->_sLangsPrefix . 'err_cannot_perform')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : echoJson(['msg' => $sMsg]);
  108. $aOrders = $this->_oModule->_oDb->getOrderProcessed(['type' => 'pending_id', 'pending_id' => $iPendingId, 'with_key' => 'id']);
  109. foreach($aOrders as $iOrder => $aOrder)
  110. if((float)$aOrder['amount'] != ($fAmount = $aItems[$aOrder['item_id']][$sPriceKey]))
  111. $this->_oModule->_oDb->updateOrderProcessed($iOrder, ['amount' => $fAmount]);
  112. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => array_keys($aOrders)]);
  113. }
  114. if($this->_bIsApi)
  115. return [
  116. bx_api_get_block('payment_order_add', $oForm->getCodeAPI(), [
  117. 'ext' => [
  118. 'name' => $this->_oModule->getName(),
  119. 'title' => $this->getFormBlockTitleAPI($sAction),
  120. 'request' => ['url' => $this->getFormCallBackUrlAPI($sAction), 'immutable' => true]]
  121. ]
  122. )
  123. ];
  124. $sKey = 'order_' . $this->_sOrdersType . '_add';
  125. $sId = $this->_oModule->_oConfig->getHtmlIds('processed', $sKey);
  126. $sTitle = _t($this->_sLangsPrefix . 'popup_title_ods_' . $sKey);
  127. $sContent = BxTemplFunctions::getInstance()->popupBox($sId, $sTitle, $this->_oModule->_oTemplate->parseHtmlByName('order_processed_add.html', array(
  128. 'js_object' => $sJsObject,
  129. 'form_id' => $oForm->aFormAttrs['id'],
  130. 'form' => $oForm->getCode(true),
  131. 'object' => $this->_sObject,
  132. 'action' => $sAction
  133. )));
  134. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  135. }
  136. protected function _getActionCancel ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  137. {
  138. if(!empty($aRow['seller_id']) && $aRow['seller_id'] != bx_get_logged_profile_id())
  139. return '';
  140. if($this->_bIsApi)
  141. return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide_row']);
  142. return $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  143. }
  144. protected function _getFilterClients()
  145. {
  146. $aIds = $this->_oModule->_oDb->getOrderProcessed(array('type' => 'clients', 'seller_id' => $this->_aQueryAppend['seller_id']));
  147. $aValues = array();
  148. foreach($aIds as $iId)
  149. if(!empty($iId) && ($oClient = BxDolProfile::getInstanceMagic($iId)) !== false)
  150. $aValues[] = array('key' => $iId, 'value' => $oClient->getDisplayName());
  151. $this->_oModule->_oConfig->sortByColumn('value', $aValues);
  152. return $this->_getFilterSelectAll('client', array(
  153. 'values' => array_merge(array(
  154. array('key' => '', 'value' => _t('_bx_payment_txt_all_clients'))
  155. ), $aValues)
  156. ));
  157. }
  158. protected function _getFilterAuthors()
  159. {
  160. $aIds = $this->_oModule->_oDb->getOrderProcessed(array('type' => 'authors', 'seller_id' => $this->_aQueryAppend['seller_id']));
  161. $aValues = array();
  162. foreach($aIds as $iId)
  163. if(!empty($iId) && ($oClient = BxDolProfile::getInstanceMagic($iId)) !== false)
  164. $aValues[] = array('key' => $iId, 'value' => $oClient->getDisplayName());
  165. $this->_oModule->_oConfig->sortByColumn('value', $aValues);
  166. return $this->_getFilterSelectAll('author', array(
  167. 'values' => array_merge(array(
  168. array('key' => '', 'value' => _t('_bx_payment_txt_all_authors'))
  169. ), $aValues)
  170. ));
  171. }
  172. protected function _getFilterModules()
  173. {
  174. $aModules = $this->_oModule->_oDb->getModules();
  175. $aValues = array();
  176. foreach($aModules as $aModule) {
  177. $sLangKey = '_' . $aModule['name'];
  178. $sLangValue = _t($sLangKey);
  179. $aValues[] = array('key' => $aModule['id'], 'value' => $sLangKey != $sLangValue ? $sLangValue : $aModule['title']);
  180. }
  181. $this->_oModule->_oConfig->sortByColumn('value', $aValues);
  182. return $this->_getFilterSelectAll('module', array(
  183. 'js_onchange' => 'onChangeFilterModule(this, ' . $this->_aQueryAppend['seller_id'] . ')',
  184. 'values' => array_merge(array(
  185. array('key' => '', 'value' => _t('_bx_payment_txt_all_modules'))
  186. ), $aValues)
  187. ));
  188. }
  189. protected function _getFilterItems()
  190. {
  191. return $this->_getFilterSelectAll('item', array(
  192. 'attrs' => array('disabled' => 'disabled'),
  193. 'values' => array(
  194. array('key' => '', 'value' => _t('_bx_payment_txt_all_items'))
  195. )
  196. ));
  197. }
  198. protected function _getFilterControls ()
  199. {
  200. parent::_getFilterControls();
  201. $sResult = $this->_getFilterClients();
  202. if($this->_bSingleSeller && $this->_aQueryAppend['seller_id'] == $this->_iSingleSeller)
  203. $sResult .= $this->_getFilterAuthors();
  204. $sResult .= $this->_getFilterModules() . $this->_getFilterItems() . $this->_getSearchInput();
  205. return $sResult;
  206. }
  207. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  208. {
  209. if(empty($this->_aQueryAppend['seller_id']))
  210. return array();
  211. $sParamsDivider = $this->_oModule->_oConfig->getDivider('DIVIDER_GRID_FILTERS');
  212. $iClient = $iAuthor = $iModule = $iItem = 0;
  213. if(strpos($sFilter, $sParamsDivider) !== false) {
  214. $aFilters = explode($sParamsDivider, $sFilter);
  215. if($this->_bSingleSeller && $this->_aQueryAppend['seller_id'] = $this->_iSingleSeller)
  216. list($iClient, $iAuthor, $iModule, $iItem, $sFilter) = $aFilters;
  217. else
  218. list($iClient, $iModule, $iItem, $sFilter) = $aFilters;
  219. }
  220. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND (`tt`.`seller_id`=? OR `tt`.`author_id`=?)", $this->_aQueryAppend['seller_id'], $this->_aQueryAppend['seller_id']);
  221. $iClient = (int)$iClient;
  222. if($iClient != 0)
  223. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`client_id`=?", $iClient);
  224. $iAuthor = (int)$iAuthor;
  225. if($iAuthor != 0)
  226. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`author_id`=?", $iAuthor);
  227. $iModule = (int)$iModule;
  228. if($iModule != 0)
  229. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`module_id`=?", $iModule);
  230. $iItem = (int)$iItem;
  231. if($iItem != 0)
  232. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`item_id`=?", $iItem);
  233. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  234. }
  235. }
  236. /** @} */