CgRefundController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace hd\controllers;
  3. use bizGhs\notify\classes\NotifyClass;
  4. use bizGhs\order\classes\RefundOrderClass;
  5. use bizHd\cg\classes\CgRefundClass;
  6. use bizHd\cg\classes\CgRefundItemClass;
  7. use bizHd\cg\services\CgRefundService;
  8. use bizHd\purchase\classes\PurchaseClass;
  9. use common\components\imgUtil;
  10. use Yii;
  11. use common\components\util;
  12. class CgRefundController extends BaseController
  13. {
  14. public $guestAccess = ['list', 'get-refund-data'];
  15. //取消退款申请 ssh 20230808
  16. public function actionCancelRefund()
  17. {
  18. $get = Yii::$app->request->get();
  19. $id = isset($get['id']) ? $get['id'] : 0;
  20. $refund = CgRefundClass::getById($id, true);
  21. if (empty($refund)) {
  22. util::fail('没有找到退款单');
  23. }
  24. if ($refund->mainId != $this->mainId) {
  25. util::fail('没有权限操作');
  26. }
  27. $connection = Yii::$app->db;
  28. $transaction = $connection->beginTransaction();
  29. try {
  30. CgRefundService::cancelRefundApply($refund);
  31. $transaction->commit();
  32. util::complete('取消成功');
  33. } catch (\Exception $exception) {
  34. $transaction->rollBack();
  35. Yii::info("取消出错了,报错信息:" . $exception->getMessage());
  36. util::fail('取消失败');
  37. }
  38. }
  39. //申请退款 ssh 20230807
  40. public function actionCreateRefund()
  41. {
  42. $post = Yii::$app->request->post();
  43. $id = isset($post['id']) ? $post['id'] : 0;
  44. $cg = PurchaseClass::getById($id, true);
  45. if (empty($cg)) {
  46. util::fail('没有找到采购单');
  47. }
  48. if ($cg->mainId != $this->mainId) {
  49. util::fail('没有权限操作');
  50. }
  51. if ($cg->status == PurchaseClass::STATUS_UN_PAY) {
  52. util::fail("待付款订单,不能申请售后");
  53. }
  54. if ($cg->status == PurchaseClass::STATUS_UN_SEND) {
  55. util::fail("待发货订单,不能申请售后");
  56. }
  57. if ($cg->status == PurchaseClass::STATUS_SENDING) {
  58. util::fail("订单配送中,不能申请售后");
  59. }
  60. if ($cg->status == PurchaseClass::STATUS_CANCEL) {
  61. util::fail("订单已取消,不能申请售后");
  62. }
  63. if (isset($cg->clearId) && !empty($cg->clearId)) {
  64. util::fail('订单已结,不能再申请售后');
  65. }
  66. $connection = Yii::$app->db;
  67. $transaction = $connection->beginTransaction();
  68. try {
  69. //退款类型 1退货并退款 2 仅退款
  70. $refundType = $post['refundType'] ?? 1;
  71. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  72. //花材列表结构
  73. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  74. $productJson = $post['product'] ?? '';
  75. if (empty($productJson)) {
  76. util::fail('请选择要退款的花材');
  77. }
  78. $productList = json_decode($productJson, true);
  79. if (!is_array($productList)) {
  80. util::fail('请选择要退款的花材哦!');
  81. }
  82. $post['product'] = $productList;
  83. } else {
  84. //仅退款花材直接设置为空
  85. $post['product'] = [];
  86. }
  87. $post['price'] = $post['price'] ?? 0;
  88. if ($post['price'] <= 0) {
  89. util::fail("退款金额不能小于0");
  90. }
  91. if (bccomp($post['price'], $cg->realPrice, 2) == 1) {
  92. util::fail("退款金额超过订单金额");
  93. }
  94. $post['shopId'] = $this->shopId;
  95. $post['sjId'] = $this->sjId;
  96. $post['shopAdminId'] = $this->shopAdminId;
  97. $post['mainId'] = $this->mainId;
  98. $shopAdmin = $this->shopAdmin;
  99. $adminName = $shopAdmin['name'] ?? '';
  100. $post['shopAdminName'] = $adminName;
  101. $respond = CgRefundService::createOrder($post, $cg);
  102. $transaction->commit();
  103. $saleRefundId = $respond->saleRefundId ?? 0;
  104. $saleRefund = RefundOrderClass::getById($saleRefundId, true);
  105. if (!empty($saleRefund)) {
  106. NotifyClass::addRefundApplyNotify($saleRefund);
  107. }
  108. util::success($respond);
  109. } catch (\Exception $exception) {
  110. $transaction->rollBack();
  111. Yii::info("申请出错了,报错信息:" . $exception->getMessage());
  112. util::fail('申请失败');
  113. }
  114. }
  115. //列表
  116. public function actionList()
  117. {
  118. $get = Yii::$app->request->get();
  119. $where = [];
  120. $cgId = $get['cgId'] ?? 0;
  121. if (!empty($cgId)) {
  122. $where['cgId'] = $cgId;
  123. }
  124. $list = CgRefundClass::getRefundList($where);
  125. util::success($list);
  126. }
  127. //根据采购单查退款信息 ssh 20210908
  128. public function actionGetRefundData()
  129. {
  130. $id = Yii::$app->request->get('id', 0);
  131. $refund = CgRefundClass::getById($id);
  132. if (empty($refund)) {
  133. util::fail('没有找到退款信息');
  134. }
  135. $imgString = $refund['imgList'] ?? '';
  136. if (!empty($imgString)) {
  137. $imgArr = json_decode($imgString, true);
  138. $smallImgList = [];
  139. $bigImgList = [];
  140. if (!empty($imgArr)) {
  141. foreach ($imgArr as $image) {
  142. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  143. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  144. $smallImgList[] = $smallImg;
  145. $bigImgList[] = $bigImg;
  146. }
  147. }
  148. $refund['imgList'] = $imgArr;
  149. $refund['smallImgList'] = $smallImgList;
  150. $refund['bigImgList'] = $bigImgList;
  151. }
  152. //未登录用户也需要查看,暂时先注释
  153. //CgRefundClass::valid($refund, $this->mainId);
  154. $cgId = $refund['cgId'] ?? 0;
  155. $cg = PurchaseClass::getById($cgId);
  156. if (empty($refund)) {
  157. util::fail('没有退款信息');
  158. }
  159. $orderSn = $refund['orderSn'] ?? '';
  160. $cgItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  161. if (!empty($cgItemList)) {
  162. foreach ($cgItemList as $key => $item) {
  163. $cover = $item['cover'] ?? '';
  164. $shortCover = $cover;
  165. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  166. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  167. $cgItemList[$key]['cover'] = $cover;
  168. $cgItemList[$key]['bigCover'] = $bigCover;
  169. $cgItemList[$key]['shortCover'] = $shortCover;
  170. $cgItemList[$key]['itemNum'] = isset($item['itemNum']) ? floatval($item['itemNum']) : 0;
  171. }
  172. }
  173. util::success(['info' => $refund, 'itemList' => $cgItemList, 'cgInfo' => $cg]);
  174. }
  175. //根据退款id查退款信息 ssh 20210809
  176. public function actionGetInfo()
  177. {
  178. $id = Yii::$app->request->get('id', 0);
  179. }
  180. }