panel.ajax.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls.
  5. *
  6. * @package chamilo.plugin.buycourses
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../../../main/inc/global.inc.php';
  10. api_protect_admin_script(true);
  11. $plugin = BuyCoursesPlugin::create();
  12. $paypalEnable = $plugin->get('paypal_enable');
  13. $commissionsEnable = $plugin->get('commissions_enable');
  14. $action = isset($_GET['a']) ? $_GET['a'] : null;
  15. switch ($action) {
  16. case 'saleInfo':
  17. //$saleId is only used in getSale() and is always filtered there
  18. $saleId = isset($_POST['id']) ? $_POST['id'] : '';
  19. $sale = $plugin->getSale($saleId);
  20. $productType = ($sale['product_type'] == 1) ? get_lang('Course') : get_lang('Session');
  21. $paymentType = ($sale['payment_type'] == 1) ? 'Paypal' : $plugin->get_lang('BankTransfer');
  22. $productInfo = ($sale['product_type'] == 1)
  23. ? api_get_course_info_by_id($sale['product_id'])
  24. : api_get_session_info($sale['product_id']);
  25. $currency = $plugin->getSelectedCurrency();
  26. if ($sale['product_type'] == 1) {
  27. $productImage = $productInfo['course_image_large'];
  28. } else {
  29. $productImage = ($productInfo['image'])
  30. ? $productInfo['image']
  31. : Template::get_icon_path('session_default.png');
  32. }
  33. $userInfo = api_get_user_info($sale['user_id']);
  34. $html = '<h2>'.$sale['product_name'].'</h2>';
  35. $html .= '<div class="row">';
  36. $html .= '<div class="col-sm-6 col-md-6">';
  37. $html .= '<ul>';
  38. $html .= '<li><b>'.$plugin->get_lang('OrderPrice').':</b> '.$sale['price'].'</li>';
  39. $html .= '<li><b>'.$plugin->get_lang('CurrencyType').':</b> '.$currency['iso_code'].'</li>';
  40. $html .= '<li><b>'.$plugin->get_lang('ProductType').':</b> '.$productType.'</li>';
  41. $html .= '<li><b>'.$plugin->get_lang('OrderDate').':</b> '
  42. .api_format_date($sale['date'], DATE_TIME_FORMAT_LONG_24H).'</li>';
  43. $html .= '<li><b>'.$plugin->get_lang('Buyer').':</b> '.$userInfo['complete_name'].'</li>';
  44. $html .= '<li><b>'.$plugin->get_lang('PaymentMethods').':</b> '.$paymentType.'</li>';
  45. $html .= '</ul>';
  46. $html .= '</div>';
  47. $html .= '<div class="col-sm-6 col-md-6">';
  48. $html .= '<img class="thumbnail" src="'.$productImage.'" >';
  49. $html .= '</div>';
  50. $html .= '</div>';
  51. echo $html;
  52. break;
  53. case 'stats':
  54. $stats = [];
  55. $stats['completed_count'] = 0;
  56. $stats['completed_total_amount'] = 0;
  57. $stats['pending_count'] = 0;
  58. $stats['pending_total_amount'] = 0;
  59. $stats['canceled_count'] = 0;
  60. $stats['canceled_total_amount'] = 0;
  61. $completedPayouts = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_COMPLETED);
  62. $pendingPayouts = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_PENDING);
  63. $canceledPayouts = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_CANCELED);
  64. $currency = $plugin->getSelectedCurrency();
  65. foreach ($completedPayouts as $completed) {
  66. $stats['completed_count'] = count($completedPayouts);
  67. $stats['completed_total_amount'] += $completed['commission'];
  68. $stats['completed_total_amount'] = number_format($stats['completed_total_amount'], 2);
  69. }
  70. foreach ($pendingPayouts as $pending) {
  71. $stats['pending_count'] = count($pendingPayouts);
  72. $stats['pending_total_amount'] += $pending['commission'];
  73. $stats['pending_total_amount'] = number_format($stats['pending_total_amount'], 2);
  74. }
  75. foreach ($canceledPayouts as $canceled) {
  76. $stats['canceled_count'] = count($canceledPayouts);
  77. $stats['canceled_total_amount'] += $canceled['commission'];
  78. $stats['canceled_total_amount'] = number_format($stats['canceled_total_amount'], 2);
  79. }
  80. $html = '
  81. <div class="row">
  82. <p>
  83. <ul>
  84. <li>
  85. '.get_plugin_lang("PayoutsTotalCompleted", "BuyCoursesPlugin").'
  86. <b>'.$stats['completed_count'].'</b> - '.get_plugin_lang("TotalAmount", "BuyCoursesPlugin").'
  87. <b>'.$stats['completed_total_amount'].' '.$currency['iso_code'].'</b>
  88. </li>
  89. <li>'.get_plugin_lang("PayoutsTotalPending", "BuyCoursesPlugin").'
  90. <b>'.$stats['pending_count'].'</b> - '.get_plugin_lang("TotalAmount", "BuyCoursesPlugin").'
  91. <b>'.$stats['pending_total_amount'].' '.$currency['iso_code'].'</b>
  92. </li>
  93. <li>'.get_plugin_lang("PayoutsTotalCanceled", "BuyCoursesPlugin").'
  94. <b>'.$stats['canceled_count'].'</b> - '.get_plugin_lang("TotalAmount", "BuyCoursesPlugin").'
  95. <b>'.$stats['canceled_total_amount'].' '.$currency['iso_code'].'</b>
  96. </li>
  97. </ul>
  98. </p>
  99. </div>
  100. ';
  101. echo $html;
  102. break;
  103. case 'processPayout':
  104. if (api_is_anonymous()) {
  105. break;
  106. }
  107. $html = '';
  108. $allPays = [];
  109. $totalAccounts = 0;
  110. $totalPayout = 0;
  111. $payouts = isset($_POST['payouts']) ? $_POST['payouts'] : '';
  112. if (!$payouts) {
  113. echo Display::return_message(get_plugin_lang("SelectOptionToProceed", "BuyCoursesPlugin"), 'error', false);
  114. break;
  115. }
  116. foreach ($payouts as $index => $id) {
  117. $allPays[] = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_PENDING, $id);
  118. }
  119. foreach ($allPays as $payout) {
  120. $totalPayout += number_format($payout['commission'], 2);
  121. $totalAccounts++;
  122. }
  123. $currentCurrency = $plugin->getSelectedCurrency();
  124. $isoCode = $currentCurrency['iso_code'];
  125. $html .= '<p>'.get_plugin_lang("VerifyTotalAmountToProceedPayout", "BuyCoursesPlugin").'</p>';
  126. $html .= '
  127. <p>
  128. <ul>
  129. <li>'.get_plugin_lang("TotalAcounts", "BuyCoursesPlugin").' <b>'.$totalAccounts.'</b></li>
  130. <li>'.get_plugin_lang("TotalPayout", "BuyCoursesPlugin").' <b>'.$isoCode.' '.$totalPayout.'</b></li>
  131. </ul>
  132. </p>
  133. <p>'.get_plugin_lang("CautionThisProcessCantBeCanceled", "BuyCoursesPlugin").'</p>
  134. <br /><br />
  135. <div id="spinner" class="text-center"></div>
  136. ';
  137. echo $html;
  138. break;
  139. case 'proceedPayout':
  140. if (api_is_anonymous()) {
  141. break;
  142. }
  143. $paypalParams = $plugin->getPaypalParams();
  144. $pruebas = $paypalParams['sandbox'] == 1;
  145. $paypalUsername = $paypalParams['username'];
  146. $paypalPassword = $paypalParams['password'];
  147. $paypalSignature = $paypalParams['signature'];
  148. require_once "paypalfunctions.php";
  149. $allPayouts = [];
  150. $totalAccounts = 0;
  151. $totalPayout = 0;
  152. $payouts = isset($_POST['payouts']) ? $_POST['payouts'] : '';
  153. if (!$payouts) {
  154. echo Display::return_message(get_plugin_lang("SelectOptionToProceed", "BuyCoursesPlugin"), 'error', false);
  155. break;
  156. }
  157. foreach ($payouts as $index => $id) {
  158. $allPayouts[] = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_PENDING, $id);
  159. }
  160. $currentCurrency = $plugin->getSelectedCurrency();
  161. $isoCode = $currentCurrency['iso_code'];
  162. $result = MassPayment($allPayouts, $isoCode);
  163. if ($result['ACK'] === 'Success') {
  164. foreach ($allPayouts as $payout) {
  165. $plugin->setStatusPayouts($payout['id'], BuyCoursesPlugin::PAYOUT_STATUS_COMPLETED);
  166. }
  167. echo Display::return_message(get_plugin_lang("PayoutSuccess", "BuyCoursesPlugin"), 'success', false);
  168. } else {
  169. echo Display::return_message(
  170. '<b>'.$result['L_SEVERITYCODE0'].' '.$result['L_ERRORCODE0'].'</b> - '
  171. .$result['L_SHORTMESSAGE0'].'<br /><ul><li>'.$result['L_LONGMESSAGE0'].'</li></ul>',
  172. 'error',
  173. false
  174. );
  175. }
  176. break;
  177. case 'cancelPayout':
  178. if (api_is_anonymous()) {
  179. break;
  180. }
  181. $payoutId = isset($_POST['id']) ? $_POST['id'] : '';
  182. $plugin->setStatusPayouts($payoutId, BuyCoursesPlugin::PAYOUT_STATUS_CANCELED);
  183. echo '';
  184. break;
  185. }
  186. exit;