CgRefundController.php 7.6 KB

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