CgRefundController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_COMPLETE) {
  52. util::fail("完成的订单才能申请退货退款");
  53. }
  54. $connection = Yii::$app->db;
  55. $transaction = $connection->beginTransaction();
  56. try {
  57. //退款类型 1退货并退款 2 仅退款
  58. $refundType = $post['refundType'] ?? 1;
  59. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  60. //花材列表结构
  61. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  62. $productJson = $post['product'] ?? '';
  63. if (empty($productJson)) {
  64. util::fail('请选择要退款的花材');
  65. }
  66. $productList = json_decode($productJson, true);
  67. if (!is_array($productList)) {
  68. util::fail('请选择要退款的花材哦!');
  69. }
  70. $post['product'] = $productList;
  71. } else {
  72. //仅退款花材直接设置为空
  73. $post['product'] = [];
  74. }
  75. $post['price'] = $post['price'] ?? 0;
  76. if ($post['price'] <= 0) {
  77. util::fail("退款金额不能小于0");
  78. }
  79. if (bccomp($post['price'], $cg->realPrice, 2) == 1) {
  80. util::fail("退款金额超过订单金额");
  81. }
  82. $post['shopId'] = $this->shopId;
  83. $post['sjId'] = $this->sjId;
  84. $post['shopAdminId'] = $this->shopAdminId;
  85. $post['mainId'] = $this->mainId;
  86. $shopAdmin = $this->shopAdmin;
  87. $adminName = $shopAdmin['name'] ?? '';
  88. $post['shopAdminName'] = $adminName;
  89. $respond = CgRefundService::createOrder($post, $cg);
  90. $transaction->commit();
  91. $saleRefundId = $respond->saleRefundId ?? 0;
  92. $saleRefund = RefundOrderClass::getById($saleRefundId, true);
  93. if (!empty($order) && !empty($saleRefund)) {
  94. NotifyClass::addRefundApplyNotify($saleRefund);
  95. }
  96. util::success($respond);
  97. } catch (\Exception $exception) {
  98. $transaction->rollBack();
  99. Yii::info("申请出错了,报错信息:" . $exception->getMessage());
  100. util::fail('申请失败');
  101. }
  102. }
  103. //列表
  104. public function actionList()
  105. {
  106. $get = Yii::$app->request->get();
  107. $where = [];
  108. $cgId = $get['cgId'] ?? 0;
  109. if (!empty($cgId)) {
  110. $where['cgId'] = $cgId;
  111. }
  112. $list = CgRefundClass::getRefundList($where);
  113. util::success($list);
  114. }
  115. //根据采购单查退款信息 ssh 20210908
  116. public function actionGetRefundData()
  117. {
  118. $id = Yii::$app->request->get('id', 0);
  119. $refund = CgRefundClass::getById($id);
  120. if (empty($refund)) {
  121. util::fail('没有找到退款信息');
  122. }
  123. $imgString = $refund['imgList'] ?? '';
  124. if (!empty($imgString)) {
  125. $imgArr = json_decode($imgString, true);
  126. $smallImgList = [];
  127. $bigImgList = [];
  128. if (!empty($imgArr)) {
  129. foreach ($imgArr as $image) {
  130. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  131. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  132. $smallImgList[] = $smallImg;
  133. $bigImgList[] = $bigImg;
  134. }
  135. }
  136. $refund['imgList'] = $imgArr;
  137. $refund['smallImgList'] = $smallImgList;
  138. $refund['bigImgList'] = $bigImgList;
  139. }
  140. //未登录用户也需要查看,暂时先注释
  141. //CgRefundClass::valid($refund, $this->mainId);
  142. $cgId = $refund['cgId'] ?? 0;
  143. $cg = PurchaseClass::getById($cgId);
  144. if (empty($refund)) {
  145. util::fail('没有退款信息');
  146. }
  147. $orderSn = $refund['orderSn'] ?? '';
  148. $cgItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  149. if (!empty($cgItemList)) {
  150. foreach ($cgItemList as $key => $item) {
  151. $cover = $item['cover'] ?? '';
  152. $shortCover = $cover;
  153. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  154. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  155. $cgItemList[$key]['cover'] = $cover;
  156. $cgItemList[$key]['bigCover'] = $bigCover;
  157. $cgItemList[$key]['shortCover'] = $shortCover;
  158. $cgItemList[$key]['itemNum'] = isset($item['itemNum']) ? floatval($item['itemNum']) : 0;
  159. }
  160. }
  161. util::success(['info' => $refund, 'itemList' => $cgItemList, 'cgInfo' => $cg]);
  162. }
  163. //根据退款id查退款信息 ssh 20210809
  164. public function actionGetInfo()
  165. {
  166. $id = Yii::$app->request->get('id', 0);
  167. }
  168. }