CgRefundController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\cg\classes\CgRefundClass;
  4. use bizGhs\cg\classes\CgRefundItemClass;
  5. use bizGhs\cg\services\CgRefundService;
  6. use bizGhs\order\classes\PurchaseOrderClass;
  7. use common\components\dateUtil;
  8. use common\components\imgUtil;
  9. use common\components\util;
  10. use Yii;
  11. class CgRefundController extends BaseController
  12. {
  13. //详情 ssh 20220910
  14. public function actionGetRefundFullInfo()
  15. {
  16. $get = Yii::$app->request->get();
  17. $id = $get['id'] ?? 0;
  18. $refund = CgRefundClass::getById($id, true);
  19. CgRefundClass::valid($refund, $this->mainId);
  20. $refundSn = $refund->refundSn ?? '';
  21. $itemList = CgRefundItemClass::getAllByCondition(['refundSn' => $refundSn], null, '*');
  22. if (!empty($itemList)) {
  23. foreach ($itemList as $key => $item) {
  24. $shortCover = $item['cover'] ?? '';
  25. $itemList[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  26. }
  27. }
  28. util::success(['info' => $refund, 'itemList' => $itemList]);
  29. }
  30. //记录,有重复方法,需要同步修改cg_refund_list ssh 20240817
  31. public function actionGetRefundList()
  32. {
  33. $get = Yii::$app->request->get();
  34. $where = [];
  35. $where['mainId'] = $this->mainId;
  36. $relateOrderSn = $get['relateOrderSn'] ?? '';
  37. if (!empty($relateOrderSn)) {
  38. $where['relateOrderSn'] = $relateOrderSn;
  39. }
  40. $list = CgRefundClass::getRefundList($where);
  41. util::success($list);
  42. }
  43. //记录,有重复方法,需要同步修改cg_refund_list ssh 20240817
  44. public function actionList()
  45. {
  46. $get = Yii::$app->request->get();
  47. $where = [];
  48. $where['mainId'] = $this->mainId;
  49. $relateOrderSn = $get['relateOrderSn'] ?? '';
  50. if (!empty($relateOrderSn)) {
  51. $where['relateOrderSn'] = $relateOrderSn;
  52. }
  53. $searchTime = $get['searchTime'] ?? '';
  54. if (!empty($searchTime)) {
  55. $startTime = $get['startTime'] ?? '';
  56. $endTime = $get['endTime'] ?? '';
  57. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  58. $where['orderTime'] = ['between', [$period['startTime'], $period['endTime']]];
  59. }
  60. $refundSearchTime = $get['refundSearchTime'] ?? '';
  61. if (!empty($refundSearchTime)) {
  62. $startTime = $get['refundStartTime'] ?? '';
  63. $endTime = $get['refundEndTime'] ?? '';
  64. $period = dateUtil::formatTime($refundSearchTime, $startTime, $endTime);
  65. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  66. }
  67. $ghsId = $get['ghsId'] ?? '';
  68. if (!empty($ghsId)) {
  69. $where['ghsId'] = $ghsId;
  70. }
  71. $shopAdminId = $get['shopAdminId'] ?? 0;
  72. if (!empty($shopAdminId)) {
  73. $where['shopAdminId'] = $shopAdminId;
  74. }
  75. $status = $get['status'] ?? -1;
  76. if ($status > -1) {
  77. $where['status'] = $status;
  78. }
  79. $export = $get['export'] ?? 0;
  80. if ($export == 1) {
  81. if (isset($where['orderTime']) == false && isset($where['addTime']) == false) {
  82. util::fail('请选时间');
  83. }
  84. ini_set('memory_limit', '2045M');
  85. set_time_limit(0);
  86. if (isset($where['orderTime'])) {
  87. $start = $where['orderTime'][1][0];
  88. $end = $where['orderTime'][1][1];
  89. $monthTime = bcmul(31, 86400);
  90. $startArea = strtotime($start);
  91. $endArea = strtotime($end);
  92. $diff = bcsub($endArea, $startArea);
  93. if ($diff > $monthTime) {
  94. util::fail('最长可导出一个月');
  95. }
  96. }
  97. if (isset($where['addTime'])) {
  98. $start = $where['addTime'][1][0];
  99. $end = $where['addTime'][1][1];
  100. $monthTime = bcmul(31, 86400);
  101. $startArea = strtotime($start);
  102. $endArea = strtotime($end);
  103. $diff = bcsub($endArea, $startArea);
  104. if ($diff > $monthTime) {
  105. util::fail('最长可导出一个月');
  106. }
  107. }
  108. }
  109. $respond = CgRefundClass::getRefundList($where);
  110. if ($export == 1) {
  111. CgRefundClass::exportRefundData($respond, $this->mainId);
  112. }
  113. util::success($respond);
  114. }
  115. //退款 ssh 20220909
  116. public function actionCreateOrder()
  117. {
  118. $shopAdmin = $this->shopAdmin;
  119. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  120. util::fail('超管才能操作退款');
  121. }
  122. $connection = Yii::$app->db;
  123. $transaction = $connection->beginTransaction();
  124. try {
  125. $post = Yii::$app->request->post();
  126. $id = isset($post['id']) ? $post['id'] : 0;
  127. $order = PurchaseOrderClass::getLockById($id);
  128. if ($order->mainId != $this->mainId) {
  129. util::fail('没有权限');
  130. }
  131. if ($order->status != PurchaseOrderClass::PURCHASE_ORDER_STATUS_COMPLETE) {
  132. util::fail('已完成订单才能申请退货退款');
  133. }
  134. if ($order->debt != 1) {
  135. util::fail('欠款单才能申请退货退款');
  136. }
  137. if (getenv('YII_ENV') == 'production') {
  138. if ($this->mainId == 23390) {
  139. //小向花卉售后必须备注
  140. $remark = $post['remark'] ?? '';
  141. if (empty($remark)) {
  142. util::fail('请填写备注哈');
  143. }
  144. }
  145. }
  146. //退款类型 1退货并退款 2 仅退款
  147. $refundType = $post['refundType'] ?? 1;
  148. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  149. //花材列表结构
  150. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  151. $productJson = $post['product'] ?? '';
  152. if (empty($productJson)) {
  153. util::fail('请选择花材2');
  154. }
  155. $productList = json_decode($productJson, true);
  156. if (!is_array($productList)) {
  157. util::fail('请选择花材哦9');
  158. }
  159. $post['product'] = $productList;
  160. } else {
  161. //仅退款花材直接设置为空
  162. $post['product'] = [];
  163. }
  164. $post['price'] = $post['price'] ?? 0;
  165. if ($post['price'] <= 0) {
  166. util::fail("退款金额不能小于0");
  167. }
  168. if (bccomp($post['price'], $order->realPrice, 2) == 1) {
  169. util::fail("退款金额超过订单金额");
  170. }
  171. $post['shopId'] = $this->shopId;
  172. $post['shop'] = $this->shop;
  173. $post['sjId'] = $this->sjId;
  174. $post['shopAdminId'] = $this->shopAdminId;
  175. $post['mainId'] = $this->mainId;
  176. $shopAdmin = $this->shopAdmin;
  177. $adminName = $shopAdmin['name'] ?? '';
  178. $post['shopAdminName'] = $adminName;
  179. CgRefundService::addRefund($order, $post);
  180. $transaction->commit();
  181. } catch (\Exception $exception) {
  182. $transaction->rollBack();
  183. Yii::info("退款申请出错了,报错信息:" . $exception->getMessage());
  184. util::fail('操作失败');
  185. }
  186. util::complete();
  187. }
  188. }