CgRefundController.php 8.2 KB

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