CgRefundController.php 8.0 KB

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