BxPaymentGridProcessed.php 10 KB

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