BxPaymentGridProcessed.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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('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 _getFilterClients()
  103. {
  104. $aIds = $this->_oModule->_oDb->getOrderProcessed(array('type' => 'clients', 'seller_id' => $this->_aQueryAppend['seller_id']));
  105. $aValues = array(
  106. array('key' => '', 'value' => _t('_bx_payment_txt_all_clients'))
  107. );
  108. foreach($aIds as $iId)
  109. if(($oClient = BxDolProfile::getInstanceMagic($iId)) !== false)
  110. $aValues[] = array('key' => $iId, 'value' => $oClient->getDisplayName());
  111. $this->_oModule->_oConfig->sortByColumn('value', $aValues);
  112. return $this->_getFilterSelectAll('client', array(
  113. 'values' => $aValues
  114. ));
  115. }
  116. protected function _getFilterModules()
  117. {
  118. $aModules = $this->_oModule->_oDb->getModules();
  119. $aValues = array(
  120. array('key' => '', 'value' => _t('_bx_payment_txt_all_modules'))
  121. );
  122. foreach($aModules as $aModule) {
  123. $sLangKey = '_' . $aModule['name'];
  124. $sLangValue = _t($sLangKey);
  125. $aValues[] = array('key' => $aModule['id'], 'value' => $sLangKey != $sLangValue ? $sLangValue : $aModule['title']);
  126. }
  127. $this->_oModule->_oConfig->sortByColumn('value', $aValues);
  128. return $this->_getFilterSelectAll('module', array(
  129. 'js_onchange' => 'onChangeFilterModule(this, ' . $this->_aQueryAppend['seller_id'] . ')',
  130. 'values' => $aValues
  131. ));
  132. }
  133. protected function _getFilterItems()
  134. {
  135. $aValues = array(
  136. array('key' => '', 'value' => _t('_bx_payment_txt_all_items'))
  137. );
  138. return $this->_getFilterSelectAll('item', array(
  139. 'attrs' => array('disabled' => 'disabled'),
  140. 'values' => $aValues
  141. ));
  142. }
  143. protected function _getFilterControls ()
  144. {
  145. parent::_getFilterControls();
  146. return $this->_getFilterClients() . $this->_getFilterModules() . $this->_getFilterItems() . $this->_getSearchInput();
  147. }
  148. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  149. {
  150. if(empty($this->_aQueryAppend['seller_id']))
  151. return array();
  152. $sParamsDivider = $this->_oModule->_oConfig->getDivider('DIVIDER_GRID_FILTERS');
  153. $iClient = $iModule = $iItem = 0;
  154. if(strpos($sFilter, $sParamsDivider) !== false)
  155. list($iClient, $iModule, $iItem, $sFilter) = explode($sParamsDivider, $sFilter);
  156. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`seller_id`=?", $this->_aQueryAppend['seller_id']);
  157. $iClient = (int)$iClient;
  158. if($iClient != 0)
  159. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`client_id`=?", $iClient);
  160. $iModule = (int)$iModule;
  161. if($iModule != 0)
  162. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`module_id`=?", $iModule);
  163. $iItem = (int)$iItem;
  164. if($iItem != 0)
  165. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tt`.`item_id`=?", $iItem);
  166. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  167. }
  168. }
  169. /** @} */