BxPaymentGridProcessed.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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($this->_aQueryAppend['seller_id'], $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 = [];
  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[$iItemId] = [
  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' => array_values($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(['type' => 'pending_id', 'pending_id' => $iPendingId, 'with_key' => 'id']);
  88. foreach($aOrders as $iOrder => $aOrder)
  89. if((float)$aOrder['amount'] != ($fAmount = $aItems[$aOrder['item_id']][$sPriceKey]))
  90. $this->_oModule->_oDb->updateOrderProcessed($iOrder, ['amount' => $fAmount]);
  91. return echoJson(['grid' => $this->getCode(false), 'blink' => array_keys($aOrders)]);
  92. }
  93. $sKey = 'order_' . $this->_sOrdersType . '_add';
  94. $sId = $this->_oModule->_oConfig->getHtmlIds('processed', $sKey);
  95. $sTitle = _t($this->_sLangsPrefix . 'popup_title_ods_' . $sKey);
  96. $sContent = BxTemplFunctions::getInstance()->popupBox($sId, $sTitle, $this->_oModule->_oTemplate->parseHtmlByName('order_processed_add.html', array(
  97. 'js_object' => $sJsObject,
  98. 'form_id' => $oForm->aFormAttrs['id'],
  99. 'form' => $oForm->getCode(true),
  100. 'object' => $this->_sObject,
  101. 'action' => $sAction
  102. )));
  103. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  104. }
  105. protected function _getActionCancel ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  106. {
  107. if(!empty($aRow['seller_id']) && $aRow['seller_id'] != bx_get_logged_profile_id())
  108. return '';
  109. return $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  110. }
  111. protected function _getFilterClients()
  112. {
  113. $aIds = $this->_oModule->_oDb->getOrderProcessed(array('type' => 'clients', 'seller_id' => $this->_aQueryAppend['seller_id']));
  114. $aValues = array();
  115. foreach($aIds as $iId)
  116. if(!empty($iId) && ($oClient = BxDolProfile::getInstanceMagic($iId)) !== false)
  117. $aValues[] = array('key' => $iId, 'value' => $oClient->getDisplayName());
  118. $this->_oModule->_oConfig->sortByColumn('value', $aValues);
  119. return $this->_getFilterSelectAll('client', array(
  120. 'values' => array_merge(array(
  121. array('key' => '', 'value' => _t('_bx_payment_txt_all_clients'))
  122. ), $aValues)
  123. ));
  124. }
  125. protected function _getFilterAuthors()
  126. {
  127. $aIds = $this->_oModule->_oDb->getOrderProcessed(array('type' => 'authors', 'seller_id' => $this->_aQueryAppend['seller_id']));
  128. $aValues = array();
  129. foreach($aIds as $iId)
  130. if(!empty($iId) && ($oClient = BxDolProfile::getInstanceMagic($iId)) !== false)
  131. $aValues[] = array('key' => $iId, 'value' => $oClient->getDisplayName());
  132. $this->_oModule->_oConfig->sortByColumn('value', $aValues);
  133. return $this->_getFilterSelectAll('author', array(
  134. 'values' => array_merge(array(
  135. array('key' => '', 'value' => _t('_bx_payment_txt_all_authors'))
  136. ), $aValues)
  137. ));
  138. }
  139. protected function _getFilterModules()
  140. {
  141. $aModules = $this->_oModule->_oDb->getModules();
  142. $aValues = array();
  143. foreach($aModules as $aModule) {
  144. $sLangKey = '_' . $aModule['name'];
  145. $sLangValue = _t($sLangKey);
  146. $aValues[] = array('key' => $aModule['id'], 'value' => $sLangKey != $sLangValue ? $sLangValue : $aModule['title']);
  147. }
  148. $this->_oModule->_oConfig->sortByColumn('value', $aValues);
  149. return $this->_getFilterSelectAll('module', array(
  150. 'js_onchange' => 'onChangeFilterModule(this, ' . $this->_aQueryAppend['seller_id'] . ')',
  151. 'values' => array_merge(array(
  152. array('key' => '', 'value' => _t('_bx_payment_txt_all_modules'))
  153. ), $aValues)
  154. ));
  155. }
  156. protected function _getFilterItems()
  157. {
  158. return $this->_getFilterSelectAll('item', array(
  159. 'attrs' => array('disabled' => 'disabled'),
  160. 'values' => array(
  161. array('key' => '', 'value' => _t('_bx_payment_txt_all_items'))
  162. )
  163. ));
  164. }
  165. protected function _getFilterControls ()
  166. {
  167. parent::_getFilterControls();
  168. $sResult = $this->_getFilterClients();
  169. if($this->_bSingleSeller && $this->_aQueryAppend['seller_id'] == $this->_iSingleSeller)
  170. $sResult .= $this->_getFilterAuthors();
  171. $sResult .= $this->_getFilterModules() . $this->_getFilterItems() . $this->_getSearchInput();
  172. return $sResult;
  173. }
  174. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  175. {
  176. if(empty($this->_aQueryAppend['seller_id']))
  177. return array();
  178. $sParamsDivider = $this->_oModule->_oConfig->getDivider('DIVIDER_GRID_FILTERS');
  179. $iClient = $iAuthor = $iModule = $iItem = 0;
  180. if(strpos($sFilter, $sParamsDivider) !== false) {
  181. $aFilters = explode($sParamsDivider, $sFilter);
  182. if($this->_bSingleSeller && $this->_aQueryAppend['seller_id'] = $this->_iSingleSeller)
  183. list($iClient, $iAuthor, $iModule, $iItem, $sFilter) = $aFilters;
  184. else
  185. list($iClient, $iModule, $iItem, $sFilter) = $aFilters;
  186. }
  187. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND (`tt`.`seller_id`=? OR `tt`.`author_id`=?)", $this->_aQueryAppend['seller_id'], $this->_aQueryAppend['seller_id']);
  188. $iClient = (int)$iClient;
  189. if($iClient != 0)
  190. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`client_id`=?", $iClient);
  191. $iAuthor = (int)$iAuthor;
  192. if($iAuthor != 0)
  193. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`author_id`=?", $iAuthor);
  194. $iModule = (int)$iModule;
  195. if($iModule != 0)
  196. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`module_id`=?", $iModule);
  197. $iItem = (int)$iItem;
  198. if($iItem != 0)
  199. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`item_id`=?", $iItem);
  200. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  201. }
  202. }
  203. /** @} */